diff options
Diffstat (limited to 'editor/libeditor/EditorBase.h')
-rw-r--r-- | editor/libeditor/EditorBase.h | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h index 7ea0f4dab..618da12a8 100644 --- a/editor/libeditor/EditorBase.h +++ b/editor/libeditor/EditorBase.h @@ -17,6 +17,7 @@ #include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr #include "nsCycleCollectionParticipant.h" #include "nsGkAtoms.h" +#include "nsIDocument.h" // for nsIDocument #include "nsIEditor.h" // for nsIEditor::EDirection, etc. #include "nsIEditorIMESupport.h" // for NS_DECL_NSIEDITORIMESUPPORT, etc. #include "nsIObserver.h" // for NS_DECL_NSIOBSERVER, etc. @@ -32,7 +33,6 @@ class nsIAtom; class nsIContent; -class nsIDOMDocument; class nsIDOMEvent; class nsIDOMEventListener; class nsIDOMEventTarget; @@ -135,6 +135,57 @@ namespace widget { struct IMEState; } // namespace widget +/** + * CachedWeakPtr stores a pointer to a class which inherits nsIWeakReference. + * If the instance of the class has already been destroyed, this returns + * nullptr. Otherwise, returns cached pointer. + */ +template<class T> +class CachedWeakPtr final +{ +public: + CachedWeakPtr<T>() + : mCache(nullptr) + { + } + + CachedWeakPtr<T>& operator=(T* aObject) + { + mWeakPtr = do_GetWeakReference(aObject); + mCache = aObject; + return *this; + } + CachedWeakPtr<T>& operator=(const nsCOMPtr<T>& aOther) + { + mWeakPtr = do_GetWeakReference(aOther); + mCache = aOther; + return *this; + } + CachedWeakPtr<T>& operator=(already_AddRefed<T>& aOther) + { + nsCOMPtr<T> other = aOther; + mWeakPtr = do_GetWeakReference(other); + mCache = other; + return *this; + } + + bool IsAlive() const { return mWeakPtr && mWeakPtr->IsAlive(); } + + explicit operator bool() const { return mWeakPtr; } + operator T*() const { return get(); } + T* get() const + { + if (mCache && !mWeakPtr->IsAlive()) { + const_cast<CachedWeakPtr<T>*>(this)->mCache = nullptr; + } + return mCache; + } + +private: + nsWeakPtr mWeakPtr; + T* MOZ_NON_OWNING_REF mCache; +}; + #define kMOZEditorBogusNodeAttrAtom nsGkAtoms::mozeditorbogusnode #define kMOZEditorBogusNodeValue NS_LITERAL_STRING("TRUE") @@ -178,6 +229,7 @@ public: NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(EditorBase, nsIEditor) + bool IsInitialized() const { return !!mDocumentWeak; } already_AddRefed<nsIDOMDocument> GetDOMDocument(); already_AddRefed<nsIDocument> GetDocument(); already_AddRefed<nsIPresShell> GetPresShell(); @@ -848,7 +900,7 @@ public: bool HasIndependentSelection() const { - return !!mSelConWeak; + return !!mSelectionControllerWeak; } /** @@ -954,6 +1006,14 @@ public: } } +private: + // Weak reference to the nsISelectionController. + // Use GetSelectionController() to retrieve actual pointer. + CachedWeakPtr<nsISelectionController> mSelectionControllerWeak; + // Weak reference to the nsIDocument. + // Use GetDocument() to retrieve actual pointer. + CachedWeakPtr<nsIDocument> mDocumentWeak; + protected: enum Tristate { @@ -975,12 +1035,8 @@ protected: // The form field as an event receiver. nsCOMPtr<dom::EventTarget> mEventTarget; nsCOMPtr<nsIDOMEventListener> mEventListener; - // Weak reference to the nsISelectionController. - nsWeakPtr mSelConWeak; // Weak reference to placeholder for begin/end batch purposes. WeakPtr<PlaceholderTransaction> mPlaceholderTransactionWeak; - // Weak reference to the nsIDOMDocument. - nsWeakPtr mDocWeak; // Name of placeholder transaction. nsIAtom* mPlaceholderName; // Saved selection state for placeholder transaction batching. |