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/ServiceWorkerDescriptor.h"
14 #include "mozilla/OriginTrials.h"
15 #include "nsContentUtils.h"
16 #include "nsHashKeys.h"
17 #include "nsISupports.h"
18 #include "nsRFPService.h"
19 #include "nsStringFwd.h"
21 #include "nsTHashtable.h"
22 #include "js/TypeDecls.h"
24 // Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
25 #define NS_IGLOBALOBJECT_IID \
27 0x11afa8be, 0xd997, 0x4e07, { \
28 0xa6, 0xa3, 0x6f, 0x87, 0x2e, 0xc3, 0xee, 0x7f \
32 class nsCycleCollectionTraversalCallback
;
34 class nsPIDOMWindowInner
;
37 class DOMEventTargetHelper
;
38 template <typename V
, typename E
>
40 enum class StorageAccess
;
43 class DebuggerNotificationManager
;
48 class ReportingObserver
;
50 class ServiceWorkerRegistration
;
51 class ServiceWorkerRegistrationDescriptor
;
53 enum class CallerType
: uint32_t;
58 } // namespace mozilla
60 namespace JS::loader
{
61 class ModuleLoaderBase
;
62 } // namespace JS::loader
65 * See <https://developer.mozilla.org/en-US/docs/Glossary/Global_object>.
67 class nsIGlobalObject
: public nsISupports
{
69 nsTArray
<nsCString
> mHostObjectURIs
;
71 // Raw pointers to bound DETH objects. These are added by
72 // AddEventTargetObject().
73 mozilla::LinkedList
<mozilla::DOMEventTargetHelper
> mEventTargetObjects
;
76 bool mIsScriptForbidden
;
84 using RTPCallerType
= mozilla::RTPCallerType
;
85 using RFPTarget
= mozilla::RFPTarget
;
86 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID
)
89 * This check is added to deal with Promise microtask queues. On the main
90 * thread, we do not impose restrictions about when script stops running or
91 * when runnables can no longer be dispatched to the main thread. This means
92 * it is possible for a Promise chain to keep resolving an infinite chain of
93 * promises, preventing the browser from shutting down. See Bug 1058695. To
94 * prevent this, the nsGlobalWindow subclass sets this flag when it is
95 * closed. The Promise implementation checks this and prohibits new runnables
96 * from being dispatched.
98 * We pair this with checks during processing the promise microtask queue
99 * that pops up the slow script dialog when the Promise queue is preventing
100 * a window from going away.
102 bool IsDying() const { return mIsDying
; }
105 * Is it currently forbidden to call into script? JS-implemented WebIDL is
106 * a special case that's always allowed because it has the system principal,
107 * and callers should indicate this.
109 bool IsScriptForbidden(JSObject
* aCallback
,
110 bool aIsJSImplementedWebIDL
= false) const;
113 * Return the JSObject for this global, if it still has one. Otherwise return
116 * If non-null is returned, then the returned object will have been already
117 * exposed to active JS, so callers do not need to do it.
119 virtual JSObject
* GetGlobalJSObject() = 0;
122 * Return the JSObject for this global _without_ exposing it to active JS.
123 * This may return a gray object.
125 * This method is appropriate to use in assertions (so there is less of a
126 * difference in GC/CC marking between debug and optimized builds) and in
127 * situations where we are sure no CC activity can happen while the return
128 * value is used and the return value does not end up escaping to the heap in
129 * any way. In all other cases, and in particular in cases where the return
130 * value is held in a JS::Rooted or passed to the JSAutoRealm constructor, use
133 virtual JSObject
* GetGlobalJSObjectPreserveColor() const = 0;
136 * Check whether this nsIGlobalObject still has a JSObject associated with it,
137 * or whether it's torn-down enough that the JSObject is gone.
139 bool HasJSGlobal() const { return GetGlobalJSObjectPreserveColor(); }
141 virtual nsISerialEventTarget
* SerialEventTarget() const = 0;
142 virtual nsresult
Dispatch(already_AddRefed
<nsIRunnable
>&&) const = 0;
144 // This method is not meant to be overridden.
145 nsIPrincipal
* PrincipalOrNull() const;
147 void RegisterHostObjectURI(const nsACString
& aURI
);
149 void UnregisterHostObjectURI(const nsACString
& aURI
);
151 // Any CC class inheriting nsIGlobalObject should call these 2 methods to
152 // cleanup objects stored in nsIGlobalObject such as blobURLs and Reports.
153 void UnlinkObjectsInGlobal();
154 void TraverseObjectsInGlobal(nsCycleCollectionTraversalCallback
& aCb
);
156 // DETH objects must register themselves on the global when they
157 // bind to it in order to get the DisconnectFromOwner() method
158 // called correctly. RemoveEventTargetObject() must be called
159 // before the DETH object is destroyed.
160 void AddEventTargetObject(mozilla::DOMEventTargetHelper
* aObject
);
161 void RemoveEventTargetObject(mozilla::DOMEventTargetHelper
* aObject
);
163 // Iterate the registered DETH objects and call the given function
165 void ForEachEventTargetObject(
166 const std::function
<void(mozilla::DOMEventTargetHelper
*, bool* aDoneOut
)>&
169 virtual bool IsInSyncOperation() { return false; }
171 virtual mozilla::dom::DebuggerNotificationManager
*
172 GetOrCreateDebuggerNotificationManager() {
176 virtual mozilla::dom::DebuggerNotificationManager
*
177 GetExistingDebuggerNotificationManager() {
181 virtual mozilla::Maybe
<mozilla::dom::ClientInfo
> GetClientInfo() const;
183 virtual mozilla::Maybe
<nsID
> GetAgentClusterId() const;
185 virtual bool CrossOriginIsolated() const { return false; }
187 virtual bool IsSharedMemoryAllowed() const { return false; }
189 virtual mozilla::Maybe
<mozilla::dom::ServiceWorkerDescriptor
> GetController()
192 // Get the DOM object for the given descriptor or attempt to create one.
193 // Creation can still fail and return nullptr during shutdown, etc.
194 virtual RefPtr
<mozilla::dom::ServiceWorker
> GetOrCreateServiceWorker(
195 const mozilla::dom::ServiceWorkerDescriptor
& aDescriptor
);
197 // Get the DOM object for the given descriptor or return nullptr if it does
199 virtual RefPtr
<mozilla::dom::ServiceWorkerRegistration
>
200 GetServiceWorkerRegistration(
201 const mozilla::dom::ServiceWorkerRegistrationDescriptor
& aDescriptor
)
204 // Get the DOM object for the given descriptor or attempt to create one.
205 // Creation can still fail and return nullptr during shutdown, etc.
206 virtual RefPtr
<mozilla::dom::ServiceWorkerRegistration
>
207 GetOrCreateServiceWorkerRegistration(
208 const mozilla::dom::ServiceWorkerRegistrationDescriptor
& aDescriptor
);
211 * Returns the storage access of this global.
213 * If you have a global that needs storage access, you should be overriding
214 * this method in your subclass of this class!
216 virtual mozilla::StorageAccess
GetStorageAccess();
218 // Returns the set of active origin trials for this global.
219 virtual mozilla::OriginTrials
Trials() const = 0;
221 // Returns a pointer to this object as an inner window if this is one or
222 // nullptr otherwise.
223 nsPIDOMWindowInner
* GetAsInnerWindow();
225 void QueueMicrotask(mozilla::dom::VoidFunction
& aCallback
);
227 void RegisterReportingObserver(mozilla::dom::ReportingObserver
* aObserver
,
230 void UnregisterReportingObserver(mozilla::dom::ReportingObserver
* aObserver
);
232 void BroadcastReport(mozilla::dom::Report
* aReport
);
234 MOZ_CAN_RUN_SCRIPT
void NotifyReportingObservers();
236 void RemoveReportRecords();
238 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
239 // This function is set once by CountQueuingStrategy::GetSize.
240 already_AddRefed
<mozilla::dom::Function
>
241 GetCountQueuingStrategySizeFunction();
242 void SetCountQueuingStrategySizeFunction(mozilla::dom::Function
* aFunction
);
244 already_AddRefed
<mozilla::dom::Function
>
245 GetByteLengthQueuingStrategySizeFunction();
246 void SetByteLengthQueuingStrategySizeFunction(
247 mozilla::dom::Function
* aFunction
);
250 * Check whether we should avoid leaking distinguishing information to JS/CSS.
251 * https://w3c.github.io/fingerprinting-guidance/
253 virtual bool ShouldResistFingerprinting(RFPTarget aTarget
) const = 0;
255 // CallerType::System callers never have to resist fingerprinting.
256 bool ShouldResistFingerprinting(mozilla::dom::CallerType aCallerType
,
257 RFPTarget aTarget
) const;
259 RTPCallerType
GetRTPCallerType() const;
262 * Get the module loader to use for this global, if any. By default this
265 virtual JS::loader::ModuleLoaderBase
* GetModuleLoader(JSContext
* aCx
) {
269 virtual mozilla::dom::FontFaceSet
* GetFonts() { return nullptr; }
271 virtual mozilla::Result
<mozilla::ipc::PrincipalInfo
, nsresult
>
273 mozilla::Result
<bool, nsresult
> HasEqualStorageKey(
274 const mozilla::ipc::PrincipalInfo
& aStorageKey
);
276 virtual mozilla::dom::StorageManager
* GetStorageManager() { return nullptr; }
279 * https://html.spec.whatwg.org/multipage/web-messaging.html#eligible-for-messaging
280 * * a Window object whose associated Document is fully active, or
281 * * a WorkerGlobalScope object whose closing flag is false and whose worker
282 * is not a suspendable worker.
284 virtual bool IsEligibleForMessaging() { return false; };
287 virtual ~nsIGlobalObject();
289 void StartDying() { mIsDying
= true; }
291 void StartForbiddingScript() { mIsScriptForbidden
= true; }
292 void StopForbiddingScript() { mIsScriptForbidden
= false; }
294 void DisconnectEventTargetObjects();
296 size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aSizeOf
) const;
299 // List of Report objects for ReportingObservers.
300 nsTArray
<RefPtr
<mozilla::dom::ReportingObserver
>> mReportingObservers
;
301 nsTArray
<RefPtr
<mozilla::dom::Report
>> mReportRecords
;
303 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
304 RefPtr
<mozilla::dom::Function
> mCountQueuingStrategySizeFunction
;
306 // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
307 RefPtr
<mozilla::dom::Function
> mByteLengthQueuingStrategySizeFunction
;
310 NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject
, NS_IGLOBALOBJECT_IID
)
312 #endif // nsIGlobalObject_h__