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");
|
2012-08-19 12:00:19 -07:00
|
|
|
Cu.import("resource://gre/modules/ObjectWrapper.jsm");
|
2012-09-22 17:48:26 -07:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2012-09-26 15:03:25 -07:00
|
|
|
Cu.import("resource://gre/modules/BrowserElementPromptService.jsm");
|
2012-03-06 11:50:58 -08:00
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
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-08-29 14:20:03 -07:00
|
|
|
apps.push(createApplicationObject(aWindow, app));
|
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,
|
2011-11-28 12:13:26 -08:00
|
|
|
|
|
|
|
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-08-29 14:20:03 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app));
|
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-08-29 14:20:03 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app));
|
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-10-02 13:38:51 -07:00
|
|
|
case "Webapps:CheckInstalled:Return:OK":
|
|
|
|
Services.DOMRequest.fireSuccess(req, msg.app);
|
2012-09-25 08:04:24 -07: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;
|
2011-11-28 12:13:26 -08:00
|
|
|
}
|
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);
|
2012-08-27 19:43:57 -07:00
|
|
|
return uri.prePath;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-09-24 19:41:21 -07:00
|
|
|
_validateScheme: function(aURL) {
|
|
|
|
let scheme = Services.io.newURI(aURL, null, null).scheme;
|
|
|
|
if (scheme != "http" && scheme != "https") {
|
|
|
|
throw new Components.Exception(
|
|
|
|
"INVALID_URL_SCHEME: '" + scheme + "'; must be 'http' or 'https'",
|
|
|
|
Cr.NS_ERROR_FAILURE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-11-28 12:13:26 -08:00
|
|
|
// mozIDOMApplicationRegistry implementation
|
2012-08-27 19:43:57 -07:00
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
install: function(aURL, aParams) {
|
2012-09-24 19:41:21 -07:00
|
|
|
this._validateScheme(aURL);
|
|
|
|
|
2012-07-05 11:12:55 -07:00
|
|
|
let installURL = this._window.location.href;
|
2012-03-06 11:50:58 -08:00
|
|
|
let request = this.createRequest();
|
|
|
|
let requestID = this.getRequestId(request);
|
2012-12-10 15:48:59 -08:00
|
|
|
let receipts = (aParams && aParams.receipts &&
|
|
|
|
Array.isArray(aParams.receipts)) ? aParams.receipts
|
|
|
|
: [];
|
|
|
|
let categories = (aParams && aParams.categories &&
|
|
|
|
Array.isArray(aParams.categories)) ? aParams.categories
|
|
|
|
: [];
|
|
|
|
|
2012-12-10 15:49:02 -08:00
|
|
|
let principal = this._window.document.nodePrincipal;
|
2012-12-10 15:48:59 -08:00
|
|
|
cpmm.sendAsyncMessage("Webapps:Install",
|
|
|
|
{ app: {
|
|
|
|
installOrigin: this._getOrigin(installURL),
|
|
|
|
origin: this._getOrigin(aURL),
|
|
|
|
manifestURL: aURL,
|
|
|
|
receipts: receipts,
|
|
|
|
categories: categories
|
|
|
|
},
|
|
|
|
from: installURL,
|
|
|
|
oid: this._id,
|
2012-12-10 15:49:02 -08:00
|
|
|
requestID: requestID,
|
|
|
|
appId: principal.appId,
|
|
|
|
isBrowser: principal.isInBrowserElement
|
2012-12-10 15:48:59 -08:00
|
|
|
});
|
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),
|
2012-09-25 08:04:24 -07:00
|
|
|
appId: this._window.document.nodePrincipal.appId,
|
2012-03-06 11:50:58 -08:00
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-10-02 13:38:51 -07:00
|
|
|
checkInstalled: function(aManifestURL) {
|
2012-09-25 08:04:24 -07:00
|
|
|
let manifestURL = Services.io.newURI(aManifestURL, null, this._window.document.baseURIObject);
|
|
|
|
this._window.document.nodePrincipal.checkMayLoad(manifestURL, true, false);
|
|
|
|
|
|
|
|
let request = this.createRequest();
|
2012-10-02 13:38:51 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:CheckInstalled", { origin: this._getOrigin(this._window.location.href),
|
|
|
|
manifestURL: manifestURL.spec,
|
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
2012-09-25 08:04:24 -07:00
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
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-09-11 13:53:42 -07:00
|
|
|
get mgmt() {
|
|
|
|
if (!this._mgmt)
|
|
|
|
this._mgmt = new WebappsApplicationMgmt(this._window);
|
|
|
|
return this._mgmt;
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function() {
|
|
|
|
this._mgmt = null;
|
2012-10-19 11:17:05 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
|
|
|
|
["Webapps:Install:Return:OK"]);
|
2012-09-11 13:53:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// mozIDOMApplicationRegistry2 implementation
|
|
|
|
|
2012-09-27 12:34:41 -07:00
|
|
|
installPackage: function(aURL, aParams) {
|
|
|
|
this._validateScheme(aURL);
|
2012-09-24 19:41:21 -07:00
|
|
|
|
2012-12-10 15:48:59 -08:00
|
|
|
let installURL = this._window.location.href;
|
2012-07-11 08:38:33 -07:00
|
|
|
let request = this.createRequest();
|
|
|
|
let requestID = this.getRequestId(request);
|
|
|
|
let receipts = (aParams && aParams.receipts &&
|
2012-12-10 15:48:59 -08:00
|
|
|
Array.isArray(aParams.receipts)) ? aParams.receipts
|
|
|
|
: [];
|
2012-08-06 18:16:00 -07:00
|
|
|
let categories = (aParams && aParams.categories &&
|
2012-12-10 15:48:59 -08:00
|
|
|
Array.isArray(aParams.categories)) ? aParams.categories
|
|
|
|
: [];
|
|
|
|
|
2012-12-10 15:49:02 -08:00
|
|
|
let principal = this._window.document.nodePrincipal;
|
2012-12-10 15:48:59 -08:00
|
|
|
cpmm.sendAsyncMessage("Webapps:InstallPackage",
|
|
|
|
{ app: {
|
|
|
|
installOrigin: this._getOrigin(installURL),
|
|
|
|
origin: this._getOrigin(aURL),
|
|
|
|
manifestURL: aURL,
|
|
|
|
receipts: receipts,
|
|
|
|
categories: categories
|
|
|
|
},
|
|
|
|
from: installURL,
|
|
|
|
oid: this._id,
|
|
|
|
requestID: requestID,
|
2012-12-10 15:49:02 -08:00
|
|
|
isPackage: true,
|
|
|
|
appId: principal.appId,
|
|
|
|
isBrowser: principal.isInBrowserElement
|
2012-12-10 15:48:59 -08:00
|
|
|
});
|
2012-07-11 08:38:33 -07:00
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
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-08-20 10:19:56 -07:00
|
|
|
"Webapps:GetInstalled:Return:OK",
|
2012-09-25 08:04:24 -07:00
|
|
|
"Webapps:GetSelf:Return:OK",
|
2012-10-02 13:38:51 -07:00
|
|
|
"Webapps:CheckInstalled:Return:OK" ]);
|
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;
|
2012-09-18 10:34:55 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
|
|
|
|
["Webapps:Install:Return:OK"]);
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
2012-08-27 19:43:57 -07:00
|
|
|
|
2011-11-28 12:13:26 -08:00
|
|
|
classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
|
|
|
|
|
2012-09-11 13:53:42 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIDOMApplicationRegistry,
|
|
|
|
#ifdef MOZ_PHOENIX
|
|
|
|
# Firefox Desktop: installPackage not implemented
|
|
|
|
#elifdef ANDROID
|
|
|
|
#ifndef MOZ_WIDGET_GONK
|
|
|
|
# Firefox Android (Fennec): installPackage not implemented
|
|
|
|
#else
|
|
|
|
# B2G Gonk: installPackage implemented
|
|
|
|
Ci.mozIDOMApplicationRegistry2,
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
# B2G Desktop and others: installPackage implementation status varies
|
|
|
|
Ci.mozIDOMApplicationRegistry2,
|
|
|
|
#endif
|
|
|
|
Ci.nsIDOMGlobalPropertyInitializer]),
|
2012-08-27 19:43:57 -07:00
|
|
|
|
2011-11-28 12:13:26 -08:00
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
|
|
|
|
contractID: "@mozilla.org/webapps;1",
|
2012-09-11 13:53:42 -07:00
|
|
|
interfaces: [Ci.mozIDOMApplicationRegistry,
|
|
|
|
#ifdef MOZ_PHOENIX
|
|
|
|
# Firefox Desktop: installPackage not implemented
|
|
|
|
#elifdef ANDROID
|
|
|
|
#ifndef MOZ_WIDGET_GONK
|
|
|
|
# Firefox Android (Fennec): installPackage not implemented
|
|
|
|
#else
|
|
|
|
# B2G Gonk: installPackage implemented
|
|
|
|
Ci.mozIDOMApplicationRegistry2,
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
# B2G Desktop and others: installPackage implementation status varies
|
|
|
|
Ci.mozIDOMApplicationRegistry2,
|
|
|
|
#endif
|
|
|
|
],
|
2011-11-28 12:13:26 -08:00
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "Webapps Registry"})
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:01:20 -07:00
|
|
|
/**
|
|
|
|
* nsIDOMDOMError object
|
|
|
|
*/
|
|
|
|
function createDOMError(aError) {
|
|
|
|
let error = Cc["@mozilla.org/dom-error;1"]
|
|
|
|
.createInstance(Ci.nsIDOMDOMError);
|
|
|
|
error.wrappedJSObject.init(aError);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
function DOMError() {
|
|
|
|
this.wrappedJSObject = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMError.prototype = {
|
|
|
|
init: function domerror_init(aError) {
|
|
|
|
this.name = aError;
|
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{dcc1d5b7-43d8-4740-9244-b3d8db0f503d}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMDOMError]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{dcc1d5b7-43d8-4740-9244-b3d8db0f503d}"),
|
|
|
|
contractID: "@mozilla.org/dom-error;1",
|
|
|
|
interfaces: [Ci.nsIDOMDOMError],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "DOMError object"})
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
/**
|
|
|
|
* mozIDOMApplication object
|
|
|
|
*/
|
2012-06-11 11:41:46 -07:00
|
|
|
|
2012-08-29 14:20:03 -07:00
|
|
|
function createApplicationObject(aWindow, aApp) {
|
2012-06-11 11:41:46 -07:00
|
|
|
let app = Cc["@mozilla.org/webapps/application;1"].createInstance(Ci.mozIDOMApplication);
|
2012-08-29 14:20:03 -07:00
|
|
|
app.wrappedJSObject.init(aWindow, aApp);
|
2012-06-11 11:41:46 -07:00
|
|
|
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,
|
2011-11-28 12:13:26 -08:00
|
|
|
|
2012-08-29 14:20:03 -07:00
|
|
|
init: function(aWindow, aApp) {
|
2012-10-18 15:30:58 -07:00
|
|
|
this._window = aWindow;
|
2012-08-29 14:20:03 -07:00
|
|
|
this.origin = aApp.origin;
|
2012-10-18 15:30:58 -07:00
|
|
|
this._manifest = aApp.manifest;
|
|
|
|
this._updateManifest = aApp.updateManifest;
|
2012-08-29 14:20:03 -07:00
|
|
|
this.manifestURL = aApp.manifestURL;
|
|
|
|
this.receipts = aApp.receipts;
|
|
|
|
this.installOrigin = aApp.installOrigin;
|
|
|
|
this.installTime = aApp.installTime;
|
2012-09-26 18:01:20 -07:00
|
|
|
this.installState = aApp.installState || "installed";
|
2012-08-29 14:20:03 -07:00
|
|
|
this.removable = aApp.removable;
|
2012-09-26 18:01:20 -07:00
|
|
|
this.lastUpdateCheck = aApp.lastUpdateCheck ? aApp.lastUpdateCheck
|
|
|
|
: Date.now();
|
2012-11-16 19:37:41 -08:00
|
|
|
this.updateTime = aApp.updateTime ? aApp.updateTime
|
|
|
|
: aApp.installTime;
|
2012-06-11 11:41:46 -07:00
|
|
|
this.progress = NaN;
|
2012-09-26 18:01:20 -07:00
|
|
|
this.downloadAvailable = aApp.downloadAvailable;
|
|
|
|
this.downloading = aApp.downloading;
|
|
|
|
this.readyToApplyDownload = aApp.readyToApplyDownload;
|
|
|
|
this.downloadSize = aApp.downloadSize || 0;
|
|
|
|
|
2012-06-11 11:41:46 -07:00
|
|
|
this._onprogress = null;
|
2012-09-26 18:01:20 -07:00
|
|
|
this._ondownloadsuccess = null;
|
|
|
|
this._ondownloaderror = null;
|
|
|
|
this._ondownloadavailable = null;
|
|
|
|
this._ondownloadapplied = null;
|
|
|
|
|
|
|
|
this._downloadError = null;
|
|
|
|
|
2012-08-29 14:20:03 -07:00
|
|
|
this.initHelper(aWindow, ["Webapps:Uninstall:Return:OK",
|
|
|
|
"Webapps:Uninstall:Return:KO",
|
2012-09-26 18:01:20 -07:00
|
|
|
"Webapps:OfflineCache",
|
|
|
|
"Webapps:CheckForUpdate:Return:OK",
|
2012-09-27 12:34:41 -07:00
|
|
|
"Webapps:CheckForUpdate:Return:KO",
|
|
|
|
"Webapps:PackageEvent"]);
|
2012-12-28 09:57:35 -08:00
|
|
|
|
2012-09-18 10:34:55 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
|
2012-09-27 12:34:41 -07:00
|
|
|
["Webapps:Uninstall:Return:OK", "Webapps:OfflineCache",
|
2012-12-28 09:57:35 -08:00
|
|
|
"Webapps:PackageEvent", "Webapps:CheckForUpdate:Return:OK"]);
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-10-18 15:30:58 -07:00
|
|
|
get manifest() {
|
|
|
|
return this.manifest = ObjectWrapper.wrap(this._manifest, this._window);
|
|
|
|
},
|
|
|
|
|
|
|
|
get updateManifest() {
|
|
|
|
return this.updateManifest = this._updateManifest ? ObjectWrapper.wrap(this._updateManifest, this._window)
|
|
|
|
: null;
|
|
|
|
},
|
|
|
|
|
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-09-26 18:01:20 -07:00
|
|
|
set ondownloadsuccess(aCallback) {
|
|
|
|
this._ondownloadsuccess = aCallback;
|
|
|
|
},
|
|
|
|
|
|
|
|
get ondownloadsuccess() {
|
|
|
|
return this._ondownloadsuccess;
|
|
|
|
},
|
|
|
|
|
|
|
|
set ondownloaderror(aCallback) {
|
|
|
|
this._ondownloaderror = aCallback;
|
|
|
|
},
|
|
|
|
|
|
|
|
get ondownloaderror() {
|
|
|
|
return this._ondownloaderror;
|
|
|
|
},
|
|
|
|
|
|
|
|
set ondownloadavailable(aCallback) {
|
|
|
|
this._ondownloadavailable = aCallback;
|
|
|
|
},
|
|
|
|
|
|
|
|
get ondownloadavailable() {
|
|
|
|
return this._ondownloadavailable;
|
|
|
|
},
|
|
|
|
|
|
|
|
set ondownloadapplied(aCallback) {
|
|
|
|
this._ondownloadapplied = aCallback;
|
|
|
|
},
|
|
|
|
|
|
|
|
get ondownloadapplied() {
|
|
|
|
return this._ondownloadapplied;
|
|
|
|
},
|
|
|
|
|
|
|
|
get downloadError() {
|
|
|
|
return createDOMError(this._downloadError);
|
|
|
|
},
|
|
|
|
|
|
|
|
download: function() {
|
2012-09-29 10:57:18 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:Download",
|
|
|
|
{ manifestURL: this.manifestURL });
|
2012-09-26 18:01:20 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
cancelDownload: function() {
|
|
|
|
cpmm.sendAsyncMessage("Webapps:CancelDownload",
|
|
|
|
{ manifestURL: this.manifestURL });
|
|
|
|
},
|
|
|
|
|
|
|
|
checkForUpdate: function() {
|
|
|
|
let request = this.createRequest();
|
2012-12-03 11:38:09 -08:00
|
|
|
|
2012-12-17 11:11:05 -08:00
|
|
|
cpmm.sendAsyncMessage("Webapps:CheckForUpdate",
|
|
|
|
{ manifestURL: this.manifestURL,
|
|
|
|
oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
2012-09-26 18:01:20 -07:00
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
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-09-07 08:41:26 -07:00
|
|
|
manifestURL: this.manifestURL,
|
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-09-26 15:03:25 -07:00
|
|
|
clearBrowserData: function() {
|
|
|
|
let browserChild =
|
|
|
|
BrowserElementPromptService.getBrowserElementChildForWindow(this._window);
|
|
|
|
if (browserChild) {
|
|
|
|
browserChild.messageManager.sendAsyncMessage("Webapps:ClearBrowserData");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-11 11:41:46 -07:00
|
|
|
uninit: function() {
|
|
|
|
this._onprogress = null;
|
2012-10-19 11:17:05 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
|
|
|
|
["Webapps:Uninstall:Return:OK", "Webapps:OfflineCache",
|
2012-12-28 09:57:35 -08:00
|
|
|
"Webapps:PackageEvent", "Webapps:CheckForUpdate:Return:OK"]);
|
2012-06-11 11:41:46 -07:00
|
|
|
},
|
|
|
|
|
2012-09-26 18:01:20 -07:00
|
|
|
_fireEvent: function(aName, aHandler) {
|
|
|
|
if (aHandler) {
|
|
|
|
let event = new this._window.MozApplicationEvent(aName, { application: this });
|
|
|
|
aHandler.handleEvent(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
receiveMessage: function(aMessage) {
|
2012-09-27 12:34:41 -07:00
|
|
|
let msg = aMessage.json;
|
2012-06-11 11:41:46 -07:00
|
|
|
let req = this.takeRequest(msg.requestID);
|
2012-12-28 09:57:35 -08:00
|
|
|
|
|
|
|
// ondownload* callbacks should be triggered on all app instances
|
2012-09-27 12:34:41 -07:00
|
|
|
if ((msg.oid != this._id || !req) &&
|
|
|
|
aMessage.name !== "Webapps:OfflineCache" &&
|
2012-12-28 09:57:35 -08:00
|
|
|
aMessage.name !== "Webapps:PackageEvent" &&
|
|
|
|
aMessage.name !== "Webapps:CheckForUpdate:Return:OK")
|
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-09-26 18:01:20 -07:00
|
|
|
case "Webapps:Launch:Return:KO":
|
|
|
|
Services.DOMRequest.fireError(req, "APP_INSTALL_PENDING");
|
|
|
|
break;
|
|
|
|
case "Webapps:Uninstall:Return:KO":
|
|
|
|
Services.DOMRequest.fireError(req, "NOT_INSTALLED");
|
|
|
|
break;
|
2012-12-28 09:57:35 -08:00
|
|
|
case "Webapps:CheckForUpdate:Return:KO":
|
|
|
|
Services.DOMRequest.fireError(req, msg.error);
|
|
|
|
break;
|
|
|
|
case "Webapps:CheckForUpdate:Return:OK":
|
|
|
|
if (msg.manifestURL != this.manifestURL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let prop in msg.app) {
|
|
|
|
this[prop] = msg.app[prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.event == "downloadapplied") {
|
|
|
|
this._fireEvent("downloadapplied", this._ondownloadapplied);
|
|
|
|
} else if (msg.event == "downloadavailable") {
|
|
|
|
this._fireEvent("downloadavailable", this._ondownloadavailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req) {
|
|
|
|
Services.DOMRequest.fireSuccess(req, this.manifestURL);
|
|
|
|
}
|
|
|
|
break;
|
2012-06-11 11:41:46 -07:00
|
|
|
case "Webapps:OfflineCache":
|
|
|
|
if (msg.manifest != this.manifestURL)
|
|
|
|
return;
|
2012-08-27 19:43:57 -07:00
|
|
|
|
2012-10-04 11:18:39 -07:00
|
|
|
if ("installState" in msg) {
|
2012-09-26 18:01:20 -07:00
|
|
|
this.installState = msg.installState;
|
|
|
|
if (this.installState == "installed") {
|
2012-10-04 11:18:39 -07:00
|
|
|
this._downloadError = null;
|
2012-11-26 20:03:43 -08:00
|
|
|
this.downloading = false;
|
2012-09-26 18:01:20 -07:00
|
|
|
this._fireEvent("downloadsuccess", this._ondownloadsuccess);
|
|
|
|
this._fireEvent("downloadapplied", this._ondownloadapplied);
|
|
|
|
} else {
|
|
|
|
this._fireEvent("downloadprogress", this._onprogress);
|
|
|
|
}
|
|
|
|
} else if (msg.error) {
|
|
|
|
this._downloadError = msg.error;
|
2012-10-04 11:18:39 -07:00
|
|
|
this.downloading = false;
|
2012-09-26 18:01:20 -07:00
|
|
|
this._fireEvent("downloaderror", this._ondownloaderror);
|
2012-06-11 11:41:46 -07:00
|
|
|
}
|
|
|
|
break;
|
2012-12-28 09:57:35 -08:00
|
|
|
case "Webapps:PackageEvent":
|
|
|
|
if (msg.manifestURL != this.manifestURL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Set app values according to parent process results.
|
|
|
|
let app = msg.app;
|
|
|
|
this.downloading = app.downloading;
|
|
|
|
this.downloadAvailable = app.downloadAvailable;
|
|
|
|
this.downloadSize = app.downloadSize || 0;
|
|
|
|
this.installState = app.installState;
|
|
|
|
this.progress = app.progress || msg.progress || 0;
|
|
|
|
this.readyToApplyDownload = app.readyToApplyDownload;
|
|
|
|
this.updateTime = app.updateTime;
|
|
|
|
|
|
|
|
switch(msg.type) {
|
|
|
|
case "error":
|
|
|
|
case "canceled":
|
|
|
|
this._downloadError = msg.error;
|
|
|
|
this._fireEvent("downloaderror", this._ondownloaderror);
|
|
|
|
break;
|
|
|
|
case "progress":
|
|
|
|
this._fireEvent("downloadprogress", this._onprogress);
|
|
|
|
break;
|
|
|
|
case "installed":
|
|
|
|
this._manifest = msg.manifest;
|
|
|
|
this._fireEvent("downloadsuccess", this._ondownloadsuccess);
|
2012-10-29 17:17:47 -07:00
|
|
|
this._fireEvent("downloadapplied", this._ondownloadapplied);
|
2012-12-28 09:57:35 -08:00
|
|
|
break;
|
|
|
|
case "downloaded":
|
|
|
|
this._manifest = msg.manifest;
|
|
|
|
this._fireEvent("downloadsuccess", this._ondownloadsuccess);
|
|
|
|
break;
|
|
|
|
case "applied":
|
|
|
|
this._fireEvent("downloadapplied", this._ondownloadapplied);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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",
|
2012-08-20 10:19:56 -07:00
|
|
|
"Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK",
|
|
|
|
"Webapps:GetNotInstalled:Return:OK"]);
|
2012-03-06 11:50:58 -08:00
|
|
|
|
2012-09-18 10:34:55 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
|
|
|
|
["Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK"]);
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
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__: {
|
2012-09-26 18:01:20 -07:00
|
|
|
applyDownload: "r",
|
|
|
|
getAll: "r",
|
|
|
|
getNotInstalled: "r",
|
|
|
|
oninstall: "rw",
|
|
|
|
onuninstall: "rw"
|
2012-06-11 11:41:46 -07:00
|
|
|
},
|
2012-03-06 11:50:58 -08:00
|
|
|
|
|
|
|
uninit: function() {
|
|
|
|
this._oninstall = null;
|
|
|
|
this._onuninstall = null;
|
2012-10-19 11:17:05 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
|
|
|
|
["Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK"]);
|
2011-11-28 12:13:26 -08:00
|
|
|
},
|
|
|
|
|
2012-09-26 18:01:20 -07:00
|
|
|
applyDownload: function(aApp) {
|
2012-09-29 10:57:18 -07:00
|
|
|
if (!aApp.readyToApplyDownload) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-29 17:17:47 -07:00
|
|
|
cpmm.sendAsyncMessage("Webapps:ApplyDownload",
|
2012-09-29 10:57:18 -07:00
|
|
|
{ manifestURL: aApp.manifestURL });
|
2012-09-26 18:01:20 -07: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;
|
|
|
|
},
|
|
|
|
|
2012-08-20 10:19:56 -07:00
|
|
|
getNotInstalled: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Webapps:GetNotInstalled", { oid: this._id,
|
|
|
|
requestID: this.getRequestId(request) });
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
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
|
2012-08-27 19:43:57 -07:00
|
|
|
if (!((msg.oid == this._id && req)
|
2012-03-06 11:50:58 -08:00
|
|
|
|| 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;
|
2012-08-20 10:19:56 -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:Install:Return:OK":
|
|
|
|
if (this._oninstall) {
|
|
|
|
let app = msg.app;
|
2012-08-27 19:43:57 -07:00
|
|
|
let event = new this._window.MozApplicationEvent("applicationinstall",
|
2012-08-29 14:20:03 -07:00
|
|
|
{ application : createApplicationObject(this._window, app) });
|
2012-03-06 11:50:58 -08:00
|
|
|
this._oninstall.handleEvent(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Webapps:Uninstall:Return:OK":
|
|
|
|
if (this._onuninstall) {
|
2012-12-03 14:46:57 -08:00
|
|
|
let detail = {
|
|
|
|
manifestURL: msg.manifestURL,
|
|
|
|
origin: msg.origin
|
|
|
|
};
|
2012-08-27 19:43:57 -07:00
|
|
|
let event = new this._window.MozApplicationEvent("applicationuninstall",
|
2012-12-03 14:46:57 -08:00
|
|
|
{ application : createApplicationObject(this._window, detail) });
|
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"})
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsRegistry,
|
|
|
|
WebappsApplication,
|
|
|
|
DOMError]);
|