2012-03-06 19:50:58 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-07-03 00:16:55 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-04-28 07:10:08 +00:00
|
|
|
const Cu = Components.utils;
|
2011-11-28 20:13:26 +00:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2012-07-11 15:38:33 +00:00
|
|
|
const Cr = Components.results;
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2013-11-19 19:41:54 +00: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 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry"];
|
2011-11-28 20:13:26 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2011-12-06 04:22:01 +00:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2012-07-20 15:41:30 +00:00
|
|
|
Cu.import('resource://gre/modules/ActivitiesService.jsm');
|
2012-08-28 02:43:57 +00:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2012-12-21 18:28:55 +00:00
|
|
|
Cu.import("resource://gre/modules/AppDownloadManager.jsm");
|
2013-08-28 21:35:34 +00:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2013-11-25 23:14:35 +00:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2015-08-06 03:30:55 +00:00
|
|
|
Cu.import("resource://gre/modules/MessageBroadcaster.jsm");
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2014-12-16 23:32:28 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "UserCustomizations", function() {
|
|
|
|
let enabled = false;
|
|
|
|
try {
|
|
|
|
enabled = Services.prefs.getBoolPref("dom.apps.customization.enabled");
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
return Cu.import("resource://gre/modules/UserCustomizations.jsm", {})
|
|
|
|
.UserCustomizations;
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
register: function() {},
|
|
|
|
unregister: function() {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-21 17:53:55 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TrustedRootCertificate",
|
|
|
|
"resource://gre/modules/StoreTrustAnchor.jsm");
|
|
|
|
|
2014-03-24 13:37:27 +00: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 05:03:03 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ScriptPreloader",
|
|
|
|
"resource://gre/modules/ScriptPreloader.jsm");
|
|
|
|
|
2015-01-10 23:00:27 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Langpacks",
|
|
|
|
"resource://gre/modules/Langpacks.jsm");
|
|
|
|
|
2014-10-15 05:55:14 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ImportExport",
|
|
|
|
"resource://gre/modules/ImportExport.jsm");
|
|
|
|
|
2015-08-25 20:42:24 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AppConstants",
|
|
|
|
"resource://gre/modules/AppConstants.jsm");
|
|
|
|
|
2015-09-14 16:30:28 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Messaging",
|
|
|
|
"resource://gre/modules/Messaging.jsm");
|
|
|
|
|
2013-05-06 17:41:07 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "libcutils", function() {
|
|
|
|
Cu.import("resource://gre/modules/systemlibs.js");
|
|
|
|
return libcutils;
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 20:15:54 +00: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 00:27:34 +00:00
|
|
|
// We always report debug messages on Android because it's unnecessary
|
|
|
|
// to restrict reporting, per bug 1003469.
|
2015-09-15 18:19:45 +00:00
|
|
|
var debug = Cu.import("resource://gre/modules/AndroidLog.jsm", {})
|
2014-05-09 20:15:54 +00:00
|
|
|
.AndroidLog.d.bind(null, "Webapps");
|
|
|
|
#else
|
2014-07-01 00:27:34 +00:00
|
|
|
// Elsewhere, report debug messages only if dom.mozApps.debug is set to true.
|
2015-09-15 18:19:45 +00:00
|
|
|
var debug;
|
2015-08-30 20:30:21 +00:00
|
|
|
function debugPrefObserver() {
|
|
|
|
debug = Services.prefs.getBoolPref("dom.mozApps.debug")
|
|
|
|
? (aMsg) => dump("-*- Webapps.jsm : " + aMsg + "\n")
|
|
|
|
: (aMsg) => {};
|
|
|
|
}
|
|
|
|
debugPrefObserver();
|
|
|
|
Services.prefs.addObserver("dom.mozApps.debug", debugPrefObserver, false);
|
2014-05-09 20:15:54 +00:00
|
|
|
#endif
|
2012-09-27 01:01:20 +00:00
|
|
|
|
2013-11-19 19:41:54 +00:00
|
|
|
function getNSPRErrorCode(err) {
|
|
|
|
return -1 * ((err) & 0xffff);
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:41:55 +00:00
|
|
|
function supportUseCurrentProfile() {
|
|
|
|
return Services.prefs.getBoolPref("dom.webapps.useCurrentProfile");
|
|
|
|
}
|
|
|
|
|
2013-04-30 13:01:46 +00:00
|
|
|
function supportSystemMessages() {
|
2013-07-07 20:41:55 +00:00
|
|
|
return Services.prefs.getBoolPref("dom.sysmsg.enabled");
|
2013-04-30 13:01:46 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 17:54:49 +00:00
|
|
|
// Minimum delay between two progress events while downloading, in ms.
|
2013-10-17 12:47:58 +00:00
|
|
|
const MIN_PROGRESS_EVENT_DELAY = 1500;
|
2012-12-13 17:54:49 +00:00
|
|
|
|
2012-04-17 14:11:53 +00:00
|
|
|
const WEBAPP_RUNTIME = Services.appinfo.ID == "webapprt@mozilla.org";
|
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
const chromeWindowType = WEBAPP_RUNTIME ? "webapprt:webapp" : "navigator:browser";
|
|
|
|
|
2012-08-27 14:13:02 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageBroadcaster");
|
2012-03-06 19:50:58 +00:00
|
|
|
|
2012-07-20 15:41:30 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
2012-08-27 14:13:02 +00:00
|
|
|
"nsIMessageSender");
|
2012-07-20 15:41:30 +00:00
|
|
|
|
2013-08-16 09:48:37 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "interAppCommService", function() {
|
|
|
|
return Cc["@mozilla.org/inter-app-communication-service;1"]
|
|
|
|
.getService(Ci.nsIInterAppCommService);
|
|
|
|
});
|
|
|
|
|
2013-10-02 17:27:07 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "dataStoreService",
|
|
|
|
"@mozilla.org/datastore-service;1",
|
|
|
|
"nsIDataStoreService");
|
|
|
|
|
2014-11-02 19:04:08 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "appsService",
|
|
|
|
"@mozilla.org/AppsService;1",
|
|
|
|
"nsIAppsService");
|
|
|
|
|
2012-07-03 00:16:55 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "msgmgr", function() {
|
2012-07-11 15:38:33 +00:00
|
|
|
return Cc["@mozilla.org/system-message-internal;1"]
|
|
|
|
.getService(Ci.nsISystemMessagesInternal);
|
2012-07-03 00:16:55 +00:00
|
|
|
});
|
|
|
|
|
2012-12-05 16:50:32 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "updateSvc", function() {
|
|
|
|
return Cc["@mozilla.org/offlinecacheupdate-service;1"]
|
|
|
|
.getService(Ci.nsIOfflineCacheUpdateService);
|
|
|
|
});
|
|
|
|
|
2014-11-02 19:04:08 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "permMgr", function() {
|
|
|
|
return Cc["@mozilla.org/permissionmanager;1"]
|
|
|
|
.getService(Ci.nsIPermissionManager);
|
|
|
|
});
|
|
|
|
|
2012-03-23 23:39:15 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2014-02-21 19:54:03 +00:00
|
|
|
const DIRECTORY_NAME = "webappsDir";
|
2012-08-15 21:02:15 +00:00
|
|
|
#elifdef ANDROID
|
2014-02-21 19:54:03 +00:00
|
|
|
const DIRECTORY_NAME = "webappsDir";
|
2012-03-23 23:39:15 +00:00
|
|
|
#else
|
2012-04-17 14:11:53 +00: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 19:54:03 +00:00
|
|
|
const DIRECTORY_NAME = WEBAPP_RUNTIME ? "WebappRegD" : "ProfD";
|
2012-03-23 23:39:15 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-26 12:18:34 +00: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 16:13:28 +00:00
|
|
|
this.DOMApplicationRegistry = {
|
2014-08-01 17:41:52 +00:00
|
|
|
// pseudo-constants for the different application kinds.
|
2015-09-23 09:39:13 +00:00
|
|
|
get kPackaged() { return "packaged"; },
|
|
|
|
get kHosted() { return "hosted"; },
|
|
|
|
get kHostedAppcache() { return "hosted-appcache"; },
|
|
|
|
get kAndroid() { return "android-native"; },
|
2014-08-01 17:41:52 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// Path to the webapps.json file where we store the registry data.
|
2011-11-28 20:13:26 +00:00
|
|
|
appsFile: null,
|
|
|
|
webapps: { },
|
2012-06-29 20:46:21 +00:00
|
|
|
allAppsLaunchable: false,
|
2014-03-15 21:37:37 +00:00
|
|
|
_updateHandlers: [ ],
|
2014-06-11 21:23:18 +00:00
|
|
|
_pendingUninstalls: {},
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
_contentActions: new Map(),
|
2014-10-15 05:55:14 +00:00
|
|
|
dirKey: DIRECTORY_NAME,
|
2011-11-28 20:13:26 +00:00
|
|
|
|
|
|
|
init: function() {
|
2015-06-15 17:16:00 +00:00
|
|
|
// Keep the messages in sync with the lazy-loading in browser.js (bug 1171013).
|
2014-10-23 23:40:08 +00:00
|
|
|
this.messages = ["Webapps:Install",
|
|
|
|
"Webapps:Uninstall",
|
|
|
|
"Webapps:GetSelf",
|
|
|
|
"Webapps:CheckInstalled",
|
|
|
|
"Webapps:GetInstalled",
|
|
|
|
"Webapps:GetNotInstalled",
|
2014-07-07 18:01:30 +00:00
|
|
|
"Webapps:Launch",
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
"Webapps:LocationChange",
|
2013-01-31 21:35:13 +00:00
|
|
|
"Webapps:InstallPackage",
|
2014-10-23 23:40:08 +00:00
|
|
|
"Webapps:GetList",
|
|
|
|
"Webapps:RegisterForMessages",
|
2012-10-19 18:17:05 +00:00
|
|
|
"Webapps:UnregisterForMessages",
|
2014-10-23 23:40:08 +00:00
|
|
|
"Webapps:CancelDownload",
|
|
|
|
"Webapps:CheckForUpdate",
|
|
|
|
"Webapps:Download",
|
|
|
|
"Webapps:ApplyDownload",
|
|
|
|
"Webapps:Install:Return:Ack",
|
|
|
|
"Webapps:AddReceipt",
|
|
|
|
"Webapps:RemoveReceipt",
|
|
|
|
"Webapps:ReplaceReceipt",
|
2014-09-24 06:14:54 +00:00
|
|
|
"Webapps:RegisterBEP",
|
2014-10-23 23:40:08 +00:00
|
|
|
"Webapps:Export",
|
|
|
|
"Webapps:Import",
|
2014-12-18 22:40:41 +00:00
|
|
|
"Webapps:GetIcon",
|
2014-10-15 05:55:14 +00:00
|
|
|
"Webapps:ExtractManifest",
|
2014-10-23 23:40:08 +00:00
|
|
|
"Webapps:SetEnabled",
|
2012-10-01 20:29:59 +00:00
|
|
|
"child-process-shutdown"];
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-09-26 22:03:25 +00:00
|
|
|
this.frameMessages = ["Webapps:ClearBrowserData"];
|
|
|
|
|
2012-03-13 00:33:10 +00:00
|
|
|
this.messages.forEach((function(msgName) {
|
2012-03-06 19:50:58 +00:00
|
|
|
ppmm.addMessageListener(msgName, this);
|
2011-11-28 20:13:26 +00:00
|
|
|
}).bind(this));
|
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
cpmm.addMessageListener("Activities:Register:OK", this);
|
2014-07-14 21:27:57 +00:00
|
|
|
cpmm.addMessageListener("Activities:Register:KO", this);
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2012-03-13 00:33:10 +00:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2014-01-06 21:03:50 +00:00
|
|
|
Services.obs.addObserver(this, "memory-pressure", false);
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.registerCancelFunction(this.cancelDownload.bind(this));
|
|
|
|
|
2014-02-21 19:54:03 +00:00
|
|
|
this.appsFile = FileUtils.getFile(DIRECTORY_NAME,
|
|
|
|
["webapps", "webapps.json"], true).path;
|
2011-12-06 04:22:01 +00:00
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
this.loadAndUpdateApps();
|
2015-01-10 23:00:27 +00:00
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
Langpacks.registerRegistryFunctions(MessageBroadcaster.broadcastMessage.bind(MessageBroadcaster),
|
2015-03-06 06:43:57 +00:00
|
|
|
this._appIdForManifestURL.bind(this),
|
|
|
|
this.getFullAppByManifestURL.bind(this));
|
2015-08-06 03:30:55 +00:00
|
|
|
|
|
|
|
MessageBroadcaster.init(this.getAppByManifestURL);
|
2015-09-14 16:30:28 +00:00
|
|
|
|
|
|
|
if (AppConstants.MOZ_B2GDROID) {
|
|
|
|
Cu.import("resource://gre/modules/AndroidUtils.jsm");
|
|
|
|
AndroidUtils.init(this);
|
|
|
|
}
|
2012-09-19 16:28:55 +00:00
|
|
|
},
|
2012-08-29 22:58:31 +00:00
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
// loads the current registry, that could be empty on first run.
|
2014-01-17 00:26:24 +00:00
|
|
|
loadCurrentRegistry: function() {
|
2014-03-25 13:14:06 +00:00
|
|
|
return AppsUtils.loadJSONAsync(this.appsFile).then((aData) => {
|
2014-01-17 00:26:24 +00:00
|
|
|
if (!aData) {
|
2014-02-21 19:54:03 +00:00
|
|
|
return;
|
2014-01-17 00:26:24 +00:00
|
|
|
}
|
2012-12-22 13:56:21 +00:00
|
|
|
|
2014-01-17 00:26:24 +00: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 13:56:21 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
app.id = id;
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// Make sure we have a localId
|
|
|
|
if (app.localId === undefined) {
|
|
|
|
app.localId = this._nextLocalId();
|
|
|
|
}
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
if (app.basePath === undefined) {
|
|
|
|
app.basePath = appDir;
|
|
|
|
}
|
2012-08-29 21:20:03 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// Default to removable apps.
|
|
|
|
if (app.removable === undefined) {
|
|
|
|
app.removable = true;
|
|
|
|
}
|
2012-12-10 23:49:02 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// Default to a non privileged status.
|
|
|
|
if (app.appStatus === undefined) {
|
|
|
|
app.appStatus = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
}
|
2013-01-06 23:56:40 +00:00
|
|
|
|
2014-01-17 00:26:24 +00: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 06:50:28 +00:00
|
|
|
|
2014-01-17 00:26:24 +00: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 09:57:14 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// Default storeId to "" and storeVersion to 0
|
2014-08-01 17:41:52 +00:00
|
|
|
if (app.storeId === undefined) {
|
|
|
|
app.storeId = "";
|
2014-01-17 00:26:24 +00:00
|
|
|
}
|
2014-08-01 17:41:52 +00:00
|
|
|
if (app.storeVersion === undefined) {
|
|
|
|
app.storeVersion = 0;
|
2014-01-17 00:26:24 +00:00
|
|
|
}
|
2013-09-11 12:15:48 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// Default role to "".
|
2014-08-01 17:41:52 +00:00
|
|
|
if (app.role === undefined) {
|
|
|
|
app.role = "";
|
2012-09-19 16:28:55 +00:00
|
|
|
}
|
2014-01-17 00:26:24 +00:00
|
|
|
|
2014-08-19 14:14:08 +00:00
|
|
|
if (app.widgetPages === undefined) {
|
|
|
|
app.widgetPages = [];
|
|
|
|
}
|
|
|
|
|
2014-08-29 00:20:27 +00:00
|
|
|
if (!AppsUtils.checkAppRole(app.role, app.appStatus)) {
|
|
|
|
delete this.webapps[id];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-23 23:40:08 +00:00
|
|
|
if (app.enabled === undefined) {
|
|
|
|
app.enabled = true;
|
|
|
|
}
|
|
|
|
|
2015-10-29 14:32:50 +00:00
|
|
|
if (app.blockedStatus === undefined) {
|
|
|
|
app.blockedStatus = Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00: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 16:28:55 +00:00
|
|
|
},
|
|
|
|
|
2012-10-17 04:30:43 +00:00
|
|
|
// Notify we are starting with registering apps.
|
2014-01-22 18:06:29 +00:00
|
|
|
_registryStarted: Promise.defer(),
|
2012-10-17 04:30:43 +00:00
|
|
|
notifyAppsRegistryStart: function notifyAppsRegistryStart() {
|
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-start", null);
|
2014-01-22 18:06:29 +00:00
|
|
|
this._registryStarted.resolve();
|
|
|
|
},
|
|
|
|
|
|
|
|
get registryStarted() {
|
|
|
|
return this._registryStarted.promise;
|
2012-10-17 04:30:43 +00:00
|
|
|
},
|
|
|
|
|
2014-07-14 21:27:57 +00:00
|
|
|
// The registry will be safe to clone when this promise is resolved.
|
|
|
|
_safeToClone: Promise.defer(),
|
|
|
|
|
2012-10-17 04:30:43 +00:00
|
|
|
// Notify we are done with registering apps and save a copy of the registry.
|
2014-01-22 18:06:29 +00:00
|
|
|
_registryReady: Promise.defer(),
|
2012-10-17 04:30:43 +00:00
|
|
|
notifyAppsRegistryReady: function notifyAppsRegistryReady() {
|
2014-07-14 21:27:57 +00:00
|
|
|
// Usually this promise will be resolved earlier, but just in case,
|
|
|
|
// resolve it here also.
|
|
|
|
this._safeToClone.resolve();
|
2014-01-22 18:06:29 +00:00
|
|
|
this._registryReady.resolve();
|
2012-09-19 16:28:55 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-ready", null);
|
|
|
|
this._saveApps();
|
|
|
|
},
|
|
|
|
|
2014-01-22 18:06:29 +00:00
|
|
|
get registryReady() {
|
|
|
|
return this._registryReady.promise;
|
|
|
|
},
|
|
|
|
|
2014-07-14 21:27:57 +00:00
|
|
|
get safeToClone() {
|
|
|
|
return this._safeToClone.promise;
|
|
|
|
},
|
|
|
|
|
2013-05-14 19:00:09 +00: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 14:14:08 +00:00
|
|
|
_saveWidgetsFullPath: function(aManifest, aDestApp) {
|
|
|
|
if (aManifest.widgetPages) {
|
2014-11-18 11:28:41 +00:00
|
|
|
let resolve = (aPage)=>{
|
|
|
|
let filepath = AppsUtils.getFilePath(aPage);
|
2015-05-26 22:59:00 +00:00
|
|
|
return aManifest.resolveURL(filepath);
|
2014-11-18 11:28:41 +00:00
|
|
|
};
|
|
|
|
aDestApp.widgetPages = aManifest.widgetPages.map(resolve);
|
2014-08-19 14:14:08 +00:00
|
|
|
} else {
|
|
|
|
aDestApp.widgetPages = [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-17 04:30:43 +00:00
|
|
|
// Registers all the activities and system messages.
|
2014-06-10 22:52:47 +00:00
|
|
|
registerAppsHandlers: Task.async(function*(aRunUpdate) {
|
2012-10-17 04:30:43 +00:00
|
|
|
this.notifyAppsRegistryStart();
|
2012-09-19 16:28:55 +00:00
|
|
|
let ids = [];
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
ids.push({ id: id });
|
|
|
|
}
|
2013-04-30 13:01:46 +00:00
|
|
|
if (supportSystemMessages()) {
|
|
|
|
this._processManifestForIds(ids, aRunUpdate);
|
|
|
|
} else {
|
2013-09-11 12:15:48 +00:00
|
|
|
// Read the CSPs and roles. If MOZ_SYS_MSG is defined this is done on
|
2013-04-30 13:01:46 +00:00
|
|
|
// _processManifestForIds so as to not reading the manifests
|
|
|
|
// twice
|
2014-06-10 22:52:47 +00: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 14:14:08 +00:00
|
|
|
|
|
|
|
let localeManifest = new ManifestHelper(aResult.manifest, app.origin, app.manifestURL);
|
|
|
|
this._saveWidgetsFullPath(localeManifest, app);
|
|
|
|
|
2014-06-10 22:52:47 +00:00
|
|
|
if (app.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED) {
|
|
|
|
app.redirects = this.sanitizeRedirects(aResult.redirects);
|
|
|
|
}
|
2014-09-16 20:14:02 +00:00
|
|
|
app.kind = this.appKind(app, aResult.manifest);
|
2015-08-14 23:55:09 +00:00
|
|
|
UserCustomizations.register(app);
|
2015-01-10 23:00:27 +00:00
|
|
|
Langpacks.register(app, aResult.manifest);
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2012-10-19 10:43:17 +00:00
|
|
|
|
2013-04-30 13:01:46 +00:00
|
|
|
// Nothing else to do but notifying we're ready.
|
|
|
|
this.notifyAppsRegistryReady();
|
|
|
|
}
|
2014-06-10 22:52:47 +00:00
|
|
|
}),
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2014-06-10 22:52:47 +00:00
|
|
|
updateDataStoreForApp: Task.async(function*(aId) {
|
2013-10-02 17:27:07 +00:00
|
|
|
if (!this.webapps[aId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or Update the DataStore for this app
|
2014-06-10 22:52:47 +00: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 17:27:07 +00:00
|
|
|
|
2014-09-16 20:14:02 +00:00
|
|
|
appKind: function(aApp, aManifest) {
|
2015-09-14 16:30:28 +00:00
|
|
|
if (aApp.origin.startsWith("android://")) {
|
|
|
|
return this.kAndroid;
|
|
|
|
} if (aApp.origin.startsWith("app://")) {
|
2014-09-16 20:14:02 +00:00
|
|
|
return this.kPackaged;
|
|
|
|
} else {
|
|
|
|
// Hosted apps, can be appcached or not.
|
|
|
|
let kind = this.kHosted;
|
2015-08-21 17:00:54 +00:00
|
|
|
if (aManifest.appcache_path) {
|
2014-09-16 20:14:02 +00:00
|
|
|
kind = this.kHostedAppcache;
|
|
|
|
}
|
|
|
|
return kind;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-03 07:24:00 +00:00
|
|
|
updatePermissionsForApp: function(aId, aIsPreinstalled) {
|
2012-12-10 20:51:10 +00:00
|
|
|
if (!this.webapps[aId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-10 16:16:49 +00:00
|
|
|
// Install the permissions for this app, as if we were updating
|
|
|
|
// to cleanup the old ones if needed.
|
2013-06-20 16:51:18 +00:00
|
|
|
// TODO It's not clear what this should do when there are multiple profiles.
|
2013-07-07 20:41:55 +00:00
|
|
|
if (supportUseCurrentProfile()) {
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests([{ id: aId }]).then((aResult) => {
|
2013-07-07 20:41:55 +00:00
|
|
|
let data = aResult[0];
|
2014-11-03 19:08:06 +00:00
|
|
|
this.webapps[aId].kind = this.webapps[aId].kind ||
|
|
|
|
this.appKind(this.webapps[aId], aResult[0].manifest);
|
2013-07-07 20:41:55 +00:00
|
|
|
PermissionsInstaller.installPermissions({
|
|
|
|
manifest: data.manifest,
|
|
|
|
manifestURL: this.webapps[aId].manifestURL,
|
2014-05-26 10:30:11 +00:00
|
|
|
origin: this.webapps[aId].origin,
|
2014-06-11 08:47:00 +00:00
|
|
|
isPreinstalled: aIsPreinstalled,
|
2014-09-16 20:14:02 +00:00
|
|
|
kind: this.webapps[aId].kind
|
2013-07-07 20:41:55 +00:00
|
|
|
}, true, function() {
|
|
|
|
debug("Error installing permissions for " + aId);
|
|
|
|
});
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2013-07-07 20:41:55 +00:00
|
|
|
}
|
2012-10-10 16:16:49 +00:00
|
|
|
},
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
updateOfflineCacheForApp: function(aId) {
|
2012-10-23 20:52:57 +00:00
|
|
|
let app = this.webapps[aId];
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests([{ id: aId }]).then((aResult) => {
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aResult[0].manifest, app.origin, app.manifestURL);
|
2014-11-12 19:36:36 +00:00
|
|
|
let fullAppcachePath = manifest.fullAppcachePath();
|
|
|
|
if (fullAppcachePath) {
|
|
|
|
OfflineCacheInstaller.installCache({
|
|
|
|
cachePath: app.cachePath || app.basePath,
|
|
|
|
appId: aId,
|
|
|
|
origin: Services.io.newURI(app.origin, null, null),
|
|
|
|
localId: app.localId,
|
|
|
|
appcache_path: fullAppcachePath
|
|
|
|
});
|
|
|
|
}
|
2012-10-23 20:52:57 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-01-09 15:06:07 +00:00
|
|
|
// Installs a 3rd party app.
|
|
|
|
installPreinstalledApp: function installPreinstalledApp(aId) {
|
2015-09-14 16:30:28 +00:00
|
|
|
if (!AppConstants.MOZ_B2GDROID && AppConstants.platform !== "gonk") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 13:00:00 +00:00
|
|
|
// In some cases, the app might be already installed under a different ID but
|
|
|
|
// with the same manifestURL. In that case, the only content of the webapp will
|
|
|
|
// be the id of the old version, which is the one we'll keep.
|
|
|
|
let destId = this.webapps[aId].oldId || aId;
|
|
|
|
// We don't need the oldId anymore
|
|
|
|
if (destId !== aId) {
|
|
|
|
delete this.webapps[aId];
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = this.webapps[destId];
|
2014-05-26 10:30:11 +00:00
|
|
|
let baseDir, isPreinstalled = false;
|
2012-12-10 20:51:10 +00:00
|
|
|
try {
|
2012-12-22 13:56:21 +00:00
|
|
|
baseDir = FileUtils.getDir("coreAppsDir", ["webapps", aId], false);
|
2013-01-10 07:53:17 +00:00
|
|
|
if (!baseDir.exists()) {
|
2014-05-26 10:30:11 +00:00
|
|
|
return isPreinstalled;
|
2013-07-15 00:53:41 +00:00
|
|
|
} else if (!baseDir.directoryEntries.hasMoreElements()) {
|
|
|
|
debug("Error: Core app in " + baseDir.path + " is empty");
|
2014-05-26 10:30:11 +00:00
|
|
|
return isPreinstalled;
|
2013-01-10 07:53:17 +00:00
|
|
|
}
|
2012-12-10 20:51:10 +00:00
|
|
|
} catch(e) {
|
|
|
|
// In ENG builds, we don't have apps in coreAppsDir.
|
2014-05-26 10:30:11 +00:00
|
|
|
return isPreinstalled;
|
2012-12-10 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
2014-05-26 10:30:11 +00:00
|
|
|
// Beyond this point we know it's really a preinstalled app.
|
|
|
|
isPreinstalled = true;
|
|
|
|
|
2013-01-09 15:06:07 +00:00
|
|
|
let filesToMove;
|
|
|
|
let isPackage;
|
|
|
|
|
2012-12-10 20:51:10 +00:00
|
|
|
let updateFile = baseDir.clone();
|
|
|
|
updateFile.append("update.webapp");
|
|
|
|
if (!updateFile.exists()) {
|
2013-01-09 15:06:07 +00: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 10:30:11 +00:00
|
|
|
return isPreinstalled;
|
2013-01-09 15:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isPackage = false;
|
|
|
|
filesToMove = ["manifest.webapp"];
|
|
|
|
} else {
|
|
|
|
isPackage = true;
|
|
|
|
filesToMove = ["application.zip", "update.webapp"];
|
2012-12-10 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 15:06:07 +00:00
|
|
|
debug("Installing 3rd party app : " + aId +
|
2014-12-11 13:00:00 +00:00
|
|
|
" from " + baseDir.path + " to " + destId);
|
2012-12-10 20:51:10 +00:00
|
|
|
|
2014-12-11 13:00:00 +00:00
|
|
|
// We copy this app to DIRECTORY_NAME/$destId, and set the base path as needed.
|
|
|
|
let destDir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", destId], true, true);
|
2012-12-10 20:51:10 +00:00
|
|
|
|
2013-01-09 15:06:07 +00:00
|
|
|
filesToMove.forEach(function(aFile) {
|
2012-12-10 20:51:10 +00:00
|
|
|
let file = baseDir.clone();
|
|
|
|
file.append(aFile);
|
2013-07-15 00:53:41 +00:00
|
|
|
try {
|
|
|
|
file.copyTo(destDir, aFile);
|
|
|
|
} catch(e) {
|
|
|
|
debug("Error: Failed to copy " + file.path + " to " + destDir.path);
|
|
|
|
}
|
2012-12-10 20:51:10 +00:00
|
|
|
});
|
|
|
|
|
2013-02-01 19:17:32 +00:00
|
|
|
app.installState = "installed";
|
2013-05-15 20:51:41 +00:00
|
|
|
app.cachePath = app.basePath;
|
2014-01-17 00:26:24 +00:00
|
|
|
app.basePath = OS.Path.dirname(this.appsFile);
|
2013-01-09 15:06:07 +00:00
|
|
|
|
|
|
|
if (!isPackage) {
|
2014-05-26 10:30:11 +00:00
|
|
|
return isPreinstalled;
|
2013-01-09 15:06:07 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 13:00:00 +00:00
|
|
|
app.origin = "app://" + destId;
|
2012-12-10 20:51:10 +00:00
|
|
|
|
2013-06-26 12:18:34 +00: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-10 20:51:10 +00: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);
|
2014-12-11 13:00:00 +00:00
|
|
|
delete this.webapps[destId];
|
2012-12-10 20:51:10 +00:00
|
|
|
} finally {
|
|
|
|
zipReader.close();
|
|
|
|
}
|
2014-05-26 10:30:11 +00:00
|
|
|
return isPreinstalled;
|
2012-12-10 20:51:10 +00:00
|
|
|
},
|
|
|
|
|
2013-12-09 22:05:14 +00: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-11 21:23:18 +00:00
|
|
|
this.uninstall(app.manifestURL);
|
2013-12-09 22:05:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
// Implements the core of bug 787439
|
2012-10-23 20:52:57 +00:00
|
|
|
// if at first run, go through these steps:
|
2012-09-19 16:28:55 +00: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 00:26:24 +00:00
|
|
|
installSystemApps: function() {
|
2015-02-08 21:48:29 +00:00
|
|
|
return Task.spawn(function*() {
|
2014-01-17 00:26:24 +00:00
|
|
|
let file;
|
|
|
|
try {
|
|
|
|
file = FileUtils.getFile("coreAppsDir", ["webapps", "webapps.json"], false);
|
|
|
|
} catch(e) { }
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
if (!file || !file.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-22 13:56:21 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
// a
|
2014-03-25 13:14:06 +00:00
|
|
|
let data = yield AppsUtils.loadJSONAsync(file.path);
|
2014-01-17 00:26:24 +00:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2014-01-17 00:26:24 +00: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;
|
2015-10-13 02:27:42 +00:00
|
|
|
let attrs = { appId: localId };
|
|
|
|
permMgr.removePermissionsWithAttributes(JSON.stringify(attrs));
|
2014-01-17 00:26:24 +00:00
|
|
|
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.
|
2014-12-11 13:00:00 +00:00
|
|
|
// Note that in some cases, the id might change, but the
|
|
|
|
// manifest URL wont. So consider that the app is old if
|
|
|
|
// the id does not exist already and if there's no other id
|
|
|
|
// for the manifestURL.
|
|
|
|
var oldId = (id in this.webapps) ? id :
|
|
|
|
this._appIdForManifestURL(data[id].manifestURL);
|
|
|
|
if (!oldId) {
|
2014-01-17 00:26:24 +00:00
|
|
|
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 16:28:55 +00:00
|
|
|
}
|
2014-04-09 13:45:15 +00:00
|
|
|
} else {
|
2014-10-01 06:37:00 +00:00
|
|
|
// Fields that we must not update. Confere bug 993011 comment 10.
|
|
|
|
let fieldsBlacklist = ["basePath", "id", "installerAppId",
|
|
|
|
"installerIsBrowser", "localId", "receipts", "storeId",
|
|
|
|
"storeVersion"];
|
2014-04-09 13:45:15 +00:00
|
|
|
// 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
|
2014-12-11 13:00:00 +00:00
|
|
|
// We also should fall in this case when the app is a preinstalled third party app.
|
2014-10-01 06:37:00 +00:00
|
|
|
for (let field in data[id]) {
|
|
|
|
if (fieldsBlacklist.indexOf(field) === -1) {
|
2014-12-11 13:00:00 +00:00
|
|
|
this.webapps[oldId][field] = data[id][field];
|
2014-10-01 06:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-11 13:00:00 +00:00
|
|
|
// If the id for the app has changed on the update, keep a pointer to the old one
|
|
|
|
// since we'll need this to update the app files.
|
|
|
|
if (id !== oldId) {
|
|
|
|
this.webapps[id] = {oldId: oldId};
|
|
|
|
}
|
2012-10-23 20:52:57 +00:00
|
|
|
}
|
2014-01-17 00:26:24 +00:00
|
|
|
}
|
|
|
|
}.bind(this)).then(null, Cu.reportError);
|
2012-10-23 20:52:57 +00:00
|
|
|
},
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
loadAndUpdateApps: function() {
|
2015-02-08 21:48:29 +00:00
|
|
|
return Task.spawn(function*() {
|
2015-08-06 14:43:59 +00:00
|
|
|
let runUpdate = false;
|
|
|
|
try {
|
|
|
|
runUpdate = AppsUtils.isFirstRun(Services.prefs);
|
|
|
|
} catch(e) {}
|
|
|
|
|
2015-04-02 06:21:52 +00:00
|
|
|
let loadAppPermission = Services.prefs.getBoolPref("dom.apps.reset-permissions");
|
2012-10-23 20:52:57 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
yield this.loadCurrentRegistry();
|
|
|
|
|
2014-12-18 10:13:31 +00:00
|
|
|
// Sanity check and roll back previous incomplete app updates.
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
let oldDir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id + ".old"], false, true);
|
|
|
|
if (oldDir.exists()) {
|
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], false, true);
|
|
|
|
if (dir.exists()) {
|
|
|
|
dir.remove(true);
|
|
|
|
}
|
|
|
|
oldDir.moveTo(null, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:41:24 +00: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.
|
|
|
|
|
2015-04-02 06:21:52 +00:00
|
|
|
if (runUpdate || !loadAppPermission) {
|
2014-09-22 23:01:50 +00:00
|
|
|
|
2014-08-25 20:30:07 +00:00
|
|
|
// Run migration before uninstall of core apps happens.
|
2015-02-08 21:48:29 +00:00
|
|
|
let appMigrator = Components.classes["@mozilla.org/app-migrator;1"];
|
|
|
|
if (appMigrator) {
|
|
|
|
try {
|
|
|
|
appMigrator = appMigrator.createInstance(Components.interfaces.nsIObserver);
|
|
|
|
appMigrator.observe(null, "webapps-before-update-merge", null);
|
|
|
|
} catch(e) {
|
|
|
|
debug("Exception running app migration: ");
|
|
|
|
debug(e.name + " " + e.message);
|
|
|
|
debug("Skipping app migration.");
|
|
|
|
}
|
2014-09-23 20:22:54 +00:00
|
|
|
}
|
2014-08-25 20:30:07 +00:00
|
|
|
|
2015-08-25 20:42:24 +00:00
|
|
|
if (AppConstants.MOZ_B2GDROID || AppConstants.MOZ_B2G) {
|
|
|
|
yield this.installSystemApps();
|
|
|
|
}
|
2014-01-17 00:26:24 +00:00
|
|
|
|
2015-09-14 16:30:28 +00:00
|
|
|
if (AppConstants.MOZ_B2GDROID) {
|
|
|
|
yield AndroidUtils.installAndroidApps();
|
|
|
|
}
|
|
|
|
|
2012-12-10 20:51:10 +00:00
|
|
|
// At first run, install preloaded apps and set up their permissions.
|
2012-10-10 16:16:49 +00:00
|
|
|
for (let id in this.webapps) {
|
2014-05-26 10:30:11 +00:00
|
|
|
let isPreinstalled = this.installPreinstalledApp(id);
|
2013-12-09 22:05:14 +00:00
|
|
|
this.removeIfHttpsDuplicate(id);
|
2012-12-10 20:51:10 +00:00
|
|
|
if (!this.webapps[id]) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-23 20:52:57 +00:00
|
|
|
this.updateOfflineCacheForApp(id);
|
2014-11-03 07:24:00 +00:00
|
|
|
this.updatePermissionsForApp(id, isPreinstalled);
|
2012-10-10 16:16:49 +00:00
|
|
|
}
|
2012-12-10 20:51:10 +00:00
|
|
|
// Need to update the persisted list of apps since
|
2013-01-09 15:06:07 +00:00
|
|
|
// installPreinstalledApp() removes the ones failing to install.
|
2015-09-14 16:30:28 +00:00
|
|
|
yield this._saveApps();
|
2015-04-02 06:21:52 +00:00
|
|
|
|
2015-08-06 14:43:59 +00:00
|
|
|
Services.prefs.setBoolPref("dom.apps.reset-permissions", true);
|
2012-08-29 21:20:03 +00:00
|
|
|
}
|
2013-10-02 17:27:23 +00:00
|
|
|
|
|
|
|
// DataStores must be initialized at startup.
|
|
|
|
for (let id in this.webapps) {
|
2014-06-10 22:52:47 +00:00
|
|
|
yield this.updateDataStoreForApp(id);
|
2013-10-02 17:27:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 22:52:47 +00:00
|
|
|
yield this.registerAppsHandlers(runUpdate);
|
2014-01-17 00:26:24 +00:00
|
|
|
}.bind(this)).then(null, Cu.reportError);
|
2011-12-05 23:26:18 +00:00
|
|
|
},
|
|
|
|
|
2014-06-19 14:03:54 +00:00
|
|
|
updateDataStore: function(aId, aOrigin, aManifestURL, aManifest) {
|
2014-10-28 00:40:13 +00:00
|
|
|
if (!aManifest) {
|
|
|
|
debug("updateDataStore: no manifest for " + aOrigin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-19 14:03:54 +00:00
|
|
|
let uri = Services.io.newURI(aOrigin, null, null);
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
2015-08-17 09:03:19 +00:00
|
|
|
let principal = secMan.createCodebasePrincipal(uri, {appId: aId});
|
2014-06-19 14:03:54 +00:00
|
|
|
if (!dataStoreService.checkPermission(principal)) {
|
2013-12-05 23:12:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-02 17:27:14 +00: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 17:27:23 +00:00
|
|
|
dataStoreService.installDataStore(aId, name, aOrigin, aManifestURL,
|
|
|
|
readonly);
|
2013-10-02 17:27:14 +00:00
|
|
|
}
|
2013-10-02 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 17:27:14 +00: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 17:27:07 +00:00
|
|
|
|
2013-10-02 17:27:23 +00:00
|
|
|
dataStoreService.installAccessDataStore(aId, name, aOrigin,
|
|
|
|
aManifestURL, readonly);
|
2013-10-02 17:27:14 +00:00
|
|
|
}
|
2013-10-02 17:27:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-18 07:32:15 +00:00
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
2012-09-13 18:47:34 +00:00
|
|
|
// use the root of the manifest.
|
2013-08-16 09:48:37 +00:00
|
|
|
//
|
|
|
|
// TODO Bug 908094 Refine _registerSystemMessagesForEntryPoint(...).
|
2012-09-13 18:47:34 +00:00
|
|
|
_registerSystemMessagesForEntryPoint: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:23:57 +00:00
|
|
|
if (!root.messages) {
|
|
|
|
// This application just doesn't use system messages.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(root.messages) || root.messages.length == 0) {
|
|
|
|
dump("Could not register invalid system message entry for " + aApp.manifestURL + "\n");
|
2015-07-02 09:00:23 +00:00
|
|
|
try {
|
|
|
|
dump(JSON.stringify(root.messages) + "\n");
|
|
|
|
} catch(e) {}
|
2012-09-13 18:47:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin, aApp.manifestURL);
|
2014-06-03 10:27:12 +00:00
|
|
|
let launchPathURI = Services.io.newURI(manifest.fullLaunchPath(aEntryPoint), null, null);
|
|
|
|
let manifestURI = Services.io.newURI(aApp.manifestURL, null, null);
|
2012-09-13 18:47:34 +00:00
|
|
|
root.messages.forEach(function registerPages(aMessage) {
|
2014-06-03 10:27:12 +00:00
|
|
|
let handlerPageURI = launchPathURI;
|
2012-09-13 18:47:34 +00:00
|
|
|
let messageName;
|
2015-07-02 09:00:23 +00:00
|
|
|
if (typeof(aMessage) !== "object" || Object.keys(aMessage).length !== 1) {
|
2015-08-27 16:23:57 +00:00
|
|
|
dump("Could not register invalid system message entry for " + aApp.manifestURL + "\n");
|
2013-02-04 13:59:32 +00:00
|
|
|
try {
|
2015-07-02 09:00:23 +00:00
|
|
|
dump(JSON.stringify(aMessage) + "\n");
|
|
|
|
} catch(e) {}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
messageName = Object.keys(aMessage)[0];
|
|
|
|
let handlerPath = aMessage[messageName];
|
|
|
|
// Resolve the handler path from origin. If |handler_path| is absent,
|
|
|
|
// simply skip.
|
|
|
|
let fullHandlerPath;
|
|
|
|
try {
|
|
|
|
if (handlerPath && handlerPath.trim()) {
|
|
|
|
fullHandlerPath = manifest.resolveURL(handlerPath);
|
|
|
|
} else {
|
|
|
|
throw new Error("Empty or blank handler path.");
|
2013-02-04 13:59:32 +00:00
|
|
|
}
|
2015-07-02 09:00:23 +00:00
|
|
|
} catch(e) {
|
|
|
|
debug("system message handler path (" + handlerPath + ") is " +
|
|
|
|
"invalid, skipping. Error is: " + e);
|
|
|
|
return;
|
2012-09-13 18:47:34 +00:00
|
|
|
}
|
2015-07-02 09:00:23 +00:00
|
|
|
handlerPageURI = Services.io.newURI(fullHandlerPath, null, null);
|
2012-12-05 03:49:26 +00:00
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister(messageName,
|
2014-07-30 21:00:15 +00:00
|
|
|
aApp.manifestURL,
|
2015-08-21 17:00:51 +00:00
|
|
|
aApp.origin,
|
2012-12-05 03:49:26 +00:00
|
|
|
aManifest)) {
|
2014-06-03 10:27:12 +00:00
|
|
|
msgmgr.registerPage(messageName, handlerPageURI, manifestURI);
|
2012-12-05 03:49:26 +00:00
|
|
|
}
|
2012-09-13 18:47:34 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-16 09:48:37 +00: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-30 21:00:15 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin, aApp.manifestURL);
|
2013-08-16 09:48:37 +00: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-30 21:00:15 +00:00
|
|
|
fullHandlerPath = manifest.resolveURL(handlerPath);
|
2013-08-16 09:48:37 +00: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-30 21:00:15 +00:00
|
|
|
aApp.manifestURL,
|
2015-08-21 17:00:51 +00:00
|
|
|
aApp.origin,
|
2013-08-16 09:48:37 +00:00
|
|
|
aManifest)) {
|
|
|
|
msgmgr.registerPage("connection", handlerPageURI, manifestURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
interAppCommService.
|
|
|
|
registerConnection(keyword,
|
|
|
|
handlerPageURI,
|
|
|
|
manifestURI,
|
|
|
|
connection.description,
|
|
|
|
connection.rules);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-03 00:16:55 +00:00
|
|
|
_registerSystemMessages: function(aManifest, aApp) {
|
2012-09-13 18:47:34 +00: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 00:16:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-16 09:48:37 +00: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 07:32:15 +00:00
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
2012-09-13 18:47:34 +00:00
|
|
|
// use the root of the manifest.
|
2015-07-02 05:56:14 +00:00
|
|
|
_createActivitiesToRegister: function(aManifest, aApp, aEntryPoint,
|
|
|
|
aRunUpdate, aUninstall) {
|
2012-10-18 07:32:15 +00:00
|
|
|
let activitiesToRegister = [];
|
2012-09-13 18:47:34 +00:00
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
2014-07-31 13:48:00 +00:00
|
|
|
if (!root || !root.activities) {
|
2012-10-18 07:32:15 +00:00
|
|
|
return activitiesToRegister;
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin, aApp.manifestURL);
|
2012-09-13 18:47:34 +00:00
|
|
|
for (let activity in root.activities) {
|
2015-07-02 05:56:14 +00:00
|
|
|
let entry = root.activities[activity];
|
|
|
|
if (!Array.isArray(entry)) {
|
|
|
|
entry = [entry];
|
2012-08-15 02:40:12 +00:00
|
|
|
}
|
2015-07-02 05:56:14 +00:00
|
|
|
for (let i = 0; i < entry.length; i++) {
|
|
|
|
let description = entry[i];
|
|
|
|
let href = description.href;
|
|
|
|
if (!href) {
|
|
|
|
href = manifest.launch_path;
|
|
|
|
}
|
2013-02-04 13:59:32 +00:00
|
|
|
|
2015-07-02 05:56:14 +00:00
|
|
|
try {
|
|
|
|
href = manifest.resolveURL(href);
|
|
|
|
} catch (e) {
|
|
|
|
debug("Activity href (" + href + ") is invalid, skipping. " +
|
|
|
|
"Error is: " + e);
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-12 23:19:50 +00:00
|
|
|
|
2015-07-02 05:56:14 +00: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;
|
2012-11-03 03:28:32 +00:00
|
|
|
|
2015-07-02 05:56:14 +00:00
|
|
|
debug('_createActivitiesToRegister: ' + aApp.manifestURL + ', activity ' +
|
|
|
|
activity + ', description.href is ' + newDesc.href);
|
2012-11-03 03:28:32 +00:00
|
|
|
|
2015-07-02 05:56:14 +00:00
|
|
|
if (aRunUpdate || aUninstall) {
|
|
|
|
activitiesToRegister.push({ "manifest": aApp.manifestURL,
|
|
|
|
"name": activity,
|
|
|
|
"icon": manifest.iconURLForSize(128),
|
|
|
|
"description": newDesc });
|
|
|
|
}
|
2012-12-05 03:49:26 +00:00
|
|
|
|
2015-07-02 05:56:14 +00:00
|
|
|
if (aUninstall) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-02 05:56:14 +00:00
|
|
|
|
2015-07-02 05:56:14 +00:00
|
|
|
let launchPathURI = Services.io.newURI(href, null, null);
|
|
|
|
let manifestURI = Services.io.newURI(aApp.manifestURL, null, null);
|
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister("activity",
|
|
|
|
aApp.manifestURL,
|
2015-08-21 17:00:51 +00:00
|
|
|
aApp.origin,
|
2015-07-02 05:56:14 +00:00
|
|
|
aManifest)) {
|
|
|
|
msgmgr.registerPage("activity", launchPathURI, manifestURI);
|
|
|
|
}
|
2012-12-05 03:49:26 +00:00
|
|
|
}
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
2012-10-18 07:32:15 +00:00
|
|
|
return activitiesToRegister;
|
2012-07-20 15:41:30 +00:00
|
|
|
},
|
|
|
|
|
2012-10-18 07:32:15 +00: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 03:28:32 +00:00
|
|
|
_registerActivitiesForApps: function(aAppsToRegister, aRunUpdate) {
|
2012-10-18 07:32:15 +00: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 03:28:32 +00:00
|
|
|
this._createActivitiesToRegister(manifest, app, null, aRunUpdate));
|
2012-10-18 07:32:15 +00:00
|
|
|
|
2014-08-11 22:21:44 +00:00
|
|
|
if (aRunUpdate) {
|
|
|
|
cpmm.sendAsyncMessage("Activities:UnregisterAll", app.manifestURL);
|
|
|
|
}
|
|
|
|
|
2012-10-18 07:32:15 +00:00
|
|
|
if (!manifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-13 18:47:34 +00:00
|
|
|
|
2012-10-18 07:32:15 +00:00
|
|
|
for (let entryPoint in manifest.entry_points) {
|
|
|
|
activitiesToRegister.push.apply(activitiesToRegister,
|
2012-11-03 03:28:32 +00:00
|
|
|
this._createActivitiesToRegister(manifest, app, entryPoint, aRunUpdate));
|
2012-10-18 07:32:15 +00:00
|
|
|
}
|
|
|
|
}, this);
|
2012-09-13 18:47:34 +00:00
|
|
|
|
2012-11-03 03:28:32 +00:00
|
|
|
if (!aRunUpdate || activitiesToRegister.length == 0) {
|
|
|
|
this.notifyAppsRegistryReady();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-18 07:32:15 +00: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 03:28:32 +00:00
|
|
|
_registerActivities: function(aManifest, aApp, aRunUpdate) {
|
|
|
|
this._registerActivitiesForApps([{ manifest: aManifest, app: aApp }], aRunUpdate);
|
2012-09-13 18:47:34 +00:00
|
|
|
},
|
|
|
|
|
2012-10-18 07:32:15 +00: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,
|
2015-07-02 05:56:14 +00:00
|
|
|
this._createActivitiesToRegister(manifest, app, null, false, true));
|
2012-10-18 07:32:15 +00:00
|
|
|
|
|
|
|
if (!manifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-13 18:47:34 +00:00
|
|
|
|
2012-10-18 07:32:15 +00:00
|
|
|
for (let entryPoint in manifest.entry_points) {
|
|
|
|
activitiesToUnregister.push.apply(activitiesToUnregister,
|
2015-07-02 05:56:14 +00:00
|
|
|
this._createActivitiesToRegister(manifest, app, entryPoint, false, true));
|
2012-10-18 07:32:15 +00:00
|
|
|
}
|
|
|
|
}, this);
|
2012-09-13 18:47:34 +00:00
|
|
|
|
2012-10-18 07:32:15 +00:00
|
|
|
// Send the array carrying all the activities to be unregistered.
|
|
|
|
cpmm.sendAsyncMessage("Activities:Unregister", activitiesToUnregister);
|
2012-09-13 18:47:34 +00:00
|
|
|
},
|
|
|
|
|
2012-10-18 07:32:15 +00: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 04:30:43 +00:00
|
|
|
},
|
|
|
|
|
2012-11-03 03:28:32 +00:00
|
|
|
_processManifestForIds: function(aIds, aRunUpdate) {
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests(aIds).then((aResults) => {
|
2012-10-18 07:32:15 +00:00
|
|
|
let appsToRegister = [];
|
2014-01-17 00:26:24 +00:00
|
|
|
aResults.forEach((aResult) => {
|
2012-09-12 01:17:01 +00:00
|
|
|
let app = this.webapps[aResult.id];
|
|
|
|
let manifest = aResult.manifest;
|
2013-10-01 18:01:08 +00: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 09:28:45 +00:00
|
|
|
|
2014-07-30 21:00:15 +00:00
|
|
|
let localeManifest =
|
|
|
|
new ManifestHelper(manifest, app.origin, app.manifestURL);
|
2014-06-07 09:28:45 +00:00
|
|
|
|
2014-06-23 00:22:07 +00:00
|
|
|
app.name = manifest.name;
|
2012-10-19 10:43:17 +00:00
|
|
|
app.csp = manifest.csp || "";
|
2014-06-07 09:28:45 +00:00
|
|
|
app.role = localeManifest.role;
|
2014-08-19 14:14:08 +00:00
|
|
|
this._saveWidgetsFullPath(localeManifest, app);
|
|
|
|
|
2013-05-14 19:00:09 +00:00
|
|
|
if (app.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED) {
|
|
|
|
app.redirects = this.sanitizeRedirects(manifest.redirects);
|
|
|
|
}
|
2014-09-16 20:14:02 +00:00
|
|
|
app.kind = this.appKind(app, aResult.manifest);
|
2012-09-12 01:17:01 +00:00
|
|
|
this._registerSystemMessages(manifest, app);
|
2013-08-16 09:48:37 +00:00
|
|
|
this._registerInterAppConnections(manifest, app);
|
2012-10-18 07:32:15 +00:00
|
|
|
appsToRegister.push({ manifest: manifest, app: app });
|
2015-08-14 23:55:09 +00:00
|
|
|
UserCustomizations.register(app);
|
2015-01-10 23:00:27 +00:00
|
|
|
Langpacks.register(app, manifest);
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2014-07-14 21:27:57 +00:00
|
|
|
this._safeToClone.resolve();
|
2012-11-03 03:28:32 +00:00
|
|
|
this._registerActivitiesForApps(appsToRegister, aRunUpdate);
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2012-07-03 00:16:55 +00:00
|
|
|
},
|
|
|
|
|
2012-03-13 00:33:10 +00: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");
|
2015-10-29 14:32:50 +00:00
|
|
|
Services.obs.removeObserver(this, "memory-pressure");
|
2013-06-12 23:28:11 +00:00
|
|
|
cpmm = null;
|
2012-03-13 00:33:10 +00:00
|
|
|
ppmm = null;
|
2015-09-14 16:30:28 +00:00
|
|
|
if (AppConstants.MOZ_B2GDROID) {
|
|
|
|
AndroidUtils.uninit();
|
|
|
|
}
|
2014-01-06 21:03:50 +00:00
|
|
|
} else if (aTopic == "memory-pressure") {
|
|
|
|
// Clear the manifest cache on memory pressure.
|
|
|
|
this._manifestCache = {};
|
2012-03-13 00:33:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-29 14:32:50 +00:00
|
|
|
// Check extensions to be blocked.
|
|
|
|
blockExtensions: function(aExtensions) {
|
|
|
|
debug("blockExtensions");
|
|
|
|
let app;
|
|
|
|
let runtime = Services.appinfo.QueryInterface(Ci.nsIXULRuntime);
|
|
|
|
|
|
|
|
aExtensions.filter(extension => {
|
|
|
|
// Filter out id-less items and those who don't have a matching installed
|
|
|
|
// extension.
|
|
|
|
if (!extension.attributes.has("id")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check that we have an app with this extension id.
|
|
|
|
let extId = extension.attributes.get("id");
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].blocklistId == extId) {
|
|
|
|
app = this.webapps[id];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No webapp found for this extension id.
|
|
|
|
return false;
|
|
|
|
}).forEach(extension => {
|
|
|
|
// `extension` is a object such as:
|
|
|
|
// {"versions":[{"minVersion":"0.1",
|
|
|
|
// "maxVersion":"1.3.328.4",
|
|
|
|
// "severity":"1",
|
|
|
|
// "vulnerabilityStatus":0,
|
|
|
|
// "targetApps":{
|
|
|
|
// "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}":[{"minVersion":"3.7a1pre","maxVersion":"*"}]
|
|
|
|
// }
|
|
|
|
// }],
|
|
|
|
// "prefs":[],
|
|
|
|
// "blockID":"i24",
|
|
|
|
// "attributes": Map()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// `versions` is array of BlocklistItemData (see nsBlocklistService.js)
|
|
|
|
let severity = Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
|
|
|
|
for (let item of extension.versions) {
|
|
|
|
if (item.includesItem(app.extensionVersion, runtime.version, runtime.platformVersion)) {
|
|
|
|
severity = item.severity;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setBlockedStatus(app.manifestURL, severity);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-04-02 20:53:27 +00:00
|
|
|
formatMessage: function(aData) {
|
|
|
|
let msg = aData;
|
|
|
|
delete msg["mm"];
|
|
|
|
return msg;
|
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
receiveMessage: function(aMessage) {
|
2012-06-26 20:12:33 +00: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-10-01 20:29:59 +00:00
|
|
|
let msg = aMessage.data || {};
|
2012-09-18 17:34:55 +00:00
|
|
|
let mm = aMessage.target;
|
|
|
|
msg.mm = mm;
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2014-11-02 19:04:08 +00:00
|
|
|
let principal = aMessage.principal;
|
|
|
|
|
|
|
|
let checkPermission = function(aPermission) {
|
|
|
|
if (!permMgr.testPermissionFromPrincipal(principal, aPermission)) {
|
|
|
|
debug("mozApps message " + aMessage.name +
|
|
|
|
" from a content process with no " + aPermission + " privileges.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We need to check permissions for calls coming from mozApps.mgmt.
|
|
|
|
|
|
|
|
let allowed = true;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:Uninstall":
|
|
|
|
let isCurrentHomescreen =
|
|
|
|
Services.prefs.prefHasUserValue("dom.mozApps.homescreenURL") &&
|
|
|
|
Services.prefs.getCharPref("dom.mozApps.homescreenURL") ==
|
|
|
|
appsService.getManifestURLByLocalId(principal.appId);
|
|
|
|
|
|
|
|
allowed = checkPermission("webapps-manage") ||
|
|
|
|
(checkPermission("homescreen-webapps-manage") && isCurrentHomescreen);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Webapps:GetNotInstalled":
|
|
|
|
case "Webapps:ApplyDownload":
|
|
|
|
case "Webapps:Import":
|
|
|
|
case "Webapps:ExtractManifest":
|
|
|
|
case "Webapps:SetEnabled":
|
|
|
|
allowed = checkPermission("webapps-manage");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Webapps:RegisterBEP":
|
|
|
|
allowed = checkPermission("browser");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-14 21:27:57 +00:00
|
|
|
let processedImmediately = true;
|
|
|
|
|
2014-11-02 19:04:08 +00:00
|
|
|
if (!allowed) {
|
|
|
|
mm.killChild();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-07-14 21:27:57 +00:00
|
|
|
// 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-28 20:13:26 +00:00
|
|
|
switch (aMessage.name) {
|
2014-07-14 21:27:57 +00: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":
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.removeMessageListener(["Webapps:Internal:AllMessages"], mm);
|
2014-07-14 21:27:57 +00:00
|
|
|
break;
|
|
|
|
case "Webapps:RegisterForMessages":
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.addMessageListener(msg.messages, msg.app, mm);
|
2014-07-14 21:27:57 +00:00
|
|
|
break;
|
|
|
|
case "Webapps:UnregisterForMessages":
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.removeMessageListener(msg, mm);
|
2014-07-14 21:27:57 +00:00
|
|
|
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": {
|
2015-09-14 16:30:28 +00:00
|
|
|
if (AppConstants.platform == "android" && !AppConstants.MOZ_B2GDROID) {
|
|
|
|
Services.obs.notifyObservers(mm, "webapps-runtime-install", JSON.stringify(msg));
|
|
|
|
} else {
|
|
|
|
this.doInstall(msg, mm);
|
|
|
|
}
|
2014-07-14 21:27:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Webapps:GetSelf":
|
|
|
|
this.getSelf(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:Uninstall":
|
2015-09-14 16:30:28 +00:00
|
|
|
if (AppConstants.platform == "android" && !AppConstants.MOZ_B2GDROID) {
|
|
|
|
Services.obs.notifyObservers(mm, "webapps-runtime-uninstall", JSON.stringify(msg));
|
|
|
|
} else {
|
|
|
|
this.doUninstall(msg, mm);
|
|
|
|
}
|
2014-07-14 21:27:57 +00:00
|
|
|
break;
|
|
|
|
case "Webapps:Launch":
|
|
|
|
this.doLaunch(msg, mm);
|
|
|
|
break;
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
case "Webapps:LocationChange":
|
|
|
|
this.onLocationChange(msg.oid);
|
|
|
|
break;
|
2014-07-14 21:27:57 +00:00
|
|
|
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": {
|
2015-09-14 16:30:28 +00:00
|
|
|
if (AppConstants.platform == "android" && !AppConstants.MOZ_B2GDROID) {
|
|
|
|
Services.obs.notifyObservers(mm, "webapps-runtime-install-package", JSON.stringify(msg));
|
|
|
|
} else {
|
|
|
|
this.doInstallPackage(msg, mm);
|
|
|
|
}
|
2014-07-14 21:27:57 +00: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-09-24 06:14:54 +00:00
|
|
|
case "Webapps:RegisterBEP":
|
|
|
|
this.registerBrowserElementParentForApp(msg, mm);
|
|
|
|
break;
|
2014-12-18 22:40:41 +00:00
|
|
|
case "Webapps:GetIcon":
|
|
|
|
this.getIcon(msg, mm);
|
|
|
|
break;
|
2014-10-15 05:55:14 +00:00
|
|
|
case "Webapps:Export":
|
|
|
|
this.doExport(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:Import":
|
|
|
|
this.doImport(msg, mm);
|
|
|
|
break;
|
|
|
|
case "Webapps:ExtractManifest":
|
|
|
|
this.doExtractManifest(msg, mm);
|
|
|
|
break;
|
2014-10-23 23:40:08 +00:00
|
|
|
case "Webapps:SetEnabled":
|
|
|
|
this.setEnabled(msg);
|
|
|
|
break;
|
2014-01-09 18:18:55 +00:00
|
|
|
}
|
2014-07-14 21:27:57 +00:00
|
|
|
});
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
|
|
|
|
2013-01-31 21:35:13 +00:00
|
|
|
getAppInfo: function getAppInfo(aAppId) {
|
2013-03-15 14:18:58 +00:00
|
|
|
return AppsUtils.getAppInfo(this.webapps, aAppId);
|
2013-01-31 21:35:13 +00:00
|
|
|
},
|
|
|
|
|
2014-03-15 21:37:37 +00: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-29 21:20:03 +00:00
|
|
|
_getAppDir: function(aId) {
|
2014-02-21 19:54:03 +00:00
|
|
|
return FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
|
2012-08-29 21:20:03 +00:00
|
|
|
},
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
_writeFile: function(aPath, aData) {
|
|
|
|
debug("Saving " + aPath);
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2014-03-25 13:14:06 +00: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)) {
|
2014-10-15 05:55:14 +00:00
|
|
|
debug("Error saving " + aPath + " : " + aResult);
|
|
|
|
deferred.reject(aResult)
|
2014-03-25 13:14:06 +00:00
|
|
|
} else {
|
2014-10-15 05:55:14 +00:00
|
|
|
debug("Success saving " + aPath);
|
2014-03-25 13:14:06 +00:00
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2014-05-24 19:28:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the full list of apps and manifests.
|
|
|
|
*/
|
|
|
|
doGetList: function() {
|
|
|
|
let tmp = [];
|
|
|
|
|
|
|
|
let res = {};
|
|
|
|
let done = false;
|
|
|
|
|
2014-07-14 21:27:57 +00: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 });
|
2015-01-10 23:00:27 +00:00
|
|
|
this.webapps[id].additionalLanguages =
|
|
|
|
Langpacks.getAdditionalLanguages(this.webapps[id].manifestURL).langs;
|
2014-05-24 19:28:13 +00:00
|
|
|
}
|
2014-07-14 21:27:57 +00:00
|
|
|
this._readManifests(tmp).then(
|
|
|
|
function(manifests) {
|
|
|
|
manifests.forEach((item) => {
|
|
|
|
res[item.id] = item.manifest;
|
|
|
|
});
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2014-05-24 19:28:13 +00:00
|
|
|
|
|
|
|
let thread = Services.tm.currentThread;
|
|
|
|
while (!done) {
|
|
|
|
thread.processNextEvent(/* mayWait */ true);
|
|
|
|
}
|
|
|
|
return { webapps: this.webapps, manifests: res };
|
|
|
|
},
|
|
|
|
|
2014-10-15 05:55:14 +00:00
|
|
|
doExport: function(aMsg, aMm) {
|
|
|
|
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2014-10-15 05:55:14 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:Export:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
error: aError,
|
|
|
|
success: false
|
|
|
|
});
|
2015-04-02 20:53:27 +00:00
|
|
|
};
|
2014-10-15 05:55:14 +00:00
|
|
|
|
|
|
|
let app = this.getAppByManifestURL(aMsg.manifestURL);
|
|
|
|
if (!app) {
|
|
|
|
sendError("NoSuchApp");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportExport.export(app).then(
|
|
|
|
aBlob => {
|
|
|
|
debug("exporting " + aBlob);
|
|
|
|
aMm.sendAsyncMessage("Webapps:Export:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
blob: aBlob,
|
|
|
|
success: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
aError => sendError(aError));
|
|
|
|
},
|
|
|
|
|
|
|
|
doImport: function(aMsg, aMm) {
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2014-10-15 05:55:14 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:Import:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
error: aError,
|
|
|
|
success: false
|
|
|
|
});
|
2015-04-02 20:53:27 +00:00
|
|
|
};
|
2014-10-15 05:55:14 +00:00
|
|
|
|
|
|
|
if (!aMsg.blob || !aMsg.blob instanceof Ci.nsIDOMBlob) {
|
|
|
|
sendError("NoBlobFound");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportExport.import(aMsg.blob).then(
|
|
|
|
([aManifestURL, aManifest]) => {
|
|
|
|
let app = this.getAppByManifestURL(aManifestURL);
|
|
|
|
app.manifest = aManifest;
|
|
|
|
aMm.sendAsyncMessage("Webapps:Import:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
app: app,
|
|
|
|
success: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
aError => sendError(aError));
|
|
|
|
},
|
|
|
|
|
|
|
|
doExtractManifest: function(aMsg, aMm) {
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2014-10-15 05:55:14 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:ExtractManifest:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
error: aError,
|
|
|
|
success: false
|
|
|
|
});
|
2015-04-02 20:53:27 +00:00
|
|
|
};
|
2014-10-15 05:55:14 +00:00
|
|
|
|
|
|
|
if (!aMsg.blob || !aMsg.blob instanceof Ci.nsIDOMBlob) {
|
|
|
|
sendError("NoBlobFound");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportExport.extractManifest(aMsg.blob).then(
|
|
|
|
aManifest => {
|
|
|
|
aMm.sendAsyncMessage("Webapps:ExtractManifest:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
manifest: aManifest,
|
|
|
|
success: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
aError => {
|
|
|
|
aMm.sendAsyncMessage("Webapps:ExtractManifest:Return",
|
|
|
|
{ requestID: aMsg.requestID,
|
|
|
|
oid: aMsg.oid,
|
|
|
|
error: aError,
|
|
|
|
success: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2014-05-24 19:28:13 +00:00
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
doLaunch: function (aData, aMm) {
|
|
|
|
this.launch(
|
|
|
|
aData.manifestURL,
|
|
|
|
aData.startPoint,
|
2013-09-06 18:18:21 +00:00
|
|
|
aData.timestamp,
|
2015-04-02 20:53:27 +00:00
|
|
|
() => {
|
|
|
|
aMm.sendAsyncMessage("Webapps:Launch:Return:OK", this.formatMessage(aData));
|
2013-05-06 13:51:53 +00:00
|
|
|
},
|
2015-04-02 20:53:27 +00:00
|
|
|
(reason) => {
|
2014-10-23 23:40:08 +00:00
|
|
|
aData.error = reason;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:Launch:Return:KO", this.formatMessage(aData));
|
2013-05-06 13:51:53 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-09-06 18:18:21 +00:00
|
|
|
launch: function launch(aManifestURL, aStartPoint, aTimeStamp, aOnSuccess, aOnFailure) {
|
2013-05-06 13:51:53 +00:00
|
|
|
let app = this.getAppByManifestURL(aManifestURL);
|
2012-09-27 01:01:20 +00:00
|
|
|
if (!app) {
|
2013-05-06 13:51:53 +00:00
|
|
|
aOnFailure("NO_SUCH_APP");
|
2012-09-27 01:01:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire an error when trying to launch an app that is not
|
|
|
|
// yet fully installed.
|
|
|
|
if (app.installState == "pending") {
|
2013-05-06 13:51:53 +00:00
|
|
|
aOnFailure("PENDING_APP_NOT_LAUNCHABLE");
|
2012-09-27 01:01:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 16:30:28 +00:00
|
|
|
// Delegate native android apps launch.
|
|
|
|
if (this.kAndroid == app.kind) {
|
|
|
|
debug("Launching android app " + app.origin);
|
|
|
|
let [packageName, className] =
|
|
|
|
AndroidUtils.getPackageAndClassFromManifestURL(aManifestURL);
|
|
|
|
debug(" " + packageName + " " + className);
|
|
|
|
Messaging.sendRequest({ type: "Apps:Launch",
|
|
|
|
packagename: packageName,
|
|
|
|
classname: className });
|
|
|
|
aOnSuccess();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:51:53 +00: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 18:18:21 +00:00
|
|
|
appClone.timestamp = aTimeStamp;
|
2013-05-06 13:51:53 +00: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 01:01:20 +00:00
|
|
|
},
|
|
|
|
|
2012-12-21 18:28:55 +00:00
|
|
|
cancelDownload: function cancelDownload(aManifestURL, aError) {
|
2012-12-18 21:43:13 +00:00
|
|
|
debug("cancelDownload " + aManifestURL);
|
2012-12-21 18:28:55 +00:00
|
|
|
let error = aError || "DOWNLOAD_CANCELED";
|
|
|
|
let download = AppDownloadManager.get(aManifestURL);
|
2012-12-18 21:43:13 +00:00
|
|
|
if (!download) {
|
|
|
|
debug("Could not find a download for " + aManifestURL);
|
2012-09-27 19:34:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 00:55:50 +00:00
|
|
|
let app = this.webapps[download.appId];
|
|
|
|
|
2012-12-18 21:43:13 +00:00
|
|
|
if (download.cacheUpdate) {
|
|
|
|
try {
|
|
|
|
download.cacheUpdate.cancel();
|
2013-01-25 02:24:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
debug (e);
|
|
|
|
}
|
2012-12-18 21:43:13 +00:00
|
|
|
} else if (download.channel) {
|
2013-01-17 00:55:50 +00:00
|
|
|
try {
|
|
|
|
download.channel.cancel(Cr.NS_BINDING_ABORTED);
|
2013-10-17 12:47:58 +00:00
|
|
|
} catch(e) { }
|
2012-12-18 21:43:13 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-20 23:32:15 +00: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 00:26:24 +00:00
|
|
|
this._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 12:47:58 +00:00
|
|
|
app: {
|
|
|
|
progress: 0,
|
|
|
|
installState: download.previousState,
|
|
|
|
downloading: false
|
|
|
|
},
|
|
|
|
error: error,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
})
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 12:47:58 +00:00
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.remove(aManifestURL);
|
2012-09-27 01:01:20 +00:00
|
|
|
},
|
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
startDownload: Task.async(function*(aManifestURL) {
|
2012-10-30 00:17:47 +00:00
|
|
|
debug("startDownload for " + aManifestURL);
|
2014-04-25 15:49:00 +00:00
|
|
|
|
2012-10-30 00:17:47 +00:00
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
|
|
|
let app = this.webapps[id];
|
2014-06-20 23:32:15 +00:00
|
|
|
|
2012-09-29 17:57:18 +00:00
|
|
|
if (!app) {
|
2012-10-30 00:17:47 +00:00
|
|
|
debug("startDownload: No app found for " + aManifestURL);
|
2014-06-06 19:10:59 +00:00
|
|
|
throw new Error("NO_SUCH_APP");
|
2012-09-29 17:57:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 09:40:52 +00:00
|
|
|
if (app.downloading) {
|
|
|
|
debug("app is already downloading. Ignoring.");
|
2014-06-06 19:10:59 +00:00
|
|
|
throw new Error("APP_IS_DOWNLOADING");
|
2013-02-08 09:40:52 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 19:17:32 +00:00
|
|
|
// If the caller is trying to start a download but we have nothing to
|
|
|
|
// download, send an error.
|
|
|
|
if (!app.downloadAvailable) {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 12:47:58 +00:00
|
|
|
error: "NO_DOWNLOAD_AVAILABLE",
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 12:47:58 +00:00
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2014-06-06 19:10:59 +00:00
|
|
|
throw new Error("NO_DOWNLOAD_AVAILABLE");
|
2013-02-01 19:17:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 14:19:37 +00: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 16:57:45 +00: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 17:57:18 +00:00
|
|
|
// We need to get the update manifest here, not the webapp manifest.
|
2013-01-25 02:24:17 +00:00
|
|
|
// If this is an update, the update manifest is staged.
|
2014-02-21 19:54:03 +00:00
|
|
|
let file = FileUtils.getFile(DIRECTORY_NAME,
|
|
|
|
["webapps", id,
|
|
|
|
isUpdate ? "staged-update.webapp"
|
|
|
|
: "update.webapp"],
|
|
|
|
true);
|
2012-09-29 17:57:18 +00:00
|
|
|
|
2012-11-27 04:03:43 +00:00
|
|
|
if (!file.exists()) {
|
|
|
|
// This is a hosted app, let's check if it has an appcache
|
|
|
|
// and download it.
|
2014-04-25 15:49:00 +00:00
|
|
|
let results = yield this._readManifests([{ id: id }]);
|
2012-11-27 04:03:43 +00:00
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
let jsonManifest = results[0].manifest;
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2014-04-25 15:49:00 +00: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();
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-04-25 15:49:00 +00:00
|
|
|
app: app,
|
|
|
|
manifest: jsonManifest,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2014-04-25 15:49:00 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-04-25 15:49:00 +00:00
|
|
|
eventType: "downloadsuccess",
|
|
|
|
manifestURL: aManifestURL
|
|
|
|
});
|
|
|
|
}
|
2012-11-27 04:03:43 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
let json = yield AppsUtils.loadJSONAsync(file.path);
|
|
|
|
if (!json) {
|
|
|
|
debug("startDownload: No update manifest found at " + file.path + " " +
|
|
|
|
aManifestURL);
|
2014-06-06 19:10:59 +00:00
|
|
|
throw new Error("MISSING_UPDATE_MANIFEST");
|
2014-04-25 15:49:00 +00:00
|
|
|
}
|
2012-09-29 17:57:18 +00:00
|
|
|
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest = new ManifestHelper(json, app.origin, app.manifestURL);
|
2014-07-21 09:26:58 +00: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 15:49:00 +00: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 09:26:58 +00:00
|
|
|
let manFile = OS.Path.join(OS.Constants.Path.tmpDir, "webapps", newId,
|
2014-04-25 15:49:00 +00:00
|
|
|
"manifest.webapp");
|
2014-07-21 09:26:58 +00:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(newManifest));
|
|
|
|
|
|
|
|
app = this.webapps[id];
|
2014-04-25 15:49:00 +00:00
|
|
|
|
|
|
|
// Set state and fire events.
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
app.readyToApplyDownload = true;
|
|
|
|
app.updateTime = Date.now();
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-04-25 15:49:00 +00:00
|
|
|
app: app,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-04-25 15:49:00 +00:00
|
|
|
eventType: "downloadsuccess",
|
|
|
|
manifestURL: aManifestURL
|
|
|
|
});
|
|
|
|
if (app.installState == "pending") {
|
|
|
|
// We restarted a failed download, apply it automatically.
|
|
|
|
this.applyDownload(aManifestURL);
|
|
|
|
}
|
|
|
|
}),
|
2012-09-29 17:57:18 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
applyDownload: Task.async(function*(aManifestURL) {
|
2012-10-30 00:17:47 +00:00
|
|
|
debug("applyDownload for " + aManifestURL);
|
2012-11-30 16:57:45 +00:00
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
|
|
|
let app = this.webapps[id];
|
2014-06-06 19:10:59 +00:00
|
|
|
if (!app) {
|
|
|
|
throw new Error("NO_SUCH_APP");
|
|
|
|
}
|
|
|
|
if (!app.readyToApplyDownload) {
|
|
|
|
throw new Error("NOT_READY_TO_APPLY_DOWNLOAD");
|
2012-09-29 17:57:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 19:22:31 +00:00
|
|
|
// We need to get the old manifest to unregister web activities.
|
2014-06-06 19:10:59 +00: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 17:57:18 +00:00
|
|
|
|
2014-12-18 10:13:31 +00:00
|
|
|
// In order to better control the potential inconsistency due to unexpected
|
|
|
|
// shutdown during the update process, a separate folder is used to accommodate
|
|
|
|
// the updated files and to replace the current one. Some sanity check and
|
|
|
|
// correspondent rollback logic may be necessary during the initialization
|
|
|
|
// of this component to recover it at next system boot-up.
|
|
|
|
let oldDir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id + ".new"], true, true);
|
2014-06-06 19:10:59 +00:00
|
|
|
appFile.moveTo(dir, "application.zip");
|
|
|
|
manFile.moveTo(dir, "manifest.webapp");
|
2012-09-29 17:57:18 +00:00
|
|
|
|
2014-12-18 10:13:31 +00:00
|
|
|
// Copy the staged update manifest to a non staged one.
|
|
|
|
let staged = oldDir.clone();
|
2014-06-06 19:10:59 +00:00
|
|
|
staged.append("staged-update.webapp");
|
2013-01-25 02:24:17 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
// If we are applying after a restarted download, we have no
|
|
|
|
// staged update manifest.
|
|
|
|
if (staged.exists()) {
|
2014-12-18 10:13:31 +00:00
|
|
|
staged.copyTo(dir, "update.webapp");
|
|
|
|
}
|
|
|
|
|
|
|
|
oldDir.moveTo(null, id + ".old");
|
|
|
|
dir.moveTo(null, id);
|
|
|
|
|
|
|
|
try {
|
|
|
|
oldDir.remove(true);
|
|
|
|
} catch(e) {
|
|
|
|
oldDir.moveTo(tmpDir, "old." + app.updateTime);
|
2014-06-06 19:10:59 +00:00
|
|
|
}
|
2013-01-25 02:24:17 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) { }
|
2012-09-29 17:57:18 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
2013-01-25 02:24:17 +00:00
|
|
|
|
2014-06-06 19:10:59 +00: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 16:57:45 +00:00
|
|
|
|
2014-06-06 19:10:59 +00: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 19:22:31 +00:00
|
|
|
|
2014-06-06 19:10:59 +00: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 19:22:31 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
delete app.retryingDownload;
|
2013-02-28 19:22:31 +00:00
|
|
|
|
2015-10-29 14:32:50 +00:00
|
|
|
// Once updated we are not in the blocklist anymore.
|
|
|
|
app.blockedStatus = Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
|
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
// Update the asm.js scripts we need to compile.
|
|
|
|
yield ScriptPreloader.preload(app, newManifest);
|
2015-01-10 23:00:27 +00:00
|
|
|
|
|
|
|
// Update langpack information.
|
|
|
|
Langpacks.register(app, newManifest);
|
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
yield this._saveApps();
|
|
|
|
// Update the handlers and permissions for this app.
|
|
|
|
this.updateAppHandlers(oldManifest, newManifest, app);
|
2014-03-15 21:37:37 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
let updateManifest = yield AppsUtils.loadJSONAsync(staged.path);
|
|
|
|
let appObject = AppsUtils.cloneAppObject(app);
|
|
|
|
appObject.updateManifest = updateManifest;
|
|
|
|
this.notifyUpdateHandlers(appObject, newManifest, appFile.path);
|
2014-03-15 21:37:37 +00:00
|
|
|
|
2014-06-06 19:10:59 +00:00
|
|
|
if (supportUseCurrentProfile()) {
|
|
|
|
PermissionsInstaller.installPermissions(
|
|
|
|
{ manifest: newManifest,
|
|
|
|
origin: app.origin,
|
2015-10-12 12:20:00 +00:00
|
|
|
manifestURL: app.manifestURL },
|
2014-06-06 19:10:59 +00:00
|
|
|
true);
|
|
|
|
}
|
|
|
|
this.updateDataStore(this.webapps[id].localId, app.origin,
|
2014-06-19 14:03:54 +00:00
|
|
|
app.manifestURL, newManifest);
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-06-06 19:10:59 +00:00
|
|
|
app: app,
|
|
|
|
manifest: newManifest,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-06-06 19:10:59 +00:00
|
|
|
eventType: "downloadapplied",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
|
|
|
}),
|
2012-09-29 17:57:18 +00:00
|
|
|
|
2013-08-29 14:00:33 +00:00
|
|
|
startOfflineCacheDownload: function(aManifest, aApp, aProfileDir, aIsUpdate) {
|
2014-09-16 20:14:02 +00:00
|
|
|
debug("startOfflineCacheDownload " + aApp.id + " " + aApp.kind);
|
2015-08-21 17:00:54 +00:00
|
|
|
if (aApp.kind !== this.kHostedAppcache || !aManifest.appcache_path) {
|
2012-12-18 21:43:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-16 20:14:02 +00:00
|
|
|
debug("startOfflineCacheDownload " + aManifest.appcache_path);
|
2012-12-18 21:43:13 +00:00
|
|
|
|
|
|
|
// 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 08:37:49 +00:00
|
|
|
// We set the 'downloading' flag and update the apps registry right before
|
|
|
|
// starting the app download/update.
|
2012-12-18 21:43:13 +00:00
|
|
|
aApp.downloading = true;
|
2013-02-11 08:38:51 +00:00
|
|
|
aApp.progress = 0;
|
2014-01-17 00:26:24 +00:00
|
|
|
DOMApplicationRegistry._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-06-25 20:47:00 +00:00
|
|
|
// Clear any previous errors.
|
|
|
|
error: null,
|
2013-10-17 12:47:58 +00:00
|
|
|
app: {
|
|
|
|
downloading: true,
|
|
|
|
installState: aApp.installState,
|
|
|
|
progress: 0
|
|
|
|
},
|
2014-05-24 19:28:13 +00:00
|
|
|
id: aApp.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
2015-10-02 16:16:07 +00:00
|
|
|
let appURI = NetUtil.newURI(aApp.origin, null, null);
|
|
|
|
let principal =
|
|
|
|
Services.scriptSecurityManager.createCodebasePrincipal(appURI,
|
|
|
|
{appId: aApp.localId});
|
2014-02-04 00:52:57 +00:00
|
|
|
let cacheUpdate = updateSvc.scheduleAppUpdate(
|
2015-10-02 16:16:07 +00:00
|
|
|
appcacheURI, docURI, principal, aApp.localId, false, aProfileDir);
|
2013-02-19 08:37:49 +00: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 17:54:06 +00:00
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2013-01-18 17:54:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the MD5 hash of the manifest.
|
|
|
|
computeManifestHash: function(aManifest) {
|
2013-08-02 00:00:22 +00:00
|
|
|
return AppsUtils.computeHash(JSON.stringify(aManifest));
|
2013-01-18 17:54:06 +00:00
|
|
|
},
|
|
|
|
|
2013-05-14 19:00:09 +00:00
|
|
|
// Updates the redirect mapping, activities and system message handlers.
|
2013-02-28 19:22:31 +00:00
|
|
|
// aOldManifest can be null if we don't have any handler to unregister.
|
|
|
|
updateAppHandlers: function(aOldManifest, aNewManifest, aApp) {
|
2014-12-16 23:32:28 +00:00
|
|
|
debug("updateAppHandlers: old=" + uneval(aOldManifest) +
|
|
|
|
" new=" + uneval(aNewManifest));
|
2013-02-28 19:22:31 +00:00
|
|
|
this.notifyAppsRegistryStart();
|
2013-05-14 19:00:09 +00:00
|
|
|
if (aApp.appStatus >= Ci.nsIPrincipal.APP_STATUS_PRIVILEGED) {
|
|
|
|
aApp.redirects = this.sanitizeRedirects(aNewManifest.redirects);
|
|
|
|
}
|
2013-04-30 13:01:46 +00:00
|
|
|
|
2014-10-07 16:23:00 +00:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aNewManifest, aApp.origin, aApp.manifestURL);
|
|
|
|
this._saveWidgetsFullPath(manifest, aApp);
|
|
|
|
|
2014-12-16 23:32:28 +00:00
|
|
|
aApp.role = manifest.role ? manifest.role : "";
|
|
|
|
|
2013-04-30 13:01:46 +00:00
|
|
|
if (supportSystemMessages()) {
|
|
|
|
if (aOldManifest) {
|
|
|
|
this._unregisterActivities(aOldManifest, aApp);
|
|
|
|
}
|
|
|
|
this._registerSystemMessages(aNewManifest, aApp);
|
|
|
|
this._registerActivities(aNewManifest, aApp, true);
|
2013-08-16 09:48:37 +00:00
|
|
|
this._registerInterAppConnections(aNewManifest, aApp);
|
2013-04-30 13:01:46 +00:00
|
|
|
} else {
|
|
|
|
// Nothing else to do but notifying we're ready.
|
|
|
|
this.notifyAppsRegistryReady();
|
2013-02-28 19:22:31 +00:00
|
|
|
}
|
2014-12-16 23:32:28 +00:00
|
|
|
|
2015-01-10 23:00:27 +00:00
|
|
|
// Update user customizations and langpacks.
|
2014-12-16 23:32:28 +00:00
|
|
|
if (aOldManifest) {
|
2015-08-14 23:55:09 +00:00
|
|
|
UserCustomizations.unregister(aApp);
|
2015-01-10 23:00:27 +00:00
|
|
|
Langpacks.unregister(aApp, aOldManifest);
|
2014-12-16 23:32:28 +00:00
|
|
|
}
|
2015-08-14 23:55:09 +00:00
|
|
|
UserCustomizations.register(aApp);
|
2015-01-10 23:00:27 +00:00
|
|
|
Langpacks.register(aApp, aNewManifest);
|
2013-02-28 19:22:31 +00:00
|
|
|
},
|
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
checkForUpdate: function(aData, aMm) {
|
2012-10-30 00:17:47 +00:00
|
|
|
debug("checkForUpdate for " + aData.manifestURL);
|
2013-02-08 09:40:52 +00:00
|
|
|
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2014-05-24 19:28:13 +00:00
|
|
|
debug("checkForUpdate error " + aError);
|
2013-02-08 09:40:52 +00:00
|
|
|
aData.error = aError;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", this.formatMessage(aData));
|
|
|
|
};
|
2013-02-08 09:40:52 +00:00
|
|
|
|
2012-10-30 00:17:47 +00:00
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
2012-10-18 10:32:03 +00:00
|
|
|
|
2013-02-19 08:37:49 +00: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 01:01:20 +00:00
|
|
|
|
2013-02-19 08:37:49 +00:00
|
|
|
// We may be able to remove this when Bug 839071 is fixed.
|
|
|
|
if (app.downloading) {
|
|
|
|
sendError("APP_IS_DOWNLOADING");
|
|
|
|
return;
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 22:50:37 +00:00
|
|
|
// If the app is packaged and its manifestURL has an app:// scheme,
|
2015-09-14 16:30:28 +00:00
|
|
|
// or if it's a native Android app then we can't have an update.
|
|
|
|
if (app.kind == this.kAndroid ||
|
|
|
|
(app.kind == this.kPackaged && app.manifestURL.startsWith("app://"))) {
|
2014-05-24 19:28:13 +00:00
|
|
|
sendError("NOT_UPDATABLE");
|
2014-01-31 22:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-10 17:06:59 +00: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 15:06:07 +00:00
|
|
|
let onlyCheckAppCache = false;
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-10-17 12:47:58 +00:00
|
|
|
let appDir = FileUtils.getDir("coreAppsDir", ["webapps"], false);
|
|
|
|
onlyCheckAppCache = (app.basePath == appDir.path);
|
2013-01-09 15:06:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (onlyCheckAppCache) {
|
2014-08-01 17:41:52 +00:00
|
|
|
// Bail out for packaged apps & hosted apps without appcache.
|
2015-08-21 17:00:54 +00:00
|
|
|
if (aApp.kind !== this.kHostedAppcache) {
|
2014-05-24 19:28:13 +00:00
|
|
|
sendError("NOT_UPDATABLE");
|
2012-12-17 19:11:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-01 17:41:52 +00:00
|
|
|
// We need the manifest to get the appcache path.
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests([{ id: id }]).then((aResult) => {
|
2012-12-17 19:11:05 +00:00
|
|
|
debug("Checking only appcache for " + aData.manifestURL);
|
2014-08-01 17:41:52 +00:00
|
|
|
let manifest = aResult[0].manifest;
|
2014-09-16 20:14:02 +00:00
|
|
|
if (!manifest.appcache_path) {
|
|
|
|
sendError("NOT_UPDATABLE");
|
|
|
|
return;
|
|
|
|
}
|
2012-12-17 19:11:05 +00:00
|
|
|
// Check if the appcache is updatable, and send "downloadavailable" or
|
|
|
|
// "downloadapplied".
|
|
|
|
let updateObserver = {
|
|
|
|
observe: function(aSubject, aTopic, aObsData) {
|
2013-01-14 17:47:55 +00:00
|
|
|
debug("onlyCheckAppCache updateSvc.checkForUpdate return for " +
|
|
|
|
app.manifestURL + " - event is " + aTopic);
|
|
|
|
if (aTopic == "offline-cache-update-available") {
|
2013-01-10 17:06:59 +00:00
|
|
|
app.downloadAvailable = true;
|
2014-01-17 00:26:24 +00:00
|
|
|
this._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 12:47:58 +00:00
|
|
|
app: app,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 12:47:58 +00:00
|
|
|
eventType: "downloadavailable",
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
2013-02-19 08:37:49 +00:00
|
|
|
});
|
2012-12-17 19:11:05 +00:00
|
|
|
} else {
|
2014-05-24 19:28:13 +00:00
|
|
|
sendError("NOT_UPDATABLE");
|
2012-12-17 19:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 12:47:58 +00:00
|
|
|
};
|
2014-07-30 21:00:15 +00:00
|
|
|
let helper =
|
|
|
|
new ManifestHelper(manifest, aData.origin, aData.manifestURL);
|
2013-01-14 17:47:55 +00:00
|
|
|
debug("onlyCheckAppCache - launch updateSvc.checkForUpdate for " +
|
|
|
|
helper.fullAppcachePath());
|
2015-10-02 16:16:07 +00:00
|
|
|
let appURI = NetUtil.newURI(aApp.origin, null, null);
|
|
|
|
let principal =
|
|
|
|
Services.scriptSecurityManager.createCodebasePrincipal(appURI,
|
|
|
|
{appId: aApp.localId});
|
2013-01-14 17:47:55 +00:00
|
|
|
updateSvc.checkForUpdate(Services.io.newURI(helper.fullAppcachePath(), null, null),
|
2015-10-02 16:16:07 +00:00
|
|
|
principal, app.localId, false, updateObserver);
|
2012-12-17 19:11:05 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-14 17:23:58 +00:00
|
|
|
// On xhr load request event
|
|
|
|
function onload(xhr, oldManifest) {
|
2013-01-09 15:06:07 +00:00
|
|
|
debug("Got http status=" + xhr.status + " for " + aData.manifestURL);
|
2013-01-18 17:54:06 +00:00
|
|
|
let oldHash = app.manifestHash;
|
2014-08-01 17:41:52 +00:00
|
|
|
let isPackage = app.kind == DOMApplicationRegistry.kPackaged;
|
2013-01-18 17:54:06 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
if (xhr.status == 200) {
|
2012-12-03 19:38:09 +00:00
|
|
|
let manifest = xhr.response;
|
|
|
|
if (manifest == null) {
|
2012-09-27 01:01:20 +00:00
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-18 17:54:06 +00:00
|
|
|
|
2013-01-11 14:08:12 +00:00
|
|
|
if (!AppsUtils.checkManifest(manifest, app)) {
|
2012-09-27 01:01:20 +00:00
|
|
|
sendError("INVALID_MANIFEST");
|
2013-01-10 17:06:59 +00:00
|
|
|
return;
|
2012-11-18 22:43:38 +00:00
|
|
|
} else if (!AppsUtils.checkInstallAllowed(manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
2013-01-10 17:06:59 +00:00
|
|
|
return;
|
2012-09-27 01:01:20 +00:00
|
|
|
} else {
|
2013-02-14 17:23:58 +00:00
|
|
|
AppsUtils.ensureSameAppName(oldManifest, manifest, app);
|
|
|
|
|
2013-01-18 17:54:06 +00:00
|
|
|
let hash = this.computeManifestHash(manifest);
|
|
|
|
debug("Manifest hash = " + hash);
|
2013-01-25 02:24:17 +00: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 17:54:06 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
app.lastCheckedUpdate = Date.now();
|
2013-01-25 02:24:17 +00:00
|
|
|
if (isPackage) {
|
2013-01-18 17:54:06 +00:00
|
|
|
if (oldHash != hash) {
|
2014-01-31 22:50:37 +00:00
|
|
|
this.updatePackagedApp(aData, id, app, manifest);
|
2013-01-18 17:54:06 +00:00
|
|
|
} else {
|
2014-01-17 00:26:24 +00:00
|
|
|
this._saveApps().then(() => {
|
2013-10-17 12:47:58 +00: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 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
|
|
|
aMm.sendAsyncMessage("Webapps:FireEvent", {
|
|
|
|
eventType: eventType,
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
});
|
2013-01-18 17:54:06 +00:00
|
|
|
}
|
2012-09-27 01:01:20 +00:00
|
|
|
} else {
|
2013-02-14 17:23:58 +00:00
|
|
|
// Update only the appcache if the manifest has not changed
|
|
|
|
// based on the hash value.
|
2014-10-14 19:28:16 +00:00
|
|
|
if (oldHash == hash) {
|
|
|
|
debug("Update - oldhash");
|
|
|
|
this.updateHostedApp(aData, id, app, oldManifest, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For hosted apps and hosted apps with appcache, use the
|
|
|
|
// manifest "as is".
|
2015-08-21 17:00:54 +00:00
|
|
|
this.updateHostedApp(aData, id, app, oldManifest, manifest);
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (xhr.status == 304) {
|
2013-01-10 17:06:59 +00:00
|
|
|
// The manifest has not changed.
|
2013-01-25 02:24:17 +00:00
|
|
|
if (isPackage) {
|
2013-01-10 17:06:59 +00:00
|
|
|
app.lastCheckedUpdate = Date.now();
|
2014-01-17 00:26:24 +00:00
|
|
|
this._saveApps().then(() => {
|
2013-10-17 12:47:58 +00: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 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
|
|
|
aMm.sendAsyncMessage("Webapps:FireEvent", {
|
|
|
|
eventType: eventType,
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
});
|
2013-01-10 17:06:59 +00:00
|
|
|
} else {
|
|
|
|
// For hosted apps, even if the manifest has not changed, we check
|
|
|
|
// for offline cache updates.
|
2014-01-31 22:50:37 +00:00
|
|
|
this.updateHostedApp(aData, id, app, oldManifest, null);
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
2013-02-14 17:23:58 +00:00
|
|
|
}
|
2012-09-27 01:01:20 +00:00
|
|
|
|
2013-02-14 17:23:58 +00:00
|
|
|
// Try to download a new manifest.
|
2013-05-06 17:41:07 +00:00
|
|
|
function doRequest(oldManifest, headers) {
|
|
|
|
headers = headers || [];
|
2013-02-14 17:23:58 +00: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 17:41:07 +00:00
|
|
|
headers.forEach(function(aHeader) {
|
|
|
|
debug("Adding header: " + aHeader.name + ": " + aHeader.value);
|
|
|
|
xhr.setRequestHeader(aHeader.name, aHeader.value);
|
|
|
|
});
|
2013-02-14 17:23:58 +00:00
|
|
|
xhr.responseType = "json";
|
|
|
|
if (app.etag) {
|
|
|
|
debug("adding manifest etag:" + app.etag);
|
|
|
|
xhr.setRequestHeader("If-None-Match", app.etag);
|
|
|
|
}
|
|
|
|
xhr.channel.notificationCallbacks =
|
2014-09-22 14:58:59 +00:00
|
|
|
AppsUtils.createLoadContext(app.installerAppId, app.installerIsBrowser);
|
2012-09-27 01:01:20 +00:00
|
|
|
|
2013-02-14 17:23:58 +00: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 00:26:24 +00:00
|
|
|
this._readManifests([{ id: id }]).then((aResult) => {
|
2013-05-06 17:41:07 +00: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 00:26:24 +00:00
|
|
|
});
|
2012-09-27 01:01:20 +00:00
|
|
|
},
|
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
updatePackagedApp: Task.async(function*(aData, aId, aApp, aNewManifest) {
|
2014-01-31 22:50:37 +00: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 15:49:00 +00:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(aNewManifest));
|
2014-01-31 22:50:37 +00:00
|
|
|
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aNewManifest, aApp.origin, aApp.manifestURL);
|
2014-01-31 22:50:37 +00: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 14:14:08 +00:00
|
|
|
this._saveWidgetsFullPath(manifest, aApp);
|
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
yield this._saveApps();
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-04-25 15:49:00 +00:00
|
|
|
app: aApp,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: aApp.id
|
2014-01-31 22:50:37 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-04-25 15:49:00 +00:00
|
|
|
eventType: "downloadavailable",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
}),
|
2014-01-31 22:50:37 +00: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 15:49:00 +00:00
|
|
|
updateHostedApp: Task.async(function*(aData, aId, aApp, aOldManifest, aNewManifest) {
|
2014-01-31 22:50:37 +00: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 00:20:27 +00:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(aApp.manifest, aApp.origin, aApp.manifestURL);
|
|
|
|
aApp.role = manifest.role || "";
|
|
|
|
|
|
|
|
if (!AppsUtils.checkAppRole(aApp.role, aApp.appStatus)) {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-08-29 00:20:27 +00:00
|
|
|
app: aApp,
|
|
|
|
manifest: aApp.manifest,
|
|
|
|
id: aApp.id
|
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-08-29 00:20:27 +00:00
|
|
|
eventType: "downloadapplied",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
delete aApp.manifest;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-31 22:50:37 +00:00
|
|
|
if (aNewManifest) {
|
|
|
|
this.updateAppHandlers(aOldManifest, aNewManifest, aApp);
|
2014-03-15 21:37:37 +00:00
|
|
|
this.notifyUpdateHandlers(AppsUtils.cloneAppObject(aApp), aNewManifest);
|
|
|
|
|
2014-01-31 22:50:37 +00:00
|
|
|
// Store the new manifest.
|
|
|
|
let dir = this._getAppDir(aId).path;
|
|
|
|
let manFile = OS.Path.join(dir, "manifest.webapp");
|
2014-04-25 15:49:00 +00:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(aNewManifest));
|
|
|
|
|
2014-07-30 21:00:15 +00:00
|
|
|
manifest =
|
|
|
|
new ManifestHelper(aNewManifest, aApp.origin, aApp.manifestURL);
|
2014-01-31 22:50:37 +00:00
|
|
|
|
|
|
|
if (supportUseCurrentProfile()) {
|
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({
|
|
|
|
manifest: aApp.manifest,
|
|
|
|
origin: aApp.origin,
|
2015-10-12 12:20:00 +00:00
|
|
|
manifestURL: aData.manifestURL
|
2014-01-31 22:50:37 +00:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateDataStore(this.webapps[aId].localId, aApp.origin,
|
2014-06-19 14:03:54 +00:00
|
|
|
aApp.manifestURL, aApp.manifest);
|
2014-01-31 22:50:37 +00:00
|
|
|
|
2014-06-23 00:22:07 +00:00
|
|
|
aApp.name = aNewManifest.name;
|
2014-01-31 22:50:37 +00:00
|
|
|
aApp.csp = manifest.csp || "";
|
|
|
|
aApp.updateTime = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the registry.
|
|
|
|
this.webapps[aId] = aApp;
|
2014-04-25 15:49:00 +00:00
|
|
|
yield this._saveApps();
|
|
|
|
|
2015-08-21 17:00:54 +00:00
|
|
|
if (aApp.kind !== this.kHostedAppcache || !aApp.manifest.appcache_path) {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-04-25 15:49:00 +00:00
|
|
|
app: aApp,
|
|
|
|
manifest: aApp.manifest,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: aApp.id
|
2014-04-25 15:49:00 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-04-25 15:49:00 +00:00
|
|
|
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();
|
2015-10-02 16:16:07 +00:00
|
|
|
let appURI = NetUtil.newURI(aApp.origin, null, null);
|
|
|
|
let principal =
|
|
|
|
Services.scriptSecurityManager.createCodebasePrincipal(appURI,
|
|
|
|
{appId: aApp.localId});
|
2014-04-25 15:49:00 +00:00
|
|
|
|
|
|
|
updateSvc.checkForUpdate(Services.io.newURI(manifest.fullAppcachePath(), null, null),
|
2015-10-02 16:16:07 +00:00
|
|
|
principal, aApp.localId, false,
|
2014-04-25 15:49:00 +00:00
|
|
|
(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();
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-04-25 15:49:00 +00:00
|
|
|
app: aApp,
|
|
|
|
manifest: aApp.manifest,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: aApp.id
|
2014-04-25 15:49:00 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-04-25 15:49:00 +00:00
|
|
|
eventType: eventType,
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
requestID: aData.requestID
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete aApp.manifest;
|
|
|
|
}),
|
2014-01-31 22:50:37 +00:00
|
|
|
|
2012-12-10 23:48:59 +00:00
|
|
|
// Downloads the manifest and run checks, then eventually triggers the
|
|
|
|
// installation UI.
|
|
|
|
doInstall: function doInstall(aData, aMm) {
|
2013-01-21 19:13:35 +00:00
|
|
|
let app = aData.app;
|
|
|
|
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2012-12-10 23:48:59 +00:00
|
|
|
aData.error = aError;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:Install:Return:KO", this.formatMessage(aData));
|
2012-12-10 23:48:59 +00:00
|
|
|
Cu.reportError("Error installing app from: " + app.installOrigin +
|
|
|
|
": " + aError);
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
this.popContentAction(aData.oid);
|
2015-04-02 20:53:27 +00:00
|
|
|
};
|
2012-12-10 23:48:59 +00:00
|
|
|
|
2014-01-23 14:46:20 +00:00
|
|
|
if (app.receipts.length > 0) {
|
|
|
|
for (let receipt of app.receipts) {
|
|
|
|
let error = this.isReceipt(receipt);
|
|
|
|
if (error) {
|
|
|
|
sendError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 23:48:59 +00:00
|
|
|
// Hosted apps can't be trusted or certified, so just check that the
|
|
|
|
// manifest doesn't ask for those.
|
|
|
|
function checkAppStatus(aManifest) {
|
2015-04-13 16:49:50 +00:00
|
|
|
try {
|
|
|
|
// Everything is authorized in developer mode.
|
|
|
|
if (Services.prefs.getBoolPref("dom.apps.developer_mode")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
|
2012-12-10 23:48:59 +00:00
|
|
|
let manifestStatus = aManifest.type || "web";
|
2014-09-16 20:14:02 +00:00
|
|
|
return manifestStatus === "web" ||
|
|
|
|
manifestStatus === "trusted";
|
2012-12-10 23:48:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 18:18:55 +00:00
|
|
|
let checkManifest = (function() {
|
|
|
|
if (!app.manifest) {
|
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-22 20:08:21 +00:00
|
|
|
// Disallow reinstalls from the same manifest url for now.
|
2014-01-09 18:18:55 +00:00
|
|
|
for (let id in this.webapps) {
|
2014-07-22 20:08:21 +00:00
|
|
|
if (this.webapps[id].manifestURL == app.manifestURL &&
|
2014-01-09 18:18:55 +00:00
|
|
|
this._isLaunchable(this.webapps[id])) {
|
2014-07-22 20:08:21 +00:00
|
|
|
sendError("REINSTALL_FORBIDDEN");
|
2014-01-09 18:18:55 +00: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 00:20:27 +00:00
|
|
|
app.role = app.manifest.role || "";
|
|
|
|
if (!AppsUtils.checkAppRole(app.role, app.appStatus)) {
|
|
|
|
sendError("INVALID_ROLE");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-09 18:18:55 +00:00
|
|
|
return true;
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
let installApp = (function() {
|
|
|
|
app.manifestHash = this.computeManifestHash(app.manifest);
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
|
|
|
|
// Check to see if the action has been cancelled in the interim.
|
|
|
|
let cancelled = this.actionCancelled(aData.oid);
|
|
|
|
this.popContentAction(aData.oid);
|
|
|
|
if (!cancelled) {
|
|
|
|
// 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));
|
|
|
|
}
|
2014-01-09 18:18:55 +00:00
|
|
|
}
|
|
|
|
}).bind(this);
|
|
|
|
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
// This action will be popped on success in installApp, or on
|
|
|
|
// failure in sendError.
|
|
|
|
this.pushContentAction(aData.oid);
|
|
|
|
|
2014-01-09 18:18:55 +00:00
|
|
|
// We may already have the manifest (e.g. AutoInstall),
|
|
|
|
// in which case we don't need to load it.
|
|
|
|
if (app.manifest) {
|
|
|
|
if (checkManifest()) {
|
2014-09-22 14:59:00 +00:00
|
|
|
debug("Installed manifest check OK");
|
2015-08-21 17:00:54 +00:00
|
|
|
installApp();
|
2014-09-22 14:59:00 +00:00
|
|
|
} else {
|
|
|
|
debug("Installed manifest check failed");
|
|
|
|
// checkManifest() sends error before return
|
2014-01-09 18:18:55 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-10 23:48:59 +00:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", app.manifestURL, true);
|
2013-01-09 10:02:08 +00:00
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2014-09-22 14:58:59 +00:00
|
|
|
xhr.channel.notificationCallbacks = AppsUtils.createLoadContext(aData.appId,
|
|
|
|
aData.isBrowser);
|
2012-12-10 23:48:59 +00:00
|
|
|
xhr.responseType = "json";
|
2012-12-10 23:49:02 +00:00
|
|
|
|
2012-12-10 23:48:59 +00:00
|
|
|
xhr.addEventListener("load", (function() {
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
if (!AppsUtils.checkManifestContentType(app.installOrigin, app.origin,
|
|
|
|
xhr.getResponseHeader("content-type"))) {
|
2014-07-06 16:47:28 +00:00
|
|
|
sendError("INVALID_MANIFEST_CONTENT_TYPE");
|
2012-12-10 23:48:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.manifest = xhr.response;
|
2014-01-09 18:18:55 +00:00
|
|
|
if (checkManifest()) {
|
2014-09-22 14:59:00 +00:00
|
|
|
debug("Downloaded manifest check OK");
|
2012-12-10 23:48:59 +00:00
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
2015-08-21 17:00:54 +00:00
|
|
|
installApp();
|
2014-09-22 14:59:00 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
debug("Downloaded manifest check failed");
|
|
|
|
// checkManifest() sends error before return
|
2012-12-10 23:48:59 +00: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 19:13:35 +00:00
|
|
|
let app = aData.app;
|
|
|
|
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2012-12-10 23:48:59 +00:00
|
|
|
aData.error = aError;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:Install:Return:KO", this.formatMessage(aData));
|
2012-12-10 23:48:59 +00:00
|
|
|
Cu.reportError("Error installing packaged app from: " +
|
|
|
|
app.installOrigin + ": " + aError);
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
this.popContentAction(aData.oid);
|
2015-04-02 20:53:27 +00:00
|
|
|
};
|
2012-12-10 23:48:59 +00:00
|
|
|
|
2014-01-23 14:46:20 +00: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 18:18:55 +00: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);
|
|
|
|
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
// Check to see if the action has been cancelled in the interim.
|
|
|
|
let cancelled = this.actionCancelled(aData.oid);
|
|
|
|
this.popContentAction(aData.oid);
|
|
|
|
if (!cancelled) {
|
|
|
|
// 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));
|
|
|
|
}
|
2014-01-09 18:18:55 +00:00
|
|
|
}
|
|
|
|
}).bind(this);
|
|
|
|
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
// This action will be popped on success in installApp, or on
|
|
|
|
// failure in sendError.
|
|
|
|
this.pushContentAction(aData.oid);
|
|
|
|
|
2014-01-09 18:18:55 +00:00
|
|
|
// 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-10 23:48:59 +00:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", app.manifestURL, true);
|
2013-01-09 10:02:08 +00:00
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2014-09-22 14:58:59 +00:00
|
|
|
xhr.channel.notificationCallbacks = AppsUtils.createLoadContext(aData.appId,
|
|
|
|
aData.isBrowser);
|
2012-12-10 23:48:59 +00: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 16:47:28 +00:00
|
|
|
sendError("INVALID_MANIFEST_CONTENT_TYPE");
|
2012-12-10 23:48:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-09 18:18:55 +00:00
|
|
|
app.updateManifest = xhr.response;
|
|
|
|
if (!app.updateManifest) {
|
2012-12-10 23:48:59 +00:00
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2014-01-09 18:18:55 +00:00
|
|
|
if (checkUpdateManifest()) {
|
2012-12-10 23:48:59 +00:00
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
2013-01-14 17:47:55 +00:00
|
|
|
debug("at install package got app etag=" + app.etag);
|
2014-01-09 18:18:55 +00:00
|
|
|
installApp();
|
2012-12-10 23:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.addEventListener("error", (function() {
|
|
|
|
sendError("NETWORK_ERROR");
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.send(null);
|
|
|
|
},
|
|
|
|
|
Bug 1077168 - Cancel in-flight Webapp install jobs from windows that change location. r=myk.
Installing a Webapp is an asynchronous job, and there is a pocket of time
between when web content requests to install an app and before the browser
displays an installation prompt that the outer window of the content can
browse away. This pocket of time is typically used by XHR to request the
web app resources and verify their contents.
This pocket of time is, essentially, bug 771294, and is a bit of a security
problem.
This problem was originally patched over on Desktop by checking in the parent
process that the outer window was still at the same URI as it had been when it
made the request. I'm not entirely sure if Android / B2G made similar checks.
With separated content processes, however, the browser front-end can no longer
performantly check to ensure that the outer window is at the same URI.
We solve this problem by sending up a message in the content process when
the location of an outer window making use of navigator.mozApps changes.
We hold a Map of "actions" mapping to in-flight installs mapped by the
outer window ID of the requesting content. When we notice a location
change, we mark those actions as cancelled. When the XHR returns, we have
it check the state of its actions, and if they're cancelled, it aborts
further action.
Normally, this wouldn't be necessary, since any XHR initiated by the
content window would be cancelled once the location changed, but in
this case, the XHR is occurring in Webapps.jsm, and is not influenced
by the outer window of the content.
--HG--
extra : rebase_source : 5f95002a21c96c5cbf3dca8ca265400448251b43
2015-02-10 18:18:47 +00:00
|
|
|
onLocationChange(oid) {
|
|
|
|
let action = this._contentActions.get(oid);
|
|
|
|
if (action) {
|
|
|
|
action.cancelled = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
pushContentAction: function(windowID) {
|
|
|
|
let actions = this._contentActions.get(windowID);
|
|
|
|
if (!actions) {
|
|
|
|
actions = {
|
|
|
|
count: 0,
|
|
|
|
cancelled: false,
|
|
|
|
};
|
|
|
|
this._contentActions.set(windowID, actions);
|
|
|
|
}
|
|
|
|
actions.count++;
|
|
|
|
},
|
|
|
|
|
|
|
|
popContentAction: function(windowID) {
|
|
|
|
let actions = this._contentActions.get(windowID);
|
|
|
|
if (!actions) {
|
|
|
|
Cu.reportError(`Failed to pop content action for window with ID ${windowID}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
actions.count--;
|
|
|
|
if (!actions.count) {
|
|
|
|
this._contentActions.delete(windowID);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actionCancelled: function(windowID) {
|
|
|
|
return this._contentActions.has(windowID) &&
|
|
|
|
this._contentActions.get(windowID).cancelled;
|
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
denyInstall: function(aData) {
|
2014-02-21 19:54:03 +00: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 15:38:33 +00:00
|
|
|
}
|
2014-02-21 19:54:03 +00:00
|
|
|
}
|
2015-04-02 20:53:27 +00:00
|
|
|
aData.mm.sendAsyncMessage("Webapps:Install:Return:KO", this.formatMessage(aData));
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2013-02-13 19:55:45 +00: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 14:58:44 +00:00
|
|
|
queuedPackageDownload: {},
|
2013-02-13 19:55:45 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
onInstallSuccessAck: Task.async(function*(aManifestURL, aDontNeedNetwork) {
|
2013-10-23 23:50:57 +00:00
|
|
|
// If we are offline, register to run when we'll be online.
|
2013-11-01 01:58:28 +00:00
|
|
|
if ((Services.io.offline) && !aDontNeedNetwork) {
|
2013-10-23 23:50:57 +00:00
|
|
|
let onlineWrapper = {
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
Services.obs.removeObserver(onlineWrapper,
|
|
|
|
"network:offline-status-changed");
|
|
|
|
DOMApplicationRegistry.onInstallSuccessAck(aManifestURL);
|
|
|
|
}
|
2013-11-01 01:58:28 +00:00
|
|
|
};
|
2013-10-23 23:50:57 +00:00
|
|
|
Services.obs.addObserver(onlineWrapper,
|
|
|
|
"network:offline-status-changed", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-06 14:58:44 +00:00
|
|
|
let cacheDownload = this.queuedDownload[aManifestURL];
|
|
|
|
if (cacheDownload) {
|
|
|
|
this.startOfflineCacheDownload(cacheDownload.manifest,
|
|
|
|
cacheDownload.app,
|
2013-08-29 14:00:33 +00:00
|
|
|
cacheDownload.profileDir);
|
2013-08-06 14:58:44 +00:00
|
|
|
delete this.queuedDownload[aManifestURL];
|
|
|
|
|
2013-02-13 19:55:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-08-06 14:58:44 +00:00
|
|
|
|
|
|
|
let packageDownload = this.queuedPackageDownload[aManifestURL];
|
|
|
|
if (packageDownload) {
|
|
|
|
let manifest = packageDownload.manifest;
|
2013-11-25 23:14:35 +00:00
|
|
|
let newApp = packageDownload.app;
|
2013-08-06 14:58:44 +00:00
|
|
|
let installSuccessCallback = packageDownload.callback;
|
|
|
|
|
|
|
|
delete this.queuedPackageDownload[aManifestURL];
|
|
|
|
|
2014-07-21 09:26:58 +00: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 14:58:44 +00:00
|
|
|
}
|
2014-07-21 09:26:58 +00:00
|
|
|
}),
|
2013-02-13 19:55:45 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
_setupApp: function(aData, aId) {
|
2011-11-28 20:13:26 +00:00
|
|
|
let app = aData.app;
|
2013-01-08 12:18:47 +00:00
|
|
|
|
2015-09-14 16:30:28 +00:00
|
|
|
// app can be uninstalled by default.
|
|
|
|
if (app.removable === undefined) {
|
|
|
|
app.removable = true;
|
|
|
|
}
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
if (aData.isPackage) {
|
2012-07-11 15:38:33 +00:00
|
|
|
// Override the origin with the correct id.
|
2013-11-25 23:14:35 +00:00
|
|
|
app.origin = "app://" + aId;
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
app.id = aId;
|
|
|
|
app.installTime = Date.now();
|
|
|
|
app.lastUpdateCheck = Date.now();
|
2011-12-06 04:22:01 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
return app;
|
|
|
|
},
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2014-06-23 00:22:07 +00:00
|
|
|
_cloneApp: function(aData, aNewApp, aLocaleManifest, aManifest, aId, aLocalId) {
|
2013-11-25 23:14:35 +00:00
|
|
|
let appObject = AppsUtils.cloneAppObject(aNewApp);
|
|
|
|
appObject.appStatus =
|
|
|
|
aNewApp.appStatus || Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
2012-09-27 01:01:20 +00:00
|
|
|
|
2014-09-16 20:14:02 +00:00
|
|
|
let usesAppcache = appObject.kind == this.kHostedAppcache;
|
|
|
|
|
|
|
|
if (usesAppcache) {
|
2012-09-27 01:01:20 +00:00
|
|
|
appObject.installState = "pending";
|
|
|
|
appObject.downloadAvailable = true;
|
|
|
|
appObject.downloading = true;
|
2012-09-27 19:34:41 +00:00
|
|
|
appObject.downloadSize = 0;
|
|
|
|
appObject.readyToApplyDownload = false;
|
2014-08-01 17:41:52 +00:00
|
|
|
} else if (appObject.kind == this.kPackaged) {
|
2012-09-27 19:34:41 +00:00
|
|
|
appObject.installState = "pending";
|
|
|
|
appObject.downloadAvailable = true;
|
|
|
|
appObject.downloading = true;
|
2014-06-23 00:22:07 +00:00
|
|
|
appObject.downloadSize = aLocaleManifest.size;
|
2012-09-27 01:01:20 +00:00
|
|
|
appObject.readyToApplyDownload = false;
|
2015-09-14 16:30:28 +00:00
|
|
|
} else if (appObject.kind == this.kHosted ||
|
|
|
|
appObject.kind == this.kAndroid) {
|
2012-09-27 01:01:20 +00:00
|
|
|
appObject.installState = "installed";
|
|
|
|
appObject.downloadAvailable = false;
|
|
|
|
appObject.downloading = false;
|
|
|
|
appObject.readyToApplyDownload = false;
|
2014-08-01 17:41:52 +00:00
|
|
|
} else {
|
|
|
|
debug("Unknown app kind: " + appObject.kind);
|
|
|
|
throw Error("Unknown app kind: " + appObject.kind);
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
appObject.localId = aLocalId;
|
2014-01-17 00:26:24 +00:00
|
|
|
appObject.basePath = OS.Path.dirname(this.appsFile);
|
2013-11-25 23:14:35 +00:00
|
|
|
appObject.name = aManifest.name;
|
2014-06-23 00:22:07 +00:00
|
|
|
appObject.csp = aLocaleManifest.csp || "";
|
|
|
|
appObject.role = aLocaleManifest.role;
|
2014-08-19 14:14:08 +00:00
|
|
|
this._saveWidgetsFullPath(aLocaleManifest, appObject);
|
2012-12-10 23:49:02 +00:00
|
|
|
appObject.installerAppId = aData.appId;
|
|
|
|
appObject.installerIsBrowser = aData.isBrowser;
|
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
return appObject;
|
|
|
|
},
|
|
|
|
|
2014-02-21 19:54:03 +00:00
|
|
|
_writeManifestFile: function(aId, aIsPackage, aJsonManifest) {
|
2013-11-25 23:14:35 +00: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 00:26:24 +00:00
|
|
|
|
2014-02-21 19:54:03 +00:00
|
|
|
let dir = this._getAppDir(aId).path;
|
|
|
|
let manFile = OS.Path.join(dir, manifestName);
|
2014-04-29 08:18:00 +00:00
|
|
|
return this._writeFile(manFile, JSON.stringify(aJsonManifest));
|
2013-11-25 23:14:35 +00:00
|
|
|
},
|
|
|
|
|
2014-04-13 18:42:39 +00: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 14:14:08 +00:00
|
|
|
let aLocaleManifest = new ManifestHelper(aManifest, app.origin, app.manifestURL);
|
|
|
|
this._saveWidgetsFullPath(aLocaleManifest, app);
|
|
|
|
|
2014-04-13 18:42:39 +00:00
|
|
|
app.appStatus = AppsUtils.getAppManifestStatus(aManifest);
|
|
|
|
|
|
|
|
// 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 00:20:27 +00:00
|
|
|
if (!AppsUtils.checkAppRole(app.role, app.appStatus)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-13 18:42:39 +00: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);
|
2015-10-29 14:32:50 +00:00
|
|
|
// If there is an id in the mini-manifest, use it for blocklisting purposes.
|
|
|
|
if (aData.isPackage && ("id" in aUpdateManifest)) {
|
|
|
|
this.webapps[app.id].blocklistId = aUpdateManifest["id"];
|
|
|
|
}
|
2014-04-13 18:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:AddApp",
|
2014-05-24 19:28:13 +00:00
|
|
|
{ id: app.id, app: app, manifest: aManifest });
|
2014-04-13 18:42:39 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2014-04-29 08:18:00 +00:00
|
|
|
confirmInstall: Task.async(function*(aData, aProfileDir, aInstallSuccessCallback) {
|
2013-11-25 23:14:35 +00: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 08:18:00 +00:00
|
|
|
yield this._writeManifestFile(id, aData.isPackage, jsonManifest);
|
2015-10-29 14:32:50 +00:00
|
|
|
// If there is an id in the mini-manifest, use it for blocklisting purposes.
|
|
|
|
if (aData.isPackage && ("id" in jsonManifest)) {
|
|
|
|
app.blocklistId = jsonManifest["id"];
|
|
|
|
}
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-02-28 23:25:21 +00:00
|
|
|
debug("app.origin: " + app.origin);
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-08-01 17:41:52 +00:00
|
|
|
// Set the application kind.
|
2014-09-16 20:14:02 +00:00
|
|
|
app.kind = this.appKind(app, manifest);
|
2014-08-01 17:41:52 +00:00
|
|
|
|
2014-06-23 00:22:07 +00:00
|
|
|
let appObject = this._cloneApp(aData, app, manifest, jsonManifest, id, localId);
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
this.webapps[id] = appObject;
|
2015-09-14 16:30:28 +00:00
|
|
|
this._manifestCache[id] = jsonManifest;
|
2012-10-03 05:38:06 +00:00
|
|
|
|
2012-10-10 21:29:07 +00:00
|
|
|
// For package apps, the permissions are not in the mini-manifest, so
|
|
|
|
// don't update the permissions yet.
|
|
|
|
if (!aData.isPackage) {
|
2013-07-07 20:41:55 +00:00
|
|
|
if (supportUseCurrentProfile()) {
|
2015-04-23 10:33:00 +00:00
|
|
|
try {
|
|
|
|
if (Services.prefs.getBoolPref("dom.apps.developer_mode")) {
|
|
|
|
this.webapps[id].appStatus =
|
|
|
|
AppsUtils.getAppManifestStatus(app.manifest);
|
|
|
|
}
|
|
|
|
} catch(e) {};
|
2013-11-25 23:14:35 +00:00
|
|
|
PermissionsInstaller.installPermissions(
|
|
|
|
{
|
|
|
|
origin: appObject.origin,
|
|
|
|
manifestURL: appObject.manifestURL,
|
2014-09-16 20:14:02 +00:00
|
|
|
manifest: jsonManifest,
|
|
|
|
kind: appObject.kind
|
2013-11-25 23:14:35 +00:00
|
|
|
},
|
|
|
|
isReinstall,
|
2014-06-11 21:23:18 +00:00
|
|
|
this.doUninstall.bind(this, aData, aData.mm)
|
2013-11-25 23:14:35 +00:00
|
|
|
);
|
2013-07-07 20:41:55 +00:00
|
|
|
}
|
2013-10-02 17:27:07 +00:00
|
|
|
|
2013-10-02 17:27:23 +00:00
|
|
|
this.updateDataStore(this.webapps[id].localId, this.webapps[id].origin,
|
2014-06-19 14:03:54 +00:00
|
|
|
this.webapps[id].manifestURL, jsonManifest);
|
2012-10-10 21:29:07 +00:00
|
|
|
}
|
2012-10-03 05:38:06 +00:00
|
|
|
|
2015-02-08 21:48:29 +00:00
|
|
|
for (let prop of ["installState", "downloadAvailable", "downloading",
|
2013-11-25 23:14:35 +00:00
|
|
|
"downloadSize", "readyToApplyDownload"]) {
|
|
|
|
aData.app[prop] = appObject[prop];
|
|
|
|
}
|
2012-06-11 18:41:46 +00:00
|
|
|
|
2014-04-29 08:18:00 +00:00
|
|
|
let dontNeedNetwork = false;
|
|
|
|
|
2015-08-21 17:00:54 +00:00
|
|
|
if (appObject.kind == this.kHostedAppcache && manifest.appcache_path) {
|
2013-08-06 13:58:47 +00:00
|
|
|
this.queuedDownload[app.manifestURL] = {
|
|
|
|
manifest: manifest,
|
|
|
|
app: appObject,
|
2013-08-29 14:00:33 +00:00
|
|
|
profileDir: aProfileDir
|
2013-08-06 13:58:47 +00:00
|
|
|
}
|
2014-08-01 17:41:52 +00:00
|
|
|
} else if (appObject.kind == this.kPackaged) {
|
2013-07-31 19:34:19 +00:00
|
|
|
// If it is a local app then it must been installed from a local file
|
|
|
|
// instead of web.
|
2014-01-09 18:18:55 +00:00
|
|
|
// In that case, we would already have the manifest, not just the update
|
|
|
|
// manifest.
|
2014-06-10 17:37:38 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-01-09 18:18:55 +00:00
|
|
|
dontNeedNetwork = !!aData.app.manifest;
|
|
|
|
#else
|
2013-07-31 19:34:19 +00:00
|
|
|
if (aData.app.localInstallPath) {
|
2013-11-01 01:58:28 +00:00
|
|
|
dontNeedNetwork = true;
|
2013-07-31 19:34:19 +00:00
|
|
|
jsonManifest.package_path = "file://" + aData.app.localInstallPath;
|
2014-08-01 17:41:52 +00:00
|
|
|
}
|
2014-01-09 18:18:55 +00:00
|
|
|
#endif
|
|
|
|
|
2012-09-29 17:57:18 +00:00
|
|
|
// origin for install apps is meaningless here, since it's app:// and this
|
|
|
|
// can't be used to resolve package paths.
|
2014-07-30 21:00:15 +00:00
|
|
|
manifest = new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2013-07-24 17:15:37 +00:00
|
|
|
|
2013-08-06 14:58:44 +00:00
|
|
|
this.queuedPackageDownload[app.manifestURL] = {
|
|
|
|
manifest: manifest,
|
|
|
|
app: appObject,
|
|
|
|
callback: aInstallSuccessCallback
|
2013-07-31 19:34:19 +00:00
|
|
|
};
|
2013-10-23 23:50:57 +00:00
|
|
|
}
|
2013-07-31 19:34:19 +00:00
|
|
|
|
2014-04-29 08:18:00 +00: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 19:28:13 +00:00
|
|
|
aData.isPackage ? appObject.updateManifest = jsonManifest :
|
|
|
|
appObject.manifest = jsonManifest;
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:AddApp", { id: id, app: appObject });
|
2014-06-03 09:13:00 +00:00
|
|
|
|
2014-07-11 11:25:46 +00: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 09:13:00 +00:00
|
|
|
// The presence of a requestID means that we have a page to update.
|
|
|
|
if (aData.isPackage && aData.apkInstall && !aData.requestID) {
|
2014-04-29 08:18:00 +00: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.
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:Install:Return:OK", aData);
|
2014-04-29 08:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(null, "webapps-installed",
|
|
|
|
JSON.stringify({ manifestURL: app.manifestURL }));
|
|
|
|
|
2013-10-23 23:50:57 +00:00
|
|
|
if (aData.forceSuccessAck) {
|
|
|
|
// If it's a local install, there's no content process so just
|
|
|
|
// ack the install.
|
2013-11-01 01:58:28 +00:00
|
|
|
this.onInstallSuccessAck(app.manifestURL, dontNeedNetwork);
|
2012-09-27 19:34:41 +00:00
|
|
|
}
|
2014-04-29 08:18:00 +00:00
|
|
|
}),
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2013-11-25 23:14:35 +00: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 15:49:00 +00:00
|
|
|
_onDownloadPackage: Task.async(function*(aNewApp, aInstallSuccessCallback,
|
2014-07-21 09:26:58 +00:00
|
|
|
aId, aManifest) {
|
2013-11-25 23:14:35 +00:00
|
|
|
debug("_onDownloadPackage");
|
|
|
|
// Success! Move the zip out of TmpD.
|
|
|
|
let app = this.webapps[aId];
|
2014-02-21 19:54:03 +00:00
|
|
|
let zipFile =
|
|
|
|
FileUtils.getFile("TmpD", ["webapps", aId, "application.zip"], true);
|
2013-11-25 23:14:35 +00:00
|
|
|
let dir = this._getAppDir(aId);
|
|
|
|
zipFile.moveTo(dir, "application.zip");
|
2014-02-21 19:54:03 +00:00
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", aId], true, true);
|
2013-11-25 23:14:35 +00:00
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
// Save the manifest
|
2014-01-17 00:26:24 +00:00
|
|
|
let manFile = OS.Path.join(dir.path, "manifest.webapp");
|
2014-04-25 15:49:00 +00:00
|
|
|
yield this._writeFile(manFile, JSON.stringify(aManifest));
|
2013-11-25 23:14:35 +00:00
|
|
|
// Set state and fire events.
|
|
|
|
app.installState = "installed";
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
2014-02-03 15:58:33 +00:00
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
yield this._saveApps();
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-04-25 15:49:00 +00: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-25 23:14:35 +00:00
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:AddApp",
|
2014-05-24 19:28:13 +00:00
|
|
|
{ id: aId, app: aNewApp, manifest: aManifest });
|
2014-04-25 15:49:00 +00:00
|
|
|
Services.obs.notifyObservers(null, "webapps-installed",
|
|
|
|
JSON.stringify({ manifestURL: aNewApp.manifestURL }));
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
if (supportUseCurrentProfile()) {
|
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({
|
2013-11-25 23:14:35 +00:00
|
|
|
manifest: aManifest,
|
2014-04-25 15:49:00 +00:00
|
|
|
origin: aNewApp.origin,
|
2014-09-16 20:14:02 +00:00
|
|
|
manifestURL: aNewApp.manifestURL,
|
|
|
|
kind: this.webapps[aId].kind
|
2014-04-25 15:49:00 +00:00
|
|
|
}, true);
|
|
|
|
}
|
2014-04-18 05:03:03 +00:00
|
|
|
|
2014-04-25 15:49:00 +00:00
|
|
|
this.updateDataStore(this.webapps[aId].localId, aNewApp.origin,
|
2014-06-19 14:03:54 +00:00
|
|
|
aNewApp.manifestURL, aManifest);
|
2014-04-25 15:49:00 +00:00
|
|
|
|
2014-07-11 11:25:46 +00: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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-04-25 15:49:00 +00:00
|
|
|
app: app,
|
|
|
|
manifest: aManifest,
|
2014-05-27 19:47:04 +00:00
|
|
|
manifestURL: aNewApp.manifestURL
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2014-04-25 15:49:00 +00:00
|
|
|
|
|
|
|
// Check if we have asm.js code to preload for this application.
|
|
|
|
yield ScriptPreloader.preload(aNewApp, aManifest);
|
|
|
|
|
2015-01-10 23:00:27 +00:00
|
|
|
// Update langpack information.
|
|
|
|
yield Langpacks.register(aNewApp, aManifest);
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-04-25 15:49:00 +00:00
|
|
|
eventType: ["downloadsuccess", "downloadapplied"],
|
|
|
|
manifestURL: aNewApp.manifestURL
|
|
|
|
});
|
|
|
|
}),
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2012-07-09 10:25:41 +00:00
|
|
|
_nextLocalId: function() {
|
2012-09-06 01:07:21 +00:00
|
|
|
let id = Services.prefs.getIntPref("dom.mozApps.maxLocalId") + 1;
|
2013-09-13 12:22:24 +00:00
|
|
|
|
|
|
|
while (this.getManifestURLByLocalId(id)) {
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
|
2012-09-06 01:07:21 +00:00
|
|
|
Services.prefs.setIntPref("dom.mozApps.maxLocalId", id);
|
2013-04-22 20:28:20 +00:00
|
|
|
Services.prefs.savePrefFile(null);
|
2012-09-06 01:07:21 +00:00
|
|
|
return id;
|
2012-07-09 10:25:41 +00:00
|
|
|
},
|
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
_appIdForManifestURL: function(aURI) {
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].manifestURL == aURI)
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2012-04-28 07:10:08 +00:00
|
|
|
makeAppId: function() {
|
|
|
|
let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
|
|
|
|
return uuidGenerator.generateUUID().toString();
|
|
|
|
},
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
_saveApps: function() {
|
|
|
|
return this._writeFile(this.appsFile, JSON.stringify(this.webapps, null, 2));
|
2011-12-08 13:32:54 +00:00
|
|
|
},
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
/**
|
|
|
|
* Asynchronously reads a list of manifests
|
|
|
|
*/
|
2012-11-03 03:28:32 +00:00
|
|
|
|
|
|
|
_manifestCache: {},
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
_readManifests: function(aData) {
|
2014-06-26 05:07:14 +00:00
|
|
|
let manifestCache = this._manifestCache;
|
2014-01-17 00:26:24 +00:00
|
|
|
return Task.spawn(function*() {
|
|
|
|
for (let elem of aData) {
|
|
|
|
let id = elem.id;
|
2012-05-22 21:38:34 +00:00
|
|
|
|
2014-06-26 05:07:14 +00:00
|
|
|
if (!manifestCache[id]) {
|
2014-01-17 00:26:24 +00: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 19:54:03 +00:00
|
|
|
? "coreAppsDir" : DIRECTORY_NAME;
|
2012-11-03 03:28:32 +00:00
|
|
|
|
2014-01-31 18:14:17 +00:00
|
|
|
let dir = FileUtils.getDir(baseDir, ["webapps", id], false, true);
|
2014-01-17 00:26:24 +00:00
|
|
|
|
2014-01-31 18:14:17 +00:00
|
|
|
let fileNames = ["manifest.webapp", "update.webapp", "manifest.json"];
|
|
|
|
for (let fileName of fileNames) {
|
2014-06-26 05:07:14 +00:00
|
|
|
manifestCache[id] = yield AppsUtils.loadJSONAsync(OS.Path.join(dir.path, fileName));
|
|
|
|
if (manifestCache[id]) {
|
2014-01-31 18:14:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-17 00:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 05:07:14 +00:00
|
|
|
elem.manifest = manifestCache[id];
|
2014-01-17 00:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return aData;
|
|
|
|
}.bind(this)).then(null, Cu.reportError);
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
downloadPackage: Task.async(function*(aId, aOldApp, aManifest, aNewApp, aIsUpdate) {
|
2012-07-11 15:38:33 +00: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 04:35:58 +00:00
|
|
|
// - check the signature on the zip.
|
2012-07-11 15:38:33 +00: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 09:26:58 +00: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 15:38:33 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
// We set the 'downloading' flag to true right before starting the fetch.
|
|
|
|
aOldApp.downloading = true;
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-07-21 09:26:58 +00: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-25 23:14:35 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
// initialize the progress to 0 right now
|
|
|
|
aOldApp.progress = 0;
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-07-21 09:26:58 +00: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 18:09:10 +00:00
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-06-25 20:47:00 +00:00
|
|
|
// Clear any previous download errors.
|
|
|
|
error: null,
|
2014-07-21 09:26:58 +00:00
|
|
|
app: aOldApp,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: aId
|
2014-07-21 09:26:58 +00:00
|
|
|
});
|
2014-06-20 23:32:15 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
let zipFile = yield this._getPackage(requestChannel, aId, aOldApp, aNewApp);
|
2014-07-31 13:48:00 +00:00
|
|
|
|
|
|
|
// After this point, it's too late to cancel the download.
|
|
|
|
AppDownloadManager.remove(aNewApp.manifestURL);
|
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
let responseStatus = requestChannel.responseStatus;
|
2014-10-31 14:36:00 +00:00
|
|
|
let oldPackage = responseStatus == 304;
|
|
|
|
|
|
|
|
// If the response was 304 we probably won't have anything to hash.
|
|
|
|
let hash = null;
|
|
|
|
if (!oldPackage) {
|
|
|
|
hash = yield this._computeFileHash(zipFile.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
oldPackage = oldPackage || (hash == aOldApp.packageHash);
|
|
|
|
|
2014-07-21 09:26:58 +00: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 19:28:13 +00:00
|
|
|
this._sendAppliedEvent(aOldApp);
|
2014-11-17 01:01:00 +00:00
|
|
|
throw "PACKAGE_UNCHANGED";
|
2014-07-21 09:26:58 +00:00
|
|
|
}
|
2013-01-25 02:24:17 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
let newManifest = yield this._openAndReadPackage(zipFile, aOldApp, aNewApp,
|
|
|
|
isLocalFileInstall, aIsUpdate, aManifest, requestChannel, hash);
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
return [aOldApp.id, newManifest];
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-07-21 09:26:58 +00:00
|
|
|
}),
|
2013-11-25 23:14:35 +00:00
|
|
|
|
|
|
|
_ensureSufficientStorage: function(aNewApp) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
let navigator = Services.wm.getMostRecentWindow(chromeWindowType)
|
2013-11-25 23:14:35 +00:00
|
|
|
.navigator;
|
|
|
|
let deviceStorage = null;
|
|
|
|
|
|
|
|
if (navigator.getDeviceStorage) {
|
|
|
|
deviceStorage = navigator.getDeviceStorage("apps");
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00: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 19:54:03 +00:00
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps"], true, true);
|
2013-11-25 23:14:35 +00: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 08:38:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00: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;
|
|
|
|
|
2015-02-05 20:53:56 +00:00
|
|
|
let appURI = NetUtil.newURI(aNewApp.origin, null, null);
|
2015-08-17 09:03:19 +00:00
|
|
|
let principal =
|
|
|
|
Services.scriptSecurityManager.createCodebasePrincipal(appURI,
|
|
|
|
{appId: aNewApp.localId});
|
2015-02-05 20:53:56 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
if (aIsLocalFileInstall) {
|
2015-05-22 02:52:03 +00:00
|
|
|
requestChannel = NetUtil.newChannel({
|
|
|
|
uri: aFullPackagePath,
|
|
|
|
loadingPrincipal: principal,
|
|
|
|
contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER}
|
|
|
|
).QueryInterface(Ci.nsIFileChannel);
|
2013-11-25 23:14:35 +00:00
|
|
|
} else {
|
2015-05-22 02:52:03 +00:00
|
|
|
requestChannel = NetUtil.newChannel({
|
|
|
|
uri: aFullPackagePath,
|
|
|
|
loadingPrincipal: principal,
|
|
|
|
contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER}
|
|
|
|
).QueryInterface(Ci.nsIHttpChannel);
|
2013-11-25 23:14:35 +00:00
|
|
|
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 12:18:34 +00:00
|
|
|
|
2013-11-25 23:14:35 +00: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,
|
2015-10-01 01:14:12 +00:00
|
|
|
originAttributes: {
|
|
|
|
appId: aOldApp.installerAppId,
|
|
|
|
inBrowser: aOldApp.installerIsBrowser
|
|
|
|
},
|
2013-11-25 23:14:35 +00:00
|
|
|
usePrivateBrowsing: false,
|
|
|
|
isContent: false,
|
|
|
|
associatedWindow: null,
|
|
|
|
topWindow : null,
|
|
|
|
isAppOfType: function(appType) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return requestChannel;
|
|
|
|
},
|
|
|
|
|
|
|
|
_sendDownloadProgressEvent: function(aNewApp, aProgress) {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-11-25 23:14:35 +00:00
|
|
|
app: {
|
|
|
|
progress: aProgress
|
|
|
|
},
|
2014-05-24 19:28:13 +00:00
|
|
|
id: aNewApp.id
|
2013-11-25 23:14:35 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-11-25 23:14:35 +00:00
|
|
|
eventType: "progress",
|
|
|
|
manifestURL: aNewApp.manifestURL
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPackage: function(aRequestChannel, aId, aOldApp, aNewApp) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-09-22 14:58:59 +00:00
|
|
|
AppsUtils.getFile(aRequestChannel, aId, "application.zip").then((aFile) => {
|
|
|
|
deferred.resolve(aFile);
|
|
|
|
}, function(rejectStatus) {
|
|
|
|
debug("Failed to download package file: " + rejectStatus.msg);
|
|
|
|
if (!rejectStatus.downloadAvailable) {
|
|
|
|
aOldApp.downloadAvailable = false;
|
2013-01-25 09:57:14 +00:00
|
|
|
}
|
2014-09-22 14:58:59 +00:00
|
|
|
deferred.reject(rejectStatus.msg);
|
2013-11-25 23:14:35 +00:00
|
|
|
});
|
2013-01-25 09:57:14 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
// send a first progress event to correctly set the DOM object's properties
|
|
|
|
this._sendDownloadProgressEvent(aNewApp, 0);
|
2012-11-15 09:35:37 +00:00
|
|
|
|
2013-11-25 23:14:35 +00: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 13:14:06 +00:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
file.initWithPath(aFilePath);
|
|
|
|
|
2015-05-22 02:51:40 +00:00
|
|
|
NetUtil.asyncFetch({
|
|
|
|
uri: NetUtil.newURI(file),
|
|
|
|
loadUsingSystemPrincipal: true
|
|
|
|
}, function(inputStream, status) {
|
2014-03-25 13:14:06 +00:00
|
|
|
if (!Components.isSuccessCode(status)) {
|
|
|
|
debug("Error reading " + aFilePath + ": " + e);
|
|
|
|
deferred.reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00: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 13:14:06 +00:00
|
|
|
const PR_UINT32_MAX = 0xffffffff;
|
|
|
|
hasher.updateFromStream(inputStream, PR_UINT32_MAX);
|
2013-11-25 23:14:35 +00:00
|
|
|
|
|
|
|
// Return the two-digit hexadecimal code for a byte.
|
|
|
|
function toHexString(charCode) {
|
|
|
|
return ("0" + charCode.toString(16)).slice(-2);
|
2013-07-31 19:34:19 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00: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 09:35:37 +00:00
|
|
|
|
2014-03-25 13:14:06 +00:00
|
|
|
deferred.resolve(hash);
|
2015-05-22 02:51:40 +00:00
|
|
|
});
|
2014-03-25 13:14:06 +00:00
|
|
|
|
|
|
|
return deferred.promise;
|
2013-11-25 23:14:35 +00: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 19:28:13 +00:00
|
|
|
* @param aApp {Object} app data
|
2013-11-25 23:14:35 +00:00
|
|
|
*/
|
2014-05-24 19:28:13 +00: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-25 23:14:35 +00: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 19:28:13 +00: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 13:14:06 +00:00
|
|
|
try {
|
2014-05-24 19:28:13 +00:00
|
|
|
let staged = this._getAppDir(aApp.id);
|
2014-03-25 13:14:06 +00: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-25 23:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the updated registry, and cleanup the tmp directory.
|
2014-01-17 00:26:24 +00:00
|
|
|
this._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-05-24 19:28:13 +00:00
|
|
|
app: aApp,
|
|
|
|
id: aApp.id
|
2013-11-25 23:14:35 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2014-05-24 19:28:13 +00:00
|
|
|
manifestURL: aApp.manifestURL,
|
2013-11-25 23:14:35 +00:00
|
|
|
eventType: ["downloadsuccess", "downloadapplied"]
|
|
|
|
});
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2014-05-24 19:28:13 +00:00
|
|
|
let file = FileUtils.getFile("TmpD", ["webapps", aApp.id], false);
|
2014-02-21 19:54:03 +00:00
|
|
|
if (file && file.exists()) {
|
|
|
|
file.remove(true);
|
|
|
|
}
|
2013-11-25 23:14:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_openAndReadPackage: function(aZipFile, aOldApp, aNewApp, aIsLocalFileInstall,
|
|
|
|
aIsUpdate, aManifest, aRequestChannel, aHash) {
|
|
|
|
return Task.spawn((function*() {
|
|
|
|
let zipReader, isSigned, newManifest;
|
|
|
|
|
|
|
|
try {
|
2013-11-19 19:41:54 +00:00
|
|
|
[zipReader, isSigned] = yield this._openPackage(aZipFile, aOldApp,
|
|
|
|
aIsLocalFileInstall);
|
2013-11-25 23:14:35 +00: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 12:09:07 +00:00
|
|
|
Cu.reportError("Error while reading package: " + e + "\n" + e.stack);
|
2013-11-25 23:14:35 +00:00
|
|
|
throw "INVALID_PACKAGE";
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (zipReader) {
|
|
|
|
zipReader.close();
|
2012-12-10 23:49:02 +00:00
|
|
|
}
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
2012-11-29 14:19:37 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
return newManifest;
|
2013-01-25 02:24:17 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
2012-12-14 19:29:25 +00:00
|
|
|
|
2013-11-19 19:41:54 +00:00
|
|
|
_openPackage: function(aZipFile, aApp, aIsLocalFileInstall) {
|
2013-11-25 23:14:35 +00: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 15:51:28 +00:00
|
|
|
|
2014-04-28 08:50:00 +00:00
|
|
|
let [result, zipReader] = yield this._openSignedPackage(aApp.installOrigin,
|
|
|
|
aApp.manifestURL,
|
|
|
|
aZipFile,
|
|
|
|
certDb);
|
2013-02-01 19:17:32 +00:00
|
|
|
|
2013-11-19 19:41:54 +00: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-25 23:14:35 +00:00
|
|
|
let isSigned;
|
2012-12-21 18:28:58 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
if (Components.isSuccessCode(result)) {
|
|
|
|
isSigned = true;
|
2013-12-15 19:03:28 +00: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-25 23:14:35 +00:00
|
|
|
throw "APP_PACKAGE_CORRUPTED";
|
2013-12-15 19:03:28 +00: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 19:41:54 +00:00
|
|
|
} else if ((!aIsLocalFileInstall || isLaterThanBuildTime) &&
|
|
|
|
(result != Cr.NS_ERROR_SIGNED_JAR_NOT_SIGNED)) {
|
2013-11-25 23:14:35 +00:00
|
|
|
throw "INVALID_SIGNATURE";
|
|
|
|
} else {
|
2013-11-19 19:41:54 +00: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-25 23:14:35 +00:00
|
|
|
zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
|
|
|
|
.createInstance(Ci.nsIZipReader);
|
|
|
|
zipReader.open(aZipFile);
|
|
|
|
}
|
2012-12-14 04:35:58 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
return [zipReader, isSigned];
|
2012-12-14 19:29:25 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
2013-02-11 08:38:51 +00:00
|
|
|
|
2014-04-28 08:50:00 +00:00
|
|
|
_openSignedPackage: function(aInstallOrigin, aManifestURL, aZipFile, aCertDb) {
|
2013-11-25 23:14:35 +00:00
|
|
|
let deferred = Promise.defer();
|
2012-11-15 09:35:37 +00:00
|
|
|
|
2014-04-28 08:50:00 +00: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;
|
2015-10-13 18:35:57 +00:00
|
|
|
let isReviewer = false;
|
|
|
|
// There are different reviewer paths for apps & addons so we keep
|
|
|
|
// them in a comma separated preference.
|
|
|
|
try {
|
|
|
|
let reviewerPaths =
|
|
|
|
Services.prefs.getCharPref("dom.apps.reviewer_paths").split(",");
|
|
|
|
isReviewer = reviewerPaths.some(path => { return manifestPath.startsWith(path); });
|
|
|
|
} catch(e) {}
|
2014-04-28 08:50:00 +00:00
|
|
|
|
|
|
|
switch (aInstallOrigin) {
|
|
|
|
case "https://marketplace.firefox.com":
|
2015-10-13 18:35:57 +00:00
|
|
|
root = isReviewer
|
2014-04-28 08:50:00 +00:00
|
|
|
? Ci.nsIX509CertDB.AppMarketplaceProdReviewersRoot
|
|
|
|
: Ci.nsIX509CertDB.AppMarketplaceProdPublicRoot;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "https://marketplace-dev.allizom.org":
|
2015-10-13 16:07:10 +00:00
|
|
|
root = isReviewer
|
2014-04-28 08:50:00 +00:00
|
|
|
? Ci.nsIX509CertDB.AppMarketplaceDevReviewersRoot
|
|
|
|
: Ci.nsIX509CertDB.AppMarketplaceDevPublicRoot;
|
|
|
|
break;
|
2014-07-23 11:20:25 +00: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 08:50:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:37:07 +00:00
|
|
|
aCertDb.openSignedAppFileAsync(
|
2014-04-28 08:50:00 +00:00
|
|
|
root, aZipFile,
|
2013-11-25 23:14:35 +00:00
|
|
|
function(aRv, aZipReader) {
|
|
|
|
deferred.resolve([aRv, aZipReader]);
|
2013-03-25 20:03:58 +00:00
|
|
|
}
|
2013-11-25 23:14:35 +00:00
|
|
|
);
|
2013-03-25 20:03:58 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
return deferred.promise;
|
|
|
|
},
|
2013-07-29 19:53:14 +00:00
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
_readPackage: function(aOldApp, aNewApp, aIsLocalFileInstall, aIsUpdate,
|
|
|
|
aManifest, aRequestChannel, aHash, aZipReader,
|
|
|
|
aIsSigned) {
|
|
|
|
this._checkSignature(aNewApp, aIsSigned, aIsLocalFileInstall);
|
|
|
|
|
2015-08-14 23:55:14 +00:00
|
|
|
// Chrome-style extensions only have a manifest.json manifest.
|
|
|
|
// In this case we extract it, and convert it to a minimal
|
|
|
|
// manifest.webapp manifest.
|
|
|
|
// Packages that contain both manifest.webapp and manifest.json
|
|
|
|
// are considered as apps, not extensions.
|
|
|
|
let hasWebappManifest = aZipReader.hasEntry("manifest.webapp");
|
|
|
|
let hasJsonManifest = aZipReader.hasEntry("manifest.json");
|
|
|
|
|
|
|
|
if (!hasWebappManifest && !hasJsonManifest) {
|
2013-11-25 23:14:35 +00:00
|
|
|
throw "MISSING_MANIFEST";
|
2013-07-29 19:53:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 23:55:14 +00:00
|
|
|
let istream =
|
|
|
|
aZipReader.getInputStream(hasWebappManifest ? "manifest.webapp"
|
|
|
|
: "manifest.json");
|
2013-03-25 20:03:58 +00:00
|
|
|
|
2013-11-25 23:14:35 +00: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()) || ""));
|
|
|
|
|
2015-08-14 23:55:14 +00:00
|
|
|
if (!hasWebappManifest) {
|
|
|
|
// Validate the extension manifest, and convert it.
|
|
|
|
if (!UserCustomizations.checkExtensionManifest(newManifest)) {
|
|
|
|
throw "INVALID_MANIFEST";
|
|
|
|
}
|
|
|
|
newManifest = UserCustomizations.convertManifest(newManifest);
|
2015-10-27 15:10:43 +00:00
|
|
|
// Keep track of the add-on version, to use for blocklisting.
|
|
|
|
if (newManifest.version) {
|
|
|
|
aNewApp.extensionVersion = newManifest.version;
|
|
|
|
}
|
2015-08-14 23:55:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00:00
|
|
|
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;
|
|
|
|
|
2015-04-13 16:49:50 +00:00
|
|
|
try {
|
|
|
|
// Anything is possible in developer mode.
|
|
|
|
if (Services.prefs.getBoolPref("dom.apps.developer_mode")) {
|
|
|
|
maxStatus = Ci.nsIPrincipal.APP_STATUS_CERTIFIED;
|
|
|
|
}
|
|
|
|
} catch(e) {};
|
|
|
|
|
2015-04-06 21:22:26 +00:00
|
|
|
let allowUnsignedLangpack = false;
|
|
|
|
try {
|
|
|
|
allowUnsignedLangpack =
|
2015-04-13 16:49:50 +00:00
|
|
|
Services.prefs.getBoolPref("dom.apps.allow_unsigned_langpacks") ||
|
|
|
|
Services.prefs.getBoolPref("dom.apps.developer_mode");
|
2015-04-06 21:22:26 +00:00
|
|
|
} catch(e) {}
|
|
|
|
let isLangPack = newManifest.role === "langpack" &&
|
|
|
|
(aIsSigned || allowUnsignedLangpack);
|
|
|
|
|
2015-08-14 23:55:14 +00:00
|
|
|
let isAddon = newManifest.role === "addon" &&
|
|
|
|
(aIsSigned || AppsUtils.allowUnsignedAddons);
|
|
|
|
|
2014-08-29 00:20:27 +00:00
|
|
|
let status = AppsUtils.getAppManifestStatus(newManifest);
|
2015-08-14 23:55:14 +00:00
|
|
|
if (status > maxStatus && !isLangPack && !isAddon) {
|
2013-11-25 23:14:35 +00:00
|
|
|
throw "INVALID_SECURITY_LEVEL";
|
|
|
|
}
|
|
|
|
|
2014-08-29 00:20:27 +00:00
|
|
|
// Check if the role is allowed for this app.
|
|
|
|
if (!AppsUtils.checkAppRole(newManifest.role, status)) {
|
|
|
|
throw "INVALID_ROLE";
|
|
|
|
}
|
2013-11-25 23:14:35 +00:00
|
|
|
|
|
|
|
this._saveEtag(aIsUpdate, aOldApp, aRequestChannel, aHash, newManifest);
|
2014-01-22 14:42:56 +00:00
|
|
|
this._checkOrigin(aIsSigned || aIsLocalFileInstall, aOldApp, newManifest,
|
|
|
|
aIsUpdate);
|
2013-11-25 23:14:35 +00: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-25 20:03:58 +00:00
|
|
|
}
|
2013-11-25 23:14:35 +00:00
|
|
|
try {
|
|
|
|
aOldApp.staged.packageEtag = aRequestChannel.getResponseHeader("Etag");
|
|
|
|
} catch(e) { }
|
|
|
|
aOldApp.staged.packageHash = aHash;
|
|
|
|
aOldApp.staged.appStatus = AppsUtils.getAppManifestStatus(aManifest);
|
2013-03-25 20:03:58 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2013-11-25 23:14:35 +00: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 14:42:56 +00:00
|
|
|
if (uri.prePath != aOldApp.origin) {
|
2013-11-25 23:14:35 +00: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 14:42:56 +00:00
|
|
|
" for " + aOldApp.manifestURL);
|
2013-11-25 23:14:35 +00:00
|
|
|
let newId = uri.prePath.substring(6); // "app://".length
|
2014-07-15 01:53:48 +00:00
|
|
|
if (newId in this.webapps && this._isLaunchable(this.webapps[newId])) {
|
2013-11-25 23:14:35 +00:00
|
|
|
throw "DUPLICATE_ORIGIN";
|
|
|
|
}
|
|
|
|
aOldApp.origin = uri.prePath;
|
|
|
|
// Update the registry.
|
2014-01-22 14:42:56 +00:00
|
|
|
let oldId = aOldApp.id;
|
2014-07-15 01:53:48 +00: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-25 23:14:35 +00:00
|
|
|
aOldApp.id = newId;
|
|
|
|
this.webapps[newId] = aOldApp;
|
2014-01-22 14:42:56 +00:00
|
|
|
delete this.webapps[oldId];
|
2013-11-25 23:14:35 +00:00
|
|
|
// Rename the directories where the files are installed.
|
2014-02-21 19:54:03 +00: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-25 23:14:35 +00:00
|
|
|
dir.moveTo(parent, newId);
|
|
|
|
});
|
|
|
|
// Signals that we need to swap the old id with the new app.
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateApp", { oldId: oldId,
|
2014-05-24 19:28:13 +00:00
|
|
|
newId: newId,
|
|
|
|
app: aOldApp });
|
|
|
|
|
2013-11-25 23:14:35 +00: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 09:35:37 +00:00
|
|
|
}
|
2013-11-25 23:14:35 +00: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 09:26:58 +00:00
|
|
|
revertDownloadPackage: function(aId, aOldApp, aNewApp, aIsUpdate, aError) {
|
|
|
|
debug("Error downloading package: " + aError);
|
2014-02-21 19:54:03 +00:00
|
|
|
let dir = FileUtils.getDir("TmpD", ["webapps", aId], true, true);
|
2013-11-25 23:14:35 +00: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 09:35:37 +00:00
|
|
|
}
|
2013-11-25 23:14:35 +00:00
|
|
|
|
2014-11-17 01:01:00 +00:00
|
|
|
// If the error that got us here was that the package hasn't changed,
|
|
|
|
// since we already sent a success and an applied, let's not confuse
|
|
|
|
// the clients...
|
|
|
|
if (aError == "PACKAGE_UNCHANGED") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 23:14:35 +00: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";
|
|
|
|
|
2014-11-21 09:45:00 +00:00
|
|
|
// Erase the .staged properties only if there's no download available
|
|
|
|
// anymore.
|
|
|
|
if (!aOldApp.downloadAvailable && aOldApp.staged) {
|
2013-11-25 23:14:35 +00:00
|
|
|
delete aOldApp.staged;
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
this._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-11-25 23:14:35 +00:00
|
|
|
app: aOldApp,
|
|
|
|
error: aError,
|
2014-09-26 17:23:21 +00:00
|
|
|
id: aId
|
2013-11-25 23:14:35 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-11-25 23:14:35 +00:00
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: aNewApp.manifestURL
|
|
|
|
});
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2013-11-25 23:14:35 +00:00
|
|
|
AppDownloadManager.remove(aNewApp.manifestURL);
|
2012-07-11 15:38:33 +00:00
|
|
|
},
|
|
|
|
|
2014-06-11 21:23:18 +00:00
|
|
|
doUninstall: Task.async(function*(aData, aMm) {
|
|
|
|
let response = "Webapps:Uninstall:Return:OK";
|
2012-11-03 03:28:32 +00:00
|
|
|
|
2014-06-11 21:23:18 +00:00
|
|
|
try {
|
|
|
|
aData.app = yield this._getAppWithManifest(aData.manifestURL);
|
|
|
|
|
2015-09-14 16:30:28 +00:00
|
|
|
if (this.kAndroid == aData.app.kind) {
|
|
|
|
debug("Uninstalling android app " + aData.app.origin);
|
|
|
|
let [packageName, className] =
|
|
|
|
AndroidUtils.getPackageAndClassFromManifestURL(aData.manifestURL);
|
|
|
|
Messaging.sendRequest({ type: "Apps:Uninstall",
|
|
|
|
packagename: packageName,
|
|
|
|
classname: className });
|
|
|
|
// We have to wait for Android's uninstall before sending the
|
|
|
|
// uninstall event, so fake an error here.
|
|
|
|
response = "Webapps:Uninstall:Return:KO";
|
2014-06-11 21:23:18 +00:00
|
|
|
} else {
|
2015-09-14 16:30:28 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-06-11 21:23:18 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
aData.error = error;
|
|
|
|
response = "Webapps:Uninstall:Return:KO";
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:14:14 +00:00
|
|
|
aMm.sendAsyncMessage(response, this.formatMessage(aData));
|
2014-06-11 21:23:18 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
uninstall: function(aManifestURL) {
|
|
|
|
return this._getAppWithManifest(aManifestURL)
|
|
|
|
.then(this._uninstallApp.bind(this));
|
|
|
|
},
|
2012-09-28 16:24:45 +00:00
|
|
|
|
2014-06-11 21:23:18 +00:00
|
|
|
_uninstallApp: Task.async(function*(aApp) {
|
|
|
|
if (!aApp.removable) {
|
2013-05-06 13:51:53 +00:00
|
|
|
debug("Error: cannot uninstall a non-removable app.");
|
2014-06-11 21:23:18 +00:00
|
|
|
throw new Error("NON_REMOVABLE_APP");
|
2013-05-06 13:51:53 +00:00
|
|
|
}
|
2012-09-28 16:24:45 +00:00
|
|
|
|
2014-06-11 21:23:18 +00:00
|
|
|
let id = aApp.id;
|
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
// Check if we are downloading something for this app, and cancel the
|
|
|
|
// download if needed.
|
2014-06-11 21:23:18 +00:00
|
|
|
this.cancelDownload(aApp.manifestURL);
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
2012-07-20 15:41:30 +00:00
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
// Clear private data first.
|
2014-06-11 21:23:18 +00:00
|
|
|
this._clearPrivateData(aApp.localId, false);
|
2012-07-20 15:41:30 +00:00
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
// Then notify observers.
|
2014-06-11 21:23:18 +00:00
|
|
|
Services.obs.notifyObservers(null, "webapps-uninstall", JSON.stringify(aApp));
|
2012-07-20 15:41:30 +00:00
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
if (supportSystemMessages()) {
|
2014-06-11 21:23:18 +00:00
|
|
|
this._unregisterActivities(aApp.manifest, aApp);
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
2015-08-14 23:55:09 +00:00
|
|
|
UserCustomizations.unregister(aApp);
|
2015-01-10 23:00:27 +00:00
|
|
|
Langpacks.unregister(aApp, aApp.manifest);
|
2012-09-28 16:24:45 +00:00
|
|
|
|
2013-05-06 13:51:53 +00:00
|
|
|
let dir = this._getAppDir(id);
|
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
delete this.webapps[id];
|
|
|
|
|
2014-06-11 21:23:18 +00:00
|
|
|
yield this._saveApps();
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:Uninstall:Broadcast:Return:OK", aApp);
|
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:RemoveApp", { id: id });
|
2014-06-11 21:23:18 +00:00
|
|
|
|
|
|
|
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-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2012-09-18 17:34:55 +00:00
|
|
|
getSelf: function(aData, aMm) {
|
2011-11-28 20:13:26 +00:00
|
|
|
aData.apps = [];
|
2012-09-25 15:04:24 +00:00
|
|
|
|
|
|
|
if (aData.appId == Ci.nsIScriptSecurityManager.NO_APP_ID ||
|
|
|
|
aData.appId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) {
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", this.formatMessage(aData));
|
2012-09-25 15:04:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
let tmp = [];
|
2012-03-06 19:50:58 +00:00
|
|
|
|
2012-09-25 15:04:24 +00:00
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].origin == aData.origin &&
|
|
|
|
this.webapps[id].localId == aData.appId &&
|
2013-08-02 00:00:22 +00:00
|
|
|
this._isLaunchable(this.webapps[id])) {
|
2012-09-25 15:04:24 +00:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
|
|
|
aData.apps.push(app);
|
|
|
|
tmp.push({ id: id });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aData.apps.length) {
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", this.formatMessage(aData));
|
2012-09-25 15:04:24 +00:00
|
|
|
return;
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2012-03-06 19:50:58 +00:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2012-03-06 19:50:58 +00:00
|
|
|
},
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-10-02 20:38:51 +00:00
|
|
|
checkInstalled: function(aData, aMm) {
|
|
|
|
aData.app = null;
|
|
|
|
let tmp = [];
|
2012-09-25 15:04:24 +00:00
|
|
|
|
|
|
|
for (let appId in this.webapps) {
|
2013-09-13 12:07:04 +00:00
|
|
|
if (this.webapps[appId].manifestURL == aData.manifestURL &&
|
|
|
|
this._isLaunchable(this.webapps[appId])) {
|
2012-10-02 20:38:51 +00:00
|
|
|
aData.app = AppsUtils.cloneAppObject(this.webapps[appId]);
|
|
|
|
tmp.push({ id: appId });
|
2012-09-25 15:04:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2012-10-02 20:38:51 +00:00
|
|
|
for (let i = 0; i < aResult.length; i++) {
|
|
|
|
aData.app.manifest = aResult[i].manifest;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:CheckInstalled:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2012-09-25 15:04:24 +00:00
|
|
|
},
|
|
|
|
|
2012-09-18 17:34:55 +00:00
|
|
|
getInstalled: function(aData, aMm) {
|
2012-03-06 19:50:58 +00:00
|
|
|
aData.apps = [];
|
|
|
|
let tmp = [];
|
|
|
|
|
2012-06-29 20:46:21 +00:00
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].installOrigin == aData.origin &&
|
2013-08-02 00:00:22 +00:00
|
|
|
this._isLaunchable(this.webapps[id])) {
|
2012-08-28 02:43:57 +00:00
|
|
|
aData.apps.push(AppsUtils.cloneAppObject(this.webapps[id]));
|
2012-03-06 19:50:58 +00:00
|
|
|
tmp.push({ id: id });
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2011-12-05 23:26:18 +00:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetInstalled:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
|
|
|
|
2012-09-18 17:34:55 +00:00
|
|
|
getNotInstalled: function(aData, aMm) {
|
2012-06-29 20:46:21 +00:00
|
|
|
aData.apps = [];
|
|
|
|
let tmp = [];
|
|
|
|
|
|
|
|
for (let id in this.webapps) {
|
2013-08-02 00:00:22 +00:00
|
|
|
if (!this._isLaunchable(this.webapps[id])) {
|
2012-08-28 02:43:57 +00:00
|
|
|
aData.apps.push(AppsUtils.cloneAppObject(this.webapps[id]));
|
2012-06-29 20:46:21 +00:00
|
|
|
tmp.push({ id: id });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
this._readManifests(tmp).then((aResult) => {
|
2012-06-29 20:46:21 +00:00
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetNotInstalled:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:24 +00:00
|
|
|
});
|
2012-06-29 20:46:21 +00:00
|
|
|
},
|
2013-05-06 13:51:53 +00:00
|
|
|
|
2014-12-18 22:40:41 +00:00
|
|
|
getIcon: function(aData, aMm) {
|
2015-04-02 20:53:27 +00:00
|
|
|
let sendError = (aError) => {
|
2014-12-18 22:40:41 +00:00
|
|
|
debug("getIcon error: " + aError);
|
|
|
|
aData.error = aError;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetIcon:Return", this.formatMessage(aData));
|
|
|
|
};
|
2014-12-18 22:40:41 +00:00
|
|
|
|
|
|
|
let app = this.getAppByManifestURL(aData.manifestURL);
|
|
|
|
if (!app) {
|
|
|
|
sendError("NO_APP");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadIcon(aUrl) {
|
|
|
|
let fallbackMimeType = aUrl.indexOf('.') >= 0 ?
|
|
|
|
"image/" + aUrl.split(".").reverse()[0] : "";
|
|
|
|
// Set up an xhr to download a blob.
|
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.mozBackgroundRequest = true;
|
|
|
|
xhr.open("GET", aUrl, true);
|
|
|
|
xhr.responseType = "blob";
|
|
|
|
xhr.addEventListener("load", function() {
|
|
|
|
debug("Got http status=" + xhr.status + " for " + aUrl);
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
let blob = xhr.response;
|
|
|
|
// Reusing aData with sendAsyncMessage() leads to an empty blob in
|
|
|
|
// the child.
|
|
|
|
let payload = {
|
|
|
|
"oid": aData.oid,
|
|
|
|
"requestID": aData.requestID,
|
|
|
|
"blob": blob,
|
|
|
|
"type": xhr.getResponseHeader("Content-Type") || fallbackMimeType
|
|
|
|
};
|
|
|
|
aMm.sendAsyncMessage("Webapps:GetIcon:Return", payload);
|
|
|
|
} else if (xhr.status === 0) {
|
|
|
|
sendError("NETWORK_ERROR");
|
|
|
|
} else {
|
|
|
|
sendError("FETCH_ICON_FAILED");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
xhr.addEventListener("error", function() {
|
|
|
|
sendError("FETCH_ICON_FAILED");
|
|
|
|
});
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the manifest, to find the icon url in the current locale.
|
|
|
|
this.getManifestFor(aData.manifestURL, aData.entryPoint)
|
|
|
|
.then((aManifest) => {
|
|
|
|
if (!aManifest) {
|
|
|
|
sendError("FETCH_ICON_FAILED");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifest = new ManifestHelper(aManifest, app.origin, app.manifestURL);
|
|
|
|
let url = manifest.iconURLForSize(aData.iconID);
|
|
|
|
if (!url) {
|
|
|
|
sendError("NO_ICON");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loadIcon(url);
|
|
|
|
}).catch(() => {
|
|
|
|
sendError("FETCH_ICON_FAILED");
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-01-17 00:26:26 +00: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";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = this.isReceipt(receipt);
|
|
|
|
if (error) {
|
|
|
|
aData.error = error;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
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";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = app.receipts.indexOf(receipt);
|
|
|
|
if (index >= 0) {
|
|
|
|
aData.error = "RECEIPT_ALREADY_EXISTS";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.receipts.push(receipt);
|
|
|
|
|
2014-01-17 01:00:29 +00:00
|
|
|
this._saveApps().then(() => {
|
2014-01-17 00:26:26 +00:00
|
|
|
aData.receipts = app.receipts;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:AddReceipt:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeReceipt: function(aData, aMm) {
|
|
|
|
debug("removeReceipt " + aData.manifestURL);
|
|
|
|
|
|
|
|
let receipt = aData.receipt;
|
|
|
|
|
|
|
|
if (!receipt) {
|
|
|
|
aData.error = "INVALID_PARAMETERS";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
|
|
|
|
|
|
|
if (!app.receipts) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = app.receipts.indexOf(receipt);
|
|
|
|
if (index == -1) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.receipts.splice(index, 1);
|
|
|
|
|
2014-01-17 01:00:29 +00:00
|
|
|
this._saveApps().then(() => {
|
2014-01-17 00:26:26 +00:00
|
|
|
aData.receipts = app.receipts;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
replaceReceipt: function(aData, aMm) {
|
|
|
|
debug("replaceReceipt " + aData.manifestURL);
|
|
|
|
|
|
|
|
let oldReceipt = aData.oldReceipt;
|
|
|
|
let newReceipt = aData.newReceipt;
|
|
|
|
|
|
|
|
if (!oldReceipt || !newReceipt) {
|
|
|
|
aData.error = "INVALID_PARAMETERS";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = this.isReceipt(newReceipt);
|
|
|
|
if (error) {
|
|
|
|
aData.error = error;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
|
|
|
|
|
|
|
if (!app.receipts) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:RemoveReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let oldIndex = app.receipts.indexOf(oldReceipt);
|
|
|
|
if (oldIndex == -1) {
|
|
|
|
aData.error = "NO_SUCH_RECEIPT";
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:KO", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.receipts[oldIndex] = newReceipt;
|
|
|
|
|
2014-01-17 01:00:29 +00:00
|
|
|
this._saveApps().then(() => {
|
2014-01-17 00:26:26 +00:00
|
|
|
aData.receipts = app.receipts;
|
2015-04-02 20:53:27 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:ReplaceReceipt:Return:OK", this.formatMessage(aData));
|
2014-01-17 00:26:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-29 14:32:50 +00:00
|
|
|
setBlockedStatus: function(aManifestURL, aSeverity) {
|
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
|
|
|
if (!id || !this.webapps[id]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(`Setting blocked status ${aSeverity} on ${id}`);
|
|
|
|
let app = this.webapps[id];
|
|
|
|
|
|
|
|
app.blockedStatus = aSeverity;
|
|
|
|
let enabled = aSeverity == Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
|
|
|
|
this.setEnabled({ manifestURL: aManifestURL, enabled });
|
|
|
|
},
|
|
|
|
|
2014-10-23 23:40:08 +00:00
|
|
|
setEnabled: function(aData) {
|
|
|
|
debug("setEnabled " + aData.manifestURL + " : " + aData.enabled);
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
if (!id || !this.webapps[id]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Enabling " + id);
|
|
|
|
let app = this.webapps[id];
|
2015-10-29 14:32:50 +00:00
|
|
|
|
|
|
|
// If we try to enable an app, check if it's not blocked.
|
|
|
|
if (!aData.enabled ||
|
|
|
|
app.blockedStatus == Ci.nsIBlocklistService.STATE_NOT_BLOCKED) {
|
|
|
|
app.enabled = aData.enabled;
|
|
|
|
}
|
|
|
|
|
2014-10-23 23:40:08 +00:00
|
|
|
this._saveApps().then(() => {
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2014-10-23 23:40:08 +00:00
|
|
|
app: app,
|
|
|
|
id: app.id
|
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:SetEnabled:Return", app);
|
2014-10-23 23:40:08 +00:00
|
|
|
});
|
2014-12-16 23:32:28 +00:00
|
|
|
|
|
|
|
// Update customization.
|
2015-08-14 23:55:09 +00:00
|
|
|
if (app.enabled) {
|
|
|
|
UserCustomizations.register(app);
|
|
|
|
} else {
|
|
|
|
UserCustomizations.unregister(app);
|
|
|
|
}
|
2014-10-23 23:40:08 +00:00
|
|
|
},
|
|
|
|
|
2015-09-04 18:12:53 +00:00
|
|
|
// Returns a promise that resolves once all the add-ons are disabled.
|
|
|
|
disableAllAddons: function() {
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
let app = this.webapps[id];
|
|
|
|
if (app.role == "addon" && app.enabled) {
|
|
|
|
app.enabled = false;
|
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
|
|
|
app: app,
|
|
|
|
id: app.id
|
|
|
|
});
|
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:SetEnabled:Return", app);
|
|
|
|
|
|
|
|
UserCustomizations.unregister(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._saveApps();
|
|
|
|
},
|
|
|
|
|
2014-12-18 22:40:41 +00:00
|
|
|
getManifestFor: function(aManifestURL, aEntryPoint) {
|
2013-08-28 12:10:55 +00:00
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
2012-11-30 16:57:45 +00:00
|
|
|
let app = this.webapps[id];
|
|
|
|
if (!id || (app.installState == "pending" && !app.retryingDownload)) {
|
2014-01-17 00:26:24 +00:00
|
|
|
return Promise.resolve(null);
|
2011-12-05 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
return this._readManifests([{ id: id }]).then((aResult) => {
|
2014-12-18 22:40:41 +00:00
|
|
|
if (aEntryPoint) {
|
|
|
|
return aResult[0].manifest.entry_points[aEntryPoint];
|
|
|
|
} else {
|
|
|
|
return aResult[0].manifest;
|
|
|
|
}
|
2011-12-05 23:26:18 +00:00
|
|
|
});
|
2011-12-08 13:32:54 +00:00
|
|
|
},
|
|
|
|
|
2012-05-16 10:40:47 +00:00
|
|
|
getAppByManifestURL: function(aManifestURL) {
|
2012-08-28 02:43:57 +00:00
|
|
|
return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL);
|
2012-05-16 10:40:47 +00:00
|
|
|
},
|
2012-07-09 10:25:41 +00:00
|
|
|
|
2015-03-06 06:43:57 +00:00
|
|
|
// Returns a promise that resolves to the app object with the manifest.
|
2015-03-19 17:06:32 +00:00
|
|
|
getFullAppByManifestURL: function(aManifestURL, aEntryPoint, aLang) {
|
2015-03-06 06:43:57 +00:00
|
|
|
let app = this.getAppByManifestURL(aManifestURL);
|
|
|
|
if (!app) {
|
|
|
|
return Promise.reject("NoSuchApp");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getManifestFor(aManifestURL).then((aManifest) => {
|
2015-10-28 16:00:50 +00:00
|
|
|
if (!aManifest) {
|
|
|
|
return Promise.reject("NoManifest");
|
|
|
|
}
|
|
|
|
|
2015-03-06 06:43:57 +00:00
|
|
|
let manifest = aEntryPoint && aManifest.entry_points &&
|
|
|
|
aManifest.entry_points[aEntryPoint]
|
|
|
|
? aManifest.entry_points[aEntryPoint]
|
|
|
|
: aManifest;
|
|
|
|
|
|
|
|
// `version` doesn't change based on entry points, and we need it
|
|
|
|
// to check langpack versions.
|
|
|
|
if (manifest !== aManifest) {
|
|
|
|
manifest.version = aManifest.version;
|
|
|
|
}
|
|
|
|
|
2015-03-19 17:06:32 +00:00
|
|
|
app.manifest =
|
|
|
|
new ManifestHelper(manifest, app.origin, app.manifestURL, aLang);
|
2015-03-06 06:43:57 +00:00
|
|
|
return app;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-06-11 21:23:18 +00: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;
|
|
|
|
}),
|
|
|
|
|
2014-09-16 20:14:02 +00:00
|
|
|
getManifestCSPByLocalId: function(aLocalId) {
|
|
|
|
debug("getManifestCSPByLocalId:" + aLocalId);
|
|
|
|
return AppsUtils.getManifestCSPByLocalId(this.webapps, aLocalId);
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultCSPByLocalId: function(aLocalId) {
|
|
|
|
debug("getDefaultCSPByLocalId:" + aLocalId);
|
|
|
|
return AppsUtils.getDefaultCSPByLocalId(this.webapps, aLocalId);
|
2012-10-19 10:43:17 +00:00
|
|
|
},
|
|
|
|
|
2013-01-25 09:57:14 +00:00
|
|
|
getAppLocalIdByStoreId: function(aStoreId) {
|
|
|
|
debug("getAppLocalIdByStoreId:" + aStoreId);
|
|
|
|
return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId);
|
|
|
|
},
|
|
|
|
|
2012-08-08 16:41:47 +00:00
|
|
|
getAppByLocalId: function(aLocalId) {
|
2012-08-28 02:43:57 +00:00
|
|
|
return AppsUtils.getAppByLocalId(this.webapps, aLocalId);
|
2012-08-08 16:41:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getManifestURLByLocalId: function(aLocalId) {
|
2012-08-28 02:43:57 +00:00
|
|
|
return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId);
|
2012-08-08 16:41:47 +00:00
|
|
|
},
|
|
|
|
|
2012-07-09 10:25:41 +00:00
|
|
|
getAppLocalIdByManifestURL: function(aManifestURL) {
|
2012-08-28 02:43:57 +00:00
|
|
|
return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL);
|
2012-07-09 10:25:41 +00:00
|
|
|
},
|
|
|
|
|
2012-12-22 13:56:21 +00:00
|
|
|
getCoreAppsBasePath: function() {
|
2013-03-15 14:18:58 +00:00
|
|
|
return AppsUtils.getCoreAppsBasePath();
|
2012-12-22 13:56:21 +00:00
|
|
|
},
|
|
|
|
|
2014-01-17 00:26:24 +00:00
|
|
|
getWebAppsBasePath: function() {
|
|
|
|
return OS.Path.dirname(this.appsFile);
|
2012-12-22 13:56:21 +00:00
|
|
|
},
|
|
|
|
|
2015-09-18 05:49:24 +00:00
|
|
|
updateDataStoreEntriesFromLocalId: function(aLocalId) {
|
|
|
|
let app = appsService.getAppByLocalId(aLocalId);
|
|
|
|
if (app) {
|
|
|
|
this.updateDataStoreForApp(app.id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-02 00:00:22 +00:00
|
|
|
_isLaunchable: function(aApp) {
|
2012-06-29 20:46:21 +00:00
|
|
|
if (this.allAppsLaunchable)
|
|
|
|
return true;
|
|
|
|
|
2013-08-02 00:00:22 +00:00
|
|
|
return WebappOSUtils.isLaunchable(aApp);
|
2012-09-26 22:03:25 +00:00
|
|
|
},
|
|
|
|
|
2013-05-20 17:20:19 +00:00
|
|
|
_notifyCategoryAndObservers: function(subject, topic, data, msg) {
|
2012-09-26 22:03:25 +00: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 16:24:45 +00:00
|
|
|
try {
|
|
|
|
let observer = enumerator.getNext().QueryInterface(Ci.nsIObserver);
|
|
|
|
if (observers.indexOf(observer) == -1) {
|
|
|
|
observers.push(observer);
|
|
|
|
}
|
|
|
|
} catch (e) { }
|
2012-09-26 22:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
observers.forEach(function (observer) {
|
|
|
|
try {
|
|
|
|
observer.observe(subject, topic, data);
|
|
|
|
} catch(e) { }
|
|
|
|
});
|
2013-05-20 17:20:19 +00:00
|
|
|
// Send back an answer to the child.
|
2013-06-07 16:52:58 +00:00
|
|
|
if (msg) {
|
|
|
|
ppmm.broadcastAsyncMessage("Webapps:ClearBrowserData:Return", msg);
|
|
|
|
}
|
2012-09-26 22:03:25 +00:00
|
|
|
},
|
|
|
|
|
2014-09-24 06:14:54 +00:00
|
|
|
registerBrowserElementParentForApp: function(aMsg, aMn) {
|
|
|
|
let appId = this.getAppLocalIdByManifestURL(aMsg.manifestURL);
|
|
|
|
if (appId == Ci.nsIScriptSecurityManager.NO_APP_ID) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-26 22:03:25 +00:00
|
|
|
// Make a listener function that holds on to this appId.
|
|
|
|
let listener = this.receiveAppMessage.bind(this, appId);
|
|
|
|
|
|
|
|
this.frameMessages.forEach(function(msgName) {
|
2014-09-24 06:14:54 +00:00
|
|
|
aMn.addMessageListener(msgName, listener);
|
2012-09-26 22:03:25 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveAppMessage: function(appId, message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "Webapps:ClearBrowserData":
|
2013-05-20 17:20:19 +00:00
|
|
|
this._clearPrivateData(appId, true, message.data);
|
2012-09-26 22:03:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-09-28 16:24:45 +00:00
|
|
|
},
|
|
|
|
|
2013-05-20 17:20:19 +00:00
|
|
|
_clearPrivateData: function(appId, browserOnly, msg) {
|
2012-09-28 16:24:45 +00:00
|
|
|
let subject = {
|
|
|
|
appId: appId,
|
|
|
|
browserOnly: browserOnly,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams])
|
|
|
|
};
|
2015-07-09 19:14:26 +00:00
|
|
|
this._clearOriginData(appId, browserOnly);
|
2013-05-20 17:20:19 +00:00
|
|
|
this._notifyCategoryAndObservers(subject, "webapps-clear-data", null, msg);
|
2015-05-27 08:10:42 +00:00
|
|
|
},
|
|
|
|
|
2015-07-09 19:14:26 +00:00
|
|
|
_clearOriginData: function(appId, browserOnly) {
|
|
|
|
let attributes = {appId: appId};
|
|
|
|
if (browserOnly) {
|
|
|
|
attributes.inBrowser = true;
|
2015-05-27 08:10:42 +00:00
|
|
|
}
|
2015-07-09 19:14:26 +00:00
|
|
|
this._notifyCategoryAndObservers(null, "clear-origin-data", JSON.stringify(attributes));
|
2012-04-28 07:10:08 +00:00
|
|
|
}
|
2011-11-28 20:13:26 +00:00
|
|
|
};
|
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
/**
|
|
|
|
* Appcache download observer
|
|
|
|
*/
|
2015-09-15 18:19:45 +00:00
|
|
|
var AppcacheObserver = function(aApp) {
|
2012-11-27 04:03:43 +00:00
|
|
|
debug("Creating AppcacheObserver for " + aApp.origin +
|
|
|
|
" - " + aApp.installState);
|
2012-06-11 18:41:46 +00:00
|
|
|
this.app = aApp;
|
2012-09-27 01:01:20 +00:00
|
|
|
this.startStatus = aApp.installState;
|
2012-12-13 17:54:49 +00:00
|
|
|
this.lastProgressTime = 0;
|
2013-10-17 12:47:58 +00:00
|
|
|
// Send a first progress event to correctly set the DOM object's properties.
|
2013-02-11 08:38:51 +00:00
|
|
|
this._sendProgressEvent();
|
2012-06-11 18:41:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AppcacheObserver.prototype = {
|
|
|
|
// nsIOfflineCacheUpdateObserver implementation
|
2013-02-11 08:38:51 +00:00
|
|
|
_sendProgressEvent: function() {
|
|
|
|
let app = this.app;
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 12:47:58 +00:00
|
|
|
app: app,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 12:47:58 +00:00
|
|
|
eventType: "progress",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2013-02-11 08:38:51 +00:00
|
|
|
},
|
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
updateStateChanged: function appObs_Update(aUpdate, aState) {
|
|
|
|
let mustSave = false;
|
|
|
|
let app = this.app;
|
|
|
|
|
2012-11-27 04:03:43 +00:00
|
|
|
debug("Offline cache state change for " + app.origin + " : " + aState);
|
|
|
|
|
2013-02-11 08:38:51 +00:00
|
|
|
var self = this;
|
2013-01-11 10:58:33 +00:00
|
|
|
let setStatus = function appObs_setStatus(aStatus, aProgress) {
|
2013-02-11 08:38:51 +00:00
|
|
|
debug("Offlinecache setStatus to " + aStatus + " with progress " +
|
2013-10-17 12:47:58 +00:00
|
|
|
aProgress + " for " + app.origin);
|
2012-09-27 01:01:20 +00:00
|
|
|
mustSave = (app.installState != aStatus);
|
2013-10-17 12:47:58 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
app.installState = aStatus;
|
2013-01-11 10:58:33 +00:00
|
|
|
app.progress = aProgress;
|
2013-10-17 12:47:58 +00:00
|
|
|
if (aStatus != "installed") {
|
|
|
|
self._sendProgressEvent();
|
|
|
|
return;
|
2012-11-29 14:19:37 +00:00
|
|
|
}
|
2013-10-17 12:47:58 +00:00
|
|
|
|
|
|
|
app.updateTime = Date.now();
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 12:47:58 +00:00
|
|
|
app: app,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 12:47:58 +00:00
|
|
|
eventType: ["downloadsuccess", "downloadapplied"],
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let setError = function appObs_setError(aError) {
|
2012-11-27 04:03:43 +00:00
|
|
|
debug("Offlinecache setError to " + aError);
|
2012-10-04 18:18:39 +00:00
|
|
|
app.downloading = false;
|
2014-06-20 23:32:15 +00:00
|
|
|
mustSave = true;
|
|
|
|
|
|
|
|
// If we are canceling the download, we already send a DOWNLOAD_CANCELED
|
|
|
|
// error.
|
|
|
|
if (app.isCanceling) {
|
|
|
|
delete app.isCanceling;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:UpdateState", {
|
2013-10-17 12:47:58 +00:00
|
|
|
app: app,
|
2014-05-10 08:16:07 +00:00
|
|
|
error: aError,
|
2014-05-24 19:28:13 +00:00
|
|
|
id: app.id
|
2013-10-17 12:47:58 +00:00
|
|
|
});
|
2015-08-06 03:30:55 +00:00
|
|
|
MessageBroadcaster.broadcastMessage("Webapps:FireEvent", {
|
2013-10-17 12:47:58 +00:00
|
|
|
eventType: "downloaderror",
|
|
|
|
manifestURL: app.manifestURL
|
|
|
|
});
|
2012-06-11 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (aState) {
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ERROR:
|
|
|
|
aUpdate.removeObserver(this);
|
2013-01-25 02:24:17 +00:00
|
|
|
AppDownloadManager.remove(app.manifestURL);
|
2012-09-27 01:01:20 +00:00
|
|
|
setError("APP_CACHE_DOWNLOAD_ERROR");
|
2012-06-11 18:41:46 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_NOUPDATE:
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_FINISHED:
|
|
|
|
aUpdate.removeObserver(this);
|
2013-01-25 02:24:17 +00:00
|
|
|
AppDownloadManager.remove(app.manifestURL);
|
2013-01-11 10:58:33 +00:00
|
|
|
setStatus("installed", aUpdate.byteProgress);
|
2012-06-11 18:41:46 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_DOWNLOADING:
|
2013-01-11 10:58:33 +00:00
|
|
|
setStatus(this.startStatus, aUpdate.byteProgress);
|
2012-06-11 18:41:46 +00:00
|
|
|
break;
|
2013-10-17 12:47:58 +00:00
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMSTARTED:
|
2012-12-13 17:54:49 +00:00
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMPROGRESS:
|
|
|
|
let now = Date.now();
|
|
|
|
if (now - this.lastProgressTime > MIN_PROGRESS_EVENT_DELAY) {
|
2013-01-11 10:58:33 +00:00
|
|
|
setStatus(this.startStatus, aUpdate.byteProgress);
|
2012-12-13 17:54:49 +00:00
|
|
|
this.lastProgressTime = now;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-11 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Status changed, update the stored version.
|
|
|
|
if (mustSave) {
|
|
|
|
DOMApplicationRegistry._saveApps();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applicationCacheAvailable: function appObs_CacheAvail(aApplicationCache) {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
DOMApplicationRegistry.init();
|