Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / ipc / glue / BackgroundChildImpl.cpp
blob536ef2a10e8c4771d5d4a73a5c53a80495b54711
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"
10 #ifdef MOZ_WEBRTC
11 # include "CamerasChild.h"
12 #endif
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"
46 #include "nsID.h"
48 namespace {
50 class TestChild final : public mozilla::ipc::PBackgroundTestChild {
51 friend class mozilla::ipc::BackgroundChildImpl;
53 nsCString mTestArg;
55 explicit TestChild(const nsACString& aTestArg) : mTestArg(aTestArg) {
56 MOZ_COUNT_CTOR(TestChild);
59 protected:
60 ~TestChild() override { MOZ_COUNT_DTOR(TestChild); }
62 public:
63 mozilla::ipc::IPCResult Recv__delete__(const nsACString& aTestArg) override;
66 } // namespace
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;
120 switch (aCode) {
121 case MsgDropped:
122 return;
124 #define HANDLE_CASE(_result) \
125 case _result: \
126 abortMessage.AssignLiteral(#_result); \
127 break
129 HANDLE_CASE(MsgNotKnown);
130 HANDLE_CASE(MsgNotAllowed);
131 HANDLE_CASE(MsgPayloadError);
132 HANDLE_CASE(MsgProcessingError);
133 HANDLE_CASE(MsgRouteError);
134 HANDLE_CASE(MsgValueError);
136 #undef HANDLE_CASE
138 default:
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) {
156 MOZ_ASSERT(aActor);
158 delete static_cast<TestChild*>(aActor);
159 return true;
162 BackgroundChildImpl::PBackgroundIndexedDBUtilsChild*
163 BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() {
164 MOZ_CRASH(
165 "PBackgroundIndexedDBUtilsChild actors should be manually "
166 "constructed!");
169 bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild(
170 PBackgroundIndexedDBUtilsChild* aActor) {
171 MOZ_ASSERT(aActor);
173 delete aActor;
174 return true;
177 BackgroundChildImpl::PBackgroundSDBConnectionChild*
178 BackgroundChildImpl::AllocPBackgroundSDBConnectionChild(
179 const PersistenceType& aPersistenceType,
180 const PrincipalInfo& aPrincipalInfo) {
181 MOZ_CRASH(
182 "PBackgroundSDBConnectionChild actor should be manually "
183 "constructed!");
186 bool BackgroundChildImpl::DeallocPBackgroundSDBConnectionChild(
187 PBackgroundSDBConnectionChild* aActor) {
188 MOZ_ASSERT(aActor);
190 delete aActor;
191 return true;
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) {
203 MOZ_ASSERT(aActor);
205 delete aActor;
206 return true;
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) {
217 MOZ_ASSERT(aActor);
219 delete aActor;
220 return true;
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) {
231 MOZ_ASSERT(aActor);
233 delete aActor;
234 return true;
237 BackgroundChildImpl::PBackgroundLocalStorageCacheChild*
238 BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild(
239 const PrincipalInfo& aPrincipalInfo, const nsACString& aOriginKey,
240 const uint32_t& aPrivateBrowsingId) {
241 MOZ_CRASH(
242 "PBackgroundLocalStorageChild actors should be manually "
243 "constructed!");
246 bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild(
247 PBackgroundLocalStorageCacheChild* aActor) {
248 MOZ_ASSERT(aActor);
250 delete aActor;
251 return true;
254 BackgroundChildImpl::PBackgroundLSSimpleRequestChild*
255 BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild(
256 const LSSimpleRequestParams& aParams) {
257 MOZ_CRASH(
258 "PBackgroundLSSimpleRequestChild actor should be manually "
259 "constructed!");
262 bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild(
263 PBackgroundLSSimpleRequestChild* aActor) {
264 MOZ_ASSERT(aActor);
266 delete aActor;
267 return true;
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) {
278 MOZ_ASSERT(aActor);
280 StorageDBChild* child = static_cast<StorageDBChild*>(aActor);
281 child->ReleaseIPDLReference();
282 return true;
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);
294 return IPC_OK();
297 dom::PRemoteWorkerControllerChild*
298 BackgroundChildImpl::AllocPRemoteWorkerControllerChild(
299 const dom::RemoteWorkerData& aRemoteWorkerData) {
300 MOZ_CRASH(
301 "PRemoteWorkerControllerChild actors must be manually constructed!");
302 return nullptr;
305 bool BackgroundChildImpl::DeallocPRemoteWorkerControllerChild(
306 dom::PRemoteWorkerControllerChild* aActor) {
307 MOZ_ASSERT(aActor);
309 RefPtr<dom::RemoteWorkerControllerChild> actor =
310 dont_AddRef(static_cast<dom::RemoteWorkerControllerChild*>(aActor));
311 return true;
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));
325 return true;
328 dom::PTemporaryIPCBlobChild*
329 BackgroundChildImpl::AllocPTemporaryIPCBlobChild() {
330 MOZ_CRASH("This is not supposed to be called.");
331 return nullptr;
334 bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild(
335 dom::PTemporaryIPCBlobChild* aActor) {
336 RefPtr<dom::TemporaryIPCBlobChild> actor =
337 dont_AddRef(static_cast<dom::TemporaryIPCBlobChild*>(aActor));
338 return true;
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);
350 return true;
353 PUDPSocketChild* BackgroundChildImpl::AllocPUDPSocketChild(
354 const Maybe<PrincipalInfo>& aPrincipalInfo, const nsACString& aFilter) {
355 MOZ_CRASH("AllocPUDPSocket should not be called");
356 return nullptr;
359 bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild* child) {
360 UDPSocketChild* p = static_cast<UDPSocketChild*>(child);
361 p->ReleaseIPDLReference();
362 return true;
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));
380 MOZ_ASSERT(child);
381 return true;
384 camera::PCamerasChild* BackgroundChildImpl::AllocPCamerasChild() {
385 #ifdef MOZ_WEBRTC
386 RefPtr<camera::CamerasChild> agent = new camera::CamerasChild();
387 return agent.forget().take();
388 #else
389 return nullptr;
390 #endif
393 bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild* aActor) {
394 #ifdef MOZ_WEBRTC
395 RefPtr<camera::CamerasChild> child =
396 dont_AddRef(static_cast<camera::CamerasChild*>(aActor));
397 MOZ_ASSERT(aActor);
398 camera::Shutdown();
399 #endif
400 return true;
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));
418 MOZ_ASSERT(child);
419 return true;
422 // -----------------------------------------------------------------------------
423 // Cache API
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));
449 MOZ_ASSERT(child);
450 return true;
453 BackgroundChildImpl::PQuotaChild* BackgroundChildImpl::AllocPQuotaChild() {
454 MOZ_CRASH("PQuotaChild actor should be manually constructed!");
457 bool BackgroundChildImpl::DeallocPQuotaChild(PQuotaChild* aActor) {
458 MOZ_ASSERT(aActor);
459 delete aActor;
460 return true;
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!");
476 return nullptr;
479 bool BackgroundChildImpl::DeallocPWebAuthnTransactionChild(
480 PWebAuthnTransactionChild* aActor) {
481 MOZ_ASSERT(aActor);
482 RefPtr<dom::WebAuthnTransactionChild> child =
483 dont_AddRef(static_cast<dom::WebAuthnTransactionChild*>(aActor));
484 return true;
487 already_AddRefed<PServiceWorkerChild>
488 BackgroundChildImpl::AllocPServiceWorkerChild(
489 const IPCServiceWorkerDescriptor&) {
490 MOZ_CRASH("Shouldn't be called.");
491 return {};
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.");
503 return {};
506 dom::PEndpointForReportChild* BackgroundChildImpl::AllocPEndpointForReportChild(
507 const nsAString& aGroupName, const PrincipalInfo& aPrincipalInfo) {
508 return new dom::EndpointForReportChild();
511 bool BackgroundChildImpl::DeallocPEndpointForReportChild(
512 PEndpointForReportChild* aActor) {
513 MOZ_ASSERT(aActor);
514 delete static_cast<dom::EndpointForReportChild*>(aActor);
515 return true;
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!");
524 return nullptr;
527 bool BackgroundChildImpl::DeallocPMediaTransportChild(
528 dom::PMediaTransportChild* aActor) {
529 delete aActor;
530 return true;
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!");
539 return IPC_OK();