summaryrefslogtreecommitdiffstats
path: root/ipc/glue/BackgroundChildImpl.cpp
blob: 92d10f51d9f8854475785f85b746d0f2fc967f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "BackgroundChildImpl.h"

#include "BroadcastChannelChild.h"
#ifdef MOZ_WEBRTC
#  include "CamerasChild.h"
#endif
#include "mozilla/Assertions.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/dom/ClientManagerActors.h"
#include "mozilla/dom/FileCreatorChild.h"
#include "mozilla/dom/PBackgroundLSDatabaseChild.h"
#include "mozilla/dom/PBackgroundLSObserverChild.h"
#include "mozilla/dom/PBackgroundLSRequestChild.h"
#include "mozilla/dom/PBackgroundLSSimpleRequestChild.h"
#include "mozilla/dom/PBackgroundSDBConnectionChild.h"
#include "mozilla/dom/PFileSystemRequestChild.h"
#include "mozilla/dom/EndpointForReportChild.h"
#include "mozilla/dom/PMediaTransportChild.h"
#include "mozilla/dom/PVsync.h"
#include "mozilla/dom/TemporaryIPCBlobChild.h"
#include "mozilla/dom/cache/ActorUtils.h"
#include "mozilla/dom/indexedDB/PBackgroundIndexedDBUtilsChild.h"
#include "mozilla/dom/indexedDB/ThreadLocal.h"
#include "mozilla/dom/quota/PQuotaChild.h"
#include "mozilla/dom/RemoteWorkerChild.h"
#include "mozilla/dom/RemoteWorkerControllerChild.h"
#include "mozilla/dom/RemoteWorkerServiceChild.h"
#include "mozilla/dom/ServiceWorkerChild.h"
#include "mozilla/dom/SharedWorkerChild.h"
#include "mozilla/dom/StorageIPC.h"
#include "mozilla/dom/MessagePortChild.h"
#include "mozilla/dom/ServiceWorkerContainerChild.h"
#include "mozilla/dom/ServiceWorkerManagerChild.h"
#include "mozilla/ipc/PBackgroundTestChild.h"
#include "mozilla/net/PUDPSocketChild.h"
#include "mozilla/dom/network/UDPSocketChild.h"
#include "mozilla/dom/WebAuthnTransactionChild.h"
#include "mozilla/dom/MIDIPortChild.h"
#include "mozilla/dom/MIDIManagerChild.h"
#include "nsID.h"

namespace {

class TestChild final : public mozilla::ipc::PBackgroundTestChild {
  friend class mozilla::ipc::BackgroundChildImpl;

  nsCString mTestArg;

  explicit TestChild(const nsACString& aTestArg) : mTestArg(aTestArg) {
    MOZ_COUNT_CTOR(TestChild);
  }

 protected:
  ~TestChild() override { MOZ_COUNT_DTOR(TestChild); }

 public:
  mozilla::ipc::IPCResult Recv__delete__(const nsACString& aTestArg) override;
};

}  // namespace

namespace mozilla::ipc {

using mozilla::dom::UDPSocketChild;
using mozilla::net::PUDPSocketChild;

using mozilla::dom::PServiceWorkerChild;
using mozilla::dom::PServiceWorkerContainerChild;
using mozilla::dom::PServiceWorkerRegistrationChild;
using mozilla::dom::StorageDBChild;
using mozilla::dom::cache::PCacheChild;
using mozilla::dom::cache::PCacheStreamControlChild;

using mozilla::dom::WebAuthnTransactionChild;

using mozilla::dom::PMIDIManagerChild;
using mozilla::dom::PMIDIPortChild;

// -----------------------------------------------------------------------------
// BackgroundChildImpl::ThreadLocal
// -----------------------------------------------------------------------------

BackgroundChildImpl::ThreadLocal::ThreadLocal() : mCurrentFileHandle(nullptr) {
  // May happen on any thread!
  MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal);
}

BackgroundChildImpl::ThreadLocal::~ThreadLocal() {
  // May happen on any thread!
  MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal);
}

