Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const Ci = Components.interfaces; |
michael@0 | 6 | const Cc = Components.classes; |
michael@0 | 7 | const Cu = Components.utils; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const SIDEBAR_CID = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}"); |
michael@0 | 13 | const SIDEBAR_CONTRACTID = "@mozilla.org/sidebar;1"; |
michael@0 | 14 | |
michael@0 | 15 | function Sidebar() { |
michael@0 | 16 | // Depending on if we are in the parent or child, prepare to remote |
michael@0 | 17 | // certain calls |
michael@0 | 18 | var appInfo = Cc["@mozilla.org/xre/app-info;1"]; |
michael@0 | 19 | if (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) { |
michael@0 | 20 | // Parent process |
michael@0 | 21 | |
michael@0 | 22 | this.inContentProcess = false; |
michael@0 | 23 | |
michael@0 | 24 | // Used for wakeups service. FIXME: clean up with bug 593407 |
michael@0 | 25 | this.wrappedJSObject = this; |
michael@0 | 26 | |
michael@0 | 27 | // Setup listener for child messages. We don't need to call |
michael@0 | 28 | // addMessageListener as the wakeup service will do that for us. |
michael@0 | 29 | this.receiveMessage = function(aMessage) { |
michael@0 | 30 | switch (aMessage.name) { |
michael@0 | 31 | case "Sidebar:AddSearchProvider": |
michael@0 | 32 | this.AddSearchProvider(aMessage.json.descriptionURL); |
michael@0 | 33 | } |
michael@0 | 34 | }; |
michael@0 | 35 | } else { |
michael@0 | 36 | // Child process |
michael@0 | 37 | |
michael@0 | 38 | this.inContentProcess = true; |
michael@0 | 39 | this.messageManager = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); |
michael@0 | 40 | } |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | Sidebar.prototype = { |
michael@0 | 44 | // =========================== utility code =========================== |
michael@0 | 45 | _validateSearchEngine: function validateSearchEngine(engineURL, iconURL) { |
michael@0 | 46 | try { |
michael@0 | 47 | // Make sure we're using HTTP, HTTPS, or FTP. |
michael@0 | 48 | if (! /^(https?|ftp):\/\//i.test(engineURL)) |
michael@0 | 49 | throw "Unsupported search engine URL"; |
michael@0 | 50 | |
michael@0 | 51 | // Make sure we're using HTTP, HTTPS, or FTP and refering to a |
michael@0 | 52 | // .gif/.jpg/.jpeg/.png/.ico file for the icon. |
michael@0 | 53 | if (iconURL && |
michael@0 | 54 | ! /^(https?|ftp):\/\/.+\.(gif|jpg|jpeg|png|ico)$/i.test(iconURL)) |
michael@0 | 55 | throw "Unsupported search icon URL."; |
michael@0 | 56 | } catch(ex) { |
michael@0 | 57 | Cu.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex); |
michael@0 | 58 | |
michael@0 | 59 | var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties"); |
michael@0 | 60 | var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties"); |
michael@0 | 61 | var brandName = brandBundle.GetStringFromName("brandShortName"); |
michael@0 | 62 | var title = searchBundle.GetStringFromName("error_invalid_engine_title"); |
michael@0 | 63 | var msg = searchBundle.formatStringFromName("error_invalid_engine_msg", |
michael@0 | 64 | [brandName], 1); |
michael@0 | 65 | Services.prompt.alert(null, title, msg); |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | return true; |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | // The suggestedTitle and suggestedCategory parameters are ignored, but remain |
michael@0 | 73 | // for backward compatibility. |
michael@0 | 74 | addSearchEngine: function addSearchEngine(engineURL, iconURL, suggestedTitle, |
michael@0 | 75 | suggestedCategory) { |
michael@0 | 76 | if (!this._validateSearchEngine(engineURL, iconURL)) |
michael@0 | 77 | return; |
michael@0 | 78 | |
michael@0 | 79 | // File extension for Sherlock search plugin description files |
michael@0 | 80 | const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i; |
michael@0 | 81 | |
michael@0 | 82 | // OpenSearch files will likely be far more common than Sherlock files, and |
michael@0 | 83 | // have less consistent suffixes, so we assume that ".src" is a Sherlock |
michael@0 | 84 | // (text) file, and anything else is OpenSearch (XML). |
michael@0 | 85 | var dataType; |
michael@0 | 86 | if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL)) |
michael@0 | 87 | dataType = Ci.nsISearchEngine.DATA_TEXT; |
michael@0 | 88 | else |
michael@0 | 89 | dataType = Ci.nsISearchEngine.DATA_XML; |
michael@0 | 90 | |
michael@0 | 91 | Services.search.addEngine(engineURL, dataType, iconURL, true); |
michael@0 | 92 | }, |
michael@0 | 93 | |
michael@0 | 94 | // This function exists to implement window.external.AddSearchProvider(), |
michael@0 | 95 | // to match other browsers' APIs. The capitalization, although nonstandard here, |
michael@0 | 96 | // is therefore important. |
michael@0 | 97 | AddSearchProvider: function AddSearchProvider(aDescriptionURL) { |
michael@0 | 98 | if (!this._validateSearchEngine(aDescriptionURL, "")) |
michael@0 | 99 | return; |
michael@0 | 100 | |
michael@0 | 101 | if (this.inContentProcess) { |
michael@0 | 102 | this.messageManager.sendAsyncMessage("Sidebar:AddSearchProvider", |
michael@0 | 103 | { descriptionURL: aDescriptionURL }); |
michael@0 | 104 | return; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | const typeXML = Ci.nsISearchEngine.DATA_XML; |
michael@0 | 108 | Services.search.addEngine(aDescriptionURL, typeXML, "", true); |
michael@0 | 109 | }, |
michael@0 | 110 | |
michael@0 | 111 | // This function exists to implement window.external.IsSearchProviderInstalled(), |
michael@0 | 112 | // for compatibility with other browsers. It will return an integer value |
michael@0 | 113 | // indicating whether the given engine is installed for the current user. |
michael@0 | 114 | // However, it is currently stubbed out due to security/privacy concerns |
michael@0 | 115 | // stemming from difficulties in determining what domain issued the request. |
michael@0 | 116 | // See bug 340604 and |
michael@0 | 117 | // http://msdn.microsoft.com/en-us/library/aa342526%28VS.85%29.aspx . |
michael@0 | 118 | // XXX Implement this! |
michael@0 | 119 | IsSearchProviderInstalled: function IsSearchProviderInstalled(aSearchURL) { |
michael@0 | 120 | return 0; |
michael@0 | 121 | }, |
michael@0 | 122 | |
michael@0 | 123 | // =========================== nsISupports =========================== |
michael@0 | 124 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), |
michael@0 | 125 | |
michael@0 | 126 | // XPCOMUtils stuff |
michael@0 | 127 | classID: SIDEBAR_CID, |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Sidebar]); |