2018-08-30 01:21:25 +03:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 ft=cpp : */
|
|
|
|
/* 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/WindowGlobalChild.h"
|
|
|
|
#include "mozilla/ipc/InProcessChild.h"
|
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
2018-10-20 02:02:56 +03:00
|
|
|
#include "mozilla/dom/WindowGlobalActorsBinding.h"
|
2018-08-30 01:21:25 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-10-20 23:04:00 +03:00
|
|
|
typedef nsRefPtrHashtable<nsUint64HashKey, WindowGlobalChild> WGCByIdMap;
|
|
|
|
static StaticAutoPtr<WGCByIdMap> gWindowGlobalChildById;
|
|
|
|
|
|
|
|
WindowGlobalChild::WindowGlobalChild(nsGlobalWindowInner* aWindow,
|
|
|
|
dom::BrowsingContext* aBrowsingContext)
|
2018-08-30 01:21:25 +03:00
|
|
|
: mWindowGlobal(aWindow)
|
|
|
|
, mBrowsingContext(aBrowsingContext)
|
2018-10-20 23:04:00 +03:00
|
|
|
, mInnerWindowId(aWindow->WindowID())
|
|
|
|
, mOuterWindowId(aWindow->GetOuterWindow()->WindowID())
|
|
|
|
, mIPCClosed(true)
|
2018-08-30 01:21:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WindowGlobalChild>
|
|
|
|
WindowGlobalChild::Create(nsGlobalWindowInner* aWindow)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = aWindow->GetPrincipal();
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
|
|
|
|
RefPtr<nsDocShell> docshell = nsDocShell::Cast(aWindow->GetDocShell());
|
|
|
|
MOZ_ASSERT(docshell);
|
|
|
|
|
|
|
|
// Initalize our WindowGlobalChild object.
|
|
|
|
RefPtr<dom::BrowsingContext> bc = docshell->GetBrowsingContext();
|
|
|
|
RefPtr<WindowGlobalChild> wgc = new WindowGlobalChild(aWindow, bc);
|
|
|
|
|
2018-10-20 23:04:00 +03:00
|
|
|
WindowGlobalInit init(principal,
|
|
|
|
BrowsingContextId(wgc->BrowsingContext()->Id()),
|
|
|
|
wgc->mInnerWindowId,
|
|
|
|
wgc->mOuterWindowId);
|
2018-08-30 01:21:25 +03:00
|
|
|
|
|
|
|
// Send the link constructor over PInProcessChild or PBrowser.
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
InProcessChild* ipc = InProcessChild::Singleton();
|
|
|
|
if (!ipc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: ref is released in DeallocPWindowGlobalChild
|
|
|
|
ipc->SendPWindowGlobalConstructor(do_AddRef(wgc).take(), init);
|
|
|
|
} else {
|
|
|
|
RefPtr<TabChild> tabChild = TabChild::GetFrom(static_cast<mozIDOMWindow*>(aWindow));
|
|
|
|
MOZ_ASSERT(tabChild);
|
|
|
|
|
|
|
|
// Note: ref is released in DeallocPWindowGlobalChild
|
|
|
|
tabChild->SendPWindowGlobalConstructor(do_AddRef(wgc).take(), init);
|
|
|
|
}
|
2018-10-20 23:04:00 +03:00
|
|
|
wgc->mIPCClosed = false;
|
|
|
|
|
|
|
|
// Register this WindowGlobal in the gWindowGlobalParentsById map.
|
|
|
|
if (!gWindowGlobalChildById) {
|
|
|
|
gWindowGlobalChildById = new WGCByIdMap();
|
|
|
|
ClearOnShutdown(&gWindowGlobalChildById);
|
|
|
|
}
|
|
|
|
auto entry = gWindowGlobalChildById->LookupForAdd(wgc->mInnerWindowId);
|
|
|
|
MOZ_RELEASE_ASSERT(!entry, "Duplicate WindowGlobalChild entry for ID!");
|
|
|
|
entry.OrInsert([&] { return wgc; });
|
2018-08-30 01:21:25 +03:00
|
|
|
|
|
|
|
return wgc.forget();
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:04:00 +03:00
|
|
|
/* static */ already_AddRefed<WindowGlobalChild>
|
|
|
|
WindowGlobalChild::GetByInnerWindowId(uint64_t aInnerWindowId)
|
|
|
|
{
|
|
|
|
if (!gWindowGlobalChildById) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return gWindowGlobalChildById->Get(aInnerWindowId);
|
|
|
|
}
|
|
|
|
|
2018-11-27 23:03:05 +03:00
|
|
|
bool
|
|
|
|
WindowGlobalChild::IsCurrentGlobal()
|
|
|
|
{
|
|
|
|
return !mIPCClosed && mWindowGlobal->IsCurrentInnerWindow();
|
|
|
|
}
|
|
|
|
|
2018-08-30 01:21:25 +03:00
|
|
|
already_AddRefed<WindowGlobalParent>
|
2018-10-20 02:02:56 +03:00
|
|
|
WindowGlobalChild::GetParentActor()
|
2018-08-30 01:21:25 +03:00
|
|
|
{
|
|
|
|
if (mIPCClosed) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
IProtocol* otherSide = InProcessChild::ParentActorFor(this);
|
|
|
|
return do_AddRef(static_cast<WindowGlobalParent*>(otherSide));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WindowGlobalChild::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mIPCClosed = true;
|
2018-10-20 23:04:00 +03:00
|
|
|
gWindowGlobalChildById->Remove(mInnerWindowId);
|
2018-08-30 01:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowGlobalChild::~WindowGlobalChild()
|
|
|
|
{
|
2018-10-20 23:04:00 +03:00
|
|
|
MOZ_ASSERT(!gWindowGlobalChildById ||
|
|
|
|
!gWindowGlobalChildById->Contains(mInnerWindowId));
|
2018-08-30 01:21:25 +03:00
|
|
|
}
|
|
|
|
|
2018-10-20 02:02:56 +03:00
|
|
|
JSObject*
|
|
|
|
WindowGlobalChild::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return WindowGlobalChild_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports*
|
|
|
|
WindowGlobalChild::GetParentObject()
|
|
|
|
{
|
|
|
|
return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(WindowGlobalChild,
|
|
|
|
mWindowGlobal,
|
|
|
|
mBrowsingContext)
|
|
|
|
|
2018-08-30 01:21:25 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WindowGlobalChild, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WindowGlobalChild, Release)
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|