uriloader/exthandler/nsWebHandlerApp.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 6
michael@0 7 ////////////////////////////////////////////////////////////////////////////////
michael@0 8 //// Constants
michael@0 9
michael@0 10 const Ci = Components.interfaces;
michael@0 11 const Cr = Components.results;
michael@0 12 const Cc = Components.classes;
michael@0 13
michael@0 14 ////////////////////////////////////////////////////////////////////////////////
michael@0 15 //// nsWebHandler class
michael@0 16
michael@0 17 function nsWebHandlerApp() {}
michael@0 18
michael@0 19 nsWebHandlerApp.prototype = {
michael@0 20 //////////////////////////////////////////////////////////////////////////////
michael@0 21 //// nsWebHandler
michael@0 22
michael@0 23 classDescription: "A web handler for protocols and content",
michael@0 24 classID: Components.ID("8b1ae382-51a9-4972-b930-56977a57919d"),
michael@0 25 contractID: "@mozilla.org/uriloader/web-handler-app;1",
michael@0 26
michael@0 27 _name: null,
michael@0 28 _detailedDescription: null,
michael@0 29 _uriTemplate: null,
michael@0 30
michael@0 31 //////////////////////////////////////////////////////////////////////////////
michael@0 32 //// nsIHandlerApp
michael@0 33
michael@0 34 get name() {
michael@0 35 return this._name;
michael@0 36 },
michael@0 37
michael@0 38 set name(aName) {
michael@0 39 this._name = aName;
michael@0 40 },
michael@0 41
michael@0 42 get detailedDescription() {
michael@0 43 return this._detailedDescription;
michael@0 44 },
michael@0 45
michael@0 46 set detailedDescription(aDesc) {
michael@0 47 this._detailedDescription = aDesc;
michael@0 48 },
michael@0 49
michael@0 50 equals: function(aHandlerApp) {
michael@0 51 if (!aHandlerApp)
michael@0 52 throw Cr.NS_ERROR_NULL_POINTER;
michael@0 53
michael@0 54 if (aHandlerApp instanceof Ci.nsIWebHandlerApp &&
michael@0 55 aHandlerApp.uriTemplate &&
michael@0 56 this.uriTemplate &&
michael@0 57 aHandlerApp.uriTemplate == this.uriTemplate)
michael@0 58 return true;
michael@0 59
michael@0 60 return false;
michael@0 61 },
michael@0 62
michael@0 63 launchWithURI: function nWHA__launchWithURI(aURI, aWindowContext) {
michael@0 64
michael@0 65 // XXX need to strip passwd & username from URI to handle, as per the
michael@0 66 // WhatWG HTML5 draft. nsSimpleURL, which is what we're going to get,
michael@0 67 // can't do this directly. Ideally, we'd fix nsStandardURL to make it
michael@0 68 // possible to turn off all of its quirks handling, and use that...
michael@0 69
michael@0 70 // encode the URI to be handled
michael@0 71 var escapedUriSpecToHandle = encodeURIComponent(aURI.spec);
michael@0 72
michael@0 73 // insert the encoded URI and create the object version
michael@0 74 var uriSpecToSend = this.uriTemplate.replace("%s", escapedUriSpecToHandle);
michael@0 75 var ioService = Cc["@mozilla.org/network/io-service;1"].
michael@0 76 getService(Ci.nsIIOService);
michael@0 77 var uriToSend = ioService.newURI(uriSpecToSend, null, null);
michael@0 78
michael@0 79 // if we have a window context, use the URI loader to load there
michael@0 80 if (aWindowContext) {
michael@0 81
michael@0 82 // create a channel from this URI
michael@0 83 var channel = ioService.newChannelFromURI(uriToSend);
michael@0 84 channel.loadFlags = Ci.nsIChannel.LOAD_DOCUMENT_URI;
michael@0 85
michael@0 86 // load the channel
michael@0 87 var uriLoader = Cc["@mozilla.org/uriloader;1"].
michael@0 88 getService(Ci.nsIURILoader);
michael@0 89 // XXX ideally, whether to pass the IS_CONTENT_PREFERRED flag should be
michael@0 90 // passed in from above. Practically, the flag is probably a reasonable
michael@0 91 // default since browsers don't care much, and link click is likely to be
michael@0 92 // the more interesting case for non-browser apps. See
michael@0 93 // <https://bugzilla.mozilla.org/show_bug.cgi?id=392957#c9> for details.
michael@0 94 uriLoader.openURI(channel, Ci.nsIURILoader.IS_CONTENT_PREFERRED,
michael@0 95 aWindowContext);
michael@0 96 return;
michael@0 97 }
michael@0 98
michael@0 99 // since we don't have a window context, hand it off to a browser
michael@0 100 var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
michael@0 101 getService(Ci.nsIWindowMediator);
michael@0 102
michael@0 103 // get browser dom window
michael@0 104 var browserDOMWin = windowMediator.getMostRecentWindow("navigator:browser")
michael@0 105 .QueryInterface(Ci.nsIDOMChromeWindow)
michael@0 106 .browserDOMWindow;
michael@0 107
michael@0 108 // if we got an exception, there are several possible reasons why:
michael@0 109 // a) this gecko embedding doesn't provide an nsIBrowserDOMWindow
michael@0 110 // implementation (i.e. doesn't support browser-style functionality),
michael@0 111 // so we need to kick the URL out to the OS default browser. This is
michael@0 112 // the subject of bug 394479.
michael@0 113 // b) this embedding does provide an nsIBrowserDOMWindow impl, but
michael@0 114 // there doesn't happen to be a browser window open at the moment; one
michael@0 115 // should be opened. It's not clear whether this situation will really
michael@0 116 // ever occur in real life. If it does, the only API that I can find
michael@0 117 // that seems reasonably likely to work for most embedders is the
michael@0 118 // command line handler.
michael@0 119 // c) something else went wrong
michael@0 120 //
michael@0 121 // it's not clear how one would differentiate between the three cases
michael@0 122 // above, so for now we don't catch the exception
michael@0 123
michael@0 124 // openURI
michael@0 125 browserDOMWin.openURI(uriToSend,
michael@0 126 null, // no window.opener
michael@0 127 Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW,
michael@0 128 Ci.nsIBrowserDOMWindow.OPEN_NEW);
michael@0 129
michael@0 130 return;
michael@0 131 },
michael@0 132
michael@0 133 //////////////////////////////////////////////////////////////////////////////
michael@0 134 //// nsIWebHandlerApp
michael@0 135
michael@0 136 get uriTemplate() {
michael@0 137 return this._uriTemplate;
michael@0 138 },
michael@0 139
michael@0 140 set uriTemplate(aURITemplate) {
michael@0 141 this._uriTemplate = aURITemplate;
michael@0 142 },
michael@0 143
michael@0 144 //////////////////////////////////////////////////////////////////////////////
michael@0 145 //// nsISupports
michael@0 146
michael@0 147 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebHandlerApp, Ci.nsIHandlerApp])
michael@0 148 };
michael@0 149
michael@0 150 ////////////////////////////////////////////////////////////////////////////////
michael@0 151 //// Module
michael@0 152
michael@0 153 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsWebHandlerApp]);
michael@0 154

mercurial