// -----------------------------------------------------------------------------
// BackgroundChildImpl
// -----------------------------------------------------------------------------

BackgroundChildImpl::BackgroundChildImpl() {
  // May happen on any thread!
  MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl);
}

BackgroundChildImpl::~BackgroundChildImpl() {
  // May happen on any thread!
  MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl);
}

void BackgroundChildImpl::ProcessingError(Result aCode, const char* aReason) {
  // May happen on any thread!

  nsAutoCString abortMessage;

  switch (aCode) {
    case MsgDropped:
      return;

#define HANDLE_CASE(_result)              \
  case _result:                           \
    abortMessage.AssignLiteral(#_result); \
    break

      HANDLE_CASE(MsgNotKnown);
      HANDLE_CASE(MsgNotAllowed);
      HANDLE_CASE(MsgPayloadError);
      HANDLE_CASE(MsgProcessingError);
      HANDLE_CASE(MsgRouteError);
      HANDLE_CASE(MsgValueError);

#undef HANDLE_CASE

    default:
      MOZ_CRASH("Unknown error code!");
  }

  MOZ_CRASH_UNSAFE_PRINTF("%s: %s", abortMessage.get(), aReason);
}

void BackgroundChildImpl::ActorDestroy(ActorDestroyReason aWhy) {
  // May happen on any thread!
}

PBackgroundTestChild* BackgroundChildImpl::AllocPBackgroundTestChild(
    const nsACString& aTestArg) {
  return new TestChild(aTestArg);
}

bool BackgroundChildImpl::DeallocPBackgroundTestChild(
    PBackgroundTestChild* aActor) {
  MOZ_ASSERT(aActor);

  delete static_cast<TestChild*>(aActor);
  return true;
}

BackgroundChildImpl::PBackgroundIndexedDBUtilsChild*
BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() {
  MOZ_CRASH(
      "PBackgroundIndexedDBUtilsChild actors should be manually "
      "constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild(
    PBackgroundIndexedDBUtilsChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundSDBConnectionChild*
BackgroundChildImpl::AllocPBackgroundSDBConnectionChild(
    const PersistenceType& aPersistenceType,
    const PrincipalInfo& aPrincipalInfo) {
  MOZ_CRASH(
      "PBackgroundSDBConnectionChild actor should be manually "
      "constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundSDBConnectionChild(
    PBackgroundSDBConnectionChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundLSDatabaseChild*
BackgroundChildImpl::AllocPBackgroundLSDatabaseChild(
    const PrincipalInfo& aPrincipalInfo, const uint32_t& aPrivateBrowsingId,
    const uint64_t& aDatastoreId) {
  MOZ_CRASH("PBackgroundLSDatabaseChild actor should be manually constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundLSDatabaseChild(
    PBackgroundLSDatabaseChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundLSObserverChild*
BackgroundChildImpl::AllocPBackgroundLSObserverChild(
    const uint64_t& aObserverId) {
  MOZ_CRASH("PBackgroundLSObserverChild actor should be manually constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundLSObserverChild(
    PBackgroundLSObserverChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundLSRequestChild*
BackgroundChildImpl::AllocPBackgroundLSRequestChild(
    const LSRequestParams& aParams) {
  MOZ_CRASH("PBackgroundLSRequestChild actor should be manually constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundLSRequestChild(
    PBackgroundLSRequestChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundLocalStorageCacheChild*
BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild(
    const PrincipalInfo& aPrincipalInfo, const nsACString& aOriginKey,
    const uint32_t& aPrivateBrowsingId) {
  MOZ_CRASH(
      "PBackgroundLocalStorageChild actors should be manually "
      "constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild(
    PBackgroundLocalStorageCacheChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundLSSimpleRequestChild*
BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild(
    const LSSimpleRequestParams& aParams) {
  MOZ_CRASH(
      "PBackgroundLSSimpleRequestChild actor should be manually "
      "constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild(
    PBackgroundLSSimpleRequestChild* aActor) {
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

BackgroundChildImpl::PBackgroundStorageChild*
BackgroundChildImpl::AllocPBackgroundStorageChild(
    const nsAString& aProfilePath, const uint32_t& aPrivateBrowsingId) {
  MOZ_CRASH("PBackgroundStorageChild actors should be manually constructed!");
}

bool BackgroundChildImpl::DeallocPBackgroundStorageChild(
    PBackgroundStorageChild* aActor) {
  MOZ_ASSERT(aActor);

  StorageDBChild* child = static_cast<StorageDBChild*>(aActor);
  child->ReleaseIPDLReference();
  return true;
}

dom::PRemoteWorkerChild* BackgroundChildImpl::AllocPRemoteWorkerChild(
    const RemoteWorkerData& aData) {
  RefPtr<dom::RemoteWorkerChild> agent = new dom::RemoteWorkerChild(aData);
  return agent.forget().take();
}

IPCResult BackgroundChildImpl::RecvPRemoteWorkerConstructor(
    PRemoteWorkerChild* aActor, const RemoteWorkerData& aData) {
  dom::RemoteWorkerChild* actor = static_cast<dom::RemoteWorkerChild*>(aActor);
  actor->ExecWorker(aData);
  return IPC_OK();
}

bool BackgroundChildImpl::DeallocPRemoteWorkerChild(
    dom::PRemoteWorkerChild* aActor) {
  RefPtr<dom::RemoteWorkerChild> actor =
      dont_AddRef(static_cast<dom::RemoteWorkerChild*>(aActor));
  return true;
}

dom::PRemoteWorkerControllerChild*
BackgroundChildImpl::AllocPRemoteWorkerControllerChild(
    const dom::RemoteWorkerData& aRemoteWorkerData) {
  MOZ_CRASH(
      "PRemoteWorkerControllerChild actors must be manually constructed!");
  return nullptr;
}

bool BackgroundChildImpl::DeallocPRemoteWorkerControllerChild(
    dom::PRemoteWorkerControllerChild* aActor) {
  MOZ_ASSERT(aActor);

  RefPtr<dom::RemoteWorkerControllerChild> actor =
      dont_AddRef(static_cast<dom::RemoteWorkerControllerChild*>(aActor));
  return true;
}

dom::PRemoteWorkerServiceChild*
BackgroundChildImpl::AllocPRemoteWorkerServiceChild() {
  RefPtr<dom::RemoteWorkerServiceChild> agent =
      new dom::RemoteWorkerServiceChild();
  return agent.forget().take();
}

bool BackgroundChildImpl::DeallocPRemoteWorkerServiceChild(
    dom::PRemoteWorkerServiceChild* aActor) {
  RefPtr<dom::RemoteWorkerServiceChild> actor =
      dont_AddRef(static_cast<dom::RemoteWorkerServiceChild*>(aActor));
  return true;
}

dom::PSharedWorkerChild* BackgroundChildImpl::AllocPSharedWorkerChild(
    const dom::RemoteWorkerData& aData, const uint64_t& aWindowID,
    const dom::MessagePortIdentifier& aPortIdentifier) {
  RefPtr<dom::SharedWorkerChild> agent = new dom::SharedWorkerChild();
  return agent.forget().take();
}

bool BackgroundChildImpl::DeallocPSharedWorkerChild(
    dom::PSharedWorkerChild* aActor) {
  RefPtr<dom::SharedWorkerChild> actor =
      dont_AddRef(static_cast<dom::SharedWorkerChild*>(aActor));
  return true;
}

dom::PTemporaryIPCBlobChild*
BackgroundChildImpl::AllocPTemporaryIPCBlobChild() {
  MOZ_CRASH("This is not supposed to be called.");
  return nullptr;
}

bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild(
    dom::PTemporaryIPCBlobChild* aActor) {
  RefPtr<dom::TemporaryIPCBlobChild> actor =
      dont_AddRef(static_cast<dom::TemporaryIPCBlobChild*>(aActor));
  return true;
}

dom::PFileCreatorChild* BackgroundChildImpl::AllocPFileCreatorChild(
    const nsAString& aFullPath, const nsAString& aType, const nsAString& aName,
    const Maybe<int64_t>& aLastModified, const bool& aExistenceCheck,
    const bool& aIsFromNsIFile) {
  return new dom::FileCreatorChild();
}

bool BackgroundChildImpl::DeallocPFileCreatorChild(PFileCreatorChild* aActor) {
  delete static_cast<dom::FileCreatorChild*>(aActor);
  return true;
}

PUDPSocketChild* BackgroundChildImpl::AllocPUDPSocketChild(
    const Maybe<PrincipalInfo>& aPrincipalInfo, const nsACString& aFilter) {
  MOZ_CRASH("AllocPUDPSocket should not be called");
  return nullptr;
}

bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild* child) {
  UDPSocketChild* p = static_cast<UDPSocketChild*>(child);
  p->ReleaseIPDLReference();
  return true;
}

// -----------------------------------------------------------------------------
// BroadcastChannel API
// -----------------------------------------------------------------------------

dom::PBroadcastChannelChild* BackgroundChildImpl::AllocPBroadcastChannelChild(
    const PrincipalInfo& aPrincipalInfo, const nsACString& aOrigin,
    const nsAString& aChannel) {
  RefPtr<dom::BroadcastChannelChild> agent = new dom::BroadcastChannelChild();
  return agent.forget().take();
}

bool BackgroundChildImpl::DeallocPBroadcastChannelChild(
    PBroadcastChannelChild* aActor) {
  RefPtr<dom::BroadcastChannelChild> child =
      dont_AddRef(static_cast<dom::BroadcastChannelChild*>(aActor));
  MOZ_ASSERT(child);
  return true;
}

camera::PCamerasChild* BackgroundChildImpl::AllocPCamerasChild() {
#ifdef MOZ_WEBRTC
  RefPtr<camera::CamerasChild> agent = new camera::CamerasChild();
  return agent.forget().take();
#else
  return nullptr;
#endif
}

bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild* aActor) {
#ifdef MOZ_WEBRTC
  RefPtr<camera::CamerasChild> child =
      dont_AddRef(static_cast<camera::CamerasChild*>(aActor));
  MOZ_ASSERT(aActor);
  camera::Shutdown();
#endif
  return true;
}

// -----------------------------------------------------------------------------
// ServiceWorkerManager
// -----------------------------------------------------------------------------

dom::PServiceWorkerManagerChild*
BackgroundChildImpl::AllocPServiceWorkerManagerChild() {
  RefPtr<dom::ServiceWorkerManagerChild> agent =
      new dom::ServiceWorkerManagerChild();
  return agent.forget().take();
}

bool BackgroundChildImpl::DeallocPServiceWorkerManagerChild(
    PServiceWorkerManagerChild* aActor) {
  RefPtr<dom::ServiceWorkerManagerChild> child =
      dont_AddRef(static_cast<dom::ServiceWorkerManagerChild*>(aActor));
  MOZ_ASSERT(child);
  return true;
}

// -----------------------------------------------------------------------------
// Cache API
// -----------------------------------------------------------------------------

already_AddRefed<PCacheChild> BackgroundChildImpl::AllocPCacheChild() {
  return dom::cache::AllocPCacheChild();
}

already_AddRefed<PCacheStreamControlChild>
BackgroundChildImpl::AllocPCacheStreamControlChild() {
  return dom::cache::AllocPCacheStreamControlChild();
}

// -----------------------------------------------------------------------------
// MessageChannel/MessagePort API
// -----------------------------------------------------------------------------

dom::PMessagePortChild* BackgroundChildImpl::AllocPMessagePortChild(
    const nsID& aUUID, const nsID& aDestinationUUID,
    const uint32_t& aSequenceID) {
  RefPtr<dom::MessagePortChild> agent = new dom::MessagePortChild();
  return agent.forget().take();
}

bool BackgroundChildImpl::DeallocPMessagePortChild(PMessagePortChild* aActor) {
  RefPtr<dom::MessagePortChild> child =
      dont_AddRef(static_cast<dom::MessagePortChild*>(aActor));
  MOZ_ASSERT(child);
  return true;
}

BackgroundChildImpl::PQuotaChild* BackgroundChildImpl::AllocPQuotaChild() {
  MOZ_CRASH("PQuotaChild actor should be manually constructed!");
}

bool BackgroundChildImpl::DeallocPQuotaChild(PQuotaChild* aActor) {
  MOZ_ASSERT(aActor);
  delete aActor;
  return true;
}

mozilla::dom::PClientManagerChild*
BackgroundChildImpl::AllocPClientManagerChild() {
  return mozilla::dom::AllocClientManagerChild();
}

bool BackgroundChildImpl::DeallocPClientManagerChild(
    mozilla::dom::PClientManagerChild* aActor) {
  return mozilla::dom::DeallocClientManagerChild(aActor);
}

#ifdef EARLY_BETA_OR_EARLIER
void BackgroundChildImpl::OnChannelReceivedMessage(const Message& aMsg) {
  if (aMsg.type() == dom::PVsync::MessageType::Msg_Notify__ID) {
    // Not really necessary to look at the message payload, it will be
    // <0.5ms away from TimeStamp::Now()
    SchedulerGroup::MarkVsyncReceived();
  }
}
#endif

dom::PWebAuthnTransactionChild*
BackgroundChildImpl::AllocPWebAuthnTransactionChild() {
  MOZ_CRASH("PWebAuthnTransaction actor should be manually constructed!");
  return nullptr;
}

bool BackgroundChildImpl::DeallocPWebAuthnTransactionChild(
    PWebAuthnTransactionChild* aActor) {
  MOZ_ASSERT(aActor);
  RefPtr<dom::WebAuthnTransactionChild> child =
      dont_AddRef(static_cast<dom::WebAuthnTransactionChild*>(aActor));
  return true;
}

already_AddRefed<PServiceWorkerChild>
BackgroundChildImpl::AllocPServiceWorkerChild(
    const IPCServiceWorkerDescriptor&) {
  MOZ_CRASH("Shouldn't be called.");
  return {};
}

already_AddRefed<PServiceWorkerContainerChild>
BackgroundChildImpl::AllocPServiceWorkerContainerChild() {
  return mozilla::dom::ServiceWorkerContainerChild::Create();
}

already_AddRefed<PServiceWorkerRegistrationChild>
BackgroundChildImpl::AllocPServiceWorkerRegistrationChild(
    const IPCServiceWorkerRegistrationDescriptor&) {
  MOZ_CRASH("Shouldn't be called.");
  return {};
}

dom::PEndpointForReportChild* BackgroundChildImpl::AllocPEndpointForReportChild(
    const nsAString& aGroupName, const PrincipalInfo& aPrincipalInfo) {
  return new dom::EndpointForReportChild();
}

bool BackgroundChildImpl::DeallocPEndpointForReportChild(
    PEndpointForReportChild* aActor) {
  MOZ_ASSERT(aActor);
  delete static_cast<dom::EndpointForReportChild*>(aActor);
  return true;
}

dom::PMediaTransportChild* BackgroundChildImpl::AllocPMediaTransportChild() {
  // We don't allocate here: MediaTransportHandlerIPC is in charge of that,
  // so we don't need to know the implementation particulars here.
  MOZ_ASSERT_UNREACHABLE(
      "The only thing that ought to be creating a PMediaTransportChild is "
      "MediaTransportHandlerIPC!");
  return nullptr;
}

bool BackgroundChildImpl::DeallocPMediaTransportChild(
    dom::PMediaTransportChild* aActor) {
  delete aActor;
  return true;
}

}  // namespace mozilla::ipc

mozilla::ipc::IPCResult TestChild::Recv__delete__(const nsACString& aTestArg) {
  MOZ_RELEASE_ASSERT(aTestArg == mTestArg,
                     "BackgroundTest message was corrupted!");

  return IPC_OK();
}