Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / browser / components / nsBrowserContentHandler.js
blob0dad6930734f1723560e879cf79154d2dbc1f5ba
1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 # The Original Code is the Mozilla Firefox browser.
16 # The Initial Developer of the Original Code is
17 # Benjamin Smedberg <benjamin@smedbergs.us>
19 # Portions created by the Initial Developer are Copyright (C) 2004
20 # the Initial Developer. All Rights Reserved.
22 # Contributor(s):
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.
36 # ***** END LICENSE BLOCK *****
38 const nsISupports = Components.interfaces.nsISupports;
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 nsINetUtil = Components.interfaces.nsINetUtil;
55 const nsIPrefBranch = Components.interfaces.nsIPrefBranch;
56 const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
57 const nsISupportsString = Components.interfaces.nsISupportsString;
58 const nsIURIFixup = Components.interfaces.nsIURIFixup;
59 const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
60 const nsIWindowMediator = Components.interfaces.nsIWindowMediator;
61 const nsIWindowWatcher = Components.interfaces.nsIWindowWatcher;
62 const nsICategoryManager = Components.interfaces.nsICategoryManager;
63 const nsIWebNavigationInfo = Components.interfaces.nsIWebNavigationInfo;
64 const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
65 const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
67 const NS_BINDING_ABORTED = 0x804b0002;
68 const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
69 const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
71 const URI_INHERITS_SECURITY_CONTEXT = nsIHttpProtocolHandler
72 .URI_INHERITS_SECURITY_CONTEXT;
74 function shouldLoadURI(aURI) {
75 if (aURI && !aURI.schemeIs("chrome"))
76 return true;
78 dump("*** Preventing external load of chrome: URI into browser window\n");
79 dump(" Use -chrome <uri> instead\n");
80 return false;
83 function resolveURIInternal(aCmdLine, aArgument) {
84 var uri = aCmdLine.resolveURI(aArgument);
86 if (!(uri instanceof nsIFileURL)) {
87 return uri;
90 try {
91 if (uri.file.exists())
92 return uri;
94 catch (e) {
95 Components.utils.reportError(e);
98 // We have interpreted the argument as a relative file URI, but the file
99 // doesn't exist. Try URI fixup heuristics: see bug 290782.
101 try {
102 var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
103 .getService(nsIURIFixup);
105 uri = urifixup.createFixupURI(aArgument, 0);
107 catch (e) {
108 Components.utils.reportError(e);
111 return uri;
114 const OVERRIDE_NONE = 0;
115 const OVERRIDE_NEW_PROFILE = 1;
116 const OVERRIDE_NEW_MSTONE = 2;
118 * Determines whether a home page override is needed.
119 * Returns:
120 * OVERRIDE_NEW_PROFILE if this is the first run with a new profile.
121 * OVERRIDE_NEW_MSTONE if this is the first run with a build with a different
122 * Gecko milestone (i.e. right after an upgrade).
123 * OVERRIDE_NONE otherwise.
125 function needHomepageOverride(prefb) {
126 var savedmstone = null;
127 try {
128 savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
129 } catch (e) {}
131 if (savedmstone == "ignore")
132 return OVERRIDE_NONE;
134 var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
135 .getService(nsIHttpProtocolHandler).misc;
137 if (mstone != savedmstone) {
138 prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
139 return (savedmstone ? OVERRIDE_NEW_MSTONE : OVERRIDE_NEW_PROFILE);
142 return OVERRIDE_NONE;
145 // Copies a pref override file into the user's profile pref-override folder,
146 // and then tells the pref service to reload it's default prefs.
147 function copyPrefOverride() {
148 try {
149 var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
150 .getService(Components.interfaces.nsIProperties);
151 const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
152 var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
153 Components.interfaces.nsIFile);
154 if (!prefOverride.exists())
155 return; // nothing to do
157 const NS_APP_PREFS_OVERRIDE_DIR = "PrefDOverride";
158 var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
159 Components.interfaces.nsIFile);
161 // Check for any existing pref overrides, and remove them if present
162 var existingPrefOverridesFile = prefOverridesDir.clone();
163 existingPrefOverridesFile.append(prefOverride.leafName);
164 if (existingPrefOverridesFile.exists())
165 existingPrefOverridesFile.remove(false);
167 prefOverride.copyTo(prefOverridesDir, null);
169 // Now that we've installed the new-profile pref override file,
170 // re-read the default prefs.
171 var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
172 .getService(Components.interfaces.nsIObserver);
173 prefSvcObs.observe(null, "reload-default-prefs", null);
174 } catch (ex) {
175 Components.utils.reportError(ex);
179 // Flag used to indicate that the arguments to openWindow can be passed directly.
180 const NO_EXTERNAL_URIS = 1;
182 function openWindow(parent, url, target, features, args, noExternalArgs) {
183 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
184 .getService(nsIWindowWatcher);
186 if (noExternalArgs == NO_EXTERNAL_URIS) {
187 // Just pass in the defaultArgs directly
188 var argstring;
189 if (args) {
190 argstring = Components.classes["@mozilla.org/supports-string;1"]
191 .createInstance(nsISupportsString);
192 argstring.data = args;
195 return wwatch.openWindow(parent, url, target, features, argstring);
198 // Pass an array to avoid the browser "|"-splitting behavior.
199 var argArray = Components.classes["@mozilla.org/supports-array;1"]
200 .createInstance(Components.interfaces.nsISupportsArray);
202 // add args to the arguments array
203 var stringArgs = null;
204 if (args instanceof Array) // array
205 stringArgs = args;
206 else if (args) // string
207 stringArgs = [args];
209 if (stringArgs) {
210 // put the URIs into argArray
211 var uriArray = Components.classes["@mozilla.org/supports-array;1"]
212 .createInstance(Components.interfaces.nsISupportsArray);
213 stringArgs.forEach(function (uri) {
214 var sstring = Components.classes["@mozilla.org/supports-string;1"]
215 .createInstance(nsISupportsString);
216 sstring.data = uri;
217 uriArray.AppendElement(sstring);
219 argArray.AppendElement(uriArray);
220 } else {
221 argArray.AppendElement(null);
224 // Pass these as null to ensure that we always trigger the "single URL"
225 // behavior in browser.js's BrowserStartup (which handles the window
226 // arguments)
227 argArray.AppendElement(null); // charset
228 argArray.AppendElement(null); // referer
229 argArray.AppendElement(null); // postData
230 argArray.AppendElement(null); // allowThirdPartyFixup
232 return wwatch.openWindow(parent, url, target, features, argArray);
235 function openPreferences() {
236 var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
237 var url = "chrome://browser/content/preferences/preferences.xul";
239 var win = getMostRecentWindow("Browser:Preferences");
240 if (win) {
241 win.focus();
242 } else {
243 openWindow(null, url, "_blank", features);
247 function getMostRecentWindow(aType) {
248 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
249 .getService(nsIWindowMediator);
250 return wm.getMostRecentWindow(aType);
253 #ifdef XP_UNIX
254 #ifndef XP_MACOSX
255 #define BROKEN_WM_Z_ORDER
256 #endif
257 #endif
258 #ifdef XP_OS2
259 #define BROKEN_WM_Z_ORDER
260 #endif
262 // this returns the most recent non-popup browser window
263 function getMostRecentBrowserWindow() {
264 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
265 .getService(Components.interfaces.nsIWindowMediator);
267 #ifdef BROKEN_WM_Z_ORDER
268 var win = wm.getMostRecentWindow("navigator:browser", true);
270 // if we're lucky, this isn't a popup, and we can just return this
271 if (win && win.document.documentElement.getAttribute("chromehidden")) {
272 var windowList = wm.getEnumerator("navigator:browser", true);
273 // this is oldest to newest, so this gets a bit ugly
274 while (windowList.hasMoreElements()) {
275 var nextWin = windowList.getNext();
276 if (!nextWin.document.documentElement.getAttribute("chromehidden"))
277 win = nextWin;
280 #else
281 var windowList = wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
282 if (!windowList.hasMoreElements())
283 return null;
285 var win = windowList.getNext();
286 while (win.document.documentElement.getAttribute("chromehidden")) {
287 if (!windowList.hasMoreElements())
288 return null;
290 win = windowList.getNext();
292 #endif
294 return win;
297 function doSearch(searchTerm, cmdLine) {
298 var ss = Components.classes["@mozilla.org/browser/search-service;1"]
299 .getService(nsIBrowserSearchService);
301 var submission = ss.defaultEngine.getSubmission(searchTerm, null);
303 // fill our nsISupportsArray with uri-as-wstring, null, null, postData
304 var sa = Components.classes["@mozilla.org/supports-array;1"]
305 .createInstance(Components.interfaces.nsISupportsArray);
307 var wuri = Components.classes["@mozilla.org/supports-string;1"]
308 .createInstance(Components.interfaces.nsISupportsString);
309 wuri.data = submission.uri.spec;
311 sa.AppendElement(wuri);
312 sa.AppendElement(null);
313 sa.AppendElement(null);
314 sa.AppendElement(submission.postData);
316 // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
317 // preferences, but need nsIBrowserDOMWindow extensions
319 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
320 .getService(nsIWindowWatcher);
322 return wwatch.openWindow(null, nsBrowserContentHandler.chromeURL,
323 "_blank",
324 "chrome,dialog=no,all" +
325 nsBrowserContentHandler.getFeatures(cmdLine),
326 sa);
329 var nsBrowserContentHandler = {
330 /* helper functions */
332 mChromeURL : null,
334 get chromeURL() {
335 if (this.mChromeURL) {
336 return this.mChromeURL;
339 var prefb = Components.classes["@mozilla.org/preferences-service;1"]
340 .getService(nsIPrefBranch);
341 this.mChromeURL = prefb.getCharPref("browser.chromeURL");
343 return this.mChromeURL;
346 /* nsISupports */
347 QueryInterface : function bch_QI(iid) {
348 if (!iid.equals(nsISupports) &&
349 !iid.equals(nsICommandLineHandler) &&
350 !iid.equals(nsIBrowserHandler) &&
351 !iid.equals(nsIContentHandler) &&
352 !iid.equals(nsICommandLineValidator) &&
353 !iid.equals(nsIFactory))
354 throw Components.results.NS_ERROR_NO_INTERFACE;
356 return this;
359 /* nsICommandLineHandler */
360 handle : function bch_handle(cmdLine) {
361 if (cmdLine.handleFlag("browser", false)) {
362 // Passing defaultArgs, so use NO_EXTERNAL_URIS
363 openWindow(null, this.chromeURL, "_blank",
364 "chrome,dialog=no,all" + this.getFeatures(cmdLine),
365 this.defaultArgs, NO_EXTERNAL_URIS);
366 cmdLine.preventDefault = true;
369 try {
370 var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
372 catch (e) {
373 throw NS_ERROR_ABORT;
376 if (remoteCommand != null) {
377 try {
378 var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
379 var remoteVerb;
380 if (a) {
381 remoteVerb = a[1].toLowerCase();
382 var remoteParams = [];
383 var sepIndex = a[2].lastIndexOf(",");
384 if (sepIndex == -1)
385 remoteParams[0] = a[2];
386 else {
387 remoteParams[0] = a[2].substring(0, sepIndex);
388 remoteParams[1] = a[2].substring(sepIndex + 1);
392 switch (remoteVerb) {
393 case "openurl":
394 case "openfile":
395 // openURL(<url>)
396 // openURL(<url>,new-window)
397 // openURL(<url>,new-tab)
399 // First param is the URL, second param (if present) is the "target"
400 // (tab, window)
401 var url = remoteParams[0];
402 var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
403 if (remoteParams[1]) {
404 var targetParam = remoteParams[1].toLowerCase()
405 .replace(/^\s*|\s*$/g, "");
406 if (targetParam == "new-tab")
407 target = nsIBrowserDOMWindow.OPEN_NEWTAB;
408 else if (targetParam == "new-window")
409 target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
410 else {
411 // The "target" param isn't one of our supported values, so
412 // assume it's part of a URL that contains commas.
413 url += "," + remoteParams[1];
417 var uri = resolveURIInternal(cmdLine, url);
418 handURIToExistingBrowser(uri, target, cmdLine);
419 break;
421 case "xfedocommand":
422 // xfeDoCommand(openBrowser)
423 if (remoteParams[0].toLowerCase() != "openbrowser")
424 throw NS_ERROR_ABORT;
426 // Passing defaultArgs, so use NO_EXTERNAL_URIS
427 openWindow(null, this.chromeURL, "_blank",
428 "chrome,dialog=no,all" + this.getFeatures(cmdLine),
429 this.defaultArgs, NO_EXTERNAL_URIS);
430 break;
432 default:
433 // Somebody sent us a remote command we don't know how to process:
434 // just abort.
435 throw "Unknown remote command.";
438 cmdLine.preventDefault = true;
440 catch (e) {
441 Components.utils.reportError(e);
442 // If we had a -remote flag but failed to process it, throw
443 // NS_ERROR_ABORT so that the xremote code knows to return a failure
444 // back to the handling code.
445 throw NS_ERROR_ABORT;
449 var uriparam;
450 try {
451 while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
452 var uri = resolveURIInternal(cmdLine, uriparam);
453 if (!shouldLoadURI(uri))
454 continue;
455 openWindow(null, this.chromeURL, "_blank",
456 "chrome,dialog=no,all" + this.getFeatures(cmdLine),
457 uri.spec);
458 cmdLine.preventDefault = true;
461 catch (e) {
462 Components.utils.reportError(e);
465 try {
466 while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
467 var uri = resolveURIInternal(cmdLine, uriparam);
468 handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
469 cmdLine.preventDefault = true;
472 catch (e) {
473 Components.utils.reportError(e);
476 var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
477 if (chromeParam) {
479 // Handle the old preference dialog URL separately (bug 285416)
480 if (chromeParam == "chrome://browser/content/pref/pref.xul") {
481 openPreferences();
482 cmdLine.preventDefault = true;
483 } else try {
484 // only load URIs which do not inherit chrome privs
485 var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
486 var uri = resolveURIInternal(cmdLine, chromeParam);
487 var netutil = Components.classes["@mozilla.org/network/util;1"]
488 .getService(nsINetUtil);
489 if (!netutil.URIChainHasFlags(uri, URI_INHERITS_SECURITY_CONTEXT)) {
490 openWindow(null, uri.spec, "_blank", features);
491 cmdLine.preventDefault = true;
494 catch (e) {
495 Components.utils.reportError(e);
498 if (cmdLine.handleFlag("preferences", false)) {
499 openPreferences();
500 cmdLine.preventDefault = true;
502 if (cmdLine.handleFlag("silent", false))
503 cmdLine.preventDefault = true;
505 var searchParam = cmdLine.handleFlagWithParam("search", false);
506 if (searchParam) {
507 doSearch(searchParam, cmdLine);
508 cmdLine.preventDefault = true;
511 #ifdef XP_WIN
512 // Handle "? searchterm" for Windows Vista start menu integration
513 for (var i = cmdLine.length - 1; i >= 0; --i) {
514 var param = cmdLine.getArgument(i);
515 if (param.match(/^\? /)) {
516 cmdLine.removeArguments(i, i);
517 cmdLine.preventDefault = true;
519 searchParam = param.substr(2);
520 doSearch(searchParam, cmdLine);
523 #endif
526 helpInfo : " -browser Open a browser window.\n",
528 /* nsIBrowserHandler */
530 get defaultArgs() {
531 var prefb = Components.classes["@mozilla.org/preferences-service;1"]
532 .getService(nsIPrefBranch);
533 var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
534 .getService(Components.interfaces.nsIURLFormatter);
536 var overridePage = "";
537 var haveUpdateSession = false;
538 try {
539 switch (needHomepageOverride(prefb)) {
540 case OVERRIDE_NEW_PROFILE:
541 // New profile
542 overridePage = formatter.formatURLPref("startup.homepage_welcome_url");
543 break;
544 case OVERRIDE_NEW_MSTONE:
545 // Existing profile, new build
546 copyPrefOverride();
548 // Check whether we have a session to restore. If we do, we assume
549 // that this is an "update" session.
550 var ss = Components.classes["@mozilla.org/browser/sessionstartup;1"]
551 .getService(Components.interfaces.nsISessionStartup);
552 haveUpdateSession = ss.doRestore();
553 overridePage = formatter.formatURLPref("startup.homepage_override_url");
554 break;
556 } catch (ex) {}
558 // formatURLPref might return "about:blank" if getting the pref fails
559 if (overridePage == "about:blank")
560 overridePage = "";
562 var startPage = "";
563 try {
564 var choice = prefb.getIntPref("browser.startup.page");
565 if (choice == 1 || choice == 3)
566 startPage = this.startPage;
568 if (choice == 2)
569 startPage = Components.classes["@mozilla.org/browser/global-history;2"]
570 .getService(nsIBrowserHistory).lastPageVisited;
571 } catch (e) {
572 Components.utils.reportError(e);
575 if (startPage == "about:blank")
576 startPage = "";
578 // Only show the startPage if we're not restoring an update session.
579 if (overridePage && startPage && !haveUpdateSession)
580 return overridePage + "|" + startPage;
582 return overridePage || startPage || "about:blank";
585 get startPage() {
586 var prefb = Components.classes["@mozilla.org/preferences-service;1"]
587 .getService(nsIPrefBranch);
589 var uri = prefb.getComplexValue("browser.startup.homepage",
590 nsIPrefLocalizedString).data;
592 if (!uri) {
593 prefb.clearUserPref("browser.startup.homepage");
594 uri = prefb.getComplexValue("browser.startup.homepage",
595 nsIPrefLocalizedString).data;
598 var count;
599 try {
600 count = prefb.getIntPref("browser.startup.homepage.count");
602 catch (e) {
603 return uri;
606 for (var i = 1; i < count; ++i) {
607 try {
608 var page = prefb.getComplexValue("browser.startup.homepage." + i,
609 nsIPrefLocalizedString).data;
610 uri += "\n" + page;
612 catch (e) {
616 return uri;
619 mFeatures : null,
621 getFeatures : function bch_features(cmdLine) {
622 if (this.mFeatures === null) {
623 this.mFeatures = "";
625 try {
626 var width = cmdLine.handleFlagWithParam("width", false);
627 var height = cmdLine.handleFlagWithParam("height", false);
629 if (width)
630 this.mFeatures += ",width=" + width;
631 if (height)
632 this.mFeatures += ",height=" + height;
634 catch (e) {
638 return this.mFeatures;
641 /* nsIContentHandler */
643 handleContent : function bch_handleContent(contentType, context, request) {
644 try {
645 var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
646 .getService(nsIWebNavigationInfo);
647 if (!webNavInfo.isTypeSupported(contentType, null)) {
648 throw NS_ERROR_WONT_HANDLE_CONTENT;
650 } catch (e) {
651 throw NS_ERROR_WONT_HANDLE_CONTENT;
654 request.QueryInterface(nsIChannel);
655 handURIToExistingBrowser(request.URI,
656 nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, null);
657 request.cancel(NS_BINDING_ABORTED);
660 /* nsICommandLineValidator */
661 validate : function bch_validate(cmdLine) {
662 // Other handlers may use osint so only handle the osint flag if the url
663 // flag is also present and the command line is valid.
664 var osintFlagIdx = cmdLine.findFlag("osint", false);
665 var urlFlagIdx = cmdLine.findFlag("url", false);
666 if (urlFlagIdx > -1 && (osintFlagIdx > -1 ||
667 cmdLine.state == nsICommandLine.STATE_REMOTE_EXPLICIT)) {
668 var urlParam = cmdLine.getArgument(urlFlagIdx + 1);
669 if (cmdLine.length != urlFlagIdx + 2 || /firefoxurl:/.test(urlParam))
670 throw NS_ERROR_ABORT;
671 cmdLine.handleFlag("osint", false)
675 /* nsIFactory */
676 createInstance: function bch_CI(outer, iid) {
677 if (outer != null)
678 throw Components.results.NS_ERROR_NO_AGGREGATION;
680 return this.QueryInterface(iid);
683 lockFactory : function bch_lock(lock) {
684 /* no-op */
688 const bch_contractID = "@mozilla.org/browser/clh;1";
689 const bch_CID = Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}");
690 const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type=";
692 function handURIToExistingBrowser(uri, location, cmdLine)
694 if (!shouldLoadURI(uri))
695 return;
697 var navWin = getMostRecentBrowserWindow();
698 if (!navWin) {
699 // if we couldn't load it in an existing window, open a new one
700 openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
701 "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
702 uri.spec);
703 return;
706 var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
707 .getInterface(nsIWebNavigation);
708 var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
709 var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
710 .getInterface(nsIDOMWindow);
711 var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
712 bwin.openURI(uri, null, location,
713 nsIBrowserDOMWindow.OPEN_EXTERNAL);
717 var nsDefaultCommandLineHandler = {
718 /* nsISupports */
719 QueryInterface : function dch_QI(iid) {
720 if (!iid.equals(nsISupports) &&
721 !iid.equals(nsICommandLineHandler) &&
722 !iid.equals(nsIFactory))
723 throw Components.results.NS_ERROR_NO_INTERFACE;
725 return this;
728 // List of uri's that were passed via the command line without the app
729 // running and have already been handled. This is compared against uri's
730 // opened using DDE on Win32 so we only open one of the requests.
731 _handledURIs: [ ],
732 #ifdef XP_WIN
733 _haveProfile: false,
734 #endif
736 /* nsICommandLineHandler */
737 handle : function dch_handle(cmdLine) {
738 var urilist = [];
740 #ifdef XP_WIN
741 // If we don't have a profile selected yet (e.g. the Profile Manager is
742 // displayed) we will crash if we open an url and then select a profile. To
743 // prevent this handle all url command line flags and set the command line's
744 // preventDefault to true to prevent the display of the ui. The initial
745 // command line will be retained when nsAppRunner calls LaunchChild though
746 // urls launched after the initial launch will be lost.
747 if (!this._haveProfile) {
748 try {
749 // This will throw when a profile has not been selected.
750 var fl = Components.classes["@mozilla.org/file/directory_service;1"]
751 .getService(Components.interfaces.nsIProperties);
752 var dir = fl.get("ProfD", Components.interfaces.nsILocalFile);
753 this._haveProfile = true;
755 catch (e) {
756 while ((ar = cmdLine.handleFlagWithParam("url", false))) { }
757 cmdLine.preventDefault = true;
760 #endif
762 try {
763 var ar;
764 while ((ar = cmdLine.handleFlagWithParam("url", false))) {
765 var found = false;
766 var uri = resolveURIInternal(cmdLine, ar);
767 // count will never be greater than zero except on Win32.
768 var count = this._handledURIs.length;
769 for (var i = 0; i < count; ++i) {
770 if (this._handledURIs[i].spec == uri.spec) {
771 this._handledURIs.splice(i, 1);
772 found = true;
773 cmdLine.preventDefault = true;
774 break;
777 if (!found) {
778 urilist.push(uri);
779 // The requestpending command line flag is only used on Win32.
780 if (cmdLine.handleFlag("requestpending", false) &&
781 cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
782 this._handledURIs.push(uri)
786 catch (e) {
787 Components.utils.reportError(e);
790 count = cmdLine.length;
792 for (i = 0; i < count; ++i) {
793 var curarg = cmdLine.getArgument(i);
794 if (curarg.match(/^-/)) {
795 Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
796 // To emulate the pre-nsICommandLine behavior, we ignore
797 // the argument after an unrecognized flag.
798 ++i;
799 } else {
800 try {
801 urilist.push(resolveURIInternal(cmdLine, curarg));
803 catch (e) {
804 Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
809 if (urilist.length) {
810 if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
811 urilist.length == 1) {
812 // Try to find an existing window and load our URI into the
813 // current tab, new tab, or new window as prefs determine.
814 try {
815 handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
816 return;
818 catch (e) {
822 var URLlist = urilist.filter(shouldLoadURI).map(function (u) u.spec);
823 if (URLlist.length) {
824 openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
825 "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
826 URLlist);
830 else if (!cmdLine.preventDefault) {
831 // Passing defaultArgs, so use NO_EXTERNAL_URIS
832 openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
833 "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
834 nsBrowserContentHandler.defaultArgs, NO_EXTERNAL_URIS);
838 // XXX localize me... how?
839 helpInfo : "Usage: firefox [-flags] [<url>]\n",
841 /* nsIFactory */
842 createInstance: function dch_CI(outer, iid) {
843 if (outer != null)
844 throw Components.results.NS_ERROR_NO_AGGREGATION;
846 return this.QueryInterface(iid);
849 lockFactory : function dch_lock(lock) {
850 /* no-op */
854 const dch_contractID = "@mozilla.org/browser/final-clh;1";
855 const dch_CID = Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}");
857 var Module = {
858 /* nsISupports */
859 QueryInterface: function mod_QI(iid) {
860 if (iid.equals(Components.interfaces.nsIModule) ||
861 iid.equals(Components.interfaces.nsISupports))
862 return this;
864 throw Components.results.NS_ERROR_NO_INTERFACE;
867 /* nsIModule */
868 getClassObject: function mod_getco(compMgr, cid, iid) {
869 if (cid.equals(bch_CID))
870 return nsBrowserContentHandler.QueryInterface(iid);
872 if (cid.equals(dch_CID))
873 return nsDefaultCommandLineHandler.QueryInterface(iid);
875 throw Components.results.NS_ERROR_NO_INTERFACE;
878 registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
879 if (Components.classes["@mozilla.org/xre/app-info;1"]) {
880 // Don't register these if Firefox is launching a XULRunner application
881 const FIREFOX_UID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
882 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
883 .getService(Components.interfaces.nsIXULAppInfo);
884 if (appInfo.ID != FIREFOX_UID)
885 return;
888 var compReg =
889 compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
891 compReg.registerFactoryLocation( bch_CID,
892 "nsBrowserContentHandler",
893 bch_contractID,
894 fileSpec,
895 location,
896 type );
897 compReg.registerFactoryLocation( dch_CID,
898 "nsDefaultCommandLineHandler",
899 dch_contractID,
900 fileSpec,
901 location,
902 type );
904 function registerType(contentType) {
905 compReg.registerFactoryLocation( bch_CID,
906 "Browser Cmdline Handler",
907 CONTRACTID_PREFIX + contentType,
908 fileSpec,
909 location,
910 type );
913 registerType("text/html");
914 registerType("application/vnd.mozilla.xul+xml");
915 #ifdef MOZ_SVG
916 registerType("image/svg+xml");
917 #endif
918 registerType("text/rdf");
919 registerType("text/xml");
920 registerType("application/xhtml+xml");
921 registerType("text/css");
922 registerType("text/plain");
923 registerType("image/gif");
924 registerType("image/jpeg");
925 registerType("image/jpg");
926 registerType("image/png");
927 registerType("image/bmp");
928 registerType("image/x-icon");
929 registerType("image/vnd.microsoft.icon");
930 registerType("image/x-xbitmap");
931 registerType("application/http-index-format");
933 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
934 .getService(nsICategoryManager);
936 catMan.addCategoryEntry("command-line-handler",
937 "m-browser",
938 bch_contractID, true, true);
939 catMan.addCategoryEntry("command-line-handler",
940 "x-default",
941 dch_contractID, true, true);
942 catMan.addCategoryEntry("command-line-validator",
943 "b-browser",
944 bch_contractID, true, true);
947 unregisterSelf : function mod_unregself(compMgr, location, type) {
948 var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
949 compReg.unregisterFactoryLocation(bch_CID, location);
950 compReg.unregisterFactoryLocation(dch_CID, location);
952 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
953 .getService(nsICategoryManager);
955 catMan.deleteCategoryEntry("command-line-handler",
956 "m-browser", true);
957 catMan.deleteCategoryEntry("command-line-handler",
958 "x-default", true);
959 catMan.deleteCategoryEntry("command-line-validator",
960 "b-browser", true);
963 canUnload: function(compMgr) {
964 return true;
968 // NSGetModule: Return the nsIModule object.
969 function NSGetModule(compMgr, fileSpec) {
970 return Module;