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 nsIGlobalObject_h__
8 #define nsIGlobalObject_h__
10 #include "mozilla/LinkedList.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/dom/ClientInfo.h"
13 #include "mozilla/dom/DispatcherTrait.h"
14 #include "mozilla/dom/ServiceWorkerDescriptor.h"
15 #include "nsHashKeys.h"
16 #include "nsISupports.h"
17 #include "nsStringFwd.h"
19 #include "nsTHashtable.h"
20 #include "js/TypeDecls.h"
22 // Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
23 #define NS_IGLOBALOBJECT_IID \
25 0x11afa8be, 0xd997, 0x4e07, { \
26 0xa6, 0xa3, 0x6f, 0x87, 0x2e, 0xc3, 0xee, 0x7f \
30 class nsCycleCollectionTraversalCallback
;
32 class nsPIDOMWindowInner
;
35 class DOMEventTargetHelper
;
38 class DebuggerNotificationManager
;
41 class ReportingObserver
;
43 class ServiceWorkerRegistration
;
44 class ServiceWorkerRegistrationDescriptor
;
46 } // namespace mozilla
48 class nsIGlobalObject
: public nsISupports
,
49 public mozilla::dom::DispatcherTrait
{
50 nsTArray
<nsCString
> mHostObjectURIs
;
52 // Raw pointers to bound DETH objects. These are added by
53 // AddEventTargetObject().
54 mozilla::LinkedList
<mozilla::DOMEventTargetHelper
> mEventTargetObjects
;
57 bool mIsScriptForbidden
;
65 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID
)
68 * This check is added to deal with Promise microtask queues. On the main
69 * thread, we do not impose restrictions about when script stops running or
70 * when runnables can no longer be dispatched to the main thread. This means
71 * it is possible for a Promise chain to keep resolving an infinite chain of
72 * promises, preventing the browser from shutting down. See Bug 1058695. To
73 * prevent this, the nsGlobalWindow subclass sets this flag when it is
74 * closed. The Promise implementation checks this and prohibits new runnables
75 * from being dispatched.
77 * We pair this with checks during processing the promise microtask queue
78 * that pops up the slow script dialog when the Promise queue is preventing
79 * a window from going away.
81 bool IsDying() const { return mIsDying
; }
84 * Is it currently forbidden to call into script? JS-implemented WebIDL is
85 * a special case that's always allowed because it has the system principal,
86 * and callers should indicate this.
88 bool IsScriptForbidden(JSObject
* aCallback
,
89 bool aIsJSImplementedWebIDL
= false) const;
92 * Return the JSObject for this global, if it still has one. Otherwise return
95 * If non-null is returned, then the returned object will have been already
96 * exposed to active JS, so callers do not need to do it.
98 virtual JSObject
* GetGlobalJSObject() = 0;
101 * Return the JSObject for this global _without_ exposing it to active JS.
102 * This may return a gray object.
104 * This method is appropriate to use in assertions (so there is less of a
105 * difference in GC/CC marking between debug and optimized builds) and in
106 * situations where we are sure no CC activity can happen while the return
107 * value is used and the return value does not end up escaping to the heap in
108 * any way. In all other cases, and in particular in cases where the return
109 * value is held in a JS::Rooted or passed to the JSAutoRealm constructor, use
112 virtual JSObject
* GetGlobalJSObjectPreserveColor() const = 0;
115 * Check whether this nsIGlobalObject still has a JSObject associated with it,
116 * or whether it's torn-down enough that the JSObject is gone.
118 bool HasJSGlobal() const { return GetGlobalJSObjectPreserveColor(); }
120 // This method is not meant to be overridden.
121 nsIPrincipal
* PrincipalOrNull();
123 void RegisterHostObjectURI(const nsACString
& aURI
);
125 void UnregisterHostObjectURI(const nsACString
& aURI
);
127 // Any CC class inheriting nsIGlobalObject should call these 2 methods to
128 // cleanup objects stored in nsIGlobalObject such as blobURLs and Reports.
129 void UnlinkObjectsInGlobal();
130 void TraverseObjectsInGlobal(nsCycleCollectionTraversalCallback
& aCb
);
132 // DETH objects must register themselves on the global when they
133 // bind to it in order to get the DisconnectFromOwner() method
134 // called correctly. RemoveEventTargetObject() must be called
135 // before the DETH object is destroyed.
136 void AddEventTargetObject(mozilla::DOMEventTargetHelper
* aObject
);
137 void RemoveEventTargetObject(mozilla::DOMEventTargetHelper
* aObject
);
139 // Iterate the registered DETH objects and call the given function
141 void ForEachEventTargetObject(
142 const std::function
<void(mozilla::DOMEventTargetHelper
*, bool* aDoneOut
)>&
145 virtual bool IsInSyncOperation() { return false; }
147 virtual mozilla::dom::DebuggerNotificationManager
*
148 GetOrCreateDebuggerNotificationManager() {
152 virtual mozilla::dom::DebuggerNotificationManager
*
153 GetExistingDebuggerNotificationManager() {
157 virtual mozilla::Maybe
<mozilla::dom::ClientInfo
> GetClientInfo() const;
159 virtual mozilla::Maybe
<nsID
> GetAgentClusterId() const;
161 virtual bool CrossOriginIsolated() const { return false; }
163 virtual bool IsSharedMemoryAllowed() const { return false; }
165 virtual mozilla::Maybe
<mozilla::dom::ServiceWorkerDescriptor
> GetController()
168 // Get the DOM object for the given descriptor or attempt to create one.
169 // Creation can still fail and return nullptr during shutdown, etc.
170 virtual RefPtr
<mozilla::dom::ServiceWorker
> GetOrCreateServiceWorker(
171 const mozilla::dom::ServiceWorkerDescriptor
& aDescriptor
);
173 // Get the DOM object for the given descriptor or return nullptr if it does
175 virtual RefPtr
<mozilla::dom::ServiceWorkerRegistration
>
176 GetServiceWorkerRegistration(
177 const mozilla::dom::ServiceWorkerRegistrationDescriptor
& aDescriptor
)
180 // Get the DOM object for the given descriptor or attempt to create one.
181 // Creation can still fail and return nullptr during shutdown, etc.
182 virtual RefPtr
<mozilla::dom::ServiceWorkerRegistration
>
183 GetOrCreateServiceWorkerRegistration(
184 const mozilla::dom::ServiceWorkerRegistrationDescriptor
& aDescriptor
);
186 // Returns a pointer to this object as an inner window if this is one or
187 // nullptr otherwise.
188 nsPIDOMWindowInner
* AsInnerWindow();
190 void QueueMicrotask(mozilla::dom::VoidFunction
& aCallback
);
192 void RegisterReportingObserver(mozilla::dom::ReportingObserver
* aObserver
,
195 void UnregisterReportingObserver(mozilla::dom::ReportingObserver
* aObserver
);
197 void BroadcastReport(mozilla::dom::Report
* aReport
);
199 MOZ_CAN_RUN_SCRIPT
void NotifyReportingObservers();
201 void RemoveReportRecords();
204 virtual ~nsIGlobalObject();
206 void StartDying() { mIsDying
= true; }
208 void StartForbiddingScript() { mIsScriptForbidden
= true; }
209 void StopForbiddingScript() { mIsScriptForbidden
= false; }
211 void DisconnectEventTargetObjects();
213 size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aSizeOf
) const;
216 // List of Report objects for ReportingObservers.
217 nsTArray
<RefPtr
<mozilla::dom::ReportingObserver
>> mReportingObservers
;
218 nsTArray
<RefPtr
<mozilla::dom::Report
>> mReportRecords
;
221 NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject
, NS_IGLOBALOBJECT_IID
)
223 #endif // nsIGlobalObject_h__