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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BackgroundChildImpl.h"
9 #include "BroadcastChannelChild.h"
11 # include "CamerasChild.h"
13 #include "mozilla/Assertions.h"
14 #include "mozilla/SchedulerGroup.h"
15 #include "mozilla/dom/ClientManagerActors.h"
16 #include "mozilla/dom/FileCreatorChild.h"
17 #include "mozilla/dom/PBackgroundLSDatabaseChild.h"
18 #include "mozilla/dom/PBackgroundLSObserverChild.h"
19 #include "mozilla/dom/PBackgroundLSRequestChild.h"
20 #include "mozilla/dom/PBackgroundLSSimpleRequestChild.h"
21 #include "mozilla/dom/PBackgroundSDBConnectionChild.h"
22 #include "mozilla/dom/PFileSystemRequestChild.h"
23 #include "mozilla/dom/EndpointForReportChild.h"
24 #include "mozilla/dom/PVsync.h"
25 #include "mozilla/dom/TemporaryIPCBlobChild.h"
26 #include "mozilla/dom/cache/ActorUtils.h"
27 #include "mozilla/dom/indexedDB/PBackgroundIndexedDBUtilsChild.h"
28 #include "mozilla/dom/indexedDB/ThreadLocal.h"
29 #include "mozilla/dom/quota/PQuotaChild.h"
30 #include "mozilla/dom/RemoteWorkerChild.h"
31 #include "mozilla/dom/RemoteWorkerControllerChild.h"
32 #include "mozilla/dom/RemoteWorkerServiceChild.h"
33 #include "mozilla/dom/ServiceWorkerChild.h"
34 #include "mozilla/dom/SharedWorkerChild.h"
35 #include "mozilla/dom/StorageIPC.h"
36 #include "mozilla/dom/MessagePortChild.h"
37 #include "mozilla/dom/ServiceWorkerContainerChild.h"
38 #include "mozilla/dom/ServiceWorkerManagerChild.h"
39 #include "mozilla/ipc/PBackgroundTestChild.h"
40 #include "mozilla/net/PUDPSocketChild.h"
41 #include "mozilla/dom/network/UDPSocketChild.h"
42 #include "mozilla/dom/WebAuthnTransactionChild.h"
43 #include "mozilla/dom/MIDIPortChild.h"
44 #include "mozilla/dom/MIDIManagerChild.h"
49 class TestChild final
: public mozilla::ipc::PBackgroundTestChild
{
50 friend class mozilla::ipc::BackgroundChildImpl
;
54 explicit TestChild(const nsACString
& aTestArg
) : mTestArg(aTestArg
) {
55 MOZ_COUNT_CTOR(TestChild
);
59 ~TestChild() override
{ MOZ_COUNT_DTOR(TestChild
); }
62 mozilla::ipc::IPCResult
Recv__delete__(const nsACString
& aTestArg
) override
;
67 namespace mozilla::ipc
{
69 using mozilla::dom::UDPSocketChild
;
70 using mozilla::net::PUDPSocketChild
;
72 using mozilla::dom::PRemoteWorkerChild
;
73 using mozilla::dom::PServiceWorkerChild
;
74 using mozilla::dom::PServiceWorkerContainerChild
;
75 using mozilla::dom::PServiceWorkerRegistrationChild
;
76 using mozilla::dom::RemoteWorkerChild
;
77 using mozilla::dom::StorageDBChild
;
78 using mozilla::dom::cache::PCacheChild
;
79 using mozilla::dom::cache::PCacheStreamControlChild
;
81 using mozilla::dom::WebAuthnTransactionChild
;
83 using mozilla::dom::PMIDIManagerChild
;
84 using mozilla::dom::PMIDIPortChild
;
86 // -----------------------------------------------------------------------------
87 // BackgroundChildImpl::ThreadLocal
88 // -----------------------------------------------------------------------------
90 BackgroundChildImpl::ThreadLocal::ThreadLocal() : mCurrentFileHandle(nullptr) {
91 // May happen on any thread!
92 MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal
);
95 BackgroundChildImpl::ThreadLocal::~ThreadLocal() {
96 // May happen on any thread!
97 MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal
);
100 // -----------------------------------------------------------------------------
101 // BackgroundChildImpl
102 // -----------------------------------------------------------------------------
104 BackgroundChildImpl::BackgroundChildImpl() {
105 // May happen on any thread!
106 MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl
);
109 BackgroundChildImpl::~BackgroundChildImpl() {
110 // May happen on any thread!
111 MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl
);
114 void BackgroundChildImpl::ProcessingError(Result aCode
, const char* aReason
) {
115 // May happen on any thread!
117 nsAutoCString abortMessage
;
123 #define HANDLE_CASE(_result) \
125 abortMessage.AssignLiteral(#_result); \
128 HANDLE_CASE(MsgNotKnown
);
129 HANDLE_CASE(MsgNotAllowed
);
130 HANDLE_CASE(MsgPayloadError
);
131 HANDLE_CASE(MsgProcessingError
);
132 HANDLE_CASE(MsgRouteError
);
133 HANDLE_CASE(MsgValueError
);
138 MOZ_CRASH("Unknown error code!");
141 nsDependentCString
reason(aReason
);
142 CrashReporter::AnnotateCrashReport(
143 CrashReporter::Annotation::ipc_channel_error
, reason
);
145 MOZ_CRASH_UNSAFE_PRINTF("%s: %s", abortMessage
.get(), aReason
);
148 void BackgroundChildImpl::ActorDestroy(ActorDestroyReason aWhy
) {
149 // May happen on any thread!
152 PBackgroundTestChild
* BackgroundChildImpl::AllocPBackgroundTestChild(
153 const nsACString
& aTestArg
) {
154 return new TestChild(aTestArg
);
157 bool BackgroundChildImpl::DeallocPBackgroundTestChild(
158 PBackgroundTestChild
* aActor
) {
161 delete static_cast<TestChild
*>(aActor
);
165 BackgroundChildImpl::PBackgroundIndexedDBUtilsChild
*
166 BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() {
168 "PBackgroundIndexedDBUtilsChild actors should be manually "
172 bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild(
173 PBackgroundIndexedDBUtilsChild
* aActor
) {
180 BackgroundChildImpl::PBackgroundLSObserverChild
*
181 BackgroundChildImpl::AllocPBackgroundLSObserverChild(
182 const uint64_t& aObserverId
) {
183 MOZ_CRASH("PBackgroundLSObserverChild actor should be manually constructed!");
186 bool BackgroundChildImpl::DeallocPBackgroundLSObserverChild(
187 PBackgroundLSObserverChild
* aActor
) {
194 BackgroundChildImpl::PBackgroundLSRequestChild
*
195 BackgroundChildImpl::AllocPBackgroundLSRequestChild(
196 const LSRequestParams
& aParams
) {
197 MOZ_CRASH("PBackgroundLSRequestChild actor should be manually constructed!");
200 bool BackgroundChildImpl::DeallocPBackgroundLSRequestChild(
201 PBackgroundLSRequestChild
* aActor
) {
208 BackgroundChildImpl::PBackgroundLocalStorageCacheChild
*
209 BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild(
210 const PrincipalInfo
& aPrincipalInfo
, const nsACString
& aOriginKey
,
211 const uint32_t& aPrivateBrowsingId
) {
213 "PBackgroundLocalStorageChild actors should be manually "
217 bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild(
218 PBackgroundLocalStorageCacheChild
* aActor
) {
225 BackgroundChildImpl::PBackgroundLSSimpleRequestChild
*
226 BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild(
227 const LSSimpleRequestParams
& aParams
) {
229 "PBackgroundLSSimpleRequestChild actor should be manually "
233 bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild(
234 PBackgroundLSSimpleRequestChild
* aActor
) {
241 BackgroundChildImpl::PBackgroundStorageChild
*
242 BackgroundChildImpl::AllocPBackgroundStorageChild(
243 const nsAString
& aProfilePath
, const uint32_t& aPrivateBrowsingId
) {
244 MOZ_CRASH("PBackgroundStorageChild actors should be manually constructed!");
247 bool BackgroundChildImpl::DeallocPBackgroundStorageChild(
248 PBackgroundStorageChild
* aActor
) {
251 StorageDBChild
* child
= static_cast<StorageDBChild
*>(aActor
);
252 child
->ReleaseIPDLReference();
256 already_AddRefed
<PRemoteWorkerChild
>
257 BackgroundChildImpl::AllocPRemoteWorkerChild(const RemoteWorkerData
& aData
) {
258 return MakeAndAddRef
<RemoteWorkerChild
>(aData
);
261 IPCResult
BackgroundChildImpl::RecvPRemoteWorkerConstructor(
262 PRemoteWorkerChild
* aActor
, const RemoteWorkerData
& aData
) {
263 dom::RemoteWorkerChild
* actor
= static_cast<dom::RemoteWorkerChild
*>(aActor
);
264 actor
->ExecWorker(aData
);
268 dom::PSharedWorkerChild
* BackgroundChildImpl::AllocPSharedWorkerChild(
269 const dom::RemoteWorkerData
& aData
, const uint64_t& aWindowID
,
270 const dom::MessagePortIdentifier
& aPortIdentifier
) {
271 RefPtr
<dom::SharedWorkerChild
> agent
= new dom::SharedWorkerChild();
272 return agent
.forget().take();
275 bool BackgroundChildImpl::DeallocPSharedWorkerChild(
276 dom::PSharedWorkerChild
* aActor
) {
277 RefPtr
<dom::SharedWorkerChild
> actor
=
278 dont_AddRef(static_cast<dom::SharedWorkerChild
*>(aActor
));
282 dom::PTemporaryIPCBlobChild
*
283 BackgroundChildImpl::AllocPTemporaryIPCBlobChild() {
284 MOZ_CRASH("This is not supposed to be called.");
288 bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild(
289 dom::PTemporaryIPCBlobChild
* aActor
) {
290 RefPtr
<dom::TemporaryIPCBlobChild
> actor
=
291 dont_AddRef(static_cast<dom::TemporaryIPCBlobChild
*>(aActor
));
295 dom::PFileCreatorChild
* BackgroundChildImpl::AllocPFileCreatorChild(
296 const nsAString
& aFullPath
, const nsAString
& aType
, const nsAString
& aName
,
297 const Maybe
<int64_t>& aLastModified
, const bool& aExistenceCheck
,
298 const bool& aIsFromNsIFile
) {
299 return new dom::FileCreatorChild();
302 bool BackgroundChildImpl::DeallocPFileCreatorChild(PFileCreatorChild
* aActor
) {
303 delete static_cast<dom::FileCreatorChild
*>(aActor
);
307 PUDPSocketChild
* BackgroundChildImpl::AllocPUDPSocketChild(
308 const Maybe
<PrincipalInfo
>& aPrincipalInfo
, const nsACString
& aFilter
) {
309 MOZ_CRASH("AllocPUDPSocket should not be called");
313 bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild
* child
) {
314 UDPSocketChild
* p
= static_cast<UDPSocketChild
*>(child
);
315 p
->ReleaseIPDLReference();
319 // -----------------------------------------------------------------------------
320 // BroadcastChannel API
321 // -----------------------------------------------------------------------------
323 dom::PBroadcastChannelChild
* BackgroundChildImpl::AllocPBroadcastChannelChild(
324 const PrincipalInfo
& aPrincipalInfo
, const nsACString
& aOrigin
,
325 const nsAString
& aChannel
) {
326 RefPtr
<dom::BroadcastChannelChild
> agent
= new dom::BroadcastChannelChild();
327 return agent
.forget().take();
330 bool BackgroundChildImpl::DeallocPBroadcastChannelChild(
331 PBroadcastChannelChild
* aActor
) {
332 RefPtr
<dom::BroadcastChannelChild
> child
=
333 dont_AddRef(static_cast<dom::BroadcastChannelChild
*>(aActor
));
338 camera::PCamerasChild
* BackgroundChildImpl::AllocPCamerasChild() {
340 RefPtr
<camera::CamerasChild
> agent
= new camera::CamerasChild();
341 return agent
.forget().take();
347 bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild
* aActor
) {
349 RefPtr
<camera::CamerasChild
> child
=
350 dont_AddRef(static_cast<camera::CamerasChild
*>(aActor
));
357 // -----------------------------------------------------------------------------
358 // ServiceWorkerManager
359 // -----------------------------------------------------------------------------
361 dom::PServiceWorkerManagerChild
*
362 BackgroundChildImpl::AllocPServiceWorkerManagerChild() {
363 RefPtr
<dom::ServiceWorkerManagerChild
> agent
=
364 new dom::ServiceWorkerManagerChild();
365 return agent
.forget().take();
368 bool BackgroundChildImpl::DeallocPServiceWorkerManagerChild(
369 PServiceWorkerManagerChild
* aActor
) {
370 RefPtr
<dom::ServiceWorkerManagerChild
> child
=
371 dont_AddRef(static_cast<dom::ServiceWorkerManagerChild
*>(aActor
));
376 // -----------------------------------------------------------------------------
378 // -----------------------------------------------------------------------------
380 already_AddRefed
<PCacheChild
> BackgroundChildImpl::AllocPCacheChild() {
381 return dom::cache::AllocPCacheChild();
384 already_AddRefed
<PCacheStreamControlChild
>
385 BackgroundChildImpl::AllocPCacheStreamControlChild() {
386 return dom::cache::AllocPCacheStreamControlChild();
389 // -----------------------------------------------------------------------------
390 // MessageChannel/MessagePort API
391 // -----------------------------------------------------------------------------
393 dom::PMessagePortChild
* BackgroundChildImpl::AllocPMessagePortChild(
394 const nsID
& aUUID
, const nsID
& aDestinationUUID
,
395 const uint32_t& aSequenceID
) {
396 RefPtr
<dom::MessagePortChild
> agent
= new dom::MessagePortChild();
397 return agent
.forget().take();
400 bool BackgroundChildImpl::DeallocPMessagePortChild(PMessagePortChild
* aActor
) {
401 RefPtr
<dom::MessagePortChild
> child
=
402 dont_AddRef(static_cast<dom::MessagePortChild
*>(aActor
));
407 dom::PWebAuthnTransactionChild
*
408 BackgroundChildImpl::AllocPWebAuthnTransactionChild() {
409 MOZ_CRASH("PWebAuthnTransaction actor should be manually constructed!");
413 bool BackgroundChildImpl::DeallocPWebAuthnTransactionChild(
414 PWebAuthnTransactionChild
* aActor
) {
416 RefPtr
<dom::WebAuthnTransactionChild
> child
=
417 dont_AddRef(static_cast<dom::WebAuthnTransactionChild
*>(aActor
));
421 already_AddRefed
<PServiceWorkerChild
>
422 BackgroundChildImpl::AllocPServiceWorkerChild(
423 const IPCServiceWorkerDescriptor
&) {
424 MOZ_CRASH("Shouldn't be called.");
428 already_AddRefed
<PServiceWorkerContainerChild
>
429 BackgroundChildImpl::AllocPServiceWorkerContainerChild() {
430 return mozilla::dom::ServiceWorkerContainerChild::Create();
433 already_AddRefed
<PServiceWorkerRegistrationChild
>
434 BackgroundChildImpl::AllocPServiceWorkerRegistrationChild(
435 const IPCServiceWorkerRegistrationDescriptor
&) {
436 MOZ_CRASH("Shouldn't be called.");
440 dom::PEndpointForReportChild
* BackgroundChildImpl::AllocPEndpointForReportChild(
441 const nsAString
& aGroupName
, const PrincipalInfo
& aPrincipalInfo
) {
442 return new dom::EndpointForReportChild();
445 bool BackgroundChildImpl::DeallocPEndpointForReportChild(
446 PEndpointForReportChild
* aActor
) {
448 delete static_cast<dom::EndpointForReportChild
*>(aActor
);
452 } // namespace mozilla::ipc
454 mozilla::ipc::IPCResult
TestChild::Recv__delete__(const nsACString
& aTestArg
) {
455 MOZ_RELEASE_ASSERT(aTestArg
== mTestArg
,
456 "BackgroundTest message was corrupted!");