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
#include "mozilla/dom/BrowsingContextGroup.h"
8
#include "mozilla/dom/BrowsingContextBinding.h"
9
#include "mozilla/dom/BindingUtils.h"
10
#include "mozilla/dom/ContentParent.h"
11
#include "mozilla/StaticPrefs_dom.h"
12
#include "mozilla/ThrottledEventQueue.h"
13
#include "nsFocusManager.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
BrowsingContextGroup::BrowsingContextGroup() {
19
if (XRE_IsContentProcess()) {
20
ContentChild::GetSingleton()->HoldBrowsingContextGroup(this);
21
}
22
}
23
24
bool BrowsingContextGroup::Contains(BrowsingContext* aBrowsingContext) {
25
return aBrowsingContext->Group() == this;
26
}
27
28
void BrowsingContextGroup::Register(BrowsingContext* aBrowsingContext) {
29
MOZ_DIAGNOSTIC_ASSERT(aBrowsingContext);
30
mContexts.PutEntry(aBrowsingContext);
31
}
32
33
void BrowsingContextGroup::Unregister(BrowsingContext* aBrowsingContext) {
34
MOZ_DIAGNOSTIC_ASSERT(aBrowsingContext);
35
mContexts.RemoveEntry(aBrowsingContext);
36
37
if (mContexts.IsEmpty()) {
38
// There are no browsing context still referencing this group. We can clear
39
// all subscribers.
40
UnsubscribeAllContentParents();
41
if (XRE_IsContentProcess()) {
42
ContentChild::GetSingleton()->ReleaseBrowsingContextGroup(this);
43
// We may have been deleted here as the ContentChild may have held the
44
// last references to `this`.
45
// Do not access any members at this point.
46
}
47
}
48
}
49
50
void BrowsingContextGroup::Subscribe(ContentParent* aOriginProcess) {
51
MOZ_DIAGNOSTIC_ASSERT(aOriginProcess);
52
mSubscribers.PutEntry(aOriginProcess);
53
aOriginProcess->OnBrowsingContextGroupSubscribe(this);
54
}
55
56
void BrowsingContextGroup::Unsubscribe(ContentParent* aOriginProcess) {
57
MOZ_DIAGNOSTIC_ASSERT(aOriginProcess);
58
mSubscribers.RemoveEntry(aOriginProcess);
59
aOriginProcess->OnBrowsingContextGroupUnsubscribe(this);
60
}
61
62
void BrowsingContextGroup::EnsureSubscribed(ContentParent* aProcess) {
63
MOZ_DIAGNOSTIC_ASSERT(aProcess);
64
if (mSubscribers.Contains(aProcess)) {
65
return;
66
}
67
68
Subscribe(aProcess);
69
70
bool sendFocused = false;
71
bool sendActive = false;
72
BrowsingContext* focused = nullptr;
73
BrowsingContext* active = nullptr;
74
nsFocusManager* fm = nsFocusManager::GetFocusManager();
75
if (fm) {
76
focused = fm->GetFocusedBrowsingContextInChrome();
77
active = fm->GetActiveBrowsingContextInChrome();
78
}
79
80
nsTArray<BrowsingContext::IPCInitializer> inits(mContexts.Count());
81
nsTArray<WindowContext::IPCInitializer> windowInits(mContexts.Count());
82
83
auto addInits = [&](BrowsingContext* aContext) {
84
inits.AppendElement(aContext->GetIPCInitializer());
85
if (focused == aContext) {
86
sendFocused = true;
87
}
88
if (active == aContext) {
89
sendActive = true;
90
}
91
for (auto& window : aContext->GetWindowContexts()) {
92
windowInits.AppendElement(window->GetIPCInitializer());
93
}
94
};
95
96
// First, perform a pre-order walk of our BrowsingContext objects from our
97
// toplevels. This should visit every active BrowsingContext.
98
for (auto& context : mToplevels) {
99
MOZ_DIAGNOSTIC_ASSERT(!IsContextCached(context),
100
"cached contexts must have a parent");
101
context->PreOrderWalk(addInits);
102
}
103
104
// Ensure that cached BrowsingContext objects are also visited, by visiting
105
// them after mToplevels.
106
for (auto iter = mCachedContexts.Iter(); !iter.Done(); iter.Next()) {
107
iter.Get()->GetKey()->PreOrderWalk(addInits);
108
}
109
110
// We should have visited every browsing context.
111
MOZ_DIAGNOSTIC_ASSERT(inits.Length() == mContexts.Count(),
112
"Visited the wrong number of contexts!");
113
114
// Send all of our contexts to the target content process.
115
Unused << aProcess->SendRegisterBrowsingContextGroup(inits, windowInits);
116
117
if (sendActive || sendFocused) {
118
Unused << aProcess->SendSetupFocusedAndActive(
119
sendFocused ? focused : nullptr, sendActive ? active : nullptr);
120
}
121
}
122
123
bool BrowsingContextGroup::IsContextCached(BrowsingContext* aContext) const {
124
MOZ_DIAGNOSTIC_ASSERT(aContext);
125
return mCachedContexts.Contains(aContext);
126
}
127
128
void BrowsingContextGroup::CacheContext(BrowsingContext* aContext) {
129
mCachedContexts.PutEntry(aContext);
130
}
131
132
void BrowsingContextGroup::CacheContexts(
133
const BrowsingContext::Children& aContexts) {
134
for (BrowsingContext* child : aContexts) {
135
mCachedContexts.PutEntry(child);
136
}
137
}
138
139
bool BrowsingContextGroup::EvictCachedContext(BrowsingContext* aContext) {
140
return mCachedContexts.EnsureRemoved(aContext);
141
}
142
143
BrowsingContextGroup::~BrowsingContextGroup() {
144
UnsubscribeAllContentParents();
145
}
146
147
void BrowsingContextGroup::UnsubscribeAllContentParents() {
148
for (auto iter = mSubscribers.Iter(); !iter.Done(); iter.Next()) {
149
nsRefPtrHashKey<ContentParent>* entry = iter.Get();
150
entry->GetKey()->OnBrowsingContextGroupUnsubscribe(this);
151
}
152
mSubscribers.Clear();
153
}
154
155
nsISupports* BrowsingContextGroup::GetParentObject() const {
156
return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
157
}
158
159
JSObject* BrowsingContextGroup::WrapObject(JSContext* aCx,
160
JS::Handle<JSObject*> aGivenProto) {
161
return BrowsingContextGroup_Binding::Wrap(aCx, this, aGivenProto);
162
}
163
164
nsresult BrowsingContextGroup::QueuePostMessageEvent(
165
already_AddRefed<nsIRunnable>&& aRunnable) {
166
if (StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) {
167
if (!mPostMessageEventQueue) {
168
nsCOMPtr<nsISerialEventTarget> target = GetMainThreadSerialEventTarget();
169
mPostMessageEventQueue = ThrottledEventQueue::Create(
170
target, "PostMessage Queue",
171
nsIRunnablePriority::PRIORITY_DEFERRED_TIMERS);
172
nsresult rv = mPostMessageEventQueue->SetIsPaused(false);
173
MOZ_ALWAYS_SUCCEEDS(rv);
174
}
175
176
// Ensure the queue is enabled
177
if (mPostMessageEventQueue->IsPaused()) {
178
nsresult rv = mPostMessageEventQueue->SetIsPaused(false);
179
MOZ_ALWAYS_SUCCEEDS(rv);
180
}
181
182
if (mPostMessageEventQueue) {
183
mPostMessageEventQueue->Dispatch(std::move(aRunnable),
184
NS_DISPATCH_NORMAL);
185
return NS_OK;
186
}
187
}
188
return NS_ERROR_FAILURE;
189
}
190
191
void BrowsingContextGroup::FlushPostMessageEvents() {
192
if (StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) {
193
if (mPostMessageEventQueue) {
194
nsresult rv = mPostMessageEventQueue->SetIsPaused(true);
195
MOZ_ALWAYS_SUCCEEDS(rv);
196
nsCOMPtr<nsIRunnable> event;
197
while ((event = mPostMessageEventQueue->GetEvent())) {
198
NS_DispatchToMainThread(event.forget());
199
}
200
}
201
}
202
}
203
204
static StaticRefPtr<BrowsingContextGroup> sChromeGroup;
205
206
/* static */
207
BrowsingContextGroup* BrowsingContextGroup::GetChromeGroup() {
208
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
209
if (!sChromeGroup && XRE_IsParentProcess()) {
210
sChromeGroup = new BrowsingContextGroup();
211
ClearOnShutdown(&sChromeGroup);
212
}
213
214
return sChromeGroup;
215
}
216
217
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BrowsingContextGroup, mContexts,
218
mToplevels, mSubscribers, mCachedContexts)
219
220
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(BrowsingContextGroup, AddRef)
221
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(BrowsingContextGroup, Release)
222
223
} // namespace dom
224
} // namespace mozilla