toolkit/mozapps/extensions/amContentHandler.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:81dce02bd26c
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9 const Cr = Components.results;
10
11 const XPI_CONTENT_TYPE = "application/x-xpinstall";
12
13 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
14
15 function amContentHandler() {
16 }
17
18 amContentHandler.prototype = {
19 /**
20 * Handles a new request for an application/x-xpinstall file.
21 *
22 * @param aMimetype
23 * The mimetype of the file
24 * @param aContext
25 * The context passed to nsIChannel.asyncOpen
26 * @param aRequest
27 * The nsIRequest dealing with the content
28 */
29 handleContent: function XCH_handleContent(aMimetype, aContext, aRequest) {
30 if (aMimetype != XPI_CONTENT_TYPE)
31 throw Cr.NS_ERROR_WONT_HANDLE_CONTENT;
32
33 if (!(aRequest instanceof Ci.nsIChannel))
34 throw Cr.NS_ERROR_WONT_HANDLE_CONTENT;
35
36 let uri = aRequest.URI;
37
38 let window = null;
39 let callbacks = aRequest.notificationCallbacks ?
40 aRequest.notificationCallbacks :
41 aRequest.loadGroup.notificationCallbacks;
42 if (callbacks)
43 window = callbacks.getInterface(Ci.nsIDOMWindow);
44
45 aRequest.cancel(Cr.NS_BINDING_ABORTED);
46
47 let appinfo = Cc["@mozilla.org/xre/app-info;1"].
48 getService(Ci.nsIXULRuntime);
49 if (appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT) {
50 try {
51 if (!window.InstallTrigger)
52 window = window.wrappedJSObject;
53
54 if (window.InstallTrigger)
55 window.InstallTrigger.startSoftwareUpdate(uri.spec);
56 else
57 this.log("Window does not have an InstallTrigger");
58 } catch(ex) {
59 this.log(ex);
60 }
61 }
62 else {
63 let referer = null;
64 if (aRequest instanceof Ci.nsIPropertyBag2) {
65 referer = aRequest.getPropertyAsInterface("docshell.internalReferrer",
66 Ci.nsIURI);
67 }
68
69 let manager = Cc["@mozilla.org/addons/integration;1"].
70 getService(Ci.amIWebInstaller);
71 manager.installAddonsFromWebpage(aMimetype, window, referer, [uri.spec],
72 [null], [null], [null], null, 1);
73 }
74 },
75
76 classID: Components.ID("{7beb3ba8-6ec3-41b4-b67c-da89b8518922}"),
77 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler]),
78
79 log : function XCH_log(aMsg) {
80 let msg = "amContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg);
81 Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
82 logStringMessage(msg);
83 dump(msg + "\n");
84 }
85 };
86
87 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([amContentHandler]);

mercurial