1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_a11y_DocAccessible_inl_h_
8 #define mozilla_a11y_DocAccessible_inl_h_
9 
10 #include "DocAccessible.h"
11 #include "nsAccessibilityService.h"
12 #include "nsAccessiblePivot.h"
13 #include "NotificationController.h"
14 #include "States.h"
15 #include "nsIScrollableFrame.h"
16 #include "mozilla/dom/DocumentInlines.h"
17 
18 #ifdef A11Y_LOG
19 #  include "Logging.h"
20 #endif
21 
22 namespace mozilla {
23 namespace a11y {
24 
AccessibleOrTrueContainer(nsINode * aNode,bool aNoContainerIfPruned)25 inline LocalAccessible* DocAccessible::AccessibleOrTrueContainer(
26     nsINode* aNode, bool aNoContainerIfPruned) const {
27   // HTML comboboxes have no-content list accessible as an intermediate
28   // containing all options.
29   LocalAccessible* container =
30       GetAccessibleOrContainer(aNode, aNoContainerIfPruned);
31   if (container && container->IsHTMLCombobox()) {
32     return container->LocalFirstChild();
33   }
34   return container;
35 }
36 
VirtualCursor()37 inline nsIAccessiblePivot* DocAccessible::VirtualCursor() {
38   if (!mVirtualCursor) {
39     mVirtualCursor = new nsAccessiblePivot(this);
40     mVirtualCursor->AddObserver(this);
41   }
42   return mVirtualCursor;
43 }
44 
IsContentLoaded()45 inline bool DocAccessible::IsContentLoaded() const {
46   // eDOMLoaded flag check is used for error pages as workaround to make this
47   // method return correct result since error pages do not receive 'pageshow'
48   // event and as consequence Document::IsShowing() returns false.
49   return mDocumentNode && mDocumentNode->IsVisible() &&
50          (mDocumentNode->IsShowing() || HasLoadState(eDOMLoaded));
51 }
52 
IsHidden()53 inline bool DocAccessible::IsHidden() const { return mDocumentNode->Hidden(); }
54 
FireDelayedEvent(AccEvent * aEvent)55 inline void DocAccessible::FireDelayedEvent(AccEvent* aEvent) {
56 #ifdef A11Y_LOG
57   if (logging::IsEnabled(logging::eDocLoad)) logging::DocLoadEventFired(aEvent);
58 #endif
59 
60   mNotificationController->QueueEvent(aEvent);
61 }
62 
FireDelayedEvent(uint32_t aEventType,LocalAccessible * aTarget)63 inline void DocAccessible::FireDelayedEvent(uint32_t aEventType,
64                                             LocalAccessible* aTarget) {
65   RefPtr<AccEvent> event = new AccEvent(aEventType, aTarget);
66   FireDelayedEvent(event);
67 }
68 
BindChildDocument(DocAccessible * aDocument)69 inline void DocAccessible::BindChildDocument(DocAccessible* aDocument) {
70   mNotificationController->ScheduleChildDocBinding(aDocument);
71 }
72 
73 template <class Class, class... Args>
HandleNotification(Class * aInstance,typename TNotification<Class,Args...>::Callback aMethod,Args * ...aArgs)74 inline void DocAccessible::HandleNotification(
75     Class* aInstance, typename TNotification<Class, Args...>::Callback aMethod,
76     Args*... aArgs) {
77   if (mNotificationController) {
78     mNotificationController->HandleNotification<Class, Args...>(
79         aInstance, aMethod, aArgs...);
80   }
81 }
82 
UpdateText(nsIContent * aTextNode)83 inline void DocAccessible::UpdateText(nsIContent* aTextNode) {
84   NS_ASSERTION(mNotificationController, "The document was shut down!");
85 
86   // Ignore the notification if initial tree construction hasn't been done yet.
87   if (mNotificationController && HasLoadState(eTreeConstructed)) {
88     mNotificationController->ScheduleTextUpdate(aTextNode);
89   }
90 }
91 
NotifyOfLoad(uint32_t aLoadEventType)92 inline void DocAccessible::NotifyOfLoad(uint32_t aLoadEventType) {
93   mLoadState |= eDOMLoaded;
94   mLoadEventType = aLoadEventType;
95 
96   // If the document is loaded completely then network activity was presumingly
97   // caused by file loading. Fire busy state change event.
98   if (HasLoadState(eCompletelyLoaded) && IsLoadEventTarget()) {
99     RefPtr<AccEvent> stateEvent =
100         new AccStateChangeEvent(this, states::BUSY, false);
101     FireDelayedEvent(stateEvent);
102   }
103 }
104 
MaybeNotifyOfValueChange(LocalAccessible * aAccessible)105 inline void DocAccessible::MaybeNotifyOfValueChange(
106     LocalAccessible* aAccessible) {
107   if (aAccessible->IsCombobox() || aAccessible->Role() == roles::ENTRY ||
108       aAccessible->Role() == roles::SPINBUTTON) {
109     FireDelayedEvent(nsIAccessibleEvent::EVENT_TEXT_VALUE_CHANGE, aAccessible);
110   }
111 }
112 
GetAccessibleEvenIfNotInMapOrContainer(nsINode * aNode)113 inline LocalAccessible* DocAccessible::GetAccessibleEvenIfNotInMapOrContainer(
114     nsINode* aNode) const {
115   LocalAccessible* acc = GetAccessibleEvenIfNotInMap(aNode);
116   return acc ? acc : GetContainerAccessible(aNode);
117 }
118 
CreateSubtree(LocalAccessible * aChild)119 inline void DocAccessible::CreateSubtree(LocalAccessible* aChild) {
120   // If a focused node has been shown then it could mean its frame was recreated
121   // while the node stays focused and we need to fire focus event on
122   // the accessible we just created. If the queue contains a focus event for
123   // this node already then it will be suppressed by this one.
124   LocalAccessible* focusedAcc = nullptr;
125   CacheChildrenInSubtree(aChild, &focusedAcc);
126 
127 #ifdef A11Y_LOG
128   if (logging::IsEnabled(logging::eVerbose)) {
129     logging::Tree("TREE", "Created subtree", aChild);
130   }
131 #endif
132 
133   // Fire events for ARIA elements.
134   if (aChild->HasARIARole()) {
135     roles::Role role = aChild->ARIARole();
136     if (role == roles::MENUPOPUP) {
137       FireDelayedEvent(nsIAccessibleEvent::EVENT_MENUPOPUP_START, aChild);
138     } else if (role == roles::ALERT) {
139       FireDelayedEvent(nsIAccessibleEvent::EVENT_ALERT, aChild);
140     }
141   }
142 
143   // XXX: do we really want to send focus to focused DOM node not taking into
144   // account active item?
145   if (focusedAcc) {
146     FocusMgr()->DispatchFocusEvent(this, focusedAcc);
147     SelectionMgr()->SetControlSelectionListener(
148         focusedAcc->GetNode()->AsElement());
149   }
150 }
151 
GetRelProviders(dom::Element * aElement,const nsAString & aID)152 inline DocAccessible::AttrRelProviders* DocAccessible::GetRelProviders(
153     dom::Element* aElement, const nsAString& aID) const {
154   DependentIDsHashtable* hash = mDependentIDsHashes.Get(
155       aElement->GetUncomposedDocOrConnectedShadowRoot());
156   if (hash) {
157     return hash->Get(aID);
158   }
159   return nullptr;
160 }
161 
GetOrCreateRelProviders(dom::Element * aElement,const nsAString & aID)162 inline DocAccessible::AttrRelProviders* DocAccessible::GetOrCreateRelProviders(
163     dom::Element* aElement, const nsAString& aID) {
164   dom::DocumentOrShadowRoot* docOrShadowRoot =
165       aElement->GetUncomposedDocOrConnectedShadowRoot();
166   DependentIDsHashtable* hash =
167       mDependentIDsHashes.GetOrInsertNew(docOrShadowRoot);
168 
169   return hash->GetOrInsertNew(aID);
170 }
171 
RemoveRelProvidersIfEmpty(dom::Element * aElement,const nsAString & aID)172 inline void DocAccessible::RemoveRelProvidersIfEmpty(dom::Element* aElement,
173                                                      const nsAString& aID) {
174   dom::DocumentOrShadowRoot* docOrShadowRoot =
175       aElement->GetUncomposedDocOrConnectedShadowRoot();
176   DependentIDsHashtable* hash = mDependentIDsHashes.Get(docOrShadowRoot);
177   if (hash) {
178     AttrRelProviders* providers = hash->Get(aID);
179     if (providers && providers->Length() == 0) {
180       hash->Remove(aID);
181       if (mDependentIDsHashes.IsEmpty()) {
182         mDependentIDsHashes.Remove(docOrShadowRoot);
183       }
184     }
185   }
186 }
187 
188 }  // namespace a11y
189 }  // namespace mozilla
190 
191 #endif
192