toolkit/mozapps/extensions/amContentHandler.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/amContentHandler.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     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 +"use strict";
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cr = Components.results;
    1.13 +
    1.14 +const XPI_CONTENT_TYPE = "application/x-xpinstall";
    1.15 +
    1.16 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +
    1.18 +function amContentHandler() {
    1.19 +}
    1.20 +
    1.21 +amContentHandler.prototype = {
    1.22 +  /**
    1.23 +   * Handles a new request for an application/x-xpinstall file.
    1.24 +   *
    1.25 +   * @param  aMimetype
    1.26 +   *         The mimetype of the file
    1.27 +   * @param  aContext
    1.28 +   *         The context passed to nsIChannel.asyncOpen
    1.29 +   * @param  aRequest
    1.30 +   *         The nsIRequest dealing with the content
    1.31 +   */
    1.32 +  handleContent: function XCH_handleContent(aMimetype, aContext, aRequest) {
    1.33 +    if (aMimetype != XPI_CONTENT_TYPE)
    1.34 +      throw Cr.NS_ERROR_WONT_HANDLE_CONTENT;
    1.35 +
    1.36 +    if (!(aRequest instanceof Ci.nsIChannel))
    1.37 +      throw Cr.NS_ERROR_WONT_HANDLE_CONTENT;
    1.38 +
    1.39 +    let uri = aRequest.URI;
    1.40 +
    1.41 +    let window = null;
    1.42 +    let callbacks = aRequest.notificationCallbacks ?
    1.43 +                    aRequest.notificationCallbacks :
    1.44 +                    aRequest.loadGroup.notificationCallbacks;
    1.45 +    if (callbacks)
    1.46 +      window = callbacks.getInterface(Ci.nsIDOMWindow);
    1.47 +
    1.48 +    aRequest.cancel(Cr.NS_BINDING_ABORTED);
    1.49 +
    1.50 +    let appinfo = Cc["@mozilla.org/xre/app-info;1"].
    1.51 +                  getService(Ci.nsIXULRuntime);
    1.52 +    if (appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT) {
    1.53 +      try {
    1.54 +        if (!window.InstallTrigger) 
    1.55 +          window = window.wrappedJSObject;
    1.56 +  
    1.57 +        if (window.InstallTrigger)
    1.58 +          window.InstallTrigger.startSoftwareUpdate(uri.spec);
    1.59 +        else
    1.60 +          this.log("Window does not have an InstallTrigger");
    1.61 +      } catch(ex) {
    1.62 +        this.log(ex);
    1.63 +      }
    1.64 +    }
    1.65 +    else {
    1.66 +      let referer = null;
    1.67 +      if (aRequest instanceof Ci.nsIPropertyBag2) {
    1.68 +        referer = aRequest.getPropertyAsInterface("docshell.internalReferrer",
    1.69 +                                                Ci.nsIURI);
    1.70 +      }
    1.71 +
    1.72 +      let manager = Cc["@mozilla.org/addons/integration;1"].
    1.73 +                    getService(Ci.amIWebInstaller);
    1.74 +      manager.installAddonsFromWebpage(aMimetype, window, referer, [uri.spec],
    1.75 +                                       [null], [null], [null], null, 1);
    1.76 +    }
    1.77 +  },
    1.78 +
    1.79 +  classID: Components.ID("{7beb3ba8-6ec3-41b4-b67c-da89b8518922}"),
    1.80 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler]),
    1.81 +
    1.82 +  log : function XCH_log(aMsg) {
    1.83 +    let msg = "amContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg);
    1.84 +    Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
    1.85 +      logStringMessage(msg);
    1.86 +    dump(msg + "\n");
    1.87 +  }
    1.88 +};
    1.89 +
    1.90 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([amContentHandler]);

mercurial