browser/components/sidebar/nsSidebar.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 8
michael@0 9 const DEBUG = false; /* set to false to suppress debug messages */
michael@0 10
michael@0 11 const SIDEBAR_CONTRACTID = "@mozilla.org/sidebar;1";
michael@0 12 const SIDEBAR_CID = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
michael@0 13
michael@0 14 // File extension for Sherlock search plugin description files
michael@0 15 const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i;
michael@0 16
michael@0 17 function nsSidebar()
michael@0 18 {
michael@0 19 }
michael@0 20
michael@0 21 nsSidebar.prototype.classID = SIDEBAR_CID;
michael@0 22
michael@0 23 nsSidebar.prototype.validateSearchEngine =
michael@0 24 function (engineURL, iconURL)
michael@0 25 {
michael@0 26 try
michael@0 27 {
michael@0 28 // Make sure the URLs are HTTP, HTTPS, or FTP.
michael@0 29 var isWeb = /^(https?|ftp):\/\//i;
michael@0 30
michael@0 31 if (!isWeb.test(engineURL))
michael@0 32 throw "Unsupported search engine URL";
michael@0 33
michael@0 34 if (iconURL && !isWeb.test(iconURL))
michael@0 35 throw "Unsupported search icon URL.";
michael@0 36 }
michael@0 37 catch(ex)
michael@0 38 {
michael@0 39 debug(ex);
michael@0 40 Components.utils.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex);
michael@0 41
michael@0 42 var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
michael@0 43 var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
michael@0 44 var brandName = brandBundle.GetStringFromName("brandShortName");
michael@0 45 var title = searchBundle.GetStringFromName("error_invalid_engine_title");
michael@0 46 var msg = searchBundle.formatStringFromName("error_invalid_engine_msg",
michael@0 47 [brandName], 1);
michael@0 48 Services.ww.getNewPrompter(null).alert(title, msg);
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 return true;
michael@0 53 }
michael@0 54
michael@0 55 // The suggestedTitle and suggestedCategory parameters are ignored, but remain
michael@0 56 // for backward compatibility.
michael@0 57 nsSidebar.prototype.addSearchEngine =
michael@0 58 function (engineURL, iconURL, suggestedTitle, suggestedCategory)
michael@0 59 {
michael@0 60 debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
michael@0 61 suggestedCategory + ", " + suggestedTitle + ")");
michael@0 62
michael@0 63 if (!this.validateSearchEngine(engineURL, iconURL))
michael@0 64 return;
michael@0 65
michael@0 66 // OpenSearch files will likely be far more common than Sherlock files, and
michael@0 67 // have less consistent suffixes, so we assume that ".src" is a Sherlock
michael@0 68 // (text) file, and anything else is OpenSearch (XML).
michael@0 69 var dataType;
michael@0 70 if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL))
michael@0 71 dataType = Components.interfaces.nsISearchEngine.DATA_TEXT;
michael@0 72 else
michael@0 73 dataType = Components.interfaces.nsISearchEngine.DATA_XML;
michael@0 74
michael@0 75 Services.search.addEngine(engineURL, dataType, iconURL, true);
michael@0 76 }
michael@0 77
michael@0 78 // This function exists largely to implement window.external.AddSearchProvider(),
michael@0 79 // to match other browsers' APIs. The capitalization, although nonstandard here,
michael@0 80 // is therefore important.
michael@0 81 nsSidebar.prototype.AddSearchProvider =
michael@0 82 function (aDescriptionURL)
michael@0 83 {
michael@0 84 // Get the favicon URL for the current page, or our best guess at the current
michael@0 85 // page since we don't have easy access to the active document. Most search
michael@0 86 // engines will override this with an icon specified in the OpenSearch
michael@0 87 // description anyway.
michael@0 88 var win = Services.wm.getMostRecentWindow("navigator:browser");
michael@0 89 var browser = win.gBrowser;
michael@0 90 var iconURL = "";
michael@0 91 // Use documentURIObject in the check for shouldLoadFavIcon so that we
michael@0 92 // do the right thing with about:-style error pages. Bug 453442
michael@0 93 if (browser.shouldLoadFavIcon(browser.selectedBrowser
michael@0 94 .contentDocument
michael@0 95 .documentURIObject))
michael@0 96 iconURL = browser.getIcon();
michael@0 97
michael@0 98 if (!this.validateSearchEngine(aDescriptionURL, iconURL))
michael@0 99 return;
michael@0 100
michael@0 101 const typeXML = Components.interfaces.nsISearchEngine.DATA_XML;
michael@0 102 Services.search.addEngine(aDescriptionURL, typeXML, iconURL, true);
michael@0 103 }
michael@0 104
michael@0 105 // This function exists to implement window.external.IsSearchProviderInstalled(),
michael@0 106 // for compatibility with other browsers. It will return an integer value
michael@0 107 // indicating whether the given engine is installed for the current user.
michael@0 108 // However, it is currently stubbed out due to security/privacy concerns
michael@0 109 // stemming from difficulties in determining what domain issued the request.
michael@0 110 // See bug 340604 and
michael@0 111 // http://msdn.microsoft.com/en-us/library/aa342526%28VS.85%29.aspx .
michael@0 112 // XXX Implement this!
michael@0 113 nsSidebar.prototype.IsSearchProviderInstalled =
michael@0 114 function (aSearchURL)
michael@0 115 {
michael@0 116 return 0;
michael@0 117 }
michael@0 118
michael@0 119 nsSidebar.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsISupports]);
michael@0 120
michael@0 121 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]);
michael@0 122
michael@0 123 /* static functions */
michael@0 124 if (DEBUG)
michael@0 125 debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
michael@0 126 else
michael@0 127 debug = function (s) {}

mercurial