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/. */
5 #ifndef mozilla_a11_DocManager_h_
6 #define mozilla_a11_DocManager_h_
8 #include "mozilla/ClearOnShutdown.h"
9 #include "nsIDOMEventListener.h"
10 #include "nsRefPtrHashtable.h"
11 #include "nsIWebProgressListener.h"
12 #include "nsWeakReference.h"
13 #include "mozilla/StaticPtr.h"
16 namespace mozilla::dom
{
25 class LocalAccessible
;
27 class xpcAccessibleDocument
;
28 class DocAccessibleParent
;
31 * Manage the document accessible life cycle.
33 class DocManager
: public nsIWebProgressListener
,
34 public nsIDOMEventListener
,
35 public nsSupportsWeakReference
{
37 NS_DECL_THREADSAFE_ISUPPORTS
38 NS_DECL_NSIWEBPROGRESSLISTENER
39 NS_DECL_NSIDOMEVENTLISTENER
42 * Return document accessible for the given DOM node.
44 DocAccessible
* GetDocAccessible(dom::Document
* aDocument
);
47 * Return document accessible for the given presshell.
49 DocAccessible
* GetDocAccessible(const PresShell
* aPresShell
);
52 * Search through all document accessibles for an accessible with the given
55 LocalAccessible
* FindAccessibleInCache(nsINode
* aNode
) const;
58 * Called by document accessible when it gets shutdown.
59 * @param aAllowServiceShutdown true to shut down nsAccessibilityService
60 * if it is no longer required, false to prevent it.
62 void NotifyOfDocumentShutdown(DocAccessible
* aDocument
,
63 dom::Document
* aDOMDocument
,
64 bool aAllowServiceShutdown
= true);
66 void RemoveFromXPCDocumentCache(DocAccessible
* aDocument
,
67 bool aAllowServiceShutdown
= true);
70 * Return XPCOM accessible document.
72 xpcAccessibleDocument
* GetXPCDocument(DocAccessible
* aDocument
);
73 xpcAccessibleDocument
* GetCachedXPCDocument(DocAccessible
* aDocument
) const {
74 return mXPCDocumentCache
.GetWeak(aDocument
);
78 * Notification that a top level document in a content process has gone away.
80 static void RemoteDocShutdown(DocAccessibleParent
* aDoc
) {
81 DebugOnly
<bool> result
= sRemoteDocuments
->RemoveElement(aDoc
);
82 MOZ_ASSERT(result
, "Why didn't we find the document!");
86 * Notify of a new top level document in a content process.
88 static void RemoteDocAdded(DocAccessibleParent
* aDoc
);
90 static const nsTArray
<DocAccessibleParent
*>* TopLevelRemoteDocs() {
91 return sRemoteDocuments
;
95 * Remove the xpc document for a remote document if there is one.
97 static void NotifyOfRemoteDocShutdown(DocAccessibleParent
* adoc
);
99 static void RemoveFromRemoteXPCDocumentCache(DocAccessibleParent
* aDoc
);
102 * Get a XPC document for a remote document.
104 static xpcAccessibleDocument
* GetXPCDocument(DocAccessibleParent
* aDoc
);
105 static xpcAccessibleDocument
* GetCachedXPCDocument(
106 const DocAccessibleParent
* aDoc
) {
107 return sRemoteXPCDocumentCache
? sRemoteXPCDocumentCache
->GetWeak(aDoc
)
112 bool IsProcessingRefreshDriverNotification() const;
117 virtual ~DocManager() = default;
120 * Initialize the manager.
125 * Shutdown the manager.
129 bool HasXPCDocuments() {
130 return mXPCDocumentCache
.Count() > 0 ||
131 (sRemoteXPCDocumentCache
&& sRemoteXPCDocumentCache
->Count() > 0);
135 DocManager(const DocManager
&);
136 DocManager
& operator=(const DocManager
&);
140 * Create an accessible document if it was't created and fire accessibility
143 * @param aDocument [in] loaded DOM document
144 * @param aLoadEventType [in] specifies the event type to fire load event,
145 * if 0 then no event is fired
147 void HandleDOMDocumentLoad(dom::Document
* aDocument
, uint32_t aLoadEventType
);
150 * Add/remove 'pagehide' and 'DOMContentLoaded' event listeners.
152 void AddListeners(dom::Document
* aDocument
, bool aAddPageShowListener
);
153 void RemoveListeners(dom::Document
* aDocument
);
156 * Create document or root accessible.
158 DocAccessible
* CreateDocOrRootAccessible(dom::Document
* aDocument
);
161 * Clear the cache and shutdown the document accessibles.
163 void ClearDocCache();
165 typedef nsRefPtrHashtable
<nsPtrHashKey
<const dom::Document
>, DocAccessible
>
166 DocAccessibleHashtable
;
167 DocAccessibleHashtable mDocAccessibleCache
;
169 typedef nsRefPtrHashtable
<nsPtrHashKey
<const DocAccessible
>,
170 xpcAccessibleDocument
>
171 XPCDocumentHashtable
;
172 XPCDocumentHashtable mXPCDocumentCache
;
173 static StaticAutoPtr
<nsRefPtrHashtable
<
174 nsPtrHashKey
<const DocAccessibleParent
>, xpcAccessibleDocument
>>
175 sRemoteXPCDocumentCache
;
178 * The list of remote top level documents.
180 static StaticAutoPtr
<nsTArray
<DocAccessibleParent
*>> sRemoteDocuments
;
184 * Return the existing document accessible for the document if any.
185 * Note this returns the doc accessible for the primary pres shell if there is
188 DocAccessible
* GetExistingDocAccessible(const dom::Document
* aDocument
);
191 } // namespace mozilla
193 #endif // mozilla_a11_DocManager_h_