browser/metro/components/BrowserCLH.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/components/BrowserCLH.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,285 @@
     1.4 +/* -*- Mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil; -*- */
     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 +const Cc = Components.classes;
    1.10 +const Ci = Components.interfaces;
    1.11 +const Cu = Components.utils;
    1.12 +const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +
    1.17 +function openWindow(aParent, aURL, aTarget, aFeatures, aArgs) {
    1.18 +  let argString = null;
    1.19 +  if (aArgs && !(aArgs instanceof Ci.nsISupportsArray)) {
    1.20 +    argString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    1.21 +    argString.data = aArgs;
    1.22 +  }
    1.23 +
    1.24 +  return Services.ww.openWindow(aParent, aURL, aTarget, aFeatures, argString || aArgs);
    1.25 +}
    1.26 +
    1.27 +function resolveURIInternal(aCmdLine, aArgument) {
    1.28 +  let uri = aCmdLine.resolveURI(aArgument);
    1.29 +
    1.30 +  if (!(uri instanceof Ci.nsIFileURL))
    1.31 +    return uri;
    1.32 +
    1.33 +  try {
    1.34 +    if (uri.file.exists())
    1.35 +      return uri;
    1.36 +  } catch (e) {
    1.37 +    Cu.reportError(e);
    1.38 +  }
    1.39 +
    1.40 +  try {
    1.41 +    let urifixup = Cc["@mozilla.org/docshell/urifixup;1"].getService(Ci.nsIURIFixup);
    1.42 +    uri = urifixup.createFixupURI(aArgument, 0);
    1.43 +  } catch (e) {
    1.44 +    Cu.reportError(e);
    1.45 +  }
    1.46 +
    1.47 +  return uri;
    1.48 +}
    1.49 +
    1.50 +/**
    1.51 + * Determines whether a home page override is needed.
    1.52 + * Returns:
    1.53 + *  "new profile" if this is the first run with a new profile.
    1.54 + *  "new version" if this is the first run with a build with a different
    1.55 + *                      Gecko milestone (i.e. right after an upgrade).
    1.56 + *  "none" otherwise.
    1.57 + */
    1.58 +function needHomepageOverride() {
    1.59 +  let savedmstone = null;
    1.60 +  try {
    1.61 +    savedmstone = Services.prefs.getCharPref("browser.startup.homepage_override.mstone");
    1.62 +  } catch (e) {}
    1.63 +
    1.64 +  if (savedmstone == "ignore")
    1.65 +    return "none";
    1.66 +
    1.67 +  let ourmstone = Services.appinfo.platformVersion;
    1.68 +
    1.69 +  if (ourmstone != savedmstone) {
    1.70 +    Services.prefs.setCharPref("browser.startup.homepage_override.mstone", ourmstone);
    1.71 +
    1.72 +    return (savedmstone ? "new version" : "new profile");
    1.73 +  }
    1.74 +
    1.75 +  return "none";
    1.76 +}
    1.77 +
    1.78 +function getHomePage() {
    1.79 +  let url = "about:newtab";
    1.80 +  try {
    1.81 +    url = Services.prefs.getComplexValue("browser.startup.homepage", Ci.nsIPrefLocalizedString).data;
    1.82 +  } catch (e) { }
    1.83 +
    1.84 +  return url;
    1.85 +}
    1.86 +
    1.87 +function showPanelWhenReady(aWindow, aPage) {
    1.88 +  aWindow.addEventListener("UIReadyDelayed", function(aEvent) {
    1.89 +    aWindow.PanelUI.show(aPage);
    1.90 +  }, false);
    1.91 +}
    1.92 +
    1.93 +function haveSystemLocale() {
    1.94 +  let localeService = Cc["@mozilla.org/intl/nslocaleservice;1"].getService(Ci.nsILocaleService);
    1.95 +  let systemLocale = localeService.getSystemLocale().getCategory("NSILOCALE_CTYPE");
    1.96 +  return isLocaleAvailable(systemLocale);
    1.97 +}
    1.98 +
    1.99 +function checkCurrentLocale() {
   1.100 +  if (Services.prefs.prefHasUserValue("general.useragent.locale")) {
   1.101 +    // if the user has a compatible locale from a different buildid, we need to update
   1.102 +    var buildID = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo).platformBuildID;
   1.103 +    let localeBuildID = Services.prefs.getCharPref("extensions.compatability.locales.buildid");
   1.104 +    if (buildID != localeBuildID)
   1.105 +      return false;
   1.106 +
   1.107 +    let currentLocale = Services.prefs.getCharPref("general.useragent.locale");
   1.108 +    return isLocaleAvailable(currentLocale);
   1.109 +  }
   1.110 +  return true;
   1.111 +}
   1.112 +
   1.113 +function isLocaleAvailable(aLocale) {
   1.114 +  let chrome = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry);
   1.115 +  chrome.QueryInterface(Ci.nsIToolkitChromeRegistry);
   1.116 +  let availableLocales = chrome.getLocalesForPackage("browser");
   1.117 + 
   1.118 +  let locale = aLocale.split("-")[0];
   1.119 +  let localeRegEx = new RegExp("^" + locale);
   1.120 + 
   1.121 +  while (availableLocales.hasMore()) {
   1.122 +    let locale = availableLocales.getNext();
   1.123 +    if (localeRegEx.test(locale))
   1.124 +      return true;
   1.125 +  }
   1.126 +  return false;
   1.127 +}
   1.128 +
   1.129 +function BrowserCLH() { }
   1.130 +
   1.131 +BrowserCLH.prototype = {
   1.132 +  //
   1.133 +  // nsICommandLineHandler
   1.134 +  //
   1.135 +  handle: function fs_handle(aCmdLine) {
   1.136 +#ifdef DEBUG
   1.137 +    for (var idx = 0; idx <  aCmdLine.length; idx++) {
   1.138 +      dump(aCmdLine.getArgument(idx) + "\n");
   1.139 +    }
   1.140 +#endif
   1.141 +    // Instantiate the search service so the search engine cache is created now
   1.142 +    // instead when the application is running. The install process will register
   1.143 +    // this component by using the -silent command line flag, thereby creating
   1.144 +    // the cache during install, not runtime.
   1.145 +    // NOTE: This code assumes this CLH is run before the nsDefaultCLH, which
   1.146 +    // consumes the "-silent" flag.
   1.147 +    if (aCmdLine.findFlag("silent", false) > -1) {
   1.148 +      let searchService = Services.search;
   1.149 +      let autoComplete = Cc["@mozilla.org/autocomplete/search;1?name=history"].
   1.150 +                         getService(Ci.nsIAutoCompleteSearch);
   1.151 +      return;
   1.152 +    }
   1.153 +
   1.154 +    // Handle chrome windows loaded via commandline
   1.155 +    let chromeParam = aCmdLine.handleFlagWithParam("chrome", false);
   1.156 +    if (chromeParam) {
   1.157 +      try {
   1.158 +        // Only load URIs which do not inherit chrome privs
   1.159 +        let features = "chrome,dialog=no,all";
   1.160 +        let uri = resolveURIInternal(aCmdLine, chromeParam);
   1.161 +        let netutil = Cc["@mozilla.org/network/util;1"].getService(Ci.nsINetUtil);
   1.162 +        if (!netutil.URIChainHasFlags(uri, Ci.nsIHttpProtocolHandler.URI_INHERITS_SECURITY_CONTEXT)) {
   1.163 +          openWindow(null, uri.spec, "_blank", features, null);
   1.164 +
   1.165 +          // Stop the normal commandline processing from continuing
   1.166 +          aCmdLine.preventDefault = true;
   1.167 +        }
   1.168 +      } catch (e) {
   1.169 +        Cu.reportError(e);
   1.170 +      }
   1.171 +      return;
   1.172 +    }
   1.173 +
   1.174 +    // Check and remove the alert flag here, but we'll handle it a bit later - see below
   1.175 +    let alertFlag = aCmdLine.handleFlagWithParam("alert", false);
   1.176 +
   1.177 +    // Check and remove the webapp param
   1.178 +    let appFlag = aCmdLine.handleFlagWithParam("webapp", false);
   1.179 +    let appURI;
   1.180 +    if (appFlag)
   1.181 +      appURI = resolveURIInternal(aCmdLine, appFlag);
   1.182 +
   1.183 +    // Keep an array of possible URL arguments
   1.184 +    let uris = [];
   1.185 +
   1.186 +    // Check for the "url" flag
   1.187 +    let uriFlag = aCmdLine.handleFlagWithParam("url", false);
   1.188 +    if (uriFlag) {
   1.189 +      let uri = resolveURIInternal(aCmdLine, uriFlag);
   1.190 +      if (uri)
   1.191 +        uris.push(uri);
   1.192 +    }
   1.193 +
   1.194 +    // Check for the "search" flag
   1.195 +    let searchParam = aCmdLine.handleFlagWithParam("search", false);
   1.196 +    if (searchParam) {
   1.197 +      var ss = Components.classes["@mozilla.org/browser/search-service;1"]
   1.198 +                         .getService(nsIBrowserSearchService);
   1.199 +      var submission = ss.defaultEngine.getSubmission(searchParam.replace("\"", "", "g"));
   1.200 +      uris.push(submission.uri);
   1.201 +    }
   1.202 +
   1.203 +    for (let i = 0; i < aCmdLine.length; i++) {
   1.204 +      let arg = aCmdLine.getArgument(i);
   1.205 +      if (!arg || arg[0] == '-')
   1.206 +        continue;
   1.207 +
   1.208 +      let uri = resolveURIInternal(aCmdLine, arg);
   1.209 +      if (uri)
   1.210 +        uris.push(uri);
   1.211 +    }
   1.212 +
   1.213 +    // Open the main browser window, if we don't already have one
   1.214 +    let browserWin;
   1.215 +    try {
   1.216 +      let localeWin = Services.wm.getMostRecentWindow("navigator:localepicker");
   1.217 +      if (localeWin) {
   1.218 +        localeWin.focus();
   1.219 +        aCmdLine.preventDefault = true;
   1.220 +        return;
   1.221 +      }
   1.222 +
   1.223 +      browserWin = Services.wm.getMostRecentWindow("navigator:browser");
   1.224 +      if (!browserWin) {
   1.225 +        // Default to the saved homepage
   1.226 +        let defaultURL = getHomePage();
   1.227 +
   1.228 +        // Override the default if we have a URL passed on command line
   1.229 +        if (uris.length > 0) {
   1.230 +          defaultURL = uris[0].spec;
   1.231 +          uris = uris.slice(1);
   1.232 +        }
   1.233 +
   1.234 +        // Show the locale selector if we have a new profile, or if the selected locale is no longer compatible
   1.235 +        let showLocalePicker = Services.prefs.getBoolPref("browser.firstrun.show.localepicker");
   1.236 +        if ((needHomepageOverride() == "new profile" && showLocalePicker && !haveSystemLocale())) { // || !checkCurrentLocale()) {
   1.237 +          browserWin = openWindow(null, "chrome://browser/content/localePicker.xul", "_blank", "chrome,dialog=no,all", defaultURL);
   1.238 +          aCmdLine.preventDefault = true;
   1.239 +          return;
   1.240 +        }
   1.241 +
   1.242 +        browserWin = openWindow(null, "chrome://browser/content/browser.xul", "_blank", "chrome,dialog=no,all", defaultURL);
   1.243 +      }
   1.244 +
   1.245 +      browserWin.focus();
   1.246 +
   1.247 +      // Stop the normal commandline processing from continuing. We just opened the main browser window
   1.248 +      aCmdLine.preventDefault = true;
   1.249 +    } catch (e) {
   1.250 +      Cu.reportError(e);
   1.251 +    }
   1.252 +
   1.253 +    // Assumption: All remaining command line arguments have been sent remotely (browser is already running)
   1.254 +    // Action: Open any URLs we find into an existing browser window
   1.255 +
   1.256 +    // First, get a browserDOMWindow object
   1.257 +    while (!browserWin.browserDOMWindow)
   1.258 +      Services.tm.currentThread.processNextEvent(true);
   1.259 +
   1.260 +    // Open any URIs into new tabs
   1.261 +    for (let i = 0; i < uris.length; i++)
   1.262 +      browserWin.browserDOMWindow.openURI(uris[i], null, Ci.nsIBrowserDOMWindow.OPEN_NEWTAB, Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL);
   1.263 +
   1.264 +    if (appURI)
   1.265 +      browserWin.browserDOMWindow.openURI(appURI, null, browserWin.OPEN_APPTAB, Ci.nsIBrowserDOMWindow.OPEN_NEW);
   1.266 +
   1.267 +    // Handle the notification, if called from it
   1.268 +    if (alertFlag) {
   1.269 +      if (alertFlag == "update-app") {
   1.270 +        // Notification was already displayed and clicked, skip it next time
   1.271 +        Services.prefs.setBoolPref("app.update.skipNotification", true);
   1.272 +
   1.273 +        var updateService = Cc["@mozilla.org/updates/update-service;1"].getService(Ci.nsIApplicationUpdateService);
   1.274 +        var updateTimerCallback = updateService.QueryInterface(Ci.nsITimerCallback);
   1.275 +        updateTimerCallback.notify(null);
   1.276 +      }
   1.277 +    }
   1.278 +  },
   1.279 +
   1.280 +  // QI
   1.281 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]),
   1.282 +
   1.283 +  // XPCOMUtils factory
   1.284 +  classID: Components.ID("{be623d20-d305-11de-8a39-0800200c9a66}"),
   1.285 +};
   1.286 +
   1.287 +var components = [ BrowserCLH ];
   1.288 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

mercurial