2012-03-06 23:50:58 +04: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-29 00:13:26 +04:00
|
|
|
|
2012-07-03 04:16:55 +04:00
|
|
|
"use strict";
|
|
|
|
|
2012-04-28 11:10:08 +04:00
|
|
|
const Cu = Components.utils;
|
2011-11-29 00:13:26 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2012-07-11 19:38:33 +04:00
|
|
|
const Cr = Components.results;
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2013-11-19 23:41:54 +04:00
|
|
|
// Possible errors thrown by the signature verifier.
|
|
|
|
const SEC_ERROR_BASE = Ci.nsINSSErrorsService.NSS_SEC_ERROR_BASE;
|
|
|
|
const SEC_ERROR_EXPIRED_CERTIFICATE = (SEC_ERROR_BASE + 11);
|
|
|
|
|
|
|
|
// We need this to decide if we should accept or not files signed with expired
|
|
|
|
// certificates.
|
|
|
|
function buildIDToTime() {
|
|
|
|
let platformBuildID =
|
|
|
|
Cc["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Ci.nsIXULAppInfo).platformBuildID;
|
|
|
|
let platformBuildIDDate = new Date();
|
|
|
|
platformBuildIDDate.setUTCFullYear(platformBuildID.substr(0,4),
|
|
|
|
platformBuildID.substr(4,2) - 1,
|
|
|
|
platformBuildID.substr(6,2));
|
|
|
|
platformBuildIDDate.setUTCHours(platformBuildID.substr(8,2),
|
|
|
|
platformBuildID.substr(10,2),
|
|
|
|
platformBuildID.substr(12,2));
|
|
|
|
return platformBuildIDDate.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
const PLATFORM_BUILD_ID_TIME = buildIDToTime();
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry"];
|
2011-11-29 00:13:26 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2011-12-06 08:22:01 +04:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2012-07-20 19:41:30 +04:00
|
|
|
Cu.import('resource://gre/modules/ActivitiesService.jsm');
|
2012-08-28 06:43:57 +04:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2012-12-21 22:28:55 +04:00
|
|
|
Cu.import("resource://gre/modules/AppDownloadManager.jsm");
|
2013-08-29 01:35:34 +04:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2013-11-26 03:14:35 +04:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2014-01-21 21:53:55 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TrustedRootCertificate",
|
|
|
|
"resource://gre/modules/StoreTrustAnchor.jsm");
|
|
|
|
|
2014-03-24 17:37:27 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PermissionsInstaller",
|
|
|
|
"resource://gre/modules/PermissionsInstaller.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "OfflineCacheInstaller",
|
|
|
|
"resource://gre/modules/OfflineCacheInstaller.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SystemMessagePermissionsChecker",
|
|
|
|
"resource://gre/modules/SystemMessagePermissionsChecker.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "WebappOSUtils",
|
|
|
|
"resource://gre/modules/WebappOSUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
|
|
|
|
"resource://gre/modules/NetUtil.jsm");
|
|
|
|
|
2014-04-18 09:03:03 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ScriptPreloader",
|
|
|
|
"resource://gre/modules/ScriptPreloader.jsm");
|
|
|
|
|
2013-05-06 21:41:07 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "libcutils", function() {
|
|
|
|
Cu.import("resource://gre/modules/systemlibs.js");
|
|
|
|
return libcutils;
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
2014-05-10 00:15:54 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
// On Android, define the "debug" function as a binding of the "d" function
|
|
|
|
// from the AndroidLog module so it gets the "debug" priority and a log tag.
|
2014-07-01 04:27:34 +04:00
|
|
|
// We always report debug messages on Android because it's unnecessary
|
|
|
|
// to restrict reporting, per bug 1003469.
|
2014-05-10 00:15:54 +04:00
|
|
|
let debug = Cu.import("resource://gre/modules/AndroidLog.jsm", {})
|
|
|
|
.AndroidLog.d.bind(null, "Webapps");
|
|
|
|
#else
|
2014-07-01 04:27:34 +04:00
|
|
|
// Elsewhere, report debug messages only if dom.mozApps.debug is set to true.
|
|
|
|
// The pref is only checked once, on startup, so restart after changing it.
|
|
|
|
let debug = Services.prefs.getBoolPref("dom.mozApps.debug")
|
|
|
|
? (aMsg) => dump("-*- Webapps.jsm : " + aMsg + "\n")
|
|
|
|
: (aMsg) => {};
|
2014-05-10 00:15:54 +04:00
|
|
|
#endif
|
2012-09-27 05:01:20 +04:00
|
|
|
|
2013-11-19 23:41:54 +04:00
|
|
|
function getNSPRErrorCode(err) {
|
|
|
|
return -1 * ((err) & 0xffff);
|
|
|
|
}
|
|
|
|
|
2013-07-08 00:41:55 +04:00
|
|
|
function supportUseCurrentProfile() {
|
|
|
|
return Services.prefs.getBoolPref("dom.webapps.useCurrentProfile");
|
|
|
|
}
|
|
|
|
|
2013-04-30 17:01:46 +04:00
|
|
|
function supportSystemMessages() {
|
2013-07-08 00:41:55 +04:00
|
|
|
return Services.prefs.getBoolPref("dom.sysmsg.enabled");
|
2013-04-30 17:01:46 +04:00
|
|
|
}
|
|
|
|
|
2012-12-13 21:54:49 +04:00
|
|
|
// Minimum delay between two progress events while downloading, in ms.
|
2013-10-17 16:47:58 +04:00
|
|
|
const MIN_PROGRESS_EVENT_DELAY = 1500;
|
2012-12-13 21:54:49 +04:00
|
|
|
|
2012-04-17 18:11:53 +04:00
|
|
|
const WEBAPP_RUNTIME = Services.appinfo.ID == "webapprt@mozilla.org";
|
|
|
|
|
2014-03-16 01:37:37 +04:00
|
|
|
const chromeWindowType = WEBAPP_RUNTIME ? "webapprt:webapp" : "navigator:browser";
|
|
|
|
|
2012-08-27 18:13:02 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageBroadcaster");
|
2012-03-06 23:50:58 +04:00
|
|
|
|
2012-07-20 19:41:30 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
2012-08-27 18:13:02 +04:00
|
|
|
"nsIMessageSender");
|
2012-07-20 19:41:30 +04:00
|
|
|
|
2013-08-16 13:48:37 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "interAppCommService", function() {
|
|
|
|
return Cc["@mozilla.org/inter-app-communication-service;1"]
|
|
|
|
.getService(Ci.nsIInterAppCommService);
|
|
|
|
});
|
|
|
|
|
2013-10-02 21:27:07 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "dataStoreService",
|
|
|
|
"@mozilla.org/datastore-service;1",
|
|
|
|
"nsIDataStoreService");
|
|
|
|
|
2012-07-03 04:16:55 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "msgmgr", function() {
|
2012-07-11 19:38:33 +04:00
|
|
|
return Cc["@mozilla.org/system-message-internal;1"]
|
|
|
|
.getService(Ci.nsISystemMessagesInternal);
|
2012-07-03 04:16:55 +04:00
|
|
|
});
|
|
|
|
|
2012-12-05 20:50:32 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "updateSvc", function() {
|
|
|
|
return Cc["@mozilla.org/offlinecacheupdate-service;1"]
|
|
|
|
.getService(Ci.nsIOfflineCacheUpdateService);
|
|
|
|
});
|
|
|
|
|
2012-03-24 03:39:15 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2014-02-21 23:54:03 +04:00
|
|
|
const DIRECTORY_NAME = "webappsDir";
|
2012-08-16 01:02:15 +04:00
|
|
|
#elifdef ANDROID
|
2014-02-21 23:54:03 +04:00
|
|
|
const DIRECTORY_NAME = "webappsDir";
|
2012-03-24 03:39:15 +04:00
|
|
|
#else
|
2012-04-17 18:11:53 +04:00
|
|
|
// If we're executing in the context of the webapp runtime, the data files
|
|
|
|
// are in a different directory (currently the Firefox profile that installed
|
|
|
|
// the webapp); otherwise, they're in the current profile.
|
2014-02-21 23:54:03 +04:00
|
|
|
const DIRECTORY_NAME = WEBAPP_RUNTIME ? "WebappRegD" : "ProfD";
|
2012-03-24 03:39:15 +04:00
|
|
|
#endif
|
|
|
|
|
2013-06-26 16:18:34 +04:00
|
|
|
// We'll use this to identify privileged apps that have been preinstalled
|
|
|
|
// For those apps we'll set
|
|
|
|
// STORE_ID_PENDING_PREFIX + installOrigin
|
|
|
|
// as the storeID. This ensures it's unique and can't be set from a legit
|
|
|
|
// store even by error.
|
|
|
|
const STORE_ID_PENDING_PREFIX = "#unknownID#";
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.DOMApplicationRegistry = {
|
2014-08-01 21:41:52 +04:00
|
|
|
// pseudo-constants for the different application kinds.
|
|
|
|
get kPackaged() "packaged",
|
|
|
|
get kHosted() "hosted",
|
|
|
|
get kHostedAppcache() "hosted-appcache",
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Path to the webapps.json file where we store the registry data.
|
2011-11-29 00:13:26 +04:00
|
|
|
appsFile: null,
|
|
|
|
webapps: { },
|
2012-08-28 06:43:57 +04:00
|
|
|
children: [ ],
|
2012-06-30 00:46:21 +04:00
|
|
|
allAppsLaunchable: false,
|
2014-03-16 01:37:37 +04:00
|
|
|
_updateHandlers: [ ],
|
2014-06-12 01:23:18 +04:00
|
|
|
_pendingUninstalls: {},
|
2011-11-29 00:13:26 +04:00
|
|
|
|
|
|
|
init: function() {
|
2012-03-13 04:33:10 +04:00
|
|
|
this.messages = ["Webapps:Install", "Webapps:Uninstall",
|
2012-10-03 00:38:51 +04:00
|
|
|
"Webapps:GetSelf", "Webapps:CheckInstalled",
|
2012-08-28 06:43:57 +04:00
|
|
|
"Webapps:GetInstalled", "Webapps:GetNotInstalled",
|
2014-07-07 22:01:30 +04:00
|
|
|
"Webapps:Launch",
|
2013-02-01 01:35:13 +04:00
|
|
|
"Webapps:InstallPackage",
|
2012-09-18 21:34:55 +04:00
|
|
|
"Webapps:GetList", "Webapps:RegisterForMessages",
|
2012-10-19 22:17:05 +04:00
|
|
|
"Webapps:UnregisterForMessages",
|
2012-09-29 21:57:18 +04:00
|
|
|
"Webapps:CancelDownload", "Webapps:CheckForUpdate",
|
2012-10-30 04:17:47 +04:00
|
|
|
"Webapps:Download", "Webapps:ApplyDownload",
|
2014-01-17 04:26:26 +04:00
|
|
|
"Webapps:Install:Return:Ack", "Webapps:AddReceipt",
|
|
|
|
"Webapps:RemoveReceipt", "Webapps:ReplaceReceipt",
|
2012-10-02 00:29:59 +04:00
|
|
|
"child-process-shutdown"];
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2012-09-27 02:03:25 +04:00
|
|
|
this.frameMessages = ["Webapps:ClearBrowserData"];
|
|
|
|
|
2012-03-13 04:33:10 +04:00
|
|
|
this.messages.forEach((function(msgName) {
|
2012-03-06 23:50:58 +04:00
|
|
|
ppmm.addMessageListener(msgName, this);
|
2011-11-29 00:13:26 +04:00
|
|
|
}).bind(this));
|
|
|
|
|
2012-09-19 20:28:55 +04:00
|
|
|
cpmm.addMessageListener("Activities:Register:OK", this);
|
2014-07-15 01:27:57 +04:00
|
|
|
cpmm.addMessageListener("Activities:Register:KO", this);
|
2012-09-19 20:28:55 +04:00
|
|
|
|
2012-03-13 04:33:10 +04:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2014-01-07 01:03:50 +04:00
|
|
|
Services.obs.addObserver(this, "memory-pressure", false);
|
2012-03-13 04:33:10 +04:00
|
|
|
|
2012-12-21 22:28:55 +04:00
|
|
|
AppDownloadManager.registerCancelFunction(this.cancelDownload.bind(this));
|
|
|
|
|
2014-02-21 23:54:03 +04:00
|
|
|
this.appsFile = FileUtils.getFile(DIRECTORY_NAME,
|
|
|
|
["webapps", "webapps.json"], true).path;
|
2011-12-06 08:22:01 +04:00
|
|
|
|
2012-09-19 20:28:55 +04:00
|
|
|
this.loadAndUpdateApps();
|
|
|
|
},
|
2012-08-30 02:58:31 +04:00
|
|
|
|
2012-09-19 20:28:55 +04:00
|
|
|
// loads the current registry, that could be empty on first run.
|
2014-01-17 04:26:24 +04:00
|
|
|
loadCurrentRegistry: function() {
|
2014-03-25 17:14:06 +04:00
|
|
|
return AppsUtils.loadJSONAsync(this.appsFile).then((aData) => {
|
2014-01-17 04:26:24 +04:00
|
|
|
if (!aData) {
|
2014-02-21 23:54:03 +04:00
|
|
|
return;
|
2014-01-17 04:26:24 +04:00
|
|
|
}
|
2012-12-22 17:56:21 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this.webapps = aData;
|
|
|
|
let appDir = OS.Path.dirname(this.appsFile);
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
let app = this.webapps[id];
|
|
|
|
if (!app) {
|
|
|
|
delete this.webapps[id];
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-22 17:56:21 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
app.id = id;
|
2012-09-19 20:28:55 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Make sure we have a localId
|
|
|
|
if (app.localId === undefined) {
|
|
|
|
app.localId = this._nextLocalId();
|
|
|
|
}
|
2012-09-19 20:28:55 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
if (app.basePath === undefined) {
|
|
|
|
app.basePath = appDir;
|
|
|
|
}
|
2012-08-30 01:20:03 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Default to removable apps.
|
|
|
|
if (app.removable === undefined) {
|
|
|
|
app.removable = true;
|
|
|
|
}
|
2012-12-11 03:49:02 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Default to a non privileged status.
|
|
|
|
if (app.appStatus === undefined) {
|
|
|
|
app.appStatus = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
}
|
2013-01-07 03:56:40 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Default to NO_APP_ID and not in browser.
|
|
|
|
if (app.installerAppId === undefined) {
|
|
|
|
app.installerAppId = Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
}
|
|
|
|
if (app.installerIsBrowser === undefined) {
|
|
|
|
app.installerIsBrowser = false;
|
|
|
|
}
|
2013-01-31 10:50:28 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Default installState to "installed", and reset if we shutdown
|
|
|
|
// during an update.
|
|
|
|
if (app.installState === undefined ||
|
|
|
|
app.installState === "updating") {
|
|
|
|
app.installState = "installed";
|
|
|
|
}
|
2013-01-25 13:57:14 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Default storeId to "" and storeVersion to 0
|
2014-08-01 21:41:52 +04:00
|
|
|
if (app.storeId === undefined) {
|
|
|
|
app.storeId = "";
|
2014-01-17 04:26:24 +04:00
|
|
|
}
|
2014-08-01 21:41:52 +04:00
|
|
|
if (app.storeVersion === undefined) {
|
|
|
|
app.storeVersion = 0;
|
2014-01-17 04:26:24 +04:00
|
|
|
}
|
2013-09-11 16:15:48 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// Default role to "".
|
2014-08-01 21:41:52 +04:00
|
|
|
if (app.role === undefined) {
|
|
|
|
app.role = "";
|
2012-09-19 20:28:55 +04:00
|
|
|
}
|
2014-01-17 04:26:24 +04:00
|
|
|
|
2014-08-19 18:14:08 +04:00
|
|
|
if (app.widgetPages === undefined) {
|
|
|
|
app.widgetPages = [];
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:20:27 +04:00
|
|
|
if (!AppsUtils.checkAppRole(app.role, app.appStatus)) {
|
|
|
|
delete this.webapps[id];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// At startup we can't be downloading, and the $TMP directory
|
|
|
|
// will be empty so we can't just apply a staged update.
|
|
|
|
app.downloading = false;
|
|
|
|
app.readyToApplyDownload = false;
|
|
|
|
}
|
|
|
|
});
|
2012-09-19 20:28:55 +04:00
|
|
|
},
|
|
|
|
|
2012-10-17 08:30:43 +04:00
|
|
|
// Notify we are starting with registering apps.
|
2014-01-22 22:06:29 +04:00
|
|
|
_registryStarted: Promise.defer(),
|
2012-10-17 08:30:43 +04:00
|
|
|
notifyAppsRegistryStart: function notifyAppsRegistryStart() {
|
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-start", null);
|
2014-01-22 22:06:29 +04:00
|
|
|
this._registryStarted.resolve();
|
|
|
|
},
|
|
|
|
|
|
|
|
get registryStarted() {
|
|
|
|
return this._registryStarted.promise;
|
2012-10-17 08:30:43 +04:00
|
|
|
},
|
|
|
|
|
2014-07-15 01:27:57 +04:00
|
|
|
// The registry will be safe to clone when this promise is resolved.
|
|
|
|
_safeToClone: Promise.defer(),
|
|
|
|
|
2012-10-17 08:30:43 +04:00
|
|
|
// Notify we are done with registering apps and save a copy of the registry.
|
2014-01-22 22:06:29 +04:00
|
|
|
_registryReady: Promise.defer(),
|
2012-10-17 08:30:43 +04:00
|
|
|
notifyAppsRegistryReady: function notifyAppsRegistryReady() {
|
2014-07-15 01:27:57 +04:00
|
|
|
// Usually this promise will be resolved earlier, but just in case,
|
|
|
|
// resolve it here also.
|
|
|
|
this._safeToClone.resolve();
|
2014-01-22 22:06:29 +04:00
|
|
|
this._registryReady.resolve();
|
2012-09-19 20:28:55 +04:00
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-ready", null);
|
|
|
|
this._saveApps();
|
|
|
|
},
|
|
|
|
|
2014-01-22 22:06:29 +04:00
|
|
|
get registryReady() {
|
|
|
|
return this._registryReady.promise;
|
|
|
|
},
|
|
|
|
|
2014-07-15 01:27:57 +04:00
|
|
|
get safeToClone() {
|
|
|
|
return this._safeToClone.promise;
|
|
|
|
},
|
|
|
|
|
2013-05-14 23:00:09 +04:00
|
|
|
// Ensure that the .to property in redirects is a relative URL.
|
|
|
|
sanitizeRedirects: function sanitizeRedirects(aSource) {
|
|
|
|
if (!aSource) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = [];
|
|
|
|
for (let i = 0; i < aSource.length; i++) {
|
|
|
|
let redirect = aSource[i];
|
|
|
|
if (redirect.from && redirect.to &&
|
|
|
|
isAbsoluteURI(redirect.from) &&
|
|
|
|
!isAbsoluteURI(redirect.to)) {
|
|
|
|
res.push(redirect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res.length > 0 ? res : null;
|
|
|
|
},
|
|
|
|
|
2014-08-19 18:14:08 +04:00
|
|
|
_saveWidgetsFullPath: function(aManifest, aDestApp) {
|
|
|
|
if (aManifest.widgetPages) {
|
|
|
|
aDestApp.widgetPages = aManifest.widgetPages.map(aManifest.resolveURL,
|
|
|
|
aManifest/* thisArg */);
|
|
|
|
} else {
|
|
|
|
aDestApp.widgetPages = [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-17 08:30:43 +04:00
|
|
|
// Registers all the activities and system messages.
|
2014-06-11 02:52:47 +04:00
|
|
|
registerAppsHandlers: Task.async(function*(aRunUpdate) {
|
2012-10-17 08:30:43 +04:00
|
|
|
this.notifyAppsRegistryStart();
|
2012-09-19 20:28:55 +04:00
|
|
|
let ids = [];
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
ids.push({ id: id });
|
|
|
|
}
|
2013-04-30 17:01:46 +04:00
|
|
|
if (supportSystemMessages()) {
|
|
|
|
this._processManifestForIds(ids, aRunUpdate);
|
|
|
|
} else {
|
2013-09-11 16:15:48 +04:00
|
|
|
// Read the CSPs and roles. If MOZ_SYS_MSG is defined this is done on
|
2013-04-30 17:01:46 +04:00
|
|
|
// _processManifestForIds so as to not reading the manifests
|
|
|
|
// twice
|
2014-06-11 02:52:47 +04:00
|
|
|
let results = yield this._readManifests(ids);
|
|
|
|
results.forEach((aResult) => {
|
|
|
|
if (!aResult.manifest) {
|
|
|
|
// If we can't load the manifest, we probably have a corrupted
|
|
|
|
// registry. We delete the app since we can't do anything with it.
|
|
|
|
delete this.webapps[aResult.id];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let app = this.webapps[aResult.id];
|
|
|
|
app.csp = aResult.manifest.csp || "";
|
|
|
|
app.role = aResult.manifest.role || "";
|
2014-08-19 18:14:08 +04:00
|
|
|
|
|
|
|
let localeManifest = new ManifestHelper(aResult.manifest, app.origin, app.manifestURL);
|
|
|
|
this._saveWidgetsFullPath(localeManifest, app);
|
|
|
|
|
2014-06-11 02:52:47 +04:00
|
|
|
if (app.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED) {
|
|
|
|
app.redirects = this.sanitizeRedirects(aResult.redirects);
|
|
|
|
}
|
2014-08-01 21:41:52 +04:00
|
|
|
if (app.origin.startsWith("app://")) {
|
|
|
|
app.kind = this.kPackaged;
|
|
|
|
} else {
|
|
|
|
// Hosted apps, can be appcached or not.
|
|
|
|
app.kind = aResult.manifest.appcache_path ? this.kHostedAppcache
|
|
|
|
: this.kHosted;
|
|
|
|
}
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-10-19 14:43:17 +04:00
|
|
|
|
2013-04-30 17:01:46 +04:00
|
|
|
// Nothing else to do but notifying we're ready.
|
|
|
|
this.notifyAppsRegistryReady();
|
|
|
|
}
|
2014-06-11 02:52:47 +04:00
|
|
|
}),
|
2012-09-19 20:28:55 +04:00
|
|
|
|
2014-06-11 02:52:47 +04:00
|
|
|
updateDataStoreForApp: Task.async(function*(aId) {
|
2013-10-02 21:27:07 +04:00
|
|
|
if (!this.webapps[aId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or Update the DataStore for this app
|
2014-06-11 02:52:47 +04:00
|
|
|
let results = yield this._readManifests([{ id: aId }]);
|
|
|
|
let app = this.webapps[aId];
|
|
|
|
this.updateDataStore(app.localId, app.origin, app.manifestURL,
|
|
|
|
results[0].manifest, app.appStatus);
|
|
|
|
}),
|
2013-10-02 21:27:07 +04:00
|
|
|
|
2014-06-11 12:47:00 +04:00
|
|
|
updatePermissionsForApp: function(aId, aIsPreinstalled, aIsSystemUpdate) {
|
2012-12-11 00:51:10 +04:00
|
|
|
if (!this.webapps[aId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-10 20:16:49 +04:00
|
|
|
// Install the permissions for this app, as if we were updating
|
|
|
|
// to cleanup the old ones if needed.
|
2013-06-20 20:51:18 +04:00
|
|
|
// TODO It's not clear what this should do when there are multiple profiles.
|
2013-07-08 00:41:55 +04:00
|
|
|
if (supportUseCurrentProfile()) {
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests([{ id: aId }]).then((aResult) => {
|
2013-07-08 00:41:55 +04:00
|
|
|
let data = aResult[0];
|
|
|
|
PermissionsInstaller.installPermissions({
|
|
|
|
manifest: data.manifest,
|
|
|
|
manifestURL: this.webapps[aId].manifestURL,
|
2014-05-26 14:30:11 +04:00
|
|
|
origin: this.webapps[aId].origin,
|
2014-06-11 12:47:00 +04:00
|
|
|
isPreinstalled: aIsPreinstalled,
|
|
|
|
isSystemUpdate: aIsSystemUpdate
|
2013-07-08 00:41:55 +04:00
|
|
|
}, true, function() {
|
|
|
|
debug("Error installing permissions for " + aId);
|
|
|
|
});
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2013-07-08 00:41:55 +04:00
|
|
|
}
|
2012-10-10 20:16:49 +04:00
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
updateOfflineCacheForApp: function(aId) {
|
2012-10-24 00:52:57 +04:00
|
|
|
let app = this.webapps[aId];
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests([{ id: aId }]).then((aResult) => {
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aResult[0].manifest, app.origin, app.manifestURL);
|
2013-05-16 00:51:41 +04:00
|
|
|
OfflineCacheInstaller.installCache({
|
|
|
|
cachePath: app.cachePath,
|
|
|
|
appId: aId,
|
|
|
|
origin: Services.io.newURI(app.origin, null, null),
|
|
|
|
localId: app.localId,
|
|
|
|
appcache_path: manifest.fullAppcachePath()
|
|
|
|
});
|
2012-10-24 00:52:57 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-01-09 19:06:07 +04:00
|
|
|
// Installs a 3rd party app.
|
|
|
|
installPreinstalledApp: function installPreinstalledApp(aId) {
|
2012-12-12 01:45:37 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-12-11 00:51:10 +04:00
|
|
|
let app = this.webapps[aId];
|
2014-05-26 14:30:11 +04:00
|
|
|
let baseDir, isPreinstalled = false;
|
2012-12-11 00:51:10 +04:00
|
|
|
try {
|
2012-12-22 17:56:21 +04:00
|
|
|
baseDir = FileUtils.getDir("coreAppsDir", ["webapps", aId], false);
|
2013-01-10 11:53:17 +04:00
|
|
|
if (!baseDir.exists()) {
|
2014-05-26 14:30:11 +04:00
|
|
|
return isPreinstalled;
|
2013-07-15 04:53:41 +04:00
|
|
|
} else if (!baseDir.directoryEntries.hasMoreElements()) {
|
|
|
|
debug("Error: Core app in " + baseDir.path + " is empty");
|
2014-05-26 14:30:11 +04:00
|
|
|
return isPreinstalled;
|
2013-01-10 11:53:17 +04:00
|
|
|
}
|
2012-12-11 00:51:10 +04:00
|
|
|
} catch(e) {
|
|
|
|
// In ENG builds, we don't have apps in coreAppsDir.
|
2014-05-26 14:30:11 +04:00
|
|
|
return isPreinstalled;
|
2012-12-11 00:51:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-26 14:30:11 +04:00
|
|
|
// Beyond this point we know it's really a preinstalled app.
|
|
|
|
isPreinstalled = true;
|
|
|
|
|
2013-01-09 19:06:07 +04:00
|
|
|
let filesToMove;
|
|
|
|
let isPackage;
|
|
|
|
|
2012-12-11 00:51:10 +04:00
|
|
|
let updateFile = baseDir.clone();
|
|
|
|
updateFile.append("update.webapp");
|
|
|
|
if (!updateFile.exists()) {
|
2013-01-09 19:06:07 +04:00
|
|
|
// The update manifest is missing, this is a hosted app only if there is
|
|
|
|
// no application.zip
|
|
|
|
let appFile = baseDir.clone();
|
|
|
|
appFile.append("application.zip");
|
|
|
|
if (appFile.exists()) {
|
2014-05-26 14:30:11 +04:00
|
|
|
return isPreinstalled;
|
2013-01-09 19:06:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
isPackage = false;
|
|
|
|
filesToMove = ["manifest.webapp"];
|
|
|
|
} else {
|
|
|
|
isPackage = true;
|
|
|
|
filesToMove = ["application.zip", "update.webapp"];
|
2012-12-11 00:51:10 +04:00
|
|
|
}
|
|
|
|
|
2013-01-09 19:06:07 +04:00
|
|
|
debug("Installing 3rd party app : " + aId +
|
2012-12-11 00:51:10 +04:00
|
|
|
" from " + baseDir.path);
|
|
|
|
|
2014-02-21 23:54:03 +04:00
|
|
|
// We copy this app to DIRECTORY_NAME/$aId, and set the base path as needed.
|
|
|
|
let destDir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
|
2012-12-11 00:51:10 +04:00
|
|
|
|
2013-01-09 19:06:07 +04:00
|
|
|
filesToMove.forEach(function(aFile) {
|
2012-12-11 00:51:10 +04:00
|
|
|
let file = baseDir.clone();
|
|
|
|
file.append(aFile);
|
2013-07-15 04:53:41 +04:00
|
|
|
try {
|
|
|
|
file.copyTo(destDir, aFile);
|
|
|
|
} catch(e) {
|
|
|
|
debug("Error: Failed to copy " + file.path + " to " + destDir.path);
|
|
|
|
}
|
2012-12-11 00:51:10 +04:00
|
|
|
});
|
|
|
|
|
2013-02-01 23:17:32 +04:00
|
|
|
app.installState = "installed";
|
2013-05-16 00:51:41 +04:00
|
|
|
app.cachePath = app.basePath;
|
2014-01-17 04:26:24 +04:00
|
|
|
app.basePath = OS.Path.dirname(this.appsFile);
|
2013-01-09 19:06:07 +04:00
|
|
|
|
|
|
|
if (!isPackage) {
|
2014-05-26 14:30:11 +04:00
|
|
|
return isPreinstalled;
|
2013-01-09 19:06:07 +04:00
|
|
|
}
|
|
|
|
|
2012-12-11 00:51:10 +04:00
|
|
|
app.origin = "app://" + aId;
|
|
|
|
|
2013-06-26 16:18:34 +04:00
|
|
|
// Do this for all preinstalled apps... we can't know at this
|
|
|
|
// point if the updates will be signed or not and it doesn't
|
|
|
|
// hurt to have it always.
|
|
|
|
app.storeId = STORE_ID_PENDING_PREFIX + app.installOrigin;
|
|
|
|
|
2012-12-11 00:51:10 +04:00
|
|
|
// Extract the manifest.webapp file from application.zip.
|
|
|
|
let zipFile = baseDir.clone();
|
|
|
|
zipFile.append("application.zip");
|
|
|
|
let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
|
|
|
|
.createInstance(Ci.nsIZipReader);
|
|
|
|
try {
|
|
|
|
debug("Opening " + zipFile.path);
|
|
|
|
zipReader.open(zipFile);
|
|
|
|
if (!zipReader.hasEntry("manifest.webapp")) {
|
|
|
|
throw "MISSING_MANIFEST";
|
|
|
|
}
|
|
|
|
let manifestFile = destDir.clone();
|
|
|
|
manifestFile.append("manifest.webapp");
|
|
|
|
zipReader.extract("manifest.webapp", manifestFile);
|
|
|
|
} catch(e) {
|
|
|
|
// If we are unable to extract the manifest, cleanup and remove this app.
|
|
|
|
debug("Cleaning up: " + e);
|
|
|
|
destDir.remove(true);
|
|
|
|
delete this.webapps[aId];
|
|
|
|
} finally {
|
|
|
|
zipReader.close();
|
|
|
|
}
|
2014-05-26 14:30:11 +04:00
|
|
|
return isPreinstalled;
|
2012-12-12 01:45:37 +04:00
|
|
|
#endif
|
2012-12-11 00:51:10 +04:00
|
|
|
},
|
|
|
|
|
2013-12-10 02:05:14 +04:00
|
|
|
// For hosted apps, uninstall an app served from http:// if we have
|
|
|
|
// one installed from the same url with an https:// scheme.
|
|
|
|
removeIfHttpsDuplicate: function(aId) {
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
let app = this.webapps[aId];
|
|
|
|
if (!app || !app.origin.startsWith("http://")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let httpsManifestURL =
|
|
|
|
"https://" + app.manifestURL.substring("http://".length);
|
|
|
|
|
|
|
|
// This will uninstall the http apps and remove any data hold by this
|
|
|
|
// app. Bug 948105 tracks data migration from http to https apps.
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].manifestURL === httpsManifestURL) {
|
|
|
|
debug("Found a http/https match: " + app.manifestURL + " / " +
|
|
|
|
this.webapps[id].manifestURL);
|
2014-06-12 01:23:18 +04:00
|
|
|
this.uninstall(app.manifestURL);
|
2013-12-10 02:05:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
|
2012-09-19 20:28:55 +04:00
|
|
|
// Implements the core of bug 787439
|
2012-10-24 00:52:57 +04:00
|
|
|
// if at first run, go through these steps:
|
2012-09-19 20:28:55 +04:00
|
|
|
// a. load the core apps registry.
|
|
|
|
// b. uninstall any core app from the current registry but not in the
|
|
|
|
// new core apps registry.
|
|
|
|
// c. for all apps in the new core registry, install them if they are not
|
|
|
|
// yet in the current registry, and run installPermissions()
|
2014-01-17 04:26:24 +04:00
|
|
|
installSystemApps: function() {
|
|
|
|
return Task.spawn(function() {
|
|
|
|
let file;
|
|
|
|
try {
|
|
|
|
file = FileUtils.getFile("coreAppsDir", ["webapps", "webapps.json"], false);
|
|
|
|
} catch(e) { }
|
2012-09-19 20:28:55 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
if (!file || !file.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-22 17:56:21 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// a
|
2014-03-25 17:14:06 +04:00
|
|
|
let data = yield AppsUtils.loadJSONAsync(file.path);
|
2014-01-17 04:26:24 +04:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-19 20:28:55 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
// b : core apps are not removable.
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (id in data || this.webapps[id].removable)
|
|
|
|
continue;
|
|
|
|
// Remove the permissions, cookies and private data for this app.
|
|
|
|
let localId = this.webapps[id].localId;
|
|
|
|
let permMgr = Cc["@mozilla.org/permissionmanager;1"]
|
|
|
|
.getService(Ci.nsIPermissionManager);
|
|
|
|
permMgr.removePermissionsForApp(localId, false);
|
|
|
|
Services.cookies.removeCookiesForApp(localId, false);
|
|
|
|
this._clearPrivateData(localId, false);
|
|
|
|
delete this.webapps[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
let appDir = FileUtils.getDir("coreAppsDir", ["webapps"], false);
|
|
|
|
// c
|
|
|
|
for (let id in data) {
|
|
|
|
// Core apps have ids matching their domain name (eg: dialer.gaiamobile.org)
|
|
|
|
// Use that property to check if they are new or not.
|
|
|
|
if (!(id in this.webapps)) {
|
|
|
|
this.webapps[id] = data[id];
|
|
|
|
this.webapps[id].basePath = appDir.path;
|
|
|
|
|
|
|
|
this.webapps[id].id = id;
|
|
|
|
|
|
|
|
// Create a new localId.
|
|
|
|
this.webapps[id].localId = this._nextLocalId();
|
|
|
|
|
|
|
|
// Core apps are not removable.
|
|
|
|
if (this.webapps[id].removable === undefined) {
|
|
|
|
this.webapps[id].removable = false;
|
2012-09-19 20:28:55 +04:00
|
|
|
}
|
2014-04-09 17:45:15 +04:00
|
|
|
} else {
|
|
|
|
// we fall into this case if the app is present in /system/b2g/webapps/webapps.json
|
|
|
|
// and in /data/local/webapps/webapps.json: this happens when updating gaia apps
|
|
|
|
// Confere bug 989876
|
|
|
|
this.webapps[id].updateTime = data[id].updateTime;
|
|
|
|
this.webapps[id].lastUpdateCheck = data[id].updateTime;
|
2012-10-24 00:52:57 +04:00
|
|
|
}
|
2014-01-17 04:26:24 +04:00
|
|
|
}
|
|
|
|
}.bind(this)).then(null, Cu.reportError);
|
2012-10-24 00:52:57 +04:00
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
loadAndUpdateApps: function() {
|
|
|
|
return Task.spawn(function() {
|
|
|
|
let runUpdate = AppsUtils.isFirstRun(Services.prefs);
|
2012-10-24 00:52:57 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
yield this.loadCurrentRegistry();
|
|
|
|
|
2014-09-06 03:41:24 +04:00
|
|
|
try {
|
|
|
|
let systemManifestURL =
|
|
|
|
Services.prefs.getCharPref("b2g.system_manifest_url");
|
|
|
|
let systemAppFound =
|
|
|
|
this.webapps.some(v => v.manifestURL == systemManifestURL);
|
|
|
|
|
|
|
|
// We configured a system app but can't find it. That prevents us
|
|
|
|
// from starting so we clear our registry to start again from scratch.
|
|
|
|
if (!systemAppFound) {
|
|
|
|
runUpdate = true;
|
|
|
|
}
|
|
|
|
} catch(e) {} // getCharPref will throw on non-b2g platforms. That's ok.
|
|
|
|
|
2012-10-24 00:52:57 +04:00
|
|
|
if (runUpdate) {
|
2014-08-26 00:30:07 +04:00
|
|
|
|
|
|
|
// Run migration before uninstall of core apps happens.
|
2014-09-06 03:41:24 +04:00
|
|
|
Services.obs.notifyObservers(null, "webapps-before-update-merge", null);
|
2014-08-26 00:30:07 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
yield this.installSystemApps();
|
|
|
|
#endif
|
|
|
|
|
2012-12-11 00:51:10 +04:00
|
|
|
// At first run, install preloaded apps and set up their permissions.
|
2012-10-10 20:16:49 +04:00
|
|
|
for (let id in this.webapps) {
|
2014-05-26 14:30:11 +04:00
|
|
|
let isPreinstalled = this.installPreinstalledApp(id);
|
2013-12-10 02:05:14 +04:00
|
|
|
this.removeIfHttpsDuplicate(id);
|
2012-12-11 00:51:10 +04:00
|
|
|
if (!this.webapps[id]) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-24 00:52:57 +04:00
|
|
|
this.updateOfflineCacheForApp(id);
|
2014-06-11 12:47:00 +04:00
|
|
|
this.updatePermissionsForApp(id, isPreinstalled,
|
|
|
|
true /* isSystemUpdate */);
|
2012-10-10 20:16:49 +04:00
|
|
|
}
|
2012-12-11 00:51:10 +04:00
|
|
|
// Need to update the persisted list of apps since
|
2013-01-09 19:06:07 +04:00
|
|
|
// installPreinstalledApp() removes the ones failing to install.
|
2012-12-11 00:51:10 +04:00
|
|
|
this._saveApps();
|
2012-08-30 01:20:03 +04:00
|
|
|
}
|
2013-10-02 21:27:23 +04:00
|
|
|
|
|
|
|
// DataStores must be initialized at startup.
|
|
|
|
for (let id in this.webapps) {
|
2014-06-11 02:52:47 +04:00
|
|
|
yield this.updateDataStoreForApp(id);
|
2013-10-02 21:27:23 +04:00
|
|
|
}
|
|
|
|
|
2014-06-11 02:52:47 +04:00
|
|
|
yield this.registerAppsHandlers(runUpdate);
|
2014-01-17 04:26:24 +04:00
|
|
|
}.bind(this)).then(null, Cu.reportError);
|
2011-12-06 03:26:18 +04:00
|
|
|
},
|
|
|
|
|
2014-06-19 18:03:54 +04:00
|
|
|
updateDataStore: function(aId, aOrigin, aManifestURL, aManifest) {
|
|
|
|
let uri = Services.io.newURI(aOrigin, null, null);
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
|
|
|
let principal = secMan.getAppCodebasePrincipal(uri, aId,
|
|
|
|
/*mozbrowser*/ false);
|
|
|
|
if (!dataStoreService.checkPermission(principal)) {
|
2013-12-06 03:12:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-02 21:27:14 +04:00
|
|
|
if ('datastores-owned' in aManifest) {
|
|
|
|
for (let name in aManifest['datastores-owned']) {
|
|
|
|
let readonly = "access" in aManifest['datastores-owned'][name]
|
|
|
|
? aManifest['datastores-owned'][name].access == 'readonly'
|
|
|
|
: false;
|
|
|
|
|
2013-10-02 21:27:23 +04:00
|
|
|
dataStoreService.installDataStore(aId, name, aOrigin, aManifestURL,
|
|
|
|
readonly);
|
2013-10-02 21:27:14 +04:00
|
|
|
}
|
2013-10-02 21:27:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 21:27:14 +04:00
|
|
|
if ('datastores-access' in aManifest) {
|
|
|
|
for (let name in aManifest['datastores-access']) {
|
|
|
|
let readonly = ("readonly" in aManifest['datastores-access'][name]) &&
|
|
|
|
!aManifest['datastores-access'][name].readonly
|
|
|
|
? false : true;
|
2013-10-02 21:27:07 +04:00
|
|
|
|
2013-10-02 21:27:23 +04:00
|
|
|
dataStoreService.installAccessDataStore(aId, name, aOrigin,
|
|
|
|
aManifestURL, readonly);
|
2013-10-02 21:27:14 +04:00
|
|
|
}
|
2013-10-02 21:27:07 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
2012-09-13 22:47:34 +04:00
|
|
|
// use the root of the manifest.
|
2013-08-16 13:48:37 +04:00
|
|
|
//
|
|
|
|
// TODO Bug 908094 Refine _registerSystemMessagesForEntryPoint(...).
|
2012-09-13 22:47:34 +04:00
|
|
|
_registerSystemMessagesForEntryPoint: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.messages || !Array.isArray(root.messages) ||
|
|
|
|
root.messages.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin, aApp.manifestURL);
|
2014-06-03 14:27:12 +04:00
|
|
|
let launchPathURI = Services.io.newURI(manifest.fullLaunchPath(aEntryPoint), null, null);
|
|
|
|
let manifestURI = Services.io.newURI(aApp.manifestURL, null, null);
|
2012-09-13 22:47:34 +04:00
|
|
|
root.messages.forEach(function registerPages(aMessage) {
|
2014-06-03 14:27:12 +04:00
|
|
|
let handlerPageURI = launchPathURI;
|
2012-09-13 22:47:34 +04:00
|
|
|
let messageName;
|
|
|
|
if (typeof(aMessage) === "object" && Object.keys(aMessage).length === 1) {
|
|
|
|
messageName = Object.keys(aMessage)[0];
|
2014-06-03 14:27:12 +04:00
|
|
|
let handlerPath = aMessage[messageName];
|
|
|
|
// Resolve the handler path from origin. If |handler_path| is absent,
|
|
|
|
// simply skip.
|
|
|
|
let fullHandlerPath;
|
2013-02-04 17:59:32 +04:00
|
|
|
try {
|
2014-06-03 14:27:12 +04:00
|
|
|
if (handlerPath && handlerPath.trim()) {
|
2014-07-31 01:00:15 +04:00
|
|
|
fullHandlerPath = manifest.resolveURL(handlerPath);
|
2014-06-03 14:27:12 +04:00
|
|
|
} else {
|
|
|
|
throw new Error("Empty or blank handler path.");
|
|
|
|
}
|
2013-02-04 17:59:32 +04:00
|
|
|
} catch(e) {
|
2014-06-03 14:27:12 +04:00
|
|
|
debug("system message handler path (" + handlerPath + ") is " +
|
|
|
|
"invalid, skipping. Error is: " + e);
|
2013-02-04 17:59:32 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-03 14:27:12 +04:00
|
|
|
handlerPageURI = Services.io.newURI(fullHandlerPath, null, null);
|
2012-09-13 22:47:34 +04:00
|
|
|
} else {
|
|
|
|
messageName = aMessage;
|
|
|
|
}
|
2012-12-05 07:49:26 +04:00
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister(messageName,
|
2014-07-31 01:00:15 +04:00
|
|
|
aApp.manifestURL,
|
2012-12-05 07:49:26 +04:00
|
|
|
aManifest)) {
|
2014-06-03 14:27:12 +04:00
|
|
|
msgmgr.registerPage(messageName, handlerPageURI, manifestURI);
|
2012-12-05 07:49:26 +04:00
|
|
|
}
|
2012-09-13 22:47:34 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-16 13:48:37 +04:00
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
|
|
|
// use the root of the manifest.
|
|
|
|
//
|
|
|
|
// TODO Bug 908094 Refine _registerInterAppConnectionsForEntryPoint(...).
|
|
|
|
_registerInterAppConnectionsForEntryPoint: function(aManifest, aApp,
|
|
|
|
aEntryPoint) {
|
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
let connections = root.connections;
|
|
|
|
if (!connections) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((typeof connections) !== "object") {
|
|
|
|
debug("|connections| is not an object. Skipping: " + connections);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin, aApp.manifestURL);
|
2013-08-16 13:48:37 +04:00
|
|
|
let launchPathURI = Services.io.newURI(manifest.fullLaunchPath(aEntryPoint),
|
|
|
|
null, null);
|
|
|
|
let manifestURI = Services.io.newURI(aApp.manifestURL, null, null);
|
|
|
|
|
|
|
|
for (let keyword in connections) {
|
|
|
|
let connection = connections[keyword];
|
|
|
|
|
|
|
|
// Resolve the handler path from origin. If |handler_path| is absent,
|
|
|
|
// use |launch_path| as default.
|
|
|
|
let fullHandlerPath;
|
|
|
|
let handlerPath = connection.handler_path;
|
|
|
|
if (handlerPath) {
|
|
|
|
try {
|
2014-07-31 01:00:15 +04:00
|
|
|
fullHandlerPath = manifest.resolveURL(handlerPath);
|
2013-08-16 13:48:37 +04:00
|
|
|
} catch(e) {
|
|
|
|
debug("Connection's handler path is invalid. Skipping: keyword: " +
|
|
|
|
keyword + " handler_path: " + handlerPath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let handlerPageURI = fullHandlerPath
|
|
|
|
? Services.io.newURI(fullHandlerPath, null, null)
|
|
|
|
: launchPathURI;
|
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister("connection",
|
2014-07-31 01:00:15 +04:00
|
|
|
aApp.manifestURL,
|
2013-08-16 13:48:37 +04:00
|
|
|
aManifest)) {
|
|
|
|
msgmgr.registerPage("connection", handlerPageURI, manifestURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
interAppCommService.
|
|
|
|
registerConnection(keyword,
|
|
|
|
handlerPageURI,
|
|
|
|
manifestURI,
|
|
|
|
connection.description,
|
|
|
|
connection.rules);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-03 04:16:55 +04:00
|
|
|
_registerSystemMessages: function(aManifest, aApp) {
|
2012-09-13 22:47:34 +04:00
|
|
|
this._registerSystemMessagesForEntryPoint(aManifest, aApp, null);
|
|
|
|
|
|
|
|
if (!aManifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let entryPoint in aManifest.entry_points) {
|
|
|
|
this._registerSystemMessagesForEntryPoint(aManifest, aApp, entryPoint);
|
2012-07-03 04:16:55 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-16 13:48:37 +04:00
|
|
|
_registerInterAppConnections: function(aManifest, aApp) {
|
|
|
|
this._registerInterAppConnectionsForEntryPoint(aManifest, aApp, null);
|
|
|
|
|
|
|
|
if (!aManifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let entryPoint in aManifest.entry_points) {
|
|
|
|
this._registerInterAppConnectionsForEntryPoint(aManifest, aApp,
|
|
|
|
entryPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
2012-09-13 22:47:34 +04:00
|
|
|
// use the root of the manifest.
|
2012-11-03 07:28:32 +04:00
|
|
|
_createActivitiesToRegister: function(aManifest, aApp, aEntryPoint, aRunUpdate) {
|
2012-10-18 11:32:15 +04:00
|
|
|
let activitiesToRegister = [];
|
2012-09-13 22:47:34 +04:00
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
2014-07-31 17:48:00 +04:00
|
|
|
if (!root || !root.activities) {
|
2012-10-18 11:32:15 +04:00
|
|
|
return activitiesToRegister;
|
2012-07-20 19:41:30 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin, aApp.manifestURL);
|
2012-09-13 22:47:34 +04:00
|
|
|
for (let activity in root.activities) {
|
|
|
|
let description = root.activities[activity];
|
2013-02-10 22:54:13 +04:00
|
|
|
let href = description.href;
|
|
|
|
if (!href) {
|
|
|
|
href = manifest.launch_path;
|
2012-08-15 06:40:12 +04:00
|
|
|
}
|
2013-02-04 17:59:32 +04:00
|
|
|
|
|
|
|
try {
|
2014-07-31 01:00:15 +04:00
|
|
|
href = manifest.resolveURL(href);
|
2013-02-04 17:59:32 +04:00
|
|
|
} catch (e) {
|
2013-02-10 22:54:13 +04:00
|
|
|
debug("Activity href (" + href + ") is invalid, skipping. " +
|
2013-02-04 17:59:32 +04:00
|
|
|
"Error is: " + e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:19:50 +04:00
|
|
|
// Make a copy of the description object since we don't want to modify
|
|
|
|
// the manifest itself, but need to register with a resolved URI.
|
|
|
|
let newDesc = {};
|
|
|
|
for (let prop in description) {
|
|
|
|
newDesc[prop] = description[prop];
|
|
|
|
}
|
|
|
|
newDesc.href = href;
|
|
|
|
|
2013-02-04 17:59:32 +04:00
|
|
|
debug('_createActivitiesToRegister: ' + aApp.manifestURL + ', activity ' +
|
2013-02-13 03:19:50 +04:00
|
|
|
activity + ', description.href is ' + newDesc.href);
|
2012-11-03 07:28:32 +04:00
|
|
|
|
|
|
|
if (aRunUpdate) {
|
|
|
|
activitiesToRegister.push({ "manifest": aApp.manifestURL,
|
|
|
|
"name": activity,
|
|
|
|
"icon": manifest.iconURLForSize(128),
|
2013-02-13 03:19:50 +04:00
|
|
|
"description": newDesc });
|
2012-11-03 07:28:32 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 14:27:12 +04:00
|
|
|
let launchPathURI = Services.io.newURI(href, null, null);
|
|
|
|
let manifestURI = Services.io.newURI(aApp.manifestURL, null, null);
|
2012-12-05 07:49:26 +04:00
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister("activity",
|
2014-07-31 01:00:15 +04:00
|
|
|
aApp.manifestURL,
|
2012-12-05 07:49:26 +04:00
|
|
|
aManifest)) {
|
2014-06-03 14:27:12 +04:00
|
|
|
msgmgr.registerPage("activity", launchPathURI, manifestURI);
|
2012-12-05 07:49:26 +04:00
|
|
|
}
|
2012-07-20 19:41:30 +04:00
|
|
|
}
|
2012-10-18 11:32:15 +04:00
|
|
|
return activitiesToRegister;
|
2012-07-20 19:41:30 +04:00
|
|
|
},
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// |aAppsToRegister| contains an array of apps to be registered, where
|
|
|
|
// each element is an object in the format of {manifest: foo, app: bar}.
|
2012-11-03 07:28:32 +04:00
|
|
|
_registerActivitiesForApps: function(aAppsToRegister, aRunUpdate) {
|
2012-10-18 11:32:15 +04:00
|
|
|
// Collect the activities to be registered for root and entry_points.
|
|
|
|
let activitiesToRegister = [];
|
|
|
|
aAppsToRegister.forEach(function (aApp) {
|
|
|
|
let manifest = aApp.manifest;
|
|
|
|
let app = aApp.app;
|
|
|
|
activitiesToRegister.push.apply(activitiesToRegister,
|
2012-11-03 07:28:32 +04:00
|
|
|
this._createActivitiesToRegister(manifest, app, null, aRunUpdate));
|
2012-10-18 11:32:15 +04:00
|
|
|
|
2014-08-12 02:21:44 +04:00
|
|
|
if (aRunUpdate) {
|
|
|
|
cpmm.sendAsyncMessage("Activities:UnregisterAll", app.manifestURL);
|
|
|
|
}
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
if (!manifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-13 22:47:34 +04:00
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
for (let entryPoint in manifest.entry_points) {
|
|
|
|
activitiesToRegister.push.apply(activitiesToRegister,
|
2012-11-03 07:28:32 +04:00
|
|
|
this._createActivitiesToRegister(manifest, app, entryPoint, aRunUpdate));
|
2012-10-18 11:32:15 +04:00
|
|
|
}
|
|
|
|
}, this);
|
2012-09-13 22:47:34 +04:00
|
|
|
|
2012-11-03 07:28:32 +04:00
|
|
|
if (!aRunUpdate || activitiesToRegister.length == 0) {
|
|
|
|
this.notifyAppsRegistryReady();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// Send the array carrying all the activities to be registered.
|
|
|
|
cpmm.sendAsyncMessage("Activities:Register", activitiesToRegister);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Better to directly use |_registerActivitiesForApps()| if we have
|
|
|
|
// multiple apps to be registered for activities.
|
2012-11-03 07:28:32 +04:00
|
|
|
_registerActivities: function(aManifest, aApp, aRunUpdate) {
|
|
|
|
this._registerActivitiesForApps([{ manifest: aManifest, app: aApp }], aRunUpdate);
|
2012-09-13 22:47:34 +04:00
|
|
|
},
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
|
|
|
// use the root of the manifest.
|
|
|
|
_createActivitiesToUnregister: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let activitiesToUnregister = [];
|
2012-09-13 22:47:34 +04:00
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.activities) {
|
2012-10-18 11:32:15 +04:00
|
|
|
return activitiesToUnregister;
|
2012-07-20 19:41:30 +04:00
|
|
|
}
|
|
|
|
|
2012-09-13 22:47:34 +04:00
|
|
|
for (let activity in root.activities) {
|
|
|
|
let description = root.activities[activity];
|
2012-10-18 11:32:15 +04:00
|
|
|
activitiesToUnregister.push({ "manifest": aApp.manifestURL,
|
2013-08-09 02:04:03 +04:00
|
|
|
"name": activity,
|
|
|
|
"description": description });
|
2012-07-20 19:41:30 +04:00
|
|
|
}
|
2012-10-18 11:32:15 +04:00
|
|
|
return activitiesToUnregister;
|
2012-07-20 19:41:30 +04:00
|
|
|
},
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// |aAppsToUnregister| contains an array of apps to be unregistered, where
|
|
|
|
// each element is an object in the format of {manifest: foo, app: bar}.
|
|
|
|
_unregisterActivitiesForApps: function(aAppsToUnregister) {
|
|
|
|
// Collect the activities to be unregistered for root and entry_points.
|
|
|
|
let activitiesToUnregister = [];
|
|
|
|
aAppsToUnregister.forEach(function (aApp) {
|
|
|
|
let manifest = aApp.manifest;
|
|
|
|
let app = aApp.app;
|
|
|
|
activitiesToUnregister.push.apply(activitiesToUnregister,
|
|
|
|
this._createActivitiesToUnregister(manifest, app, null));
|
|
|
|
|
|
|
|
if (!manifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-13 22:47:34 +04:00
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
for (let entryPoint in manifest.entry_points) {
|
|
|
|
activitiesToUnregister.push.apply(activitiesToUnregister,
|
|
|
|
this._createActivitiesToUnregister(manifest, app, entryPoint));
|
|
|
|
}
|
|
|
|
}, this);
|
2012-09-13 22:47:34 +04:00
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// Send the array carrying all the activities to be unregistered.
|
|
|
|
cpmm.sendAsyncMessage("Activities:Unregister", activitiesToUnregister);
|
2012-09-13 22:47:34 +04:00
|
|
|
},
|
|
|
|
|
2012-10-18 11:32:15 +04:00
|
|
|
// Better to directly use |_unregisterActivitiesForApps()| if we have
|
|
|
|
// multiple apps to be unregistered for activities.
|
|
|
|
_unregisterActivities: function(aManifest, aApp) {
|
|
|
|
this._unregisterActivitiesForApps([{ manifest: aManifest, app: aApp }]);
|
2012-10-17 08:30:43 +04:00
|
|
|
},
|
|
|
|
|
2012-11-03 07:28:32 +04:00
|
|
|
_processManifestForIds: function(aIds, aRunUpdate) {
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests(aIds).then((aResults) => {
|
2012-10-18 11:32:15 +04:00
|
|
|
let appsToRegister = [];
|
2014-01-17 04:26:24 +04:00
|
|
|
aResults.forEach((aResult) => {
|
2012-09-12 05:17:01 +04:00
|
|
|
let app = this.webapps[aResult.id];
|
|
|
|
let manifest = aResult.manifest;
|
2013-10-01 22:01:08 +04:00
|
|
|
if (!manifest) {
|
|
|
|
// If we can't load the manifest, we probably have a corrupted
|
|
|
|
// registry. We delete the app since we can't do anything with it.
|
|
|
|
delete this.webapps[aResult.id];
|
|
|
|
return;
|
|
|
|
}
|
2014-06-07 13:28:45 +04:00
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
let localeManifest =
|
|
|
|
new ManifestHelper(manifest, app.origin, app.manifestURL);
|
2014-06-07 13:28:45 +04:00
|
|
|
|
2014-06-23 04:22:07 +04:00
|
|
|
app.name = manifest.name;
|
2012-10-19 14:43:17 +04:00
|
|
|
app.csp = manifest.csp || "";
|
2014-06-07 13:28:45 +04:00
|
|
|
app.role = localeManifest.role;
|
2014-08-19 18:14:08 +04:00
|
|
|
this._saveWidgetsFullPath(localeManifest, app);
|
|
|
|
|
2013-05-14 23:00:09 +04:00
|
|
|
if (app.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED) {
|
|
|
|
app.redirects = this.sanitizeRedirects(manifest.redirects);
|
|
|
|
}
|
2014-08-01 21:41:52 +04:00
|
|
|
if (app.origin.startsWith("app://")) {
|
|
|
|
app.kind = this.kPackaged;
|
|
|
|
} else {
|
|
|
|
// Hosted apps, can be appcached or not.
|
|
|
|
app.kind = aResult.manifest.appcache_path ? this.kHostedAppcache
|
|
|
|
: this.kHosted;
|
|
|
|
}
|
2012-09-12 05:17:01 +04:00
|
|
|
this._registerSystemMessages(manifest, app);
|
2013-08-16 13:48:37 +04:00
|
|
|
this._registerInterAppConnections(manifest, app);
|
2012-10-18 11:32:15 +04:00
|
|
|
appsToRegister.push({ manifest: manifest, app: app });
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2014-07-15 01:27:57 +04:00
|
|
|
this._safeToClone.resolve();
|
2012-11-03 07:28:32 +04:00
|
|
|
this._registerActivitiesForApps(appsToRegister, aRunUpdate);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-07-03 04:16:55 +04:00
|
|
|
},
|
|
|
|
|
2012-03-13 04:33:10 +04:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic == "xpcom-shutdown") {
|
|
|
|
this.messages.forEach((function(msgName) {
|
|
|
|
ppmm.removeMessageListener(msgName, this);
|
|
|
|
}).bind(this));
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
2013-06-13 03:28:11 +04:00
|
|
|
cpmm = null;
|
2012-03-13 04:33:10 +04:00
|
|
|
ppmm = null;
|
2014-01-07 01:03:50 +04:00
|
|
|
} else if (aTopic == "memory-pressure") {
|
|
|
|
// Clear the manifest cache on memory pressure.
|
|
|
|
this._manifestCache = {};
|
2012-03-13 04:33:10 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-13 13:33:52 +04:00
|
|
|
addMessageListener: function(aMsgNames, aApp, aMm) {
|
2012-09-18 21:34:55 +04:00
|
|
|
aMsgNames.forEach(function (aMsgName) {
|
2013-09-13 13:33:52 +04:00
|
|
|
let man = aApp && aApp.manifestURL;
|
2012-09-18 21:34:55 +04:00
|
|
|
if (!(aMsgName in this.children)) {
|
|
|
|
this.children[aMsgName] = [];
|
|
|
|
}
|
2012-10-19 22:17:05 +04:00
|
|
|
|
|
|
|
let mmFound = this.children[aMsgName].some(function(mmRef) {
|
|
|
|
if (mmRef.mm === aMm) {
|
|
|
|
mmRef.refCount++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!mmFound) {
|
|
|
|
this.children[aMsgName].push({
|
|
|
|
mm: aMm,
|
|
|
|
refCount: 1
|
|
|
|
});
|
2013-11-20 03:12:12 +04:00
|
|
|
}
|
2013-09-13 13:33:52 +04:00
|
|
|
|
2013-11-20 03:12:12 +04:00
|
|
|
// If the state reported by the registration is outdated, update it now.
|
|
|
|
if ((aMsgName === 'Webapps:FireEvent') ||
|
|
|
|
(aMsgName === 'Webapps:UpdateState')) {
|
|
|
|
if (man) {
|
|
|
|
let app = this.getAppByManifestURL(aApp.manifestURL);
|
|
|
|
if (app && ((aApp.installState !== app.installState) ||
|
|
|
|
(aApp.downloading !== app.downloading))) {
|
|
|
|
debug("Got a registration from an outdated app: " +
|
|
|
|
aApp.manifestURL);
|
|
|
|
let aEvent ={
|
|
|
|
type: app.installState,
|
|
|
|
app: app,
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
manifest: app.manifest
|
|
|
|
};
|
|
|
|
aMm.sendAsyncMessage(aMsgName, aEvent);
|
2013-09-13 13:33:52 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-19 22:17:05 +04:00
|
|
|
}
|
2012-09-18 21:34:55 +04:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
2012-10-19 22:17:05 +04:00
|
|
|
removeMessageListener: function(aMsgNames, aMm) {
|
|
|
|
if (aMsgNames.length === 1 &&
|
|
|
|
aMsgNames[0] === "Webapps:Internal:AllMessages") {
|
2012-11-09 04:53:31 +04:00
|
|
|
for (let msgName in this.children) {
|
|
|
|
let msg = this.children[msgName];
|
2012-10-19 22:17:05 +04:00
|
|
|
|
|
|
|
for (let mmI = msg.length - 1; mmI >= 0; mmI -= 1) {
|
|
|
|
let mmRef = msg[mmI];
|
|
|
|
if (mmRef.mm === aMm) {
|
|
|
|
msg.splice(mmI, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.length === 0) {
|
2012-11-09 04:53:31 +04:00
|
|
|
delete this.children[msgName];
|
2012-10-19 22:17:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMsgNames.forEach(function(aMsgName) {
|
|
|
|
if (!(aMsgName in this.children)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-02 00:29:59 +04:00
|
|
|
|
2012-10-19 22:17:05 +04:00
|
|
|
let removeIndex;
|
|
|
|
this.children[aMsgName].some(function(mmRef, index) {
|
|
|
|
if (mmRef.mm === aMm) {
|
|
|
|
mmRef.refCount--;
|
|
|
|
if (mmRef.refCount === 0) {
|
|
|
|
removeIndex = index;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2012-09-18 21:34:55 +04:00
|
|
|
|
2012-10-19 22:17:05 +04:00
|
|
|
if (removeIndex) {
|
|
|
|
this.children[aMsgName].splice(removeIndex, 1);
|
2012-09-18 21:34:55 +04:00
|
|
|
}
|
2012-10-19 22:17:05 +04:00
|
|
|
}, this);
|
2012-09-18 21:34:55 +04:00
|
|
|
},
|
|
|
|
|
2011-11-29 00:13:26 +04:00
|
|
|
receiveMessage: function(aMessage) {
|
2012-06-27 00:12:33 +04:00
|
|
|
// nsIPrefBranch throws if pref does not exist, faster to simply write
|
|
|
|
// the pref instead of first checking if it is false.
|
|
|
|
Services.prefs.setBoolPref("dom.mozApps.used", true);
|
|
|
|
|
2012-09-30 07:08:01 +04:00
|
|
|
// We need to check permissions for calls coming from mozApps.mgmt.
|
2014-07-07 22:01:30 +04:00
|
|
|
// These are: getNotInstalled(), applyDownload() and uninstall().
|
|
|
|
if (["Webapps:GetNotInstalled",
|
2013-01-11 16:50:46 +04:00
|
|
|
"Webapps:ApplyDownload",
|
|
|
|
"Webapps:Uninstall"].indexOf(aMessage.name) != -1) {
|
2012-09-30 07:08:01 +04:00
|
|
|
if (!aMessage.target.assertPermission("webapps-manage")) {
|
|
|
|
debug("mozApps message " + aMessage.name +
|
|
|
|
" from a content process with no 'webapps-manage' privileges.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 00:29:59 +04:00
|
|
|
let msg = aMessage.data || {};
|
2012-09-18 21:34:55 +04:00
|
|
|
let mm = aMessage.target;
|
|
|
|
msg.mm = mm;
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2014-07-15 01:27:57 +04:00
|
|
|
let processedImmediately = true;
|
|
|
|
|
|
|
|
// There are two kind of messages: the messages that only make sense once the
|
|
|
|
// registry is ready, and those that can (or have to) be treated as soon as
|
|
|
|
// they're received.
|
2011-11-29 00:13:26 +04:00
|
|
|
switch (aMessage.name) {
|
2014-07-15 01:27:57 +04:00
|
|
|
case "Activities:Register:KO":
|
|
|
|
dump("Activities didn't register correctly!");
|
|
|
|
case "Activities:Register:OK":
|
|
|
|
// Activities:Register:OK is special because it's one way the registryReady
|
|
|
|
// promise can be resolved.
|
|
|
|
// XXX: What to do when the activities registration failed? At this point
|
|
|
|
// just act as if nothing happened.
|
|
|
|
this.notifyAppsRegistryReady();
|
|
|
|
break;
|
|
|
|
case "Webapps:GetList":
|
|
|
|
// GetList is special because it's synchronous. So far so well, it's the
|
|
|
|
// only synchronous message, if we get more at some point they should get
|
|
|
|
// this treatment also.
|
|
|
|
return this.doGetList();
|
|
|
|
case "child-process-shutdown":
|
|
|
|
this.removeMessageListener(["Webapps:Internal:AllMessages"], mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:RegisterForMessages":
|
|
|
|
this.addMessageListener(msg.messages, msg.app, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:UnregisterForMessages":
|
|
|
|
this.removeMessageListener(msg, mm);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
processedImmediately = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (processedImmediately) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all the rest (asynchronous), we wait till the registry is ready
|
|
|
|
// before processing the message.
|
|
|
|
this.registryReady.then( () => {
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:Install": {
|
2014-06-10 21:37:38 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-07-15 01:27:57 +04:00
|
|
|
Services.obs.notifyObservers(mm, "webapps-runtime-install", JSON.stringify(msg));
|
2014-01-09 22:18:55 +04:00
|
|
|
#else
|
2014-07-15 01:27:57 +04:00
|
|
|
this.doInstall(msg, mm);
|
2014-01-09 22:18:55 +04:00
|
|
|
#endif
|
2014-07-15 01:27:57 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Webapps:GetSelf":
|
|
|
|
this.getSelf(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:Uninstall":
|
2014-06-11 02:52:47 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-07-15 01:27:57 +04:00
|
|
|
Services.obs.notifyObservers(mm, "webapps-runtime-uninstall", JSON.stringify(msg));
|
2014-06-11 02:52:47 +04:00
|
|
|
#else
|
2014-07-15 01:27:57 +04:00
|
|
|
this.doUninstall(msg, mm);
|
2014-06-11 02:52:47 +04:00
|
|
|
#endif
|
2014-07-15 01:27:57 +04:00
|
|
|
break;
|
|
|
|
case "Webapps:Launch":
|
|
|
|
this.doLaunch(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:CheckInstalled":
|
|
|
|
this.checkInstalled(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:GetInstalled":
|
|
|
|
this.getInstalled(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:GetNotInstalled":
|
|
|
|
this.getNotInstalled(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:InstallPackage": {
|
2014-06-10 21:37:38 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-07-15 01:27:57 +04:00
|
|
|
Services.obs.notifyObservers(mm, "webapps-runtime-install-package", JSON.stringify(msg));
|
2014-01-09 22:18:55 +04:00
|
|
|
#else
|
2014-07-15 01:27:57 +04:00
|
|
|
this.doInstallPackage(msg, mm);
|
2014-01-09 22:18:55 +04:00
|
|
|
#endif
|
2014-07-15 01:27:57 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Webapps:Download":
|
|
|
|
this.startDownload(msg.manifestURL);
|
|
|
|
break;
|
|
|
|
case "Webapps:CancelDownload":
|
|
|
|
this.cancelDownload(msg.manifestURL);
|
|
|
|
break;
|
|
|
|
case "Webapps:CheckForUpdate":
|
|
|
|
this.checkForUpdate(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:ApplyDownload":
|
|
|
|
this.applyDownload(msg.manifestURL);
|
|
|
|
break;
|
|
|
|
case "Webapps:Install:Return:Ack":
|
|
|
|
this.onInstallSuccessAck(msg.manifestURL);
|
|
|
|
break;
|
|
|
|
case "Webapps:AddReceipt":
|
|
|
|
this.addReceipt(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:RemoveReceipt":
|
|
|
|
this.removeReceipt(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:ReplaceReceipt":
|
|
|
|
this.replaceReceipt(msg, mm);
|
|
|
|
break;
|
2014-01-09 22:18:55 +04:00
|
|
|
}
|
2014-07-15 01:27:57 +04:00
|
|
|
});
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
|
|
|
|
2013-02-01 01:35:13 +04:00
|
|
|
getAppInfo: function getAppInfo(aAppId) {
|
2013-03-15 18:18:58 +04:00
|
|
|
return AppsUtils.getAppInfo(this.webapps, aAppId);
|
2013-02-01 01:35:13 +04:00
|
|
|
},
|
|
|
|
|
2012-09-18 21:34:55 +04:00
|
|
|
// Some messages can be listened by several content processes:
|
|
|
|
// Webapps:AddApp
|
|
|
|
// Webapps:RemoveApp
|
|
|
|
// Webapps:Install:Return:OK
|
|
|
|
// Webapps:Uninstall:Return:OK
|
2013-01-11 16:50:46 +04:00
|
|
|
// Webapps:Uninstall:Broadcast:Return:OK
|
2013-10-17 16:47:58 +04:00
|
|
|
// Webapps:FireEvent
|
2013-01-09 05:00:01 +04:00
|
|
|
// Webapps:checkForUpdate:Return:OK
|
2013-10-17 16:47:58 +04:00
|
|
|
// Webapps:UpdateState
|
2012-09-18 21:34:55 +04:00
|
|
|
broadcastMessage: function broadcastMessage(aMsgName, aContent) {
|
|
|
|
if (!(aMsgName in this.children)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-19 22:17:05 +04:00
|
|
|
this.children[aMsgName].forEach(function(mmRef) {
|
|
|
|
mmRef.mm.sendAsyncMessage(aMsgName, aContent);
|
|
|
|
});
|
2012-09-18 21:34:55 +04:00
|
|
|
},
|
|
|
|
|
2014-03-16 01:37:37 +04:00
|
|
|
registerUpdateHandler: function(aHandler) {
|
|
|
|
this._updateHandlers.push(aHandler);
|
|
|
|
},
|
|
|
|
|
|
|
|
unregisterUpdateHandler: function(aHandler) {
|
|
|
|
let index = this._updateHandlers.indexOf(aHandler);
|
|
|
|
if (index != -1) {
|
|
|
|
this._updateHandlers.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
notifyUpdateHandlers: function(aApp, aManifest, aZipPath) {
|
|
|
|
for (let updateHandler of this._updateHandlers) {
|
|
|
|
updateHandler(aApp, aManifest, aZipPath);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-30 01:20:03 +04:00
|
|
|
_getAppDir: function(aId) {
|
2014-02-21 23:54:03 +04:00
|
|
|
return FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
|
2012-08-30 01:20:03 +04:00
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
_writeFile: function(aPath, aData) {
|
|
|
|
debug("Saving " + aPath);
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2014-03-25 17:14:06 +04:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
file.initWithPath(aPath);
|
|
|
|
|
|
|
|
// Initialize the file output stream
|
|
|
|
let ostream = FileUtils.openSafeFileOutputStream(file);
|
|
|
|
|
|
|
|
// Obtain a converter to convert our data to a UTF-8 encoded input stream.
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
|
|
|
|
// Asynchronously copy the data to the file.
|
|
|
|
let istream = converter.convertToInputStream(aData);
|
|
|
|
NetUtil.asyncCopy(istream, ostream, function(aResult) {
|
|
|
|
if (!Components.isSuccessCode(aResult)) {
|
|
|
|
deferred.reject()
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
2011-12-08 17:32:54 +04:00
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
/**
|
|
|
|
* Returns the full list of apps and manifests.
|
|
|
|
*/
|
|
|
|
doGetList: function() {
|
|
|
|
let tmp = [];
|
|
|
|
|
|
|
|
let res = {};
|
|
|
|
let done = false;
|
|
|
|
|
2014-07-15 01:27:57 +04:00
|
|
|
// We allow cloning the registry when the local processing has been done.
|
|
|
|
this.safeToClone.then( () => {
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
tmp.push({ id: id });
|
2014-05-24 23:28:13 +04:00
|
|
|
}
|
2014-07-15 01:27:57 +04:00
|
|
|
this._readManifests(tmp).then(
|
|
|
|
function(manifests) {
|
|
|
|
manifests.forEach((item) => {
|
|
|
|
res[item.id] = item.manifest;
|
|
|
|
});
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2014-05-24 23:28:13 +04:00
|
|
|
|
|
|
|
let thread = Services.tm.currentThread;
|
|
|
|
while (!done) {
|
|
|
|
thread.processNextEvent(/* mayWait */ true);
|
|
|
|
}
|
|
|
|
return { webapps: this.webapps, manifests: res };
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
doLaunch: function (aData, aMm) {
|
|
|
|
this.launch(
|
|
|
|
aData.manifestURL,
|
|
|
|
aData.startPoint,
|
2013-09-06 22:18:21 +04:00
|
|
|
aData.timestamp,
|
2013-05-06 17:51:53 +04:00
|
|
|
function onsuccess() {
|
|
|
|
aMm.sendAsyncMessage("Webapps:Launch:Return:OK", aData);
|
|
|
|
},
|
|
|
|
function onfailure(reason) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:Launch:Return:KO", aData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-09-06 22:18:21 +04:00
|
|
|
launch: function launch(aManifestURL, aStartPoint, aTimeStamp, aOnSuccess, aOnFailure) {
|
2013-05-06 17:51:53 +04:00
|
|
|
let app = this.getAppByManifestURL(aManifestURL);
|
2012-09-27 05:01:20 +04:00
|
|
|
if (!app) {
|
2013-05-06 17:51:53 +04:00
|
|
|
aOnFailure("NO_SUCH_APP");
|
2012-09-27 05:01:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire an error when trying to launch an app that is not
|
|
|
|
// yet fully installed.
|
|
|
|
if (app.installState == "pending") {
|
2013-05-06 17:51:53 +04:00
|
|
|
aOnFailure("PENDING_APP_NOT_LAUNCHABLE");
|
2012-09-27 05:01:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
// We have to clone the app object as nsIDOMApplication objects are
|
|
|
|
// stringified as an empty object. (see bug 830376)
|
|
|
|
let appClone = AppsUtils.cloneAppObject(app);
|
|
|
|
appClone.startPoint = aStartPoint;
|
2013-09-06 22:18:21 +04:00
|
|
|
appClone.timestamp = aTimeStamp;
|
2013-05-06 17:51:53 +04:00
|
|
|
Services.obs.notifyObservers(null, "webapps-launch", JSON.stringify(appClone));
|
|
|
|
aOnSuccess();
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function close(aApp) {
|
|
|
|
debug("close");
|
|
|
|
|
|
|
|
// We have to clone the app object as nsIDOMApplication objects are
|
|
|
|
// stringified as an empty object. (see bug 830376)
|
|
|
|
let appClone = AppsUtils.cloneAppObject(aApp);
|
|
|
|
Services.obs.notifyObservers(null, "webapps-close", JSON.stringify(appClone));
|
2012-09-27 05:01:20 +04:00
|
|
|
},
|
|
|
|
|
2012-12-21 22:28:55 +04:00
|
|
|
cancelDownload: function cancelDownload(aManifestURL, aError) {
|
2012-12-19 01:43:13 +04:00
|
|
|
debug("cancelDownload " + aManifestURL);
|
2012-12-21 22:28:55 +04:00
|
|
|
let error = aError || "DOWNLOAD_CANCELED";
|
|
|
|
let download = AppDownloadManager.get(aManifestURL);
|
2012-12-19 01:43:13 +04:00
|
|
|
if (!download) {
|
|
|
|
debug("Could not find a download for " + aManifestURL);
|
2012-09-27 23:34:41 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 04:55:50 +04:00
|
|
|
let app = this.webapps[download.appId];
|
|
|
|
|
2012-12-19 01:43:13 +04:00
|
|
|
if (download.cacheUpdate) {
|
|
|
|
try {
|
|
|
|
download.cacheUpdate.cancel();
|
2013-01-25 06:24:17 +04:00
|
|
|
} catch (e) {
|
|
|
|
debug (e);
|
|
|
|
}
|
2012-12-19 01:43:13 +04:00
|
|
|
} else if (download.channel) {
|
2013-01-17 04:55:50 +04:00
|
|
|
try {
|
|
|
|
download.channel.cancel(Cr.NS_BINDING_ABORTED);
|
2013-10-17 16:47:58 +04:00
|
|
|
} catch(e) { }
|
2012-12-19 01:43:13 +04:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:32:15 +04:00
|
|
|
// Ensure we don't send additional errors for this download
|
|
|
|
app.isCanceling = true;
|
|
|
|
|
|
|
|
// Ensure this app can be downloaded again after canceling
|
|
|
|
app.downloading = false;
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._saveApps().then(() => {
|
2013-10-17 16:47:58 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: {
|
|
|
|
progress: 0,
|
|
|
|
installState: download.previousState,
|
|
|
|
downloading: false
|
|
|
|
},
|
|
|
|
error: error,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
})
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-12-21 22:28:55 +04:00
|
|
|
AppDownloadManager.remove(aManifestURL);
|
2012-09-27 05:01:20 +04:00
|
|
|
},
|
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
startDownload: Task.async(function*(aManifestURL) {
|
2012-10-30 04:17:47 +04:00
|
|
|
debug("startDownload for " + aManifestURL);
|
2014-04-25 19:49:00 +04:00
|
|
|
|
2012-10-30 04:17:47 +04:00
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
|
|
|
let app = this.webapps[id];
|
2014-06-21 03:32:15 +04:00
|
|
|
|
2012-09-29 21:57:18 +04:00
|
|
|
if (!app) {
|
2012-10-30 04:17:47 +04:00
|
|
|
debug("startDownload: No app found for " + aManifestURL);
|
2014-06-06 23:10:59 +04:00
|
|
|
throw new Error("NO_SUCH_APP");
|
2012-09-29 21:57:18 +04:00
|
|
|
}
|
|
|
|
|
2013-02-08 13:40:52 +04:00
|
|
|
if (app.downloading) {
|
|
|
|
debug("app is already downloading. Ignoring.");
|
2014-06-06 23:10:59 +04:00
|
|
|
throw new Error("APP_IS_DOWNLOADING");
|
2013-02-08 13:40:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-01 23:17:32 +04:00
|
|
|
// If the caller is trying to start a download but we have nothing to
|
|
|
|
// download, send an error.
|
|
|
|
if (!app.downloadAvailable) {
|
2013-10-17 16:47:58 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
error: "NO_DOWNLOAD_AVAILABLE",
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2014-06-06 23:10:59 +04:00
|
|
|
throw new Error("NO_DOWNLOAD_AVAILABLE");
|
2013-02-01 23:17:32 +04:00
|
|
|
}
|
|
|
|
|
2012-11-29 18:19:37 +04:00
|
|
|
// First of all, we check if the download is supposed to update an
|
|
|
|
// already installed application.
|
|
|
|
let isUpdate = (app.installState == "installed");
|
|
|
|
|
2012-11-30 20:57:45 +04:00
|
|
|
// An app download would only be triggered for two reasons: an app
|
|
|
|
// update or while retrying to download a previously failed or canceled
|
|
|
|
// instalation.
|
|
|
|
app.retryingDownload = !isUpdate;
|
|
|
|
|
2012-09-29 21:57:18 +04:00
|
|
|
// We need to get the update manifest here, not the webapp manifest.
|
2013-01-25 06:24:17 +04:00
|
|
|
// If this is an update, the update manifest is staged.
|
2014-02-21 23:54:03 +04:00
|
|
|
let file = FileUtils.getFile(DIRECTORY_NAME,
|
|
|
|
["webapps", id,
|
|
|
|
isUpdate ? "staged-update.webapp"
|
|
|
|
: "update.webapp"],
|
|
|
|
true);
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2012-11-27 08:03:43 +04:00
|
|
|
if (!file.exists()) {
|
|
|
|
// This is a hosted app, let's check if it has an appcache
|
|
|
|
// and download it.
|
2014-04-25 19:49:00 +04:00
|
|
|
let results = yield this._readManifests([{ id: id }]);
|
2012-11-27 08:03:43 +04:00
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
let jsonManifest = results[0].manifest;
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2014-04-25 19:49:00 +04:00
|
|
|
|
|
|
|
if (manifest.appcache_path) {
|
|
|
|
debug("appcache found");
|
|
|
|
this.startOfflineCacheDownload(manifest, app, null, isUpdate);
|
|
|
|
} else {
|
|
|
|
// Hosted app with no appcache, nothing to do, but we fire a
|
|
|
|
// downloaded event.
|
|
|
|
debug("No appcache found, sending 'downloaded' for " + aManifestURL);
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
|
|
|
|
yield this._saveApps();
|
|
|
|
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
|
|
|
manifest: jsonManifest,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2014-04-25 19:49:00 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloadsuccess",
|
|
|
|
manifestURL: aManifestURL
|
|
|
|
});
|
|
|
|
}
|
2012-11-27 08:03:43 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
let json = yield AppsUtils.loadJSONAsync(file.path);
|
|
|
|
if (!json) {
|
|
|
|
debug("startDownload: No update manifest found at " + file.path + " " +
|
|
|
|
aManifestURL);
|
2014-06-06 23:10:59 +04:00
|
|
|
throw new Error("MISSING_UPDATE_MANIFEST");
|
2014-04-25 19:49:00 +04:00
|
|
|
}
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest = new ManifestHelper(json, app.origin, app.manifestURL);
|
2014-07-21 13:26:58 +04:00
|
|
|
let newApp = {
|
|
|
|
manifestURL: aManifestURL,
|
|
|
|
origin: app.origin,
|
|
|
|
installOrigin: app.installOrigin,
|
|
|
|
downloadSize: app.downloadSize
|
|
|
|
};
|
|
|
|
|
|
|
|
let newManifest, newId;
|
|
|
|
|
|
|
|
try {
|
|
|
|
[newId, newManifest] = yield this.downloadPackage(id, app, manifest, newApp, isUpdate);
|
|
|
|
} catch (ex) {
|
|
|
|
this.revertDownloadPackage(id, app, newApp, isUpdate, ex);
|
|
|
|
throw ex;
|
|
|
|
}
|
2014-04-25 19:49:00 +04:00
|
|
|
|
|
|
|
// Success! Keep the zip in of TmpD, we'll move it out when
|
|
|
|
// applyDownload() will be called.
|
|
|
|
// Save the manifest in TmpD also
|
2014-07-21 13:26:58 +04:00
|
|
|
let manFile = OS.Path.join(OS.Constants.Path.tmpDir, "webapps", newId,
|
2014-04-25 19:49:00 +04:00
|
|
|
"manifest.webapp");
|
2014-07-21 13:26:58 +04:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(newManifest));
|
|
|
|
|
|
|
|
app = this.webapps[id];
|
2014-04-25 19:49:00 +04:00
|
|
|
|
|
|
|
// Set state and fire events.
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
app.readyToApplyDownload = true;
|
|
|
|
app.updateTime = Date.now();
|
|
|
|
|
|
|
|
yield this._saveApps();
|
|
|
|
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2014-04-25 19:49:00 +04:00
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloadsuccess",
|
|
|
|
manifestURL: aManifestURL
|
|
|
|
});
|
|
|
|
if (app.installState == "pending") {
|
|
|
|
// We restarted a failed download, apply it automatically.
|
|
|
|
this.applyDownload(aManifestURL);
|
|
|
|
}
|
|
|
|
}),
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
applyDownload: Task.async(function*(aManifestURL) {
|
2012-10-30 04:17:47 +04:00
|
|
|
debug("applyDownload for " + aManifestURL);
|
2012-11-30 20:57:45 +04:00
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
|
|
|
let app = this.webapps[id];
|
2014-06-06 23:10:59 +04:00
|
|
|
if (!app) {
|
|
|
|
throw new Error("NO_SUCH_APP");
|
|
|
|
}
|
|
|
|
if (!app.readyToApplyDownload) {
|
|
|
|
throw new Error("NOT_READY_TO_APPLY_DOWNLOAD");
|
2012-09-29 21:57:18 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 23:22:31 +04:00
|
|
|
// We need to get the old manifest to unregister web activities.
|
2014-06-06 23:10:59 +04:00
|
|
|
let oldManifest = yield this.getManifestFor(aManifestURL);
|
|
|
|
// Move the application.zip and manifest.webapp files out of TmpD
|
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", id], true, true);
|
|
|
|
let manFile = tmpDir.clone();
|
|
|
|
manFile.append("manifest.webapp");
|
|
|
|
let appFile = tmpDir.clone();
|
|
|
|
appFile.append("application.zip");
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
|
|
|
appFile.moveTo(dir, "application.zip");
|
|
|
|
manFile.moveTo(dir, "manifest.webapp");
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// Move the staged update manifest to a non staged one.
|
|
|
|
let staged = dir.clone();
|
|
|
|
staged.append("staged-update.webapp");
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// If we are applying after a restarted download, we have no
|
|
|
|
// staged update manifest.
|
|
|
|
if (staged.exists()) {
|
|
|
|
staged.moveTo(dir, "update.webapp");
|
|
|
|
}
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) { }
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// Flush the zip reader cache to make sure we use the new application.zip
|
|
|
|
// when re-launching the application.
|
|
|
|
let zipFile = dir.clone();
|
|
|
|
zipFile.append("application.zip");
|
|
|
|
Services.obs.notifyObservers(zipFile, "flush-cache-entry", null);
|
2012-11-30 20:57:45 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// Get the manifest, and set properties.
|
|
|
|
let newManifest = yield this.getManifestFor(aManifestURL);
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
app.downloadSize = 0;
|
|
|
|
app.installState = "installed";
|
|
|
|
app.readyToApplyDownload = false;
|
2013-02-28 23:22:31 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// Update the staged properties.
|
|
|
|
if (app.staged) {
|
|
|
|
for (let prop in app.staged) {
|
|
|
|
app[prop] = app.staged[prop];
|
|
|
|
}
|
|
|
|
delete app.staged;
|
|
|
|
}
|
2013-02-28 23:22:31 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
delete app.retryingDownload;
|
2013-02-28 23:22:31 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
// Update the asm.js scripts we need to compile.
|
|
|
|
yield ScriptPreloader.preload(app, newManifest);
|
|
|
|
yield this._saveApps();
|
|
|
|
// Update the handlers and permissions for this app.
|
|
|
|
this.updateAppHandlers(oldManifest, newManifest, app);
|
2014-03-16 01:37:37 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
let updateManifest = yield AppsUtils.loadJSONAsync(staged.path);
|
|
|
|
let appObject = AppsUtils.cloneAppObject(app);
|
|
|
|
appObject.updateManifest = updateManifest;
|
|
|
|
this.notifyUpdateHandlers(appObject, newManifest, appFile.path);
|
2014-03-16 01:37:37 +04:00
|
|
|
|
2014-06-06 23:10:59 +04:00
|
|
|
if (supportUseCurrentProfile()) {
|
|
|
|
PermissionsInstaller.installPermissions(
|
|
|
|
{ manifest: newManifest,
|
|
|
|
origin: app.origin,
|
|
|
|
manifestURL: app.manifestURL },
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
this.updateDataStore(this.webapps[id].localId, app.origin,
|
2014-06-19 18:03:54 +04:00
|
|
|
app.manifestURL, newManifest);
|
2014-06-06 23:10:59 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
|
|
|
manifest: newManifest,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2014-06-06 23:10:59 +04:00
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloadapplied",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
|
|
|
}),
|
2012-09-29 21:57:18 +04:00
|
|
|
|
2013-08-29 18:00:33 +04:00
|
|
|
startOfflineCacheDownload: function(aManifest, aApp, aProfileDir, aIsUpdate) {
|
2014-08-01 21:41:52 +04:00
|
|
|
if (aApp.kind !== this.kHostedAppcache) {
|
2012-12-19 01:43:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the manifest has an appcache_path property, use it to populate the
|
|
|
|
// appcache.
|
|
|
|
let appcacheURI = Services.io.newURI(aManifest.fullAppcachePath(),
|
|
|
|
null, null);
|
|
|
|
let docURI = Services.io.newURI(aManifest.fullLaunchPath(), null, null);
|
|
|
|
|
|
|
|
// We determine the app's 'installState' according to its previous
|
|
|
|
// state. Cancelled downloads should remain as 'pending'. Successfully
|
|
|
|
// installed apps should morph to 'updating'.
|
|
|
|
if (aIsUpdate) {
|
|
|
|
aApp.installState = "updating";
|
|
|
|
}
|
|
|
|
|
2013-02-19 12:37:49 +04:00
|
|
|
// We set the 'downloading' flag and update the apps registry right before
|
|
|
|
// starting the app download/update.
|
2012-12-19 01:43:13 +04:00
|
|
|
aApp.downloading = true;
|
2013-02-11 12:38:51 +04:00
|
|
|
aApp.progress = 0;
|
2014-01-17 04:26:24 +04:00
|
|
|
DOMApplicationRegistry._saveApps().then(() => {
|
2013-10-17 16:47:58 +04:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:UpdateState", {
|
2014-06-26 00:47:00 +04:00
|
|
|
// Clear any previous errors.
|
|
|
|
error: null,
|
2013-10-17 16:47:58 +04:00
|
|
|
app: {
|
|
|
|
downloading: true,
|
|
|
|
installState: aApp.installState,
|
|
|
|
progress: 0
|
|
|
|
},
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aApp.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
2014-02-04 04:52:57 +04:00
|
|
|
let cacheUpdate = updateSvc.scheduleAppUpdate(
|
|
|
|
appcacheURI, docURI, aApp.localId, false, aProfileDir);
|
2013-02-19 12:37:49 +04:00
|
|
|
|
|
|
|
// We save the download details for potential further usage like
|
|
|
|
// cancelling it.
|
|
|
|
let download = {
|
|
|
|
cacheUpdate: cacheUpdate,
|
|
|
|
appId: this._appIdForManifestURL(aApp.manifestURL),
|
|
|
|
previousState: aIsUpdate ? "installed" : "pending"
|
|
|
|
};
|
|
|
|
AppDownloadManager.add(aApp.manifestURL, download);
|
|
|
|
|
|
|
|
cacheUpdate.addObserver(new AppcacheObserver(aApp), false);
|
2013-01-18 21:54:06 +04:00
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2013-01-18 21:54:06 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the MD5 hash of the manifest.
|
|
|
|
computeManifestHash: function(aManifest) {
|
2013-08-02 04:00:22 +04:00
|
|
|
return AppsUtils.computeHash(JSON.stringify(aManifest));
|
2013-01-18 21:54:06 +04:00
|
|
|
},
|
|
|
|
|
2013-05-14 23:00:09 +04:00
|
|
|
// Updates the redirect mapping, activities and system message handlers.
|
2013-02-28 23:22:31 +04:00
|
|
|
// aOldManifest can be null if we don't have any handler to unregister.
|
|
|
|
updateAppHandlers: function(aOldManifest, aNewManifest, aApp) {
|
|
|
|
debug("updateAppHandlers: old=" + aOldManifest + " new=" + aNewManifest);
|
|
|
|
this.notifyAppsRegistryStart();
|
2013-05-14 23:00:09 +04:00
|
|
|
if (aApp.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED) {
|
|
|
|
aApp.redirects = this.sanitizeRedirects(aNewManifest.redirects);
|
|
|
|
}
|
2013-04-30 17:01:46 +04:00
|
|
|
|
|
|
|
if (supportSystemMessages()) {
|
|
|
|
if (aOldManifest) {
|
|
|
|
this._unregisterActivities(aOldManifest, aApp);
|
|
|
|
}
|
|
|
|
this._registerSystemMessages(aNewManifest, aApp);
|
|
|
|
this._registerActivities(aNewManifest, aApp, true);
|
2013-08-16 13:48:37 +04:00
|
|
|
this._registerInterAppConnections(aNewManifest, aApp);
|
2013-04-30 17:01:46 +04:00
|
|
|
} else {
|
|
|
|
// Nothing else to do but notifying we're ready.
|
|
|
|
this.notifyAppsRegistryReady();
|
2013-02-28 23:22:31 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-27 05:01:20 +04:00
|
|
|
checkForUpdate: function(aData, aMm) {
|
2012-10-30 04:17:47 +04:00
|
|
|
debug("checkForUpdate for " + aData.manifestURL);
|
2013-02-08 13:40:52 +04:00
|
|
|
|
|
|
|
function sendError(aError) {
|
2014-05-24 23:28:13 +04:00
|
|
|
debug("checkForUpdate error " + aError);
|
2013-02-08 13:40:52 +04:00
|
|
|
aData.error = aError;
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
}
|
|
|
|
|
2012-10-30 04:17:47 +04:00
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
2012-10-18 14:32:03 +04:00
|
|
|
|
2013-02-19 12:37:49 +04:00
|
|
|
// We cannot update an app that does not exists.
|
|
|
|
if (!app) {
|
|
|
|
sendError("NO_SUCH_APP");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot update an app that is not fully installed.
|
|
|
|
if (app.installState !== "installed") {
|
|
|
|
sendError("PENDING_APP_NOT_UPDATABLE");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-27 05:01:20 +04:00
|
|
|
|
2013-02-19 12:37:49 +04:00
|
|
|
// We may be able to remove this when Bug 839071 is fixed.
|
|
|
|
if (app.downloading) {
|
|
|
|
sendError("APP_IS_DOWNLOADING");
|
|
|
|
return;
|
2012-09-27 05:01:20 +04:00
|
|
|
}
|
|
|
|
|
2014-02-01 02:50:37 +04:00
|
|
|
// If the app is packaged and its manifestURL has an app:// scheme,
|
|
|
|
// then we can't have an update.
|
2014-08-01 21:41:52 +04:00
|
|
|
if (app.kind == this.kPackaged && app.manifestURL.startsWith("app://")) {
|
2014-05-24 23:28:13 +04:00
|
|
|
sendError("NOT_UPDATABLE");
|
2014-02-01 02:50:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-10 21:06:59 +04:00
|
|
|
// For non-removable hosted apps that lives in the core apps dir we
|
|
|
|
// only check the appcache because we can't modify the manifest even
|
|
|
|
// if it has changed.
|
2013-01-09 19:06:07 +04:00
|
|
|
let onlyCheckAppCache = false;
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-10-17 16:47:58 +04:00
|
|
|
let appDir = FileUtils.getDir("coreAppsDir", ["webapps"], false);
|
|
|
|
onlyCheckAppCache = (app.basePath == appDir.path);
|
2013-01-09 19:06:07 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (onlyCheckAppCache) {
|
2014-08-01 21:41:52 +04:00
|
|
|
// Bail out for packaged apps & hosted apps without appcache.
|
|
|
|
if (app.kind !== this.kHostedAppcache) {
|
2014-05-24 23:28:13 +04:00
|
|
|
sendError("NOT_UPDATABLE");
|
2012-12-17 23:11:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:41:52 +04:00
|
|
|
// We need the manifest to get the appcache path.
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests([{ id: id }]).then((aResult) => {
|
2012-12-17 23:11:05 +04:00
|
|
|
debug("Checking only appcache for " + aData.manifestURL);
|
2014-08-01 21:41:52 +04:00
|
|
|
let manifest = aResult[0].manifest;
|
2012-12-17 23:11:05 +04:00
|
|
|
// Check if the appcache is updatable, and send "downloadavailable" or
|
|
|
|
// "downloadapplied".
|
|
|
|
let updateObserver = {
|
|
|
|
observe: function(aSubject, aTopic, aObsData) {
|
2013-01-14 21:47:55 +04:00
|
|
|
debug("onlyCheckAppCache updateSvc.checkForUpdate return for " +
|
|
|
|
app.manifestURL + " - event is " + aTopic);
|
|
|
|
if (aTopic == "offline-cache-update-available") {
|
2013-01-10 21:06:59 +04:00
|
|
|
app.downloadAvailable = true;
|
2014-01-17 04:26:24 +04:00
|
|
|
this._saveApps().then(() => {
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 16:47:58 +04:00
|
|
|
app: app,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
2014-01-17 04:26:24 +04:00
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 16:47:58 +04:00
|
|
|
eventType: "downloadavailable",
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
2013-02-19 12:37:49 +04:00
|
|
|
});
|
2012-12-17 23:11:05 +04:00
|
|
|
} else {
|
2014-05-24 23:28:13 +04:00
|
|
|
sendError("NOT_UPDATABLE");
|
2012-12-17 23:11:05 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 16:47:58 +04:00
|
|
|
};
|
2014-07-31 01:00:15 +04:00
|
|
|
let helper =
|
|
|
|
new ManifestHelper(manifest, aData.origin, aData.manifestURL);
|
2013-01-14 21:47:55 +04:00
|
|
|
debug("onlyCheckAppCache - launch updateSvc.checkForUpdate for " +
|
|
|
|
helper.fullAppcachePath());
|
|
|
|
updateSvc.checkForUpdate(Services.io.newURI(helper.fullAppcachePath(), null, null),
|
2012-12-17 23:11:05 +04:00
|
|
|
app.localId, false, updateObserver);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-14 21:23:58 +04:00
|
|
|
// On xhr load request event
|
|
|
|
function onload(xhr, oldManifest) {
|
2013-01-09 19:06:07 +04:00
|
|
|
debug("Got http status=" + xhr.status + " for " + aData.manifestURL);
|
2013-01-18 21:54:06 +04:00
|
|
|
let oldHash = app.manifestHash;
|
2014-08-01 21:41:52 +04:00
|
|
|
let isPackage = app.kind == DOMApplicationRegistry.kPackaged;
|
2013-01-18 21:54:06 +04:00
|
|
|
|
2012-09-27 05:01:20 +04:00
|
|
|
if (xhr.status == 200) {
|
2012-12-03 23:38:09 +04:00
|
|
|
let manifest = xhr.response;
|
|
|
|
if (manifest == null) {
|
2012-09-27 05:01:20 +04:00
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-18 21:54:06 +04:00
|
|
|
|
2013-01-11 18:08:12 +04:00
|
|
|
if (!AppsUtils.checkManifest(manifest, app)) {
|
2012-09-27 05:01:20 +04:00
|
|
|
sendError("INVALID_MANIFEST");
|
2013-01-10 21:06:59 +04:00
|
|
|
return;
|
2012-11-19 02:43:38 +04:00
|
|
|
} else if (!AppsUtils.checkInstallAllowed(manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
2013-01-10 21:06:59 +04:00
|
|
|
return;
|
2012-09-27 05:01:20 +04:00
|
|
|
} else {
|
2013-02-14 21:23:58 +04:00
|
|
|
AppsUtils.ensureSameAppName(oldManifest, manifest, app);
|
|
|
|
|
2013-01-18 21:54:06 +04:00
|
|
|
let hash = this.computeManifestHash(manifest);
|
|
|
|
debug("Manifest hash = " + hash);
|
2013-01-25 06:24:17 +04:00
|
|
|
if (isPackage) {
|
|
|
|
if (!app.staged) {
|
|
|
|
app.staged = { };
|
|
|
|
}
|
|
|
|
app.staged.manifestHash = hash;
|
|
|
|
app.staged.etag = xhr.getResponseHeader("Etag");
|
|
|
|
} else {
|
|
|
|
app.manifestHash = hash;
|
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
|
|
|
}
|
2013-01-18 21:54:06 +04:00
|
|
|
|
2012-09-27 05:01:20 +04:00
|
|
|
app.lastCheckedUpdate = Date.now();
|
2013-01-25 06:24:17 +04:00
|
|
|
if (isPackage) {
|
2013-01-18 21:54:06 +04:00
|
|
|
if (oldHash != hash) {
|
2014-02-01 02:50:37 +04:00
|
|
|
this.updatePackagedApp(aData, id, app, manifest);
|
2013-01-18 21:54:06 +04:00
|
|
|
} else {
|
2014-01-17 04:26:24 +04:00
|
|
|
this._saveApps().then(() => {
|
2013-10-17 16:47:58 +04:00
|
|
|
// Like if we got a 304, just send a 'downloadapplied'
|
|
|
|
// or downloadavailable event.
|
|
|
|
let eventType = app.downloadAvailable ? "downloadavailable"
|
|
|
|
: "downloadapplied";
|
|
|
|
aMm.sendAsyncMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
|
|
|
aMm.sendAsyncMessage("Webapps:FireEvent", {
|
|
|
|
eventType: eventType,
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
});
|
2013-01-18 21:54:06 +04:00
|
|
|
}
|
2012-09-27 05:01:20 +04:00
|
|
|
} else {
|
2013-02-14 21:23:58 +04:00
|
|
|
// Update only the appcache if the manifest has not changed
|
|
|
|
// based on the hash value.
|
2014-02-01 02:50:37 +04:00
|
|
|
this.updateHostedApp(aData, id, app, oldManifest,
|
2013-02-14 21:23:58 +04:00
|
|
|
oldHash == hash ? null : manifest);
|
2012-09-27 05:01:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (xhr.status == 304) {
|
2013-01-10 21:06:59 +04:00
|
|
|
// The manifest has not changed.
|
2013-01-25 06:24:17 +04:00
|
|
|
if (isPackage) {
|
2013-01-10 21:06:59 +04:00
|
|
|
app.lastCheckedUpdate = Date.now();
|
2014-01-17 04:26:24 +04:00
|
|
|
this._saveApps().then(() => {
|
2013-10-17 16:47:58 +04:00
|
|
|
// If the app is a packaged app, we just send a 'downloadapplied'
|
|
|
|
// or downloadavailable event.
|
|
|
|
let eventType = app.downloadAvailable ? "downloadavailable"
|
|
|
|
: "downloadapplied";
|
|
|
|
aMm.sendAsyncMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
|
|
|
aMm.sendAsyncMessage("Webapps:FireEvent", {
|
|
|
|
eventType: eventType,
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
});
|
2013-01-10 21:06:59 +04:00
|
|
|
} else {
|
|
|
|
// For hosted apps, even if the manifest has not changed, we check
|
|
|
|
// for offline cache updates.
|
2014-02-01 02:50:37 +04:00
|
|
|
this.updateHostedApp(aData, id, app, oldManifest, null);
|
2012-09-27 05:01:20 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
2013-02-14 21:23:58 +04:00
|
|
|
}
|
2012-09-27 05:01:20 +04:00
|
|
|
|
2013-02-14 21:23:58 +04:00
|
|
|
// Try to download a new manifest.
|
2013-05-06 21:41:07 +04:00
|
|
|
function doRequest(oldManifest, headers) {
|
|
|
|
headers = headers || [];
|
2013-02-14 21:23:58 +04:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", aData.manifestURL, true);
|
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2013-05-06 21:41:07 +04:00
|
|
|
headers.forEach(function(aHeader) {
|
|
|
|
debug("Adding header: " + aHeader.name + ": " + aHeader.value);
|
|
|
|
xhr.setRequestHeader(aHeader.name, aHeader.value);
|
|
|
|
});
|
2013-02-14 21:23:58 +04:00
|
|
|
xhr.responseType = "json";
|
|
|
|
if (app.etag) {
|
|
|
|
debug("adding manifest etag:" + app.etag);
|
|
|
|
xhr.setRequestHeader("If-None-Match", app.etag);
|
|
|
|
}
|
|
|
|
xhr.channel.notificationCallbacks =
|
|
|
|
this.createLoadContext(app.installerAppId, app.installerIsBrowser);
|
2012-09-27 05:01:20 +04:00
|
|
|
|
2013-02-14 21:23:58 +04:00
|
|
|
xhr.addEventListener("load", onload.bind(this, xhr, oldManifest), false);
|
|
|
|
xhr.addEventListener("error", (function() {
|
|
|
|
sendError("NETWORK_ERROR");
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
debug("Checking manifest at " + aData.manifestURL);
|
|
|
|
xhr.send(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the current app manifest file
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests([{ id: id }]).then((aResult) => {
|
2013-05-06 21:41:07 +04:00
|
|
|
let extraHeaders = [];
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
let pingManifestURL;
|
|
|
|
try {
|
|
|
|
pingManifestURL = Services.prefs.getCharPref("ping.manifestURL");
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
if (pingManifestURL && pingManifestURL == aData.manifestURL) {
|
|
|
|
// Get the device info.
|
|
|
|
let device = libcutils.property_get("ro.product.model");
|
|
|
|
extraHeaders.push({ name: "X-MOZ-B2G-DEVICE",
|
|
|
|
value: device || "unknown" });
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
doRequest.call(this, aResult[0].manifest, extraHeaders);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-09-27 05:01:20 +04:00
|
|
|
},
|
|
|
|
|
2012-12-11 03:49:02 +04:00
|
|
|
// Creates a nsILoadContext object with a given appId and isBrowser flag.
|
|
|
|
createLoadContext: function createLoadContext(aAppId, aIsBrowser) {
|
|
|
|
return {
|
|
|
|
associatedWindow: null,
|
|
|
|
topWindow : null,
|
|
|
|
appId: aAppId,
|
|
|
|
isInBrowserElement: aIsBrowser,
|
|
|
|
usePrivateBrowsing: false,
|
|
|
|
isContent: false,
|
|
|
|
|
|
|
|
isAppOfType: function(appType) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsILoadContext,
|
|
|
|
Ci.nsIInterfaceRequestor,
|
|
|
|
Ci.nsISupports]),
|
|
|
|
getInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsILoadContext))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
updatePackagedApp: Task.async(function*(aData, aId, aApp, aNewManifest) {
|
2014-02-01 02:50:37 +04:00
|
|
|
debug("updatePackagedApp");
|
|
|
|
|
|
|
|
// Store the new update manifest.
|
|
|
|
let dir = this._getAppDir(aId).path;
|
|
|
|
let manFile = OS.Path.join(dir, "staged-update.webapp");
|
2014-04-25 19:49:00 +04:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(aNewManifest));
|
2014-02-01 02:50:37 +04:00
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aNewManifest, aApp.origin, aApp.manifestURL);
|
2014-02-01 02:50:37 +04:00
|
|
|
// A package is available: set downloadAvailable to fire the matching
|
|
|
|
// event.
|
|
|
|
aApp.downloadAvailable = true;
|
|
|
|
aApp.downloadSize = manifest.size;
|
|
|
|
aApp.updateManifest = aNewManifest;
|
2014-08-19 18:14:08 +04:00
|
|
|
this._saveWidgetsFullPath(manifest, aApp);
|
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
yield this._saveApps();
|
|
|
|
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: aApp,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aApp.id
|
2014-02-01 02:50:37 +04:00
|
|
|
});
|
2014-04-25 19:49:00 +04:00
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloadavailable",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
}),
|
2014-02-01 02:50:37 +04:00
|
|
|
|
|
|
|
// A hosted app is updated if the app manifest or the appcache needs
|
|
|
|
// updating. Even if the app manifest has not changed, we still check
|
|
|
|
// for changes in the app cache.
|
|
|
|
// 'aNewManifest' would contain the updated app manifest if
|
|
|
|
// it has actually been updated, while 'aOldManifest' contains the
|
|
|
|
// stored app manifest.
|
2014-04-25 19:49:00 +04:00
|
|
|
updateHostedApp: Task.async(function*(aData, aId, aApp, aOldManifest, aNewManifest) {
|
2014-02-01 02:50:37 +04:00
|
|
|
debug("updateHostedApp " + aData.manifestURL);
|
|
|
|
|
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (aId in this._manifestCache) {
|
|
|
|
delete this._manifestCache[aId];
|
|
|
|
}
|
|
|
|
|
|
|
|
aApp.manifest = aNewManifest || aOldManifest;
|
|
|
|
|
2014-08-29 04:20:27 +04:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aApp.manifest, aApp.origin, aApp.manifestURL);
|
|
|
|
aApp.role = manifest.role || "";
|
|
|
|
|
|
|
|
if (!AppsUtils.checkAppRole(aApp.role, aApp.appStatus)) {
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: aApp,
|
|
|
|
manifest: aApp.manifest,
|
|
|
|
id: aApp.id
|
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloadapplied",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
delete aApp.manifest;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-01 02:50:37 +04:00
|
|
|
if (aNewManifest) {
|
|
|
|
this.updateAppHandlers(aOldManifest, aNewManifest, aApp);
|
2014-03-16 01:37:37 +04:00
|
|
|
this.notifyUpdateHandlers(AppsUtils.cloneAppObject(aApp), aNewManifest);
|
|
|
|
|
2014-02-01 02:50:37 +04:00
|
|
|
// Store the new manifest.
|
|
|
|
let dir = this._getAppDir(aId).path;
|
|
|
|
let manFile = OS.Path.join(dir, "manifest.webapp");
|
2014-04-25 19:49:00 +04:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(aNewManifest));
|
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
manifest =
|
|
|
|
new ManifestHelper(aNewManifest, aApp.origin, aApp.manifestURL);
|
2014-02-01 02:50:37 +04:00
|
|
|
|
|
|
|
if (supportUseCurrentProfile()) {
|
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({
|
|
|
|
manifest: aApp.manifest,
|
|
|
|
origin: aApp.origin,
|
|
|
|
manifestURL: aData.manifestURL
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateDataStore(this.webapps[aId].localId, aApp.origin,
|
2014-06-19 18:03:54 +04:00
|
|
|
aApp.manifestURL, aApp.manifest);
|
2014-02-01 02:50:37 +04:00
|
|
|
|
2014-06-23 04:22:07 +04:00
|
|
|
aApp.name = aNewManifest.name;
|
2014-02-01 02:50:37 +04:00
|
|
|
aApp.csp = manifest.csp || "";
|
2014-08-19 18:14:08 +04:00
|
|
|
this._saveWidgetsFullPath(manifest, aApp);
|
2014-02-01 02:50:37 +04:00
|
|
|
aApp.updateTime = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the registry.
|
|
|
|
this.webapps[aId] = aApp;
|
2014-04-25 19:49:00 +04:00
|
|
|
yield this._saveApps();
|
|
|
|
|
2014-08-01 21:41:52 +04:00
|
|
|
if (aApp.kind !== this.kHostedAppcache) {
|
2014-04-25 19:49:00 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: aApp,
|
|
|
|
manifest: aApp.manifest,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aApp.id
|
2014-04-25 19:49:00 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloadapplied",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Check if the appcache is updatable, and send "downloadavailable" or
|
|
|
|
// "downloadapplied".
|
|
|
|
debug("updateHostedApp: updateSvc.checkForUpdate for " +
|
|
|
|
manifest.fullAppcachePath());
|
|
|
|
|
|
|
|
let updateDeferred = Promise.defer();
|
|
|
|
|
|
|
|
updateSvc.checkForUpdate(Services.io.newURI(manifest.fullAppcachePath(), null, null),
|
|
|
|
aApp.localId, false,
|
|
|
|
(aSubject, aTopic, aData) => updateDeferred.resolve(aTopic));
|
|
|
|
|
|
|
|
let topic = yield updateDeferred.promise;
|
|
|
|
|
|
|
|
debug("updateHostedApp: updateSvc.checkForUpdate return for " +
|
|
|
|
aApp.manifestURL + " - event is " + topic);
|
|
|
|
|
|
|
|
let eventType =
|
|
|
|
topic == "offline-cache-update-available" ? "downloadavailable"
|
|
|
|
: "downloadapplied";
|
|
|
|
|
|
|
|
aApp.downloadAvailable = (eventType == "downloadavailable");
|
|
|
|
yield this._saveApps();
|
|
|
|
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: aApp,
|
|
|
|
manifest: aApp.manifest,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aApp.id
|
2014-04-25 19:49:00 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: eventType,
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete aApp.manifest;
|
|
|
|
}),
|
2014-02-01 02:50:37 +04:00
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
// Downloads the manifest and run checks, then eventually triggers the
|
|
|
|
// installation UI.
|
|
|
|
doInstall: function doInstall(aData, aMm) {
|
2013-01-21 23:13:35 +04:00
|
|
|
let app = aData.app;
|
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
let sendError = function sendError(aError) {
|
|
|
|
aData.error = aError;
|
|
|
|
aMm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
|
|
|
|
Cu.reportError("Error installing app from: " + app.installOrigin +
|
|
|
|
": " + aError);
|
|
|
|
}.bind(this);
|
|
|
|
|
2014-01-23 18:46:20 +04:00
|
|
|
if (app.receipts.length > 0) {
|
|
|
|
for (let receipt of app.receipts) {
|
|
|
|
let error = this.isReceipt(receipt);
|
|
|
|
if (error) {
|
|
|
|
sendError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
// Hosted apps can't be trusted or certified, so just check that the
|
|
|
|
// manifest doesn't ask for those.
|
|
|
|
function checkAppStatus(aManifest) {
|
|
|
|
let manifestStatus = aManifest.type || "web";
|
2013-01-10 02:34:13 +04:00
|
|
|
return manifestStatus === "web";
|
2012-12-11 03:48:59 +04:00
|
|
|
}
|
|
|
|
|
2014-01-09 22:18:55 +04:00
|
|
|
let checkManifest = (function() {
|
|
|
|
if (!app.manifest) {
|
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-23 00:08:21 +04:00
|
|
|
// Disallow reinstalls from the same manifest url for now.
|
2014-01-09 22:18:55 +04:00
|
|
|
for (let id in this.webapps) {
|
2014-07-23 00:08:21 +04:00
|
|
|
if (this.webapps[id].manifestURL == app.manifestURL &&
|
2014-01-09 22:18:55 +04:00
|
|
|
this._isLaunchable(this.webapps[id])) {
|
2014-07-23 00:08:21 +04:00
|
|
|
sendError("REINSTALL_FORBIDDEN");
|
2014-01-09 22:18:55 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.checkManifest(app.manifest, app)) {
|
|
|
|
sendError("INVALID_MANIFEST");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.checkInstallAllowed(app.manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkAppStatus(app.manifest)) {
|
|
|
|
sendError("INVALID_SECURITY_LEVEL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:20:27 +04:00
|
|
|
app.role = app.manifest.role || "";
|
|
|
|
if (!AppsUtils.checkAppRole(app.role, app.appStatus)) {
|
|
|
|
sendError("INVALID_ROLE");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-09 22:18:55 +04:00
|
|
|
return true;
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
let installApp = (function() {
|
|
|
|
app.manifestHash = this.computeManifestHash(app.manifest);
|
|
|
|
// We allow bypassing the install confirmation process to facilitate
|
|
|
|
// automation.
|
|
|
|
let prefName = "dom.mozApps.auto_confirm_install";
|
|
|
|
if (Services.prefs.prefHasUserValue(prefName) &&
|
|
|
|
Services.prefs.getBoolPref(prefName)) {
|
|
|
|
this.confirmInstall(aData);
|
|
|
|
} else {
|
|
|
|
Services.obs.notifyObservers(aMm, "webapps-ask-install",
|
|
|
|
JSON.stringify(aData));
|
|
|
|
}
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
// We may already have the manifest (e.g. AutoInstall),
|
|
|
|
// in which case we don't need to load it.
|
|
|
|
if (app.manifest) {
|
|
|
|
if (checkManifest()) {
|
|
|
|
installApp();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", app.manifestURL, true);
|
2013-01-09 14:02:08 +04:00
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2012-12-11 03:49:02 +04:00
|
|
|
xhr.channel.notificationCallbacks = this.createLoadContext(aData.appId,
|
|
|
|
aData.isBrowser);
|
2012-12-11 03:48:59 +04:00
|
|
|
xhr.responseType = "json";
|
2012-12-11 03:49:02 +04:00
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
xhr.addEventListener("load", (function() {
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
if (!AppsUtils.checkManifestContentType(app.installOrigin, app.origin,
|
|
|
|
xhr.getResponseHeader("content-type"))) {
|
2014-07-06 20:47:28 +04:00
|
|
|
sendError("INVALID_MANIFEST_CONTENT_TYPE");
|
2012-12-11 03:48:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.manifest = xhr.response;
|
2014-01-09 22:18:55 +04:00
|
|
|
if (checkManifest()) {
|
2012-12-11 03:48:59 +04:00
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
2014-01-09 22:18:55 +04:00
|
|
|
installApp();
|
2012-12-11 03:48:59 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.addEventListener("error", (function() {
|
|
|
|
sendError("NETWORK_ERROR");
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.send(null);
|
|
|
|
},
|
|
|
|
|
|
|
|
doInstallPackage: function doInstallPackage(aData, aMm) {
|
2013-01-21 23:13:35 +04:00
|
|
|
let app = aData.app;
|
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
let sendError = function sendError(aError) {
|
|
|
|
aData.error = aError;
|
|
|
|
aMm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
|
|
|
|
Cu.reportError("Error installing packaged app from: " +
|
|
|
|
app.installOrigin + ": " + aError);
|
|
|
|
}.bind(this);
|
|
|
|
|
2014-01-23 18:46:20 +04:00
|
|
|
if (app.receipts.length > 0) {
|
|
|
|
for (let receipt of app.receipts) {
|
|
|
|
let error = this.isReceipt(receipt);
|
|
|
|
if (error) {
|
|
|
|
sendError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 22:18:55 +04:00
|
|
|
let checkUpdateManifest = (function() {
|
|
|
|
let manifest = app.updateManifest;
|
|
|
|
|
|
|
|
// Disallow reinstalls from the same manifest URL for now.
|
|
|
|
let id = this._appIdForManifestURL(app.manifestURL);
|
|
|
|
if (id !== null && this._isLaunchable(this.webapps[id])) {
|
|
|
|
sendError("REINSTALL_FORBIDDEN");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(AppsUtils.checkManifest(manifest, app) && manifest.package_path)) {
|
|
|
|
sendError("INVALID_MANIFEST");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.checkInstallAllowed(manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
let installApp = (function() {
|
|
|
|
app.manifestHash = this.computeManifestHash(app.updateManifest);
|
|
|
|
|
|
|
|
// We allow bypassing the install confirmation process to facilitate
|
|
|
|
// automation.
|
|
|
|
let prefName = "dom.mozApps.auto_confirm_install";
|
|
|
|
if (Services.prefs.prefHasUserValue(prefName) &&
|
|
|
|
Services.prefs.getBoolPref(prefName)) {
|
|
|
|
this.confirmInstall(aData);
|
|
|
|
} else {
|
|
|
|
Services.obs.notifyObservers(aMm, "webapps-ask-install",
|
|
|
|
JSON.stringify(aData));
|
|
|
|
}
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
// We may already have the manifest (e.g. AutoInstall),
|
|
|
|
// in which case we don't need to load it.
|
|
|
|
if (app.updateManifest) {
|
|
|
|
if (checkUpdateManifest()) {
|
|
|
|
installApp();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-11 03:48:59 +04:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", app.manifestURL, true);
|
2013-01-09 14:02:08 +04:00
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2012-12-11 03:49:02 +04:00
|
|
|
xhr.channel.notificationCallbacks = this.createLoadContext(aData.appId,
|
|
|
|
aData.isBrowser);
|
2012-12-11 03:48:59 +04:00
|
|
|
xhr.responseType = "json";
|
|
|
|
|
|
|
|
xhr.addEventListener("load", (function() {
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
if (!AppsUtils.checkManifestContentType(app.installOrigin, app.origin,
|
|
|
|
xhr.getResponseHeader("content-type"))) {
|
2014-07-06 20:47:28 +04:00
|
|
|
sendError("INVALID_MANIFEST_CONTENT_TYPE");
|
2012-12-11 03:48:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-09 22:18:55 +04:00
|
|
|
app.updateManifest = xhr.response;
|
|
|
|
if (!app.updateManifest) {
|
2012-12-11 03:48:59 +04:00
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2014-01-09 22:18:55 +04:00
|
|
|
if (checkUpdateManifest()) {
|
2012-12-11 03:48:59 +04:00
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
2013-01-14 21:47:55 +04:00
|
|
|
debug("at install package got app etag=" + app.etag);
|
2014-01-09 22:18:55 +04:00
|
|
|
installApp();
|
2012-12-11 03:48:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.addEventListener("error", (function() {
|
|
|
|
sendError("NETWORK_ERROR");
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.send(null);
|
|
|
|
},
|
|
|
|
|
2011-11-29 00:13:26 +04:00
|
|
|
denyInstall: function(aData) {
|
2014-02-21 23:54:03 +04:00
|
|
|
let packageId = aData.app.packageId;
|
|
|
|
if (packageId) {
|
|
|
|
let dir = FileUtils.getDir("TmpD", ["webapps", packageId],
|
|
|
|
true, true);
|
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch(e) {
|
2012-07-11 19:38:33 +04:00
|
|
|
}
|
2014-02-21 23:54:03 +04:00
|
|
|
}
|
|
|
|
aData.mm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
2011-12-08 17:32:54 +04:00
|
|
|
|
2013-02-13 23:55:45 +04:00
|
|
|
// This function is called after we called the onsuccess callback on the
|
|
|
|
// content side. This let the webpage the opportunity to set event handlers
|
|
|
|
// on the app before we start firing progress events.
|
|
|
|
queuedDownload: {},
|
2013-08-06 18:58:44 +04:00
|
|
|
queuedPackageDownload: {},
|
2013-02-13 23:55:45 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
onInstallSuccessAck: Task.async(function*(aManifestURL, aDontNeedNetwork) {
|
2013-10-24 03:50:57 +04:00
|
|
|
// If we are offline, register to run when we'll be online.
|
2013-11-01 05:58:28 +04:00
|
|
|
if ((Services.io.offline) && !aDontNeedNetwork) {
|
2013-10-24 03:50:57 +04:00
|
|
|
let onlineWrapper = {
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
Services.obs.removeObserver(onlineWrapper,
|
|
|
|
"network:offline-status-changed");
|
|
|
|
DOMApplicationRegistry.onInstallSuccessAck(aManifestURL);
|
|
|
|
}
|
2013-11-01 05:58:28 +04:00
|
|
|
};
|
2013-10-24 03:50:57 +04:00
|
|
|
Services.obs.addObserver(onlineWrapper,
|
|
|
|
"network:offline-status-changed", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-06 18:58:44 +04:00
|
|
|
let cacheDownload = this.queuedDownload[aManifestURL];
|
|
|
|
if (cacheDownload) {
|
|
|
|
this.startOfflineCacheDownload(cacheDownload.manifest,
|
|
|
|
cacheDownload.app,
|
2013-08-29 18:00:33 +04:00
|
|
|
cacheDownload.profileDir);
|
2013-08-06 18:58:44 +04:00
|
|
|
delete this.queuedDownload[aManifestURL];
|
|
|
|
|
2013-02-13 23:55:45 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-06 18:58:44 +04:00
|
|
|
|
|
|
|
let packageDownload = this.queuedPackageDownload[aManifestURL];
|
|
|
|
if (packageDownload) {
|
|
|
|
let manifest = packageDownload.manifest;
|
2013-11-26 03:14:35 +04:00
|
|
|
let newApp = packageDownload.app;
|
2013-08-06 18:58:44 +04:00
|
|
|
let installSuccessCallback = packageDownload.callback;
|
|
|
|
|
|
|
|
delete this.queuedPackageDownload[aManifestURL];
|
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
let id = this._appIdForManifestURL(newApp.manifestURL);
|
|
|
|
let oldApp = this.webapps[id];
|
|
|
|
let newManifest, newId;
|
|
|
|
try {
|
|
|
|
[newId, newManifest] = yield this.downloadPackage(id, oldApp, manifest, newApp, false);
|
|
|
|
|
|
|
|
yield this._onDownloadPackage(newApp, installSuccessCallback, newId, newManifest);
|
|
|
|
} catch (ex) {
|
|
|
|
this.revertDownloadPackage(id, oldApp, newApp, false, ex);
|
|
|
|
}
|
2013-08-06 18:58:44 +04:00
|
|
|
}
|
2014-07-21 13:26:58 +04:00
|
|
|
}),
|
2013-02-13 23:55:45 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
_setupApp: function(aData, aId) {
|
2011-11-29 00:13:26 +04:00
|
|
|
let app = aData.app;
|
2013-01-08 16:18:47 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
// app can be uninstalled
|
|
|
|
app.removable = true;
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2012-09-27 23:34:41 +04:00
|
|
|
if (aData.isPackage) {
|
2012-07-11 19:38:33 +04:00
|
|
|
// Override the origin with the correct id.
|
2013-11-26 03:14:35 +04:00
|
|
|
app.origin = "app://" + aId;
|
2012-07-11 19:38:33 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
app.id = aId;
|
|
|
|
app.installTime = Date.now();
|
|
|
|
app.lastUpdateCheck = Date.now();
|
2011-12-06 08:22:01 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return app;
|
|
|
|
},
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2014-06-23 04:22:07 +04:00
|
|
|
_cloneApp: function(aData, aNewApp, aLocaleManifest, aManifest, aId, aLocalId) {
|
2013-11-26 03:14:35 +04:00
|
|
|
let appObject = AppsUtils.cloneAppObject(aNewApp);
|
|
|
|
appObject.appStatus =
|
|
|
|
aNewApp.appStatus || Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
2012-09-27 05:01:20 +04:00
|
|
|
|
2014-08-01 21:41:52 +04:00
|
|
|
if (appObject.kind == this.kHostedAppcache) {
|
2012-09-27 05:01:20 +04:00
|
|
|
appObject.installState = "pending";
|
|
|
|
appObject.downloadAvailable = true;
|
|
|
|
appObject.downloading = true;
|
2012-09-27 23:34:41 +04:00
|
|
|
appObject.downloadSize = 0;
|
|
|
|
appObject.readyToApplyDownload = false;
|
2014-08-01 21:41:52 +04:00
|
|
|
} else if (appObject.kind == this.kPackaged) {
|
2012-09-27 23:34:41 +04:00
|
|
|
appObject.installState = "pending";
|
|
|
|
appObject.downloadAvailable = true;
|
|
|
|
appObject.downloading = true;
|
2014-06-23 04:22:07 +04:00
|
|
|
appObject.downloadSize = aLocaleManifest.size;
|
2012-09-27 05:01:20 +04:00
|
|
|
appObject.readyToApplyDownload = false;
|
2014-08-01 21:41:52 +04:00
|
|
|
} else if (appObject.kind == this.kHosted) {
|
2012-09-27 05:01:20 +04:00
|
|
|
appObject.installState = "installed";
|
|
|
|
appObject.downloadAvailable = false;
|
|
|
|
appObject.downloading = false;
|
|
|
|
appObject.readyToApplyDownload = false;
|
2014-08-01 21:41:52 +04:00
|
|
|
} else {
|
|
|
|
debug("Unknown app kind: " + appObject.kind);
|
|
|
|
throw Error("Unknown app kind: " + appObject.kind);
|
2012-09-27 05:01:20 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
appObject.localId = aLocalId;
|
2014-01-17 04:26:24 +04:00
|
|
|
appObject.basePath = OS.Path.dirname(this.appsFile);
|
2013-11-26 03:14:35 +04:00
|
|
|
appObject.name = aManifest.name;
|
2014-06-23 04:22:07 +04:00
|
|
|
appObject.csp = aLocaleManifest.csp || "";
|
|
|
|
appObject.role = aLocaleManifest.role;
|
2014-08-19 18:14:08 +04:00
|
|
|
this._saveWidgetsFullPath(aLocaleManifest, appObject);
|
2012-12-11 03:49:02 +04:00
|
|
|
appObject.installerAppId = aData.appId;
|
|
|
|
appObject.installerIsBrowser = aData.isBrowser;
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return appObject;
|
|
|
|
},
|
|
|
|
|
2014-02-21 23:54:03 +04:00
|
|
|
_writeManifestFile: function(aId, aIsPackage, aJsonManifest) {
|
2013-11-26 03:14:35 +04:00
|
|
|
debug("_writeManifestFile");
|
|
|
|
|
|
|
|
// For packaged apps, keep the update manifest distinct from the app manifest.
|
|
|
|
let manifestName = aIsPackage ? "update.webapp" : "manifest.webapp";
|
2014-01-17 04:26:24 +04:00
|
|
|
|
2014-02-21 23:54:03 +04:00
|
|
|
let dir = this._getAppDir(aId).path;
|
|
|
|
let manFile = OS.Path.join(dir, manifestName);
|
2014-04-29 12:18:00 +04:00
|
|
|
return this._writeFile(manFile, JSON.stringify(aJsonManifest));
|
2013-11-26 03:14:35 +04:00
|
|
|
},
|
|
|
|
|
2014-04-13 22:42:39 +04:00
|
|
|
// Add an app that is already installed to the registry.
|
|
|
|
addInstalledApp: Task.async(function*(aApp, aManifest, aUpdateManifest) {
|
|
|
|
if (this.getAppLocalIdByManifestURL(aApp.manifestURL) !=
|
|
|
|
Ci.nsIScriptSecurityManager.NO_APP_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = AppsUtils.cloneAppObject(aApp);
|
|
|
|
|
|
|
|
if (!AppsUtils.checkManifest(aManifest, app) ||
|
|
|
|
(aUpdateManifest && !AppsUtils.checkManifest(aUpdateManifest, app))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.name = aManifest.name;
|
|
|
|
|
|
|
|
app.csp = aManifest.csp || "";
|
|
|
|
|
2014-08-19 18:14:08 +04:00
|
|
|
let aLocaleManifest = new ManifestHelper(aManifest, app.origin, app.manifestURL);
|
|
|
|
this._saveWidgetsFullPath(aLocaleManifest, app);
|
|
|
|
|
2014-04-13 22:42:39 +04:00
|
|
|
app.appStatus = AppsUtils.getAppManifestStatus(aManifest);
|
|
|
|
|
|
|
|
app.removable = true;
|
|
|
|
|
|
|
|
// Reuse the app ID if the scheme is "app".
|
|
|
|
let uri = Services.io.newURI(app.origin, null, null);
|
|
|
|
if (uri.scheme == "app") {
|
|
|
|
app.id = uri.host;
|
|
|
|
} else {
|
|
|
|
app.id = this.makeAppId();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.localId = this._nextLocalId();
|
|
|
|
|
|
|
|
app.basePath = OS.Path.dirname(this.appsFile);
|
|
|
|
|
|
|
|
app.progress = 0.0;
|
|
|
|
app.installState = "installed";
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
app.downloading = false;
|
|
|
|
app.readyToApplyDownload = false;
|
|
|
|
|
|
|
|
if (aUpdateManifest && aUpdateManifest.size) {
|
|
|
|
app.downloadSize = aUpdateManifest.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.manifestHash = AppsUtils.computeHash(JSON.stringify(aUpdateManifest ||
|
|
|
|
aManifest));
|
|
|
|
|
|
|
|
let zipFile = WebappOSUtils.getPackagePath(app);
|
|
|
|
app.packageHash = yield this._computeFileHash(zipFile);
|
|
|
|
|
|
|
|
app.role = aManifest.role || "";
|
2014-08-29 04:20:27 +04:00
|
|
|
if (!AppsUtils.checkAppRole(app.role, app.appStatus)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-13 22:42:39 +04:00
|
|
|
|
|
|
|
app.redirects = this.sanitizeRedirects(aManifest.redirects);
|
|
|
|
|
|
|
|
this.webapps[app.id] = app;
|
|
|
|
|
|
|
|
// Store the manifest in the manifest cache, so we don't need to re-read it
|
|
|
|
this._manifestCache[app.id] = app.manifest;
|
|
|
|
|
|
|
|
// Store the manifest and the updateManifest.
|
|
|
|
this._writeManifestFile(app.id, false, aManifest);
|
|
|
|
if (aUpdateManifest) {
|
|
|
|
this._writeManifestFile(app.id, true, aUpdateManifest);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._saveApps().then(() => {
|
2014-05-24 23:28:13 +04:00
|
|
|
this.broadcastMessage("Webapps:AddApp",
|
|
|
|
{ id: app.id, app: app, manifest: aManifest });
|
2014-04-13 22:42:39 +04:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2014-04-29 12:18:00 +04:00
|
|
|
confirmInstall: Task.async(function*(aData, aProfileDir, aInstallSuccessCallback) {
|
2013-11-26 03:14:35 +04:00
|
|
|
debug("confirmInstall");
|
|
|
|
|
|
|
|
let origin = Services.io.newURI(aData.app.origin, null, null);
|
|
|
|
let id = this._appIdForManifestURL(aData.app.manifestURL);
|
|
|
|
let manifestURL = origin.resolve(aData.app.manifestURL);
|
|
|
|
let localId = this.getAppLocalIdByManifestURL(manifestURL);
|
|
|
|
|
|
|
|
let isReinstall = false;
|
|
|
|
|
|
|
|
// Installing an application again is considered as an update.
|
|
|
|
if (id) {
|
|
|
|
isReinstall = true;
|
|
|
|
let dir = this._getAppDir(id);
|
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch(e) { }
|
|
|
|
} else {
|
|
|
|
id = this.makeAppId();
|
|
|
|
localId = this._nextLocalId();
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = this._setupApp(aData, id);
|
|
|
|
|
|
|
|
let jsonManifest = aData.isPackage ? app.updateManifest : app.manifest;
|
2014-04-29 12:18:00 +04:00
|
|
|
yield this._writeManifestFile(id, aData.isPackage, jsonManifest);
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-03-01 03:25:21 +04:00
|
|
|
debug("app.origin: " + app.origin);
|
2014-07-31 01:00:15 +04:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-08-01 21:41:52 +04:00
|
|
|
// Set the application kind.
|
|
|
|
if (aData.isPackage) {
|
|
|
|
app.kind = this.kPackaged;
|
|
|
|
} else {
|
|
|
|
app.kind = manifest.appcache_path ? this.kHostedAppcache : this.kHosted;
|
|
|
|
}
|
|
|
|
|
2014-06-23 04:22:07 +04:00
|
|
|
let appObject = this._cloneApp(aData, app, manifest, jsonManifest, id, localId);
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2012-09-27 05:01:20 +04:00
|
|
|
this.webapps[id] = appObject;
|
2012-10-03 09:38:06 +04:00
|
|
|
|
2012-10-11 01:29:07 +04:00
|
|
|
// For package apps, the permissions are not in the mini-manifest, so
|
|
|
|
// don't update the permissions yet.
|
|
|
|
if (!aData.isPackage) {
|
2013-07-08 00:41:55 +04:00
|
|
|
if (supportUseCurrentProfile()) {
|
2013-11-26 03:14:35 +04:00
|
|
|
PermissionsInstaller.installPermissions(
|
|
|
|
{
|
|
|
|
origin: appObject.origin,
|
|
|
|
manifestURL: appObject.manifestURL,
|
|
|
|
manifest: jsonManifest
|
|
|
|
},
|
|
|
|
isReinstall,
|
2014-06-12 01:23:18 +04:00
|
|
|
this.doUninstall.bind(this, aData, aData.mm)
|
2013-11-26 03:14:35 +04:00
|
|
|
);
|
2013-07-08 00:41:55 +04:00
|
|
|
}
|
2013-10-02 21:27:07 +04:00
|
|
|
|
2013-10-02 21:27:23 +04:00
|
|
|
this.updateDataStore(this.webapps[id].localId, this.webapps[id].origin,
|
2014-06-19 18:03:54 +04:00
|
|
|
this.webapps[id].manifestURL, jsonManifest);
|
2012-10-11 01:29:07 +04:00
|
|
|
}
|
2012-10-03 09:38:06 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
for each (let prop in ["installState", "downloadAvailable", "downloading",
|
|
|
|
"downloadSize", "readyToApplyDownload"]) {
|
|
|
|
aData.app[prop] = appObject[prop];
|
|
|
|
}
|
2012-06-11 22:41:46 +04:00
|
|
|
|
2014-04-29 12:18:00 +04:00
|
|
|
let dontNeedNetwork = false;
|
|
|
|
|
2014-08-01 21:41:52 +04:00
|
|
|
if (appObject.kind == this.kHostedAppcache) {
|
2013-08-06 17:58:47 +04:00
|
|
|
this.queuedDownload[app.manifestURL] = {
|
|
|
|
manifest: manifest,
|
|
|
|
app: appObject,
|
2013-08-29 18:00:33 +04:00
|
|
|
profileDir: aProfileDir
|
2013-08-06 17:58:47 +04:00
|
|
|
}
|
2014-08-01 21:41:52 +04:00
|
|
|
} else if (appObject.kind == this.kPackaged) {
|
2013-07-31 23:34:19 +04:00
|
|
|
// If it is a local app then it must been installed from a local file
|
|
|
|
// instead of web.
|
2014-01-09 22:18:55 +04:00
|
|
|
// In that case, we would already have the manifest, not just the update
|
|
|
|
// manifest.
|
2014-06-10 21:37:38 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-01-09 22:18:55 +04:00
|
|
|
dontNeedNetwork = !!aData.app.manifest;
|
|
|
|
#else
|
2013-07-31 23:34:19 +04:00
|
|
|
if (aData.app.localInstallPath) {
|
2013-11-01 05:58:28 +04:00
|
|
|
dontNeedNetwork = true;
|
2013-07-31 23:34:19 +04:00
|
|
|
jsonManifest.package_path = "file://" + aData.app.localInstallPath;
|
2014-08-01 21:41:52 +04:00
|
|
|
}
|
2014-01-09 22:18:55 +04:00
|
|
|
#endif
|
|
|
|
|
2012-09-29 21:57:18 +04:00
|
|
|
// origin for install apps is meaningless here, since it's app:// and this
|
|
|
|
// can't be used to resolve package paths.
|
2014-07-31 01:00:15 +04:00
|
|
|
manifest = new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2013-07-24 21:15:37 +04:00
|
|
|
|
2013-08-06 18:58:44 +04:00
|
|
|
this.queuedPackageDownload[app.manifestURL] = {
|
|
|
|
manifest: manifest,
|
|
|
|
app: appObject,
|
|
|
|
callback: aInstallSuccessCallback
|
2013-07-31 23:34:19 +04:00
|
|
|
};
|
2013-10-24 03:50:57 +04:00
|
|
|
}
|
2013-07-31 23:34:19 +04:00
|
|
|
|
2014-04-29 12:18:00 +04:00
|
|
|
// We notify about the successful installation via mgmt.oninstall and the
|
|
|
|
// corresponding DOMRequest.onsuccess event as soon as the app is properly
|
|
|
|
// saved in the registry.
|
|
|
|
yield this._saveApps();
|
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
aData.isPackage ? appObject.updateManifest = jsonManifest :
|
|
|
|
appObject.manifest = jsonManifest;
|
2014-04-29 12:18:00 +04:00
|
|
|
this.broadcastMessage("Webapps:AddApp", { id: id, app: appObject });
|
2014-06-03 13:13:00 +04:00
|
|
|
|
2014-07-11 15:25:46 +04:00
|
|
|
if (!aData.isPackage) {
|
|
|
|
this.updateAppHandlers(null, app.manifest, app);
|
|
|
|
if (aInstallSuccessCallback) {
|
|
|
|
try {
|
|
|
|
yield aInstallSuccessCallback(app, app.manifest);
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore exceptions during the local installation of
|
|
|
|
// an app. If it fails, the app will anyway be considered
|
|
|
|
// as not installed because isLaunchable will return false.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:13:00 +04:00
|
|
|
// The presence of a requestID means that we have a page to update.
|
|
|
|
if (aData.isPackage && aData.apkInstall && !aData.requestID) {
|
2014-04-29 12:18:00 +04:00
|
|
|
// Skip directly to onInstallSuccessAck, since there isn't
|
|
|
|
// a WebappsRegistry to receive Webapps:Install:Return:OK and respond
|
|
|
|
// Webapps:Install:Return:Ack when an app is being auto-installed.
|
|
|
|
this.onInstallSuccessAck(app.manifestURL);
|
|
|
|
} else {
|
|
|
|
// Broadcast Webapps:Install:Return:OK so the WebappsRegistry can notify
|
|
|
|
// the installing page about the successful install, after which it'll
|
|
|
|
// respond Webapps:Install:Return:Ack, which calls onInstallSuccessAck.
|
|
|
|
this.broadcastMessage("Webapps:Install:Return:OK", aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(null, "webapps-installed",
|
|
|
|
JSON.stringify({ manifestURL: app.manifestURL }));
|
|
|
|
|
2013-10-24 03:50:57 +04:00
|
|
|
if (aData.forceSuccessAck) {
|
|
|
|
// If it's a local install, there's no content process so just
|
|
|
|
// ack the install.
|
2013-11-01 05:58:28 +04:00
|
|
|
this.onInstallSuccessAck(app.manifestURL, dontNeedNetwork);
|
2012-09-27 23:34:41 +04:00
|
|
|
}
|
2014-04-29 12:18:00 +04:00
|
|
|
}),
|
2011-12-08 17:32:54 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
/**
|
|
|
|
* Install the package after successfully downloading it
|
|
|
|
*
|
|
|
|
* Bound params:
|
|
|
|
*
|
|
|
|
* @param aNewApp {Object} the new app data
|
|
|
|
* @param aInstallSuccessCallback {Function}
|
|
|
|
* the callback to call on install success
|
|
|
|
*
|
|
|
|
* Passed params:
|
|
|
|
*
|
|
|
|
* @param aId {Integer} the unique ID of the application
|
|
|
|
* @param aManifest {Object} The manifest of the application
|
|
|
|
*/
|
2014-04-25 19:49:00 +04:00
|
|
|
_onDownloadPackage: Task.async(function*(aNewApp, aInstallSuccessCallback,
|
2014-07-21 13:26:58 +04:00
|
|
|
aId, aManifest) {
|
2013-11-26 03:14:35 +04:00
|
|
|
debug("_onDownloadPackage");
|
|
|
|
// Success! Move the zip out of TmpD.
|
|
|
|
let app = this.webapps[aId];
|
2014-02-21 23:54:03 +04:00
|
|
|
let zipFile =
|
|
|
|
FileUtils.getFile("TmpD", ["webapps", aId, "application.zip"], true);
|
2013-11-26 03:14:35 +04:00
|
|
|
let dir = this._getAppDir(aId);
|
|
|
|
zipFile.moveTo(dir, "application.zip");
|
2014-02-21 23:54:03 +04:00
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", aId], true, true);
|
2013-11-26 03:14:35 +04:00
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
// Save the manifest
|
2014-01-17 04:26:24 +04:00
|
|
|
let manFile = OS.Path.join(dir.path, "manifest.webapp");
|
2014-04-25 19:49:00 +04:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(aManifest));
|
2013-11-26 03:14:35 +04:00
|
|
|
// Set state and fire events.
|
|
|
|
app.installState = "installed";
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
2014-02-03 19:58:33 +04:00
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
yield this._saveApps();
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
this.updateAppHandlers(null, aManifest, aNewApp);
|
|
|
|
// Clear the manifest cache in case it holds the update manifest.
|
|
|
|
if (aId in this._manifestCache) {
|
|
|
|
delete this._manifestCache[aId];
|
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
this.broadcastMessage("Webapps:AddApp",
|
|
|
|
{ id: aId, app: aNewApp, manifest: aManifest });
|
2014-04-25 19:49:00 +04:00
|
|
|
Services.obs.notifyObservers(null, "webapps-installed",
|
|
|
|
JSON.stringify({ manifestURL: aNewApp.manifestURL }));
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
if (supportUseCurrentProfile()) {
|
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({
|
2013-11-26 03:14:35 +04:00
|
|
|
manifest: aManifest,
|
2014-04-25 19:49:00 +04:00
|
|
|
origin: aNewApp.origin,
|
2014-03-01 03:25:21 +04:00
|
|
|
manifestURL: aNewApp.manifestURL
|
2014-04-25 19:49:00 +04:00
|
|
|
}, true);
|
|
|
|
}
|
2014-04-18 09:03:03 +04:00
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
this.updateDataStore(this.webapps[aId].localId, aNewApp.origin,
|
2014-06-19 18:03:54 +04:00
|
|
|
aNewApp.manifestURL, aManifest);
|
2014-04-25 19:49:00 +04:00
|
|
|
|
2014-07-11 15:25:46 +04:00
|
|
|
if (aInstallSuccessCallback) {
|
|
|
|
try {
|
|
|
|
yield aInstallSuccessCallback(aNewApp, aManifest, zipFile.path);
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore exceptions during the local installation of
|
|
|
|
// an app. If it fails, the app will anyway be considered
|
|
|
|
// as not installed because isLaunchable will return false.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 19:49:00 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
|
|
|
manifest: aManifest,
|
2014-05-27 23:47:04 +04:00
|
|
|
manifestURL: aNewApp.manifestURL
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2014-04-25 19:49:00 +04:00
|
|
|
|
|
|
|
// Check if we have asm.js code to preload for this application.
|
|
|
|
yield ScriptPreloader.preload(aNewApp, aManifest);
|
|
|
|
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: ["downloadsuccess", "downloadapplied"],
|
|
|
|
manifestURL: aNewApp.manifestURL
|
|
|
|
});
|
|
|
|
}),
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2012-07-09 14:25:41 +04:00
|
|
|
_nextLocalId: function() {
|
2012-09-06 05:07:21 +04:00
|
|
|
let id = Services.prefs.getIntPref("dom.mozApps.maxLocalId") + 1;
|
2013-09-13 16:22:24 +04:00
|
|
|
|
|
|
|
while (this.getManifestURLByLocalId(id)) {
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
|
2012-09-06 05:07:21 +04:00
|
|
|
Services.prefs.setIntPref("dom.mozApps.maxLocalId", id);
|
2013-04-23 00:28:20 +04:00
|
|
|
Services.prefs.savePrefFile(null);
|
2012-09-06 05:07:21 +04:00
|
|
|
return id;
|
2012-07-09 14:25:41 +04:00
|
|
|
},
|
|
|
|
|
2012-09-27 23:34:41 +04:00
|
|
|
_appIdForManifestURL: function(aURI) {
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].manifestURL == aURI)
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2012-04-28 11:10:08 +04:00
|
|
|
makeAppId: function() {
|
|
|
|
let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
|
|
|
|
return uuidGenerator.generateUUID().toString();
|
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
_saveApps: function() {
|
|
|
|
return this._writeFile(this.appsFile, JSON.stringify(this.webapps, null, 2));
|
2011-12-08 17:32:54 +04:00
|
|
|
},
|
|
|
|
|
2011-12-06 03:26:18 +04:00
|
|
|
/**
|
|
|
|
* Asynchronously reads a list of manifests
|
|
|
|
*/
|
2012-11-03 07:28:32 +04:00
|
|
|
|
|
|
|
_manifestCache: {},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
_readManifests: function(aData) {
|
2014-06-26 09:07:14 +04:00
|
|
|
let manifestCache = this._manifestCache;
|
2014-01-17 04:26:24 +04:00
|
|
|
return Task.spawn(function*() {
|
|
|
|
for (let elem of aData) {
|
|
|
|
let id = elem.id;
|
2012-05-23 01:38:34 +04:00
|
|
|
|
2014-06-26 09:07:14 +04:00
|
|
|
if (!manifestCache[id]) {
|
2014-01-17 04:26:24 +04:00
|
|
|
// the manifest file used to be named manifest.json, so fallback on this.
|
|
|
|
let baseDir = this.webapps[id].basePath == this.getCoreAppsBasePath()
|
2014-02-21 23:54:03 +04:00
|
|
|
? "coreAppsDir" : DIRECTORY_NAME;
|
2012-11-03 07:28:32 +04:00
|
|
|
|
2014-01-31 22:14:17 +04:00
|
|
|
let dir = FileUtils.getDir(baseDir, ["webapps", id], false, true);
|
2014-01-17 04:26:24 +04:00
|
|
|
|
2014-01-31 22:14:17 +04:00
|
|
|
let fileNames = ["manifest.webapp", "update.webapp", "manifest.json"];
|
|
|
|
for (let fileName of fileNames) {
|
2014-06-26 09:07:14 +04:00
|
|
|
manifestCache[id] = yield AppsUtils.loadJSONAsync(OS.Path.join(dir.path, fileName));
|
|
|
|
if (manifestCache[id]) {
|
2014-01-31 22:14:17 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-01-17 04:26:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 09:07:14 +04:00
|
|
|
elem.manifest = manifestCache[id];
|
2014-01-17 04:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return aData;
|
|
|
|
}.bind(this)).then(null, Cu.reportError);
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
2011-12-08 17:32:54 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
downloadPackage: Task.async(function*(aId, aOldApp, aManifest, aNewApp, aIsUpdate) {
|
2012-07-11 19:38:33 +04:00
|
|
|
// Here are the steps when installing a package:
|
|
|
|
// - create a temp directory where to store the app.
|
|
|
|
// - download the zip in this directory.
|
2012-12-14 08:35:58 +04:00
|
|
|
// - check the signature on the zip.
|
2012-07-11 19:38:33 +04:00
|
|
|
// - extract the manifest from the zip and check it.
|
|
|
|
// - ask confirmation to the user.
|
|
|
|
// - add the new app to the registry.
|
2014-07-21 13:26:58 +04:00
|
|
|
yield this._ensureSufficientStorage(aNewApp);
|
|
|
|
|
|
|
|
let fullPackagePath = aManifest.fullPackagePath();
|
|
|
|
// Check if it's a local file install (we've downloaded/sideloaded the
|
|
|
|
// package already, it existed on the build, or it came with an APK).
|
|
|
|
// Note that this variable also controls whether files signed with expired
|
|
|
|
// certificates are accepted or not. If isLocalFileInstall is true and the
|
|
|
|
// device date is earlier than the build generation date, then the signature
|
|
|
|
// will be accepted even if the certificate is expired.
|
|
|
|
let isLocalFileInstall =
|
|
|
|
Services.io.extractScheme(fullPackagePath) === 'file';
|
|
|
|
|
|
|
|
debug("About to download " + fullPackagePath);
|
|
|
|
|
|
|
|
let requestChannel = this._getRequestChannel(fullPackagePath,
|
|
|
|
isLocalFileInstall,
|
|
|
|
aOldApp,
|
|
|
|
aNewApp);
|
|
|
|
|
|
|
|
AppDownloadManager.add(
|
|
|
|
aNewApp.manifestURL,
|
|
|
|
{
|
|
|
|
channel: requestChannel,
|
|
|
|
appId: aId,
|
|
|
|
previousState: aIsUpdate ? "installed" : "pending"
|
|
|
|
}
|
|
|
|
);
|
2012-07-11 19:38:33 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
// We set the 'downloading' flag to true right before starting the fetch.
|
|
|
|
aOldApp.downloading = true;
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
// We determine the app's 'installState' according to its previous
|
|
|
|
// state. Cancelled download should remain as 'pending'. Successfully
|
|
|
|
// installed apps should morph to 'updating'.
|
|
|
|
aOldApp.installState = aIsUpdate ? "updating" : "pending";
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
// initialize the progress to 0 right now
|
|
|
|
aOldApp.progress = 0;
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
// Save the current state of the app to handle cases where we may be
|
|
|
|
// retrying a past download.
|
|
|
|
yield DOMApplicationRegistry._saveApps();
|
2012-11-21 22:09:10 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:UpdateState", {
|
2014-06-26 00:47:00 +04:00
|
|
|
// Clear any previous download errors.
|
|
|
|
error: null,
|
2014-07-21 13:26:58 +04:00
|
|
|
app: aOldApp,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aId
|
2014-07-21 13:26:58 +04:00
|
|
|
});
|
2014-06-21 03:32:15 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
let zipFile = yield this._getPackage(requestChannel, aId, aOldApp, aNewApp);
|
2014-07-31 17:48:00 +04:00
|
|
|
|
|
|
|
// After this point, it's too late to cancel the download.
|
|
|
|
AppDownloadManager.remove(aNewApp.manifestURL);
|
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
let hash = yield this._computeFileHash(zipFile.path);
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
let responseStatus = requestChannel.responseStatus;
|
|
|
|
let oldPackage = (responseStatus == 304 || hash == aOldApp.packageHash);
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
if (oldPackage) {
|
|
|
|
debug("package's etag or hash unchanged; sending 'applied' event");
|
|
|
|
// The package's Etag or hash has not changed.
|
|
|
|
// We send an "applied" event right away so code awaiting that event
|
|
|
|
// can proceed to access the app. We also throw an error to alert
|
|
|
|
// the caller that the package wasn't downloaded.
|
2014-05-24 23:28:13 +04:00
|
|
|
this._sendAppliedEvent(aOldApp);
|
2014-07-21 13:26:58 +04:00
|
|
|
throw new Error("PACKAGE_UNCHANGED");
|
|
|
|
}
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
let newManifest = yield this._openAndReadPackage(zipFile, aOldApp, aNewApp,
|
|
|
|
isLocalFileInstall, aIsUpdate, aManifest, requestChannel, hash);
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
return [aOldApp.id, newManifest];
|
2013-11-26 03:14:35 +04:00
|
|
|
|
2014-07-21 13:26:58 +04:00
|
|
|
}),
|
2013-11-26 03:14:35 +04:00
|
|
|
|
|
|
|
_ensureSufficientStorage: function(aNewApp) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-03-16 01:37:37 +04:00
|
|
|
let navigator = Services.wm.getMostRecentWindow(chromeWindowType)
|
2013-11-26 03:14:35 +04:00
|
|
|
.navigator;
|
|
|
|
let deviceStorage = null;
|
|
|
|
|
|
|
|
if (navigator.getDeviceStorage) {
|
|
|
|
deviceStorage = navigator.getDeviceStorage("apps");
|
2012-07-11 19:38:33 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
if (deviceStorage) {
|
|
|
|
let req = deviceStorage.freeSpace();
|
|
|
|
req.onsuccess = req.onerror = e => {
|
|
|
|
let freeBytes = e.target.result;
|
|
|
|
let sufficientStorage = this._checkDownloadSize(freeBytes, aNewApp);
|
|
|
|
if (sufficientStorage) {
|
|
|
|
deferred.resolve();
|
|
|
|
} else {
|
|
|
|
deferred.reject("INSUFFICIENT_STORAGE");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug("No deviceStorage");
|
|
|
|
// deviceStorage isn't available, so use FileUtils to find the size of
|
|
|
|
// available storage.
|
2014-02-21 23:54:03 +04:00
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps"], true, true);
|
2013-11-26 03:14:35 +04:00
|
|
|
try {
|
|
|
|
let sufficientStorage = this._checkDownloadSize(dir.diskSpaceAvailable,
|
|
|
|
aNewApp);
|
|
|
|
if (sufficientStorage) {
|
|
|
|
deferred.resolve();
|
|
|
|
} else {
|
|
|
|
deferred.reject("INSUFFICIENT_STORAGE");
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
// If disk space information isn't available, we'll end up here.
|
|
|
|
// We should proceed anyway, otherwise devices that support neither
|
|
|
|
// deviceStorage nor diskSpaceAvailable will never be able to install
|
|
|
|
// packaged apps.
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
2013-02-11 12:38:51 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkDownloadSize: function(aFreeBytes, aNewApp) {
|
|
|
|
if (aFreeBytes) {
|
|
|
|
debug("Free storage: " + aFreeBytes + ". Download size: " +
|
|
|
|
aNewApp.downloadSize);
|
|
|
|
if (aFreeBytes <=
|
|
|
|
aNewApp.downloadSize + AppDownloadManager.MIN_REMAINING_FREESPACE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getRequestChannel: function(aFullPackagePath, aIsLocalFileInstall, aOldApp,
|
|
|
|
aNewApp) {
|
|
|
|
let requestChannel;
|
|
|
|
|
|
|
|
if (aIsLocalFileInstall) {
|
|
|
|
requestChannel = NetUtil.newChannel(aFullPackagePath)
|
|
|
|
.QueryInterface(Ci.nsIFileChannel);
|
|
|
|
} else {
|
|
|
|
requestChannel = NetUtil.newChannel(aFullPackagePath)
|
|
|
|
.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
requestChannel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOldApp.packageEtag && !aIsLocalFileInstall) {
|
|
|
|
debug("Add If-None-Match header: " + aOldApp.packageEtag);
|
|
|
|
requestChannel.setRequestHeader("If-None-Match", aOldApp.packageEtag,
|
|
|
|
false);
|
|
|
|
}
|
2013-06-26 16:18:34 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
let lastProgressTime = 0;
|
|
|
|
|
|
|
|
requestChannel.notificationCallbacks = {
|
|
|
|
QueryInterface: function(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsISupports) ||
|
|
|
|
aIID.equals(Ci.nsIProgressEventSink) ||
|
|
|
|
aIID.equals(Ci.nsILoadContext))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
getInterface: function(aIID) {
|
|
|
|
return this.QueryInterface(aIID);
|
|
|
|
},
|
|
|
|
onProgress: (function(aRequest, aContext, aProgress, aProgressMax) {
|
|
|
|
aOldApp.progress = aProgress;
|
|
|
|
let now = Date.now();
|
|
|
|
if (now - lastProgressTime > MIN_PROGRESS_EVENT_DELAY) {
|
|
|
|
debug("onProgress: " + aProgress + "/" + aProgressMax);
|
|
|
|
this._sendDownloadProgressEvent(aNewApp, aProgress);
|
|
|
|
lastProgressTime = now;
|
|
|
|
this._saveApps();
|
|
|
|
}
|
|
|
|
}).bind(this),
|
|
|
|
onStatus: function(aRequest, aContext, aStatus, aStatusArg) { },
|
|
|
|
|
|
|
|
// nsILoadContext
|
|
|
|
appId: aOldApp.installerAppId,
|
|
|
|
isInBrowserElement: aOldApp.installerIsBrowser,
|
|
|
|
usePrivateBrowsing: false,
|
|
|
|
isContent: false,
|
|
|
|
associatedWindow: null,
|
|
|
|
topWindow : null,
|
|
|
|
isAppOfType: function(appType) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return requestChannel;
|
|
|
|
},
|
|
|
|
|
|
|
|
_sendDownloadProgressEvent: function(aNewApp, aProgress) {
|
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: {
|
|
|
|
progress: aProgress
|
|
|
|
},
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aNewApp.id
|
2013-11-26 03:14:35 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "progress",
|
|
|
|
manifestURL: aNewApp.manifestURL
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPackage: function(aRequestChannel, aId, aOldApp, aNewApp) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
// Staging the zip in TmpD until all the checks are done.
|
|
|
|
let zipFile =
|
|
|
|
FileUtils.getFile("TmpD", ["webapps", aId, "application.zip"], true);
|
|
|
|
|
|
|
|
// We need an output stream to write the channel content to the zip file.
|
|
|
|
let outputStream = Cc["@mozilla.org/network/file-output-stream;1"]
|
|
|
|
.createInstance(Ci.nsIFileOutputStream);
|
|
|
|
// write, create, truncate
|
|
|
|
outputStream.init(zipFile, 0x02 | 0x08 | 0x20, parseInt("0664", 8), 0);
|
|
|
|
let bufferedOutputStream =
|
|
|
|
Cc['@mozilla.org/network/buffered-output-stream;1']
|
|
|
|
.createInstance(Ci.nsIBufferedOutputStream);
|
|
|
|
bufferedOutputStream.init(outputStream, 1024);
|
|
|
|
|
|
|
|
// Create a listener that will give data to the file output stream.
|
|
|
|
let listener = Cc["@mozilla.org/network/simple-stream-listener;1"]
|
|
|
|
.createInstance(Ci.nsISimpleStreamListener);
|
|
|
|
|
|
|
|
listener.init(bufferedOutputStream, {
|
|
|
|
onStartRequest: function(aRequest, aContext) {
|
|
|
|
// Nothing to do there anymore.
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
|
|
|
bufferedOutputStream.close();
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
if (!Components.isSuccessCode(aStatusCode)) {
|
|
|
|
deferred.reject("NETWORK_ERROR");
|
|
|
|
return;
|
2013-01-25 13:57:14 +04:00
|
|
|
}
|
2013-06-26 16:18:34 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
// If we get a 4XX or a 5XX http status, bail out like if we had a
|
|
|
|
// network error.
|
|
|
|
let responseStatus = aRequestChannel.responseStatus;
|
|
|
|
if (responseStatus >= 400 && responseStatus <= 599) {
|
|
|
|
// unrecoverable error, don't bug the user
|
|
|
|
aOldApp.downloadAvailable = false;
|
|
|
|
deferred.reject("NETWORK_ERROR");
|
|
|
|
return;
|
2013-01-25 13:57:14 +04:00
|
|
|
}
|
2013-06-26 16:18:34 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
deferred.resolve(zipFile);
|
2013-01-25 13:57:14 +04:00
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
});
|
|
|
|
aRequestChannel.asyncOpen(listener, null);
|
2013-01-25 13:57:14 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
// send a first progress event to correctly set the DOM object's properties
|
|
|
|
this._sendDownloadProgressEvent(aNewApp, 0);
|
2012-11-15 13:35:37 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the MD5 hash of a file, doing async IO off the main thread.
|
|
|
|
*
|
|
|
|
* @param {String} aFilePath
|
|
|
|
* the path of the file to hash
|
|
|
|
* @returns {String} the MD5 hash of the file
|
|
|
|
*/
|
|
|
|
_computeFileHash: function(aFilePath) {
|
2014-03-25 17:14:06 +04:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
file.initWithPath(aFilePath);
|
|
|
|
|
|
|
|
NetUtil.asyncFetch(file, function(inputStream, status) {
|
|
|
|
if (!Components.isSuccessCode(status)) {
|
|
|
|
debug("Error reading " + aFilePath + ": " + e);
|
|
|
|
deferred.reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
// We want to use the MD5 algorithm.
|
|
|
|
hasher.init(hasher.MD5);
|
|
|
|
|
2014-03-25 17:14:06 +04:00
|
|
|
const PR_UINT32_MAX = 0xffffffff;
|
|
|
|
hasher.updateFromStream(inputStream, PR_UINT32_MAX);
|
2013-11-26 03:14:35 +04:00
|
|
|
|
|
|
|
// Return the two-digit hexadecimal code for a byte.
|
|
|
|
function toHexString(charCode) {
|
|
|
|
return ("0" + charCode.toString(16)).slice(-2);
|
2013-07-31 23:34:19 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
// We're passing false to get the binary hash and not base64.
|
|
|
|
let data = hasher.finish(false);
|
|
|
|
// Convert the binary hash data to a hex string.
|
|
|
|
let hash = [toHexString(data.charCodeAt(i)) for (i in data)].join("");
|
|
|
|
debug("File hash computed: " + hash);
|
2012-11-15 13:35:37 +04:00
|
|
|
|
2014-03-25 17:14:06 +04:00
|
|
|
deferred.resolve(hash);
|
2013-11-26 03:14:35 +04:00
|
|
|
});
|
2014-03-25 17:14:06 +04:00
|
|
|
|
|
|
|
return deferred.promise;
|
2013-11-26 03:14:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an "applied" event right away for the package being installed.
|
|
|
|
*
|
|
|
|
* XXX We use this to exit the app update process early when the downloaded
|
|
|
|
* package is identical to the last one we installed. Presumably we do
|
|
|
|
* something similar after updating the app, and we could refactor both cases
|
|
|
|
* to use the same code to send the "applied" event.
|
|
|
|
*
|
2014-05-24 23:28:13 +04:00
|
|
|
* @param aApp {Object} app data
|
2013-11-26 03:14:35 +04:00
|
|
|
*/
|
2014-05-24 23:28:13 +04:00
|
|
|
_sendAppliedEvent: function(aApp) {
|
|
|
|
aApp.downloading = false;
|
|
|
|
aApp.downloadAvailable = false;
|
|
|
|
aApp.downloadSize = 0;
|
|
|
|
aApp.installState = "installed";
|
|
|
|
aApp.readyToApplyDownload = false;
|
|
|
|
if (aApp.staged && aApp.staged.manifestHash) {
|
2013-11-26 03:14:35 +04:00
|
|
|
// If we're here then the manifest has changed but the package
|
|
|
|
// hasn't. Let's clear this, so we don't keep offering
|
|
|
|
// a bogus update to the user
|
2014-05-24 23:28:13 +04:00
|
|
|
aApp.manifestHash = aApp.staged.manifestHash;
|
|
|
|
aApp.etag = aApp.staged.etag || aApp.etag;
|
|
|
|
aApp.staged = {};
|
|
|
|
// Move the staged update manifest to a non staged one.
|
2014-03-25 17:14:06 +04:00
|
|
|
try {
|
2014-05-24 23:28:13 +04:00
|
|
|
let staged = this._getAppDir(aApp.id);
|
2014-03-25 17:14:06 +04:00
|
|
|
staged.append("staged-update.webapp");
|
|
|
|
staged.moveTo(staged.parent, "update.webapp");
|
|
|
|
} catch (ex) {
|
|
|
|
// We don't really mind much if this fails.
|
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the updated registry, and cleanup the tmp directory.
|
2014-01-17 04:26:24 +04:00
|
|
|
this._saveApps().then(() => {
|
2013-11-26 03:14:35 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
2014-05-24 23:28:13 +04:00
|
|
|
app: aApp,
|
|
|
|
id: aApp.id
|
2013-11-26 03:14:35 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
2014-05-24 23:28:13 +04:00
|
|
|
manifestURL: aApp.manifestURL,
|
2013-11-26 03:14:35 +04:00
|
|
|
eventType: ["downloadsuccess", "downloadapplied"]
|
|
|
|
});
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2014-05-24 23:28:13 +04:00
|
|
|
let file = FileUtils.getFile("TmpD", ["webapps", aApp.id], false);
|
2014-02-21 23:54:03 +04:00
|
|
|
if (file && file.exists()) {
|
|
|
|
file.remove(true);
|
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_openAndReadPackage: function(aZipFile, aOldApp, aNewApp, aIsLocalFileInstall,
|
|
|
|
aIsUpdate, aManifest, aRequestChannel, aHash) {
|
|
|
|
return Task.spawn((function*() {
|
|
|
|
let zipReader, isSigned, newManifest;
|
|
|
|
|
|
|
|
try {
|
2013-11-19 23:41:54 +04:00
|
|
|
[zipReader, isSigned] = yield this._openPackage(aZipFile, aOldApp,
|
|
|
|
aIsLocalFileInstall);
|
2013-11-26 03:14:35 +04:00
|
|
|
newManifest = yield this._readPackage(aOldApp, aNewApp,
|
|
|
|
aIsLocalFileInstall, aIsUpdate, aManifest, aRequestChannel,
|
|
|
|
aHash, zipReader, isSigned);
|
|
|
|
} catch (e) {
|
|
|
|
debug("package open/read error: " + e);
|
|
|
|
// Something bad happened when opening/reading the package.
|
|
|
|
// Unrecoverable error, don't bug the user.
|
|
|
|
// Apps with installState 'pending' does not produce any
|
|
|
|
// notification, so we are safe with its current
|
|
|
|
// downloadAvailable state.
|
|
|
|
if (aOldApp.installState !== "pending") {
|
|
|
|
aOldApp.downloadAvailable = false;
|
|
|
|
}
|
|
|
|
if (typeof e == 'object') {
|
2014-07-17 16:09:07 +04:00
|
|
|
Cu.reportError("Error while reading package: " + e + "\n" + e.stack);
|
2013-11-26 03:14:35 +04:00
|
|
|
throw "INVALID_PACKAGE";
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (zipReader) {
|
|
|
|
zipReader.close();
|
2012-12-11 03:49:02 +04:00
|
|
|
}
|
2012-07-11 19:38:33 +04:00
|
|
|
}
|
2012-11-29 18:19:37 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return newManifest;
|
2013-01-25 06:24:17 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
}).bind(this));
|
|
|
|
},
|
2012-12-14 23:29:25 +04:00
|
|
|
|
2013-11-19 23:41:54 +04:00
|
|
|
_openPackage: function(aZipFile, aApp, aIsLocalFileInstall) {
|
2013-11-26 03:14:35 +04:00
|
|
|
return Task.spawn((function*() {
|
|
|
|
let certDb;
|
|
|
|
try {
|
|
|
|
certDb = Cc["@mozilla.org/security/x509certdb;1"]
|
|
|
|
.getService(Ci.nsIX509CertDB);
|
|
|
|
} catch (e) {
|
|
|
|
debug("nsIX509CertDB error: " + e);
|
|
|
|
// unrecoverable error, don't bug the user
|
|
|
|
aApp.downloadAvailable = false;
|
|
|
|
throw "CERTDB_ERROR";
|
|
|
|
}
|
2013-01-09 19:51:28 +04:00
|
|
|
|
2014-04-28 12:50:00 +04:00
|
|
|
let [result, zipReader] = yield this._openSignedPackage(aApp.installOrigin,
|
|
|
|
aApp.manifestURL,
|
|
|
|
aZipFile,
|
|
|
|
certDb);
|
2013-02-01 23:17:32 +04:00
|
|
|
|
2013-11-19 23:41:54 +04:00
|
|
|
// We cannot really know if the system date is correct or
|
|
|
|
// not. What we can know is if it's after the build date or not,
|
|
|
|
// and assume the build date is correct (which we cannot
|
|
|
|
// really know either).
|
|
|
|
let isLaterThanBuildTime = Date.now() > PLATFORM_BUILD_ID_TIME;
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
let isSigned;
|
2012-12-21 22:28:58 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
if (Components.isSuccessCode(result)) {
|
|
|
|
isSigned = true;
|
2013-12-15 23:03:28 +04:00
|
|
|
} else if (result == Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY ||
|
|
|
|
result == Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY ||
|
|
|
|
result == Cr.NS_ERROR_SIGNED_JAR_ENTRY_MISSING) {
|
2013-11-26 03:14:35 +04:00
|
|
|
throw "APP_PACKAGE_CORRUPTED";
|
2013-12-15 23:03:28 +04:00
|
|
|
} else if (result == Cr.NS_ERROR_FILE_CORRUPTED ||
|
|
|
|
result == Cr.NS_ERROR_SIGNED_JAR_ENTRY_TOO_LARGE ||
|
|
|
|
result == Cr.NS_ERROR_SIGNED_JAR_ENTRY_INVALID ||
|
|
|
|
result == Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID) {
|
|
|
|
throw "APP_PACKAGE_INVALID";
|
2013-11-19 23:41:54 +04:00
|
|
|
} else if ((!aIsLocalFileInstall || isLaterThanBuildTime) &&
|
|
|
|
(result != Cr.NS_ERROR_SIGNED_JAR_NOT_SIGNED)) {
|
2013-11-26 03:14:35 +04:00
|
|
|
throw "INVALID_SIGNATURE";
|
|
|
|
} else {
|
2013-11-19 23:41:54 +04:00
|
|
|
// If it's a localFileInstall and the validation failed
|
|
|
|
// because of a expired certificate, just assume it was valid
|
|
|
|
// and that the error occurred because the system time has not
|
|
|
|
// been set yet.
|
|
|
|
isSigned = (aIsLocalFileInstall &&
|
|
|
|
(getNSPRErrorCode(result) ==
|
|
|
|
SEC_ERROR_EXPIRED_CERTIFICATE));
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
|
|
|
|
.createInstance(Ci.nsIZipReader);
|
|
|
|
zipReader.open(aZipFile);
|
|
|
|
}
|
2012-12-14 08:35:58 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return [zipReader, isSigned];
|
2012-12-14 23:29:25 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
}).bind(this));
|
|
|
|
},
|
2013-02-11 12:38:51 +04:00
|
|
|
|
2014-04-28 12:50:00 +04:00
|
|
|
_openSignedPackage: function(aInstallOrigin, aManifestURL, aZipFile, aCertDb) {
|
2013-11-26 03:14:35 +04:00
|
|
|
let deferred = Promise.defer();
|
2012-11-15 13:35:37 +04:00
|
|
|
|
2014-04-28 12:50:00 +04:00
|
|
|
let root = TrustedRootCertificate.index;
|
|
|
|
|
|
|
|
let useReviewerCerts = false;
|
|
|
|
try {
|
|
|
|
useReviewerCerts = Services.prefs.
|
|
|
|
getBoolPref("dom.mozApps.use_reviewer_certs");
|
|
|
|
} catch (ex) { }
|
|
|
|
|
|
|
|
// We'll use the reviewer and dev certificates only if the pref is set to
|
|
|
|
// true.
|
|
|
|
if (useReviewerCerts) {
|
|
|
|
let manifestPath = Services.io.newURI(aManifestURL, null, null).path;
|
|
|
|
|
|
|
|
switch (aInstallOrigin) {
|
|
|
|
case "https://marketplace.firefox.com":
|
|
|
|
root = manifestPath.startsWith("/reviewers/")
|
|
|
|
? Ci.nsIX509CertDB.AppMarketplaceProdReviewersRoot
|
|
|
|
: Ci.nsIX509CertDB.AppMarketplaceProdPublicRoot;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "https://marketplace-dev.allizom.org":
|
|
|
|
root = manifestPath.startsWith("/reviewers/")
|
|
|
|
? Ci.nsIX509CertDB.AppMarketplaceDevReviewersRoot
|
|
|
|
: Ci.nsIX509CertDB.AppMarketplaceDevPublicRoot;
|
|
|
|
break;
|
2014-07-23 15:20:25 +04:00
|
|
|
|
|
|
|
// The staging server uses the same certificate for both
|
|
|
|
// public and unreviewed apps.
|
|
|
|
case "https://marketplace.allizom.org":
|
|
|
|
root = Ci.nsIX509CertDB.AppMarketplaceStageRoot;
|
|
|
|
break;
|
2014-04-28 12:50:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 02:37:07 +04:00
|
|
|
aCertDb.openSignedAppFileAsync(
|
2014-04-28 12:50:00 +04:00
|
|
|
root, aZipFile,
|
2013-11-26 03:14:35 +04:00
|
|
|
function(aRv, aZipReader) {
|
|
|
|
deferred.resolve([aRv, aZipReader]);
|
2013-03-26 00:03:58 +04:00
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
);
|
2013-03-26 00:03:58 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
return deferred.promise;
|
|
|
|
},
|
2013-07-29 23:53:14 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
_readPackage: function(aOldApp, aNewApp, aIsLocalFileInstall, aIsUpdate,
|
|
|
|
aManifest, aRequestChannel, aHash, aZipReader,
|
|
|
|
aIsSigned) {
|
|
|
|
this._checkSignature(aNewApp, aIsSigned, aIsLocalFileInstall);
|
|
|
|
|
|
|
|
if (!aZipReader.hasEntry("manifest.webapp")) {
|
|
|
|
throw "MISSING_MANIFEST";
|
2013-07-29 23:53:14 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
let istream = aZipReader.getInputStream("manifest.webapp");
|
2013-03-26 00:03:58 +04:00
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
// Obtain a converter to read from a UTF-8 encoded input stream.
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
|
|
|
|
let newManifest = JSON.parse(converter.ConvertToUnicode(
|
|
|
|
NetUtil.readInputStreamToString(istream, istream.available()) || ""));
|
|
|
|
|
|
|
|
if (!AppsUtils.checkManifest(newManifest, aOldApp)) {
|
|
|
|
throw "INVALID_MANIFEST";
|
|
|
|
}
|
|
|
|
|
|
|
|
// For app updates we don't forbid apps to rename themselves but
|
|
|
|
// we still retain the old name of the app. In the future we
|
|
|
|
// will use UI to allow updates to rename an app after we check
|
|
|
|
// with the user that the rename is ok.
|
|
|
|
if (aIsUpdate) {
|
|
|
|
// Call ensureSameAppName before compareManifests as `manifest`
|
|
|
|
// has been normalized to avoid app rename.
|
|
|
|
AppsUtils.ensureSameAppName(aManifest._manifest, newManifest, aOldApp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.compareManifests(newManifest, aManifest._manifest)) {
|
|
|
|
throw "MANIFEST_MISMATCH";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.checkInstallAllowed(newManifest, aNewApp.installOrigin)) {
|
|
|
|
throw "INSTALL_FROM_DENIED";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local file installs can be privileged even without the signature.
|
|
|
|
let maxStatus = aIsSigned || aIsLocalFileInstall
|
|
|
|
? Ci.nsIPrincipal.APP_STATUS_PRIVILEGED
|
|
|
|
: Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
|
2014-08-29 04:20:27 +04:00
|
|
|
let status = AppsUtils.getAppManifestStatus(newManifest);
|
|
|
|
if (status > maxStatus) {
|
2013-11-26 03:14:35 +04:00
|
|
|
throw "INVALID_SECURITY_LEVEL";
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:20:27 +04:00
|
|
|
// Check if the role is allowed for this app.
|
|
|
|
if (!AppsUtils.checkAppRole(newManifest.role, status)) {
|
|
|
|
throw "INVALID_ROLE";
|
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
|
|
|
|
this._saveEtag(aIsUpdate, aOldApp, aRequestChannel, aHash, newManifest);
|
2014-01-22 18:42:56 +04:00
|
|
|
this._checkOrigin(aIsSigned || aIsLocalFileInstall, aOldApp, newManifest,
|
|
|
|
aIsUpdate);
|
2013-11-26 03:14:35 +04:00
|
|
|
this._getIds(aIsSigned, aZipReader, converter, aNewApp, aOldApp, aIsUpdate);
|
|
|
|
|
|
|
|
return newManifest;
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkSignature: function(aApp, aIsSigned, aIsLocalFileInstall) {
|
|
|
|
// XXX Security: You CANNOT safely add a new app store for
|
|
|
|
// installing privileged apps just by modifying this pref and
|
|
|
|
// adding the signing cert for that store to the cert trust
|
|
|
|
// database. *Any* origin listed can install apps signed with
|
|
|
|
// *any* certificate trusted; we don't try to maintain a strong
|
|
|
|
// association between certificate with installOrign. The
|
|
|
|
// expectation here is that in production builds the pref will
|
|
|
|
// contain exactly one origin. However, in custom development
|
|
|
|
// builds it may contain more than one origin so we can test
|
|
|
|
// different stages (dev, staging, prod) of the same app store.
|
|
|
|
//
|
|
|
|
// Only allow signed apps to be installed from a whitelist of
|
|
|
|
// domains, and require all packages installed from any of the
|
|
|
|
// domains on the whitelist to be signed. This is a stopgap until
|
|
|
|
// we have a real story for handling multiple app stores signing
|
|
|
|
// apps.
|
|
|
|
let signedAppOriginsStr =
|
|
|
|
Services.prefs.getCharPref("dom.mozApps.signed_apps_installable_from");
|
|
|
|
// If it's a local install and it's signed then we assume
|
|
|
|
// the app origin is a valid signer.
|
|
|
|
let isSignedAppOrigin = (aIsSigned && aIsLocalFileInstall) ||
|
|
|
|
signedAppOriginsStr.split(",").
|
|
|
|
indexOf(aApp.installOrigin) > -1;
|
|
|
|
if (!aIsSigned && isSignedAppOrigin) {
|
|
|
|
// Packaged apps installed from these origins must be signed;
|
|
|
|
// if not, assume somebody stripped the signature.
|
|
|
|
throw "INVALID_SIGNATURE";
|
|
|
|
} else if (aIsSigned && !isSignedAppOrigin) {
|
|
|
|
// Other origins are *prohibited* from installing signed apps.
|
|
|
|
// One reason is that our app revocation mechanism requires
|
|
|
|
// strong cooperation from the host of the mini-manifest, which
|
|
|
|
// we assume to be under the control of the install origin,
|
|
|
|
// even if it has a different origin.
|
|
|
|
throw "INSTALL_FROM_DENIED";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_saveEtag: function(aIsUpdate, aOldApp, aRequestChannel, aHash, aManifest) {
|
|
|
|
// Save the new Etag for the package.
|
|
|
|
if (aIsUpdate) {
|
|
|
|
if (!aOldApp.staged) {
|
|
|
|
aOldApp.staged = { };
|
2013-03-26 00:03:58 +04:00
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
try {
|
|
|
|
aOldApp.staged.packageEtag = aRequestChannel.getResponseHeader("Etag");
|
|
|
|
} catch(e) { }
|
|
|
|
aOldApp.staged.packageHash = aHash;
|
|
|
|
aOldApp.staged.appStatus = AppsUtils.getAppManifestStatus(aManifest);
|
2013-03-26 00:03:58 +04:00
|
|
|
} else {
|
|
|
|
try {
|
2013-11-26 03:14:35 +04:00
|
|
|
aOldApp.packageEtag = aRequestChannel.getResponseHeader("Etag");
|
|
|
|
} catch(e) { }
|
|
|
|
aOldApp.packageHash = aHash;
|
|
|
|
aOldApp.appStatus = AppsUtils.getAppManifestStatus(aManifest);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkOrigin: function(aIsSigned, aOldApp, aManifest, aIsUpdate) {
|
|
|
|
// Check if the app declares which origin it will use.
|
|
|
|
if (aIsSigned &&
|
|
|
|
aOldApp.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED &&
|
|
|
|
aManifest.origin !== undefined) {
|
|
|
|
let uri;
|
|
|
|
try {
|
|
|
|
uri = Services.io.newURI(aManifest.origin, null, null);
|
|
|
|
} catch(e) {
|
|
|
|
throw "INVALID_ORIGIN";
|
|
|
|
}
|
|
|
|
if (uri.scheme != "app") {
|
|
|
|
throw "INVALID_ORIGIN";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIsUpdate) {
|
|
|
|
// Changing the origin during an update is not allowed.
|
2014-01-22 18:42:56 +04:00
|
|
|
if (uri.prePath != aOldApp.origin) {
|
2013-11-26 03:14:35 +04:00
|
|
|
throw "INVALID_ORIGIN_CHANGE";
|
|
|
|
}
|
|
|
|
// Nothing else to do for an update... since the
|
|
|
|
// origin can't change we don't need to move the
|
|
|
|
// app nor can we have a duplicated origin
|
|
|
|
} else {
|
|
|
|
debug("Setting origin to " + uri.prePath +
|
2014-01-22 18:42:56 +04:00
|
|
|
" for " + aOldApp.manifestURL);
|
2013-11-26 03:14:35 +04:00
|
|
|
let newId = uri.prePath.substring(6); // "app://".length
|
2014-07-15 05:53:48 +04:00
|
|
|
if (newId in this.webapps && this._isLaunchable(this.webapps[newId])) {
|
2013-11-26 03:14:35 +04:00
|
|
|
throw "DUPLICATE_ORIGIN";
|
|
|
|
}
|
|
|
|
aOldApp.origin = uri.prePath;
|
|
|
|
// Update the registry.
|
2014-01-22 18:42:56 +04:00
|
|
|
let oldId = aOldApp.id;
|
2014-07-15 05:53:48 +04:00
|
|
|
|
|
|
|
if (oldId == newId) {
|
|
|
|
// This could happen when we have an app in the registry
|
|
|
|
// that is not launchable. Since the app already has
|
|
|
|
// the correct id, we don't need to change it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
aOldApp.id = newId;
|
|
|
|
this.webapps[newId] = aOldApp;
|
2014-01-22 18:42:56 +04:00
|
|
|
delete this.webapps[oldId];
|
2013-11-26 03:14:35 +04:00
|
|
|
// Rename the directories where the files are installed.
|
2014-02-21 23:54:03 +04:00
|
|
|
[DIRECTORY_NAME, "TmpD"].forEach(function(aDir) {
|
|
|
|
let parent = FileUtils.getDir(aDir, ["webapps"], true, true);
|
|
|
|
let dir = FileUtils.getDir(aDir, ["webapps", oldId], true, true);
|
2013-11-26 03:14:35 +04:00
|
|
|
dir.moveTo(parent, newId);
|
|
|
|
});
|
|
|
|
// Signals that we need to swap the old id with the new app.
|
2014-05-24 23:28:13 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateApp", { oldId: oldId,
|
|
|
|
newId: newId,
|
|
|
|
app: aOldApp });
|
|
|
|
|
2013-11-26 03:14:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getIds: function(aIsSigned, aZipReader, aConverter, aNewApp, aOldApp,
|
|
|
|
aIsUpdate) {
|
|
|
|
// Get ids.json if the file is signed
|
|
|
|
if (aIsSigned) {
|
|
|
|
let idsStream;
|
|
|
|
try {
|
|
|
|
idsStream = aZipReader.getInputStream("META-INF/ids.json");
|
|
|
|
} catch (e) {
|
|
|
|
throw aZipReader.hasEntry("META-INF/ids.json")
|
|
|
|
? e
|
|
|
|
: "MISSING_IDS_JSON";
|
|
|
|
}
|
|
|
|
|
|
|
|
let ids = JSON.parse(aConverter.ConvertToUnicode(NetUtil.
|
|
|
|
readInputStreamToString( idsStream, idsStream.available()) || ""));
|
|
|
|
if ((!ids.id) || !Number.isInteger(ids.version) ||
|
|
|
|
(ids.version <= 0)) {
|
|
|
|
throw "INVALID_IDS_JSON";
|
|
|
|
}
|
|
|
|
let storeId = aNewApp.installOrigin + "#" + ids.id;
|
|
|
|
this._checkForStoreIdMatch(aIsUpdate, aOldApp, storeId, ids.version);
|
|
|
|
aOldApp.storeId = storeId;
|
|
|
|
aOldApp.storeVersion = ids.version;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// aStoreId must be a string of the form
|
|
|
|
// <installOrigin>#<storeId from ids.json>
|
|
|
|
// aStoreVersion must be a positive integer.
|
|
|
|
_checkForStoreIdMatch: function(aIsUpdate, aNewApp, aStoreId, aStoreVersion) {
|
|
|
|
// Things to check:
|
|
|
|
// 1. if it's a update:
|
|
|
|
// a. We should already have this storeId, or the original storeId must
|
|
|
|
// start with STORE_ID_PENDING_PREFIX
|
|
|
|
// b. The manifestURL for the stored app should be the same one we're
|
|
|
|
// updating
|
|
|
|
// c. And finally the version of the update should be higher than the one
|
|
|
|
// on the already installed package
|
|
|
|
// 2. else
|
|
|
|
// a. We should not have this storeId on the list
|
|
|
|
// We're currently launching WRONG_APP_STORE_ID for all the mismatch kind of
|
|
|
|
// errors, and APP_STORE_VERSION_ROLLBACK for the version error.
|
|
|
|
|
|
|
|
// Does an app with this storeID exist already?
|
|
|
|
let appId = this.getAppLocalIdByStoreId(aStoreId);
|
|
|
|
let isInstalled = appId != Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
if (aIsUpdate) {
|
|
|
|
let isDifferent = aNewApp.localId !== appId;
|
|
|
|
let isPending = aNewApp.storeId.indexOf(STORE_ID_PENDING_PREFIX) == 0;
|
|
|
|
|
|
|
|
if ((!isInstalled && !isPending) || (isInstalled && isDifferent)) {
|
|
|
|
throw "WRONG_APP_STORE_ID";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPending && (aNewApp.storeVersion >= aStoreVersion)) {
|
|
|
|
throw "APP_STORE_VERSION_ROLLBACK";
|
2012-11-15 13:35:37 +04:00
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
|
|
|
|
} else if (isInstalled) {
|
|
|
|
throw "WRONG_APP_STORE_ID";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes the directory we created, and sends an error to the DOM side.
|
2014-07-21 13:26:58 +04:00
|
|
|
revertDownloadPackage: function(aId, aOldApp, aNewApp, aIsUpdate, aError) {
|
|
|
|
debug("Error downloading package: " + aError);
|
2014-02-21 23:54:03 +04:00
|
|
|
let dir = FileUtils.getDir("TmpD", ["webapps", aId], true, true);
|
2013-11-26 03:14:35 +04:00
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
// We avoid notifying the error to the DOM side if the app download
|
|
|
|
// was cancelled via cancelDownload, which already sends its own
|
|
|
|
// notification.
|
|
|
|
if (aOldApp.isCanceling) {
|
|
|
|
delete aOldApp.isCanceling;
|
|
|
|
return;
|
2012-11-15 13:35:37 +04:00
|
|
|
}
|
2013-11-26 03:14:35 +04:00
|
|
|
|
|
|
|
let download = AppDownloadManager.get(aNewApp.manifestURL);
|
|
|
|
aOldApp.downloading = false;
|
|
|
|
|
|
|
|
// If there were not enough storage to download the package we
|
|
|
|
// won't have a record of the download details, so we just set the
|
|
|
|
// installState to 'pending' at first download and to 'installed' when
|
|
|
|
// updating.
|
|
|
|
aOldApp.installState = download ? download.previousState
|
|
|
|
: aIsUpdate ? "installed"
|
|
|
|
: "pending";
|
|
|
|
|
|
|
|
if (aOldApp.staged) {
|
|
|
|
delete aOldApp.staged;
|
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._saveApps().then(() => {
|
2013-11-26 03:14:35 +04:00
|
|
|
this.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: aOldApp,
|
|
|
|
error: aError,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: aNewApp.id
|
2013-11-26 03:14:35 +04:00
|
|
|
});
|
|
|
|
this.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: aNewApp.manifestURL
|
|
|
|
});
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2013-11-26 03:14:35 +04:00
|
|
|
AppDownloadManager.remove(aNewApp.manifestURL);
|
2012-07-11 19:38:33 +04:00
|
|
|
},
|
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
doUninstall: Task.async(function*(aData, aMm) {
|
|
|
|
// The yields here could get stuck forever, so we only hold
|
|
|
|
// a weak reference to the message manager while yielding, to avoid
|
|
|
|
// leaking the whole page associationed with the message manager.
|
|
|
|
aMm = Cu.getWeakReference(aMm);
|
2012-08-30 01:20:03 +04:00
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
let response = "Webapps:Uninstall:Return:OK";
|
2012-11-03 07:28:32 +04:00
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
try {
|
|
|
|
aData.app = yield this._getAppWithManifest(aData.manifestURL);
|
|
|
|
|
|
|
|
let prefName = "dom.mozApps.auto_confirm_uninstall";
|
|
|
|
if (Services.prefs.prefHasUserValue(prefName) &&
|
|
|
|
Services.prefs.getBoolPref(prefName)) {
|
|
|
|
yield this._uninstallApp(aData.app);
|
|
|
|
} else {
|
|
|
|
yield this._promptForUninstall(aData);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
aData.error = error;
|
|
|
|
response = "Webapps:Uninstall:Return:KO";
|
|
|
|
}
|
|
|
|
|
2014-08-28 21:35:56 +04:00
|
|
|
if ((aMm = aMm.get())) {
|
2014-06-12 01:23:18 +04:00
|
|
|
aMm.sendAsyncMessage(response, aData);
|
2013-05-06 17:51:53 +04:00
|
|
|
}
|
2014-06-12 01:23:18 +04:00
|
|
|
}),
|
|
|
|
|
|
|
|
uninstall: function(aManifestURL) {
|
|
|
|
return this._getAppWithManifest(aManifestURL)
|
|
|
|
.then(this._uninstallApp.bind(this));
|
|
|
|
},
|
2012-09-28 20:24:45 +04:00
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
_uninstallApp: Task.async(function*(aApp) {
|
|
|
|
if (!aApp.removable) {
|
2013-05-06 17:51:53 +04:00
|
|
|
debug("Error: cannot uninstall a non-removable app.");
|
2014-06-12 01:23:18 +04:00
|
|
|
throw new Error("NON_REMOVABLE_APP");
|
2013-05-06 17:51:53 +04:00
|
|
|
}
|
2012-09-28 20:24:45 +04:00
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
let id = aApp.id;
|
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
// Check if we are downloading something for this app, and cancel the
|
|
|
|
// download if needed.
|
2014-06-12 01:23:18 +04:00
|
|
|
this.cancelDownload(aApp.manifestURL);
|
2012-04-28 11:10:08 +04:00
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
2012-07-20 19:41:30 +04:00
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
// Clear private data first.
|
2014-06-12 01:23:18 +04:00
|
|
|
this._clearPrivateData(aApp.localId, false);
|
2012-07-20 19:41:30 +04:00
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
// Then notify observers.
|
2014-06-12 01:23:18 +04:00
|
|
|
Services.obs.notifyObservers(null, "webapps-uninstall", JSON.stringify(aApp));
|
2012-07-20 19:41:30 +04:00
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
if (supportSystemMessages()) {
|
2014-06-12 01:23:18 +04:00
|
|
|
this._unregisterActivities(aApp.manifest, aApp);
|
2012-07-20 19:41:30 +04:00
|
|
|
}
|
2012-09-28 20:24:45 +04:00
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
let dir = this._getAppDir(id);
|
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
delete this.webapps[id];
|
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
yield this._saveApps();
|
|
|
|
|
|
|
|
this.broadcastMessage("Webapps:Uninstall:Broadcast:Return:OK", aApp);
|
|
|
|
this.broadcastMessage("Webapps:RemoveApp", { id: id });
|
|
|
|
|
|
|
|
return aApp;
|
|
|
|
}),
|
|
|
|
|
|
|
|
_promptForUninstall: function(aData) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
this._pendingUninstalls[aData.requestID] = deferred;
|
|
|
|
Services.obs.notifyObservers(null, "webapps-ask-uninstall",
|
|
|
|
JSON.stringify(aData));
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
confirmUninstall: function(aData) {
|
|
|
|
let pending = this._pendingUninstalls[aData.requestID];
|
|
|
|
if (pending) {
|
|
|
|
delete this._pendingUninstalls[aData.requestID];
|
|
|
|
return this._uninstallApp(aData.app).then(() => {
|
|
|
|
pending.resolve();
|
|
|
|
return aData.app;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.reject(new Error("PENDING_UNINSTALL_NOT_FOUND"));
|
|
|
|
},
|
|
|
|
|
|
|
|
denyUninstall: function(aData, aReason = "ERROR_UNKNOWN_FAILURE") {
|
|
|
|
// Fails to uninstall the desired app because:
|
|
|
|
// - we cannot find the app to be uninstalled.
|
|
|
|
// - the app to be uninstalled is not removable.
|
|
|
|
// - the user declined the confirmation
|
|
|
|
debug("Failed to uninstall app: " + aReason);
|
|
|
|
let pending = this._pendingUninstalls[aData.requestID];
|
|
|
|
if (pending) {
|
|
|
|
delete this._pendingUninstalls[aData.requestID];
|
|
|
|
pending.reject(new Error(aReason));
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return Promise.reject(new Error("PENDING_UNINSTALL_NOT_FOUND"));
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
2011-12-08 17:32:54 +04:00
|
|
|
|
2012-09-18 21:34:55 +04:00
|
|
|
getSelf: function(aData, aMm) {
|
2011-11-29 00:13:26 +04:00
|
|
|
aData.apps = [];
|
2012-09-25 19:04:24 +04:00
|
|
|
|
|
|
|
if (aData.appId == Ci.nsIScriptSecurityManager.NO_APP_ID ||
|
|
|
|
aData.appId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-06 03:26:18 +04:00
|
|
|
let tmp = [];
|
2012-03-06 23:50:58 +04:00
|
|
|
|
2012-09-25 19:04:24 +04:00
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].origin == aData.origin &&
|
|
|
|
this.webapps[id].localId == aData.appId &&
|
2013-08-02 04:00:22 +04:00
|
|
|
this._isLaunchable(this.webapps[id])) {
|
2012-09-25 19:04:24 +04:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
|
|
|
aData.apps.push(app);
|
|
|
|
tmp.push({ id: id });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aData.apps.length) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
|
|
|
return;
|
2011-11-29 00:13:26 +04:00
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2012-03-06 23:50:58 +04:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 21:34:55 +04:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-03-06 23:50:58 +04:00
|
|
|
},
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2012-10-03 00:38:51 +04:00
|
|
|
checkInstalled: function(aData, aMm) {
|
|
|
|
aData.app = null;
|
|
|
|
let tmp = [];
|
2012-09-25 19:04:24 +04:00
|
|
|
|
|
|
|
for (let appId in this.webapps) {
|
2013-09-13 16:07:04 +04:00
|
|
|
if (this.webapps[appId].manifestURL == aData.manifestURL &&
|
|
|
|
this._isLaunchable(this.webapps[appId])) {
|
2012-10-03 00:38:51 +04:00
|
|
|
aData.app = AppsUtils.cloneAppObject(this.webapps[appId]);
|
|
|
|
tmp.push({ id: appId });
|
2012-09-25 19:04:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2012-10-03 00:38:51 +04:00
|
|
|
for (let i = 0; i < aResult.length; i++) {
|
|
|
|
aData.app.manifest = aResult[i].manifest;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckInstalled:Return:OK", aData);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-09-25 19:04:24 +04:00
|
|
|
},
|
|
|
|
|
2012-09-18 21:34:55 +04:00
|
|
|
getInstalled: function(aData, aMm) {
|
2012-03-06 23:50:58 +04:00
|
|
|
aData.apps = [];
|
|
|
|
let tmp = [];
|
|
|
|
|
2012-06-30 00:46:21 +04:00
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].installOrigin == aData.origin &&
|
2013-08-02 04:00:22 +04:00
|
|
|
this._isLaunchable(this.webapps[id])) {
|
2012-08-28 06:43:57 +04:00
|
|
|
aData.apps.push(AppsUtils.cloneAppObject(this.webapps[id]));
|
2012-03-06 23:50:58 +04:00
|
|
|
tmp.push({ id: id });
|
2011-11-29 00:13:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2011-12-06 03:26:18 +04:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 21:34:55 +04:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetInstalled:Return:OK", aData);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
|
|
|
|
2012-09-18 21:34:55 +04:00
|
|
|
getNotInstalled: function(aData, aMm) {
|
2012-06-30 00:46:21 +04:00
|
|
|
aData.apps = [];
|
|
|
|
let tmp = [];
|
|
|
|
|
|
|
|
for (let id in this.webapps) {
|
2013-08-02 04:00:22 +04:00
|
|
|
if (!this._isLaunchable(this.webapps[id])) {
|
2012-08-28 06:43:57 +04:00
|
|
|
aData.apps.push(AppsUtils.cloneAppObject(this.webapps[id]));
|
2012-06-30 00:46:21 +04:00
|
|
|
tmp.push({ id: id });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2012-06-30 00:46:21 +04:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 21:34:55 +04:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetNotInstalled:Return:OK", aData);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2012-06-30 00:46:21 +04:00
|
|
|
},
|
2013-05-06 17:51:53 +04:00
|
|
|
|
|
|
|
getAll: function(aCallback) {
|
|
|
|
debug("getAll");
|
|
|
|
let apps = [];
|
2011-12-06 03:26:18 +04:00
|
|
|
let tmp = [];
|
2011-11-29 00:13:26 +04:00
|
|
|
|
2012-04-28 11:10:08 +04:00
|
|
|
for (let id in this.webapps) {
|
2012-08-28 06:43:57 +04:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
2013-08-02 04:00:22 +04:00
|
|
|
if (!this._isLaunchable(app))
|
2012-06-30 00:46:21 +04:00
|
|
|
continue;
|
|
|
|
|
2013-05-06 17:51:53 +04:00
|
|
|
apps.push(app);
|
2011-12-06 03:26:18 +04:00
|
|
|
tmp.push({ id: id });
|
2011-11-29 00:13:26 +04:00
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2011-12-06 03:26:18 +04:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
2013-05-06 17:51:53 +04:00
|
|
|
apps[i].manifest = aResult[i].manifest;
|
|
|
|
aCallback(apps);
|
2014-01-17 04:26:24 +04:00
|
|
|
});
|
2011-11-29 00:13:26 +04:00
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:26 +04:00
|
|
|
/* Check if |data| is actually a receipt */
|
|
|
|
isReceipt: function(data) {
|
|
|
|
try {
|
|
|
|
// The receipt data shouldn't be too big (allow up to 1 MiB of data)
|
|
|
|
const MAX_RECEIPT_SIZE = 1048576;
|
|
|
|
|
|
|
|
if (data.length > MAX_RECEIPT_SIZE) {
|
|
|
|
return "RECEIPT_TOO_BIG";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marketplace receipts are JWK + "~" + JWT
|
|
|
|
// Other receipts may contain only the JWT
|
|
|
|
let receiptParts = data.split('~');
|
|
|
|
let jwtData = null;
|
|
|
|
if (receiptParts.length == 2) {
|
|
|
|
jwtData = receiptParts[1];
|
|
|
|
} else {
|
|
|
|
jwtData = receiptParts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
let segments = jwtData.split('.');
|
|
|
|
if (segments.length != 3) {
|
|
|
|
return "INVALID_SEGMENTS_NUMBER";
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to translate the base64 alphabet used in JWT to our base64 alphabet
|
|
|
|
// before calling atob.
|
|
|
|
let decodedReceipt = JSON.parse(atob(segments[1].replace(/-/g, '+')
|
|
|
|
.replace(/_/g, '/')));
|
|
|
|
if (!decodedReceipt) {
|
|
|
|
return "INVALID_RECEIPT_ENCODING";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Required values for a receipt
|
|
|
|
if (!decodedReceipt.typ) {
|
|
|
|
return "RECEIPT_TYPE_REQUIRED";
|
|
|
|
}
|
|
|
|
if (!decodedReceipt.product) {
|
|
|
|
return "RECEIPT_PRODUCT_REQUIRED";
|
|
|
|
}
|
|
|
|
if (!decodedReceipt.user) {
|
|
|
|
return "RECEIPT_USER_REQUIRED";
|
|
|
|
}
|
|
|
|
if (!decodedReceipt.iss) {
|
|
|
|
return "RECEIPT_ISS_REQUIRED";
|
|
|
|
}
|
|
|
|
if (!decodedReceipt.nbf) {
|
|
|
|
return "RECEIPT_NBF_REQUIRED";
|
|
|
|
}
|
|
|
|
if (!decodedReceipt.iat) {
|
|
|
|
return "RECEIPT_IAT_REQUIRED";
|
|
|
|
}
|
|
|
|
|
|
|
|
let allowedTypes = [ "purchase-receipt", "developer-receipt",
|
|
|
|
"reviewer-receipt", "test-receipt" ];
|
|
|
|
if (allowedTypes.indexOf(decodedReceipt.typ) < 0) {
|
|
|
|
return "RECEIPT_TYPE_UNSUPPORTED";
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return "RECEIPT_ERROR";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
addReceipt: function(aData, aMm) {
|
|
|
|
debug("addReceipt " + aData.manifestURL);
|
|
|
|
|
|
|
|
let receipt = aData.receipt;
|
|
|
|
|
|
|
|
if (!receipt) {
|
|
|
|
aData.error = "INVALID_PARAMETERS";
|
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = this.isReceipt(receipt);
|
|
|
|
if (error) {
|
|
|
|
aData.error = error;
|
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
|
|
|
|
|
|
|
if (!app.receipts) {
|
|
|
|
app.receipts = [];
|
|
|
|
} else if (app.receipts.length > 500) {
|
|
|
|
aData.error = "TOO_MANY_RECEIPTS";
|
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = app.receipts.indexOf(receipt);
|
|
|
|
if (index >= 0) {
|
|
|
|
aData.error = "RECEIPT_ALREADY_EXISTS";
|
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.receipts.push(receipt);
|
|
|
|
|
2014-01-17 05:00:29 +04:00
|
|
|
this._saveApps().then(() => {
|
2014-01-17 04:26:26 +04:00
|
|
|
aData.receipts = app.receipts;
|
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:OK", aData);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeReceipt: function(aData, aMm) {
|
|
|
|
debug("removeReceipt " + aData.manifestURL);
|
|
|
|
|
|
|
|
let receipt = aData.receipt;
|
|
|
|
|
|
|
|
if (!receipt) {
|
|
|
|
aData.error = "INVALID_PARAMETERS";
|
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
|
|
|
|
|
|
|
if (!app.receipts) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = app.receipts.indexOf(receipt);
|
|
|
|
if (index == -1) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.receipts.splice(index, 1);
|
|
|
|
|
2014-01-17 05:00:29 +04:00
|
|
|
this._saveApps().then(() => {
|
2014-01-17 04:26:26 +04:00
|
|
|
aData.receipts = app.receipts;
|
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:OK", aData);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
replaceReceipt: function(aData, aMm) {
|
|
|
|
debug("replaceReceipt " + aData.manifestURL);
|
|
|
|
|
|
|
|
let oldReceipt = aData.oldReceipt;
|
|
|
|
let newReceipt = aData.newReceipt;
|
|
|
|
|
|
|
|
if (!oldReceipt || !newReceipt) {
|
|
|
|
aData.error = "INVALID_PARAMETERS";
|
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = this.isReceipt(newReceipt);
|
|
|
|
if (error) {
|
|
|
|
aData.error = error;
|
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
|
|
|
|
|
|
|
if (!app.receipts) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let oldIndex = app.receipts.indexOf(oldReceipt);
|
|
|
|
if (oldIndex == -1) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.receipts[oldIndex] = newReceipt;
|
|
|
|
|
2014-01-17 05:00:29 +04:00
|
|
|
this._saveApps().then(() => {
|
2014-01-17 04:26:26 +04:00
|
|
|
aData.receipts = app.receipts;
|
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:OK", aData);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
getManifestFor: function(aManifestURL) {
|
2013-08-28 16:10:55 +04:00
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
2012-11-30 20:57:45 +04:00
|
|
|
let app = this.webapps[id];
|
|
|
|
if (!id || (app.installState == "pending" && !app.retryingDownload)) {
|
2014-01-17 04:26:24 +04:00
|
|
|
return Promise.resolve(null);
|
2011-12-06 03:26:18 +04:00
|
|
|
}
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
return this._readManifests([{ id: id }]).then((aResult) => {
|
|
|
|
return aResult[0].manifest;
|
2011-12-06 03:26:18 +04:00
|
|
|
});
|
2011-12-08 17:32:54 +04:00
|
|
|
},
|
|
|
|
|
2012-05-16 14:40:47 +04:00
|
|
|
getAppByManifestURL: function(aManifestURL) {
|
2012-08-28 06:43:57 +04:00
|
|
|
return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL);
|
2012-05-16 14:40:47 +04:00
|
|
|
},
|
2012-07-09 14:25:41 +04:00
|
|
|
|
2014-06-12 01:23:18 +04:00
|
|
|
_getAppWithManifest: Task.async(function*(aManifestURL) {
|
|
|
|
let app = this.getAppByManifestURL(aManifestURL);
|
|
|
|
if (!app) {
|
|
|
|
throw new Error("NO_SUCH_APP");
|
|
|
|
}
|
|
|
|
|
|
|
|
app.manifest = ( yield this._readManifests([{ id: app.id }]) )[0].manifest;
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}),
|
|
|
|
|
2012-10-19 14:43:17 +04:00
|
|
|
getCSPByLocalId: function(aLocalId) {
|
|
|
|
debug("getCSPByLocalId:" + aLocalId);
|
|
|
|
return AppsUtils.getCSPByLocalId(this.webapps, aLocalId);
|
|
|
|
},
|
|
|
|
|
2013-01-25 13:57:14 +04:00
|
|
|
getAppLocalIdByStoreId: function(aStoreId) {
|
|
|
|
debug("getAppLocalIdByStoreId:" + aStoreId);
|
|
|
|
return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId);
|
|
|
|
},
|
|
|
|
|
2012-08-08 20:41:47 +04:00
|
|
|
getAppByLocalId: function(aLocalId) {
|
2012-08-28 06:43:57 +04:00
|
|
|
return AppsUtils.getAppByLocalId(this.webapps, aLocalId);
|
2012-08-08 20:41:47 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getManifestURLByLocalId: function(aLocalId) {
|
2012-08-28 06:43:57 +04:00
|
|
|
return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId);
|
2012-08-08 20:41:47 +04:00
|
|
|
},
|
|
|
|
|
2012-07-09 14:25:41 +04:00
|
|
|
getAppLocalIdByManifestURL: function(aManifestURL) {
|
2012-08-28 06:43:57 +04:00
|
|
|
return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL);
|
2012-07-09 14:25:41 +04:00
|
|
|
},
|
|
|
|
|
2012-12-22 17:56:21 +04:00
|
|
|
getCoreAppsBasePath: function() {
|
2013-03-15 18:18:58 +04:00
|
|
|
return AppsUtils.getCoreAppsBasePath();
|
2012-12-22 17:56:21 +04:00
|
|
|
},
|
|
|
|
|
2014-01-17 04:26:24 +04:00
|
|
|
getWebAppsBasePath: function() {
|
|
|
|
return OS.Path.dirname(this.appsFile);
|
2012-12-22 17:56:21 +04:00
|
|
|
},
|
|
|
|
|
2013-08-02 04:00:22 +04:00
|
|
|
_isLaunchable: function(aApp) {
|
2012-06-30 00:46:21 +04:00
|
|
|
if (this.allAppsLaunchable)
|
|
|
|
return true;
|
|
|
|
|
2013-08-02 04:00:22 +04:00
|
|
|
return WebappOSUtils.isLaunchable(aApp);
|
2012-09-27 02:03:25 +04:00
|
|
|
},
|
|
|
|
|
2013-05-20 21:20:19 +04:00
|
|
|
_notifyCategoryAndObservers: function(subject, topic, data, msg) {
|
2012-09-27 02:03:25 +04:00
|
|
|
const serviceMarker = "service,";
|
|
|
|
|
|
|
|
// First create observers from the category manager.
|
|
|
|
let cm =
|
|
|
|
Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
|
|
|
|
let enumerator = cm.enumerateCategory(topic);
|
|
|
|
|
|
|
|
let observers = [];
|
|
|
|
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
let entry =
|
|
|
|
enumerator.getNext().QueryInterface(Ci.nsISupportsCString).data;
|
|
|
|
let contractID = cm.getCategoryEntry(topic, entry);
|
|
|
|
|
|
|
|
let factoryFunction;
|
|
|
|
if (contractID.substring(0, serviceMarker.length) == serviceMarker) {
|
|
|
|
contractID = contractID.substring(serviceMarker.length);
|
|
|
|
factoryFunction = "getService";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
factoryFunction = "createInstance";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let handler = Cc[contractID][factoryFunction]();
|
|
|
|
if (handler) {
|
|
|
|
let observer = handler.QueryInterface(Ci.nsIObserver);
|
|
|
|
observers.push(observer);
|
|
|
|
}
|
|
|
|
} catch(e) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next enumerate the registered observers.
|
|
|
|
enumerator = Services.obs.enumerateObservers(topic);
|
|
|
|
while (enumerator.hasMoreElements()) {
|
2012-09-28 20:24:45 +04:00
|
|
|
try {
|
|
|
|
let observer = enumerator.getNext().QueryInterface(Ci.nsIObserver);
|
|
|
|
if (observers.indexOf(observer) == -1) {
|
|
|
|
observers.push(observer);
|
|
|
|
}
|
|
|
|
} catch (e) { }
|
2012-09-27 02:03:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
observers.forEach(function (observer) {
|
|
|
|
try {
|
|
|
|
observer.observe(subject, topic, data);
|
|
|
|
} catch(e) { }
|
|
|
|
});
|
2013-05-20 21:20:19 +04:00
|
|
|
// Send back an answer to the child.
|
2013-06-07 20:52:58 +04:00
|
|
|
if (msg) {
|
|
|
|
ppmm.broadcastAsyncMessage("Webapps:ClearBrowserData:Return", msg);
|
|
|
|
}
|
2012-09-27 02:03:25 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
registerBrowserElementParentForApp: function(bep, appId) {
|
|
|
|
let mm = bep._mm;
|
|
|
|
|
|
|
|
// Make a listener function that holds on to this appId.
|
|
|
|
let listener = this.receiveAppMessage.bind(this, appId);
|
|
|
|
|
|
|
|
this.frameMessages.forEach(function(msgName) {
|
|
|
|
mm.addMessageListener(msgName, listener);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveAppMessage: function(appId, message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "Webapps:ClearBrowserData":
|
2013-05-20 21:20:19 +04:00
|
|
|
this._clearPrivateData(appId, true, message.data);
|
2012-09-27 02:03:25 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-09-28 20:24:45 +04:00
|
|
|
},
|
|
|
|
|
2013-05-20 21:20:19 +04:00
|
|
|
_clearPrivateData: function(appId, browserOnly, msg) {
|
2012-09-28 20:24:45 +04:00
|
|
|
let subject = {
|
|
|
|
appId: appId,
|
|
|
|
browserOnly: browserOnly,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams])
|
|
|
|
};
|
2013-05-20 21:20:19 +04:00
|
|
|
this._notifyCategoryAndObservers(subject, "webapps-clear-data", null, msg);
|
2012-04-28 11:10:08 +04:00
|
|
|
}
|
2011-11-29 00:13:26 +04:00
|
|
|
};
|
|
|
|
|
2012-06-11 22:41:46 +04:00
|
|
|
/**
|
|
|
|
* Appcache download observer
|
|
|
|
*/
|
2012-07-03 04:16:55 +04:00
|
|
|
let AppcacheObserver = function(aApp) {
|
2012-11-27 08:03:43 +04:00
|
|
|
debug("Creating AppcacheObserver for " + aApp.origin +
|
|
|
|
" - " + aApp.installState);
|
2012-06-11 22:41:46 +04:00
|
|
|
this.app = aApp;
|
2012-09-27 05:01:20 +04:00
|
|
|
this.startStatus = aApp.installState;
|
2012-12-13 21:54:49 +04:00
|
|
|
this.lastProgressTime = 0;
|
2013-10-17 16:47:58 +04:00
|
|
|
// Send a first progress event to correctly set the DOM object's properties.
|
2013-02-11 12:38:51 +04:00
|
|
|
this._sendProgressEvent();
|
2012-06-11 22:41:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
AppcacheObserver.prototype = {
|
|
|
|
// nsIOfflineCacheUpdateObserver implementation
|
2013-02-11 12:38:51 +04:00
|
|
|
_sendProgressEvent: function() {
|
|
|
|
let app = this.app;
|
2013-10-17 16:47:58 +04:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "progress",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2013-02-11 12:38:51 +04:00
|
|
|
},
|
|
|
|
|
2012-06-11 22:41:46 +04:00
|
|
|
updateStateChanged: function appObs_Update(aUpdate, aState) {
|
|
|
|
let mustSave = false;
|
|
|
|
let app = this.app;
|
|
|
|
|
2012-11-27 08:03:43 +04:00
|
|
|
debug("Offline cache state change for " + app.origin + " : " + aState);
|
|
|
|
|
2013-02-11 12:38:51 +04:00
|
|
|
var self = this;
|
2013-01-11 14:58:33 +04:00
|
|
|
let setStatus = function appObs_setStatus(aStatus, aProgress) {
|
2013-02-11 12:38:51 +04:00
|
|
|
debug("Offlinecache setStatus to " + aStatus + " with progress " +
|
2013-10-17 16:47:58 +04:00
|
|
|
aProgress + " for " + app.origin);
|
2012-09-27 05:01:20 +04:00
|
|
|
mustSave = (app.installState != aStatus);
|
2013-10-17 16:47:58 +04:00
|
|
|
|
2012-09-27 05:01:20 +04:00
|
|
|
app.installState = aStatus;
|
2013-01-11 14:58:33 +04:00
|
|
|
app.progress = aProgress;
|
2013-10-17 16:47:58 +04:00
|
|
|
if (aStatus != "installed") {
|
|
|
|
self._sendProgressEvent();
|
|
|
|
return;
|
2012-11-29 18:19:37 +04:00
|
|
|
}
|
2013-10-17 16:47:58 +04:00
|
|
|
|
|
|
|
app.updateTime = Date.now();
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: ["downloadsuccess", "downloadapplied"],
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2012-09-27 05:01:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let setError = function appObs_setError(aError) {
|
2012-11-27 08:03:43 +04:00
|
|
|
debug("Offlinecache setError to " + aError);
|
2012-10-04 22:18:39 +04:00
|
|
|
app.downloading = false;
|
2014-06-21 03:32:15 +04:00
|
|
|
mustSave = true;
|
|
|
|
|
|
|
|
// If we are canceling the download, we already send a DOWNLOAD_CANCELED
|
|
|
|
// error.
|
|
|
|
if (app.isCanceling) {
|
|
|
|
delete app.isCanceling;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-17 16:47:58 +04:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
2014-05-10 12:16:07 +04:00
|
|
|
error: aError,
|
2014-05-24 23:28:13 +04:00
|
|
|
id: app.id
|
2013-10-17 16:47:58 +04:00
|
|
|
});
|
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:FireEvent", {
|
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2012-06-11 22:41:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (aState) {
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ERROR:
|
|
|
|
aUpdate.removeObserver(this);
|
2013-01-25 06:24:17 +04:00
|
|
|
AppDownloadManager.remove(app.manifestURL);
|
2012-09-27 05:01:20 +04:00
|
|
|
setError("APP_CACHE_DOWNLOAD_ERROR");
|
2012-06-11 22:41:46 +04:00
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_NOUPDATE:
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_FINISHED:
|
|
|
|
aUpdate.removeObserver(this);
|
2013-01-25 06:24:17 +04:00
|
|
|
AppDownloadManager.remove(app.manifestURL);
|
2013-01-11 14:58:33 +04:00
|
|
|
setStatus("installed", aUpdate.byteProgress);
|
2012-06-11 22:41:46 +04:00
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_DOWNLOADING:
|
2013-01-11 14:58:33 +04:00
|
|
|
setStatus(this.startStatus, aUpdate.byteProgress);
|
2012-06-11 22:41:46 +04:00
|
|
|
break;
|
2013-10-17 16:47:58 +04:00
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMSTARTED:
|
2012-12-13 21:54:49 +04:00
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMPROGRESS:
|
|
|
|
let now = Date.now();
|
|
|
|
if (now - this.lastProgressTime > MIN_PROGRESS_EVENT_DELAY) {
|
2013-01-11 14:58:33 +04:00
|
|
|
setStatus(this.startStatus, aUpdate.byteProgress);
|
2012-12-13 21:54:49 +04:00
|
|
|
this.lastProgressTime = now;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-11 22:41:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Status changed, update the stored version.
|
|
|
|
if (mustSave) {
|
|
|
|
DOMApplicationRegistry._saveApps();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applicationCacheAvailable: function appObs_CacheAvail(aApplicationCache) {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 00:13:26 +04:00
|
|
|
DOMApplicationRegistry.init();
|