2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-13 02:01:25 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2019-04-10 00:38:15 +03:00
|
|
|
#include "BrowserParent.h"
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2019-04-10 00:38:15 +03:00
|
|
|
// BrowserParent.h transitively includes <windows.h>, which does
|
2012-06-13 02:01:25 +04:00
|
|
|
// #define CreateEvent CreateEventW
|
2014-03-18 08:48:21 +04:00
|
|
|
// That messes up our call to EventDispatcher::CreateEvent below.
|
2012-06-13 02:01:25 +04:00
|
|
|
|
|
|
|
#ifdef CreateEvent
|
|
|
|
# undef CreateEvent
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "BrowserElementParent.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2019-03-29 18:11:04 +03:00
|
|
|
#include "mozilla/dom/DocumentInlines.h"
|
2013-02-23 04:59:26 +04:00
|
|
|
#include "mozilla/dom/HTMLIFrameElement.h"
|
2014-06-11 23:38:55 +04:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2019-01-02 16:26:56 +03:00
|
|
|
#include "mozilla/dom/WindowProxyHolder.h"
|
2012-08-23 23:33:46 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2012-06-13 02:01:25 +04:00
|
|
|
#include "nsVariant.h"
|
2013-08-24 18:08:55 +04:00
|
|
|
#include "mozilla/dom/BrowserElementDictionariesBinding.h"
|
2014-07-01 03:02:04 +04:00
|
|
|
#include "mozilla/dom/CustomEvent.h"
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2013-08-24 18:08:55 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2016-03-31 07:58:05 +03:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
using namespace mozilla::layout;
|
2012-06-13 02:01:25 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
using mozilla::BrowserElementParent;
|
2012-06-13 02:01:25 +04:00
|
|
|
/**
|
|
|
|
* Create an <iframe mozbrowser> owned by the same document as
|
|
|
|
* aOpenerFrameElement.
|
|
|
|
*/
|
2012-08-18 01:07:54 +04:00
|
|
|
already_AddRefed<HTMLIFrameElement> CreateIframe(Element* aOpenerFrameElement,
|
|
|
|
const nsAString& aName,
|
|
|
|
bool aRemote) {
|
2012-06-13 02:01:25 +04:00
|
|
|
nsNodeInfoManager* nodeInfoManager =
|
|
|
|
aOpenerFrameElement->OwnerDoc()->NodeInfoManager();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-06-13 02:01:25 +04:00
|
|
|
RefPtr<NodeInfo> nodeInfo = nodeInfoManager->GetNodeInfo(
|
|
|
|
nsGkAtoms::iframe,
|
2012-07-30 18:20:58 +04:00
|
|
|
/* aPrefix = */ nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLIFrameElement> popupFrameElement =
|
2013-02-23 04:59:26 +04:00
|
|
|
static_cast<HTMLIFrameElement*>(NS_NewHTMLIFrameElement(
|
2012-06-13 02:01:25 +04:00
|
|
|
nodeInfo.forget(), mozilla::dom::NOT_FROM_PARSER));
|
|
|
|
|
|
|
|
popupFrameElement->SetMozbrowser(true);
|
|
|
|
|
2012-08-13 23:19:10 +04:00
|
|
|
// Copy the window name onto the iframe.
|
|
|
|
popupFrameElement->SetAttr(kNameSpaceID_None, nsGkAtoms::name, aName,
|
|
|
|
/* aNotify = */ false);
|
|
|
|
|
2012-08-18 01:07:54 +04:00
|
|
|
// Indicate whether the iframe is should be remote.
|
2018-03-14 02:27:25 +03:00
|
|
|
popupFrameElement->SetAttr(
|
|
|
|
kNameSpaceID_None, nsGkAtoms::remote,
|
2012-08-18 01:07:54 +04:00
|
|
|
aRemote ? NS_LITERAL_STRING("true") : NS_LITERAL_STRING("false"),
|
|
|
|
/* aNotify = */ false);
|
|
|
|
|
2014-10-11 01:28:04 +04:00
|
|
|
// Copy the opener frame's mozprivatebrowsing attribute to the popup frame.
|
|
|
|
nsAutoString mozprivatebrowsing;
|
|
|
|
if (aOpenerFrameElement->GetAttr(kNameSpaceID_None,
|
|
|
|
nsGkAtoms::mozprivatebrowsing,
|
|
|
|
mozprivatebrowsing)) {
|
|
|
|
popupFrameElement->SetAttr(kNameSpaceID_None, nsGkAtoms::mozprivatebrowsing,
|
|
|
|
mozprivatebrowsing, /* aNotify = */ false);
|
|
|
|
}
|
|
|
|
|
2012-06-13 02:01:25 +04:00
|
|
|
return popupFrameElement.forget();
|
|
|
|
}
|
|
|
|
|
2012-12-25 10:09:34 +04:00
|
|
|
bool DispatchCustomDOMEvent(Element* aFrameElement, const nsAString& aEventName,
|
2013-09-10 08:18:46 +04:00
|
|
|
JSContext* cx, JS::Handle<JS::Value> aDetailValue,
|
|
|
|
nsEventStatus* aStatus) {
|
2012-12-25 10:09:34 +04:00
|
|
|
NS_ENSURE_TRUE(aFrameElement, false);
|
2018-02-21 01:00:10 +03:00
|
|
|
RefPtr<nsPresContext> presContext =
|
|
|
|
aFrameElement->OwnerDoc()->GetPresContext();
|
2012-12-25 10:09:34 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CustomEvent> event =
|
2015-08-12 14:39:31 +03:00
|
|
|
NS_NewDOMCustomEvent(aFrameElement, presContext, nullptr);
|
|
|
|
|
2013-08-24 18:08:55 +04:00
|
|
|
event->InitCustomEvent(cx, aEventName,
|
2016-09-09 11:45:00 +03:00
|
|
|
/* aCanBubble = */ true,
|
|
|
|
/* aCancelable = */ true, aDetailValue);
|
2018-03-26 21:53:51 +03:00
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
event->SetTrusted(true);
|
2012-12-25 10:09:34 +04:00
|
|
|
// Dispatch the event.
|
2015-08-14 05:00:02 +03:00
|
|
|
// We don't initialize aStatus here, as our callers have already done so.
|
2015-12-04 17:51:04 +03:00
|
|
|
nsresult rv = EventDispatcher::DispatchDOMEvent(aFrameElement, nullptr, event,
|
2015-08-12 14:39:31 +03:00
|
|
|
presContext, aStatus);
|
2012-12-25 10:09:34 +04:00
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-09-10 08:18:46 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-06-13 02:01:25 +04:00
|
|
|
/**
|
|
|
|
* Dispatch a mozbrowseropenwindow event to the given opener frame element.
|
|
|
|
* The "popup iframe" (event.detail.frameElement) will be |aPopupFrameElement|.
|
|
|
|
*
|
|
|
|
* Returns true iff there were no unexpected failures and the window.open call
|
|
|
|
* was accepted by the embedder.
|
|
|
|
*/
|
2013-09-10 08:18:46 +04:00
|
|
|
/*static*/
|
|
|
|
BrowserElementParent::OpenWindowResult
|
|
|
|
BrowserElementParent::DispatchOpenWindowEvent(Element* aOpenerFrameElement,
|
2012-06-13 02:01:25 +04:00
|
|
|
Element* aPopupFrameElement,
|
|
|
|
const nsAString& aURL,
|
|
|
|
const nsAString& aName,
|
2019-04-25 02:50:40 +03:00
|
|
|
bool aForceNoReferrer,
|
2012-06-13 02:01:25 +04:00
|
|
|
const nsAString& aFeatures) {
|
|
|
|
// Dispatch a CustomEvent at aOpenerFrameElement with a detail object
|
2013-08-24 18:08:55 +04:00
|
|
|
// (OpenWindowEventDetail) containing aPopupFrameElement, aURL, aName, and
|
2012-06-13 02:01:25 +04:00
|
|
|
// aFeatures.
|
|
|
|
|
|
|
|
// Create the event's detail object.
|
2013-10-09 20:05:22 +04:00
|
|
|
OpenWindowEventDetail detail;
|
2015-11-04 07:02:26 +03:00
|
|
|
if (aURL.IsEmpty()) {
|
|
|
|
// URL should never be empty. Assign about:blank as default.
|
|
|
|
detail.mUrl = NS_LITERAL_STRING("about:blank");
|
|
|
|
} else {
|
|
|
|
detail.mUrl = aURL;
|
|
|
|
}
|
2013-08-24 18:08:55 +04:00
|
|
|
detail.mName = aName;
|
|
|
|
detail.mFeatures = aFeatures;
|
|
|
|
detail.mFrameElement = aPopupFrameElement;
|
2019-04-25 02:50:40 +03:00
|
|
|
detail.mForceNoReferrer = aForceNoReferrer;
|
2013-08-24 18:08:55 +04:00
|
|
|
|
|
|
|
nsIGlobalObject* sgo = aPopupFrameElement->OwnerDoc()->GetScopeObject();
|
|
|
|
if (!sgo) {
|
2013-09-10 08:18:46 +04:00
|
|
|
return BrowserElementParent::OPEN_WINDOW_IGNORED;
|
2013-08-24 18:08:55 +04:00
|
|
|
}
|
|
|
|
|
2019-04-04 11:45:14 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(sgo)) {
|
|
|
|
return BrowserElementParent::OPEN_WINDOW_IGNORED;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
|
2013-08-24 18:08:55 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, sgo->GetGlobalJSObject());
|
2018-08-02 09:49:00 +03:00
|
|
|
JSAutoRealm ar(cx, global);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, detail, &val)) {
|
2013-08-24 18:08:55 +04:00
|
|
|
MOZ_CRASH("Failed to convert dictionary to JS::Value due to OOM.");
|
2013-09-10 08:18:46 +04:00
|
|
|
return BrowserElementParent::OPEN_WINDOW_IGNORED;
|
2013-08-24 18:08:55 +04:00
|
|
|
}
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2015-08-14 05:00:02 +03:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2012-12-25 10:09:34 +04:00
|
|
|
bool dispatchSucceeded = DispatchCustomDOMEvent(
|
|
|
|
aOpenerFrameElement, NS_LITERAL_STRING("mozbrowseropenwindow"), cx, val,
|
2013-09-10 08:18:46 +04:00
|
|
|
&status);
|
|
|
|
|
|
|
|
if (dispatchSucceeded) {
|
2018-07-03 18:08:19 +03:00
|
|
|
if (aPopupFrameElement->IsInComposedDoc()) {
|
2013-09-10 08:18:46 +04:00
|
|
|
return BrowserElementParent::OPEN_WINDOW_ADDED;
|
2017-02-09 15:29:40 +03:00
|
|
|
}
|
|
|
|
if (status == nsEventStatus_eConsumeNoDefault) {
|
2013-09-10 08:18:46 +04:00
|
|
|
// If the frame was not added to a document, report to callers whether
|
|
|
|
// preventDefault was called on or not
|
|
|
|
return BrowserElementParent::OPEN_WINDOW_CANCELLED;
|
|
|
|
}
|
|
|
|
}
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
return BrowserElementParent::OPEN_WINDOW_IGNORED;
|
2012-06-13 02:01:25 +04:00
|
|
|
}
|
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
/*static*/
|
2012-12-25 10:09:34 +04:00
|
|
|
BrowserElementParent::OpenWindowResult BrowserElementParent::OpenWindowOOP(
|
2019-04-10 00:38:15 +03:00
|
|
|
BrowserParent* aOpenerBrowserParent, BrowserParent* aPopupBrowserParent,
|
2019-04-25 02:50:40 +03:00
|
|
|
const nsAString& aURL, const nsAString& aName, bool aForceNoReferrer,
|
|
|
|
const nsAString& aFeatures) {
|
2012-06-13 02:01:25 +04:00
|
|
|
// Create an iframe owned by the same document which owns openerFrameElement.
|
2019-04-10 00:38:15 +03:00
|
|
|
nsCOMPtr<Element> openerFrameElement =
|
|
|
|
aOpenerBrowserParent->GetOwnerElement();
|
2013-09-10 08:18:46 +04:00
|
|
|
NS_ENSURE_TRUE(openerFrameElement, BrowserElementParent::OPEN_WINDOW_IGNORED);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLIFrameElement> popupFrameElement =
|
2012-08-18 01:07:54 +04:00
|
|
|
CreateIframe(openerFrameElement, aName, /* aRemote = */ true);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
|
|
|
// Normally an <iframe> element will try to create a frameLoader when the
|
|
|
|
// page touches iframe.contentWindow or sets iframe.src.
|
|
|
|
//
|
|
|
|
// But in our case, we want to delay the creation of the frameLoader until
|
|
|
|
// we've verified that the popup has gone through successfully. If the popup
|
|
|
|
// is "blocked" by the embedder, we don't want to load the popup's url.
|
|
|
|
//
|
|
|
|
// Therefore we call DisallowCreateFrameLoader() on the element and call
|
|
|
|
// AllowCreateFrameLoader() only after we've verified that the popup was
|
|
|
|
// allowed.
|
|
|
|
popupFrameElement->DisallowCreateFrameLoader();
|
|
|
|
|
2019-04-25 02:50:40 +03:00
|
|
|
OpenWindowResult opened =
|
|
|
|
DispatchOpenWindowEvent(openerFrameElement, popupFrameElement, aURL,
|
|
|
|
aName, aForceNoReferrer, aFeatures);
|
2013-09-10 08:18:46 +04:00
|
|
|
|
|
|
|
if (opened != BrowserElementParent::OPEN_WINDOW_ADDED) {
|
|
|
|
return opened;
|
2012-06-13 02:01:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The popup was not blocked, so hook up the frame element and the popup tab
|
|
|
|
// parent, and return success.
|
2019-04-10 00:38:15 +03:00
|
|
|
aPopupBrowserParent->SetOwnerElement(popupFrameElement);
|
2012-06-13 02:01:25 +04:00
|
|
|
popupFrameElement->AllowCreateFrameLoader();
|
2019-04-10 00:38:15 +03:00
|
|
|
popupFrameElement->CreateRemoteFrameLoader(aPopupBrowserParent);
|
2016-03-31 07:58:05 +03:00
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
return opened;
|
2012-06-13 02:01:25 +04:00
|
|
|
}
|
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
/* static */
|
|
|
|
BrowserElementParent::OpenWindowResult
|
2019-01-02 16:27:05 +03:00
|
|
|
BrowserElementParent::OpenWindowInProcess(BrowsingContext* aOpenerWindow,
|
2012-06-13 02:01:25 +04:00
|
|
|
nsIURI* aURI, const nsAString& aName,
|
|
|
|
const nsACString& aFeatures,
|
2016-10-27 19:37:44 +03:00
|
|
|
bool aForceNoOpener,
|
2019-08-02 23:48:33 +03:00
|
|
|
BrowsingContext** aReturnBC) {
|
|
|
|
*aReturnBC = nullptr;
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2012-06-23 19:05:12 +04:00
|
|
|
// If we call window.open from an <iframe> inside an <iframe mozbrowser>,
|
|
|
|
// it's as though the top-level document inside the <iframe mozbrowser>
|
|
|
|
// called window.open. (Indeed, in the OOP case, the inner <iframe> lives
|
|
|
|
// out-of-process, so we couldn't touch it if we tried.)
|
|
|
|
//
|
2019-07-26 19:48:31 +03:00
|
|
|
// GetInProcessScriptableTop gets us the <iframe mozbrowser>'s window; we'll
|
|
|
|
// use its frame element, rather than aOpenerWindow's frame element, as our
|
|
|
|
// "opener frame element" below.
|
2019-01-02 16:27:05 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> win =
|
2019-07-26 19:48:31 +03:00
|
|
|
aOpenerWindow->GetDOMWindow()->GetInProcessScriptableTop();
|
2014-02-27 20:56:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<Element> openerFrameElement = win->GetFrameElementInternal();
|
|
|
|
NS_ENSURE_TRUE(openerFrameElement, BrowserElementParent::OPEN_WINDOW_IGNORED);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLIFrameElement> popupFrameElement =
|
2012-08-18 01:07:54 +04:00
|
|
|
CreateIframe(openerFrameElement, aName, /* aRemote = */ false);
|
2013-09-10 08:18:46 +04:00
|
|
|
NS_ENSURE_TRUE(popupFrameElement, BrowserElementParent::OPEN_WINDOW_IGNORED);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2012-08-01 07:12:04 +04:00
|
|
|
if (aURI) {
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
}
|
2013-09-10 08:18:46 +04:00
|
|
|
|
2016-10-27 19:37:44 +03:00
|
|
|
if (!aForceNoOpener) {
|
|
|
|
ErrorResult res;
|
2019-01-02 16:26:56 +03:00
|
|
|
popupFrameElement->PresetOpenerWindow(WindowProxyHolder(aOpenerWindow),
|
|
|
|
res);
|
2016-10-27 19:37:44 +03:00
|
|
|
MOZ_ASSERT(!res.Failed());
|
|
|
|
}
|
2016-10-07 21:59:59 +03:00
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
OpenWindowResult opened = DispatchOpenWindowEvent(
|
2012-06-13 02:01:25 +04:00
|
|
|
openerFrameElement, popupFrameElement, NS_ConvertUTF8toUTF16(spec), aName,
|
2019-04-25 02:50:40 +03:00
|
|
|
false, NS_ConvertUTF8toUTF16(aFeatures));
|
2013-09-10 08:18:46 +04:00
|
|
|
|
|
|
|
if (opened != BrowserElementParent::OPEN_WINDOW_ADDED) {
|
|
|
|
return opened;
|
2012-06-13 02:01:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return popupFrameElement's window.
|
2016-08-11 19:09:22 +03:00
|
|
|
RefPtr<nsFrameLoader> frameLoader = popupFrameElement->GetFrameLoader();
|
2013-09-10 08:18:46 +04:00
|
|
|
NS_ENSURE_TRUE(frameLoader, BrowserElementParent::OPEN_WINDOW_IGNORED);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2018-03-22 05:43:15 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docshell = frameLoader->GetDocShell(IgnoreErrors());
|
2013-09-10 08:18:46 +04:00
|
|
|
NS_ENSURE_TRUE(docshell, BrowserElementParent::OPEN_WINDOW_IGNORED);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2019-08-07 19:59:30 +03:00
|
|
|
docshell->GetBrowsingContextXPCOM(aReturnBC);
|
2013-09-10 08:18:46 +04:00
|
|
|
|
2019-08-02 23:48:33 +03:00
|
|
|
return *aReturnBC ? opened : BrowserElementParent::OPEN_WINDOW_CANCELLED;
|
2012-06-13 02:01:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|