Skip to content

Commit

Permalink
Merge pull request mozilla#29 from KershawChang/bug_1268810
Browse files Browse the repository at this point in the history
Bug 1268810
  • Loading branch information
xeonchen authored Jun 30, 2016
2 parents 49b98aa + ae9c15c commit e81fd19
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 9 deletions.
3 changes: 3 additions & 0 deletions dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8888,6 +8888,9 @@ nsContentUtils::GetPresentationURL(nsIDocShell* aDocShell, nsAString& aPresentat
nsCOMPtr<nsILoadContext> loadContext(do_QueryInterface(aDocShell));
nsCOMPtr<nsIDOMElement> topFrameElement;
loadContext->GetTopFrameElement(getter_AddRefs(topFrameElement));
if (!topFrameElement) {
return;
}

topFrameElement->GetAttribute(NS_LITERAL_STRING("mozpresentation"), aPresentationUrl);
}
29 changes: 20 additions & 9 deletions dom/base/nsFrameLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1880,15 +1880,6 @@ nsFrameLoader::MaybeCreateDocShell()
NS_ENSURE_SUCCESS(rv,rv);
}

// Apply sandbox flags even if our owner is not an iframe, as this copies
// flags from our owning content's owning document.
uint32_t sandboxFlags = 0;
HTMLIFrameElement* iframe = HTMLIFrameElement::FromContent(mOwnerContent);
if (iframe) {
sandboxFlags = iframe->GetSandboxFlags();
}
ApplySandboxFlags(sandboxFlags);

if (!mNetworkCreated) {
if (mDocShell) {
mDocShell->SetCreatedDynamically(true);
Expand Down Expand Up @@ -2024,6 +2015,17 @@ nsFrameLoader::MaybeCreateDocShell()
mDocShell->SetFrameType(nsIDocShell::FRAME_TYPE_BROWSER);
}

// Apply sandbox flags even if our owner is not an iframe, as this copies
// flags from our owning content's owning document.
// Note: ApplySandboxFlags should be called after mDocShell->SetFrameType
// because we need to get the correct presentation URL in ApplySandboxFlags.
uint32_t sandboxFlags = 0;
HTMLIFrameElement* iframe = HTMLIFrameElement::FromContent(mOwnerContent);
if (iframe) {
sandboxFlags = iframe->GetSandboxFlags();
}
ApplySandboxFlags(sandboxFlags);

// Grab the userContextId from owner if XUL
nsAutoString userContextIdStr;
if ((namespaceID == kNameSpaceID_XUL) &&
Expand Down Expand Up @@ -2858,6 +2860,15 @@ nsFrameLoader::ApplySandboxFlags(uint32_t sandboxFlags)

// The child can only add restrictions, never remove them.
sandboxFlags |= parentSandboxFlags;

// If this frame is a receiving browsing context, we should add
// sandboxed auxiliary navigation flag to sandboxFlags. See
// https://w3c.github.io/presentation-api/#creating-a-receiving-browsing-context
nsAutoString presentationURL;
nsContentUtils::GetPresentationURL(mDocShell, presentationURL);
if (!presentationURL.IsEmpty()) {
sandboxFlags |= SANDBOXED_AUXILIARY_NAVIGATION;
}
mDocShell->SetSandboxFlags(sandboxFlags);
}
}
Expand Down
6 changes: 6 additions & 0 deletions dom/ipc/TabChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#include "nsIScriptError.h"
#include "mozilla/EventForwards.h"
#include "nsDeviceContext.h"
#include "nsSandboxFlags.h"
#include "FrameLayerBuilder.h"

#define BROWSER_ELEMENT_CHILD_SCRIPT \
Expand Down Expand Up @@ -876,6 +877,11 @@ TabChild::NotifyTabContextUpdated()
nsIDocShell::FRAME_TYPE_APP :
nsIDocShell::FRAME_TYPE_REGULAR);
nsDocShell::Cast(docShell)->SetOriginAttributes(OriginAttributesRef());

// Set SANDBOXED_AUXILIARY_NAVIGATION flag if this is a receiver page.
if (!PresentationURL().IsEmpty()) {
docShell->SetSandboxFlags(SANDBOXED_AUXILIARY_NAVIGATION);
}
}

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TabChild)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for sandboxed auxiliary navigation flag in receiver page</title>
</head>
<body>
<div id="content"></div>
<script type="application/javascript;version=1.7">

"use strict";

function is(a, b, msg) {
alert((a === b ? 'OK ' : 'KO ') + msg);
}

function ok(a, msg) {
alert((a ? 'OK ' : 'KO ') + msg);
}

function info(msg) {
alert('INFO ' + msg);
}

function command(msg) {
alert('COMMAND ' + JSON.stringify(msg));
}

function finish() {
alert('DONE');
}

