michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: michael@0: const XPI_CONTENT_TYPE = "application/x-xpinstall"; michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: function amContentHandler() { michael@0: } michael@0: michael@0: amContentHandler.prototype = { michael@0: /** michael@0: * Handles a new request for an application/x-xpinstall file. michael@0: * michael@0: * @param aMimetype michael@0: * The mimetype of the file michael@0: * @param aContext michael@0: * The context passed to nsIChannel.asyncOpen michael@0: * @param aRequest michael@0: * The nsIRequest dealing with the content michael@0: */ michael@0: handleContent: function XCH_handleContent(aMimetype, aContext, aRequest) { michael@0: if (aMimetype != XPI_CONTENT_TYPE) michael@0: throw Cr.NS_ERROR_WONT_HANDLE_CONTENT; michael@0: michael@0: if (!(aRequest instanceof Ci.nsIChannel)) michael@0: throw Cr.NS_ERROR_WONT_HANDLE_CONTENT; michael@0: michael@0: let uri = aRequest.URI; michael@0: michael@0: let window = null; michael@0: let callbacks = aRequest.notificationCallbacks ? michael@0: aRequest.notificationCallbacks : michael@0: aRequest.loadGroup.notificationCallbacks; michael@0: if (callbacks) michael@0: window = callbacks.getInterface(Ci.nsIDOMWindow); michael@0: michael@0: aRequest.cancel(Cr.NS_BINDING_ABORTED); michael@0: michael@0: let appinfo = Cc["@mozilla.org/xre/app-info;1"]. michael@0: getService(Ci.nsIXULRuntime); michael@0: if (appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT) { michael@0: try { michael@0: if (!window.InstallTrigger) michael@0: window = window.wrappedJSObject; michael@0: michael@0: if (window.InstallTrigger) michael@0: window.InstallTrigger.startSoftwareUpdate(uri.spec); michael@0: else michael@0: this.log("Window does not have an InstallTrigger"); michael@0: } catch(ex) { michael@0: this.log(ex); michael@0: } michael@0: } michael@0: else { michael@0: let referer = null; michael@0: if (aRequest instanceof Ci.nsIPropertyBag2) { michael@0: referer = aRequest.getPropertyAsInterface("docshell.internalReferrer", michael@0: Ci.nsIURI); michael@0: } michael@0: michael@0: let manager = Cc["@mozilla.org/addons/integration;1"]. michael@0: getService(Ci.amIWebInstaller); michael@0: manager.installAddonsFromWebpage(aMimetype, window, referer, [uri.spec], michael@0: [null], [null], [null], null, 1); michael@0: } michael@0: }, michael@0: michael@0: classID: Components.ID("{7beb3ba8-6ec3-41b4-b67c-da89b8518922}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler]), michael@0: michael@0: log : function XCH_log(aMsg) { michael@0: let msg = "amContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg); michael@0: Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService). michael@0: logStringMessage(msg); michael@0: dump(msg + "\n"); michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([amContentHandler]);