home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / firefox-3.0.14 / components / nsBrowserContentHandler.js < prev    next >
Encoding:
JavaScript  |  2009-09-07  |  31.2 KB  |  891 lines

  1. //@line 37 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  2.  
  3. const nsISupports            = Components.interfaces.nsISupports;
  4.  
  5. const nsIBrowserDOMWindow    = Components.interfaces.nsIBrowserDOMWindow;
  6. const nsIBrowserHandler      = Components.interfaces.nsIBrowserHandler;
  7. const nsIBrowserHistory      = Components.interfaces.nsIBrowserHistory;
  8. const nsIChannel             = Components.interfaces.nsIChannel;
  9. const nsICommandLine         = Components.interfaces.nsICommandLine;
  10. const nsICommandLineHandler  = Components.interfaces.nsICommandLineHandler;
  11. const nsIContentHandler      = Components.interfaces.nsIContentHandler;
  12. const nsIDocShellTreeItem    = Components.interfaces.nsIDocShellTreeItem;
  13. const nsIDOMChromeWindow     = Components.interfaces.nsIDOMChromeWindow;
  14. const nsIDOMWindow           = Components.interfaces.nsIDOMWindow;
  15. const nsIFactory             = Components.interfaces.nsIFactory;
  16. const nsIFileURL             = Components.interfaces.nsIFileURL;
  17. const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
  18. const nsIInterfaceRequestor  = Components.interfaces.nsIInterfaceRequestor;
  19. const nsINetUtil             = Components.interfaces.nsINetUtil;
  20. const nsIPrefBranch          = Components.interfaces.nsIPrefBranch;
  21. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  22. const nsISupportsString      = Components.interfaces.nsISupportsString;
  23. const nsIURIFixup            = Components.interfaces.nsIURIFixup;
  24. const nsIWebNavigation       = Components.interfaces.nsIWebNavigation;
  25. const nsIWindowMediator      = Components.interfaces.nsIWindowMediator;
  26. const nsIWindowWatcher       = Components.interfaces.nsIWindowWatcher;
  27. const nsICategoryManager     = Components.interfaces.nsICategoryManager;
  28. const nsIWebNavigationInfo   = Components.interfaces.nsIWebNavigationInfo;
  29. const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  30. const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
  31.  
  32. const NS_BINDING_ABORTED = 0x804b0002;
  33. const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
  34. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  35.  
  36. const URI_INHERITS_SECURITY_CONTEXT = nsIHttpProtocolHandler
  37.                                         .URI_INHERITS_SECURITY_CONTEXT;
  38.  
  39. function shouldLoadURI(aURI) {
  40.   if (aURI && !aURI.schemeIs("chrome"))
  41.     return true;
  42.  
  43.   dump("*** Preventing external load of chrome: URI into browser window\n");
  44.   dump("    Use -chrome <uri> instead\n");
  45.   return false;
  46. }
  47.  
  48. function resolveURIInternal(aCmdLine, aArgument) {
  49.   var uri = aCmdLine.resolveURI(aArgument);
  50.  
  51.   if (!(uri instanceof nsIFileURL)) {
  52.     return uri;
  53.   }
  54.  
  55.   try {
  56.     if (uri.file.exists())
  57.       return uri;
  58.   }
  59.   catch (e) {
  60.     Components.utils.reportError(e);
  61.   }
  62.  
  63.   // We have interpreted the argument as a relative file URI, but the file
  64.   // doesn't exist. Try URI fixup heuristics: see bug 290782.
  65.  
  66.   try {
  67.     var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  68.                              .getService(nsIURIFixup);
  69.  
  70.     uri = urifixup.createFixupURI(aArgument, 0);
  71.   }
  72.   catch (e) {
  73.     Components.utils.reportError(e);
  74.   }
  75.  
  76.   return uri;
  77. }
  78.  
  79. const OVERRIDE_NONE        = 0;
  80. const OVERRIDE_NEW_PROFILE = 1;
  81. const OVERRIDE_NEW_MSTONE  = 2;
  82. /**
  83.  * Determines whether a home page override is needed.
  84.  * Returns:
  85.  *  OVERRIDE_NEW_PROFILE if this is the first run with a new profile.
  86.  *  OVERRIDE_NEW_MSTONE if this is the first run with a build with a different
  87.  *                      Gecko milestone (i.e. right after an upgrade).
  88.  *  OVERRIDE_NONE otherwise.
  89.  */
  90. function needHomepageOverride(prefb) {
  91.   var savedmstone = null;
  92.   try {
  93.     savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
  94.   } catch (e) {}
  95.  
  96.   if (savedmstone == "ignore")
  97.     return OVERRIDE_NONE;
  98.  
  99.   var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
  100.                          .getService(nsIHttpProtocolHandler).misc;
  101.  
  102.   if (mstone != savedmstone) {
  103.     // Bug 462254. Previous releases had a default pref to suppress the EULA
  104.     // agreement if the platform's installer had already shown one. Now with
  105.     // about:rights we've removed the EULA stuff and default pref, but we need
  106.     // a way to make existing profiles retain the default that we removed.
  107.     if (savedmstone)
  108.       prefb.setBoolPref("browser.rights.3.shown", true);
  109.     
  110.     prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
  111.     return (savedmstone ? OVERRIDE_NEW_MSTONE : OVERRIDE_NEW_PROFILE);
  112.   }
  113.  
  114.   return OVERRIDE_NONE;
  115. }
  116.  
  117. // Copies a pref override file into the user's profile pref-override folder,
  118. // and then tells the pref service to reload it's default prefs.
  119. function copyPrefOverride() {
  120.   try {
  121.     var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  122.                                 .getService(Components.interfaces.nsIProperties);
  123.     const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
  124.     var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
  125.                                        Components.interfaces.nsIFile);
  126.     if (!prefOverride.exists())
  127.       return; // nothing to do
  128.  
  129.     const NS_APP_PREFS_OVERRIDE_DIR     = "PrefDOverride";
  130.     var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
  131.                                            Components.interfaces.nsIFile);
  132.  
  133.     // Check for any existing pref overrides, and remove them if present
  134.     var existingPrefOverridesFile = prefOverridesDir.clone();
  135.     existingPrefOverridesFile.append(prefOverride.leafName);
  136.     if (existingPrefOverridesFile.exists())
  137.       existingPrefOverridesFile.remove(false);
  138.  
  139.     prefOverride.copyTo(prefOverridesDir, null);
  140.  
  141.     // Now that we've installed the new-profile pref override file,
  142.     // re-read the default prefs.
  143.     var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
  144.                                .getService(Components.interfaces.nsIObserver);
  145.     prefSvcObs.observe(null, "reload-default-prefs", null);
  146.   } catch (ex) {
  147.     Components.utils.reportError(ex);
  148.   }
  149. }
  150.  
  151. // Flag used to indicate that the arguments to openWindow can be passed directly.
  152. const NO_EXTERNAL_URIS = 1;
  153.  
  154. function openWindow(parent, url, target, features, args, noExternalArgs) {
  155.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  156.                          .getService(nsIWindowWatcher);
  157.  
  158.   if (noExternalArgs == NO_EXTERNAL_URIS) {
  159.     // Just pass in the defaultArgs directly
  160.     var argstring;
  161.     if (args) {
  162.       argstring = Components.classes["@mozilla.org/supports-string;1"]
  163.                             .createInstance(nsISupportsString);
  164.       argstring.data = args;
  165.     }
  166.  
  167.     return wwatch.openWindow(parent, url, target, features, argstring);
  168.   }
  169.   
  170.   // Pass an array to avoid the browser "|"-splitting behavior.
  171.   var argArray = Components.classes["@mozilla.org/supports-array;1"]
  172.                     .createInstance(Components.interfaces.nsISupportsArray);
  173.  
  174.   // add args to the arguments array
  175.   var stringArgs = null;
  176.   if (args instanceof Array) // array
  177.     stringArgs = args;
  178.   else if (args) // string
  179.     stringArgs = [args];
  180.  
  181.   if (stringArgs) {
  182.     // put the URIs into argArray
  183.     var uriArray = Components.classes["@mozilla.org/supports-array;1"]
  184.                        .createInstance(Components.interfaces.nsISupportsArray);
  185.     stringArgs.forEach(function (uri) {
  186.       var sstring = Components.classes["@mozilla.org/supports-string;1"]
  187.                               .createInstance(nsISupportsString);
  188.       sstring.data = uri;
  189.       uriArray.AppendElement(sstring);
  190.     });
  191.     argArray.AppendElement(uriArray);
  192.   } else {
  193.     argArray.AppendElement(null);
  194.   }
  195.  
  196.   // Pass these as null to ensure that we always trigger the "single URL"
  197.   // behavior in browser.js's BrowserStartup (which handles the window
  198.   // arguments)
  199.   argArray.AppendElement(null); // charset
  200.   argArray.AppendElement(null); // referer
  201.   argArray.AppendElement(null); // postData
  202.   argArray.AppendElement(null); // allowThirdPartyFixup
  203.  
  204.   return wwatch.openWindow(parent, url, target, features, argArray);
  205. }
  206.  
  207. function openPreferences() {
  208.   var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
  209.   var url = "chrome://browser/content/preferences/preferences.xul";
  210.  
  211.   var win = getMostRecentWindow("Browser:Preferences");
  212.   if (win) {
  213.     win.focus();
  214.   } else {
  215.     openWindow(null, url, "_blank", features);
  216.   }
  217. }
  218.  
  219. function getMostRecentWindow(aType) {
  220.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  221.                      .getService(nsIWindowMediator);
  222.   return wm.getMostRecentWindow(aType);
  223. }
  224.  
  225. //@line 268 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  226.  
  227. // this returns the most recent non-popup browser window
  228. function getMostRecentBrowserWindow() {
  229.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  230.                      .getService(Components.interfaces.nsIWindowMediator);
  231.  
  232. //@line 275 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  233.   var win = wm.getMostRecentWindow("navigator:browser", true);
  234.  
  235.   // if we're lucky, this isn't a popup, and we can just return this
  236.   if (win && win.document.documentElement.getAttribute("chromehidden")) {
  237.     var windowList = wm.getEnumerator("navigator:browser", true);
  238.     // this is oldest to newest, so this gets a bit ugly
  239.     while (windowList.hasMoreElements()) {
  240.       var nextWin = windowList.getNext();
  241.       if (!nextWin.document.documentElement.getAttribute("chromehidden"))
  242.         win = nextWin;
  243.     }
  244.   }
  245. //@line 300 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  246.  
  247.   return win;
  248. }
  249.  
  250. function doSearch(searchTerm, cmdLine) {
  251.   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
  252.                      .getService(nsIBrowserSearchService);
  253.  
  254.   var submission = ss.defaultEngine.getSubmission(searchTerm, null);
  255.  
  256.   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
  257.   var sa = Components.classes["@mozilla.org/supports-array;1"]
  258.                      .createInstance(Components.interfaces.nsISupportsArray);
  259.  
  260.   var wuri = Components.classes["@mozilla.org/supports-string;1"]
  261.                        .createInstance(Components.interfaces.nsISupportsString);
  262.   wuri.data = submission.uri.spec;
  263.  
  264.   sa.AppendElement(wuri);
  265.   sa.AppendElement(null);
  266.   sa.AppendElement(null);
  267.   sa.AppendElement(submission.postData);
  268.  
  269.   // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
  270.   // preferences, but need nsIBrowserDOMWindow extensions
  271.  
  272.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  273.                          .getService(nsIWindowWatcher);
  274.  
  275.   return wwatch.openWindow(null, nsBrowserContentHandler.chromeURL,
  276.                            "_blank",
  277.                            "chrome,dialog=no,all" +
  278.                              nsBrowserContentHandler.getFeatures(cmdLine),
  279.                            sa);
  280. }
  281.  
  282. var nsBrowserContentHandler = {
  283.   /* helper functions */
  284.  
  285.   mChromeURL : null,
  286.  
  287.   get chromeURL() {
  288.     if (this.mChromeURL) {
  289.       return this.mChromeURL;
  290.     }
  291.  
  292.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  293.                           .getService(nsIPrefBranch);
  294.     this.mChromeURL = prefb.getCharPref("browser.chromeURL");
  295.  
  296.     return this.mChromeURL;
  297.   },
  298.  
  299.   /* nsISupports */
  300.   QueryInterface : function bch_QI(iid) {
  301.     if (!iid.equals(nsISupports) &&
  302.         !iid.equals(nsICommandLineHandler) &&
  303.         !iid.equals(nsIBrowserHandler) &&
  304.         !iid.equals(nsIContentHandler) &&
  305.         !iid.equals(nsICommandLineValidator) &&
  306.         !iid.equals(nsIFactory))
  307.       throw Components.results.NS_ERROR_NO_INTERFACE;
  308.  
  309.     return this;
  310.   },
  311.  
  312.   /* nsICommandLineHandler */
  313.   handle : function bch_handle(cmdLine) {
  314.     if (cmdLine.handleFlag("browser", false)) {
  315.       // Passing defaultArgs, so use NO_EXTERNAL_URIS
  316.       openWindow(null, this.chromeURL, "_blank",
  317.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  318.                  this.defaultArgs, NO_EXTERNAL_URIS);
  319.       cmdLine.preventDefault = true;
  320.     }
  321.  
  322.     try {
  323.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  324.     }
  325.     catch (e) {
  326.       throw NS_ERROR_ABORT;
  327.     }
  328.  
  329.     if (remoteCommand != null) {
  330.       try {
  331.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  332.         var remoteVerb;
  333.         if (a) {
  334.           remoteVerb = a[1].toLowerCase();
  335.           var remoteParams = [];
  336.           var sepIndex = a[2].lastIndexOf(",");
  337.           if (sepIndex == -1)
  338.             remoteParams[0] = a[2];
  339.           else {
  340.             remoteParams[0] = a[2].substring(0, sepIndex);
  341.             remoteParams[1] = a[2].substring(sepIndex + 1);
  342.           }
  343.         }
  344.  
  345.         switch (remoteVerb) {
  346.         case "openurl":
  347.         case "openfile":
  348.           // openURL(<url>)
  349.           // openURL(<url>,new-window)
  350.           // openURL(<url>,new-tab)
  351.  
  352.           // First param is the URL, second param (if present) is the "target"
  353.           // (tab, window)
  354.           var url = remoteParams[0];
  355.           var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
  356.           if (remoteParams[1]) {
  357.             var targetParam = remoteParams[1].toLowerCase()
  358.                                              .replace(/^\s*|\s*$/g, "");
  359.             if (targetParam == "new-tab")
  360.               target = nsIBrowserDOMWindow.OPEN_NEWTAB;
  361.             else if (targetParam == "new-window")
  362.               target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
  363.             else {
  364.               // The "target" param isn't one of our supported values, so
  365.               // assume it's part of a URL that contains commas.
  366.               url += "," + remoteParams[1];
  367.             }
  368.           }
  369.  
  370.           var uri = resolveURIInternal(cmdLine, url);
  371.           handURIToExistingBrowser(uri, target, cmdLine);
  372.           break;
  373.  
  374.         case "xfedocommand":
  375.           // xfeDoCommand(openBrowser)
  376.           if (remoteParams[0].toLowerCase() != "openbrowser")
  377.             throw NS_ERROR_ABORT;
  378.  
  379.           // Passing defaultArgs, so use NO_EXTERNAL_URIS
  380.           openWindow(null, this.chromeURL, "_blank",
  381.                      "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  382.                      this.defaultArgs, NO_EXTERNAL_URIS);
  383.           break;
  384.  
  385.         default:
  386.           // Somebody sent us a remote command we don't know how to process:
  387.           // just abort.
  388.           throw "Unknown remote command.";
  389.         }
  390.  
  391.         cmdLine.preventDefault = true;
  392.       }
  393.       catch (e) {
  394.         Components.utils.reportError(e);
  395.         // If we had a -remote flag but failed to process it, throw
  396.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  397.         // back to the handling code.
  398.         throw NS_ERROR_ABORT;
  399.       }
  400.     }
  401.  
  402.     var uriparam;
  403.     try {
  404.       while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
  405.         var uri = resolveURIInternal(cmdLine, uriparam);
  406.         if (!shouldLoadURI(uri))
  407.           continue;
  408.         openWindow(null, this.chromeURL, "_blank",
  409.                    "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  410.                    uri.spec);
  411.         cmdLine.preventDefault = true;
  412.       }
  413.     }
  414.     catch (e) {
  415.       Components.utils.reportError(e);
  416.     }
  417.  
  418.     try {
  419.       while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
  420.         var uri = resolveURIInternal(cmdLine, uriparam);
  421.         handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
  422.         cmdLine.preventDefault = true;
  423.       }
  424.     }
  425.     catch (e) {
  426.       Components.utils.reportError(e);
  427.     }
  428.  
  429.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  430.     if (chromeParam) {
  431.  
  432.       // Handle the old preference dialog URL separately (bug 285416)
  433.       if (chromeParam == "chrome://browser/content/pref/pref.xul") {
  434.         openPreferences();
  435.         cmdLine.preventDefault = true;
  436.       } else try {
  437.         // only load URIs which do not inherit chrome privs
  438.         var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
  439.         var uri = resolveURIInternal(cmdLine, chromeParam);
  440.         var netutil = Components.classes["@mozilla.org/network/util;1"]
  441.                                 .getService(nsINetUtil);
  442.         if (!netutil.URIChainHasFlags(uri, URI_INHERITS_SECURITY_CONTEXT)) {
  443.           openWindow(null, uri.spec, "_blank", features);
  444.           cmdLine.preventDefault = true;
  445.         }
  446.       }
  447.       catch (e) {
  448.         Components.utils.reportError(e);
  449.       }
  450.     }
  451.     if (cmdLine.handleFlag("preferences", false)) {
  452.       openPreferences();
  453.       cmdLine.preventDefault = true;
  454.     }
  455.     if (cmdLine.handleFlag("silent", false))
  456.       cmdLine.preventDefault = true;
  457.  
  458.     var searchParam = cmdLine.handleFlagWithParam("search", false);
  459.     if (searchParam) {
  460.       doSearch(searchParam, cmdLine);
  461.       cmdLine.preventDefault = true;
  462.     }
  463.  
  464. //@line 531 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  465.   },
  466.  
  467.   helpInfo : "  -browser            Open a browser window.\n",
  468.  
  469.   /* nsIBrowserHandler */
  470.  
  471.   get defaultArgs() {
  472.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  473.                           .getService(nsIPrefBranch);
  474.     var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
  475.                               .getService(Components.interfaces.nsIURLFormatter);
  476.  
  477.     var overridePage = "";
  478.     var haveUpdateSession = false;
  479.     try {
  480.       switch (needHomepageOverride(prefb)) {
  481.         case OVERRIDE_NEW_PROFILE:
  482.           // New profile
  483.           overridePage = formatter.formatURLPref("startup.homepage_welcome_url");
  484.           break;
  485.         case OVERRIDE_NEW_MSTONE:
  486.           // Existing profile, new build
  487.           copyPrefOverride();
  488.  
  489.           // Check whether we have a session to restore. If we do, we assume
  490.           // that this is an "update" session.
  491.           var ss = Components.classes["@mozilla.org/browser/sessionstartup;1"]
  492.                              .getService(Components.interfaces.nsISessionStartup);
  493.           haveUpdateSession = ss.doRestore();
  494.           overridePage = formatter.formatURLPref("startup.homepage_override_url");
  495.           break;
  496.     }
  497.     } catch (ex) {}
  498.  
  499.     // formatURLPref might return "about:blank" if getting the pref fails
  500.     if (overridePage == "about:blank")
  501.       overridePage = "";
  502.  
  503.     var startPage = "";
  504.     try {
  505.       var choice = prefb.getIntPref("browser.startup.page");
  506.       if (choice == 1 || choice == 3)
  507.         startPage = this.startPage;
  508.  
  509.       if (choice == 2)
  510.         startPage = Components.classes["@mozilla.org/browser/global-history;2"]
  511.                               .getService(nsIBrowserHistory).lastPageVisited;
  512.     } catch (e) {
  513.       Components.utils.reportError(e);
  514.     }
  515.  
  516.     if (startPage == "about:blank")
  517.       startPage = "";
  518.  
  519.     // Only show the startPage if we're not restoring an update session.
  520.     if (overridePage && startPage && !haveUpdateSession)
  521.       return overridePage + "|" + startPage;
  522.  
  523.     return overridePage || startPage || "about:blank";
  524.   },
  525.  
  526.   get startPage() {
  527.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  528.                           .getService(nsIPrefBranch);
  529.  
  530.     var uri = prefb.getComplexValue("browser.startup.homepage",
  531.                                     nsIPrefLocalizedString).data;
  532.  
  533.     if (!uri) {
  534.       prefb.clearUserPref("browser.startup.homepage");
  535.       uri = prefb.getComplexValue("browser.startup.homepage",
  536.                                   nsIPrefLocalizedString).data;
  537.     }
  538.                                 
  539.     var count;
  540.     try {
  541.       count = prefb.getIntPref("browser.startup.homepage.count");
  542.     }
  543.     catch (e) {
  544.       return uri;
  545.     }
  546.  
  547.     for (var i = 1; i < count; ++i) {
  548.       try {
  549.         var page = prefb.getComplexValue("browser.startup.homepage." + i,
  550.                                          nsIPrefLocalizedString).data;
  551.         uri += "\n" + page;
  552.       }
  553.       catch (e) {
  554.       }
  555.     }
  556.  
  557.     return uri;
  558.   },
  559.  
  560.   mFeatures : null,
  561.  
  562.   getFeatures : function bch_features(cmdLine) {
  563.     if (this.mFeatures === null) {
  564.       this.mFeatures = "";
  565.  
  566.       try {
  567.         var width = cmdLine.handleFlagWithParam("width", false);
  568.         var height = cmdLine.handleFlagWithParam("height", false);
  569.  
  570.         if (width)
  571.           this.mFeatures += ",width=" + width;
  572.         if (height)
  573.           this.mFeatures += ",height=" + height;
  574.       }
  575.       catch (e) {
  576.       }
  577.     }
  578.  
  579.     return this.mFeatures;
  580.   },
  581.  
  582.   /* nsIContentHandler */
  583.  
  584.   handleContent : function bch_handleContent(contentType, context, request) {
  585.     try {
  586.       var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
  587.                                  .getService(nsIWebNavigationInfo);
  588.       if (!webNavInfo.isTypeSupported(contentType, null)) {
  589.         throw NS_ERROR_WONT_HANDLE_CONTENT;
  590.       }
  591.     } catch (e) {
  592.       throw NS_ERROR_WONT_HANDLE_CONTENT;
  593.     }
  594.  
  595.     request.QueryInterface(nsIChannel);
  596.     handURIToExistingBrowser(request.URI,
  597.       nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, null);
  598.     request.cancel(NS_BINDING_ABORTED);
  599.   },
  600.  
  601.   /* nsICommandLineValidator */
  602.   validate : function bch_validate(cmdLine) {
  603.     // Other handlers may use osint so only handle the osint flag if the url
  604.     // flag is also present and the command line is valid.
  605.     var osintFlagIdx = cmdLine.findFlag("osint", false);
  606.     var urlFlagIdx = cmdLine.findFlag("url", false);
  607.     if (urlFlagIdx > -1 && (osintFlagIdx > -1 ||
  608.         cmdLine.state == nsICommandLine.STATE_REMOTE_EXPLICIT)) {
  609.       var urlParam = cmdLine.getArgument(urlFlagIdx + 1);
  610.       if (cmdLine.length != urlFlagIdx + 2 || /firefoxurl:/.test(urlParam))
  611.         throw NS_ERROR_ABORT;
  612.       cmdLine.handleFlag("osint", false)
  613.     }
  614.   },
  615.  
  616.   /* nsIFactory */
  617.   createInstance: function bch_CI(outer, iid) {
  618.     if (outer != null)
  619.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  620.  
  621.     return this.QueryInterface(iid);
  622.   },
  623.     
  624.   lockFactory : function bch_lock(lock) {
  625.     /* no-op */
  626.   }
  627. };
  628.  
  629. const bch_contractID = "@mozilla.org/browser/clh;1";
  630. const bch_CID = Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}");
  631. const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type=";
  632.  
  633. function handURIToExistingBrowser(uri, location, cmdLine)
  634. {
  635.   if (!shouldLoadURI(uri))
  636.     return;
  637.  
  638.   var navWin = getMostRecentBrowserWindow();
  639.   if (!navWin) {
  640.     // if we couldn't load it in an existing window, open a new one
  641.     openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  642.                "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  643.                uri.spec);
  644.     return;
  645.   }
  646.  
  647.   var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
  648.                      .getInterface(nsIWebNavigation);
  649.   var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
  650.   var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
  651.                         .getInterface(nsIDOMWindow);
  652.   var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
  653.   bwin.openURI(uri, null, location,
  654.                nsIBrowserDOMWindow.OPEN_EXTERNAL);
  655. }
  656.  
  657.  
  658. var nsDefaultCommandLineHandler = {
  659.   /* nsISupports */
  660.   QueryInterface : function dch_QI(iid) {
  661.     if (!iid.equals(nsISupports) &&
  662.         !iid.equals(nsICommandLineHandler) &&
  663.         !iid.equals(nsIFactory))
  664.       throw Components.results.NS_ERROR_NO_INTERFACE;
  665.  
  666.     return this;
  667.   },
  668.  
  669.   // List of uri's that were passed via the command line without the app
  670.   // running and have already been handled. This is compared against uri's
  671.   // opened using DDE on Win32 so we only open one of the requests.
  672.   _handledURIs: [ ],
  673. //@line 742 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  674.  
  675.   /* nsICommandLineHandler */
  676.   handle : function dch_handle(cmdLine) {
  677.     var urilist = [];
  678.  
  679. //@line 768 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  680.  
  681.     try {
  682.       var ar;
  683.       while ((ar = cmdLine.handleFlagWithParam("url", false))) {
  684.         var found = false;
  685.         var uri = resolveURIInternal(cmdLine, ar);
  686.         // count will never be greater than zero except on Win32.
  687.         var count = this._handledURIs.length;
  688.         for (var i = 0; i < count; ++i) {
  689.           if (this._handledURIs[i].spec == uri.spec) {
  690.             this._handledURIs.splice(i, 1);
  691.             found = true;
  692.             cmdLine.preventDefault = true;
  693.             break;
  694.           }
  695.         }
  696.         if (!found) {
  697.           urilist.push(uri);
  698.           // The requestpending command line flag is only used on Win32.
  699.           if (cmdLine.handleFlag("requestpending", false) &&
  700.               cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
  701.             this._handledURIs.push(uri)
  702.         }
  703.       }
  704.     }
  705.     catch (e) {
  706.       Components.utils.reportError(e);
  707.     }
  708.  
  709.     count = cmdLine.length;
  710.  
  711.     for (i = 0; i < count; ++i) {
  712.       var curarg = cmdLine.getArgument(i);
  713.       if (curarg.match(/^-/)) {
  714.         Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
  715.         // To emulate the pre-nsICommandLine behavior, we ignore
  716.         // the argument after an unrecognized flag.
  717.         ++i;
  718.       } else {
  719.         try {
  720.           urilist.push(resolveURIInternal(cmdLine, curarg));
  721.         }
  722.         catch (e) {
  723.           Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
  724.         }
  725.       }
  726.     }
  727.  
  728.     if (urilist.length) {
  729.       if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
  730.           urilist.length == 1) {
  731.         // Try to find an existing window and load our URI into the
  732.         // current tab, new tab, or new window as prefs determine.
  733.         try {
  734.           handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
  735.           return;
  736.         }
  737.         catch (e) {
  738.         }
  739.       }
  740.  
  741.       var URLlist = urilist.filter(shouldLoadURI).map(function (u) u.spec);
  742.       if (URLlist.length) {
  743.         openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  744.                    "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  745.                    URLlist);
  746.       }
  747.  
  748.     }
  749.     else if (!cmdLine.preventDefault) {
  750.       // Passing defaultArgs, so use NO_EXTERNAL_URIS
  751.       openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  752.                  "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  753.                  nsBrowserContentHandler.defaultArgs, NO_EXTERNAL_URIS);
  754.     }
  755.   },
  756.  
  757.   // XXX localize me... how?
  758.   helpInfo : "Usage: firefox [-flags] [<url>]\n",
  759.  
  760.   /* nsIFactory */
  761.   createInstance: function dch_CI(outer, iid) {
  762.     if (outer != null)
  763.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  764.  
  765.     return this.QueryInterface(iid);
  766.   },
  767.     
  768.   lockFactory : function dch_lock(lock) {
  769.     /* no-op */
  770.   }
  771. };
  772.  
  773. const dch_contractID = "@mozilla.org/browser/final-clh;1";
  774. const dch_CID = Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}");
  775.  
  776. var Module = {
  777.   /* nsISupports */
  778.   QueryInterface: function mod_QI(iid) {
  779.     if (iid.equals(Components.interfaces.nsIModule) ||
  780.         iid.equals(Components.interfaces.nsISupports))
  781.       return this;
  782.  
  783.     throw Components.results.NS_ERROR_NO_INTERFACE;
  784.   },
  785.  
  786.   /* nsIModule */
  787.   getClassObject: function mod_getco(compMgr, cid, iid) {
  788.     if (cid.equals(bch_CID))
  789.       return nsBrowserContentHandler.QueryInterface(iid);
  790.  
  791.     if (cid.equals(dch_CID))
  792.       return nsDefaultCommandLineHandler.QueryInterface(iid);
  793.  
  794.     throw Components.results.NS_ERROR_NO_INTERFACE;
  795.   },
  796.     
  797.   registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
  798.     if (Components.classes["@mozilla.org/xre/app-info;1"]) {
  799.       // Don't register these if Firefox is launching a XULRunner application
  800.       const FIREFOX_UID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
  801.       var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  802.                               .getService(Components.interfaces.nsIXULAppInfo);
  803.       if (appInfo.ID != FIREFOX_UID)
  804.         return;
  805.     }
  806.  
  807.     var compReg =
  808.       compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  809.  
  810.     compReg.registerFactoryLocation( bch_CID,
  811.                                      "nsBrowserContentHandler",
  812.                                      bch_contractID,
  813.                                      fileSpec,
  814.                                      location,
  815.                                      type );
  816.     compReg.registerFactoryLocation( dch_CID,
  817.                                      "nsDefaultCommandLineHandler",
  818.                                      dch_contractID,
  819.                                      fileSpec,
  820.                                      location,
  821.                                      type );
  822.  
  823.     function registerType(contentType) {
  824.       compReg.registerFactoryLocation( bch_CID,
  825.                                        "Browser Cmdline Handler",
  826.                                        CONTRACTID_PREFIX + contentType,
  827.                                        fileSpec,
  828.                                        location,
  829.                                        type );
  830.     }
  831.  
  832.     registerType("text/html");
  833.     registerType("application/vnd.mozilla.xul+xml");
  834. //@line 923 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  835.     registerType("image/svg+xml");
  836. //@line 925 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  837.     registerType("text/rdf");
  838.     registerType("text/xml");
  839.     registerType("application/xhtml+xml");
  840.     registerType("text/css");
  841.     registerType("text/plain");
  842.     registerType("image/gif");
  843.     registerType("image/jpeg");
  844.     registerType("image/jpg");
  845.     registerType("image/png");
  846.     registerType("image/bmp");
  847.     registerType("image/x-icon");
  848.     registerType("image/vnd.microsoft.icon");
  849.     registerType("image/x-xbitmap");
  850.     registerType("application/http-index-format");
  851.  
  852.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  853.                            .getService(nsICategoryManager);
  854.  
  855.     catMan.addCategoryEntry("command-line-handler",
  856.                             "m-browser",
  857.                             bch_contractID, true, true);
  858.     catMan.addCategoryEntry("command-line-handler",
  859.                             "x-default",
  860.                             dch_contractID, true, true);
  861.     catMan.addCategoryEntry("command-line-validator",
  862.                             "b-browser",
  863.                             bch_contractID, true, true);
  864.   },
  865.     
  866.   unregisterSelf : function mod_unregself(compMgr, location, type) {
  867.     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
  868.     compReg.unregisterFactoryLocation(bch_CID, location);
  869.     compReg.unregisterFactoryLocation(dch_CID, location);
  870.  
  871.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  872.                            .getService(nsICategoryManager);
  873.  
  874.     catMan.deleteCategoryEntry("command-line-handler",
  875.                                "m-browser", true);
  876.     catMan.deleteCategoryEntry("command-line-handler",
  877.                                "x-default", true);
  878.     catMan.deleteCategoryEntry("command-line-validator",
  879.                                "b-browser", true);
  880.   },
  881.  
  882.   canUnload: function(compMgr) {
  883.     return true;
  884.   }
  885. };
  886.  
  887. // NSGetModule: Return the nsIModule object.
  888. function NSGetModule(compMgr, fileSpec) {
  889.   return Module;
  890. }
  891.