2012-03-06 11:50:58 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-28 12:13:26 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-03-06 11:50:58 -08:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
|
|
|
|
return Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsIFrameMessageManager);
|
|
|
|
});
|
|
|
|
|
2012-08-18 21:08:40 -07:00
|
|
|
// Makes sure that we expose correctly chrome JS objects to content.
|
|
|
|
function wrapObjectIn(aObject, aCtxt) {
|
|
|
|
let res = Cu.createObjectIn(aCtxt);
|
|
|
|
let propList = { };
|
|
|
|
for (let prop in aObject) {
|
|
|
|
propList[prop] = {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
|
|
|
value: (typeof(aObject[prop]) == "object") ? wrapObjectIn(aObject[prop], aCtxt)
|
|
|
|
: aObject[prop]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.defineProperties(res, propList);
|
|
|
|
Cu.makeObjectPropsNormal(res);
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
function convertAppsArray(aApps, aWindow) {
|
2012-06-20 21:12:21 -07:00
|
|
|
let apps = Cu.createArrayIn(aWindow);
|
2012-03-06 11:50:58 -08:00
|
|
|
for (let i = 0; i < aApps.length; i++) {
|
|
|
|
let app = aApps[i];
|
2012-06-11 11:41:46 -07:00
|
|
|
apps.push(createApplicationObject(aWindow, app.origin, app.manifest, app.manifestURL,
|
|
|
|
app.receipts, app.installOrigin, app.installTime));
|
2012-03-06 11:50:58 -08:00
|
|
|
}
|
2012-06-20 21:12:21 -07:00
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
return apps;
|
|
|
|
}
|
2011-11-28 12:13:26 -08:00
|
|
|
|
|
|
|
function WebappsRegistry() {
|
|
|
|
}
|
|
|
|
|
|
|
|
WebappsRegistry.prototype = {
|
2012-03-06 11:50:58 -08:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2012-06-11 11:41:46 -07:00
|
|
|
__exposedProps__: {
|
|
|
|
install: 'r',
|
2012-07-11 08:38:33 -07:00
|
|
|
installPackage: 'r',
|
2012-06-11 11:41:46 -07:00
|
|
|
getSelf: 'r',
|
|
|
|
getInstalled: 'r',
|
2012-06-29 13:46:21 -07:00
|
|
|
getNotInstalled: 'r',
|
2012-06-11 11:41:46 -07:00
|
|
|
mgmt: 'r'
|
|
|
|
},
|
2011-11-28 12:13:26 -08:00
|
|
|
|
|
|
|
/** from https://developer.mozilla.org/en/OpenWebApps/The_Manifest
|
|
|
|
* only the name property is mandatory
|
|
|
|
*/
|
|
|
|
checkManifest: function(aManifest, aInstallOrigin) {
|
|
|
|
if (aManifest.name == undefined)
|
|
|
|
return false;
|
2012-03-06 11:50:58 -08:00
|
|
|
|
2011-11-28 12:13:26 -08:00
|
|
|
if (aManifest.installs_allowed_from) {
|
2012-05-04 11:02:05 -07:00
|
|
|
return aManifest.installs_allowed_from.some(function(aOrigin) {
|
|
|
|
return aOrigin == "*" || aOrigin == aInstallOrigin;
|
2011-11-28 12:13:26 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let msg = aMessage.json;
|
2012-03-06 11:50:58 -08:00
|
|
|
if (msg.oid != this._id)
|
2011-11-28 12:13:26 -08:00
|
|
|
return
|
2012-03-06 11:50:58 -08:00
|
|
|
let req = this.getRequest(msg.requestID);
|
|
|
|
if (!req)
|
|
|
|
return;
|
2011-11-28 12:13:26 -08:00
|
|
|
let app = msg.app;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:Install:Return:OK":
|
2012-06-11 11:41:46 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app.origin, app.manifest, app.manifestURL, app.receipts,
|
|
|
|
app.installOrigin, app.installTime));
|
2011-11-28 12:13:26 -08:00
|
|
|
break;
|
|
|
|
case "Webapps:Install:Return:KO":
|
2012-08-08 14:07:39 -07:00
|
|
|
Services.DOMRequest.fireError(req, msg.error || "DENIED");
|
2011-11-28 12:13:26 -08:00
|
|
|
break;
|
2012-03-06 11:50:58 -08:00
|
|
|
case "Webapps:GetSelf:Return:OK":
|
|
|
|
if (msg.apps.length) {
|
|
|
|
app = msg.apps[0];
|
2012-06-11 11:41:46 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app.origin, app.manifest, app.manifestURL, app.receipts,
|
|
|
|
app.installOrigin, app.installTime));
|
2012-03-06 11:50:58 -08:00
|
|
|
} else {
|
2012-04-10 22:55:54 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, null);
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
|
|
|
break;
|
2012-03-06 11:50:58 -08:00
|
|
|
case "Webapps:GetInstalled:Return:OK":
|
2012-04-10 22:55:54 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, convertAppsArray(msg.apps, this._window));
|
2012-03-06 11:50:58 -08:00
|
|
|
break;
|
2012-06-29 13:46:21 -07:00
|
|
|
case "Webapps:GetNotInstalled:Return:OK":
|
|
|
|
Services.DOMRequest.fireSuccess(req, convertAppsArray(msg.apps, this._window));
|
|
|
|
break;
|
2012-03-06 11:50:58 -08:00
|
|
|
case "Webapps:GetSelf:Return:KO":
|
2012-04-10 22:55:54 -07:00
|
|
|
Services.DOMRequest.fireError(req, "ERROR");
|
2011-11-28 12:13:26 -08:00
|
|
|
break;
|
|
|
|
}
|
2012-03-06 11:50:58 -08:00
|
|
|
this.removeRequest(msg.requestID);
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getOrigin: function(aURL) {
|
|
|
|
let uri = Services.io.newURI(aURL, null, null);
|
|
|
|
return uri.prePath;
|
|
|
|
},
|
|
|
|
|
|
|
|
// mozIDOMApplicationRegistry implementation
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
install: function(aURL, aParams) {
|
2012-07-05 11:12:55 -07:00
|
|
|
let installURL = this._window.location.href;
|
|
|
|
let installOrigin = this._getOrigin(installURL);
|
2012-03-06 11:50:58 -08:00
|
|
|
let request = this.createRequest();
|
|
|
|
let requestID = this.getRequestId(request);
|
2011-11-28 12:13:26 -08:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", aURL, true);
|
2012-08-03 09:24:35 -07:00
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.VALIDATE_ALWAYS;
|
2011-11-28 12:13:26 -08:00
|
|
|
|
|
|
|
xhr.addEventListener("load", (function() {
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
try {
|
|
|
|
let manifest = JSON.parse(xhr.responseText, installOrigin);
|
|
|
|
if (!this.checkManifest(manifest, installOrigin)) {
|
2012-08-08 14:07:39 -07:00
|
|
|
Services.DOMRequest.fireError(request, "INVALID_MANIFEST");
|
2011-11-28 12:13:26 -08:00
|
|
|
} else {
|
2012-03-06 11:50:58 -08:00
|
|
|
let receipts = (aParams && aParams.receipts && Array.isArray(aParams.receipts)) ? aParams.receipts : [];
|
2012-08-06 18:16:00 -07:00
|
|
|
let categories = (aParams && aParams.categories && Array.isArray(aParams.categories)) ? aParams.categories : [];
|
2012-03-06 11:50:58 -08:00
|
|
|
cpmm.sendAsyncMessage("Webapps:Install", { app: { installOrigin: installOrigin,
|
|
|
|
origin: this._getOrigin(aURL),
|
|
|
|
manifestURL: aURL,
|
|
|
|
manifest: manifest,
|
2012-08-06 18:16:00 -07:00
|
|
|
receipts: receipts,
|
|
|
|
categories: categories },
|
2012-07-05 11:12:55 -07:00
|
|
|
from: installURL,
|
2012-03-06 11:50:58 -08:00
|
|
|
oid: this._id,
|
|
|
|
requestID: requestID });
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
|
|
|
} catch(e) {
|
2012-08-08 14:07:39 -07:00
|
|
|
Services.DOMRequest.fireError(request, "MANIFEST_PARSE_ERROR");
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-08-08 14:07:39 -07:00
|
|
|
Services.DOMRequest.fireError(request, "MANIFEST_URL_ERROR");
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.addEventListener("error", (function() {
|
2012-08-08 14:07:39 -07:00
|
|
|
Services.DOMRequest.fireError(request, "NETWORK_ERROR");
|
2011-11-28 12:13:26 -08:00
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.send(null);
|
2012-03-06 11:50:58 -08:00
|
|
|
return request;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
getSelf: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Webapps:GetSelf", { origin: this._getOrigin(this._window.location.href),
|
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
getInstalled: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Webapps:GetInstalled", { origin: this._getOrigin(this._window.location.href),
|
2011-11-28 12:13:26 -08:00
|
|
|
oid: this._id,
|
2012-03-06 11:50:58 -08:00
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-06-29 13:46:21 -07:00
|
|
|
getNotInstalled: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Webapps:GetNotInstalled", { origin: this._getOrigin(this._window.location.href),
|
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-07-11 08:38:33 -07:00
|
|
|
installPackage: function(aPackageURL, aParams) {
|
|
|
|
let request = this.createRequest();
|
|
|
|
let requestID = this.getRequestId(request);
|
|
|
|
|
|
|
|
let receipts = (aParams && aParams.receipts &&
|
|
|
|
Array.isArray(aParams.receipts)) ? aParams.receipts : [];
|
2012-08-06 18:16:00 -07:00
|
|
|
let categories = (aParams && aParams.categories &&
|
|
|
|
Array.isArray(aParams.categories)) ? aParams.categories : [];
|
2012-07-11 08:38:33 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:InstallPackage", { url: aPackageURL,
|
|
|
|
receipts: receipts,
|
2012-08-06 18:16:00 -07:00
|
|
|
categories: categories,
|
2012-07-11 08:38:33 -07:00
|
|
|
requestID: requestID,
|
|
|
|
oid: this._id,
|
|
|
|
from: this._window.location.href,
|
|
|
|
installOrigin: this._getOrigin(this._window.location.href) });
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
get mgmt() {
|
|
|
|
if (!this._mgmt)
|
|
|
|
this._mgmt = new WebappsApplicationMgmt(this._window);
|
|
|
|
return this._mgmt;
|
2011-12-15 09:20:57 -08:00
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
uninit: function() {
|
|
|
|
this._mgmt = null;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
2011-12-07 18:15:42 -08:00
|
|
|
|
2011-11-28 12:13:26 -08:00
|
|
|
// nsIDOMGlobalPropertyInitializer implementation
|
|
|
|
init: function(aWindow) {
|
2012-03-06 11:50:58 -08:00
|
|
|
this.initHelper(aWindow, ["Webapps:Install:Return:OK", "Webapps:Install:Return:KO",
|
2012-06-29 13:46:21 -07:00
|
|
|
"Webapps:GetInstalled:Return:OK", "Webapps:GetNotInstalled:Return:OK",
|
2012-03-06 11:50:58 -08:00
|
|
|
"Webapps:GetSelf:Return:OK", "Webapps:GetSelf:Return:KO"]);
|
2011-12-07 18:15:42 -08:00
|
|
|
|
|
|
|
let util = this._window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
2012-03-06 11:50:58 -08:00
|
|
|
this._id = util.outerWindowID;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIDOMApplicationRegistry, Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
|
|
|
|
contractID: "@mozilla.org/webapps;1",
|
|
|
|
interfaces: [Ci.mozIDOMApplicationRegistry],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "Webapps Registry"})
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
/**
|
|
|
|
* mozIDOMApplication object
|
|
|
|
*/
|
2012-06-11 11:41:46 -07:00
|
|
|
|
|
|
|
function createApplicationObject(aWindow, aOrigin, aManifest, aManifestURL, aReceipts, aInstallOrigin, aInstallTime) {
|
|
|
|
let app = Cc["@mozilla.org/webapps/application;1"].createInstance(Ci.mozIDOMApplication);
|
|
|
|
app.wrappedJSObject.init(aWindow, aOrigin, aManifest, aManifestURL, aReceipts, aInstallOrigin, aInstallTime);
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
function WebappsApplication() {
|
|
|
|
this.wrappedJSObject = this;
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
WebappsApplication.prototype = {
|
2012-03-06 11:50:58 -08:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2012-05-24 04:04:57 -07:00
|
|
|
__exposedProps__: {
|
|
|
|
origin: 'r',
|
|
|
|
manifest: 'r',
|
|
|
|
manifestURL: 'r',
|
|
|
|
installOrigin: 'r',
|
|
|
|
installTime: 'r',
|
2012-06-11 11:41:46 -07:00
|
|
|
status: 'r',
|
|
|
|
progress: 'r',
|
|
|
|
onprogress: 'rw',
|
2012-05-24 04:04:57 -07:00
|
|
|
launch: 'r',
|
2012-06-08 11:06:16 -07:00
|
|
|
receipts: 'r',
|
2012-05-24 04:04:57 -07:00
|
|
|
uninstall: 'r'
|
|
|
|
},
|
2011-11-28 12:13:26 -08:00
|
|
|
|
2012-06-11 11:41:46 -07:00
|
|
|
init: function(aWindow, aOrigin, aManifest, aManifestURL, aReceipts, aInstallOrigin, aInstallTime) {
|
|
|
|
this.origin = aOrigin;
|
2012-08-18 21:08:40 -07:00
|
|
|
this.manifest = wrapObjectIn(aManifest, aWindow);
|
2012-06-11 11:41:46 -07:00
|
|
|
this.manifestURL = aManifestURL;
|
|
|
|
this.receipts = aReceipts;
|
|
|
|
this.installOrigin = aInstallOrigin;
|
|
|
|
this.installTime = aInstallTime;
|
|
|
|
this.status = "installed";
|
|
|
|
this.progress = NaN;
|
|
|
|
this._onprogress = null;
|
|
|
|
this.initHelper(aWindow, ["Webapps:Uninstall:Return:OK", "Webapps:Uninstall:Return:KO", "Webapps:OfflineCache"]);
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-06-11 11:41:46 -07:00
|
|
|
set onprogress(aCallback) {
|
|
|
|
this._onprogress = aCallback;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-06-11 11:41:46 -07:00
|
|
|
get onprogress() {
|
|
|
|
return this._onprogress;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
launch: function(aStartPoint) {
|
|
|
|
let request = this.createRequest();
|
2012-06-11 11:41:46 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:Launch", { origin: this.origin,
|
2012-05-04 11:02:05 -07:00
|
|
|
startPoint: aStartPoint || "",
|
2012-03-06 11:50:58 -08:00
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
uninstall: function() {
|
|
|
|
let request = this.createRequest();
|
2012-06-11 11:41:46 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:Uninstall", { origin: this.origin,
|
2012-03-06 11:50:58 -08:00
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-06-11 11:41:46 -07:00
|
|
|
uninit: function() {
|
|
|
|
this._onprogress = null;
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
var msg = aMessage.json;
|
2012-06-11 11:41:46 -07:00
|
|
|
let req = this.takeRequest(msg.requestID);
|
|
|
|
if ((msg.oid != this._id || !req) && aMessage.name !== "Webapps:OfflineCache")
|
2012-03-06 11:50:58 -08:00
|
|
|
return;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:Uninstall:Return:OK":
|
2012-04-10 22:55:54 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, msg.origin);
|
2012-03-06 11:50:58 -08:00
|
|
|
break;
|
|
|
|
case "Webapps:Uninstall:Return:KO":
|
2012-06-18 12:57:16 -07:00
|
|
|
Services.DOMRequest.fireError(req, "NOT_INSTALLED");
|
2012-03-06 11:50:58 -08:00
|
|
|
break;
|
2012-06-11 11:41:46 -07:00
|
|
|
case "Webapps:OfflineCache":
|
|
|
|
if (msg.manifest != this.manifestURL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.status = msg.status;
|
|
|
|
if (this._onprogress) {
|
|
|
|
let event = new this._window.MozApplicationEvent("applicationinstall", { application: this });
|
|
|
|
this._onprogress.handleEvent(event);
|
|
|
|
}
|
|
|
|
break;
|
2012-03-06 11:50:58 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-11-28 12:13:26 -08:00
|
|
|
classID: Components.ID("{723ed303-7757-4fb0-b261-4f78b1f6bd22}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIDOMApplication]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{723ed303-7757-4fb0-b261-4f78b1f6bd22}"),
|
|
|
|
contractID: "@mozilla.org/webapps/application;1",
|
|
|
|
interfaces: [Ci.mozIDOMApplication],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "Webapps Application"})
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
/**
|
|
|
|
* mozIDOMApplicationMgmt object
|
|
|
|
*/
|
|
|
|
function WebappsApplicationMgmt(aWindow) {
|
|
|
|
let principal = aWindow.document.nodePrincipal;
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
|
|
|
|
|
2012-07-16 08:44:52 -07:00
|
|
|
let perm = principal == secMan.getSystemPrincipal()
|
|
|
|
? Ci.nsIPermissionManager.ALLOW_ACTION
|
|
|
|
: Services.perms.testExactPermissionFromPrincipal(principal, "webapps-manage");
|
2012-03-06 11:50:58 -08:00
|
|
|
|
|
|
|
//only pages with perm set can use some functions
|
|
|
|
this.hasPrivileges = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
|
|
|
|
this.initHelper(aWindow, ["Webapps:GetAll:Return:OK", "Webapps:GetAll:Return:KO",
|
|
|
|
"Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK"]);
|
|
|
|
|
|
|
|
this._oninstall = null;
|
|
|
|
this._onuninstall = null;
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
WebappsApplicationMgmt.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2012-06-11 11:41:46 -07:00
|
|
|
__exposedProps__: {
|
|
|
|
getAll: 'r',
|
|
|
|
oninstall: 'rw',
|
|
|
|
onuninstall: 'rw'
|
|
|
|
},
|
2012-03-06 11:50:58 -08:00
|
|
|
|
|
|
|
uninit: function() {
|
|
|
|
this._oninstall = null;
|
|
|
|
this._onuninstall = null;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
getAll: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Webapps:GetAll", { oid: this._id,
|
|
|
|
requestID: this.getRequestId(request),
|
|
|
|
hasPrivileges: this.hasPrivileges });
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
get oninstall() {
|
|
|
|
return this._oninstall;
|
|
|
|
},
|
|
|
|
|
|
|
|
get onuninstall() {
|
2012-05-11 15:29:15 -07:00
|
|
|
return this._onuninstall;
|
2012-03-06 11:50:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
set oninstall(aCallback) {
|
|
|
|
if (this.hasPrivileges)
|
|
|
|
this._oninstall = aCallback;
|
|
|
|
else
|
2012-03-14 13:39:15 -07:00
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
2012-03-06 11:50:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
set onuninstall(aCallback) {
|
|
|
|
if (this.hasPrivileges)
|
|
|
|
this._onuninstall = aCallback;
|
|
|
|
else
|
2012-03-14 13:39:15 -07:00
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
2012-03-06 11:50:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
var msg = aMessage.json;
|
|
|
|
let req = this.getRequest(msg.requestID);
|
|
|
|
// We want Webapps:Install:Return:OK and Webapps:Uninstall:Return:OK to be boradcasted
|
|
|
|
// to all instances of mozApps.mgmt
|
|
|
|
if (!((msg.oid == this._id && req)
|
|
|
|
|| aMessage.name == "Webapps:Install:Return:OK" || aMessage.name == "Webapps:Uninstall:Return:OK"))
|
|
|
|
return;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:GetAll:Return:OK":
|
2012-04-10 22:55:54 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, convertAppsArray(msg.apps, this._window));
|
2012-03-06 11:50:58 -08:00
|
|
|
break;
|
|
|
|
case "Webapps:GetAll:Return:KO":
|
2012-04-10 22:55:54 -07:00
|
|
|
Services.DOMRequest.fireError(req, "DENIED");
|
2012-03-06 11:50:58 -08:00
|
|
|
break;
|
|
|
|
case "Webapps:Install:Return:OK":
|
|
|
|
if (this._oninstall) {
|
|
|
|
let app = msg.app;
|
2012-06-14 16:03:55 -07:00
|
|
|
let event = new this._window.MozApplicationEvent("applicationinstall",
|
2012-06-11 11:41:46 -07:00
|
|
|
{ application : createApplicationObject(this._window, app.origin, app.manifest, app.manifestURL, app.receipts,
|
2012-06-14 16:03:55 -07:00
|
|
|
app.installOrigin, app.installTime) });
|
2012-03-06 11:50:58 -08:00
|
|
|
this._oninstall.handleEvent(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Webapps:Uninstall:Return:OK":
|
|
|
|
if (this._onuninstall) {
|
2012-06-14 16:03:55 -07:00
|
|
|
let event = new this._window.MozApplicationEvent("applicationuninstall",
|
2012-06-11 11:41:46 -07:00
|
|
|
{ application : createApplicationObject(this._window, msg.origin, null, null, null, null, 0) });
|
2012-03-06 11:50:58 -08:00
|
|
|
this._onuninstall.handleEvent(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.removeRequest(msg.requestID);
|
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{8c1bca96-266f-493a-8d57-ec7a95098c15}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIDOMApplicationMgmt]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{8c1bca96-266f-493a-8d57-ec7a95098c15}"),
|
|
|
|
contractID: "@mozilla.org/webapps/application-mgmt;1",
|
|
|
|
interfaces: [Ci.mozIDOMApplicationMgmt],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "Webapps Application Mgmt"})
|
|
|
|
}
|
|
|
|
|
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsRegistry, WebappsApplication]);
|