GlobalFreezeObserver.h (1871B)
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_GLOBALFREEZEOBSERVER_H_ 8 #define DOM_BASE_GLOBALFREEZEOBSERVER_H_ 9 10 #include "mozilla/Attributes.h" 11 #include "nsIGlobalObject.h" 12 #include "nsISupports.h" 13 14 namespace mozilla { 15 16 class GlobalFreezeObserver : public nsISupports, 17 public LinkedListElement<GlobalFreezeObserver> { 18 public: 19 virtual void FrozenCallback(nsIGlobalObject* aOwner) = 0; 20 virtual void ThawedCallback(nsIGlobalObject* aOwner) {}; 21 22 bool Observing() { return !!mOwner; } 23 24 /** 25 * This method is non-virtual because it's expected that any object 26 * subclassing GlobalFreezeObserver that wants to know when it is disconnected 27 * from the global will also subclass GlobalTeardownObserver and take any 28 * relevant action by overriding GlobalTeardownObserver::DisconnectFromOwner. 29 */ 30 void DisconnectFreezeObserver() { 31 if (mOwner) { 32 mOwner->RemoveGlobalFreezeObserver(this); 33 mOwner = nullptr; 34 } 35 } 36 37 protected: 38 virtual ~GlobalFreezeObserver() { DisconnectFreezeObserver(); } 39 40 void BindToOwner(nsIGlobalObject* aOwner) { 41 MOZ_ASSERT(!mOwner); 42 43 if (aOwner) { 44 MOZ_ASSERT( 45 NS_IsMainThread(), 46 "GlobalFreezeObserver is currently only supported in window object"); 47 mOwner = aOwner; 48 aOwner->AddGlobalFreezeObserver(this); 49 } 50 } 51 52 private: 53 // The parent global object. The global will clear this when 54 // it is destroyed by calling DisconnectFreezeObserver(). 55 nsIGlobalObject* MOZ_NON_OWNING_REF mOwner = nullptr; 56 }; 57 58 } // namespace mozilla 59 60 #endif