ClientManagerOpParent.cpp (3051B)
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 "ClientManagerOpParent.h" 8 9 #include "ClientManagerService.h" 10 #include "mozilla/dom/PClientManagerParent.h" 11 #include "mozilla/ipc/BackgroundParent.h" 12 13 namespace mozilla::dom { 14 15 using mozilla::ipc::BackgroundParent; 16 17 template <typename Method, typename... Args> 18 void ClientManagerOpParent::DoServiceOp(Method aMethod, Args&&... aArgs) { 19 ThreadsafeContentParentHandle* originContent = 20 BackgroundParent::GetContentParentHandle(Manager()->Manager()); 21 22 // Note, we need perfect forarding of the template type in order 23 // to allow already_AddRefed<> to be passed as an arg. 24 RefPtr<ClientOpPromise> p = 25 (mService->*aMethod)(originContent, std::forward<Args>(aArgs)...); 26 27 // Capturing `this` is safe here because we disconnect the promise in 28 // ActorDestroy() which ensures neither lambda is called if the actor 29 // is destroyed before the source operation completes. 30 p->Then( 31 GetCurrentSerialEventTarget(), __func__, 32 [this](const mozilla::dom::ClientOpResult& aResult) { 33 mPromiseRequestHolder.Complete(); 34 (void)PClientManagerOpParent::Send__delete__(this, aResult); 35 }, 36 [this](const CopyableErrorResult& aRv) { 37 mPromiseRequestHolder.Complete(); 38 (void)PClientManagerOpParent::Send__delete__(this, aRv); 39 }) 40 ->Track(mPromiseRequestHolder); 41 } 42 43 void ClientManagerOpParent::ActorDestroy(ActorDestroyReason aReason) { 44 mPromiseRequestHolder.DisconnectIfExists(); 45 } 46 47 ClientManagerOpParent::ClientManagerOpParent(ClientManagerService* aService) 48 : mService(aService) { 49 MOZ_DIAGNOSTIC_ASSERT(mService); 50 } 51 52 void ClientManagerOpParent::Init(const ClientOpConstructorArgs& aArgs) { 53 switch (aArgs.type()) { 54 case ClientOpConstructorArgs::TClientNavigateArgs: { 55 DoServiceOp(&ClientManagerService::Navigate, 56 aArgs.get_ClientNavigateArgs()); 57 break; 58 } 59 case ClientOpConstructorArgs::TClientMatchAllArgs: { 60 DoServiceOp(&ClientManagerService::MatchAll, 61 aArgs.get_ClientMatchAllArgs()); 62 break; 63 } 64 case ClientOpConstructorArgs::TClientClaimArgs: { 65 DoServiceOp(&ClientManagerService::Claim, aArgs.get_ClientClaimArgs()); 66 break; 67 } 68 case ClientOpConstructorArgs::TClientGetInfoAndStateArgs: { 69 DoServiceOp(&ClientManagerService::GetInfoAndState, 70 aArgs.get_ClientGetInfoAndStateArgs()); 71 break; 72 } 73 case ClientOpConstructorArgs::TClientOpenWindowArgs: { 74 DoServiceOp(&ClientManagerService::OpenWindow, 75 aArgs.get_ClientOpenWindowArgs()); 76 break; 77 } 78 default: { 79 MOZ_ASSERT_UNREACHABLE("Unknown Client operation!"); 80 break; 81 } 82 } 83 } 84 85 } // namespace mozilla::dom