michael@0: /* -*- Mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil; -*- */ 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 Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function openWindow(aParent, aURL, aTarget, aFeatures, aArgs) { michael@0: let argString = null; michael@0: if (aArgs && !(aArgs instanceof Ci.nsISupportsArray)) { michael@0: argString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: argString.data = aArgs; michael@0: } michael@0: michael@0: return Services.ww.openWindow(aParent, aURL, aTarget, aFeatures, argString || aArgs); michael@0: } michael@0: michael@0: function resolveURIInternal(aCmdLine, aArgument) { michael@0: let uri = aCmdLine.resolveURI(aArgument); michael@0: michael@0: if (!(uri instanceof Ci.nsIFileURL)) michael@0: return uri; michael@0: michael@0: try { michael@0: if (uri.file.exists()) michael@0: return uri; michael@0: } catch (e) { michael@0: Cu.reportError(e); michael@0: } michael@0: michael@0: try { michael@0: let urifixup = Cc["@mozilla.org/docshell/urifixup;1"].getService(Ci.nsIURIFixup); michael@0: uri = urifixup.createFixupURI(aArgument, 0); michael@0: } catch (e) { michael@0: Cu.reportError(e); michael@0: } michael@0: michael@0: return uri; michael@0: } michael@0: michael@0: /** michael@0: * Determines whether a home page override is needed. michael@0: * Returns: michael@0: * "new profile" if this is the first run with a new profile. michael@0: * "new version" if this is the first run with a build with a different michael@0: * Gecko milestone (i.e. right after an upgrade). michael@0: * "none" otherwise. michael@0: */ michael@0: function needHomepageOverride() { michael@0: let savedmstone = null; michael@0: try { michael@0: savedmstone = Services.prefs.getCharPref("browser.startup.homepage_override.mstone"); michael@0: } catch (e) {} michael@0: michael@0: if (savedmstone == "ignore") michael@0: return "none"; michael@0: michael@0: let ourmstone = Services.appinfo.platformVersion; michael@0: michael@0: if (ourmstone != savedmstone) { michael@0: Services.prefs.setCharPref("browser.startup.homepage_override.mstone", ourmstone); michael@0: michael@0: return (savedmstone ? "new version" : "new profile"); michael@0: } michael@0: michael@0: return "none"; michael@0: } michael@0: michael@0: function getHomePage() { michael@0: let url = "about:newtab"; michael@0: try { michael@0: url = Services.prefs.getComplexValue("browser.startup.homepage", Ci.nsIPrefLocalizedString).data; michael@0: } catch (e) { } michael@0: michael@0: return url; michael@0: } michael@0: michael@0: function showPanelWhenReady(aWindow, aPage) { michael@0: aWindow.addEventListener("UIReadyDelayed", function(aEvent) { michael@0: aWindow.PanelUI.show(aPage); michael@0: }, false); michael@0: } michael@0: michael@0: function haveSystemLocale() { michael@0: let localeService = Cc["@mozilla.org/intl/nslocaleservice;1"].getService(Ci.nsILocaleService); michael@0: let systemLocale = localeService.getSystemLocale().getCategory("NSILOCALE_CTYPE"); michael@0: return isLocaleAvailable(systemLocale); michael@0: } michael@0: michael@0: function checkCurrentLocale() { michael@0: if (Services.prefs.prefHasUserValue("general.useragent.locale")) { michael@0: // if the user has a compatible locale from a different buildid, we need to update michael@0: var buildID = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo).platformBuildID; michael@0: let localeBuildID = Services.prefs.getCharPref("extensions.compatability.locales.buildid"); michael@0: if (buildID != localeBuildID) michael@0: return false; michael@0: michael@0: let currentLocale = Services.prefs.getCharPref("general.useragent.locale"); michael@0: return isLocaleAvailable(currentLocale); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: function isLocaleAvailable(aLocale) { michael@0: let chrome = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry); michael@0: chrome.QueryInterface(Ci.nsIToolkitChromeRegistry); michael@0: let availableLocales = chrome.getLocalesForPackage("browser"); michael@0: michael@0: let locale = aLocale.split("-")[0]; michael@0: let localeRegEx = new RegExp("^" + locale); michael@0: michael@0: while (availableLocales.hasMore()) { michael@0: let locale = availableLocales.getNext(); michael@0: if (localeRegEx.test(locale)) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: function BrowserCLH() { } michael@0: michael@0: BrowserCLH.prototype = { michael@0: // michael@0: // nsICommandLineHandler michael@0: // michael@0: handle: function fs_handle(aCmdLine) { michael@0: #ifdef DEBUG michael@0: for (var idx = 0; idx < aCmdLine.length; idx++) { michael@0: dump(aCmdLine.getArgument(idx) + "\n"); michael@0: } michael@0: #endif michael@0: // Instantiate the search service so the search engine cache is created now michael@0: // instead when the application is running. The install process will register michael@0: // this component by using the -silent command line flag, thereby creating michael@0: // the cache during install, not runtime. michael@0: // NOTE: This code assumes this CLH is run before the nsDefaultCLH, which michael@0: // consumes the "-silent" flag. michael@0: if (aCmdLine.findFlag("silent", false) > -1) { michael@0: let searchService = Services.search; michael@0: let autoComplete = Cc["@mozilla.org/autocomplete/search;1?name=history"]. michael@0: getService(Ci.nsIAutoCompleteSearch); michael@0: return; michael@0: } michael@0: michael@0: // Handle chrome windows loaded via commandline michael@0: let chromeParam = aCmdLine.handleFlagWithParam("chrome", false); michael@0: if (chromeParam) { michael@0: try { michael@0: // Only load URIs which do not inherit chrome privs michael@0: let features = "chrome,dialog=no,all"; michael@0: let uri = resolveURIInternal(aCmdLine, chromeParam); michael@0: let netutil = Cc["@mozilla.org/network/util;1"].getService(Ci.nsINetUtil); michael@0: if (!netutil.URIChainHasFlags(uri, Ci.nsIHttpProtocolHandler.URI_INHERITS_SECURITY_CONTEXT)) { michael@0: openWindow(null, uri.spec, "_blank", features, null); michael@0: michael@0: // Stop the normal commandline processing from continuing michael@0: aCmdLine.preventDefault = true; michael@0: } michael@0: } catch (e) { michael@0: Cu.reportError(e); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: // Check and remove the alert flag here, but we'll handle it a bit later - see below michael@0: let alertFlag = aCmdLine.handleFlagWithParam("alert", false); michael@0: michael@0: // Check and remove the webapp param michael@0: let appFlag = aCmdLine.handleFlagWithParam("webapp", false); michael@0: let appURI; michael@0: if (appFlag) michael@0: appURI = resolveURIInternal(aCmdLine, appFlag); michael@0: michael@0: // Keep an array of possible URL arguments michael@0: let uris = []; michael@0: michael@0: // Check for the "url" flag michael@0: let uriFlag = aCmdLine.handleFlagWithParam("url", false); michael@0: if (uriFlag) { michael@0: let uri = resolveURIInternal(aCmdLine, uriFlag); michael@0: if (uri) michael@0: uris.push(uri); michael@0: } michael@0: michael@0: // Check for the "search" flag michael@0: let searchParam = aCmdLine.handleFlagWithParam("search", false); michael@0: if (searchParam) { michael@0: var ss = Components.classes["@mozilla.org/browser/search-service;1"] michael@0: .getService(nsIBrowserSearchService); michael@0: var submission = ss.defaultEngine.getSubmission(searchParam.replace("\"", "", "g")); michael@0: uris.push(submission.uri); michael@0: } michael@0: michael@0: for (let i = 0; i < aCmdLine.length; i++) { michael@0: let arg = aCmdLine.getArgument(i); michael@0: if (!arg || arg[0] == '-') michael@0: continue; michael@0: michael@0: let uri = resolveURIInternal(aCmdLine, arg); michael@0: if (uri) michael@0: uris.push(uri); michael@0: } michael@0: michael@0: // Open the main browser window, if we don't already have one michael@0: let browserWin; michael@0: try { michael@0: let localeWin = Services.wm.getMostRecentWindow("navigator:localepicker"); michael@0: if (localeWin) { michael@0: localeWin.focus(); michael@0: aCmdLine.preventDefault = true; michael@0: return; michael@0: } michael@0: michael@0: browserWin = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: if (!browserWin) { michael@0: // Default to the saved homepage michael@0: let defaultURL = getHomePage(); michael@0: michael@0: // Override the default if we have a URL passed on command line michael@0: if (uris.length > 0) { michael@0: defaultURL = uris[0].spec; michael@0: uris = uris.slice(1); michael@0: } michael@0: michael@0: // Show the locale selector if we have a new profile, or if the selected locale is no longer compatible michael@0: let showLocalePicker = Services.prefs.getBoolPref("browser.firstrun.show.localepicker"); michael@0: if ((needHomepageOverride() == "new profile" && showLocalePicker && !haveSystemLocale())) { // || !checkCurrentLocale()) { michael@0: browserWin = openWindow(null, "chrome://browser/content/localePicker.xul", "_blank", "chrome,dialog=no,all", defaultURL); michael@0: aCmdLine.preventDefault = true; michael@0: return; michael@0: } michael@0: michael@0: browserWin = openWindow(null, "chrome://browser/content/browser.xul", "_blank", "chrome,dialog=no,all", defaultURL); michael@0: } michael@0: michael@0: browserWin.focus(); michael@0: michael@0: // Stop the normal commandline processing from continuing. We just opened the main browser window michael@0: aCmdLine.preventDefault = true; michael@0: } catch (e) { michael@0: Cu.reportError(e); michael@0: } michael@0: michael@0: // Assumption: All remaining command line arguments have been sent remotely (browser is already running) michael@0: // Action: Open any URLs we find into an existing browser window michael@0: michael@0: // First, get a browserDOMWindow object michael@0: while (!browserWin.browserDOMWindow) michael@0: Services.tm.currentThread.processNextEvent(true); michael@0: michael@0: // Open any URIs into new tabs michael@0: for (let i = 0; i < uris.length; i++) michael@0: browserWin.browserDOMWindow.openURI(uris[i], null, Ci.nsIBrowserDOMWindow.OPEN_NEWTAB, Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL); michael@0: michael@0: if (appURI) michael@0: browserWin.browserDOMWindow.openURI(appURI, null, browserWin.OPEN_APPTAB, Ci.nsIBrowserDOMWindow.OPEN_NEW); michael@0: michael@0: // Handle the notification, if called from it michael@0: if (alertFlag) { michael@0: if (alertFlag == "update-app") { michael@0: // Notification was already displayed and clicked, skip it next time michael@0: Services.prefs.setBoolPref("app.update.skipNotification", true); michael@0: michael@0: var updateService = Cc["@mozilla.org/updates/update-service;1"].getService(Ci.nsIApplicationUpdateService); michael@0: var updateTimerCallback = updateService.QueryInterface(Ci.nsITimerCallback); michael@0: updateTimerCallback.notify(null); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: // QI michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]), michael@0: michael@0: // XPCOMUtils factory michael@0: classID: Components.ID("{be623d20-d305-11de-8a39-0800200c9a66}"), michael@0: }; michael@0: michael@0: var components = [ BrowserCLH ]; michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);