function testConnectionAvailable() {
return new Promise(function(aResolve, aReject) {
ok(navigator.presentation, "navigator.presentation should be available in OOP receiving pages.");
ok(navigator.presentation.receiver, "navigator.presentation.receiver should be available in receiving pages.");

aResolve();
});
}

function testOpenWindow() {
return new Promise(function(aResolve, aReject) {
try {
window.open("http://example.com");
ok(false, "receiver page should not be able to open a new window.");
} catch(e) {
ok(true, "receiver page should not be able to open a new window.");
aResolve();
}
});
}

testConnectionAvailable().
then(testOpenWindow).
then(finish);

</script>
</body>
</html>
6 changes: 6 additions & 0 deletions dom/presentation/tests/mochitest/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ support-files =
file_presentation_receiver_inner_iframe.html
file_presentation_1ua_wentaway.html
test_presentation_1ua_connection_wentaway.js
file_presentation_receiver_auxiliary_navigation.html
test_presentation_receiver_auxiliary_navigation.js

[test_presentation_dc_sender.html]
[test_presentation_dc_receiver.html]
Expand Down Expand Up @@ -46,3 +48,7 @@ skip-if = (e10s || toolkit == 'gonk' || toolkit == 'android') # Bug 1129785
skip-if = (e10s || toolkit == 'gonk' || toolkit == 'android') # Bug 1129785
[test_presentation_tcp_receiver_oop.html]
skip-if = (e10s || toolkit == 'gonk' || toolkit == 'android') # Bug 1129785
[test_presentation_receiver_auxiliary_navigation_inproc.html]
skip-if = (e10s || toolkit == 'gonk')
[test_presentation_receiver_auxiliary_navigation_oop.html]
skip-if = (e10s || toolkit == 'gonk')
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict";

var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL("PresentationSessionChromeScript.js"));
var receiverUrl = SimpleTest.getTestFileURL("file_presentation_receiver_auxiliary_navigation.html");

var obs = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
.getService(SpecialPowers.Ci.nsIObserverService);

function setup() {
return new Promise(function(aResolve, aReject) {
gScript.sendAsyncMessage("trigger-device-add");

var iframe = document.createElement("iframe");
iframe.setAttribute("mozbrowser", "true");
iframe.setAttribute("mozpresentation", receiverUrl);
var oop = location.pathname.indexOf('_inproc') == -1;
iframe.setAttribute("remote", oop);
iframe.setAttribute("src", receiverUrl);

// This event is triggered when the iframe calls "postMessage".
iframe.addEventListener("mozbrowsershowmodalprompt", function listener(aEvent) {
var message = aEvent.detail.message;
if (/^OK /.exec(message)) {
ok(true, "Message from iframe: " + message);
} else if (/^KO /.exec(message)) {
ok(false, "Message from iframe: " + message);
} else if (/^INFO /.exec(message)) {
info("Message from iframe: " + message);
} else if (/^COMMAND /.exec(message)) {
var command = JSON.parse(message.replace(/^COMMAND /, ""));
gScript.sendAsyncMessage(command.name, command.data);
} else if (/^DONE$/.exec(message)) {
ok(true, "Messaging from iframe complete.");
iframe.removeEventListener("mozbrowsershowmodalprompt", listener);

teardown();
}
}, false);

var promise = new Promise(function(aResolve, aReject) {
document.body.appendChild(iframe);

aResolve(iframe);
});
obs.notifyObservers(promise, "setup-request-promise", null);

aResolve();
});
}

function teardown() {
gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() {
gScript.removeMessageListener("teardown-complete", teardownCompleteHandler);
gScript.destroy();
SimpleTest.finish();
});

gScript.sendAsyncMessage("teardown");
}

function runTests() {
setup().then();
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPermissions([
{type: "presentation-device-manage", allow: false, context: document},
{type: "presentation", allow: true, context: document},
{type: "browser", allow: true, context: document},
], function() {
SpecialPowers.pushPrefEnv({ "set": [["dom.presentation.enabled", true],
["dom.mozBrowserFramesEnabled", true],
["dom.presentation.session_transport.data_channel.enable", false]]},
runTests);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<!-- vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: -->
<html>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
<meta charset="utf-8">
<title>Test for B2G Presentation API when sender and receiver at the same side</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1268810">
Test for receiver page with sandboxed auxiliary navigation browsing context flag.</a>
<script type="application/javascript;version=1.8" src="test_presentation_receiver_auxiliary_navigation.js">
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<!-- vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: -->
<html>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
<meta charset="utf-8">
<title>Test for B2G Presentation API when sender and receiver at the same side</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1268810">
Test for receiver page with sandboxed auxiliary navigation browsing context flag.</a>
<script type="application/javascript;version=1.8" src="test_presentation_receiver_auxiliary_navigation.js">
</script>
</body>
</html>

0 comments on commit e81fd19

Please sign in to comment.