Source code

Revision control

Other Tools

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/. */
6
7
#ifndef nsIGlobalObject_h__
8
#define nsIGlobalObject_h__
9
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"
21
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
} \
28
}
29
30
class nsCycleCollectionTraversalCallback;
31
class nsIPrincipal;
32
class nsPIDOMWindowInner;
33
34
namespace mozilla {
35
class DOMEventTargetHelper;
36
namespace dom {
37
class VoidFunction;
38
class DebuggerNotificationManager;
39
class ServiceWorker;
40
class ServiceWorkerRegistration;
41
class ServiceWorkerRegistrationDescriptor;
42
} // namespace dom
43
} // namespace mozilla
44
45
class nsIGlobalObject : public nsISupports,
46
public mozilla::dom::DispatcherTrait {
47
nsTArray<nsCString> mHostObjectURIs;
48
49
// Raw pointers to bound DETH objects. These are added by
50
// AddEventTargetObject().
51
mozilla::LinkedList<mozilla::DOMEventTargetHelper> mEventTargetObjects;
52
53
bool mIsDying;
54
55
protected:
56
bool mIsInnerWindow;
57
58
nsIGlobalObject() : mIsDying(false), mIsInnerWindow(false) {}
59
60
public:
61
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)
62
63
/**
64
* This check is added to deal with Promise microtask queues. On the main
65
* thread, we do not impose restrictions about when script stops running or
66
* when runnables can no longer be dispatched to the main thread. This means
67
* it is possible for a Promise chain to keep resolving an infinite chain of
68
* promises, preventing the browser from shutting down. See Bug 1058695. To
69
* prevent this, the nsGlobalWindow subclass sets this flag when it is
70
* closed. The Promise implementation checks this and prohibits new runnables
71
* from being dispatched.
72
*
73
* We pair this with checks during processing the promise microtask queue
74
* that pops up the slow script dialog when the Promise queue is preventing
75
* a window from going away.
76
*/
77
bool IsDying() const { return mIsDying; }
78
79
/**
80
* Return the JSObject for this global, if it still has one. Otherwise return
81
* null.
82
*
83
* If non-null is returned, then the returned object will have been already
84
* exposed to active JS, so callers do not need to do it.
85
*/
86
virtual JSObject* GetGlobalJSObject() = 0;
87
88
/**
89
* Return the JSObject for this global _without_ exposing it to active JS.
90
* This may return a gray object.
91
*
92
* This method is appropriate to use in assertions (so there is less of a
93
* difference in GC/CC marking between debug and optimized builds) and in
94
* situations where we are sure no CC activity can happen while the return
95
* value is used and the return value does not end up escaping to the heap in
96
* any way. In all other cases, and in particular in cases where the return
97
* value is held in a JS::Rooted or passed to the JSAutoRealm constructor, use
98
* GetGlobalJSObject.
99
*/
100
virtual JSObject* GetGlobalJSObjectPreserveColor() const = 0;
101
102
/**
103
* Check whether this nsIGlobalObject still has a JSObject associated with it,
104
* or whether it's torn-down enough that the JSObject is gone.
105
*/
106
bool HasJSGlobal() const { return GetGlobalJSObjectPreserveColor(); }
107
108
// This method is not meant to be overridden.
109
nsIPrincipal* PrincipalOrNull();
110
111
void RegisterHostObjectURI(const nsACString& aURI);
112
113
void UnregisterHostObjectURI(const nsACString& aURI);
114
115
// Any CC class inheriting nsIGlobalObject should call these 2 methods if it
116
// exposes the URL API.
117
void UnlinkHostObjectURIs();
118
void TraverseHostObjectURIs(nsCycleCollectionTraversalCallback& aCb);
119
120
// DETH objects must register themselves on the global when they
121
// bind to it in order to get the DisconnectFromOwner() method
122
// called correctly. RemoveEventTargetObject() must be called
123
// before the DETH object is destroyed.
124
void AddEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
125
void RemoveEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
126
127
// Iterate the registered DETH objects and call the given function
128
// for each one.
129
void ForEachEventTargetObject(
130
const std::function<void(mozilla::DOMEventTargetHelper*, bool* aDoneOut)>&
131
aFunc) const;
132
133
virtual bool IsInSyncOperation() { return false; }
134
135
virtual mozilla::dom::DebuggerNotificationManager*
136
GetOrCreateDebuggerNotificationManager() {
137
return nullptr;
138
}
139
140
virtual mozilla::dom::DebuggerNotificationManager*
141
GetExistingDebuggerNotificationManager() {
142
return nullptr;
143
}
144
145
virtual mozilla::Maybe<mozilla::dom::ClientInfo> GetClientInfo() const;
146
147
virtual mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor> GetController()
148
const;
149
150
// Get the DOM object for the given descriptor or attempt to create one.
151
// Creation can still fail and return nullptr during shutdown, etc.
152
virtual RefPtr<mozilla::dom::ServiceWorker> GetOrCreateServiceWorker(
153
const mozilla::dom::ServiceWorkerDescriptor& aDescriptor);
154
155
// Get the DOM object for the given descriptor or return nullptr if it does
156
// not exist.
157
virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
158
GetServiceWorkerRegistration(
159
const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor)
160
const;
161
162
// Get the DOM object for the given descriptor or attempt to create one.
163
// Creation can still fail and return nullptr during shutdown, etc.
164
virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
165
GetOrCreateServiceWorkerRegistration(
166
const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor);
167
168
// Returns a pointer to this object as an inner window if this is one or
169
// nullptr otherwise.
170
nsPIDOMWindowInner* AsInnerWindow();
171
172
void QueueMicrotask(mozilla::dom::VoidFunction& aCallback);
173
174
protected:
175
virtual ~nsIGlobalObject();
176
177
void StartDying() { mIsDying = true; }
178
179
void DisconnectEventTargetObjects();
180
181
size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aSizeOf) const;
182
};
183
184
NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject, NS_IGLOBALOBJECT_IID)
185
186
#endif // nsIGlobalObject_h__