Bug 1035420 - [geckoview] Implement chrome script injection r=bnicholson

This commit is contained in:
Mark Finkle 2014-10-15 00:32:15 -04:00
parent 186ae6f1fb
commit 5c5e97bddc
4 changed files with 75 additions and 0 deletions

View File

@ -243,6 +243,15 @@ public class GeckoView extends LayerView
return Collections.unmodifiableList(browsers);
}
public void importScript(final String url) {
if (url.startsWith("resource://android/assets/")) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("GeckoView:ImportScript", url));
return;
}
throw new IllegalArgumentException("Must import script from 'resources://android/assets/' location.");
}
private void connectToGecko() {
GeckoThread.setLaunchState(GeckoThread.LaunchState.GeckoRunning);
Tab selectedTab = Tabs.getInstance().getSelectedTab();

View File

@ -0,0 +1,64 @@
/* 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/. */
"use strict";
XPCOMUtils.defineLazyModuleGetter(this, "ConsoleAPI",
"resource://gre/modules/devtools/Console.jsm");
/*
* Collection of methods and features specific to using a GeckoView instance.
* The code is isolated from browser.js for code size and performance reasons.
*/
var EmbedRT = {
_scopes: {},
observe: function(subject, topic, data) {
switch(topic) {
case "GeckoView:ImportScript":
this.importScript(data);
break;
}
},
/*
* Loads a script file into a sandbox and calls an optional load function
*/
importScript: function(scriptURL) {
if (scriptURL in this._scopes) {
return;
}
let principal = Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal);
let sandbox = new Cu.Sandbox(principal,
{
sandboxName: scriptURL,
wantGlobalProperties: ["indexedDB"]
}
);
sandbox["console"] = new ConsoleAPI({ consoleID: "script/" + scriptURL });
// As we don't want our caller to control the JS version used for the
// script file, we run loadSubScript within the context of the
// sandbox with the latest JS version set explicitly.
sandbox.__SCRIPT_URI_SPEC__ = scriptURL;
Cu.evalInSandbox("Components.classes['@mozilla.org/moz/jssubscript-loader;1'].createInstance(Components.interfaces.mozIJSSubScriptLoader).loadSubScript(__SCRIPT_URI_SPEC__);", sandbox, "ECMAv5");
this._scopes[scriptURL] = sandbox;
if ("load" in sandbox) {
let params = {
window: window,
resourceURI: scriptURL,
};
try {
sandbox["load"](params);
} catch(e) {
dump("Exception calling 'load' method in script: " + scriptURL + "\n" + e);
}
}
}
};

View File

@ -133,6 +133,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "SharedPreferences",
["Feedback", ["Feedback:Show"], "chrome://browser/content/Feedback.js"],
["SelectionHandler", ["TextSelection:Get"], "chrome://browser/content/SelectionHandler.js"],
["Notifications", ["Notification:Event"], "chrome://browser/content/Notifications.jsm"],
["EmbedRT", ["GeckoView:ImportScript"], "chrome://browser/content/EmbedRT.js"],
].forEach(function (aScript) {
let [name, notifications, script] = aScript;
XPCOMUtils.defineLazyGetter(window, name, function() {

View File

@ -39,6 +39,7 @@ chrome.jar:
content/SelectHelper.js (content/SelectHelper.js)
content/SelectionHandler.js (content/SelectionHandler.js)
* content/WebappRT.js (content/WebappRT.js)
content/EmbedRT.js (content/EmbedRT.js)
content/InputWidgetHelper.js (content/InputWidgetHelper.js)
content/WebrtcUI.js (content/WebrtcUI.js)
content/MemoryObserver.js (content/MemoryObserver.js)