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"
|
2015-07-10 19:38:44 +03:00
|
|
|
#include "BrowserElementAudioChannel.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"
|
2012-06-13 02:01:25 +04:00
|
|
|
|
2013-08-24 18:08:55 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
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);
|
|
|
|
|
|
|
|
// Copy the opener frame's mozapp attribute to the popup frame.
|
|
|
|
if (aOpenerFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::mozapp)) {
|
|
|
|
nsAutoString mozapp;
|
|
|
|
aOpenerFrameElement->GetAttr(kNameSpaceID_None, nsGkAtoms::mozapp, mozapp);
|
|
|
|
popupFrameElement->SetAttr(kNameSpaceID_None, nsGkAtoms::mozapp,
|
|
|
|
mozapp, /* aNotify = */ false);
|
|
|
|
}
|
|
|
|
|
2014-03-26 08:56:35 +04:00
|
|
|
// Copy the opener frame's parentApp attribute to the popup frame.
|
|
|
|
if (aOpenerFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::parentapp)) {
|
|
|
|
nsAutoString parentApp;
|
|
|
|
aOpenerFrameElement->GetAttr(kNameSpaceID_None, nsGkAtoms::parentapp,
|
|
|
|
parentApp);
|
|
|
|
popupFrameElement->SetAttr(kNameSpaceID_None, nsGkAtoms::parentapp,
|
|
|
|
parentApp, /* aNotify = */ false);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
/* bubbles = */ true,
|
2013-09-10 08:18:46 +04:00
|
|
|
/* cancelable = */ 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
|
|
|
|
2014-08-19 18:14:22 +04:00
|
|
|
// Do not dispatch a mozbrowseropenwindow event of a widget to its embedder
|
|
|
|
nsCOMPtr<nsIMozBrowserFrame> browserFrame =
|
|
|
|
do_QueryInterface(aOpenerFrameElement);
|
|
|
|
if (browserFrame && browserFrame->GetReallyIsWidget()) {
|
|
|
|
return BrowserElementParent::OPEN_WINDOW_CANCELLED;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (aPopupFrameElement->IsInDoc()) {
|
|
|
|
return BrowserElementParent::OPEN_WINDOW_ADDED;
|
|
|
|
} else if (status == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
// 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,
|
2012-06-13 02:01:25 +04:00
|
|
|
const nsAString& aURL,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aFeatures)
|
|
|
|
{
|
|
|
|
// 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);
|
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
|
2012-06-13 02:01:25 +04:00
|
|
|
BrowserElementParent::OpenWindowInProcess(nsIDOMWindow* aOpenerWindow,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsACString& aFeatures,
|
|
|
|
nsIDOMWindow** aReturnWindow)
|
|
|
|
{
|
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.
|
2015-10-27 00:37:32 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> openerWindow = do_QueryInterface(aOpenerWindow);
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = openerWindow->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
|
|
|
|
|
|
|
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.
|
|
|
|
nsCOMPtr<nsIFrameLoader> frameLoader;
|
|
|
|
popupFrameElement->GetFrameLoader(getter_AddRefs(frameLoader));
|
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
|
|
|
|
2014-01-10 06:03:47 +04:00
|
|
|
nsCOMPtr<nsIDOMWindow> 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
|