michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const SIDEBAR_CID = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}"); michael@0: const SIDEBAR_CONTRACTID = "@mozilla.org/sidebar;1"; michael@0: michael@0: function Sidebar() { michael@0: // Depending on if we are in the parent or child, prepare to remote michael@0: // certain calls michael@0: var appInfo = Cc["@mozilla.org/xre/app-info;1"]; michael@0: if (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) { michael@0: // Parent process michael@0: michael@0: this.inContentProcess = false; michael@0: michael@0: // Used for wakeups service. FIXME: clean up with bug 593407 michael@0: this.wrappedJSObject = this; michael@0: michael@0: // Setup listener for child messages. We don't need to call michael@0: // addMessageListener as the wakeup service will do that for us. michael@0: this.receiveMessage = function(aMessage) { michael@0: switch (aMessage.name) { michael@0: case "Sidebar:AddSearchProvider": michael@0: this.AddSearchProvider(aMessage.json.descriptionURL); michael@0: } michael@0: }; michael@0: } else { michael@0: // Child process michael@0: michael@0: this.inContentProcess = true; michael@0: this.messageManager = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); michael@0: } michael@0: } michael@0: michael@0: Sidebar.prototype = { michael@0: // =========================== utility code =========================== michael@0: _validateSearchEngine: function validateSearchEngine(engineURL, iconURL) { michael@0: try { michael@0: // Make sure we're using HTTP, HTTPS, or FTP. michael@0: if (! /^(https?|ftp):\/\//i.test(engineURL)) michael@0: throw "Unsupported search engine URL"; michael@0: michael@0: // Make sure we're using HTTP, HTTPS, or FTP and refering to a michael@0: // .gif/.jpg/.jpeg/.png/.ico file for the icon. michael@0: if (iconURL && michael@0: ! /^(https?|ftp):\/\/.+\.(gif|jpg|jpeg|png|ico)$/i.test(iconURL)) michael@0: throw "Unsupported search icon URL."; michael@0: } catch(ex) { michael@0: Cu.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex); michael@0: michael@0: var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties"); michael@0: var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties"); michael@0: var brandName = brandBundle.GetStringFromName("brandShortName"); michael@0: var title = searchBundle.GetStringFromName("error_invalid_engine_title"); michael@0: var msg = searchBundle.formatStringFromName("error_invalid_engine_msg", michael@0: [brandName], 1); michael@0: Services.prompt.alert(null, title, msg); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: // The suggestedTitle and suggestedCategory parameters are ignored, but remain michael@0: // for backward compatibility. michael@0: addSearchEngine: function addSearchEngine(engineURL, iconURL, suggestedTitle, michael@0: suggestedCategory) { michael@0: if (!this._validateSearchEngine(engineURL, iconURL)) michael@0: return; michael@0: michael@0: // File extension for Sherlock search plugin description files michael@0: const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i; michael@0: michael@0: // OpenSearch files will likely be far more common than Sherlock files, and michael@0: // have less consistent suffixes, so we assume that ".src" is a Sherlock michael@0: // (text) file, and anything else is OpenSearch (XML). michael@0: var dataType; michael@0: if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL)) michael@0: dataType = Ci.nsISearchEngine.DATA_TEXT; michael@0: else michael@0: dataType = Ci.nsISearchEngine.DATA_XML; michael@0: michael@0: Services.search.addEngine(engineURL, dataType, iconURL, true); michael@0: }, michael@0: michael@0: // This function exists to implement window.external.AddSearchProvider(), michael@0: // to match other browsers' APIs. The capitalization, although nonstandard here, michael@0: // is therefore important. michael@0: AddSearchProvider: function AddSearchProvider(aDescriptionURL) { michael@0: if (!this._validateSearchEngine(aDescriptionURL, "")) michael@0: return; michael@0: michael@0: if (this.inContentProcess) { michael@0: this.messageManager.sendAsyncMessage("Sidebar:AddSearchProvider", michael@0: { descriptionURL: aDescriptionURL }); michael@0: return; michael@0: } michael@0: michael@0: const typeXML = Ci.nsISearchEngine.DATA_XML; michael@0: Services.search.addEngine(aDescriptionURL, typeXML, "", true); michael@0: }, michael@0: michael@0: // This function exists to implement window.external.IsSearchProviderInstalled(), michael@0: // for compatibility with other browsers. It will return an integer value michael@0: // indicating whether the given engine is installed for the current user. michael@0: // However, it is currently stubbed out due to security/privacy concerns michael@0: // stemming from difficulties in determining what domain issued the request. michael@0: // See bug 340604 and michael@0: // http://msdn.microsoft.com/en-us/library/aa342526%28VS.85%29.aspx . michael@0: // XXX Implement this! michael@0: IsSearchProviderInstalled: function IsSearchProviderInstalled(aSearchURL) { michael@0: return 0; michael@0: }, michael@0: michael@0: // =========================== nsISupports =========================== michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), michael@0: michael@0: // XPCOMUtils stuff michael@0: classID: SIDEBAR_CID, michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Sidebar]);