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 CrashReporter::RecordAnnotationCString(
142 CrashReporter::Annotation::ipc_channel_error
, aReason
);
144 MOZ_CRASH_UNSAFE_PRINTF("%s: %s", abortMessage
.get(), aReason
);
147 void BackgroundChildImpl::ActorDestroy(ActorDestroyReason aWhy
) {
148 // May happen on any thread!
151 PBackgroundTestChild
* BackgroundChildImpl::AllocPBackgroundTestChild(
152 const nsACString
& aTestArg
) {
153 return new TestChild(aTestArg
);
156 bool BackgroundChildImpl::DeallocPBackgroundTestChild(
157 PBackgroundTestChild
* aActor
) {
160 delete static_cast<TestChild
*>(aActor
);
164 BackgroundChildImpl::PBackgroundIndexedDBUtilsChild
*
165 BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() {
167 "PBackgroundIndexedDBUtilsChild actors should be manually "
171 bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild(
172 PBackgroundIndexedDBUtilsChild
* aActor
) {
179 BackgroundChildImpl::PBackgroundLSObserverChild
*
180 BackgroundChildImpl::AllocPBackgroundLSObserverChild(
181 const uint64_t& aObserverId
) {
182 MOZ_CRASH("PBackgroundLSObserverChild actor should be manually constructed!");
185 bool BackgroundChildImpl::DeallocPBackgroundLSObserverChild(
186 PBackgroundLSObserverChild
* aActor
) {
193 BackgroundChildImpl::PBackgroundLSRequestChild
*
194 BackgroundChildImpl::AllocPBackgroundLSRequestChild(
195 const LSRequestParams
& aParams
) {
196 MOZ_CRASH("PBackgroundLSRequestChild actor should be manually constructed!");
199 bool BackgroundChildImpl::DeallocPBackgroundLSRequestChild(
200 PBackgroundLSRequestChild
* aActor
) {
207 BackgroundChildImpl::PBackgroundLocalStorageCacheChild
*
208 BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild(
209 const PrincipalInfo
& aPrincipalInfo
, const nsACString
& aOriginKey
,
210 const uint32_t& aPrivateBrowsingId
) {
212 "PBackgroundLocalStorageChild actors should be manually "
216 bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild(
217 PBackgroundLocalStorageCacheChild
* aActor
) {
224 BackgroundChildImpl::PBackgroundLSSimpleRequestChild
*
225 BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild(
226 const LSSimpleRequestParams
& aParams
) {
228 "PBackgroundLSSimpleRequestChild actor should be manually "
232 bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild(
233 PBackgroundLSSimpleRequestChild
* aActor
) {
240 BackgroundChildImpl::PBackgroundStorageChild
*
241 BackgroundChildImpl::AllocPBackgroundStorageChild(
242 const nsAString
& aProfilePath
, const uint32_t& aPrivateBrowsingId
) {
243 MOZ_CRASH("PBackgroundStorageChild actors should be manually constructed!");
246 bool BackgroundChildImpl::DeallocPBackgroundStorageChild(
247 PBackgroundStorageChild
* aActor
) {
250 StorageDBChild
* child
= static_cast<StorageDBChild
*>(aActor
);
251 child
->ReleaseIPDLReference();
255 already_AddRefed
<PRemoteWorkerChild
>
256 BackgroundChildImpl::AllocPRemoteWorkerChild(const RemoteWorkerData
& aData
) {
257 return MakeAndAddRef
<RemoteWorkerChild
>(aData
);
260 IPCResult
BackgroundChildImpl::RecvPRemoteWorkerConstructor(
261 PRemoteWorkerChild
* aActor
, const RemoteWorkerData
& aData
) {
262 dom::RemoteWorkerChild
* actor
= static_cast<dom::RemoteWorkerChild
*>(aActor
);
263 actor
->ExecWorker(aData
);
267 dom::PSharedWorkerChild
* BackgroundChildImpl::AllocPSharedWorkerChild(
268 const dom::RemoteWorkerData
& aData
, const uint64_t& aWindowID
,
269 const dom::MessagePortIdentifier
& aPortIdentifier
) {
270 RefPtr
<dom::SharedWorkerChild
> agent
= new dom::SharedWorkerChild();
271 return agent
.forget().take();
274 bool BackgroundChildImpl::DeallocPSharedWorkerChild(
275 dom::PSharedWorkerChild
* aActor
) {
276 RefPtr
<dom::SharedWorkerChild
> actor
=
277 dont_AddRef(static_cast<dom::SharedWorkerChild
*>(aActor
));
281 dom::PTemporaryIPCBlobChild
*
282 BackgroundChildImpl::AllocPTemporaryIPCBlobChild() {
283 MOZ_CRASH("This is not supposed to be called.");
287 bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild(
288 dom::PTemporaryIPCBlobChild
* aActor
) {
289 RefPtr
<dom::TemporaryIPCBlobChild
> actor
=
290 dont_AddRef(static_cast<dom::TemporaryIPCBlobChild
*>(aActor
));
294 dom::PFileCreatorChild
* BackgroundChildImpl::AllocPFileCreatorChild(
295 const nsAString
& aFullPath
, const nsAString
& aType
, const nsAString
& aName
,
296 const Maybe
<int64_t>& aLastModified
, const bool& aExistenceCheck
,
297 const bool& aIsFromNsIFile
) {
298 return new dom::FileCreatorChild();
301 bool BackgroundChildImpl::DeallocPFileCreatorChild(PFileCreatorChild
* aActor
) {
302 delete static_cast<dom::FileCreatorChild
*>(aActor
);
306 PUDPSocketChild
* BackgroundChildImpl::AllocPUDPSocketChild(
307 const Maybe
<PrincipalInfo
>& aPrincipalInfo
, const nsACString
& aFilter
) {
308 MOZ_CRASH("AllocPUDPSocket should not be called");
312 bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild
* child
) {
313 UDPSocketChild
* p
= static_cast<UDPSocketChild
*>(child
);
314 p
->ReleaseIPDLReference();
318 // -----------------------------------------------------------------------------
319 // BroadcastChannel API
320 // -----------------------------------------------------------------------------
322 dom::PBroadcastChannelChild
* BackgroundChildImpl::AllocPBroadcastChannelChild(
323 const PrincipalInfo
& aPrincipalInfo
, const nsACString
& aOrigin
,
324 const nsAString
& aChannel
) {
325 RefPtr
<dom::BroadcastChannelChild
> agent
= new dom::BroadcastChannelChild();
326 return agent
.forget().take();
329 bool BackgroundChildImpl::DeallocPBroadcastChannelChild(
330 PBroadcastChannelChild
* aActor
) {
331 RefPtr
<dom::BroadcastChannelChild
> child
=
332 dont_AddRef(static_cast<dom::BroadcastChannelChild
*>(aActor
));
337 camera::PCamerasChild
* BackgroundChildImpl::AllocPCamerasChild() {
339 RefPtr
<camera::CamerasChild
> agent
= new camera::CamerasChild();
340 return agent
.forget().take();
346 bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild
* aActor
) {
348 RefPtr
<camera::CamerasChild
> child
=
349 dont_AddRef(static_cast<camera::CamerasChild
*>(aActor
));
356 // -----------------------------------------------------------------------------
357 // ServiceWorkerManager
358 // -----------------------------------------------------------------------------
360 dom::PServiceWorkerManagerChild
*
361 BackgroundChildImpl::AllocPServiceWorkerManagerChild() {
362 RefPtr
<dom::ServiceWorkerManagerChild
> agent
=
363 new dom::ServiceWorkerManagerChild();
364 return agent
.forget().take();
367 bool BackgroundChildImpl::DeallocPServiceWorkerManagerChild(
368 PServiceWorkerManagerChild
* aActor
) {
369 RefPtr
<dom::ServiceWorkerManagerChild
> child
=
370 dont_AddRef(static_cast<dom::ServiceWorkerManagerChild
*>(aActor
));
375 // -----------------------------------------------------------------------------
377 // -----------------------------------------------------------------------------
379 already_AddRefed
<PCacheChild
> BackgroundChildImpl::AllocPCacheChild() {
380 return dom::cache::AllocPCacheChild();
383 already_AddRefed
<PCacheStreamControlChild
>
384 BackgroundChildImpl::AllocPCacheStreamControlChild() {
385 return dom::cache::AllocPCacheStreamControlChild();
388 // -----------------------------------------------------------------------------
389 // MessageChannel/MessagePort API
390 // -----------------------------------------------------------------------------
392 dom::PMessagePortChild
* BackgroundChildImpl::AllocPMessagePortChild(
393 const nsID
& aUUID
, const nsID
& aDestinationUUID
,
394 const uint32_t& aSequenceID
) {
395 RefPtr
<dom::MessagePortChild
> agent
= new dom::MessagePortChild();
396 return agent
.forget().take();
399 bool BackgroundChildImpl::DeallocPMessagePortChild(PMessagePortChild
* aActor
) {
400 RefPtr
<dom::MessagePortChild
> child
=
401 dont_AddRef(static_cast<dom::MessagePortChild
*>(aActor
));
406 dom::PWebAuthnTransactionChild
*
407 BackgroundChildImpl::AllocPWebAuthnTransactionChild() {
408 MOZ_CRASH("PWebAuthnTransaction actor should be manually constructed!");
412 bool BackgroundChildImpl::DeallocPWebAuthnTransactionChild(
413 PWebAuthnTransactionChild
* aActor
) {
415 RefPtr
<dom::WebAuthnTransactionChild
> child
=
416 dont_AddRef(static_cast<dom::WebAuthnTransactionChild
*>(aActor
));
420 already_AddRefed
<PServiceWorkerChild
>
421 BackgroundChildImpl::AllocPServiceWorkerChild(
422 const IPCServiceWorkerDescriptor
&) {
423 MOZ_CRASH("Shouldn't be called.");
427 already_AddRefed
<PServiceWorkerContainerChild
>
428 BackgroundChildImpl::AllocPServiceWorkerContainerChild() {
429 return mozilla::dom::ServiceWorkerContainerChild::Create();
432 already_AddRefed
<PServiceWorkerRegistrationChild
>
433 BackgroundChildImpl::AllocPServiceWorkerRegistrationChild(
434 const IPCServiceWorkerRegistrationDescriptor
&) {
435 MOZ_CRASH("Shouldn't be called.");
439 dom::PEndpointForReportChild
* BackgroundChildImpl::AllocPEndpointForReportChild(
440 const nsAString
& aGroupName
, const PrincipalInfo
& aPrincipalInfo
) {
441 return new dom::EndpointForReportChild();
444 bool BackgroundChildImpl::DeallocPEndpointForReportChild(
445 PEndpointForReportChild
* aActor
) {
447 delete static_cast<dom::EndpointForReportChild
*>(aActor
);
451 } // namespace mozilla::ipc
453 mozilla::ipc::IPCResult
TestChild::Recv__delete__(const nsACString
& aTestArg
) {
454 MOZ_RELEASE_ASSERT(aTestArg
== mTestArg
,
455 "BackgroundTest message was corrupted!");