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/. */
|
|
|
|
|
|
|
|
#include "TabParent.h"
|
|
|
|
|
|
|
|
// TabParent.h transitively includes <windows.h>, which does
|
|
|
|
// #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"
|
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"
|
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"
|
2016-03-31 07:58:05 +03:00
|
|
|
#include "mozilla/layout/RenderFrameParent.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.
|
|
|
|
*/
|
2013-02-23 04:59:26 +04:00
|
|
|
already_AddRefed<HTMLIFrameElement>
|
2012-08-18 01:07:54 +04:00
|
|
|
CreateIframe(Element* aOpenerFrameElement, const nsAString& aName, bool aRemote)
|
2012-06-13 02:01:25 +04:00
|
|
|
{
|
|
|
|
nsNodeInfoManager *nodeInfoManager =
|
|
|
|
aOpenerFrameElement->OwnerDoc()->NodeInfoManager();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NodeInfo> nodeInfo =
|
2012-06-13 02:01:25 +04:00
|
|
|
nodeInfoManager->GetNodeInfo(nsGkAtoms::iframe,
|
2012-07-30 18:20:58 +04:00
|
|
|
/* aPrefix = */ nullptr,
|
2012-06-13 02:01:25 +04:00
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLIFrameElement> popupFrameElement =
|
2013-02-23 04:59:26 +04:00
|
|
|
static_cast<HTMLIFrameElement*>(
|
2012-06-13 02:01:25 +04:00
|
|
|
NS_NewHTMLIFrameElement(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.
|
|
|
|
popupFrameElement->SetAttr(kNameSpaceID_None, nsGkAtoms::Remote,
|
|
|
|
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);
|
|
|
|
nsIPresShell *shell = aFrameElement->OwnerDoc()->GetShell();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext;
|
2012-12-25 10:09:34 +04:00
|
|
|
if (shell) {
|
|
|
|
presContext = shell->GetPresContext();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ErrorResult res;
|
|
|
|
event->InitCustomEvent(cx,
|
|
|
|
aEventName,
|
2016-09-09 11:45:00 +03:00
|
|
|
/* aCanBubble = */ true,
|
|
|
|
/* aCancelable = */ true,
|
2013-08-24 18:08:55 +04:00
|
|
|
aDetailValue,
|
|
|
|
res);
|
|
|
|
if (res.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
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.
|
2014-03-18 08:48:21 +04:00
|
|
|
nsresult rv =
|
2015-12-04 17:51:04 +03:00
|
|
|
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,
|
|
|
|
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;
|
|
|
|
|
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(cx, sgo->GetGlobalJSObject());
|
|
|
|
JSAutoCompartment ac(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"),
|
2013-08-24 18:08:55 +04:00
|
|
|
cx,
|
2013-09-10 08:18:46 +04:00
|
|
|
val, &status);
|
|
|
|
|
|
|
|
if (dispatchSucceeded) {
|
2016-03-31 13:58:25 +03:00
|
|
|
if (aPopupFrameElement->IsInUncomposedDoc()) {
|
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*/
|
|
|
|
BrowserElementParent::OpenWindowResult
|
2012-12-25 10:09:34 +04:00
|
|
|
BrowserElementParent::OpenWindowOOP(TabParent* aOpenerTabParent,
|
|
|
|
TabParent* aPopupTabParent,
|
2016-03-31 07:58:05 +03:00
|
|
|
PRenderFrameParent* aRenderFrame,
|
2012-06-13 02:01:25 +04:00
|
|
|
const nsAString& aURL,
|
|
|
|
const nsAString& aName,
|
2016-03-31 07:58:05 +03:00
|
|
|
const nsAString& aFeatures,
|
|
|
|
TextureFactoryIdentifier* aTextureFactoryIdentifier,
|
|
|
|
uint64_t* aLayersId)
|
2012-06-13 02:01:25 +04:00
|
|
|
{
|
|
|
|
// Create an iframe owned by the same document which owns openerFrameElement.
|
2013-07-24 03:39:17 +04:00
|
|
|
nsCOMPtr<Element> openerFrameElement = aOpenerTabParent->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();
|
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
OpenWindowResult opened =
|
2012-06-13 02:01:25 +04:00
|
|
|
DispatchOpenWindowEvent(openerFrameElement, popupFrameElement,
|
|
|
|
aURL, aName, 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.
|
|
|
|
aPopupTabParent->SetOwnerElement(popupFrameElement);
|
|
|
|
popupFrameElement->AllowCreateFrameLoader();
|
|
|
|
popupFrameElement->CreateRemoteFrameLoader(aPopupTabParent);
|
2016-03-31 07:58:05 +03:00
|
|
|
|
|
|
|
RenderFrameParent* rfp = static_cast<RenderFrameParent*>(aRenderFrame);
|
|
|
|
if (!aPopupTabParent->SetRenderFrame(rfp) ||
|
|
|
|
!aPopupTabParent->GetRenderFrameInfo(aTextureFactoryIdentifier, aLayersId)) {
|
|
|
|
return BrowserElementParent::OPEN_WINDOW_IGNORED;
|
|
|
|
}
|
|
|
|
|
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
|
2016-01-30 20:05:36 +03:00
|
|
|
BrowserElementParent::OpenWindowInProcess(nsPIDOMWindowOuter* 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,
|
2016-01-30 20:05:36 +03:00
|
|
|
mozIDOMWindowProxy** aReturnWindow)
|
2012-06-13 02:01:25 +04:00
|
|
|
{
|
2013-10-28 18:04:12 +04:00
|
|
|
*aReturnWindow = 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.)
|
|
|
|
//
|
|
|
|
// GetScriptableTop 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.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> win = aOpenerWindow->GetScriptableTop();
|
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;
|
|
|
|
popupFrameElement->PresetOpenerWindow(aOpenerWindow, res);
|
|
|
|
MOZ_ASSERT(!res.Failed());
|
|
|
|
}
|
2016-10-07 21:59:59 +03:00
|
|
|
|
2013-09-10 08:18:46 +04:00
|
|
|
OpenWindowResult opened =
|
2012-06-13 02:01:25 +04:00
|
|
|
DispatchOpenWindowEvent(openerFrameElement, popupFrameElement,
|
|
|
|
NS_ConvertUTF8toUTF16(spec),
|
|
|
|
aName,
|
|
|
|
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
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docshell;
|
|
|
|
frameLoader->GetDocShell(getter_AddRefs(docshell));
|
2013-09-10 08:18:46 +04:00
|
|
|
NS_ENSURE_TRUE(docshell, BrowserElementParent::OPEN_WINDOW_IGNORED);
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = docshell->GetWindow();
|
2012-06-13 02:01:25 +04:00
|
|
|
window.forget(aReturnWindow);
|
2013-09-10 08:18:46 +04:00
|
|
|
|
|
|
|
return !!*aReturnWindow ? opened : BrowserElementParent::OPEN_WINDOW_CANCELLED;
|
2012-06-13 02:01:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|