!import
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 browser.
15 *
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.
20 *
21 * Contributor(s):
22 * Shawn Wilsher <me@shawnwilsher.com>
23 * Myk Melez <myk@mozilla.org>
24 * Dan Mosedale <dmose@mozilla.org>
25 *
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.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
41
42 ////////////////////////////////////////////////////////////////////////////////
43 //// Constants
44
45 const Ci = Components.interfaces;
46 const Cr = Components.results;
47 const Cc = Components.classes;
48
49 ////////////////////////////////////////////////////////////////////////////////
50 //// nsWebHandler class
51
nsWebHandlerApp
52 function nsWebHandlerApp() {}
53
54 nsWebHandlerApp.prototype = {
55 //////////////////////////////////////////////////////////////////////////////
56 //// nsWebHandler
57
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",
61
62 _name: null,
63 _uriTemplate: null,
64
65 //////////////////////////////////////////////////////////////////////////////
66 //// nsIHandlerApp
67
get_name
68 get name() {
69 return this._name;
70 },
71
set_name
72 set name(aName) {
73 this._name = aName;
74 },
75
equals
76 equals: function(aHandlerApp) {
77 if (!aHandlerApp)
78 throw Cr.NS_ERROR_NULL_POINTER;
79
80 if (aHandlerApp instanceof Ci.nsIWebHandlerApp &&
81 aHandlerApp.uriTemplate &&
82 this.uriTemplate &&
83 aHandlerApp.uriTemplate == this.uriTemplate)
84 return true;
85
86 return false;
87 },
88
nWHA__launchWithURI
89 launchWithURI: function nWHA__launchWithURI(aURI, aWindowContext) {
90
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...
95
96 // encode the URI to be handled
97 var escapedUriSpecToHandle = encodeURIComponent(aURI.spec);
98
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);
104
105 // if we have a window context, use the URI loader to load there
106 if (aWindowContext) {
107
108 // create a channel from this URI
109 var channel = ioService.newChannelFromURI(uriToSend);
110 channel.loadFlags = Ci.nsIChannel.LOAD_DOCUMENT_URI;
111
112 // load the channel
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);
121 return;
122 }
123
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);
127
128 // get browser dom window
129 var browserDOMWin = windowMediator.getMostRecentWindow("navigator:browser")
130 .QueryInterface(Ci.nsIDOMChromeWindow)
131 .browserDOMWindow;
132
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
145 //
146 // it's not clear how one would differentiate between the three cases
147 // above, so for now we don't catch the exception
148
149 // openURI
150 browserDOMWin.openURI(uriToSend,
151 null, // no window.opener
152 Ci.nsIBrowserDOMWindow.OPEN_DEFAULT_WINDOW,
153 Ci.nsIBrowserDOMWindow.OPEN_NEW);
154
155 return;
156 },
157
158 //////////////////////////////////////////////////////////////////////////////
159 //// nsIWebHandlerApp
160
get_uriTemplate
161 get uriTemplate() {
162 return this._uriTemplate;
163 },
164
set_uriTemplate
165 set uriTemplate(aURITemplate) {
166 this._uriTemplate = aURITemplate;
167 },
168
169 //////////////////////////////////////////////////////////////////////////////
170 //// nsISupports
171
172 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebHandlerApp, Ci.nsIHandlerApp])
173 };
174
175 ////////////////////////////////////////////////////////////////////////////////
176 //// Module
177
178 let components = [nsWebHandlerApp];
179
NSGetModule
180 function NSGetModule(compMgr, fileSpec)
181 {
182 return XPCOMUtils.generateModule(components);
183 }