Bug 1728955: part 3) Add logging to `nsBaseClipboard`. r=masayuki
[gecko.git] / dom / base / nsIGlobalObject.h
blob4c2a0833224ee438a1e987194e07f2611180d331
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"
18 #include "nsTArray.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 \
24 { \
25 0x11afa8be, 0xd997, 0x4e07, { \
26 0xa6, 0xa3, 0x6f, 0x87, 0x2e, 0xc3, 0xee, 0x7f \
27 } \
30 class nsCycleCollectionTraversalCallback;
31 class nsIPrincipal;
32 class nsPIDOMWindowInner;
34 namespace mozilla {
35 class DOMEventTargetHelper;
36 namespace dom {
37 class VoidFunction;
38 class DebuggerNotificationManager;
39 class Report;
40 class ReportBody;
41 class ReportingObserver;
42 class ServiceWorker;
43 class ServiceWorkerRegistration;
44 class ServiceWorkerRegistrationDescriptor;
45 } // namespace dom
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;
56 bool mIsDying;
57 bool mIsScriptForbidden;
59 protected:
60 bool mIsInnerWindow;
62 nsIGlobalObject();
64 public:
65 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)
67 /**
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; }
83 /**
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;
91 /**
92 * Return the JSObject for this global, if it still has one. Otherwise return
93 * null.
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
110 * GetGlobalJSObject.
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
140 // for each one.
141 void ForEachEventTargetObject(
142 const std::function<void(mozilla::DOMEventTargetHelper*, bool* aDoneOut)>&
143 aFunc) const;
145 virtual bool IsInSyncOperation() { return false; }
147 virtual mozilla::dom::DebuggerNotificationManager*
148 GetOrCreateDebuggerNotificationManager() {
149 return nullptr;
152 virtual mozilla::dom::DebuggerNotificationManager*
153 GetExistingDebuggerNotificationManager() {
154 return nullptr;
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()
166 const;
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
174 // not exist.
175 virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
176 GetServiceWorkerRegistration(
177 const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor)
178 const;
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,
193 bool aBuffered);
195 void UnregisterReportingObserver(mozilla::dom::ReportingObserver* aObserver);
197 void BroadcastReport(mozilla::dom::Report* aReport);
199 MOZ_CAN_RUN_SCRIPT void NotifyReportingObservers();
201 void RemoveReportRecords();
203 protected:
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;
215 private:
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__