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/PMediaTransportChild.h"
25 #include "mozilla/dom/PVsync.h"
26 #include "mozilla/dom/TemporaryIPCBlobChild.h"
27 #include "mozilla/dom/cache/ActorUtils.h"
28 #include "mozilla/dom/indexedDB/PBackgroundIndexedDBUtilsChild.h"
29 #include "mozilla/dom/indexedDB/ThreadLocal.h"
30 #include "mozilla/dom/quota/PQuotaChild.h"
31 #include "mozilla/dom/RemoteWorkerChild.h"
32 #include "mozilla/dom/RemoteWorkerControllerChild.h"
33 #include "mozilla/dom/RemoteWorkerServiceChild.h"
34 #include "mozilla/dom/ServiceWorkerChild.h"
35 #include "mozilla/dom/SharedWorkerChild.h"
36 #include "mozilla/dom/StorageIPC.h"
37 #include "mozilla/dom/MessagePortChild.h"
38 #include "mozilla/dom/ServiceWorkerContainerChild.h"
39 #include "mozilla/dom/ServiceWorkerManagerChild.h"
40 #include "mozilla/ipc/PBackgroundTestChild.h"
41 #include "mozilla/net/PUDPSocketChild.h"
42 #include "mozilla/dom/network/UDPSocketChild.h"
43 #include "mozilla/dom/WebAuthnTransactionChild.h"
44 #include "mozilla/dom/MIDIPortChild.h"
45 #include "mozilla/dom/MIDIManagerChild.h"
50 class TestChild final
: public mozilla::ipc::PBackgroundTestChild
{
51 friend class mozilla::ipc::BackgroundChildImpl
;
55 explicit TestChild(const nsACString
& aTestArg
) : mTestArg(aTestArg
) {
56 MOZ_COUNT_CTOR(TestChild
);
60 ~TestChild() override
{ MOZ_COUNT_DTOR(TestChild
); }
63 mozilla::ipc::IPCResult
Recv__delete__(const nsACString
& aTestArg
) override
;
68 namespace mozilla::ipc
{
70 using mozilla::dom::UDPSocketChild
;
71 using mozilla::net::PUDPSocketChild
;
73 using mozilla::dom::PRemoteWorkerChild
;
74 using mozilla::dom::PServiceWorkerChild
;
75 using mozilla::dom::PServiceWorkerContainerChild
;
76 using mozilla::dom::PServiceWorkerRegistrationChild
;
77 using mozilla::dom::RemoteWorkerChild
;
78 using mozilla::dom::StorageDBChild
;
79 using mozilla::dom::cache::PCacheChild
;
80 using mozilla::dom::cache::PCacheStreamControlChild
;
82 using mozilla::dom::WebAuthnTransactionChild
;
84 using mozilla::dom::PMIDIManagerChild
;
85 using mozilla::dom::PMIDIPortChild
;
87 // -----------------------------------------------------------------------------
88 // BackgroundChildImpl::ThreadLocal
89 // -----------------------------------------------------------------------------
91 BackgroundChildImpl::ThreadLocal::ThreadLocal() : mCurrentFileHandle(nullptr) {
92 // May happen on any thread!
93 MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal
);
96 BackgroundChildImpl::ThreadLocal::~ThreadLocal() {
97 // May happen on any thread!
98 MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal
);
101 // -----------------------------------------------------------------------------
102 // BackgroundChildImpl
103 // -----------------------------------------------------------------------------
105 BackgroundChildImpl::BackgroundChildImpl() {
106 // May happen on any thread!
107 MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl
);
110 BackgroundChildImpl::~BackgroundChildImpl() {
111 // May happen on any thread!
112 MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl
);
115 void BackgroundChildImpl::ProcessingError(Result aCode
, const char* aReason
) {
116 // May happen on any thread!
118 nsAutoCString abortMessage
;
124 #define HANDLE_CASE(_result) \
126 abortMessage.AssignLiteral(#_result); \
129 HANDLE_CASE(MsgNotKnown
);
130 HANDLE_CASE(MsgNotAllowed
);
131 HANDLE_CASE(MsgPayloadError
);
132 HANDLE_CASE(MsgProcessingError
);
133 HANDLE_CASE(MsgRouteError
);
134 HANDLE_CASE(MsgValueError
);
139 MOZ_CRASH("Unknown error code!");
142 MOZ_CRASH_UNSAFE_PRINTF("%s: %s", abortMessage
.get(), aReason
);
145 void BackgroundChildImpl::ActorDestroy(ActorDestroyReason aWhy
) {
146 // May happen on any thread!
149 PBackgroundTestChild
* BackgroundChildImpl::AllocPBackgroundTestChild(
150 const nsACString
& aTestArg
) {
151 return new TestChild(aTestArg
);
154 bool BackgroundChildImpl::DeallocPBackgroundTestChild(
155 PBackgroundTestChild
* aActor
) {
158 delete static_cast<TestChild
*>(aActor
);
162 BackgroundChildImpl::PBackgroundIndexedDBUtilsChild
*
163 BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() {
165 "PBackgroundIndexedDBUtilsChild actors should be manually "
169 bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild(
170 PBackgroundIndexedDBUtilsChild
* aActor
) {
177 BackgroundChildImpl::PBackgroundSDBConnectionChild
*
178 BackgroundChildImpl::AllocPBackgroundSDBConnectionChild(
179 const PersistenceType
& aPersistenceType
,
180 const PrincipalInfo
& aPrincipalInfo
) {
182 "PBackgroundSDBConnectionChild actor should be manually "
186 bool BackgroundChildImpl::DeallocPBackgroundSDBConnectionChild(
187 PBackgroundSDBConnectionChild
* aActor
) {
194 BackgroundChildImpl::PBackgroundLSDatabaseChild
*
195 BackgroundChildImpl::AllocPBackgroundLSDatabaseChild(
196 const PrincipalInfo
& aPrincipalInfo
, const uint32_t& aPrivateBrowsingId
,
197 const uint64_t& aDatastoreId
) {
198 MOZ_CRASH("PBackgroundLSDatabaseChild actor should be manually constructed!");
201 bool BackgroundChildImpl::DeallocPBackgroundLSDatabaseChild(
202 PBackgroundLSDatabaseChild
* aActor
) {
209 BackgroundChildImpl::PBackgroundLSObserverChild
*
210 BackgroundChildImpl::AllocPBackgroundLSObserverChild(
211 const uint64_t& aObserverId
) {
212 MOZ_CRASH("PBackgroundLSObserverChild actor should be manually constructed!");
215 bool BackgroundChildImpl::DeallocPBackgroundLSObserverChild(
216 PBackgroundLSObserverChild
* aActor
) {
223 BackgroundChildImpl::PBackgroundLSRequestChild
*
224 BackgroundChildImpl::AllocPBackgroundLSRequestChild(
225 const LSRequestParams
& aParams
) {
226 MOZ_CRASH("PBackgroundLSRequestChild actor should be manually constructed!");
229 bool BackgroundChildImpl::DeallocPBackgroundLSRequestChild(
230 PBackgroundLSRequestChild
* aActor
) {
237 BackgroundChildImpl::PBackgroundLocalStorageCacheChild
*
238 BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild(
239 const PrincipalInfo
& aPrincipalInfo
, const nsACString
& aOriginKey
,
240 const uint32_t& aPrivateBrowsingId
) {
242 "PBackgroundLocalStorageChild actors should be manually "
246 bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild(
247 PBackgroundLocalStorageCacheChild
* aActor
) {
254 BackgroundChildImpl::PBackgroundLSSimpleRequestChild
*
255 BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild(
256 const LSSimpleRequestParams
& aParams
) {
258 "PBackgroundLSSimpleRequestChild actor should be manually "
262 bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild(
263 PBackgroundLSSimpleRequestChild
* aActor
) {
270 BackgroundChildImpl::PBackgroundStorageChild
*
271 BackgroundChildImpl::AllocPBackgroundStorageChild(
272 const nsAString
& aProfilePath
, const uint32_t& aPrivateBrowsingId
) {
273 MOZ_CRASH("PBackgroundStorageChild actors should be manually constructed!");
276 bool BackgroundChildImpl::DeallocPBackgroundStorageChild(
277 PBackgroundStorageChild
* aActor
) {
280 StorageDBChild
* child
= static_cast<StorageDBChild
*>(aActor
);
281 child
->ReleaseIPDLReference();
285 already_AddRefed
<PRemoteWorkerChild
>
286 BackgroundChildImpl::AllocPRemoteWorkerChild(const RemoteWorkerData
& aData
) {
287 return MakeAndAddRef
<RemoteWorkerChild
>(aData
);
290 IPCResult
BackgroundChildImpl::RecvPRemoteWorkerConstructor(
291 PRemoteWorkerChild
* aActor
, const RemoteWorkerData
& aData
) {
292 dom::RemoteWorkerChild
* actor
= static_cast<dom::RemoteWorkerChild
*>(aActor
);
293 actor
->ExecWorker(aData
);
297 dom::PRemoteWorkerControllerChild
*
298 BackgroundChildImpl::AllocPRemoteWorkerControllerChild(
299 const dom::RemoteWorkerData
& aRemoteWorkerData
) {
301 "PRemoteWorkerControllerChild actors must be manually constructed!");
305 bool BackgroundChildImpl::DeallocPRemoteWorkerControllerChild(
306 dom::PRemoteWorkerControllerChild
* aActor
) {
309 RefPtr
<dom::RemoteWorkerControllerChild
> actor
=
310 dont_AddRef(static_cast<dom::RemoteWorkerControllerChild
*>(aActor
));
314 dom::PSharedWorkerChild
* BackgroundChildImpl::AllocPSharedWorkerChild(
315 const dom::RemoteWorkerData
& aData
, const uint64_t& aWindowID
,
316 const dom::MessagePortIdentifier
& aPortIdentifier
) {
317 RefPtr
<dom::SharedWorkerChild
> agent
= new dom::SharedWorkerChild();
318 return agent
.forget().take();
321 bool BackgroundChildImpl::DeallocPSharedWorkerChild(
322 dom::PSharedWorkerChild
* aActor
) {
323 RefPtr
<dom::SharedWorkerChild
> actor
=
324 dont_AddRef(static_cast<dom::SharedWorkerChild
*>(aActor
));
328 dom::PTemporaryIPCBlobChild
*
329 BackgroundChildImpl::AllocPTemporaryIPCBlobChild() {
330 MOZ_CRASH("This is not supposed to be called.");
334 bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild(
335 dom::PTemporaryIPCBlobChild
* aActor
) {
336 RefPtr
<dom::TemporaryIPCBlobChild
> actor
=
337 dont_AddRef(static_cast<dom::TemporaryIPCBlobChild
*>(aActor
));
341 dom::PFileCreatorChild
* BackgroundChildImpl::AllocPFileCreatorChild(
342 const nsAString
& aFullPath
, const nsAString
& aType
, const nsAString
& aName
,
343 const Maybe
<int64_t>& aLastModified
, const bool& aExistenceCheck
,
344 const bool& aIsFromNsIFile
) {
345 return new dom::FileCreatorChild();
348 bool BackgroundChildImpl::DeallocPFileCreatorChild(PFileCreatorChild
* aActor
) {
349 delete static_cast<dom::FileCreatorChild
*>(aActor
);
353 PUDPSocketChild
* BackgroundChildImpl::AllocPUDPSocketChild(
354 const Maybe
<PrincipalInfo
>& aPrincipalInfo
, const nsACString
& aFilter
) {
355 MOZ_CRASH("AllocPUDPSocket should not be called");
359 bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild
* child
) {
360 UDPSocketChild
* p
= static_cast<UDPSocketChild
*>(child
);
361 p
->ReleaseIPDLReference();
365 // -----------------------------------------------------------------------------
366 // BroadcastChannel API
367 // -----------------------------------------------------------------------------
369 dom::PBroadcastChannelChild
* BackgroundChildImpl::AllocPBroadcastChannelChild(
370 const PrincipalInfo
& aPrincipalInfo
, const nsACString
& aOrigin
,
371 const nsAString
& aChannel
) {
372 RefPtr
<dom::BroadcastChannelChild
> agent
= new dom::BroadcastChannelChild();
373 return agent
.forget().take();
376 bool BackgroundChildImpl::DeallocPBroadcastChannelChild(
377 PBroadcastChannelChild
* aActor
) {
378 RefPtr
<dom::BroadcastChannelChild
> child
=
379 dont_AddRef(static_cast<dom::BroadcastChannelChild
*>(aActor
));
384 camera::PCamerasChild
* BackgroundChildImpl::AllocPCamerasChild() {
386 RefPtr
<camera::CamerasChild
> agent
= new camera::CamerasChild();
387 return agent
.forget().take();
393 bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild
* aActor
) {
395 RefPtr
<camera::CamerasChild
> child
=
396 dont_AddRef(static_cast<camera::CamerasChild
*>(aActor
));
403 // -----------------------------------------------------------------------------
404 // ServiceWorkerManager
405 // -----------------------------------------------------------------------------
407 dom::PServiceWorkerManagerChild
*
408 BackgroundChildImpl::AllocPServiceWorkerManagerChild() {
409 RefPtr
<dom::ServiceWorkerManagerChild
> agent
=
410 new dom::ServiceWorkerManagerChild();
411 return agent
.forget().take();
414 bool BackgroundChildImpl::DeallocPServiceWorkerManagerChild(
415 PServiceWorkerManagerChild
* aActor
) {
416 RefPtr
<dom::ServiceWorkerManagerChild
> child
=
417 dont_AddRef(static_cast<dom::ServiceWorkerManagerChild
*>(aActor
));
422 // -----------------------------------------------------------------------------
424 // -----------------------------------------------------------------------------
426 already_AddRefed
<PCacheChild
> BackgroundChildImpl::AllocPCacheChild() {
427 return dom::cache::AllocPCacheChild();
430 already_AddRefed
<PCacheStreamControlChild
>
431 BackgroundChildImpl::AllocPCacheStreamControlChild() {
432 return dom::cache::AllocPCacheStreamControlChild();
435 // -----------------------------------------------------------------------------
436 // MessageChannel/MessagePort API
437 // -----------------------------------------------------------------------------
439 dom::PMessagePortChild
* BackgroundChildImpl::AllocPMessagePortChild(
440 const nsID
& aUUID
, const nsID
& aDestinationUUID
,
441 const uint32_t& aSequenceID
) {
442 RefPtr
<dom::MessagePortChild
> agent
= new dom::MessagePortChild();
443 return agent
.forget().take();
446 bool BackgroundChildImpl::DeallocPMessagePortChild(PMessagePortChild
* aActor
) {
447 RefPtr
<dom::MessagePortChild
> child
=
448 dont_AddRef(static_cast<dom::MessagePortChild
*>(aActor
));
453 BackgroundChildImpl::PQuotaChild
* BackgroundChildImpl::AllocPQuotaChild() {
454 MOZ_CRASH("PQuotaChild actor should be manually constructed!");
457 bool BackgroundChildImpl::DeallocPQuotaChild(PQuotaChild
* aActor
) {
463 mozilla::dom::PClientManagerChild
*
464 BackgroundChildImpl::AllocPClientManagerChild() {
465 return mozilla::dom::AllocClientManagerChild();
468 bool BackgroundChildImpl::DeallocPClientManagerChild(
469 mozilla::dom::PClientManagerChild
* aActor
) {
470 return mozilla::dom::DeallocClientManagerChild(aActor
);
473 dom::PWebAuthnTransactionChild
*
474 BackgroundChildImpl::AllocPWebAuthnTransactionChild() {
475 MOZ_CRASH("PWebAuthnTransaction actor should be manually constructed!");
479 bool BackgroundChildImpl::DeallocPWebAuthnTransactionChild(
480 PWebAuthnTransactionChild
* aActor
) {
482 RefPtr
<dom::WebAuthnTransactionChild
> child
=
483 dont_AddRef(static_cast<dom::WebAuthnTransactionChild
*>(aActor
));
487 already_AddRefed
<PServiceWorkerChild
>
488 BackgroundChildImpl::AllocPServiceWorkerChild(
489 const IPCServiceWorkerDescriptor
&) {
490 MOZ_CRASH("Shouldn't be called.");
494 already_AddRefed
<PServiceWorkerContainerChild
>
495 BackgroundChildImpl::AllocPServiceWorkerContainerChild() {
496 return mozilla::dom::ServiceWorkerContainerChild::Create();
499 already_AddRefed
<PServiceWorkerRegistrationChild
>
500 BackgroundChildImpl::AllocPServiceWorkerRegistrationChild(
501 const IPCServiceWorkerRegistrationDescriptor
&) {
502 MOZ_CRASH("Shouldn't be called.");
506 dom::PEndpointForReportChild
* BackgroundChildImpl::AllocPEndpointForReportChild(
507 const nsAString
& aGroupName
, const PrincipalInfo
& aPrincipalInfo
) {
508 return new dom::EndpointForReportChild();
511 bool BackgroundChildImpl::DeallocPEndpointForReportChild(
512 PEndpointForReportChild
* aActor
) {
514 delete static_cast<dom::EndpointForReportChild
*>(aActor
);
518 dom::PMediaTransportChild
* BackgroundChildImpl::AllocPMediaTransportChild() {
519 // We don't allocate here: MediaTransportHandlerIPC is in charge of that,
520 // so we don't need to know the implementation particulars here.
521 MOZ_ASSERT_UNREACHABLE(
522 "The only thing that ought to be creating a PMediaTransportChild is "
523 "MediaTransportHandlerIPC!");
527 bool BackgroundChildImpl::DeallocPMediaTransportChild(
528 dom::PMediaTransportChild
* aActor
) {
533 } // namespace mozilla::ipc
535 mozilla::ipc::IPCResult
TestChild::Recv__delete__(const nsACString
& aTestArg
) {
536 MOZ_RELEASE_ASSERT(aTestArg
== mTestArg
,
537 "BackgroundTest message was corrupted!");