1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/InProcessParent.h"
#include "mozilla/dom/InProcessChild.h"
#include "mozilla/dom/JSProcessActorBinding.h"
#include "nsIObserverService.h"
#include "mozilla/Services.h"
using namespace mozilla::ipc;
// This file contains the implementation of core InProcess lifecycle management
// facilities.
namespace mozilla::dom {
StaticRefPtr<InProcessParent> InProcessParent::sSingleton;
StaticRefPtr<InProcessChild> InProcessChild::sSingleton;
bool InProcessParent::sShutdown = false;
//////////////////////////////////////////
// InProcess actor lifecycle management //
//////////////////////////////////////////
/* static */
InProcessChild* InProcessChild::Singleton() {
MOZ_ASSERT(NS_IsMainThread());
if (!sSingleton) {
InProcessParent::Startup();
}
return sSingleton;
}
/* static */
InProcessParent* InProcessParent::Singleton() {
MOZ_ASSERT(NS_IsMainThread());
if (!sSingleton) {
InProcessParent::Startup();
}
return sSingleton;
}
/* static */
void InProcessParent::Startup() {
MOZ_ASSERT(NS_IsMainThread());
if (sShutdown) {
NS_WARNING("Could not get in-process actor while shutting down!");
return;
}
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (!obs) {
sShutdown = true;
NS_WARNING("Failed to get nsIObserverService for in-process actor");
return;
}
RefPtr<InProcessParent> parent = new InProcessParent();
RefPtr<InProcessChild> child = new InProcessChild();
// Observe the shutdown event to close & clean up after ourselves.
nsresult rv = obs->AddObserver(parent, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
// Link the two actors
if (!child->OpenOnSameThread(parent, ChildSide)) {
MOZ_CRASH("Failed to open InProcessChild!");
}
parent->SetOtherProcessId(base::GetCurrentProcId());
// Stash global references to fetch the other side of the reference.
InProcessParent::sSingleton = std::move(parent);
InProcessChild::sSingleton = std::move(child);
}
/* static */
void InProcessParent::Shutdown() {
MOZ_ASSERT(NS_IsMainThread());
if (!sSingleton || sShutdown) {
return;
}
sShutdown = true;
RefPtr<InProcessParent> parent = sSingleton;
InProcessParent::sSingleton = nullptr;
InProcessChild::sSingleton = nullptr;
// Calling `Close` on the actor will cause the `Dealloc` methods to be called,
// freeing the remaining references.
parent->Close();
}
NS_IMETHODIMP
InProcessParent::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
MOZ_ASSERT(!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID));
InProcessParent::Shutdown();
return NS_OK;
}
void InProcessParent::ActorDestroy(ActorDestroyReason aWhy) {
JSActorDidDestroy();
InProcessParent::Shutdown();
}
void InProcessChild::ActorDestroy(ActorDestroyReason aWhy) {
JSActorDidDestroy();
InProcessParent::Shutdown();
}
/////////////////////////
// nsIDOMProcessParent //
/////////////////////////
NS_IMETHODIMP
InProcessParent::GetChildID(uint64_t* aChildID) {
*aChildID = 0;
return NS_OK;
}
NS_IMETHODIMP
InProcessParent::GetOsPid(int32_t* aOsPid) {
// InProcessParent always run in the parent process,
// so we can return the current process id.
*aOsPid = base::GetCurrentProcId();
return NS_OK;
}
NS_IMETHODIMP InProcessParent::GetRemoteType(nsACString& aRemoteType) {
aRemoteType = NOT_REMOTE_TYPE;
return NS_OK;
}
NS_IMETHODIMP
InProcessParent::GetActor(const nsACString& aName, JSContext* aCx,
JSProcessActorParent** aActor) {
ErrorResult error;
RefPtr<JSProcessActorParent> actor =
JSActorManager::GetActor(aCx, aName, error)
.downcast<JSProcessActorParent>();
if (error.MaybeSetPendingException(aCx)) {
return NS_ERROR_FAILURE;
}
actor.forget(aActor);
return NS_OK;
}
NS_IMETHODIMP
InProcessParent::GetExistingActor(const nsACString& aName,
JSProcessActorParent** aActor) {
RefPtr<JSProcessActorParent> actor =
JSActorManager::GetExistingActor(aName).downcast<JSProcessActorParent>();
actor.forget(aActor);
return NS_OK;
}
already_AddRefed<JSActor> InProcessParent::InitJSActor(
JS::Handle<JSObject*> aMaybeActor, const nsACString& aName,
ErrorResult& aRv) {
RefPtr<JSProcessActorParent> actor;
if (aMaybeActor.get()) {
aRv = UNWRAP_OBJECT(JSProcessActorParent, aMaybeActor.get(), actor);
if (aRv.Failed()) {
return nullptr;
}
} else {
actor = new JSProcessActorParent();
}
MOZ_RELEASE_ASSERT(!actor->Manager(),
"mManager was already initialized once!");
actor->Init(aName, this);
return actor.forget();
}
NS_IMETHODIMP
InProcessParent::GetCanSend(bool* aCanSend) {
*aCanSend = CanSend();
return NS_OK;
}
ContentParent* InProcessParent::AsContentParent() { return nullptr; }
JSActorManager* InProcessParent::AsJSActorManager() { return this; }
////////////////////////
// nsIDOMProcessChild //
////////////////////////
NS_IMETHODIMP
InProcessChild::GetChildID(uint64_t* aChildID) {
*aChildID = 0;
return NS_OK;
}
NS_IMETHODIMP
InProcessChild::GetActor(const nsACString& aName, JSContext* aCx,
JSProcessActorChild** aActor) {
ErrorResult error;
RefPtr<JSProcessActorChild> actor =
JSActorManager::GetActor(aCx, aName, error)
.downcast<JSProcessActorChild>();
if (error.MaybeSetPendingException(aCx)) {
return NS_ERROR_FAILURE;
}
actor.forget(aActor);
return NS_OK;
}
NS_IMETHODIMP
InProcessChild::GetExistingActor(const nsACString& aName,
JSProcessActorChild** aActor) {
RefPtr<JSProcessActorChild> actor =
JSActorManager::GetExistingActor(aName).downcast<JSProcessActorChild>();
actor.forget(aActor);
return NS_OK;
}
already_AddRefed<JSActor> InProcessChild::InitJSActor(
JS::Handle<JSObject*> aMaybeActor, const nsACString& aName,
ErrorResult& aRv) {
RefPtr<JSProcessActorChild> actor;
if (aMaybeActor.get()) {
aRv = UNWRAP_OBJECT(JSProcessActorChild, aMaybeActor.get(), actor);
if (aRv.Failed()) {
return nullptr;
}
} else {
actor = new JSProcessActorChild();
}
MOZ_RELEASE_ASSERT(!actor->Manager(),
"mManager was already initialized once!");
actor->Init(aName, this);
return actor.forget();
}
NS_IMETHODIMP
InProcessChild::GetCanSend(bool* aCanSend) {
*aCanSend = CanSend();
return NS_OK;
}
ContentChild* InProcessChild::AsContentChild() { return nullptr; }
JSActorManager* InProcessChild::AsJSActorManager() { return this; }
////////////////////////////////
// In-Process Actor Utilities //
////////////////////////////////
// Helper method for implementing ParentActorFor and ChildActorFor.
static IProtocol* GetOtherInProcessActor(IProtocol* aActor) {
MOZ_ASSERT(aActor->GetSide() != UnknownSide, "bad unknown side");
// Discover the manager of aActor which is PInProcess.
IProtocol* current = aActor;
while (current && current->CanRecv()) {
if (current->GetProtocolId() == PInProcessMsgStart) {
break; // Found the correct actor.
}
current = current->Manager();
}
if (!current || !current->CanRecv()) {
return nullptr; // Not a live PInProcess actor, return |nullptr|
}
MOZ_ASSERT(current->GetSide() == aActor->GetSide(), "side changed?");
MOZ_ASSERT_IF(aActor->GetSide() == ParentSide,
current == InProcessParent::Singleton());
MOZ_ASSERT_IF(aActor->GetSide() == ChildSide,
current == InProcessChild::Singleton());
// Check whether this is InProcessParent or InProcessChild, and get the other
// side's toplevel actor.
IProtocol* otherRoot = nullptr;
if (aActor->GetSide() == ParentSide) {
otherRoot = InProcessChild::Singleton();
} else {
otherRoot = InProcessParent::Singleton();
}
if (NS_WARN_IF(!otherRoot)) {
return nullptr;
}
// Look up the actor on the other side, and return it.
IProtocol* otherActor = otherRoot->Lookup(aActor->Id());
if (otherActor) {
MOZ_ASSERT(otherActor->GetSide() != UnknownSide, "bad unknown side");
MOZ_ASSERT(otherActor->GetSide() != aActor->GetSide(), "Wrong side!");
MOZ_ASSERT(otherActor->GetProtocolId() == aActor->GetProtocolId(),
"Wrong type of protocol!");
}
return otherActor;
}
/* static */
IProtocol* InProcessParent::ChildActorFor(IProtocol* aActor) {
MOZ_ASSERT(aActor && aActor->GetSide() == ParentSide);
return GetOtherInProcessActor(aActor);
}
/* static */
IProtocol* InProcessChild::ParentActorFor(IProtocol* aActor) {
MOZ_ASSERT(aActor && aActor->GetSide() == ChildSide);
return GetOtherInProcessActor(aActor);
}
NS_IMPL_ISUPPORTS(InProcessParent, nsIDOMProcessParent, nsIObserver)
NS_IMPL_ISUPPORTS(InProcessChild, nsIDOMProcessChild)
} // namespace mozilla::dom
|