home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 233 / Computer Shopper 233 / ComputerShopperDVD233.iso / Toolkit / Internet / Firefox / Firefox Setup 2.0.0.3.exe / nonlocalized / components / nsBrowserContentHandler.js < prev    next >
Encoding:
Text File  |  2007-03-10  |  29.3 KB  |  857 lines

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