Bug 473045 - Update to nsIHandlerApp for win7 jump lists (plus tests). r=bz
[mozilla-central.git] / uriloader / exthandler / nsWebHandlerApp.js
blobcd5a74ad3bdc8e752263150b79905a2d0ad0eefe
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 ***** */
40 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
42 ////////////////////////////////////////////////////////////////////////////////
43 //// Constants
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   //////////////////////////////////////////////////////////////////////////////
56   //// nsWebHandler
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",
62   _name: null,
63   _detailedDescription: null,
64   _uriTemplate: null,
66   //////////////////////////////////////////////////////////////////////////////
67   //// nsIHandlerApp
69   get name() {
70     return this._name;
71   },
73   set name(aName) {
74     this._name = aName;
75   },
77   get detailedDescription() {
78     return this._detailedDescription;
79   },
81   set detailedDescription(aDesc) {
82     this._detailedDescription = aDesc;
83   },
85   equals: function(aHandlerApp) {
86     if (!aHandlerApp)
87       throw Cr.NS_ERROR_NULL_POINTER;
89     if (aHandlerApp instanceof Ci.nsIWebHandlerApp &&
90         aHandlerApp.uriTemplate &&
91         this.uriTemplate &&
92         aHandlerApp.uriTemplate == this.uriTemplate)
93       return true;
95     return false;
96   },
98   launchWithURI: function nWHA__launchWithURI(aURI, aWindowContext) {
100     // XXX need to strip passwd & username from URI to handle, as per the
101     // WhatWG HTML5 draft.  nsSimpleURL, which is what we're going to get,
102     // can't do this directly.  Ideally, we'd fix nsStandardURL to make it
103     // possible to turn off all of its quirks handling, and use that...
105     // encode the URI to be handled
106     var escapedUriSpecToHandle = encodeURIComponent(aURI.spec);
108     // insert the encoded URI and create the object version 
109     var uriSpecToSend = this.uriTemplate.replace("%s", escapedUriSpecToHandle);
110     var ioService = Cc["@mozilla.org/network/io-service;1"].
111                     getService(Ci.nsIIOService);
112     var uriToSend = ioService.newURI(uriSpecToSend, null, null);
113     
114     // if we have a window context, use the URI loader to load there
115     if (aWindowContext) {
117       // create a channel from this URI
118       var channel = ioService.newChannelFromURI(uriToSend);
119       channel.loadFlags = Ci.nsIChannel.LOAD_DOCUMENT_URI;
121       // load the channel
122       var uriLoader = Cc["@mozilla.org/uriloader;1"].
123                       getService(Ci.nsIURILoader);
124       // XXX ideally, aIsContentPreferred (the second param) should really be
125       // passed in from above.  Practically, true is probably a reasonable
126       // default since browsers don't care much, and link click is likely to be
127       // the more interesting case for non-browser apps.  See 
128       // <https://bugzilla.mozilla.org/show_bug.cgi?id=392957#c9> for details.
129       uriLoader.openURI(channel, true, aWindowContext);
130       return;
131     } 
133     // since we don't have a window context, hand it off to a browser
134     var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
135       getService(Ci.nsIWindowMediator);
137     // get browser dom window
138     var browserDOMWin = windowMediator.getMostRecentWindow("navigator:browser")
139                         .QueryInterface(Ci.nsIDOMChromeWindow)
140                         .browserDOMWindow;
142     // if we got an exception, there are several possible reasons why:
143     // a) this gecko embedding doesn't provide an nsIBrowserDOMWindow
144     //    implementation (i.e. doesn't support browser-style functionality),
145     //    so we need to kick the URL out to the OS default browser.  This is
146     //    the subject of bug 394479.
147     // b) this embedding does provide an nsIBrowserDOMWindow impl, but
148     //    there doesn't happen to be a browser window open at the moment; one
149     //    should be opened.  It's not clear whether this situation will really
150     //    ever occur in real life.  If it does, the only API that I can find
151     //    that seems reasonably likely to work for most embedders is the
152     //    command line handler.  
153     // c) something else went wrong 
154     //
155     // it's not clear how one would differentiate between the three cases
156     // above, so for now we don't catch the exception
158     // openURI
159     browserDOMWin.openURI(uriToSend,
160                           null, // no window.opener 
161                           Ci.nsIBrowserDOMWindow.OPEN_DEFAULT_WINDOW,
162                           Ci.nsIBrowserDOMWindow.OPEN_NEW);
163       
164     return;
165   },
167   //////////////////////////////////////////////////////////////////////////////
168   //// nsIWebHandlerApp
170   get uriTemplate() {
171     return this._uriTemplate;
172   },
174   set uriTemplate(aURITemplate) {
175     this._uriTemplate = aURITemplate;
176   },
178   //////////////////////////////////////////////////////////////////////////////
179   //// nsISupports
181   QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebHandlerApp, Ci.nsIHandlerApp])
184 ////////////////////////////////////////////////////////////////////////////////
185 //// Module
187 let components = [nsWebHandlerApp];
189 function NSGetModule(compMgr, fileSpec)
191   return XPCOMUtils.generateModule(components);