2019-01-28 22:02:02 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "mozilla/dom/JSWindowActorBinding.h"
|
|
|
|
#include "mozilla/dom/JSWindowActorChild.h"
|
|
|
|
#include "mozilla/dom/WindowGlobalChild.h"
|
2019-02-08 16:02:08 +03:00
|
|
|
#include "mozilla/dom/WindowGlobalParent.h"
|
|
|
|
#include "mozilla/dom/MessageManagerBinding.h"
|
2019-04-17 21:18:33 +03:00
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
|
|
|
#include "nsGlobalWindowInner.h"
|
2019-01-28 22:02:02 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-05-10 18:01:33 +03:00
|
|
|
JSWindowActorChild::~JSWindowActorChild() { MOZ_ASSERT(!mManager); }
|
|
|
|
|
2019-01-28 22:02:02 +03:00
|
|
|
JSObject* JSWindowActorChild::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return JSWindowActorChild_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowGlobalChild* JSWindowActorChild::Manager() const { return mManager; }
|
|
|
|
|
2019-02-21 22:53:59 +03:00
|
|
|
void JSWindowActorChild::Init(const nsAString& aName,
|
|
|
|
WindowGlobalChild* aManager) {
|
2019-01-28 22:02:02 +03:00
|
|
|
MOZ_ASSERT(!mManager, "Cannot Init() a JSWindowActorChild twice!");
|
2019-04-18 22:37:15 +03:00
|
|
|
SetName(aName);
|
2019-01-28 22:02:02 +03:00
|
|
|
mManager = aManager;
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:02:08 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AsyncMessageToParent : public Runnable {
|
|
|
|
public:
|
2019-04-18 22:37:15 +03:00
|
|
|
AsyncMessageToParent(const JSWindowActorMessageMeta& aMetadata,
|
2019-02-08 16:02:08 +03:00
|
|
|
ipc::StructuredCloneData&& aData,
|
|
|
|
WindowGlobalParent* aParent)
|
|
|
|
: mozilla::Runnable("WindowGlobalParent::HandleAsyncMessage"),
|
2019-04-18 22:37:15 +03:00
|
|
|
mMetadata(aMetadata),
|
2019-02-08 16:02:08 +03:00
|
|
|
mData(std::move(aData)),
|
|
|
|
mParent(aParent) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be called on the main thread.");
|
2019-04-18 22:37:15 +03:00
|
|
|
mParent->ReceiveRawMessage(mMetadata, std::move(mData));
|
2019-02-08 16:02:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-04-18 22:37:15 +03:00
|
|
|
JSWindowActorMessageMeta mMetadata;
|
2019-02-08 16:02:08 +03:00
|
|
|
ipc::StructuredCloneData mData;
|
|
|
|
RefPtr<WindowGlobalParent> mParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
void JSWindowActorChild::SendRawMessage(const JSWindowActorMessageMeta& aMeta,
|
|
|
|
ipc::StructuredCloneData&& aData,
|
|
|
|
ErrorResult& aRv) {
|
2019-05-10 18:01:33 +03:00
|
|
|
if (NS_WARN_IF(!mCanSend || !mManager || mManager->IsClosed())) {
|
2019-02-08 16:02:08 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mManager->IsInProcess()) {
|
2019-04-18 22:37:15 +03:00
|
|
|
RefPtr<WindowGlobalParent> wgp = mManager->GetParentActor();
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new AsyncMessageToParent(aMeta, std::move(aData), wgp);
|
|
|
|
NS_DispatchToMainThread(runnable.forget());
|
2019-02-08 16:02:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
// Cross-process case - send data over WindowGlobalChild to other side.
|
2019-02-08 16:02:08 +03:00
|
|
|
ClonedMessageData msgData;
|
2019-02-25 23:04:49 +03:00
|
|
|
ContentChild* cc = ContentChild::GetSingleton();
|
2019-04-18 22:37:15 +03:00
|
|
|
if (NS_WARN_IF(!aData.BuildClonedMessageDataForChild(cc, msgData))) {
|
2019-02-08 16:02:08 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
if (NS_WARN_IF(!mManager->SendRawMessage(aMeta, msgData))) {
|
2019-02-08 16:02:08 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 21:18:33 +03:00
|
|
|
Document* JSWindowActorChild::GetDocument(ErrorResult& aRv) {
|
|
|
|
if (!mManager) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalWindowInner* window = mManager->WindowGlobal();
|
|
|
|
return window ? window->GetDocument() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowsingContext* JSWindowActorChild::GetBrowsingContext(ErrorResult& aRv) {
|
|
|
|
if (!mManager) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mManager->BrowsingContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<WindowProxyHolder> JSWindowActorChild::GetContentWindow(
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (BrowsingContext* bc = GetBrowsingContext(aRv)) {
|
|
|
|
return WindowProxyHolder(bc);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:01:33 +03:00
|
|
|
void JSWindowActorChild::StartDestroy() {
|
2019-05-10 18:01:40 +03:00
|
|
|
JSWindowActor::StartDestroy();
|
2019-05-10 18:01:33 +03:00
|
|
|
mCanSend = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSWindowActorChild::AfterDestroy() {
|
2019-05-10 18:01:40 +03:00
|
|
|
JSWindowActor::AfterDestroy();
|
2019-05-10 18:01:33 +03:00
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(JSWindowActorChild, JSWindowActor, mManager)
|
2019-03-05 20:12:40 +03:00
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(JSWindowActorChild,
|
|
|
|
JSWindowActor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorChild)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(JSWindowActor)
|
2019-01-28 22:02:02 +03:00
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(JSWindowActorChild, JSWindowActor)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(JSWindowActorChild, JSWindowActor)
|
2019-01-28 22:02:02 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|