From 5d8fab206648dc918bdf8032f3f23bb4c287d1b3 Mon Sep 17 00:00:00 2001 From: Mark Finkle Date: Wed, 15 Oct 2014 00:32:15 -0400 Subject: [PATCH] Bug 1035420 - [geckoview] Implement chrome script injection r=bnicholson --- mobile/android/base/GeckoView.java | 9 ++++ mobile/android/chrome/content/EmbedRT.js | 64 ++++++++++++++++++++++++ mobile/android/chrome/content/browser.js | 1 + mobile/android/chrome/jar.mn | 1 + 4 files changed, 75 insertions(+) create mode 100644 mobile/android/chrome/content/EmbedRT.js diff --git a/mobile/android/base/GeckoView.java b/mobile/android/base/GeckoView.java index 733b2d46bf53..31e1a6ff7277 100644 --- a/mobile/android/base/GeckoView.java +++ b/mobile/android/base/GeckoView.java @@ -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(); diff --git a/mobile/android/chrome/content/EmbedRT.js b/mobile/android/chrome/content/EmbedRT.js new file mode 100644 index 000000000000..5ab86664a477 --- /dev/null +++ b/mobile/android/chrome/content/EmbedRT.js @@ -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); + } + } + } +}; diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js index 28e245480c8f..c07b180f0df6 100644 --- a/mobile/android/chrome/content/browser.js +++ b/mobile/android/chrome/content/browser.js @@ -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() { diff --git a/mobile/android/chrome/jar.mn b/mobile/android/chrome/jar.mn index 1f61db80a6a8..bcb130999b3b 100644 --- a/mobile/android/chrome/jar.mn +++ b/mobile/android/chrome/jar.mn @@ -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)