Bug 1858921 - Part 1: Remove unnecessary Allocator.h includes r=sfink
[gecko.git] / accessible / base / DocManager.h
blob94da5d0a247dc70c326d253cf03fe07895cb2e9b
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"
14 #include "nsINode.h"
16 namespace mozilla::dom {
17 class Document;
20 namespace mozilla {
21 class PresShell;
23 namespace a11y {
25 class LocalAccessible;
26 class DocAccessible;
27 class xpcAccessibleDocument;
28 class DocAccessibleParent;
30 /**
31 * Manage the document accessible life cycle.
33 class DocManager : public nsIWebProgressListener,
34 public nsIDOMEventListener,
35 public nsSupportsWeakReference {
36 public:
37 NS_DECL_THREADSAFE_ISUPPORTS
38 NS_DECL_NSIWEBPROGRESSLISTENER
39 NS_DECL_NSIDOMEVENTLISTENER
41 /**
42 * Return document accessible for the given DOM node.
44 DocAccessible* GetDocAccessible(dom::Document* aDocument);
46 /**
47 * Return document accessible for the given presshell.
49 DocAccessible* GetDocAccessible(const PresShell* aPresShell);
51 /**
52 * Search through all document accessibles for an accessible with the given
53 * unique id.
55 LocalAccessible* FindAccessibleInCache(nsINode* aNode) const;
57 /**
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);
69 /**
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;
94 /**
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)
108 : nullptr;
111 #ifdef DEBUG
112 bool IsProcessingRefreshDriverNotification() const;
113 #endif
115 protected:
116 DocManager();
117 virtual ~DocManager() = default;
120 * Initialize the manager.
122 bool Init();
125 * Shutdown the manager.
127 void Shutdown();
129 bool HasXPCDocuments() {
130 return mXPCDocumentCache.Count() > 0 ||
131 (sRemoteXPCDocumentCache && sRemoteXPCDocumentCache->Count() > 0);
134 private:
135 DocManager(const DocManager&);
136 DocManager& operator=(const DocManager&);
138 private:
140 * Create an accessible document if it was't created and fire accessibility
141 * events if needed.
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
186 * more than one.
188 DocAccessible* GetExistingDocAccessible(const dom::Document* aDocument);
190 } // namespace a11y
191 } // namespace mozilla
193 #endif // mozilla_a11_DocManager_h_