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
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/dom/BrowsingContextBinding.h"
11 #include "mozilla/dom/BindingUtils.h"
12 #include "mozilla/dom/ContentChild.h"
13 #include "mozilla/dom/ContentParent.h"
14 #include "mozilla/dom/DocGroup.h"
15 #include "mozilla/StaticPrefs_dom.h"
16 #include "mozilla/ThrottledEventQueue.h"
17 #include "nsFocusManager.h"
18
19 namespace mozilla {
20 namespace dom {
21
22 static StaticRefPtr<BrowsingContextGroup> sChromeGroup;
23
24 static StaticAutoPtr<
25 nsDataHashtable<nsUint64HashKey, RefPtr<BrowsingContextGroup>>>
26 sBrowsingContextGroups;
27
GetOrCreate(uint64_t aId)28 already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::GetOrCreate(
29 uint64_t aId) {
30 if (!sBrowsingContextGroups) {
31 sBrowsingContextGroups =
32 new nsDataHashtable<nsUint64HashKey, RefPtr<BrowsingContextGroup>>();
33 ClearOnShutdown(&sBrowsingContextGroups);
34 }
35
36 auto entry = sBrowsingContextGroups->LookupForAdd(aId);
37 RefPtr<BrowsingContextGroup> group =
38 entry.OrInsert([&] { return do_AddRef(new BrowsingContextGroup(aId)); });
39 return group.forget();
40 }
41
Create()42 already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::Create() {
43 return GetOrCreate(nsContentUtils::GenerateBrowsingContextId());
44 }
45
BrowsingContextGroup(uint64_t aId)46 BrowsingContextGroup::BrowsingContextGroup(uint64_t aId) : mId(aId) {
47 mTimerEventQueue = ThrottledEventQueue::Create(
48 GetMainThreadSerialEventTarget(), "BrowsingContextGroup timer queue");
49
50 mWorkerEventQueue = ThrottledEventQueue::Create(
51 GetMainThreadSerialEventTarget(), "BrowsingContextGroup worker queue");
52 }
53
Register(nsISupports * aContext)54 void BrowsingContextGroup::Register(nsISupports* aContext) {
55 MOZ_DIAGNOSTIC_ASSERT(aContext);
56 mContexts.PutEntry(aContext);
57 }
58
Unregister(nsISupports * aContext)59 void BrowsingContextGroup::Unregister(nsISupports* aContext) {
60 MOZ_DIAGNOSTIC_ASSERT(aContext);
61 mContexts.RemoveEntry(aContext);
62
63 if (mContexts.IsEmpty()) {
64 // There are no synced contexts still referencing this group. We can clear
65 // all subscribers.
66 UnsubscribeAllContentParents();
67
68 // We may have been deleted here as the ContentChild/Parent may
69 // have held the last references to `this`.
70 // Do not access any members at this point.
71 }
72 }
73
Subscribe(ContentParent * aOriginProcess)74 void BrowsingContextGroup::Subscribe(ContentParent* aOriginProcess) {
75 MOZ_DIAGNOSTIC_ASSERT(aOriginProcess);
76 mSubscribers.PutEntry(aOriginProcess);
77 aOriginProcess->OnBrowsingContextGroupSubscribe(this);
78 }
79
Unsubscribe(ContentParent * aOriginProcess)80 void BrowsingContextGroup::Unsubscribe(ContentParent* aOriginProcess) {
81 MOZ_DIAGNOSTIC_ASSERT(aOriginProcess);
82 mSubscribers.RemoveEntry(aOriginProcess);
83 aOriginProcess->OnBrowsingContextGroupUnsubscribe(this);
84
85 // If this origin process embeds any non-discarded windowless
86 // BrowsingContexts, make sure to discard them, as this process is going away.
87 // Nested subframes will be discarded by WindowGlobalParent when it is
88 // destroyed by IPC.
89 nsTArray<RefPtr<BrowsingContext>> toDiscard;
90 for (auto& context : mToplevels) {
91 if (context->Canonical()->IsEmbeddedInProcess(aOriginProcess->ChildID())) {
92 toDiscard.AppendElement(context);
93 }
94 }
95 for (auto& context : toDiscard) {
96 context->Detach(/* aFromIPC */ true);
97 }
98 }
99
CollectContextInitializers(Span<RefPtr<BrowsingContext>> aContexts,nsTArray<SyncedContextInitializer> & aInits)100 static void CollectContextInitializers(
101 Span<RefPtr<BrowsingContext>> aContexts,
102 nsTArray<SyncedContextInitializer>& aInits) {
103 // The order that we record these initializers is important, as it will keep
104 // the order that children are attached to their parent in the newly connected
105 // content process consistent.
106 for (auto& context : aContexts) {
107 aInits.AppendElement(context->GetIPCInitializer());
108 for (auto& window : context->GetWindowContexts()) {
109 aInits.AppendElement(window->GetIPCInitializer());
110 CollectContextInitializers(window->Children(), aInits);
111 }
112 }
113 }
114
EnsureSubscribed(ContentParent * aProcess)115 void BrowsingContextGroup::EnsureSubscribed(ContentParent* aProcess) {
116 MOZ_DIAGNOSTIC_ASSERT(aProcess);
117 if (mSubscribers.Contains(aProcess)) {
118 return;
119 }
120
121 Subscribe(aProcess);
122
123 // FIXME: This won't send non-discarded children of discarded BCs, but those
124 // BCs will be in the process of being destroyed anyway.
125 // FIXME: Prevent that situation from occuring.
126 nsTArray<SyncedContextInitializer> inits(mContexts.Count());
127 CollectContextInitializers(mToplevels, inits);
128
129 // Send all of our contexts to the target content process.
130 Unused << aProcess->SendRegisterBrowsingContextGroup(Id(), inits);
131
132 // If the focused or active BrowsingContexts belong in this group, tell the
133 // newly subscribed process.
134 if (nsFocusManager* fm = nsFocusManager::GetFocusManager()) {
135 BrowsingContext* focused = fm->GetFocusedBrowsingContextInChrome();
136 if (focused && focused->Group() != this) {
137 focused = nullptr;
138 }
139 BrowsingContext* active = fm->GetActiveBrowsingContextInChrome();
140 if (active && active->Group() != this) {
141 active = nullptr;
142 }
143
144 if (focused || active) {
145 Unused << aProcess->SendSetupFocusedAndActive(focused, active);
146 }
147 }
148 }
149
~BrowsingContextGroup()150 BrowsingContextGroup::~BrowsingContextGroup() {
151 UnsubscribeAllContentParents();
152 }
153
UnsubscribeAllContentParents()154 void BrowsingContextGroup::UnsubscribeAllContentParents() {
155 if (sBrowsingContextGroups) {
156 sBrowsingContextGroups->Remove(Id());
157 }
158
159 for (auto iter = mSubscribers.Iter(); !iter.Done(); iter.Next()) {
160 nsRefPtrHashKey<ContentParent>* entry = iter.Get();
161 entry->GetKey()->OnBrowsingContextGroupUnsubscribe(this);
162 }
163 mSubscribers.Clear();
164 }
165
GetParentObject() const166 nsISupports* BrowsingContextGroup::GetParentObject() const {
167 return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
168 }
169
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)170 JSObject* BrowsingContextGroup::WrapObject(JSContext* aCx,
171 JS::Handle<JSObject*> aGivenProto) {
172 return BrowsingContextGroup_Binding::Wrap(aCx, this, aGivenProto);
173 }
174
QueuePostMessageEvent(already_AddRefed<nsIRunnable> && aRunnable)175 nsresult BrowsingContextGroup::QueuePostMessageEvent(
176 already_AddRefed<nsIRunnable>&& aRunnable) {
177 if (StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) {
178 if (!mPostMessageEventQueue) {
179 nsCOMPtr<nsISerialEventTarget> target = GetMainThreadSerialEventTarget();
180 mPostMessageEventQueue = ThrottledEventQueue::Create(
181 target, "PostMessage Queue",
182 nsIRunnablePriority::PRIORITY_DEFERRED_TIMERS);
183 nsresult rv = mPostMessageEventQueue->SetIsPaused(false);
184 MOZ_ALWAYS_SUCCEEDS(rv);
185 }
186
187 // Ensure the queue is enabled
188 if (mPostMessageEventQueue->IsPaused()) {
189 nsresult rv = mPostMessageEventQueue->SetIsPaused(false);
190 MOZ_ALWAYS_SUCCEEDS(rv);
191 }
192
193 if (mPostMessageEventQueue) {
194 mPostMessageEventQueue->Dispatch(std::move(aRunnable),
195 NS_DISPATCH_NORMAL);
196 return NS_OK;
197 }
198 }
199 return NS_ERROR_FAILURE;
200 }
201
FlushPostMessageEvents()202 void BrowsingContextGroup::FlushPostMessageEvents() {
203 if (StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) {
204 if (mPostMessageEventQueue) {
205 nsresult rv = mPostMessageEventQueue->SetIsPaused(true);
206 MOZ_ALWAYS_SUCCEEDS(rv);
207 nsCOMPtr<nsIRunnable> event;
208 while ((event = mPostMessageEventQueue->GetEvent())) {
209 NS_DispatchToMainThread(event.forget());
210 }
211 }
212 }
213 }
214
215 /* static */
GetChromeGroup()216 BrowsingContextGroup* BrowsingContextGroup::GetChromeGroup() {
217 MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
218 if (!sChromeGroup && XRE_IsParentProcess()) {
219 sChromeGroup = BrowsingContextGroup::Create();
220 ClearOnShutdown(&sChromeGroup);
221 }
222
223 return sChromeGroup;
224 }
225
GetDocGroups(nsTArray<DocGroup * > & aDocGroups)226 void BrowsingContextGroup::GetDocGroups(nsTArray<DocGroup*>& aDocGroups) {
227 MOZ_ASSERT(NS_IsMainThread());
228 for (auto iter = mDocGroups.ConstIter(); !iter.Done(); iter.Next()) {
229 aDocGroups.AppendElement(iter.Data());
230 }
231 }
232
AddDocument(const nsACString & aKey,Document * aDocument)233 already_AddRefed<DocGroup> BrowsingContextGroup::AddDocument(
234 const nsACString& aKey, Document* aDocument) {
235 MOZ_ASSERT(NS_IsMainThread());
236
237 RefPtr<DocGroup>& docGroup = mDocGroups.GetOrInsert(aKey);
238 if (!docGroup) {
239 docGroup = DocGroup::Create(this, aKey);
240 }
241
242 docGroup->AddDocument(aDocument);
243 return do_AddRef(docGroup);
244 }
245
RemoveDocument(const nsACString & aKey,Document * aDocument)246 void BrowsingContextGroup::RemoveDocument(const nsACString& aKey,
247 Document* aDocument) {
248 MOZ_ASSERT(NS_IsMainThread());
249 RefPtr<DocGroup> docGroup = aDocument->GetDocGroup();
250 // Removing the last document in DocGroup might decrement the
251 // DocGroup BrowsingContextGroup's refcount to 0.
252 RefPtr<BrowsingContextGroup> kungFuDeathGrip(this);
253 docGroup->RemoveDocument(aDocument);
254
255 if (docGroup->IsEmpty()) {
256 mDocGroups.Remove(aKey);
257 }
258 }
259
Select(WindowContext * aParent,BrowsingContext * aOpener)260 already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::Select(
261 WindowContext* aParent, BrowsingContext* aOpener) {
262 if (aParent) {
263 return do_AddRef(aParent->Group());
264 }
265 if (aOpener) {
266 return do_AddRef(aOpener->Group());
267 }
268 return Create();
269 }
270
GetAllGroups(nsTArray<RefPtr<BrowsingContextGroup>> & aGroups)271 void BrowsingContextGroup::GetAllGroups(
272 nsTArray<RefPtr<BrowsingContextGroup>>& aGroups) {
273 aGroups.Clear();
274 if (!sBrowsingContextGroups) {
275 return;
276 }
277
278 aGroups.SetCapacity(sBrowsingContextGroups->Count());
279 for (auto& group : *sBrowsingContextGroups) {
280 aGroups.AppendElement(group.GetData());
281 }
282 }
283
284 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BrowsingContextGroup, mContexts,
285 mToplevels, mSubscribers,
286 mTimerEventQueue, mWorkerEventQueue)
287
288 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(BrowsingContextGroup, AddRef)
289 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(BrowsingContextGroup, Release)
290
291 } // namespace dom
292 } // namespace mozilla
293