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
14 * The Original Code is the Mozilla browser.
16 * The Initial Developer of the Original Code is
17 * Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
22 * Shawn Wilsher <me@shawnwilsher.com>
23 * Myk Melez <myk@mozilla.org>
24 * Dan Mosedale <dmose@mozilla.org>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 Components
.utils
.import("resource://gre/modules/XPCOMUtils.jsm");
42 ////////////////////////////////////////////////////////////////////////////////
45 const Ci
= Components
.interfaces
;
46 const Cr
= Components
.results
;
47 const Cc
= Components
.classes
;
49 ////////////////////////////////////////////////////////////////////////////////
50 //// nsWebHandler class
52 function nsWebHandlerApp() {}
54 nsWebHandlerApp
.prototype = {
55 //////////////////////////////////////////////////////////////////////////////
58 classDescription
: "A web handler for protocols and content",
59 classID
: Components
.ID("8b1ae382-51a9-4972-b930-56977a57919d"),
60 contractID
: "@mozilla.org/uriloader/web-handler-app;1",
65 //////////////////////////////////////////////////////////////////////////////
76 equals: function(aHandlerApp
) {
78 throw Cr
.NS_ERROR_NULL_POINTER
;
80 if (aHandlerApp
instanceof Ci
.nsIWebHandlerApp
&&
81 aHandlerApp
.uriTemplate
&&
83 aHandlerApp
.uriTemplate
== this.uriTemplate
)
89 launchWithURI
: function nWHA__launchWithURI(aURI
, aWindowContext
) {
91 // XXX need to strip passwd & username from URI to handle, as per the
92 // WhatWG HTML5 draft. nsSimpleURL, which is what we're going to get,
93 // can't do this directly. Ideally, we'd fix nsStandardURL to make it
94 // possible to turn off all of its quirks handling, and use that...
96 // encode the URI to be handled
97 var escapedUriSpecToHandle
= encodeURIComponent(aURI
.spec
);
99 // insert the encoded URI and create the object version
100 var uriSpecToSend
= this.uriTemplate
.replace("%s", escapedUriSpecToHandle
);
101 var ioService
= Cc
["@mozilla.org/network/io-service;1"].
102 getService(Ci
.nsIIOService
);
103 var uriToSend
= ioService
.newURI(uriSpecToSend
, null, null);
105 // if we have a window context, use the URI loader to load there
106 if (aWindowContext
) {
108 // create a channel from this URI
109 var channel
= ioService
.newChannelFromURI(uriToSend
);
110 channel
.loadFlags
= Ci
.nsIChannel
.LOAD_DOCUMENT_URI
;
113 var uriLoader
= Cc
["@mozilla.org/uriloader;1"].
114 getService(Ci
.nsIURILoader
);
115 // XXX ideally, aIsContentPreferred (the second param) should really be
116 // passed in from above. Practically, true is probably a reasonable
117 // default since browsers don't care much, and link click is likely to be
118 // the more interesting case for non-browser apps. See
119 // <https://bugzilla.mozilla.org/show_bug.cgi?id=392957#c9> for details.
120 uriLoader
.openURI(channel
, true, aWindowContext
);
124 // since we don't have a window context, hand it off to a browser
125 var windowMediator
= Cc
["@mozilla.org/appshell/window-mediator;1"].
126 getService(Ci
.nsIWindowMediator
);
128 // get browser dom window
129 var browserDOMWin
= windowMediator
.getMostRecentWindow("navigator:browser")
130 .QueryInterface(Ci
.nsIDOMChromeWindow
)
133 // if we got an exception, there are several possible reasons why:
134 // a) this gecko embedding doesn't provide an nsIBrowserDOMWindow
135 // implementation (i.e. doesn't support browser-style functionality),
136 // so we need to kick the URL out to the OS default browser. This is
137 // the subject of bug 394479.
138 // b) this embedding does provide an nsIBrowserDOMWindow impl, but
139 // there doesn't happen to be a browser window open at the moment; one
140 // should be opened. It's not clear whether this situation will really
141 // ever occur in real life. If it does, the only API that I can find
142 // that seems reasonably likely to work for most embedders is the
143 // command line handler.
144 // c) something else went wrong
146 // it's not clear how one would differentiate between the three cases
147 // above, so for now we don't catch the exception
150 browserDOMWin
.openURI(uriToSend
,
151 null, // no window.opener
152 Ci
.nsIBrowserDOMWindow
.OPEN_DEFAULT_WINDOW
,
153 Ci
.nsIBrowserDOMWindow
.OPEN_NEW
);
158 //////////////////////////////////////////////////////////////////////////////
159 //// nsIWebHandlerApp
162 return this._uriTemplate
;
165 set uriTemplate(aURITemplate
) {
166 this._uriTemplate
= aURITemplate
;
169 //////////////////////////////////////////////////////////////////////////////
172 QueryInterface
: XPCOMUtils
.generateQI([Ci
.nsIWebHandlerApp
, Ci
.nsIHandlerApp
])
175 ////////////////////////////////////////////////////////////////////////////////
178 let components
= [nsWebHandlerApp
];
180 function NSGetModule(compMgr
, fileSpec
)
182 return XPCOMUtils
.generateModule(components
);