Bug 1068186: Update window.sidebar and window.external APIs to support e10s. r=felipe

Moves nsSidebar.js to toolkit and makes it just pass messages to chrome for each
API call. MainProcessSingleton listens for those messages and passes the call
along to the search service.
Combines the validation code into the same function and takes the opportunity to
support relative URLs too.
Adds a bunch of tests for these web APIs.

Also fixes:
Bug 518929: Implement window.external APIs in core code
Bug 530847: Remove Fennec's nsISidebar implementation once that code is moved into the core
Bug 517720: Adding a search engine using relative URIs is not supported
This commit is contained in:
Dave Townsend
2015-02-02 12:15:26 -08:00
parent a2b19a06d8
commit bb7ee9f2fb
15 changed files with 318 additions and 284 deletions

View File

@@ -4,9 +4,7 @@
"use strict";
const Cu = Components.utils;
const Ci = Components.interfaces;
const Cc = Components.classes;
const { utils: Cu, interfaces: Ci, classes: Cc, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
@@ -19,18 +17,67 @@ XPCOMUtils.defineLazyServiceGetter(this, "globalmm",
"@mozilla.org/globalmessagemanager;1",
"nsIMessageBroadcaster");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
function MainProcessSingleton() {}
MainProcessSingleton.prototype = {
classID: Components.ID("{0636a680-45cb-11e4-916c-0800200c9a66}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
Ci.nsISupportsWeakReference]),
receiveMessage: function(message) {
logConsoleMessage: function(message) {
let logMsg = message.data;
logMsg.wrappedJSObject = logMsg;
Services.obs.notifyObservers(logMsg, "console-api-log-event", null);
},
// Called when a webpage calls either window.external.AddSearchProvider or
// window.sidebar.addSearchEngine
addSearchEngine: function({ target: browser, data: { pageURL, engineURL, iconURL, type } }) {
pageURL = NetUtil.newURI(pageURL);
engineURL = NetUtil.newURI(engineURL, null, pageURL);
if (iconURL) {
iconURL = NetUtil.newURI(iconURL, null, pageURL);
}
else {
let tabbrowser = browser.getTabBrowser();
if (browser.mIconURL && (!tabbrowser || tabbrowser.shouldLoadFavIcon(pageURL)))
iconURL = NetUtil.newURI(browser.mIconURL);
}
try {
// Make sure the URLs are HTTP, HTTPS, or FTP.
let isWeb = ["https", "http", "ftp"];
if (isWeb.indexOf(engineURL.scheme) < 0)
throw "Unsupported search engine URL: " + engineURL;
if (iconURL && isWeb.indexOf(iconURL.scheme) < 0)
throw "Unsupported search icon URL: " + iconURL;
}
catch(ex) {
Cu.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex);
var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
var brandName = brandBundle.GetStringFromName("brandShortName");
var title = searchBundle.GetStringFromName("error_invalid_engine_title");
var msg = searchBundle.formatStringFromName("error_invalid_engine_msg",
[brandName], 1);
Services.ww.getNewPrompter(browser.ownerDocument.defaultView).alert(title, msg);
return;
}
Services.search.init(function(status) {
if (status != Cr.NS_OK)
return;
Services.search.addEngine(engineURL.spec, type, iconURL ? iconURL.spec : null, true);
})
},
observe: function(subject, topic, data) {
switch (topic) {
case "app-startup": {
@@ -39,12 +86,14 @@ MainProcessSingleton.prototype = {
// Load this script early so that console.* is initialized
// before other frame scripts.
globalmm.loadFrameScript("chrome://global/content/browser-content.js", true);
ppmm.addMessageListener("Console:Log", this);
ppmm.addMessageListener("Console:Log", this.logConsoleMessage);
globalmm.addMessageListener("Search:AddEngine", this.addSearchEngine);
break;
}
case "xpcom-shutdown":
ppmm.removeMessageListener("Console:Log", this);
ppmm.removeMessageListener("Console:Log", this.logConsoleMessage);
globalmm.removeMessageListener("Search:AddEngine", this.addSearchEngine);
break;
}
},