Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / base / GlobalTeardownObserver.h
blobe6e3a562991a964494895bb0bda0e45fe41eefb9
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/. */
7 #ifndef DOM_BASE_GLOBALTEARDOWNOBSERVER_H_
8 #define DOM_BASE_GLOBALTEARDOWNOBSERVER_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/EventTarget.h"
12 #include "mozilla/RefPtr.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsID.h"
15 #include "nsIGlobalObject.h"
16 #include "nsIScriptGlobalObject.h"
17 #include "nsISupports.h"
18 #include "nsISupportsUtils.h"
19 #include "nsPIDOMWindow.h"
21 #define NS_GLOBALTEARDOWNOBSERVER_IID \
22 { \
23 0xc31fddb9, 0xec49, 0x4f24, { \
24 0x90, 0x16, 0xb5, 0x2b, 0x26, 0x6c, 0xb6, 0x29 \
25 } \
28 namespace mozilla {
30 class GlobalTeardownObserver
31 : public nsISupports,
32 public LinkedListElement<GlobalTeardownObserver> {
33 public:
34 NS_DECLARE_STATIC_IID_ACCESSOR(NS_GLOBALTEARDOWNOBSERVER_IID)
36 GlobalTeardownObserver();
37 explicit GlobalTeardownObserver(nsIGlobalObject* aGlobalObject,
38 bool aHasOrHasHadOwnerWindow = false);
40 nsPIDOMWindowInner* GetOwner() const { return mOwnerWindow; }
41 nsIGlobalObject* GetOwnerGlobal() const { return mParentObject; }
42 bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow; }
44 void GetParentObject(nsIScriptGlobalObject** aParentObject) {
45 if (mParentObject) {
46 CallQueryInterface(mParentObject, aParentObject);
47 } else {
48 *aParentObject = nullptr;
52 virtual void DisconnectFromOwner();
54 // A global permanently becomes invalid when DisconnectEventTargetObjects() is
55 // called. Normally this means:
56 // - For the main thread, when nsGlobalWindowInner::FreeInnerObjects is
57 // called.
58 // - For a worker thread, when clearing the main event queue. (Which we do
59 // slightly later than when the spec notionally calls for it to be done.)
61 // A global may also become temporarily invalid when:
62 // - For the main thread, if the window is no longer the WindowProxy's current
63 // inner window due to being placed in the bfcache.
64 nsresult CheckCurrentGlobalCorrectness() const;
66 protected:
67 virtual ~GlobalTeardownObserver();
69 void BindToOwner(nsIGlobalObject* aOwner);
71 private:
72 // The parent global object. The global will clear this when
73 // it is destroyed by calling DisconnectFromOwner().
74 nsIGlobalObject* MOZ_NON_OWNING_REF mParentObject = nullptr;
75 // mParentObject pre QI-ed and cached (inner window)
76 // (it is needed for off main thread access)
77 // It is obtained in BindToOwner and reset in DisconnectFromOwner.
78 nsPIDOMWindowInner* MOZ_NON_OWNING_REF mOwnerWindow = nullptr;
79 bool mHasOrHasHadOwnerWindow = false;
82 NS_DEFINE_STATIC_IID_ACCESSOR(GlobalTeardownObserver,
83 NS_GLOBALTEARDOWNOBSERVER_IID)
85 } // namespace mozilla
87 #endif