RemoteWorkerServiceParent.cpp (2658B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "RemoteWorkerServiceParent.h" 8 9 #include "RemoteWorkerManager.h" 10 #include "RemoteWorkerParent.h" 11 #include "mozilla/dom/RemoteWorkerTypes.h" 12 #include "mozilla/ipc/BackgroundParent.h" 13 14 namespace mozilla { 15 16 using namespace ipc; 17 18 namespace dom { 19 20 RemoteWorkerServiceParent::RemoteWorkerServiceParent( 21 ThreadsafeContentParentHandle* aProcess) 22 : mProcess(aProcess) {} 23 24 RemoteWorkerServiceParent::~RemoteWorkerServiceParent() { 25 MOZ_ASSERT(!mManager, 26 "ActorDestroy not called before ~RemoteWorkerServiceParent"); 27 } 28 29 RefPtr<RemoteWorkerServiceParent> RemoteWorkerServiceParent::CreateForProcess( 30 ContentParent* aProcess, Endpoint<PRemoteWorkerServiceChild>* aChildEp) { 31 AssertIsOnMainThread(); 32 33 nsCOMPtr<nsISerialEventTarget> backgroundThread = 34 BackgroundParent::GetBackgroundThread(); 35 NS_ENSURE_TRUE(backgroundThread, nullptr); 36 37 Endpoint<PRemoteWorkerServiceParent> parentEp; 38 nsresult rv = PRemoteWorkerService::CreateEndpoints( 39 EndpointProcInfo::Current(), 40 aProcess ? aProcess->OtherEndpointProcInfo() 41 : EndpointProcInfo::Current(), 42 &parentEp, aChildEp); 43 NS_ENSURE_SUCCESS(rv, nullptr); 44 45 RefPtr<RemoteWorkerServiceParent> actor = new RemoteWorkerServiceParent( 46 aProcess ? aProcess->ThreadsafeHandle() : nullptr); 47 rv = backgroundThread->Dispatch( 48 NS_NewRunnableFunction("RemoteWorkerServiceParent::CreateForProcess", 49 [actor, parentEp = std::move(parentEp)]() mutable { 50 actor->InitializeOnThread(std::move(parentEp)); 51 })); 52 NS_ENSURE_SUCCESS(rv, nullptr); 53 54 return actor; 55 } 56 57 void RemoteWorkerServiceParent::InitializeOnThread( 58 Endpoint<PRemoteWorkerServiceParent> aEndpoint) { 59 AssertIsOnBackgroundThread(); 60 if (NS_WARN_IF(!aEndpoint.Bind(this))) { 61 return; 62 } 63 mManager = RemoteWorkerManager::GetOrCreate(); 64 mManager->RegisterActor(this); 65 } 66 67 void RemoteWorkerServiceParent::ActorDestroy(IProtocol::ActorDestroyReason) { 68 AssertIsOnBackgroundThread(); 69 if (mManager) { 70 mManager->UnregisterActor(this); 71 mManager = nullptr; 72 } 73 } 74 75 nsCString RemoteWorkerServiceParent::GetRemoteType() const { 76 if (mProcess) { 77 return mProcess->GetRemoteType(); 78 } 79 return NOT_REMOTE_TYPE; 80 } 81 82 } // namespace dom 83 } // namespace mozilla