1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "ApplicationAccessible.h"
9
10 #include "AccAttributes.h"
11 #include "LocalAccessible-inl.h"
12 #include "nsAccessibilityService.h"
13 #include "nsAccUtils.h"
14 #include "Relation.h"
15 #include "Role.h"
16 #include "States.h"
17
18 #include "nsServiceManagerUtils.h"
19 #include "mozilla/dom/Document.h"
20 #include "mozilla/Components.h"
21 #include "nsGlobalWindow.h"
22 #include "nsIStringBundle.h"
23
24 using namespace mozilla::a11y;
25
ApplicationAccessible()26 ApplicationAccessible::ApplicationAccessible()
27 : AccessibleWrap(nullptr, nullptr) {
28 mType = eApplicationType;
29 mAppInfo = do_GetService("@mozilla.org/xre/app-info;1");
30 MOZ_ASSERT(mAppInfo, "no application info");
31 }
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // nsIAccessible
35
Name(nsString & aName) const36 ENameValueFlag ApplicationAccessible::Name(nsString& aName) const {
37 aName.Truncate();
38
39 nsCOMPtr<nsIStringBundleService> bundleService =
40 mozilla::components::StringBundle::Service();
41
42 NS_ASSERTION(bundleService, "String bundle service must be present!");
43 if (!bundleService) return eNameOK;
44
45 nsCOMPtr<nsIStringBundle> bundle;
46 nsresult rv = bundleService->CreateBundle(
47 "chrome://branding/locale/brand.properties", getter_AddRefs(bundle));
48 if (NS_FAILED(rv)) return eNameOK;
49
50 nsAutoString appName;
51 rv = bundle->GetStringFromName("brandShortName", appName);
52 if (NS_FAILED(rv) || appName.IsEmpty()) {
53 NS_WARNING("brandShortName not found, using default app name");
54 appName.AssignLiteral("Gecko based application");
55 }
56
57 aName.Assign(appName);
58 return eNameOK;
59 }
60
Description(nsString & aDescription) const61 void ApplicationAccessible::Description(nsString& aDescription) const {
62 aDescription.Truncate();
63 }
64
Value(nsString & aValue) const65 void ApplicationAccessible::Value(nsString& aValue) const { aValue.Truncate(); }
66
State()67 uint64_t ApplicationAccessible::State() {
68 return IsDefunct() ? states::DEFUNCT : 0;
69 }
70
NativeAttributes()71 already_AddRefed<AccAttributes> ApplicationAccessible::NativeAttributes() {
72 RefPtr<AccAttributes> attributes = new AccAttributes();
73 return attributes.forget();
74 }
75
GroupPosition()76 GroupPos ApplicationAccessible::GroupPosition() { return GroupPos(); }
77
LocalChildAtPoint(int32_t aX,int32_t aY,EWhichChildAtPoint aWhichChild)78 LocalAccessible* ApplicationAccessible::LocalChildAtPoint(
79 int32_t aX, int32_t aY, EWhichChildAtPoint aWhichChild) {
80 return nullptr;
81 }
82
FocusedChild()83 LocalAccessible* ApplicationAccessible::FocusedChild() {
84 LocalAccessible* focus = FocusMgr()->FocusedAccessible();
85 if (focus && focus->LocalParent() == this) {
86 return focus;
87 }
88
89 return nullptr;
90 }
91
RelationByType(RelationType aRelationType) const92 Relation ApplicationAccessible::RelationByType(
93 RelationType aRelationType) const {
94 return Relation();
95 }
96
Bounds() const97 mozilla::LayoutDeviceIntRect ApplicationAccessible::Bounds() const {
98 return mozilla::LayoutDeviceIntRect();
99 }
100
BoundsInAppUnits() const101 nsRect ApplicationAccessible::BoundsInAppUnits() const { return nsRect(); }
102
103 ////////////////////////////////////////////////////////////////////////////////
104 // LocalAccessible public methods
105
Shutdown()106 void ApplicationAccessible::Shutdown() { mAppInfo = nullptr; }
107
ApplyARIAState(uint64_t * aState) const108 void ApplicationAccessible::ApplyARIAState(uint64_t* aState) const {}
109
NativeRole() const110 role ApplicationAccessible::NativeRole() const { return roles::APP_ROOT; }
111
NativeState() const112 uint64_t ApplicationAccessible::NativeState() const { return 0; }
113
AccessKey() const114 KeyBinding ApplicationAccessible::AccessKey() const { return KeyBinding(); }
115
Init()116 void ApplicationAccessible::Init() {
117 // Basically children are kept updated by Append/RemoveChild method calls.
118 // However if there are open windows before accessibility was started
119 // then we need to make sure root accessibles for open windows are created so
120 // that all root accessibles are stored in application accessible children
121 // array.
122
123 nsGlobalWindowOuter::OuterWindowByIdTable* windowsById =
124 nsGlobalWindowOuter::GetWindowsTable();
125
126 if (!windowsById) {
127 return;
128 }
129
130 for (const auto& window : windowsById->Values()) {
131 if (window->GetDocShell() && window->IsRootOuterWindow()) {
132 if (RefPtr<dom::Document> docNode = window->GetExtantDoc()) {
133 GetAccService()->GetDocAccessible(docNode); // ensure creation
134 }
135 }
136 }
137 }
138
GetSiblingAtOffset(int32_t aOffset,nsresult * aError) const139 LocalAccessible* ApplicationAccessible::GetSiblingAtOffset(
140 int32_t aOffset, nsresult* aError) const {
141 if (aError) *aError = NS_OK; // fail peacefully
142
143 return nullptr;
144 }
145