BackgroundChildImpl.cpp (14240B)
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 "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/CookieStoreChild.h" 23 #include "mozilla/dom/PFileSystemRequestChild.h" 24 #include "mozilla/dom/EndpointForReportChild.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/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/MIDIPortChild.h" 43 #include "mozilla/dom/MIDIManagerChild.h" 44 #include "nsID.h" 45 46 namespace { 47 48 class TestChild final : public mozilla::ipc::PBackgroundTestChild { 49 friend class mozilla::ipc::BackgroundChildImpl; 50 51 nsCString mTestArg; 52 53 explicit TestChild(const nsACString& aTestArg) : mTestArg(aTestArg) { 54 MOZ_COUNT_CTOR(TestChild); 55 } 56 57 protected: 58 ~TestChild() override { MOZ_COUNT_DTOR(TestChild); } 59 60 public: 61 mozilla::ipc::IPCResult Recv__delete__(const nsACString& aTestArg) override; 62 }; 63 64 } // namespace 65 66 namespace mozilla::ipc { 67 68 using mozilla::dom::UDPSocketChild; 69 using mozilla::net::PUDPSocketChild; 70 71 using mozilla::dom::PServiceWorkerChild; 72 using mozilla::dom::PServiceWorkerContainerChild; 73 using mozilla::dom::PServiceWorkerRegistrationChild; 74 using mozilla::dom::StorageDBChild; 75 using mozilla::dom::cache::PCacheChild; 76 using mozilla::dom::cache::PCacheStreamControlChild; 77 78 using mozilla::dom::PMIDIManagerChild; 79 using mozilla::dom::PMIDIPortChild; 80 81 // ----------------------------------------------------------------------------- 82 // BackgroundChildImpl::ThreadLocal 83 // ----------------------------------------------------------------------------- 84 85 BackgroundChildImpl::ThreadLocal::ThreadLocal() : mCurrentFileHandle(nullptr) { 86 // May happen on any thread! 87 MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal); 88 } 89 90 BackgroundChildImpl::ThreadLocal::~ThreadLocal() { 91 // May happen on any thread! 92 MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl::ThreadLocal); 93 } 94 95 // ----------------------------------------------------------------------------- 96 // BackgroundChildImpl 97 // ----------------------------------------------------------------------------- 98 99 BackgroundChildImpl::BackgroundChildImpl() { 100 // May happen on any thread! 101 MOZ_COUNT_CTOR(mozilla::ipc::BackgroundChildImpl); 102 } 103 104 BackgroundChildImpl::~BackgroundChildImpl() { 105 // May happen on any thread! 106 MOZ_COUNT_DTOR(mozilla::ipc::BackgroundChildImpl); 107 } 108 109 void BackgroundChildImpl::ProcessingError(Result aCode, const char* aReason) { 110 // May happen on any thread! 111 112 nsAutoCString abortMessage; 113 114 switch (aCode) { 115 case MsgDropped: 116 return; 117 118 #define HANDLE_CASE(_result) \ 119 case _result: \ 120 abortMessage.AssignLiteral(#_result); \ 121 break 122 123 HANDLE_CASE(MsgNotKnown); 124 HANDLE_CASE(MsgNotAllowed); 125 HANDLE_CASE(MsgPayloadError); 126 HANDLE_CASE(MsgProcessingError); 127 HANDLE_CASE(MsgValueError); 128 129 #undef HANDLE_CASE 130 131 default: 132 MOZ_CRASH("Unknown error code!"); 133 } 134 135 CrashReporter::RecordAnnotationCString( 136 CrashReporter::Annotation::ipc_channel_error, aReason); 137 138 MOZ_CRASH_UNSAFE_PRINTF("%s: %s", abortMessage.get(), aReason); 139 } 140 141 void BackgroundChildImpl::ActorDestroy(ActorDestroyReason aWhy) { 142 // May happen on any thread! 143 } 144 145 PBackgroundTestChild* BackgroundChildImpl::AllocPBackgroundTestChild( 146 const nsACString& aTestArg) { 147 return new TestChild(aTestArg); 148 } 149 150 bool BackgroundChildImpl::DeallocPBackgroundTestChild( 151 PBackgroundTestChild* aActor) { 152 MOZ_ASSERT(aActor); 153 154 delete static_cast<TestChild*>(aActor); 155 return true; 156 } 157 158 BackgroundChildImpl::PBackgroundIndexedDBUtilsChild* 159 BackgroundChildImpl::AllocPBackgroundIndexedDBUtilsChild() { 160 MOZ_CRASH( 161 "PBackgroundIndexedDBUtilsChild actors should be manually " 162 "constructed!"); 163 } 164 165 bool BackgroundChildImpl::DeallocPBackgroundIndexedDBUtilsChild( 166 PBackgroundIndexedDBUtilsChild* aActor) { 167 MOZ_ASSERT(aActor); 168 169 delete aActor; 170 return true; 171 } 172 173 BackgroundChildImpl::PBackgroundLSObserverChild* 174 BackgroundChildImpl::AllocPBackgroundLSObserverChild( 175 const uint64_t& aObserverId) { 176 MOZ_CRASH("PBackgroundLSObserverChild actor should be manually constructed!"); 177 } 178 179 bool BackgroundChildImpl::DeallocPBackgroundLSObserverChild( 180 PBackgroundLSObserverChild* aActor) { 181 MOZ_ASSERT(aActor); 182 183 delete aActor; 184 return true; 185 } 186 187 BackgroundChildImpl::PBackgroundLSRequestChild* 188 BackgroundChildImpl::AllocPBackgroundLSRequestChild( 189 const LSRequestParams& aParams) { 190 MOZ_CRASH("PBackgroundLSRequestChild actor should be manually constructed!"); 191 } 192 193 bool BackgroundChildImpl::DeallocPBackgroundLSRequestChild( 194 PBackgroundLSRequestChild* aActor) { 195 MOZ_ASSERT(aActor); 196 197 delete aActor; 198 return true; 199 } 200 201 BackgroundChildImpl::PBackgroundLocalStorageCacheChild* 202 BackgroundChildImpl::AllocPBackgroundLocalStorageCacheChild( 203 const PrincipalInfo& aPrincipalInfo, const nsACString& aOriginKey, 204 const uint32_t& aPrivateBrowsingId) { 205 MOZ_CRASH( 206 "PBackgroundLocalStorageChild actors should be manually " 207 "constructed!"); 208 } 209 210 bool BackgroundChildImpl::DeallocPBackgroundLocalStorageCacheChild( 211 PBackgroundLocalStorageCacheChild* aActor) { 212 MOZ_ASSERT(aActor); 213 214 delete aActor; 215 return true; 216 } 217 218 BackgroundChildImpl::PBackgroundLSSimpleRequestChild* 219 BackgroundChildImpl::AllocPBackgroundLSSimpleRequestChild( 220 const LSSimpleRequestParams& aParams) { 221 MOZ_CRASH( 222 "PBackgroundLSSimpleRequestChild actor should be manually " 223 "constructed!"); 224 } 225 226 bool BackgroundChildImpl::DeallocPBackgroundLSSimpleRequestChild( 227 PBackgroundLSSimpleRequestChild* aActor) { 228 MOZ_ASSERT(aActor); 229 230 delete aActor; 231 return true; 232 } 233 234 BackgroundChildImpl::PBackgroundStorageChild* 235 BackgroundChildImpl::AllocPBackgroundStorageChild( 236 const nsAString& aProfilePath, const uint32_t& aPrivateBrowsingId) { 237 MOZ_CRASH("PBackgroundStorageChild actors should be manually constructed!"); 238 } 239 240 bool BackgroundChildImpl::DeallocPBackgroundStorageChild( 241 PBackgroundStorageChild* aActor) { 242 MOZ_ASSERT(aActor); 243 244 StorageDBChild* child = static_cast<StorageDBChild*>(aActor); 245 child->ReleaseIPDLReference(); 246 return true; 247 } 248 249 dom::PSharedWorkerChild* BackgroundChildImpl::AllocPSharedWorkerChild( 250 const dom::RemoteWorkerData& aData, const uint64_t& aWindowID, 251 const dom::MessagePortIdentifier& aPortIdentifier) { 252 RefPtr<dom::SharedWorkerChild> agent = new dom::SharedWorkerChild(); 253 return agent.forget().take(); 254 } 255 256 bool BackgroundChildImpl::DeallocPSharedWorkerChild( 257 dom::PSharedWorkerChild* aActor) { 258 RefPtr<dom::SharedWorkerChild> actor = 259 dont_AddRef(static_cast<dom::SharedWorkerChild*>(aActor)); 260 return true; 261 } 262 263 dom::PTemporaryIPCBlobChild* 264 BackgroundChildImpl::AllocPTemporaryIPCBlobChild() { 265 MOZ_CRASH("This is not supposed to be called."); 266 return nullptr; 267 } 268 269 bool BackgroundChildImpl::DeallocPTemporaryIPCBlobChild( 270 dom::PTemporaryIPCBlobChild* aActor) { 271 RefPtr<dom::TemporaryIPCBlobChild> actor = 272 dont_AddRef(static_cast<dom::TemporaryIPCBlobChild*>(aActor)); 273 return true; 274 } 275 276 dom::PFileCreatorChild* BackgroundChildImpl::AllocPFileCreatorChild( 277 const nsAString& aFullPath, const nsAString& aType, const nsAString& aName, 278 const Maybe<int64_t>& aLastModified, const bool& aExistenceCheck, 279 const bool& aIsFromNsIFile) { 280 return new dom::FileCreatorChild(); 281 } 282 283 bool BackgroundChildImpl::DeallocPFileCreatorChild(PFileCreatorChild* aActor) { 284 delete static_cast<dom::FileCreatorChild*>(aActor); 285 return true; 286 } 287 288 PUDPSocketChild* BackgroundChildImpl::AllocPUDPSocketChild( 289 const Maybe<PrincipalInfo>& aPrincipalInfo, const nsACString& aFilter) { 290 MOZ_CRASH("AllocPUDPSocket should not be called"); 291 return nullptr; 292 } 293 294 bool BackgroundChildImpl::DeallocPUDPSocketChild(PUDPSocketChild* child) { 295 UDPSocketChild* p = static_cast<UDPSocketChild*>(child); 296 p->ReleaseIPDLReference(); 297 return true; 298 } 299 300 // ----------------------------------------------------------------------------- 301 // BroadcastChannel API 302 // ----------------------------------------------------------------------------- 303 304 dom::PBroadcastChannelChild* BackgroundChildImpl::AllocPBroadcastChannelChild( 305 const PrincipalInfo& aPrincipalInfo, const nsACString& aOrigin, 306 const nsAString& aChannel) { 307 RefPtr<dom::BroadcastChannelChild> agent = new dom::BroadcastChannelChild(); 308 return agent.forget().take(); 309 } 310 311 bool BackgroundChildImpl::DeallocPBroadcastChannelChild( 312 PBroadcastChannelChild* aActor) { 313 RefPtr<dom::BroadcastChannelChild> child = 314 dont_AddRef(static_cast<dom::BroadcastChannelChild*>(aActor)); 315 MOZ_ASSERT(child); 316 return true; 317 } 318 319 // ----------------------------------------------------------------------------- 320 // CookieStore API 321 // ----------------------------------------------------------------------------- 322 323 dom::PCookieStoreChild* BackgroundChildImpl::AllocPCookieStoreChild() { 324 RefPtr<dom::CookieStoreChild> child = new dom::CookieStoreChild(); 325 return child.forget().take(); 326 } 327 328 bool BackgroundChildImpl::DeallocPCookieStoreChild(PCookieStoreChild* aActor) { 329 RefPtr<dom::CookieStoreChild> child = 330 dont_AddRef(static_cast<dom::CookieStoreChild*>(aActor)); 331 MOZ_ASSERT(child); 332 return true; 333 } 334 335 // ----------------------------------------------------------------------------- 336 // Camera API 337 // ----------------------------------------------------------------------------- 338 339 camera::PCamerasChild* BackgroundChildImpl::AllocPCamerasChild() { 340 #ifdef MOZ_WEBRTC 341 RefPtr<camera::CamerasChild> agent = new camera::CamerasChild(); 342 return agent.forget().take(); 343 #else 344 return nullptr; 345 #endif 346 } 347 348 bool BackgroundChildImpl::DeallocPCamerasChild(camera::PCamerasChild* aActor) { 349 #ifdef MOZ_WEBRTC 350 RefPtr<camera::CamerasChild> child = 351 dont_AddRef(static_cast<camera::CamerasChild*>(aActor)); 352 MOZ_ASSERT(aActor); 353 camera::Shutdown(); 354 #endif 355 return true; 356 } 357 358 // ----------------------------------------------------------------------------- 359 // ServiceWorkerManager 360 // ----------------------------------------------------------------------------- 361 362 dom::PServiceWorkerManagerChild* 363 BackgroundChildImpl::AllocPServiceWorkerManagerChild() { 364 RefPtr<dom::ServiceWorkerManagerChild> agent = 365 new dom::ServiceWorkerManagerChild(); 366 return agent.forget().take(); 367 } 368 369 bool BackgroundChildImpl::DeallocPServiceWorkerManagerChild( 370 PServiceWorkerManagerChild* aActor) { 371 RefPtr<dom::ServiceWorkerManagerChild> child = 372 dont_AddRef(static_cast<dom::ServiceWorkerManagerChild*>(aActor)); 373 MOZ_ASSERT(child); 374 return true; 375 } 376 377 // ----------------------------------------------------------------------------- 378 // Cache API 379 // ----------------------------------------------------------------------------- 380 381 already_AddRefed<PCacheChild> BackgroundChildImpl::AllocPCacheChild() { 382 return dom::cache::AllocPCacheChild(); 383 } 384 385 already_AddRefed<PCacheStreamControlChild> 386 BackgroundChildImpl::AllocPCacheStreamControlChild() { 387 return dom::cache::AllocPCacheStreamControlChild(); 388 } 389 390 // ----------------------------------------------------------------------------- 391 // MessageChannel/MessagePort API 392 // ----------------------------------------------------------------------------- 393 394 dom::PMessagePortChild* BackgroundChildImpl::AllocPMessagePortChild( 395 const nsID& aUUID, const nsID& aDestinationUUID, 396 const uint32_t& aSequenceID) { 397 RefPtr<dom::MessagePortChild> agent = new dom::MessagePortChild(); 398 return agent.forget().take(); 399 } 400 401 bool BackgroundChildImpl::DeallocPMessagePortChild(PMessagePortChild* aActor) { 402 RefPtr<dom::MessagePortChild> child = 403 dont_AddRef(static_cast<dom::MessagePortChild*>(aActor)); 404 MOZ_ASSERT(child); 405 return true; 406 } 407 408 already_AddRefed<PServiceWorkerChild> 409 BackgroundChildImpl::AllocPServiceWorkerChild( 410 const IPCServiceWorkerDescriptor&) { 411 MOZ_CRASH("Shouldn't be called."); 412 return {}; 413 } 414 415 already_AddRefed<PServiceWorkerContainerChild> 416 BackgroundChildImpl::AllocPServiceWorkerContainerChild() { 417 return mozilla::dom::ServiceWorkerContainerChild::Create(); 418 } 419 420 already_AddRefed<PServiceWorkerRegistrationChild> 421 BackgroundChildImpl::AllocPServiceWorkerRegistrationChild( 422 const IPCServiceWorkerRegistrationDescriptor&) { 423 MOZ_CRASH("Shouldn't be called."); 424 return {}; 425 } 426 427 dom::PEndpointForReportChild* BackgroundChildImpl::AllocPEndpointForReportChild( 428 const nsAString& aGroupName, const PrincipalInfo& aPrincipalInfo) { 429 return new dom::EndpointForReportChild(); 430 } 431 432 bool BackgroundChildImpl::DeallocPEndpointForReportChild( 433 PEndpointForReportChild* aActor) { 434 MOZ_ASSERT(aActor); 435 delete static_cast<dom::EndpointForReportChild*>(aActor); 436 return true; 437 } 438 439 } // namespace mozilla::ipc 440 441 mozilla::ipc::IPCResult TestChild::Recv__delete__(const nsACString& aTestArg) { 442 MOZ_RELEASE_ASSERT(aTestArg == mTestArg, 443 "BackgroundTest message was corrupted!"); 444 445 return IPC_OK(); 446 }