Backed out 4 changesets (bug 1825722) for causing reftest failures CLOSED TREE
[gecko.git] / dom / base / nsIGlobalObject.h
blobdc108981f7797355b46b1c71ac84cbf9e94b6e36
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"
20 #include "nsTArray.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 \
26 { \
27 0x11afa8be, 0xd997, 0x4e07, { \
28 0xa6, 0xa3, 0x6f, 0x87, 0x2e, 0xc3, 0xee, 0x7f \
29 } \
32 class nsCycleCollectionTraversalCallback;
33 class nsIPrincipal;
34 class nsPIDOMWindowInner;
36 namespace mozilla {
37 class DOMEventTargetHelper;
38 class GlobalTeardownObserver;
39 template <typename V, typename E>
40 class Result;
41 enum class StorageAccess;
42 namespace dom {
43 class VoidFunction;
44 class DebuggerNotificationManager;
45 class FontFaceSet;
46 class Function;
47 class Report;
48 class ReportBody;
49 class ReportingObserver;
50 class ServiceWorker;
51 class ServiceWorkerRegistration;
52 class ServiceWorkerRegistrationDescriptor;
53 class StorageManager;
54 enum class CallerType : uint32_t;
55 } // namespace dom
56 namespace ipc {
57 class PrincipalInfo;
58 } // namespace ipc
59 } // namespace mozilla
61 namespace JS::loader {
62 class ModuleLoaderBase;
63 } // namespace JS::loader
65 /**
66 * See <https://developer.mozilla.org/en-US/docs/Glossary/Global_object>.
68 class nsIGlobalObject : public nsISupports {
69 private:
70 nsTArray<nsCString> mHostObjectURIs;
72 // Raw pointers to bound DETH objects. These are added by
73 // AddGlobalTeardownObserver().
74 mozilla::LinkedList<mozilla::GlobalTeardownObserver> mGlobalTeardownObservers;
76 bool mIsDying;
77 bool mIsScriptForbidden;
79 protected:
80 bool mIsInnerWindow;
82 nsIGlobalObject();
84 public:
85 using RTPCallerType = mozilla::RTPCallerType;
86 using RFPTarget = mozilla::RFPTarget;
87 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)
89 /**
90 * This check is added to deal with Promise microtask queues. On the main
91 * thread, we do not impose restrictions about when script stops running or
92 * when runnables can no longer be dispatched to the main thread. This means
93 * it is possible for a Promise chain to keep resolving an infinite chain of
94 * promises, preventing the browser from shutting down. See Bug 1058695. To
95 * prevent this, the nsGlobalWindow subclass sets this flag when it is
96 * closed. The Promise implementation checks this and prohibits new runnables
97 * from being dispatched.
99 * We pair this with checks during processing the promise microtask queue
100 * that pops up the slow script dialog when the Promise queue is preventing
101 * a window from going away.
103 bool IsDying() const { return mIsDying; }
106 * Is it currently forbidden to call into script? JS-implemented WebIDL is
107 * a special case that's always allowed because it has the system principal,
108 * and callers should indicate this.
110 bool IsScriptForbidden(JSObject* aCallback,
111 bool aIsJSImplementedWebIDL = false) const;
114 * Return the JSObject for this global, if it still has one. Otherwise return
115 * null.
117 * If non-null is returned, then the returned object will have been already
118 * exposed to active JS, so callers do not need to do it.
120 virtual JSObject* GetGlobalJSObject() = 0;
123 * Return the JSObject for this global _without_ exposing it to active JS.
124 * This may return a gray object.
126 * This method is appropriate to use in assertions (so there is less of a
127 * difference in GC/CC marking between debug and optimized builds) and in
128 * situations where we are sure no CC activity can happen while the return
129 * value is used and the return value does not end up escaping to the heap in
130 * any way. In all other cases, and in particular in cases where the return
131 * value is held in a JS::Rooted or passed to the JSAutoRealm constructor, use
132 * GetGlobalJSObject.
134 virtual JSObject* GetGlobalJSObjectPreserveColor() const = 0;
137 * Check whether this nsIGlobalObject still has a JSObject associated with it,
138 * or whether it's torn-down enough that the JSObject is gone.
140 bool HasJSGlobal() const { return GetGlobalJSObjectPreserveColor(); }
142 virtual nsISerialEventTarget* SerialEventTarget() const = 0;
143 virtual nsresult Dispatch(already_AddRefed<nsIRunnable>&&) const = 0;
145 // This method is not meant to be overridden.
146 nsIPrincipal* PrincipalOrNull() const;
148 void RegisterHostObjectURI(const nsACString& aURI);
150 void UnregisterHostObjectURI(const nsACString& aURI);
152 // Any CC class inheriting nsIGlobalObject should call these 2 methods to
153 // cleanup objects stored in nsIGlobalObject such as blobURLs and Reports.
154 void UnlinkObjectsInGlobal();
155 void TraverseObjectsInGlobal(nsCycleCollectionTraversalCallback& aCb);
157 // GlobalTeardownObservers must register themselves on the global when they
158 // bind to it in order to get the DisconnectFromOwner() method
159 // called correctly. RemoveGlobalTeardownObserver() must be called
160 // before the GlobalTeardownObserver is destroyed.
161 void AddGlobalTeardownObserver(mozilla::GlobalTeardownObserver* aObject);
162 void RemoveGlobalTeardownObserver(mozilla::GlobalTeardownObserver* aObject);
164 // Iterate the registered GlobalTeardownObservers and call the given function
165 // for each one.
166 void ForEachGlobalTeardownObserver(
167 const std::function<void(mozilla::GlobalTeardownObserver*,
168 bool* aDoneOut)>& aFunc) const;
170 virtual bool IsInSyncOperation() { return false; }
172 virtual mozilla::dom::DebuggerNotificationManager*
173 GetOrCreateDebuggerNotificationManager() {
174 return nullptr;
177 virtual mozilla::dom::DebuggerNotificationManager*
178 GetExistingDebuggerNotificationManager() {
179 return nullptr;
182 virtual mozilla::Maybe<mozilla::dom::ClientInfo> GetClientInfo() const;
184 virtual mozilla::Maybe<nsID> GetAgentClusterId() const;
186 virtual bool CrossOriginIsolated() const { return false; }
188 virtual bool IsSharedMemoryAllowed() const { return false; }
190 virtual mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor> GetController()
191 const;
193 // Get the DOM object for the given descriptor or attempt to create one.
194 // Creation can still fail and return nullptr during shutdown, etc.
195 virtual RefPtr<mozilla::dom::ServiceWorker> GetOrCreateServiceWorker(
196 const mozilla::dom::ServiceWorkerDescriptor& aDescriptor);
198 // Get the DOM object for the given descriptor or return nullptr if it does
199 // not exist.
200 virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
201 GetServiceWorkerRegistration(
202 const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor)
203 const;
205 // Get the DOM object for the given descriptor or attempt to create one.
206 // Creation can still fail and return nullptr during shutdown, etc.
207 virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
208 GetOrCreateServiceWorkerRegistration(
209 const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor);
212 * Returns the storage access of this global.
214 * If you have a global that needs storage access, you should be overriding
215 * this method in your subclass of this class!
217 virtual mozilla::StorageAccess GetStorageAccess();
219 // Returns the set of active origin trials for this global.
220 virtual mozilla::OriginTrials Trials() const = 0;
222 // Returns a pointer to this object as an inner window if this is one or
223 // nullptr otherwise.
224 nsPIDOMWindowInner* GetAsInnerWindow();
226 void QueueMicrotask(mozilla::dom::VoidFunction& aCallback);
228 void RegisterReportingObserver(mozilla::dom::ReportingObserver* aObserver,
229 bool aBuffered);
231 void UnregisterReportingObserver(mozilla::dom::ReportingObserver* aObserver);
233 void BroadcastReport(mozilla::dom::Report* aReport);
235 MOZ_CAN_RUN_SCRIPT void NotifyReportingObservers();
237 void RemoveReportRecords();
239 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
240 // This function is set once by CountQueuingStrategy::GetSize.
241 already_AddRefed<mozilla::dom::Function>
242 GetCountQueuingStrategySizeFunction();
243 void SetCountQueuingStrategySizeFunction(mozilla::dom::Function* aFunction);
245 already_AddRefed<mozilla::dom::Function>
246 GetByteLengthQueuingStrategySizeFunction();
247 void SetByteLengthQueuingStrategySizeFunction(
248 mozilla::dom::Function* aFunction);
251 * Check whether we should avoid leaking distinguishing information to JS/CSS.
252 * https://w3c.github.io/fingerprinting-guidance/
254 virtual bool ShouldResistFingerprinting(RFPTarget aTarget) const = 0;
256 // CallerType::System callers never have to resist fingerprinting.
257 bool ShouldResistFingerprinting(mozilla::dom::CallerType aCallerType,
258 RFPTarget aTarget) const;
260 RTPCallerType GetRTPCallerType() const;
263 * Get the module loader to use for this global, if any. By default this
264 * returns null.
266 virtual JS::loader::ModuleLoaderBase* GetModuleLoader(JSContext* aCx) {
267 return nullptr;
270 virtual mozilla::dom::FontFaceSet* GetFonts() { return nullptr; }
272 virtual mozilla::Result<mozilla::ipc::PrincipalInfo, nsresult>
273 GetStorageKey();
274 mozilla::Result<bool, nsresult> HasEqualStorageKey(
275 const mozilla::ipc::PrincipalInfo& aStorageKey);
277 virtual mozilla::dom::StorageManager* GetStorageManager() { return nullptr; }
280 * https://html.spec.whatwg.org/multipage/web-messaging.html#eligible-for-messaging
281 * * a Window object whose associated Document is fully active, or
282 * * a WorkerGlobalScope object whose closing flag is false and whose worker
283 * is not a suspendable worker.
285 virtual bool IsEligibleForMessaging() { return false; };
287 protected:
288 virtual ~nsIGlobalObject();
290 void StartDying() { mIsDying = true; }
292 void StartForbiddingScript() { mIsScriptForbidden = true; }
293 void StopForbiddingScript() { mIsScriptForbidden = false; }
295 void DisconnectGlobalTeardownObservers();
297 size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aSizeOf) const;
299 private:
300 // List of Report objects for ReportingObservers.
301 nsTArray<RefPtr<mozilla::dom::ReportingObserver>> mReportingObservers;
302 nsTArray<RefPtr<mozilla::dom::Report>> mReportRecords;
304 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
305 RefPtr<mozilla::dom::Function> mCountQueuingStrategySizeFunction;
307 // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
308 RefPtr<mozilla::dom::Function> mByteLengthQueuingStrategySizeFunction;
311 NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject, NS_IGLOBALOBJECT_IID)
313 #endif // nsIGlobalObject_h__