Bug 1503070 part 8. Remove executeScript API from BrowserElement. r=ehsan

This commit is contained in:
Boris Zbarsky 2018-11-01 17:37:48 -04:00
Родитель a595b274e1
Коммит 6ca7e6b57e
13 изменённых файлов: 0 добавлений и 325 удалений

Просмотреть файл

@ -9,7 +9,6 @@ const METHODS = {
stop: {},
getCanGoBack: {},
getCanGoForward: {},
executeScript: {},
getWebManifest: {},
};

Просмотреть файл

@ -284,7 +284,6 @@ BrowserElementChild.prototype = {
"owner-visibility-change": this._recvOwnerVisibilityChange,
"entered-fullscreen": this._recvEnteredFullscreen,
"exit-fullscreen": this._recvExitFullscreen,
"execute-script": this._recvExecuteScript,
"get-web-manifest": this._recvGetWebManifest,
}
@ -876,90 +875,6 @@ BrowserElementChild.prototype = {
sendAsyncMsg("scroll", { top: win.scrollY, left: win.scrollX });
},
_recvExecuteScript: function(data) {
debug("Received executeScript message: (" + data.json.id + ")");
let domRequestID = data.json.id;
let sendError = errorMsg => sendAsyncMsg("execute-script-done", {
errorMsg,
id: domRequestID
});
let sendSuccess = successRv => sendAsyncMsg("execute-script-done", {
successRv,
id: domRequestID
});
let isJSON = obj => {
try {
JSON.stringify(obj);
} catch(e) {
return false;
}
return true;
}
let expectedOrigin = data.json.args.options.origin;
let expectedUrl = data.json.args.options.url;
if (expectedOrigin) {
if (expectedOrigin != content.location.origin) {
sendError("Origin mismatches");
return;
}
}
if (expectedUrl) {
let expectedURI
try {
expectedURI = Services.io.newURI(expectedUrl);
} catch(e) {
sendError("Malformed URL");
return;
}
let currentURI = docShell.QueryInterface(Ci.nsIWebNavigation).currentURI;
if (!currentURI.equalsExceptRef(expectedURI)) {
sendError("URL mismatches");
return;
}
}
let sandbox = new Cu.Sandbox([content], {
sandboxPrototype: content,
sandboxName: "browser-api-execute-script",
allowWaivers: false,
sameZoneAs: content
});
try {
let sandboxRv = Cu.evalInSandbox(data.json.args.script, sandbox, "1.8");
if (sandboxRv instanceof sandbox.Promise) {
sandboxRv.then(rv => {
if (isJSON(rv)) {
sendSuccess(rv);
} else {
sendError("Value returned (resolve) by promise is not a valid JSON object");
}
}, error => {
if (isJSON(error)) {
sendError(error);
} else {
sendError("Value returned (reject) by promise is not a valid JSON object");
}
});
} else {
if (isJSON(sandboxRv)) {
sendSuccess(sandboxRv);
} else {
sendError("Script last expression must be a promise or a JSON object");
}
}
} catch(e) {
sendError(e.toString());
}
},
_mozScrollAreaChanged: function(e) {
sendAsyncMsg('scrollareachanged', {
width: e.width,

Просмотреть файл

@ -165,7 +165,6 @@ BrowserElementParent.prototype = {
"got-set-input-method-active": this._gotDOMRequestResult,
"scrollviewchange": this._handleScrollViewChange,
"caretstatechanged": this._handleCaretStateChanged,
"execute-script-done": this._gotDOMRequestResult,
"got-web-manifest": this._gotDOMRequestResult,
};
@ -581,19 +580,6 @@ BrowserElementParent.prototype = {
this._sendAsyncMsg('stop');
}),
executeScript: function(script, options) {
if (!this._isAlive()) {
throw Components.Exception("Dead content process",
Cr.NS_ERROR_DOM_INVALID_STATE_ERR);
}
// Enforcing options.url or options.origin
if (!options.url && !options.origin) {
throw Components.Exception("Invalid argument", Cr.NS_ERROR_INVALID_ARG);
}
return this._sendDOMRequest('execute-script', {script, options});
},
getWebManifest: defineDOMRequestMethod('get-web-manifest'),
/**
* Called when the visibility of the window which owns this iframe changes.

Просмотреть файл

@ -1,118 +0,0 @@
/* Any copyright is dedicated to the public domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Bug 1174733 - Browser API: iframe.executeScript
'use strict';
SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
function runTest() {
const origin = 'http://example.org';
const url = 'http://example.org/tests/dom/browser-element/mochitest/file_browserElement_ExecuteScript.html';
// Test if all key=>value pairs in o1 are present in o2.
const c = (o1, o2) => Object.keys(o1).every(k => o1[k] == o2[k]);
let scriptId = 0;
const bail = () => {
ok(false, `scriptId: ${scriptId++}`);
}
SpecialPowers.pushPermissions([
{type: 'browser', allow: 1, context: document},
{type: 'browser:universalxss', allow: 1, context: document}
], function() {
let iframe = document.createElement('iframe');
iframe.setAttribute('mozbrowser', 'true');
iframe.addEventListener('mozbrowserloadend', function() {
onReady(iframe);
}, {once: true});
iframe.src = url;
document.body.appendChild(iframe);
});
function onReady(iframe) {
iframe.executeScript('4 + 4', {url}).then(rv => {
is(rv, 8, `scriptId: ${scriptId++}`);
return iframe.executeScript('(() => {return {a:42}})()', {url})
}, bail).then(rv => {
ok(c(rv, {a:42}), `scriptId: ${scriptId++}`);
return iframe.executeScript('(() => {return {a:42}})()', {origin})
}, bail).then(rv => {
ok(c(rv, {a:42}), `scriptId: ${scriptId++}`);
return iframe.executeScript('(() => {return {a:42}})()', {origin, url})
}, bail).then(rv => {
ok(c(rv, {a:42}), `scriptId: ${scriptId++}`);
return iframe.executeScript(`
new Promise((resolve, reject) => {
resolve(document.body.textContent.trim());
});
`, {url})
}, bail).then(rv => {
is(rv, 'foo', `scriptId: ${scriptId++}`);
return iframe.executeScript(`
new Promise((resolve, reject) => {
resolve({a:43,b:34});
});
`, {url})
}, bail).then(rv => {
ok(c(rv, {a:43,b:34}), `scriptId: ${scriptId++}`);
return iframe.executeScript(`
syntax error
`, {url});
}, bail).then(bail, (error) => {
is(error.message, 'SyntaxError: illegal character', `scriptId: ${scriptId++}`);
return iframe.executeScript(`
window
`, {url});
}).then(bail, (error) => {
is(error.message, 'Script last expression must be a promise or a JSON object', `scriptId: ${scriptId++}`);
return iframe.executeScript(`
new Promise((resolve, reject) => {
reject('BOOM');
});
`, {url});
}).then(bail, (error) => {
is(error.message, 'BOOM', `scriptId: ${scriptId++}`);
return iframe.executeScript(`
new Promise((resolve, reject) => {
resolve(window);
});
`, {url});
}).then(bail, (error) => {
is(error.message, 'Value returned (resolve) by promise is not a valid JSON object', `scriptId: ${scriptId++}`);
return iframe.executeScript('window.btoa("a")', {url})
}, bail).then(rv => {
ok(c(rv, 'YQ=='), `scriptId: ${scriptId++}`);
return iframe.executeScript('window.wrappedJSObject.btoa("a")', {url})
}, bail).then(bail, (error) => {
is(error.message, `TypeError: window.wrappedJSObject is undefined; can't access its "btoa" property`, `scriptId: ${scriptId++}`);
return iframe.executeScript('42', {})
}).then(bail, error => {
is(error.name, 'InvalidAccessError', `scriptId: ${scriptId++}`);
return iframe.executeScript('42');
}).then(bail, error => {
is(error.name, 'InvalidAccessError', `scriptId: ${scriptId++}`);
return iframe.executeScript('43', { url: 'http://foo.com' });
}).then(bail, (error) => {
is(error.message, 'URL mismatches', `scriptId: ${scriptId++}`);
return iframe.executeScript('43', { url: '_' });
}, bail).then(bail, (error) => {
is(error.message, 'Malformed URL', `scriptId: ${scriptId++}`);
return iframe.executeScript('43', { origin: 'http://foo.com' });
}, bail).then(bail, (error) => {
is(error.message, 'Origin mismatches', `scriptId: ${scriptId++}`);
return iframe.executeScript('43', { origin: 'https://example.org' });
}, bail).then(bail, (error) => {
is(error.message, 'Origin mismatches', `scriptId: ${scriptId++}`);
SimpleTest.finish();
});
}
}
addEventListener('testready', runTest);

Просмотреть файл

@ -7,12 +7,10 @@ support-files =
browserElementTestHelpers.js
browserElement_BackForward.js
browserElement_DocumentFirstPaint.js
browserElement_ExecuteScript.js
browserElement_getWebManifest.js
browserElement_ReloadPostRequest.js
browserElement_SendEvent.js
browserElement_Stop.js
file_browserElement_ExecuteScript.html
file_browserElement_SendEvent.html
file_bug709759.sjs
file_empty.html
@ -23,7 +21,6 @@ support-files =
[test_browserElement_inproc_BackForward.html]
[test_browserElement_inproc_DocumentFirstPaint.html]
[test_browserElement_inproc_ExecuteScript.html]
[test_browserElement_inproc_getWebManifest.html]
[test_browserElement_inproc_ReloadPostRequest.html]
disabled = no modal prompt on POST reload for chrome window

Просмотреть файл

@ -1,8 +0,0 @@
<html>
<head>
<script>
window.btoa = () => "fake btoa";
</script>
</head>
<body>foo</body>
</html>

Просмотреть файл

@ -3,8 +3,6 @@
# can we reenable these tests on Android and e10s?
skip-if = os == "android" || e10s
support-files =
file_browserElement_ExecuteScript.html
browserElement_ExecuteScript.js
browserElement_OpenTab.js
[test_browserElement_oop_Viewmode.html]
@ -24,8 +22,6 @@ subsuite = clipboard
[test_browserElement_oop_DocumentFirstPaint.html]
disabled = Disabling some OOP tests for WebIDL scope changes (bug 1310706 for re-enabling)
[test_browserElement_oop_ErrorSecurity.html]
[test_browserElement_oop_ExecuteScript.html]
disabled = Disabling some OOP tests for WebIDL scope changes (bug 1310706 for re-enabling)
[test_browserElement_oop_FirstPaint.html]
[test_browserElement_oop_ForwardName.html]
[test_browserElement_oop_FrameWrongURI.html]

Просмотреть файл

@ -1,18 +0,0 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1174733
-->
<head>
<title>Test for Bug 1163961</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1174733">Mozilla Bug 1174733</a>
<script type="application/javascript" src="browserElement_ExecuteScript.js">
</script>
</body>
</html>

Просмотреть файл

@ -1,18 +0,0 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1174733
-->
<head>
<title>Test for Bug 1163961</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1174733">Mozilla Bug 1174733</a>
<script type="application/javascript" src="browserElement_ExecuteScript.js">
</script>
</body>
</html>

Просмотреть файл

@ -53,8 +53,6 @@ interface nsIBrowserElementAPI : nsISupports
DOMRequest getCanGoBack();
DOMRequest getCanGoForward();
DOMRequest executeScript(in AString script, in jsval options);
/**
* Returns an object that represents a Web Manifest:
* http://w3c.github.io/manifest/

Просмотреть файл

@ -208,44 +208,6 @@ nsBrowserElement::GetCanGoForward(ErrorResult& aRv)
return req.forget();
}
already_AddRefed<DOMRequest>
nsBrowserElement::ExecuteScript(const nsAString& aScript,
const BrowserElementExecuteScriptOptions& aOptions,
ErrorResult& aRv)
{
NS_ENSURE_TRUE(IsBrowserElementOrThrow(aRv), nullptr);
RefPtr<DOMRequest> req;
nsCOMPtr<nsIXPConnectWrappedJS> wrappedObj = do_QueryInterface(mBrowserElementAPI);
MOZ_ASSERT(wrappedObj, "Failed to get wrapped JS from XPCOM component.");
MOZ_RELEASE_ASSERT(!js::IsWrapper(wrappedObj->GetJSObject()));
AutoJSAPI jsapi;
if (!jsapi.Init(wrappedObj->GetJSObject())) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JS::Value> options(cx);
aRv.MightThrowJSException();
if (!ToJSValue(cx, aOptions, &options)) {
aRv.StealExceptionFromJSContext(cx);
return nullptr;
}
nsresult rv = mBrowserElementAPI->ExecuteScript(aScript, options, getter_AddRefs(req));
if (NS_FAILED(rv)) {
if (rv == NS_ERROR_INVALID_ARG) {
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
} else {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
}
return nullptr;
}
return req.forget();
}
already_AddRefed<DOMRequest>
nsBrowserElement::GetWebManifest(ErrorResult& aRv)
{

Просмотреть файл

@ -17,7 +17,6 @@ class nsFrameLoader;
namespace mozilla {
namespace dom {
struct BrowserElementExecuteScriptOptions;
class DOMRequest;
} // namespace dom
@ -58,10 +57,6 @@ public:
already_AddRefed<dom::DOMRequest> GetCanGoBack(ErrorResult& aRv);
already_AddRefed<dom::DOMRequest> GetCanGoForward(ErrorResult& aRv);
already_AddRefed<dom::DOMRequest> ExecuteScript(const nsAString& aScript,
const dom::BrowserElementExecuteScriptOptions& aOptions,
ErrorResult& aRv);
already_AddRefed<dom::DOMRequest> GetWebManifest(ErrorResult& aRv);
protected:

Просмотреть файл

@ -4,11 +4,6 @@
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
dictionary BrowserElementExecuteScriptOptions {
DOMString? url;
DOMString? origin;
};
[NoInterfaceObject]
interface BrowserElement {
};
@ -72,12 +67,6 @@ interface BrowserElementPrivileged {
ChromeOnly]
DOMRequest getCanGoForward();
[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
DOMRequest executeScript(DOMString script,
optional BrowserElementExecuteScriptOptions options);
[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]