GlobalTeardownObserver.h (2335B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DOM_BASE_GLOBALTEARDOWNOBSERVER_H_ 8 #define DOM_BASE_GLOBALTEARDOWNOBSERVER_H_ 9 10 #include "mozilla/Attributes.h" 11 #include "nsIGlobalObject.h" 12 #include "nsIScriptGlobalObject.h" 13 14 namespace mozilla { 15 16 class GlobalTeardownObserver 17 18 : public nsISupports, 19 public LinkedListElement<GlobalTeardownObserver> { 20 public: 21 GlobalTeardownObserver(); 22 explicit GlobalTeardownObserver(nsIGlobalObject* aGlobalObject, 23 bool aHasOrHasHadOwnerWindow = false); 24 25 nsGlobalWindowInner* GetOwnerWindow() const; 26 nsIGlobalObject* GetOwnerGlobal() const { return mParentObject; } 27 bool HasOrHasHadOwnerWindow() const { return mHasOrHasHadOwnerWindow; } 28 29 void GetParentObject(nsIScriptGlobalObject** aParentObject) { 30 if (mParentObject) { 31 CallQueryInterface(mParentObject, aParentObject); 32 } else { 33 *aParentObject = nullptr; 34 } 35 } 36 37 virtual void DisconnectFromOwner(); 38 39 // A global permanently becomes invalid when DisconnectEventTargetObjects() is 40 // called. Normally this means: 41 // - For the main thread, when nsGlobalWindowInner::FreeInnerObjects is 42 // called. 43 // - For a worker thread, when clearing the main event queue. (Which we do 44 // slightly later than when the spec notionally calls for it to be done.) 45 // 46 // A global may also become temporarily invalid when: 47 // - For the main thread, if the window is no longer the WindowProxy's current 48 // inner window due to being placed in the bfcache. 49 nsresult CheckCurrentGlobalCorrectness() const; 50 51 protected: 52 virtual ~GlobalTeardownObserver(); 53 54 void BindToOwner(nsIGlobalObject* aOwner); 55 56 private: 57 // The parent global object. The global will clear this when 58 // it is destroyed by calling DisconnectFromOwner(). 59 nsIGlobalObject* MOZ_NON_OWNING_REF mParentObject = nullptr; 60 // If mParentObject is or has been an inner window, then this is true. It is 61 // obtained in BindToOwner. 62 bool mHasOrHasHadOwnerWindow = false; 63 }; 64 65 } // namespace mozilla 66 67 #endif