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/. */
6 
7 #include "BackgroundChildImpl.h"
8 
9 #include "ActorsChild.h"  // IndexedDB
10 #include "BroadcastChannelChild.h"
11 #include "FileDescriptorSetChild.h"
12 #ifdef MOZ_WEBRTC
13 #  include "CamerasChild.h"
14 #endif
15 #include "mozilla/media/MediaChild.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/SchedulerGroup.h"
18 #include "mozilla/dom/ClientManagerActors.h"
19 #include "mozilla/dom/FileCreatorChild.h"
20 #include "mozilla/dom/PBackgroundLSDatabaseChild.h"
21 #include "mozilla/dom/PBackgroundLSObserverChild.h"
22 #include "mozilla/dom/PBackgroundLSRequestChild.h"
23 #include "mozilla/dom/PBackgroundLSSimpleRequestChild.h"
24 #include "mozilla/dom/PBackgroundSDBConnectionChild.h"
25 #include "mozilla/dom/PFileSystemRequestChild.h"
26 #include "mozilla/dom/EndpointForReportChild.h"
27 #include "mozilla/dom/FileSystemTaskBase.h"
28 #include "mozilla/dom/PMediaTransportChild.h"
29 #include "mozilla/dom/TemporaryIPCBlobChild.h"
30 #include "mozilla/dom/cache/ActorUtils.h"
31 #include "mozilla/dom/indexedDB/PBackgroundIDBFactoryChild.h"
32 #include "mozilla/dom/indexedDB/PBackgroundIndexedDBUtilsChild.h"
33 #include "mozilla/dom/indexedDB/ThreadLocal.h"
34 #include "mozilla/dom/quota/PQuotaChild.h"
35 #include "mozilla/dom/RemoteWorkerChild.h"
36 #include "mozilla/dom/RemoteWorkerControllerChild.h"
37 #include "mozilla/dom/RemoteWorkerServiceChild.h"
38 #include "mozilla/dom/ServiceWorkerChild.h"
39 #include "mozilla/dom/SharedWorkerChild.h"
40 #include "mozilla/dom/StorageIPC.h"
41 #include "mozilla/dom/GamepadEventChannelChild.h"
42 #include "mozilla/dom/GamepadTestChannelChild.h"
43 #include "mozilla/dom/LocalStorage.h"
44 #include "mozilla/dom/MessagePortChild.h"
45 #include "mozilla/dom/ServiceWorkerActors.h"
46 #include "mozilla/dom/ServiceWorkerContainerChild.h"
47 #include "mozilla/dom/ServiceWorkerManagerChild.h"
48 #include "mozilla/dom/BrowserChild.h"
49 #include "mozilla/dom/VsyncChild.h"
50 #include "mozilla/ipc/IPCStreamAlloc.h"
51 #include "mozilla/ipc/PBackgroundTestChild.h"
52 #include "mozilla/ipc/PChildToParentStreamChild.h"
53 #include "mozilla/ipc/PParentToChildStreamChild.h"
54 #include "mozilla/net/HttpBackgroundChannelChild.h"
55 #include "mozilla/net/PUDPSocketChild.h"
56 #include "mozilla/dom/network/UDPSocketChild.h"
57 #include "mozilla/dom/WebAuthnTransactionChild.h"
58 #include "mozilla/dom/MIDIPortChild.h"
59 #include "mozilla/dom/MIDIManagerChild.h"
60 #include "mozilla/RemoteLazyInputStreamChild.h"
61 #include "nsID.h"
62 #include "nsTraceRefcnt.h"
63 
64 namespace {
65 
66 class TestChild final : public mozilla::ipc::PBackgroundTestChild {
67   friend class mozilla::ipc::BackgroundChildImpl;
68 
69   nsCString mTestArg;
70 
TestChild(const nsCString & aTestArg)71   explicit TestChild(const nsCString& aTestArg) : mTestArg(aTestArg) {
72     MOZ_COUNT_CTOR(TestChild);
73   }
74 
75  protected:
~TestChild()76   ~TestChild() override { MOZ_COUNT_DTOR(TestChild); }
77 
78  public:
79   mozilla::ipc::IPCResult Recv__delete__(const nsCString& aTestArg) override;
80 };
81 
82 }  // namespace
83 
84 namespace mozilla::ipc {
85 
86 using mozilla::dom::UDPSocketChild;
87 using mozilla::net::PUDPSocketChild;
88 
89 using mozilla::dom::PServiceWorkerChild;
90 using mozilla::dom::PServiceWorkerContainerChild;
91 using mozilla::dom::PServiceWorkerRegistrationChild;
92 using mozilla::dom::StorageDBChild;
93 using mozilla::dom::cache::PCacheChild;
94 using mozilla::dom::cache::PCacheStorageChild;
95 using mozilla::dom::cache::PCacheStreamControlChild;
96 
97 using mozilla::dom::WebAuthnTransactionChild;
98 
99 using mozilla::dom::PMIDIManagerChild;
100 using mozilla::dom::PMIDIPortChild;
101 
102 // -----------------------------------------------------------------------------
103 // BackgroundChildImpl::ThreadLocal
104 // -----------------------------------------------------------------------------
105 
ThreadLocal()106 BackgroundChildImpl::ThreadLocal::ThreadLocal() : mCurrentFileHandle(nullptr) {
107   // May happen on any thread!
108   MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal);
109 }
110 
~ThreadLocal()111 BackgroundChildImpl::ThreadLocal::~ThreadLocal() {
112   // May happen on any thread!
113   MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal);
114 }
115 
116 // -----------------------------------------------------------------------------
117 // BackgroundChildImpl
118 // -----------------------------------------------------------------------------
119 
BackgroundChildImpl()120 BackgroundChildImpl::BackgroundChildImpl() {
121   // May happen on any thread!
122   MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl);
123 }
124 
~BackgroundChildImpl()125 BackgroundChildImpl::~BackgroundChildImpl() {
126   // May happen on any thread!
127   MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl);
128 }
129 
ProcessingError(Result aCode,const char * aReason)130 void BackgroundChildImpl::ProcessingError(Result aCode, const char* aReason) {
131   // May happen on any thread!
132 
133   nsAutoCString abortMessage;
134 
135   switch (aCode) {
136     case MsgDropped:
137       return;
138 
139 #define HANDLE_CASE(_result)              \
140   case _result:                           \
141     abortMessage.AssignLiteral(#_result); \
142     break
143 
144       HANDLE_CASE(MsgNotKnown);
145       HANDLE_CASE(MsgNotAllowed);
146       HANDLE_CASE(MsgPayloadError);
147       HANDLE_CASE(MsgProcessingError);
148       HANDLE_CASE(MsgRouteError);
149       HANDLE_CASE(MsgValueError);
150 
151 #undef HANDLE_CASE
152 
153     default:
154       MOZ_CRASH("Unknown error code!");
155   }
156 
157   MOZ_CRASH_UNSAFE_PRINTF("%s: %s", abortMessage.get(), aReason);
158 }
159 
ActorDestroy(ActorDestroyReason aWhy)160 void BackgroundChildImpl::ActorDestroy(ActorDestroyReason aWhy) {
161   // May happen on any thread!
162 }
163 
AllocPBackgroundTestChild(const nsCString & aTestArg)164 PBackgroundTestChild* BackgroundChildImpl::AllocPBackgroundTestChild(
165     const nsCString& aTestArg) {
166   return new TestChild(aTestArg);
167 }
168 
DeallocPBackgroundTestChild(PBackgroundTestChild * aActor)169 bool BackgroundChildImpl::DeallocPBackgroundTestChild(
170     PBackgroundTestChild* aActor) {
171   MOZ_ASSERT(aActor);
172 
173   delete static_cast<TestChild*>(aActor);
174   return true;
175 }
176 
177 BackgroundChildImpl::PBackgroundIndexedDBUtilsChild*
AllocPBackgroundIndexedDBUtilsChild()178 BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() {
179   MOZ_CRASH(
180       "PBackgroundIndexedDBUtilsChild actors should be manually "
181       "constructed!");
182 }
183 
DeallocPBackgroundIndexedDBUtilsChild(PBackgroundIndexedDBUtilsChild * aActor)184 bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild(
185     PBackgroundIndexedDBUtilsChild* aActor) {
186   MOZ_ASSERT(aActor);
187 
188   delete aActor;
189   return true;
190 }
191 
192 BackgroundChildImpl::PBackgroundSDBConnectionChild*
AllocPBackgroundSDBConnectionChild(const PersistenceType & aPersistenceType,const PrincipalInfo & aPrincipalInfo)193 BackgroundChildImpl::AllocPBackgroundSDBConnectionChild(
194     const PersistenceType& aPersistenceType,
195     const PrincipalInfo& aPrincipalInfo) {
196   MOZ_CRASH(
197       "PBackgroundSDBConnectionChild actor should be manually "
198       "constructed!");
199 }
200 
DeallocPBackgroundSDBConnectionChild(PBackgroundSDBConnectionChild * aActor)201 bool BackgroundChildImpl::DeallocPBackgroundSDBConnectionChild(
202     PBackgroundSDBConnectionChild* aActor) {
203   MOZ_ASSERT(aActor);
204 
205   delete aActor;
206   return true;
207 }
208 
209 BackgroundChildImpl::PBackgroundLSDatabaseChild*
AllocPBackgroundLSDatabaseChild(const PrincipalInfo & aPrincipalInfo,const uint32_t & aPrivateBrowsingId,const uint64_t & aDatastoreId)210 BackgroundChildImpl::AllocPBackgroundLSDatabaseChild(
211     const PrincipalInfo& aPrincipalInfo, const uint32_t& aPrivateBrowsingId,
212     const uint64_t& aDatastoreId) {
213   MOZ_CRASH("PBackgroundLSDatabaseChild actor should be manually constructed!");
214 }
215 
DeallocPBackgroundLSDatabaseChild(PBackgroundLSDatabaseChild * aActor)216 bool BackgroundChildImpl::DeallocPBackgroundLSDatabaseChild(
217     PBackgroundLSDatabaseChild* aActor) {
218   MOZ_ASSERT(aActor);
219 
220   delete aActor;
221   return true;
222 }
223 
224 BackgroundChildImpl::PBackgroundLSObserverChild*
AllocPBackgroundLSObserverChild(const uint64_t & aObserverId)225 BackgroundChildImpl::AllocPBackgroundLSObserverChild(
226     const uint64_t& aObserverId) {
227   MOZ_CRASH("PBackgroundLSObserverChild actor should be manually constructed!");
228 }
229 
DeallocPBackgroundLSObserverChild(PBackgroundLSObserverChild * aActor)230 bool BackgroundChildImpl::DeallocPBackgroundLSObserverChild(
231     PBackgroundLSObserverChild* aActor) {
232   MOZ_ASSERT(aActor);
233 
234   delete aActor;
235   return true;
236 }
237 
238 BackgroundChildImpl::PBackgroundLSRequestChild*
AllocPBackgroundLSRequestChild(const LSRequestParams & aParams)239 BackgroundChildImpl::AllocPBackgroundLSRequestChild(
240     const LSRequestParams& aParams) {
241   MOZ_CRASH("PBackgroundLSRequestChild actor should be manually constructed!");
242 }
243 
DeallocPBackgroundLSRequestChild(PBackgroundLSRequestChild * aActor)244 bool BackgroundChildImpl::DeallocPBackgroundLSRequestChild(
245     PBackgroundLSRequestChild* aActor) {
246   MOZ_ASSERT(aActor);
247 
248   delete aActor;
249   return true;
250 }
251 
252 BackgroundChildImpl::PBackgroundLocalStorageCacheChild*
AllocPBackgroundLocalStorageCacheChild(const PrincipalInfo & aPrincipalInfo,const nsCString & aOriginKey,const uint32_t & aPrivateBrowsingId)253 BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild(
254     const PrincipalInfo& aPrincipalInfo, const nsCString& aOriginKey,
255     const uint32_t& aPrivateBrowsingId) {
256   MOZ_CRASH(
257       "PBackgroundLocalStorageChild actors should be manually "
258       "constructed!");
259 }
260 
DeallocPBackgroundLocalStorageCacheChild(PBackgroundLocalStorageCacheChild * aActor)261 bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild(
262     PBackgroundLocalStorageCacheChild* aActor) {
263   MOZ_ASSERT(aActor);
264 
265   delete aActor;
266   return true;
267 }
268 
269 BackgroundChildImpl::PBackgroundLSSimpleRequestChild*
AllocPBackgroundLSSimpleRequestChild(const LSSimpleRequestParams & aParams)270 BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild(
271     const LSSimpleRequestParams& aParams) {
272   MOZ_CRASH(
273       "PBackgroundLSSimpleRequestChild actor should be manually "
274       "constructed!");
275 }
276 
DeallocPBackgroundLSSimpleRequestChild(PBackgroundLSSimpleRequestChild * aActor)277 bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild(
278     PBackgroundLSSimpleRequestChild* aActor) {
279   MOZ_ASSERT(aActor);
280 
281   delete aActor;
282   return true;
283 }
284 
285 BackgroundChildImpl::PBackgroundStorageChild*
AllocPBackgroundStorageChild(const nsString & aProfilePath,const uint32_t & aPrivateBrowsingId)286 BackgroundChildImpl::AllocPBackgroundStorageChild(
287     const nsString& aProfilePath, const uint32_t& aPrivateBrowsingId) {
288   MOZ_CRASH("PBackgroundStorageChild actors should be manually constructed!");
289 }
290 
DeallocPBackgroundStorageChild(PBackgroundStorageChild * aActor)291 bool BackgroundChildImpl::DeallocPBackgroundStorageChild(
292     PBackgroundStorageChild* aActor) {
293   MOZ_ASSERT(aActor);
294 
295   StorageDBChild* child = static_cast<StorageDBChild*>(aActor);
296   child->ReleaseIPDLReference();
297   return true;
298 }
299 
AllocPRemoteWorkerChild(const RemoteWorkerData & aData)300 dom::PRemoteWorkerChild* BackgroundChildImpl::AllocPRemoteWorkerChild(
301     const RemoteWorkerData& aData) {
302   RefPtr<dom::RemoteWorkerChild> agent = new dom::RemoteWorkerChild(aData);
303   return agent.forget().take();
304 }
305 
RecvPRemoteWorkerConstructor(PRemoteWorkerChild * aActor,const RemoteWorkerData & aData)306 IPCResult BackgroundChildImpl::RecvPRemoteWorkerConstructor(
307     PRemoteWorkerChild* aActor, const RemoteWorkerData& aData) {
308   dom::RemoteWorkerChild* actor = static_cast<dom::RemoteWorkerChild*>(aActor);
309   actor->ExecWorker(aData);
310   return IPC_OK();
311 }
312 
DeallocPRemoteWorkerChild(dom::PRemoteWorkerChild * aActor)313 bool BackgroundChildImpl::DeallocPRemoteWorkerChild(
314     dom::PRemoteWorkerChild* aActor) {
315   RefPtr<dom::RemoteWorkerChild> actor =
316       dont_AddRef(static_cast<dom::RemoteWorkerChild*>(aActor));
317   return true;
318 }
319 
320 dom::PRemoteWorkerControllerChild*
AllocPRemoteWorkerControllerChild(const dom::RemoteWorkerData & aRemoteWorkerData)321 BackgroundChildImpl::AllocPRemoteWorkerControllerChild(
322     const dom::RemoteWorkerData& aRemoteWorkerData) {
323   MOZ_CRASH(
324       "PRemoteWorkerControllerChild actors must be manually constructed!");
325   return nullptr;
326 }
327 
DeallocPRemoteWorkerControllerChild(dom::PRemoteWorkerControllerChild * aActor)328 bool BackgroundChildImpl::DeallocPRemoteWorkerControllerChild(
329     dom::PRemoteWorkerControllerChild* aActor) {
330   MOZ_ASSERT(aActor);
331 
332   RefPtr<dom::RemoteWorkerControllerChild> actor =
333       dont_AddRef(static_cast<dom::RemoteWorkerControllerChild*>(aActor));
334   return true;
335 }
336 
337 dom::PRemoteWorkerServiceChild*
AllocPRemoteWorkerServiceChild()338 BackgroundChildImpl::AllocPRemoteWorkerServiceChild() {
339   RefPtr<dom::RemoteWorkerServiceChild> agent =
340       new dom::RemoteWorkerServiceChild();
341   return agent.forget().take();
342 }
343 
DeallocPRemoteWorkerServiceChild(dom::PRemoteWorkerServiceChild * aActor)344 bool BackgroundChildImpl::DeallocPRemoteWorkerServiceChild(
345     dom::PRemoteWorkerServiceChild* aActor) {
346   RefPtr<dom::RemoteWorkerServiceChild> actor =
347       dont_AddRef(static_cast<dom::RemoteWorkerServiceChild*>(aActor));
348   return true;
349 }
350 
AllocPSharedWorkerChild(const dom::RemoteWorkerData & aData,const uint64_t & aWindowID,const dom::MessagePortIdentifier & aPortIdentifier)351 dom::PSharedWorkerChild* BackgroundChildImpl::AllocPSharedWorkerChild(
352     const dom::RemoteWorkerData& aData, const uint64_t& aWindowID,
353     const dom::MessagePortIdentifier& aPortIdentifier) {
354   RefPtr<dom::SharedWorkerChild> agent = new dom::SharedWorkerChild();
355   return agent.forget().take();
356 }
357 
DeallocPSharedWorkerChild(dom::PSharedWorkerChild * aActor)358 bool BackgroundChildImpl::DeallocPSharedWorkerChild(
359     dom::PSharedWorkerChild* aActor) {
360   RefPtr<dom::SharedWorkerChild> actor =
361       dont_AddRef(static_cast<dom::SharedWorkerChild*>(aActor));
362   return true;
363 }
364 
365 dom::PTemporaryIPCBlobChild*
AllocPTemporaryIPCBlobChild()366 BackgroundChildImpl::AllocPTemporaryIPCBlobChild() {
367   MOZ_CRASH("This is not supposed to be called.");
368   return nullptr;
369 }
370 
DeallocPTemporaryIPCBlobChild(dom::PTemporaryIPCBlobChild * aActor)371 bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild(
372     dom::PTemporaryIPCBlobChild* aActor) {
373   RefPtr<dom::TemporaryIPCBlobChild> actor =
374       dont_AddRef(static_cast<dom::TemporaryIPCBlobChild*>(aActor));
375   return true;
376 }
377 
AllocPFileCreatorChild(const nsString & aFullPath,const nsString & aType,const nsString & aName,const Maybe<int64_t> & aLastModified,const bool & aExistenceCheck,const bool & aIsFromNsIFile)378 dom::PFileCreatorChild* BackgroundChildImpl::AllocPFileCreatorChild(
379     const nsString& aFullPath, const nsString& aType, const nsString& aName,
380     const Maybe<int64_t>& aLastModified, const bool& aExistenceCheck,
381     const bool& aIsFromNsIFile) {
382   return new dom::FileCreatorChild();
383 }
384 
DeallocPFileCreatorChild(PFileCreatorChild * aActor)385 bool BackgroundChildImpl::DeallocPFileCreatorChild(PFileCreatorChild* aActor) {
386   delete static_cast<dom::FileCreatorChild*>(aActor);
387   return true;
388 }
389 
390 already_AddRefed<PRemoteLazyInputStreamChild>
AllocPRemoteLazyInputStreamChild(const nsID & aID,const uint64_t & aSize)391 BackgroundChildImpl::AllocPRemoteLazyInputStreamChild(const nsID& aID,
392                                                       const uint64_t& aSize) {
393   RefPtr<RemoteLazyInputStreamChild> actor =
394       new RemoteLazyInputStreamChild(aID, aSize);
395   return actor.forget();
396 }
397 
AllocPFileDescriptorSetChild(const FileDescriptor & aFileDescriptor)398 PFileDescriptorSetChild* BackgroundChildImpl::AllocPFileDescriptorSetChild(
399     const FileDescriptor& aFileDescriptor) {
400   return new FileDescriptorSetChild(aFileDescriptor);
401 }
402 
DeallocPFileDescriptorSetChild(PFileDescriptorSetChild * aActor)403 bool BackgroundChildImpl::DeallocPFileDescriptorSetChild(
404     PFileDescriptorSetChild* aActor) {
405   MOZ_ASSERT(aActor);
406 
407   delete static_cast<FileDescriptorSetChild*>(aActor);
408   return true;
409 }
410 
AllocPVsyncChild()411 dom::PVsyncChild* BackgroundChildImpl::AllocPVsyncChild() {
412   RefPtr<dom::VsyncChild> actor = new dom::VsyncChild();
413   // There still has one ref-count after return, and it will be released in
414   // DeallocPVsyncChild().
415   return actor.forget().take();
416 }
417 
DeallocPVsyncChild(PVsyncChild * aActor)418 bool BackgroundChildImpl::DeallocPVsyncChild(PVsyncChild* aActor) {
419   MOZ_ASSERT(aActor);
420 
421   // This actor already has one ref-count. Please check AllocPVsyncChild().
422   RefPtr<dom::VsyncChild> actor =
423       dont_AddRef(static_cast<dom::VsyncChild*>(aActor));
424   return true;
425 }
426 
AllocPUDPSocketChild(const Maybe<PrincipalInfo> & aPrincipalInfo,const nsCString & aFilter)427 PUDPSocketChild* BackgroundChildImpl::AllocPUDPSocketChild(
428     const Maybe<PrincipalInfo>& aPrincipalInfo, const nsCString& aFilter) {
429   MOZ_CRASH("AllocPUDPSocket should not be called");
430   return nullptr;
431 }
432 
DeallocPUDPSocketChild(PUDPSocketChild * child)433 bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild* child) {
434   UDPSocketChild* p = static_cast<UDPSocketChild*>(child);
435   p->ReleaseIPDLReference();
436   return true;
437 }
438 
439 // -----------------------------------------------------------------------------
440 // BroadcastChannel API
441 // -----------------------------------------------------------------------------
442 
AllocPBroadcastChannelChild(const PrincipalInfo & aPrincipalInfo,const nsCString & aOrigin,const nsString & aChannel)443 dom::PBroadcastChannelChild* BackgroundChildImpl::AllocPBroadcastChannelChild(
444     const PrincipalInfo& aPrincipalInfo, const nsCString& aOrigin,
445     const nsString& aChannel) {
446   RefPtr<dom::BroadcastChannelChild> agent = new dom::BroadcastChannelChild();
447   return agent.forget().take();
448 }
449 
DeallocPBroadcastChannelChild(PBroadcastChannelChild * aActor)450 bool BackgroundChildImpl::DeallocPBroadcastChannelChild(
451     PBroadcastChannelChild* aActor) {
452   RefPtr<dom::BroadcastChannelChild> child =
453       dont_AddRef(static_cast<dom::BroadcastChannelChild*>(aActor));
454   MOZ_ASSERT(child);
455   return true;
456 }
457 
AllocPCamerasChild()458 camera::PCamerasChild* BackgroundChildImpl::AllocPCamerasChild() {
459 #ifdef MOZ_WEBRTC
460   RefPtr<camera::CamerasChild> agent = new camera::CamerasChild();
461   return agent.forget().take();
462 #else
463   return nullptr;
464 #endif
465 }
466 
DeallocPCamerasChild(camera::PCamerasChild * aActor)467 bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild* aActor) {
468 #ifdef MOZ_WEBRTC
469   RefPtr<camera::CamerasChild> child =
470       dont_AddRef(static_cast<camera::CamerasChild*>(aActor));
471   MOZ_ASSERT(aActor);
472   camera::Shutdown();
473 #endif
474   return true;
475 }
476 
477 // -----------------------------------------------------------------------------
478 // ServiceWorkerManager
479 // -----------------------------------------------------------------------------
480 
481 dom::PServiceWorkerManagerChild*
AllocPServiceWorkerManagerChild()482 BackgroundChildImpl::AllocPServiceWorkerManagerChild() {
483   RefPtr<dom::ServiceWorkerManagerChild> agent =
484       new dom::ServiceWorkerManagerChild();
485   return agent.forget().take();
486 }
487 
DeallocPServiceWorkerManagerChild(PServiceWorkerManagerChild * aActor)488 bool BackgroundChildImpl::DeallocPServiceWorkerManagerChild(
489     PServiceWorkerManagerChild* aActor) {
490   RefPtr<dom::ServiceWorkerManagerChild> child =
491       dont_AddRef(static_cast<dom::ServiceWorkerManagerChild*>(aActor));
492   MOZ_ASSERT(child);
493   return true;
494 }
495 
496 // -----------------------------------------------------------------------------
497 // Cache API
498 // -----------------------------------------------------------------------------
499 
AllocPCacheStorageChild(const Namespace & aNamespace,const PrincipalInfo & aPrincipalInfo)500 PCacheStorageChild* BackgroundChildImpl::AllocPCacheStorageChild(
501     const Namespace& aNamespace, const PrincipalInfo& aPrincipalInfo) {
502   MOZ_CRASH("CacheStorageChild actor must be provided to PBackground manager");
503   return nullptr;
504 }
505 
DeallocPCacheStorageChild(PCacheStorageChild * aActor)506 bool BackgroundChildImpl::DeallocPCacheStorageChild(
507     PCacheStorageChild* aActor) {
508   dom::cache::DeallocPCacheStorageChild(aActor);
509   return true;
510 }
511 
AllocPCacheChild()512 PCacheChild* BackgroundChildImpl::AllocPCacheChild() {
513   return dom::cache::AllocPCacheChild();
514 }
515 
DeallocPCacheChild(PCacheChild * aActor)516 bool BackgroundChildImpl::DeallocPCacheChild(PCacheChild* aActor) {
517   dom::cache::DeallocPCacheChild(aActor);
518   return true;
519 }
520 
521 already_AddRefed<PCacheStreamControlChild>
AllocPCacheStreamControlChild()522 BackgroundChildImpl::AllocPCacheStreamControlChild() {
523   return dom::cache::AllocPCacheStreamControlChild();
524 }
525 
526 // -----------------------------------------------------------------------------
527 // MessageChannel/MessagePort API
528 // -----------------------------------------------------------------------------
529 
AllocPMessagePortChild(const nsID & aUUID,const nsID & aDestinationUUID,const uint32_t & aSequenceID)530 dom::PMessagePortChild* BackgroundChildImpl::AllocPMessagePortChild(
531     const nsID& aUUID, const nsID& aDestinationUUID,
532     const uint32_t& aSequenceID) {
533   RefPtr<dom::MessagePortChild> agent = new dom::MessagePortChild();
534   return agent.forget().take();
535 }
536 
DeallocPMessagePortChild(PMessagePortChild * aActor)537 bool BackgroundChildImpl::DeallocPMessagePortChild(PMessagePortChild* aActor) {
538   RefPtr<dom::MessagePortChild> child =
539       dont_AddRef(static_cast<dom::MessagePortChild*>(aActor));
540   MOZ_ASSERT(child);
541   return true;
542 }
543 
544 PChildToParentStreamChild*
AllocPChildToParentStreamChild()545 BackgroundChildImpl::AllocPChildToParentStreamChild() {
546   MOZ_CRASH("PChildToParentStreamChild actors should be manually constructed!");
547 }
548 
DeallocPChildToParentStreamChild(PChildToParentStreamChild * aActor)549 bool BackgroundChildImpl::DeallocPChildToParentStreamChild(
550     PChildToParentStreamChild* aActor) {
551   delete aActor;
552   return true;
553 }
554 
555 PParentToChildStreamChild*
AllocPParentToChildStreamChild()556 BackgroundChildImpl::AllocPParentToChildStreamChild() {
557   return mozilla::ipc::AllocPParentToChildStreamChild();
558 }
559 
DeallocPParentToChildStreamChild(PParentToChildStreamChild * aActor)560 bool BackgroundChildImpl::DeallocPParentToChildStreamChild(
561     PParentToChildStreamChild* aActor) {
562   delete aActor;
563   return true;
564 }
565 
AllocPQuotaChild()566 BackgroundChildImpl::PQuotaChild* BackgroundChildImpl::AllocPQuotaChild() {
567   MOZ_CRASH("PQuotaChild actor should be manually constructed!");
568 }
569 
DeallocPQuotaChild(PQuotaChild * aActor)570 bool BackgroundChildImpl::DeallocPQuotaChild(PQuotaChild* aActor) {
571   MOZ_ASSERT(aActor);
572   delete aActor;
573   return true;
574 }
575 
576 // -----------------------------------------------------------------------------
577 // WebMIDI API
578 // -----------------------------------------------------------------------------
579 
AllocPMIDIPortChild(const MIDIPortInfo & aPortInfo,const bool & aSysexEnabled)580 PMIDIPortChild* BackgroundChildImpl::AllocPMIDIPortChild(
581     const MIDIPortInfo& aPortInfo, const bool& aSysexEnabled) {
582   MOZ_CRASH("Should be created manually");
583   return nullptr;
584 }
585 
DeallocPMIDIPortChild(PMIDIPortChild * aActor)586 bool BackgroundChildImpl::DeallocPMIDIPortChild(PMIDIPortChild* aActor) {
587   MOZ_ASSERT(aActor);
588   // The reference is increased in dom/midi/MIDIPort.cpp. We should
589   // decrease it after IPC.
590   RefPtr<dom::MIDIPortChild> child =
591       dont_AddRef(static_cast<dom::MIDIPortChild*>(aActor));
592   child->Teardown();
593   return true;
594 }
595 
AllocPMIDIManagerChild()596 PMIDIManagerChild* BackgroundChildImpl::AllocPMIDIManagerChild() {
597   MOZ_CRASH("Should be created manually");
598   return nullptr;
599 }
600 
DeallocPMIDIManagerChild(PMIDIManagerChild * aActor)601 bool BackgroundChildImpl::DeallocPMIDIManagerChild(PMIDIManagerChild* aActor) {
602   MOZ_ASSERT(aActor);
603   // The reference is increased in dom/midi/MIDIAccessManager.cpp. We should
604   // decrease it after IPC.
605   RefPtr<dom::MIDIManagerChild> child =
606       dont_AddRef(static_cast<dom::MIDIManagerChild*>(aActor));
607   return true;
608 }
609 
610 mozilla::dom::PClientManagerChild*
AllocPClientManagerChild()611 BackgroundChildImpl::AllocPClientManagerChild() {
612   return mozilla::dom::AllocClientManagerChild();
613 }
614 
DeallocPClientManagerChild(mozilla::dom::PClientManagerChild * aActor)615 bool BackgroundChildImpl::DeallocPClientManagerChild(
616     mozilla::dom::PClientManagerChild* aActor) {
617   return mozilla::dom::DeallocClientManagerChild(aActor);
618 }
619 
620 #ifdef EARLY_BETA_OR_EARLIER
OnChannelReceivedMessage(const Message & aMsg)621 void BackgroundChildImpl::OnChannelReceivedMessage(const Message& aMsg) {
622   if (aMsg.type() == dom::PVsync::MessageType::Msg_Notify__ID) {
623     // Not really necessary to look at the message payload, it will be
624     // <0.5ms away from TimeStamp::Now()
625     SchedulerGroup::MarkVsyncReceived();
626   }
627 }
628 #endif
629 
630 dom::PWebAuthnTransactionChild*
AllocPWebAuthnTransactionChild()631 BackgroundChildImpl::AllocPWebAuthnTransactionChild() {
632   MOZ_CRASH("PWebAuthnTransaction actor should be manually constructed!");
633   return nullptr;
634 }
635 
DeallocPWebAuthnTransactionChild(PWebAuthnTransactionChild * aActor)636 bool BackgroundChildImpl::DeallocPWebAuthnTransactionChild(
637     PWebAuthnTransactionChild* aActor) {
638   MOZ_ASSERT(aActor);
639   RefPtr<dom::WebAuthnTransactionChild> child =
640       dont_AddRef(static_cast<dom::WebAuthnTransactionChild*>(aActor));
641   return true;
642 }
643 
644 already_AddRefed<PServiceWorkerChild>
AllocPServiceWorkerChild(const IPCServiceWorkerDescriptor &)645 BackgroundChildImpl::AllocPServiceWorkerChild(
646     const IPCServiceWorkerDescriptor&) {
647   MOZ_CRASH("Shouldn't be called.");
648   return {};
649 }
650 
651 already_AddRefed<PServiceWorkerContainerChild>
AllocPServiceWorkerContainerChild()652 BackgroundChildImpl::AllocPServiceWorkerContainerChild() {
653   return mozilla::dom::ServiceWorkerContainerChild::Create();
654 }
655 
656 already_AddRefed<PServiceWorkerRegistrationChild>
AllocPServiceWorkerRegistrationChild(const IPCServiceWorkerRegistrationDescriptor &)657 BackgroundChildImpl::AllocPServiceWorkerRegistrationChild(
658     const IPCServiceWorkerRegistrationDescriptor&) {
659   MOZ_CRASH("Shouldn't be called.");
660   return {};
661 }
662 
AllocPEndpointForReportChild(const nsString & aGroupName,const PrincipalInfo & aPrincipalInfo)663 dom::PEndpointForReportChild* BackgroundChildImpl::AllocPEndpointForReportChild(
664     const nsString& aGroupName, const PrincipalInfo& aPrincipalInfo) {
665   return new dom::EndpointForReportChild();
666 }
667 
DeallocPEndpointForReportChild(PEndpointForReportChild * aActor)668 bool BackgroundChildImpl::DeallocPEndpointForReportChild(
669     PEndpointForReportChild* aActor) {
670   MOZ_ASSERT(aActor);
671   delete static_cast<dom::EndpointForReportChild*>(aActor);
672   return true;
673 }
674 
AllocPMediaTransportChild()675 dom::PMediaTransportChild* BackgroundChildImpl::AllocPMediaTransportChild() {
676   // We don't allocate here: MediaTransportHandlerIPC is in charge of that,
677   // so we don't need to know the implementation particulars here.
678   MOZ_ASSERT_UNREACHABLE(
679       "The only thing that ought to be creating a PMediaTransportChild is "
680       "MediaTransportHandlerIPC!");
681   return nullptr;
682 }
683 
DeallocPMediaTransportChild(dom::PMediaTransportChild * aActor)684 bool BackgroundChildImpl::DeallocPMediaTransportChild(
685     dom::PMediaTransportChild* aActor) {
686   delete aActor;
687   return true;
688 }
689 
690 PChildToParentStreamChild*
SendPChildToParentStreamConstructor(PChildToParentStreamChild * aActor)691 BackgroundChildImpl::SendPChildToParentStreamConstructor(
692     PChildToParentStreamChild* aActor) {
693   return PBackgroundChild::SendPChildToParentStreamConstructor(aActor);
694 }
695 
SendPFileDescriptorSetConstructor(const FileDescriptor & aFD)696 PFileDescriptorSetChild* BackgroundChildImpl::SendPFileDescriptorSetConstructor(
697     const FileDescriptor& aFD) {
698   return PBackgroundChild::SendPFileDescriptorSetConstructor(aFD);
699 }
700 
701 }  // namespace mozilla::ipc
702 
Recv__delete__(const nsCString & aTestArg)703 mozilla::ipc::IPCResult TestChild::Recv__delete__(const nsCString& aTestArg) {
704   MOZ_RELEASE_ASSERT(aTestArg == mTestArg,
705                      "BackgroundTest message was corrupted!");
706 
707   return IPC_OK();
708 }
709