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 "mozilla/PresShell.h"
10 #include "mozilla/dom/Document.h"
11 #include "nsIDOMEventListener.h"
12 #include "nsRefPtrHashtable.h"
13 #include "nsIWebProgressListener.h"
14 #include "nsWeakReference.h"
15 #include "mozilla/StaticPtr.h"
22 class xpcAccessibleDocument
;
23 class DocAccessibleParent
;
26 * Manage the document accessible life cycle.
28 class DocManager
: public nsIWebProgressListener
,
29 public nsIDOMEventListener
,
30 public nsSupportsWeakReference
{
32 NS_DECL_THREADSAFE_ISUPPORTS
33 NS_DECL_NSIWEBPROGRESSLISTENER
34 NS_DECL_NSIDOMEVENTLISTENER
37 * Return document accessible for the given DOM node.
39 DocAccessible
* GetDocAccessible(dom::Document
* aDocument
);
42 * Return document accessible for the given presshell.
44 DocAccessible
* GetDocAccessible(const PresShell
* aPresShell
) {
49 DocAccessible
* doc
= aPresShell
->GetDocAccessible();
54 return GetDocAccessible(aPresShell
->GetDocument());
58 * Search through all document accessibles for an accessible with the given
61 Accessible
* FindAccessibleInCache(nsINode
* aNode
) const;
64 * Called by document accessible when it gets shutdown.
66 void NotifyOfDocumentShutdown(DocAccessible
* aDocument
,
67 dom::Document
* aDOMDocument
);
69 void RemoveFromXPCDocumentCache(DocAccessible
* aDocument
);
72 * Return XPCOM accessible document.
74 xpcAccessibleDocument
* GetXPCDocument(DocAccessible
* aDocument
);
75 xpcAccessibleDocument
* GetCachedXPCDocument(DocAccessible
* aDocument
) const {
76 return mXPCDocumentCache
.GetWeak(aDocument
);
80 * Notification that a top level document in a content process has gone away.
82 static void RemoteDocShutdown(DocAccessibleParent
* aDoc
) {
83 DebugOnly
<bool> result
= sRemoteDocuments
->RemoveElement(aDoc
);
84 MOZ_ASSERT(result
, "Why didn't we find the document!");
88 * Notify of a new top level document in a content process.
90 static void RemoteDocAdded(DocAccessibleParent
* aDoc
);
92 static const nsTArray
<DocAccessibleParent
*>* TopLevelRemoteDocs() {
93 return sRemoteDocuments
;
97 * Remove the xpc document for a remote document if there is one.
99 static void NotifyOfRemoteDocShutdown(DocAccessibleParent
* adoc
);
101 static void RemoveFromRemoteXPCDocumentCache(DocAccessibleParent
* aDoc
);
104 * Get a XPC document for a remote document.
106 static xpcAccessibleDocument
* GetXPCDocument(DocAccessibleParent
* aDoc
);
107 static xpcAccessibleDocument
* GetCachedXPCDocument(
108 const DocAccessibleParent
* aDoc
) {
109 return sRemoteXPCDocumentCache
? sRemoteXPCDocumentCache
->GetWeak(aDoc
)
114 bool IsProcessingRefreshDriverNotification() const;
119 virtual ~DocManager() {}
122 * Initialize the manager.
127 * Shutdown the manager.
131 bool HasXPCDocuments() {
132 return mXPCDocumentCache
.Count() > 0 ||
133 (sRemoteXPCDocumentCache
&& sRemoteXPCDocumentCache
->Count() > 0);
137 DocManager(const DocManager
&);
138 DocManager
& operator=(const DocManager
&);
142 * Create an accessible document if it was't created and fire accessibility
145 * @param aDocument [in] loaded DOM document
146 * @param aLoadEventType [in] specifies the event type to fire load event,
147 * if 0 then no event is fired
149 void HandleDOMDocumentLoad(dom::Document
* aDocument
, uint32_t aLoadEventType
);
152 * Add/remove 'pagehide' and 'DOMContentLoaded' event listeners.
154 void AddListeners(dom::Document
* aDocument
, bool aAddPageShowListener
);
155 void RemoveListeners(dom::Document
* aDocument
);
158 * Create document or root accessible.
160 DocAccessible
* CreateDocOrRootAccessible(dom::Document
* aDocument
);
163 * Clear the cache and shutdown the document accessibles.
165 void ClearDocCache();
167 typedef nsRefPtrHashtable
<nsPtrHashKey
<const dom::Document
>, DocAccessible
>
168 DocAccessibleHashtable
;
169 DocAccessibleHashtable mDocAccessibleCache
;
171 typedef nsRefPtrHashtable
<nsPtrHashKey
<const DocAccessible
>,
172 xpcAccessibleDocument
>
173 XPCDocumentHashtable
;
174 XPCDocumentHashtable mXPCDocumentCache
;
175 static nsRefPtrHashtable
<nsPtrHashKey
<const DocAccessibleParent
>,
176 xpcAccessibleDocument
>* sRemoteXPCDocumentCache
;
179 * The list of remote top level documents.
181 static StaticAutoPtr
<nsTArray
<DocAccessibleParent
*>> sRemoteDocuments
;
185 * Return the existing document accessible for the document if any.
186 * Note this returns the doc accessible for the primary pres shell if there is
189 inline DocAccessible
* GetExistingDocAccessible(const dom::Document
* aDocument
) {
190 PresShell
* presShell
= aDocument
->GetPresShell();
191 return presShell
? presShell
->GetDocAccessible() : nullptr;
195 } // namespace mozilla
197 #endif // mozilla_a11_DocManager_h_