|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef mozilla_a11_DocManager_h_ |
|
6 #define mozilla_a11_DocManager_h_ |
|
7 |
|
8 #include "nsIDocument.h" |
|
9 #include "nsIDOMEventListener.h" |
|
10 #include "nsRefPtrHashtable.h" |
|
11 #include "nsIWebProgressListener.h" |
|
12 #include "nsWeakReference.h" |
|
13 #include "nsIPresShell.h" |
|
14 |
|
15 namespace mozilla { |
|
16 namespace a11y { |
|
17 |
|
18 class Accessible; |
|
19 class DocAccessible; |
|
20 |
|
21 /** |
|
22 * Manage the document accessible life cycle. |
|
23 */ |
|
24 class DocManager : public nsIWebProgressListener, |
|
25 public nsIDOMEventListener, |
|
26 public nsSupportsWeakReference |
|
27 { |
|
28 public: |
|
29 virtual ~DocManager() { } |
|
30 |
|
31 NS_DECL_THREADSAFE_ISUPPORTS |
|
32 NS_DECL_NSIWEBPROGRESSLISTENER |
|
33 NS_DECL_NSIDOMEVENTLISTENER |
|
34 |
|
35 /** |
|
36 * Return document accessible for the given DOM node. |
|
37 */ |
|
38 DocAccessible* GetDocAccessible(nsIDocument* aDocument); |
|
39 |
|
40 /** |
|
41 * Return document accessible for the given presshell. |
|
42 */ |
|
43 DocAccessible* GetDocAccessible(const nsIPresShell* aPresShell) |
|
44 { |
|
45 if (!aPresShell) |
|
46 return nullptr; |
|
47 |
|
48 DocAccessible* doc = aPresShell->GetDocAccessible(); |
|
49 if (doc) |
|
50 return doc; |
|
51 |
|
52 return GetDocAccessible(aPresShell->GetDocument()); |
|
53 } |
|
54 |
|
55 /** |
|
56 * Search through all document accessibles for an accessible with the given |
|
57 * unique id. |
|
58 */ |
|
59 Accessible* FindAccessibleInCache(nsINode* aNode) const; |
|
60 |
|
61 /** |
|
62 * Called by document accessible when it gets shutdown. |
|
63 */ |
|
64 inline void NotifyOfDocumentShutdown(nsIDocument* aDocument) |
|
65 { |
|
66 mDocAccessibleCache.Remove(aDocument); |
|
67 RemoveListeners(aDocument); |
|
68 } |
|
69 |
|
70 #ifdef DEBUG |
|
71 bool IsProcessingRefreshDriverNotification() const; |
|
72 #endif |
|
73 |
|
74 protected: |
|
75 DocManager(); |
|
76 |
|
77 /** |
|
78 * Initialize the manager. |
|
79 */ |
|
80 bool Init(); |
|
81 |
|
82 /** |
|
83 * Shutdown the manager. |
|
84 */ |
|
85 void Shutdown(); |
|
86 |
|
87 private: |
|
88 DocManager(const DocManager&); |
|
89 DocManager& operator =(const DocManager&); |
|
90 |
|
91 private: |
|
92 /** |
|
93 * Create an accessible document if it was't created and fire accessibility |
|
94 * events if needed. |
|
95 * |
|
96 * @param aDocument [in] loaded DOM document |
|
97 * @param aLoadEventType [in] specifies the event type to fire load event, |
|
98 * if 0 then no event is fired |
|
99 */ |
|
100 void HandleDOMDocumentLoad(nsIDocument* aDocument, |
|
101 uint32_t aLoadEventType); |
|
102 |
|
103 /** |
|
104 * Add/remove 'pagehide' and 'DOMContentLoaded' event listeners. |
|
105 */ |
|
106 void AddListeners(nsIDocument *aDocument, bool aAddPageShowListener); |
|
107 void RemoveListeners(nsIDocument* aDocument); |
|
108 |
|
109 /** |
|
110 * Create document or root accessible. |
|
111 */ |
|
112 DocAccessible* CreateDocOrRootAccessible(nsIDocument* aDocument); |
|
113 |
|
114 typedef nsRefPtrHashtable<nsPtrHashKey<const nsIDocument>, DocAccessible> |
|
115 DocAccessibleHashtable; |
|
116 |
|
117 /** |
|
118 * Get first entry of the document accessible from cache. |
|
119 */ |
|
120 static PLDHashOperator |
|
121 GetFirstEntryInDocCache(const nsIDocument* aKey, |
|
122 DocAccessible* aDocAccessible, |
|
123 void* aUserArg); |
|
124 |
|
125 /** |
|
126 * Clear the cache and shutdown the document accessibles. |
|
127 */ |
|
128 void ClearDocCache(); |
|
129 |
|
130 struct nsSearchAccessibleInCacheArg |
|
131 { |
|
132 Accessible* mAccessible; |
|
133 nsINode* mNode; |
|
134 }; |
|
135 |
|
136 static PLDHashOperator |
|
137 SearchAccessibleInDocCache(const nsIDocument* aKey, |
|
138 DocAccessible* aDocAccessible, |
|
139 void* aUserArg); |
|
140 |
|
141 #ifdef DEBUG |
|
142 static PLDHashOperator |
|
143 SearchIfDocIsRefreshing(const nsIDocument* aKey, |
|
144 DocAccessible* aDocAccessible, void* aUserArg); |
|
145 #endif |
|
146 |
|
147 DocAccessibleHashtable mDocAccessibleCache; |
|
148 }; |
|
149 |
|
150 /** |
|
151 * Return the existing document accessible for the document if any. |
|
152 * Note this returns the doc accessible for the primary pres shell if there is |
|
153 * more than one. |
|
154 */ |
|
155 inline DocAccessible* |
|
156 GetExistingDocAccessible(const nsIDocument* aDocument) |
|
157 { |
|
158 nsIPresShell* ps = aDocument->GetShell(); |
|
159 return ps ? ps->GetDocAccessible() : nullptr; |
|
160 } |
|
161 |
|
162 } // namespace a11y |
|
163 } // namespace mozilla |
|
164 |
|
165 #endif // mozilla_a11_DocManager_h_ |