browser/components/sidebar/nsSidebar.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sidebar/nsSidebar.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.10 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.11 +
    1.12 +const DEBUG = false; /* set to false to suppress debug messages */
    1.13 +
    1.14 +const SIDEBAR_CONTRACTID        = "@mozilla.org/sidebar;1";
    1.15 +const SIDEBAR_CID               = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
    1.16 +
    1.17 +// File extension for Sherlock search plugin description files
    1.18 +const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i;
    1.19 +
    1.20 +function nsSidebar()
    1.21 +{
    1.22 +}
    1.23 +
    1.24 +nsSidebar.prototype.classID = SIDEBAR_CID;
    1.25 +
    1.26 +nsSidebar.prototype.validateSearchEngine =
    1.27 +function (engineURL, iconURL)
    1.28 +{
    1.29 +  try
    1.30 +  {
    1.31 +    // Make sure the URLs are HTTP, HTTPS, or FTP.
    1.32 +    var isWeb = /^(https?|ftp):\/\//i;
    1.33 +
    1.34 +    if (!isWeb.test(engineURL))
    1.35 +      throw "Unsupported search engine URL";
    1.36 +
    1.37 +    if (iconURL && !isWeb.test(iconURL))
    1.38 +      throw "Unsupported search icon URL.";
    1.39 +  }
    1.40 +  catch(ex)
    1.41 +  {
    1.42 +    debug(ex);
    1.43 +    Components.utils.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex);
    1.44 +
    1.45 +    var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
    1.46 +    var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
    1.47 +    var brandName = brandBundle.GetStringFromName("brandShortName");
    1.48 +    var title = searchBundle.GetStringFromName("error_invalid_engine_title");
    1.49 +    var msg = searchBundle.formatStringFromName("error_invalid_engine_msg",
    1.50 +                                                [brandName], 1);
    1.51 +    Services.ww.getNewPrompter(null).alert(title, msg);
    1.52 +    return false;
    1.53 +  }
    1.54 +
    1.55 +  return true;
    1.56 +}
    1.57 +
    1.58 +// The suggestedTitle and suggestedCategory parameters are ignored, but remain
    1.59 +// for backward compatibility.
    1.60 +nsSidebar.prototype.addSearchEngine =
    1.61 +function (engineURL, iconURL, suggestedTitle, suggestedCategory)
    1.62 +{
    1.63 +  debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
    1.64 +        suggestedCategory + ", " + suggestedTitle + ")");
    1.65 +
    1.66 +  if (!this.validateSearchEngine(engineURL, iconURL))
    1.67 +    return;
    1.68 +
    1.69 +  // OpenSearch files will likely be far more common than Sherlock files, and
    1.70 +  // have less consistent suffixes, so we assume that ".src" is a Sherlock
    1.71 +  // (text) file, and anything else is OpenSearch (XML).
    1.72 +  var dataType;
    1.73 +  if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL))
    1.74 +    dataType = Components.interfaces.nsISearchEngine.DATA_TEXT;
    1.75 +  else
    1.76 +    dataType = Components.interfaces.nsISearchEngine.DATA_XML;
    1.77 +
    1.78 +  Services.search.addEngine(engineURL, dataType, iconURL, true);
    1.79 +}
    1.80 +
    1.81 +// This function exists largely to implement window.external.AddSearchProvider(),
    1.82 +// to match other browsers' APIs.  The capitalization, although nonstandard here,
    1.83 +// is therefore important.
    1.84 +nsSidebar.prototype.AddSearchProvider =
    1.85 +function (aDescriptionURL)
    1.86 +{
    1.87 +  // Get the favicon URL for the current page, or our best guess at the current
    1.88 +  // page since we don't have easy access to the active document.  Most search
    1.89 +  // engines will override this with an icon specified in the OpenSearch
    1.90 +  // description anyway.
    1.91 +  var win = Services.wm.getMostRecentWindow("navigator:browser");
    1.92 +  var browser = win.gBrowser;
    1.93 +  var iconURL = "";
    1.94 +  // Use documentURIObject in the check for shouldLoadFavIcon so that we
    1.95 +  // do the right thing with about:-style error pages.  Bug 453442
    1.96 +  if (browser.shouldLoadFavIcon(browser.selectedBrowser
    1.97 +                                       .contentDocument
    1.98 +                                       .documentURIObject))
    1.99 +    iconURL = browser.getIcon();
   1.100 +
   1.101 +  if (!this.validateSearchEngine(aDescriptionURL, iconURL))
   1.102 +    return;
   1.103 +
   1.104 +  const typeXML = Components.interfaces.nsISearchEngine.DATA_XML;
   1.105 +  Services.search.addEngine(aDescriptionURL, typeXML, iconURL, true);
   1.106 +}
   1.107 +
   1.108 +// This function exists to implement window.external.IsSearchProviderInstalled(),
   1.109 +// for compatibility with other browsers.  It will return an integer value
   1.110 +// indicating whether the given engine is installed for the current user.
   1.111 +// However, it is currently stubbed out due to security/privacy concerns
   1.112 +// stemming from difficulties in determining what domain issued the request.
   1.113 +// See bug 340604 and
   1.114 +// http://msdn.microsoft.com/en-us/library/aa342526%28VS.85%29.aspx .
   1.115 +// XXX Implement this!
   1.116 +nsSidebar.prototype.IsSearchProviderInstalled =
   1.117 +function (aSearchURL)
   1.118 +{
   1.119 +  return 0;
   1.120 +}
   1.121 +
   1.122 +nsSidebar.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsISupports]);
   1.123 +
   1.124 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]);
   1.125 +
   1.126 +/* static functions */
   1.127 +if (DEBUG)
   1.128 +    debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
   1.129 +else
   1.130 +    debug = function (s) {}

mercurial