uriloader/exthandler/nsWebHandlerApp.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/uriloader/exthandler/nsWebHandlerApp.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.9 +
    1.10 +////////////////////////////////////////////////////////////////////////////////
    1.11 +//// Constants
    1.12 +
    1.13 +const Ci = Components.interfaces;
    1.14 +const Cr = Components.results;
    1.15 +const Cc = Components.classes;
    1.16 +
    1.17 +////////////////////////////////////////////////////////////////////////////////
    1.18 +//// nsWebHandler class
    1.19 +
    1.20 +function nsWebHandlerApp() {}
    1.21 +
    1.22 +nsWebHandlerApp.prototype = {
    1.23 +  //////////////////////////////////////////////////////////////////////////////
    1.24 +  //// nsWebHandler
    1.25 +
    1.26 +  classDescription: "A web handler for protocols and content",
    1.27 +  classID: Components.ID("8b1ae382-51a9-4972-b930-56977a57919d"),
    1.28 +  contractID: "@mozilla.org/uriloader/web-handler-app;1",
    1.29 +
    1.30 +  _name: null,
    1.31 +  _detailedDescription: null,
    1.32 +  _uriTemplate: null,
    1.33 +
    1.34 +  //////////////////////////////////////////////////////////////////////////////
    1.35 +  //// nsIHandlerApp
    1.36 +
    1.37 +  get name() {
    1.38 +    return this._name;
    1.39 +  },
    1.40 +
    1.41 +  set name(aName) {
    1.42 +    this._name = aName;
    1.43 +  },
    1.44 +
    1.45 +  get detailedDescription() {
    1.46 +    return this._detailedDescription;
    1.47 +  },
    1.48 +
    1.49 +  set detailedDescription(aDesc) {
    1.50 +    this._detailedDescription = aDesc;
    1.51 +  },
    1.52 +
    1.53 +  equals: function(aHandlerApp) {
    1.54 +    if (!aHandlerApp)
    1.55 +      throw Cr.NS_ERROR_NULL_POINTER;
    1.56 +
    1.57 +    if (aHandlerApp instanceof Ci.nsIWebHandlerApp &&
    1.58 +        aHandlerApp.uriTemplate &&
    1.59 +        this.uriTemplate &&
    1.60 +        aHandlerApp.uriTemplate == this.uriTemplate)
    1.61 +      return true;
    1.62 +
    1.63 +    return false;
    1.64 +  },
    1.65 +
    1.66 +  launchWithURI: function nWHA__launchWithURI(aURI, aWindowContext) {
    1.67 +
    1.68 +    // XXX need to strip passwd & username from URI to handle, as per the
    1.69 +    // WhatWG HTML5 draft.  nsSimpleURL, which is what we're going to get,
    1.70 +    // can't do this directly.  Ideally, we'd fix nsStandardURL to make it
    1.71 +    // possible to turn off all of its quirks handling, and use that...
    1.72 +
    1.73 +    // encode the URI to be handled
    1.74 +    var escapedUriSpecToHandle = encodeURIComponent(aURI.spec);
    1.75 +
    1.76 +    // insert the encoded URI and create the object version 
    1.77 +    var uriSpecToSend = this.uriTemplate.replace("%s", escapedUriSpecToHandle);
    1.78 +    var ioService = Cc["@mozilla.org/network/io-service;1"].
    1.79 +                    getService(Ci.nsIIOService);
    1.80 +    var uriToSend = ioService.newURI(uriSpecToSend, null, null);
    1.81 +    
    1.82 +    // if we have a window context, use the URI loader to load there
    1.83 +    if (aWindowContext) {
    1.84 +
    1.85 +      // create a channel from this URI
    1.86 +      var channel = ioService.newChannelFromURI(uriToSend);
    1.87 +      channel.loadFlags = Ci.nsIChannel.LOAD_DOCUMENT_URI;
    1.88 +
    1.89 +      // load the channel
    1.90 +      var uriLoader = Cc["@mozilla.org/uriloader;1"].
    1.91 +                      getService(Ci.nsIURILoader);
    1.92 +      // XXX ideally, whether to pass the IS_CONTENT_PREFERRED flag should be
    1.93 +      // passed in from above.  Practically, the flag is probably a reasonable
    1.94 +      // default since browsers don't care much, and link click is likely to be
    1.95 +      // the more interesting case for non-browser apps.  See 
    1.96 +      // <https://bugzilla.mozilla.org/show_bug.cgi?id=392957#c9> for details.
    1.97 +      uriLoader.openURI(channel, Ci.nsIURILoader.IS_CONTENT_PREFERRED,
    1.98 +                        aWindowContext);
    1.99 +      return;
   1.100 +    } 
   1.101 +
   1.102 +    // since we don't have a window context, hand it off to a browser
   1.103 +    var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
   1.104 +      getService(Ci.nsIWindowMediator);
   1.105 +
   1.106 +    // get browser dom window
   1.107 +    var browserDOMWin = windowMediator.getMostRecentWindow("navigator:browser")
   1.108 +                        .QueryInterface(Ci.nsIDOMChromeWindow)
   1.109 +                        .browserDOMWindow;
   1.110 +
   1.111 +    // if we got an exception, there are several possible reasons why:
   1.112 +    // a) this gecko embedding doesn't provide an nsIBrowserDOMWindow
   1.113 +    //    implementation (i.e. doesn't support browser-style functionality),
   1.114 +    //    so we need to kick the URL out to the OS default browser.  This is
   1.115 +    //    the subject of bug 394479.
   1.116 +    // b) this embedding does provide an nsIBrowserDOMWindow impl, but
   1.117 +    //    there doesn't happen to be a browser window open at the moment; one
   1.118 +    //    should be opened.  It's not clear whether this situation will really
   1.119 +    //    ever occur in real life.  If it does, the only API that I can find
   1.120 +    //    that seems reasonably likely to work for most embedders is the
   1.121 +    //    command line handler.  
   1.122 +    // c) something else went wrong 
   1.123 +    //
   1.124 +    // it's not clear how one would differentiate between the three cases
   1.125 +    // above, so for now we don't catch the exception
   1.126 +
   1.127 +    // openURI
   1.128 +    browserDOMWin.openURI(uriToSend,
   1.129 +                          null, // no window.opener 
   1.130 +                          Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW,
   1.131 +                          Ci.nsIBrowserDOMWindow.OPEN_NEW);
   1.132 +      
   1.133 +    return;
   1.134 +  },
   1.135 +
   1.136 +  //////////////////////////////////////////////////////////////////////////////
   1.137 +  //// nsIWebHandlerApp
   1.138 +
   1.139 +  get uriTemplate() {
   1.140 +    return this._uriTemplate;
   1.141 +  },
   1.142 +
   1.143 +  set uriTemplate(aURITemplate) {
   1.144 +    this._uriTemplate = aURITemplate;
   1.145 +  },
   1.146 +
   1.147 +  //////////////////////////////////////////////////////////////////////////////
   1.148 +  //// nsISupports
   1.149 +
   1.150 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebHandlerApp, Ci.nsIHandlerApp])
   1.151 +};
   1.152 +
   1.153 +////////////////////////////////////////////////////////////////////////////////
   1.154 +//// Module
   1.155 +
   1.156 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsWebHandlerApp]);
   1.157 +

mercurial