michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_a11_DocManager_h_ michael@0: #define mozilla_a11_DocManager_h_ michael@0: michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMEventListener.h" michael@0: #include "nsRefPtrHashtable.h" michael@0: #include "nsIWebProgressListener.h" michael@0: #include "nsWeakReference.h" michael@0: #include "nsIPresShell.h" michael@0: michael@0: namespace mozilla { michael@0: namespace a11y { michael@0: michael@0: class Accessible; michael@0: class DocAccessible; michael@0: michael@0: /** michael@0: * Manage the document accessible life cycle. michael@0: */ michael@0: class DocManager : public nsIWebProgressListener, michael@0: public nsIDOMEventListener, michael@0: public nsSupportsWeakReference michael@0: { michael@0: public: michael@0: virtual ~DocManager() { } michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIWEBPROGRESSLISTENER michael@0: NS_DECL_NSIDOMEVENTLISTENER michael@0: michael@0: /** michael@0: * Return document accessible for the given DOM node. michael@0: */ michael@0: DocAccessible* GetDocAccessible(nsIDocument* aDocument); michael@0: michael@0: /** michael@0: * Return document accessible for the given presshell. michael@0: */ michael@0: DocAccessible* GetDocAccessible(const nsIPresShell* aPresShell) michael@0: { michael@0: if (!aPresShell) michael@0: return nullptr; michael@0: michael@0: DocAccessible* doc = aPresShell->GetDocAccessible(); michael@0: if (doc) michael@0: return doc; michael@0: michael@0: return GetDocAccessible(aPresShell->GetDocument()); michael@0: } michael@0: michael@0: /** michael@0: * Search through all document accessibles for an accessible with the given michael@0: * unique id. michael@0: */ michael@0: Accessible* FindAccessibleInCache(nsINode* aNode) const; michael@0: michael@0: /** michael@0: * Called by document accessible when it gets shutdown. michael@0: */ michael@0: inline void NotifyOfDocumentShutdown(nsIDocument* aDocument) michael@0: { michael@0: mDocAccessibleCache.Remove(aDocument); michael@0: RemoveListeners(aDocument); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: bool IsProcessingRefreshDriverNotification() const; michael@0: #endif michael@0: michael@0: protected: michael@0: DocManager(); michael@0: michael@0: /** michael@0: * Initialize the manager. michael@0: */ michael@0: bool Init(); michael@0: michael@0: /** michael@0: * Shutdown the manager. michael@0: */ michael@0: void Shutdown(); michael@0: michael@0: private: michael@0: DocManager(const DocManager&); michael@0: DocManager& operator =(const DocManager&); michael@0: michael@0: private: michael@0: /** michael@0: * Create an accessible document if it was't created and fire accessibility michael@0: * events if needed. michael@0: * michael@0: * @param aDocument [in] loaded DOM document michael@0: * @param aLoadEventType [in] specifies the event type to fire load event, michael@0: * if 0 then no event is fired michael@0: */ michael@0: void HandleDOMDocumentLoad(nsIDocument* aDocument, michael@0: uint32_t aLoadEventType); michael@0: michael@0: /** michael@0: * Add/remove 'pagehide' and 'DOMContentLoaded' event listeners. michael@0: */ michael@0: void AddListeners(nsIDocument *aDocument, bool aAddPageShowListener); michael@0: void RemoveListeners(nsIDocument* aDocument); michael@0: michael@0: /** michael@0: * Create document or root accessible. michael@0: */ michael@0: DocAccessible* CreateDocOrRootAccessible(nsIDocument* aDocument); michael@0: michael@0: typedef nsRefPtrHashtable, DocAccessible> michael@0: DocAccessibleHashtable; michael@0: michael@0: /** michael@0: * Get first entry of the document accessible from cache. michael@0: */ michael@0: static PLDHashOperator michael@0: GetFirstEntryInDocCache(const nsIDocument* aKey, michael@0: DocAccessible* aDocAccessible, michael@0: void* aUserArg); michael@0: michael@0: /** michael@0: * Clear the cache and shutdown the document accessibles. michael@0: */ michael@0: void ClearDocCache(); michael@0: michael@0: struct nsSearchAccessibleInCacheArg michael@0: { michael@0: Accessible* mAccessible; michael@0: nsINode* mNode; michael@0: }; michael@0: michael@0: static PLDHashOperator michael@0: SearchAccessibleInDocCache(const nsIDocument* aKey, michael@0: DocAccessible* aDocAccessible, michael@0: void* aUserArg); michael@0: michael@0: #ifdef DEBUG michael@0: static PLDHashOperator michael@0: SearchIfDocIsRefreshing(const nsIDocument* aKey, michael@0: DocAccessible* aDocAccessible, void* aUserArg); michael@0: #endif michael@0: michael@0: DocAccessibleHashtable mDocAccessibleCache; michael@0: }; michael@0: michael@0: /** michael@0: * Return the existing document accessible for the document if any. michael@0: * Note this returns the doc accessible for the primary pres shell if there is michael@0: * more than one. michael@0: */ michael@0: inline DocAccessible* michael@0: GetExistingDocAccessible(const nsIDocument* aDocument) michael@0: { michael@0: nsIPresShell* ps = aDocument->GetShell(); michael@0: return ps ? ps->GetDocAccessible() : nullptr; michael@0: } michael@0: michael@0: } // namespace a11y michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_a11_DocManager_h_