JSWindowActorParent.cpp (3401B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 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 "mozilla/dom/JSWindowActorParent.h" 8 9 #include "mozilla/dom/BrowserParent.h" 10 #include "mozilla/dom/ContentParent.h" 11 #include "mozilla/dom/JSIPCValue.h" 12 #include "mozilla/dom/JSIPCValueUtils.h" 13 #include "mozilla/dom/JSWindowActorBinding.h" 14 #include "mozilla/dom/MessageManagerBinding.h" 15 #include "mozilla/dom/WindowGlobalChild.h" 16 #include "mozilla/dom/WindowGlobalParent.h" 17 18 namespace mozilla::dom { 19 20 JSWindowActorParent::~JSWindowActorParent() { MOZ_ASSERT(!mManager); } 21 22 JSObject* JSWindowActorParent::WrapObject(JSContext* aCx, 23 JS::Handle<JSObject*> aGivenProto) { 24 return JSWindowActorParent_Binding::Wrap(aCx, this, aGivenProto); 25 } 26 27 WindowGlobalParent* JSWindowActorParent::GetManager() const { return mManager; } 28 29 WindowContext* JSWindowActorParent::GetWindowContext() const { 30 return mManager; 31 } 32 33 void JSWindowActorParent::Init(const nsACString& aName, 34 WindowGlobalParent* aManager) { 35 MOZ_ASSERT(!mManager, "Cannot Init() a JSWindowActorParent twice!"); 36 mManager = aManager; 37 JSActor::Init(aName, /* aSendTyped= */ false); 38 } 39 40 void JSWindowActorParent::SendRawMessage( 41 const JSActorMessageMeta& aMeta, JSIPCValue&& aData, 42 UniquePtr<ipc::StructuredCloneData> aStack, ErrorResult& aRv) { 43 if (NS_WARN_IF(!CanSend() || !mManager || !mManager->CanSend())) { 44 aRv.ThrowInvalidStateError("JSWindowActorParent cannot send at the moment"); 45 return; 46 } 47 48 if (mManager->IsInProcess()) { 49 SendRawMessageInProcess( 50 aMeta, std::move(aData), std::move(aStack), 51 [manager{mManager}]() { return manager->GetChildActor(); }); 52 return; 53 } 54 55 JSIPCValueUtils::SCDHolder holder; 56 if (NS_WARN_IF(!JSIPCValueUtils::PrepareForSending(holder, aData))) { 57 aRv.ThrowDataCloneError( 58 nsPrintfCString("JSWindowActorParent serialization error: cannot " 59 "clone, in actor '%s'", 60 PromiseFlatCString(aMeta.actorName()).get())); 61 return; 62 } 63 64 UniquePtr<ClonedMessageData> stackData; 65 if (aStack) { 66 stackData = MakeUnique<ClonedMessageData>(); 67 if (!aStack->BuildClonedMessageData(*stackData)) { 68 stackData.reset(); 69 } 70 } 71 72 if (NS_WARN_IF(!mManager->SendRawMessage(aMeta, aData, stackData))) { 73 aRv.ThrowOperationError( 74 nsPrintfCString("JSWindowActorParent send error in actor '%s'", 75 PromiseFlatCString(aMeta.actorName()).get())); 76 return; 77 } 78 } 79 80 CanonicalBrowsingContext* JSWindowActorParent::GetBrowsingContext( 81 ErrorResult& aRv) { 82 if (!mManager) { 83 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); 84 return nullptr; 85 } 86 87 return mManager->BrowsingContext(); 88 } 89 90 void JSWindowActorParent::ClearManager() { mManager = nullptr; } 91 92 NS_IMPL_CYCLE_COLLECTION_INHERITED(JSWindowActorParent, JSActor, mManager) 93 94 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorParent) 95 NS_INTERFACE_MAP_END_INHERITING(JSActor) 96 97 NS_IMPL_ADDREF_INHERITED(JSWindowActorParent, JSActor) 98 NS_IMPL_RELEASE_INHERITED(JSWindowActorParent, JSActor) 99 100 } // namespace mozilla::dom