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
|
|
|
|
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-10-03 05:38:06 +00:00
|
|
|
Cu.import("resource://gre/modules/PermissionsInstaller.jsm");
|
2012-10-23 20:52:57 +00:00
|
|
|
Cu.import("resource://gre/modules/OfflineCacheInstaller.jsm");
|
2012-12-05 03:49:26 +00:00
|
|
|
Cu.import("resource://gre/modules/SystemMessagePermissionsChecker.jsm");
|
2012-12-21 18:28:55 +00:00
|
|
|
Cu.import("resource://gre/modules/AppDownloadManager.jsm");
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
function debug(aMsg) {
|
2012-11-27 16:40:18 +00:00
|
|
|
//dump("-*-*- Webapps.jsm : " + aMsg + "\n");
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 17:54:49 +00:00
|
|
|
// Minimum delay between two progress events while downloading, in ms.
|
|
|
|
const MIN_PROGRESS_EVENT_DELAY = 1000;
|
|
|
|
|
2012-04-17 14:11:53 +00:00
|
|
|
const WEBAPP_RUNTIME = Services.appinfo.ID == "webapprt@mozilla.org";
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
|
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
return NetUtil;
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2012-03-23 23:39:15 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
const DIRECTORY_NAME = "webappsDir";
|
2012-08-15 21:02:15 +00:00
|
|
|
#elifdef ANDROID
|
|
|
|
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.
|
|
|
|
const DIRECTORY_NAME = WEBAPP_RUNTIME ? "WebappRegD" : "ProfD";
|
2012-03-23 23:39:15 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.DOMApplicationRegistry = {
|
2011-11-28 20:13:26 +00:00
|
|
|
appsFile: null,
|
|
|
|
webapps: { },
|
2012-08-28 02:43:57 +00:00
|
|
|
children: [ ],
|
2012-06-29 20:46:21 +00:00
|
|
|
allAppsLaunchable: false,
|
2011-11-28 20:13:26 +00:00
|
|
|
|
|
|
|
init: function() {
|
2012-03-13 00:33:10 +00:00
|
|
|
this.messages = ["Webapps:Install", "Webapps:Uninstall",
|
2012-10-02 20:38:51 +00:00
|
|
|
"Webapps:GetSelf", "Webapps:CheckInstalled",
|
2012-08-28 02:43:57 +00:00
|
|
|
"Webapps:GetInstalled", "Webapps:GetNotInstalled",
|
|
|
|
"Webapps:Launch", "Webapps:GetAll",
|
2012-12-22 13:56:21 +00:00
|
|
|
"Webapps:InstallPackage", "Webapps:GetAppInfo",
|
2012-09-18 17:34:55 +00:00
|
|
|
"Webapps:GetList", "Webapps:RegisterForMessages",
|
2012-10-19 18:17:05 +00:00
|
|
|
"Webapps:UnregisterForMessages",
|
2012-09-29 17:57:18 +00:00
|
|
|
"Webapps:CancelDownload", "Webapps:CheckForUpdate",
|
2012-10-30 00:17:47 +00:00
|
|
|
"Webapps:Download", "Webapps:ApplyDownload",
|
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);
|
|
|
|
|
2012-03-13 00:33:10 +00:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.registerCancelFunction(this.cancelDownload.bind(this));
|
|
|
|
|
2012-07-11 15:38:33 +00:00
|
|
|
this.appsFile = FileUtils.getFile(DIRECTORY_NAME,
|
|
|
|
["webapps", "webapps.json"], true);
|
2011-12-06 04:22:01 +00:00
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
this.loadAndUpdateApps();
|
|
|
|
},
|
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.
|
|
|
|
// aNext() is called after we load the current webapps list.
|
|
|
|
loadCurrentRegistry: function loadCurrentRegistry(aNext) {
|
|
|
|
let file = FileUtils.getFile(DIRECTORY_NAME, ["webapps", "webapps.json"], false);
|
2012-10-23 20:52:57 +00:00
|
|
|
if (file && file.exists()) {
|
2012-09-19 16:28:55 +00:00
|
|
|
this._loadJSONAsync(file, (function loadRegistry(aData) {
|
|
|
|
if (aData) {
|
|
|
|
this.webapps = aData;
|
|
|
|
let appDir = FileUtils.getDir(DIRECTORY_NAME, ["webapps"], false);
|
|
|
|
for (let id in this.webapps) {
|
2012-12-22 13:56:21 +00:00
|
|
|
|
|
|
|
this.webapps[id].id = id;
|
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
// Make sure we have a localId
|
|
|
|
if (this.webapps[id].localId === undefined) {
|
|
|
|
this.webapps[id].localId = this._nextLocalId();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.webapps[id].basePath === undefined) {
|
|
|
|
this.webapps[id].basePath = appDir.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to removable apps.
|
|
|
|
if (this.webapps[id].removable === undefined) {
|
|
|
|
this.webapps[id].removable = true;
|
2012-08-29 21:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default to a non privileged status.
|
|
|
|
if (this.webapps[id].appStatus === undefined) {
|
|
|
|
this.webapps[id].appStatus = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
}
|
2012-12-10 23:49:02 +00:00
|
|
|
|
|
|
|
// Default to NO_APP_ID and not in browser.
|
|
|
|
if (this.webapps[id].installerAppId === undefined) {
|
|
|
|
this.webapps[id].installerAppId = Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
}
|
|
|
|
if (this.webapps[id].installerIsBrowser === undefined) {
|
|
|
|
this.webapps[id].installerIsBrowser = false;
|
|
|
|
}
|
2013-01-06 23:56:40 +00:00
|
|
|
|
|
|
|
// Default installState to "installed".
|
|
|
|
if (this.webapps[id].installState === undefined) {
|
|
|
|
this.webapps[id].installState = "installed";
|
|
|
|
}
|
2012-08-29 21:20:03 +00:00
|
|
|
};
|
2012-09-19 16:28:55 +00:00
|
|
|
}
|
|
|
|
aNext();
|
|
|
|
}).bind(this));
|
|
|
|
} else {
|
|
|
|
aNext();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-17 04:30:43 +00:00
|
|
|
// Notify we are starting with registering apps.
|
|
|
|
notifyAppsRegistryStart: function notifyAppsRegistryStart() {
|
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-start", null);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Notify we are done with registering apps and save a copy of the registry.
|
|
|
|
notifyAppsRegistryReady: function notifyAppsRegistryReady() {
|
2012-09-19 16:28:55 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-ready", null);
|
|
|
|
this._saveApps();
|
|
|
|
},
|
|
|
|
|
2012-10-17 04:30:43 +00:00
|
|
|
// Registers all the activities and system messages.
|
2012-11-03 03:28:32 +00:00
|
|
|
registerAppsHandlers: function registerAppsHandlers(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 });
|
|
|
|
}
|
2012-10-19 10:43:17 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
2012-11-03 03:28:32 +00:00
|
|
|
this._processManifestForIds(ids, aRunUpdate);
|
2012-09-19 16:28:55 +00:00
|
|
|
#else
|
2012-10-19 10:43:17 +00:00
|
|
|
// Read the CSPs. If MOZ_SYS_MSG is defined this is done on
|
|
|
|
// _processManifestForIds so as to not reading the manifests
|
|
|
|
// twice
|
|
|
|
this._readManifests(ids, (function readCSPs(aResults) {
|
|
|
|
aResults.forEach(function registerManifest(aResult) {
|
2012-12-07 17:07:04 +00:00
|
|
|
this.webapps[aResult.id].csp = aResult.manifest.csp || "";
|
2012-10-19 10:43:17 +00:00
|
|
|
}, this);
|
|
|
|
}).bind(this));
|
|
|
|
|
2012-09-19 16:28:55 +00:00
|
|
|
// Nothing else to do but notifying we're ready.
|
2012-10-17 04:30:43 +00:00
|
|
|
this.notifyAppsRegistryReady();
|
2012-09-12 01:17:01 +00:00
|
|
|
#endif
|
2012-09-19 16:28:55 +00:00
|
|
|
},
|
|
|
|
|
2012-10-10 16:16:49 +00:00
|
|
|
updatePermissionsForApp: function updatePermissionsForApp(aId) {
|
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.
|
|
|
|
this._readManifests([{ id: aId }], (function(aResult) {
|
|
|
|
let data = aResult[0];
|
|
|
|
PermissionsInstaller.installPermissions({
|
|
|
|
manifest: data.manifest,
|
|
|
|
manifestURL: this.webapps[aId].manifestURL,
|
|
|
|
origin: this.webapps[aId].origin
|
|
|
|
}, true, function() {
|
|
|
|
debug("Error installing permissions for " + aId);
|
|
|
|
});
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
updateOfflineCacheForApp: function updateOfflineCacheForApp(aId) {
|
|
|
|
let app = this.webapps[aId];
|
|
|
|
OfflineCacheInstaller.installCache({
|
|
|
|
basePath: app.basePath,
|
|
|
|
appId: aId,
|
|
|
|
origin: app.origin,
|
|
|
|
localId: app.localId
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-12-10 20:51:10 +00:00
|
|
|
// Installs a 3rd party packaged app.
|
|
|
|
installPreinstalledPackage: function installPreinstalledPackage(aId) {
|
2012-12-11 21:45:37 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-12-10 20:51:10 +00:00
|
|
|
let app = this.webapps[aId];
|
|
|
|
let baseDir;
|
|
|
|
try {
|
2012-12-22 13:56:21 +00:00
|
|
|
baseDir = FileUtils.getDir("coreAppsDir", ["webapps", aId], false);
|
2012-12-10 20:51:10 +00:00
|
|
|
} catch(e) {
|
|
|
|
// In ENG builds, we don't have apps in coreAppsDir.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let updateFile = baseDir.clone();
|
|
|
|
updateFile.append("update.webapp");
|
|
|
|
if (!updateFile.exists()) {
|
|
|
|
// The update manifest is missing, bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Installing 3rd party packaged app : " + aId +
|
|
|
|
" from " + baseDir.path);
|
|
|
|
|
|
|
|
// We copy this app to DIRECTORY_NAME/$aId, and set the base path as needed.
|
|
|
|
let destDir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
|
|
|
|
destDir.permissions = FileUtils.PERMS_DIRECTORY;
|
|
|
|
|
|
|
|
["application.zip", "update.webapp"]
|
|
|
|
.forEach(function(aFile) {
|
|
|
|
let file = baseDir.clone();
|
|
|
|
file.append(aFile);
|
|
|
|
file.copyTo(destDir, aFile);
|
|
|
|
let newFile = destDir.clone();
|
|
|
|
newFile.append(aFile);
|
|
|
|
newFile.permissions = FileUtils.PERMS_FILE;
|
|
|
|
});
|
|
|
|
|
|
|
|
app.basePath = FileUtils.getDir(DIRECTORY_NAME, ["webapps"], true, true)
|
|
|
|
.path;
|
|
|
|
app.origin = "app://" + aId;
|
|
|
|
app.removable = true;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
manifestFile.permissions = FileUtils.PERMS_FILE;
|
|
|
|
} catch(e) {
|
|
|
|
// If we are unable to extract the manifest, cleanup and remove this app.
|
|
|
|
debug("Cleaning up: " + e);
|
|
|
|
destDir.remove(true);
|
|
|
|
delete this.webapps[aId];
|
|
|
|
} finally {
|
|
|
|
zipReader.close();
|
|
|
|
}
|
2012-12-11 21:45:37 +00:00
|
|
|
#endif
|
2012-12-10 20:51:10 +00:00
|
|
|
},
|
|
|
|
|
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()
|
2012-10-23 20:52:57 +00:00
|
|
|
installSystemApps: function installSystemApps(aNext) {
|
|
|
|
let file;
|
|
|
|
try {
|
|
|
|
file = FileUtils.getFile("coreAppsDir", ["webapps", "webapps.json"], false);
|
|
|
|
} catch(e) { }
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
if (file && file.exists()) {
|
|
|
|
// a
|
|
|
|
this._loadJSONAsync(file, (function loadCoreRegistry(aData) {
|
|
|
|
if (!aData) {
|
|
|
|
aNext();
|
|
|
|
return;
|
|
|
|
}
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
// b : core apps are not removable.
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (id in aData || this.webapps[id].removable)
|
|
|
|
continue;
|
|
|
|
delete this.webapps[id];
|
|
|
|
// Remove the permissions, cookies and private data for this app.
|
|
|
|
let localId = this.webapps[id].localId;
|
|
|
|
let permMgr = Cc["@mozilla.org/permissionmanager;1"]
|
|
|
|
.getService(Ci.nsIPermissionManager);
|
2013-01-07 17:37:01 +00:00
|
|
|
permMgr.RemovePermissionsForApp(localId, false);
|
2012-10-23 20:52:57 +00:00
|
|
|
Services.cookies.removeCookiesForApp(localId, false);
|
|
|
|
this._clearPrivateData(localId, false);
|
|
|
|
}
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
let appDir = FileUtils.getDir("coreAppsDir", ["webapps"], false);
|
|
|
|
// c
|
|
|
|
for (let id in aData) {
|
|
|
|
// Core apps have ids matching their domain name (eg: dialer.gaiamobile.org)
|
|
|
|
// Use that property to check if they are new or not.
|
|
|
|
if (!(id in this.webapps)) {
|
|
|
|
this.webapps[id] = aData[id];
|
|
|
|
this.webapps[id].basePath = appDir.path;
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2012-12-22 13:56:21 +00:00
|
|
|
this.webapps[id].id = id;
|
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
// Create a new localId.
|
|
|
|
this.webapps[id].localId = this._nextLocalId();
|
2012-09-19 16:28:55 +00:00
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
// Core apps are not removable.
|
|
|
|
if (this.webapps[id].removable === undefined) {
|
|
|
|
this.webapps[id].removable = false;
|
2012-09-19 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-23 20:52:57 +00:00
|
|
|
}
|
|
|
|
aNext();
|
|
|
|
}).bind(this));
|
|
|
|
} else {
|
|
|
|
aNext();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-30 07:11:17 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
fixIndexedDb: function fixIndexedDb() {
|
|
|
|
debug("Fixing indexedDb folder names");
|
|
|
|
let idbDir = FileUtils.getDir("indexedDBPDir", ["indexedDB"]);
|
|
|
|
|
|
|
|
if (!idbDir.isDirectory()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let re = /^(\d+)\+(.*)\+(f|t)$/;
|
|
|
|
|
|
|
|
let entries = idbDir.directoryEntries;
|
|
|
|
while (entries.hasMoreElements()) {
|
|
|
|
let entry = entries.getNext().QueryInterface(Ci.nsIFile);
|
|
|
|
if (!entry.isDirectory()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let newName = entry.leafName.replace(re, "$1+$3+$2");
|
|
|
|
if (newName != entry.leafName) {
|
|
|
|
try {
|
|
|
|
entry.moveTo(idbDir, newName);
|
|
|
|
} catch(e) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
loadAndUpdateApps: function loadAndUpdateApps() {
|
|
|
|
let runUpdate = AppsUtils.isFirstRun(Services.prefs);
|
|
|
|
|
2012-10-30 07:11:17 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
if (runUpdate) {
|
|
|
|
this.fixIndexedDb();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-23 20:52:57 +00:00
|
|
|
let onAppsLoaded = (function onAppsLoaded() {
|
|
|
|
if (runUpdate) {
|
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) {
|
2012-12-10 20:51:10 +00:00
|
|
|
this.installPreinstalledPackage(id);
|
|
|
|
if (!this.webapps[id]) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-23 20:52:57 +00:00
|
|
|
this.updateOfflineCacheForApp(id);
|
2012-12-10 20:51:10 +00:00
|
|
|
this.updatePermissionsForApp(id);
|
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
|
|
|
|
// installPreinstalledPackage() removes the ones failing to install.
|
|
|
|
this._saveApps();
|
2012-08-29 21:20:03 +00:00
|
|
|
}
|
2012-11-03 03:28:32 +00:00
|
|
|
this.registerAppsHandlers(runUpdate);
|
2012-10-23 20:52:57 +00:00
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
this.loadCurrentRegistry((function() {
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// if first run, merge the system apps.
|
|
|
|
if (runUpdate)
|
|
|
|
this.installSystemApps(onAppsLoaded);
|
|
|
|
else
|
|
|
|
onAppsLoaded();
|
2012-12-06 21:44:29 +00:00
|
|
|
|
|
|
|
// XXX: To be removed as soon as the app:// protocol is remoted.
|
|
|
|
// See Bug 819061
|
|
|
|
let dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
dir.initWithPath("/data");
|
|
|
|
dir.permissions = parseInt("755", 8);
|
|
|
|
dir.append("local");
|
|
|
|
dir.permissions = parseInt("755", 8);
|
|
|
|
dir.append("webapps");
|
|
|
|
dir.permissions = parseInt("755", 8);
|
2012-09-19 16:28:55 +00:00
|
|
|
#else
|
2012-10-23 20:52:57 +00:00
|
|
|
onAppsLoaded();
|
2012-09-19 16:28:55 +00:00
|
|
|
#endif
|
2012-08-29 21:20:03 +00:00
|
|
|
}).bind(this));
|
2011-12-05 23:26:18 +00:00
|
|
|
},
|
|
|
|
|
2012-07-03 00:16:55 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
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.
|
|
|
|
_registerSystemMessagesForEntryPoint: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.messages || !Array.isArray(root.messages) ||
|
|
|
|
root.messages.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:38:03 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin);
|
2012-09-13 18:47:34 +00:00
|
|
|
let launchPath = Services.io.newURI(manifest.fullLaunchPath(aEntryPoint), null, null);
|
|
|
|
let manifestURL = Services.io.newURI(aApp.manifestURL, null, null);
|
|
|
|
root.messages.forEach(function registerPages(aMessage) {
|
|
|
|
let href = launchPath;
|
|
|
|
let messageName;
|
|
|
|
if (typeof(aMessage) === "object" && Object.keys(aMessage).length === 1) {
|
|
|
|
messageName = Object.keys(aMessage)[0];
|
|
|
|
href = Services.io.newURI(manifest.resolveFromOrigin(aMessage[messageName]), null, null);
|
|
|
|
} else {
|
|
|
|
messageName = aMessage;
|
|
|
|
}
|
2012-12-05 03:49:26 +00:00
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister(messageName,
|
|
|
|
aApp.origin,
|
|
|
|
aManifest)) {
|
|
|
|
msgmgr.registerPage(messageName, href, manifestURL);
|
|
|
|
}
|
2012-09-13 18:47:34 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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.
|
2012-11-03 03:28:32 +00:00
|
|
|
_createActivitiesToRegister: function(aManifest, aApp, aEntryPoint, aRunUpdate) {
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.activities) {
|
2012-10-18 07:32:15 +00:00
|
|
|
return activitiesToRegister;
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 05:38:03 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, aApp.origin);
|
2012-09-13 18:47:34 +00:00
|
|
|
for (let activity in root.activities) {
|
|
|
|
let description = root.activities[activity];
|
2012-08-15 02:40:12 +00:00
|
|
|
if (!description.href) {
|
|
|
|
description.href = manifest.launch_path;
|
|
|
|
}
|
|
|
|
description.href = manifest.resolveFromOrigin(description.href);
|
2012-11-03 03:28:32 +00:00
|
|
|
|
|
|
|
if (aRunUpdate) {
|
|
|
|
activitiesToRegister.push({ "manifest": aApp.manifestURL,
|
|
|
|
"name": activity,
|
|
|
|
"icon": manifest.iconURLForSize(128),
|
|
|
|
"description": description });
|
|
|
|
}
|
|
|
|
|
|
|
|
let launchPath = Services.io.newURI(description.href, null, null);
|
2012-07-20 15:41:30 +00:00
|
|
|
let manifestURL = Services.io.newURI(aApp.manifestURL, null, null);
|
2012-12-05 03:49:26 +00:00
|
|
|
|
|
|
|
if (SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToRegister("activity",
|
|
|
|
aApp.origin,
|
|
|
|
aManifest)) {
|
|
|
|
msgmgr.registerPage("activity", launchPath, manifestURL);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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
|
|
|
// |aEntryPoint| is either the entry_point name or the null in which case we
|
|
|
|
// use the root of the manifest.
|
|
|
|
_createActivitiesToUnregister: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let activitiesToUnregister = [];
|
2012-09-13 18:47:34 +00:00
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.activities) {
|
2012-10-18 07:32:15 +00:00
|
|
|
return activitiesToUnregister;
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 18:47:34 +00:00
|
|
|
for (let activity in root.activities) {
|
|
|
|
let description = root.activities[activity];
|
2012-10-18 07:32:15 +00:00
|
|
|
activitiesToUnregister.push({ "manifest": aApp.manifestURL,
|
|
|
|
"name": activity });
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
2012-10-18 07:32:15 +00:00
|
|
|
return activitiesToUnregister;
|
2012-07-20 15:41:30 +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,
|
|
|
|
this._createActivitiesToUnregister(manifest, app, null));
|
|
|
|
|
|
|
|
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,
|
|
|
|
this._createActivitiesToUnregister(manifest, app, entryPoint));
|
|
|
|
}
|
|
|
|
}, 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) {
|
2012-09-12 01:17:01 +00:00
|
|
|
this._readManifests(aIds, (function registerManifests(aResults) {
|
2012-10-18 07:32:15 +00:00
|
|
|
let appsToRegister = [];
|
2012-09-12 01:17:01 +00:00
|
|
|
aResults.forEach(function registerManifest(aResult) {
|
|
|
|
let app = this.webapps[aResult.id];
|
|
|
|
let manifest = aResult.manifest;
|
|
|
|
app.name = manifest.name;
|
2012-10-19 10:43:17 +00:00
|
|
|
app.csp = manifest.csp || "";
|
2012-09-12 01:17:01 +00:00
|
|
|
this._registerSystemMessages(manifest, app);
|
2012-10-18 07:32:15 +00:00
|
|
|
appsToRegister.push({ manifest: manifest, app: app });
|
2012-09-12 01:17:01 +00:00
|
|
|
}, this);
|
2012-11-03 03:28:32 +00:00
|
|
|
this._registerActivitiesForApps(appsToRegister, aRunUpdate);
|
2012-07-03 00:16:55 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
|
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");
|
|
|
|
ppmm = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
_loadJSONAsync: function(aFile, aCallback) {
|
2011-11-28 20:13:26 +00:00
|
|
|
try {
|
2011-12-05 23:26:18 +00:00
|
|
|
let channel = NetUtil.newChannel(aFile);
|
2011-11-28 20:13:26 +00:00
|
|
|
channel.contentType = "application/json";
|
|
|
|
NetUtil.asyncFetch(channel, function(aStream, aResult) {
|
|
|
|
if (!Components.isSuccessCode(aResult)) {
|
2012-07-11 15:38:33 +00:00
|
|
|
Cu.reportError("DOMApplicationRegistry: Could not read from json file "
|
|
|
|
+ aFile.path);
|
2011-12-05 23:26:18 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback(null);
|
2011-11-28 20:13:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read json file into a string
|
|
|
|
let data = null;
|
|
|
|
try {
|
2012-07-31 10:06:56 +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";
|
|
|
|
|
|
|
|
data = JSON.parse(converter.ConvertToUnicode(NetUtil.readInputStreamToString(aStream,
|
|
|
|
aStream.available()) || ""));
|
2011-11-28 20:13:26 +00:00
|
|
|
aStream.close();
|
2011-12-05 23:26:18 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback(data);
|
2011-11-28 20:13:26 +00:00
|
|
|
} catch (ex) {
|
2012-09-22 03:40:03 +00:00
|
|
|
Cu.reportError("DOMApplicationRegistry: Could not parse JSON: " +
|
2012-10-23 20:52:57 +00:00
|
|
|
aFile.path + " " + ex + "\n" + ex.stack);
|
2011-12-05 23:26:18 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback(null);
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
2012-07-11 15:38:33 +00:00
|
|
|
Cu.reportError("DOMApplicationRegistry: Could not read from " +
|
2012-10-23 20:52:57 +00:00
|
|
|
aFile.path + " : " + ex + "\n" + ex.stack);
|
2011-12-05 23:26:18 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback(null);
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-18 17:34:55 +00:00
|
|
|
addMessageListener: function(aMsgNames, aMm) {
|
|
|
|
aMsgNames.forEach(function (aMsgName) {
|
|
|
|
if (!(aMsgName in this.children)) {
|
|
|
|
this.children[aMsgName] = [];
|
|
|
|
}
|
2012-10-19 18:17:05 +00:00
|
|
|
|
|
|
|
let mmFound = this.children[aMsgName].some(function(mmRef) {
|
|
|
|
if (mmRef.mm === aMm) {
|
|
|
|
mmRef.refCount++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!mmFound) {
|
|
|
|
this.children[aMsgName].push({
|
|
|
|
mm: aMm,
|
|
|
|
refCount: 1
|
|
|
|
});
|
|
|
|
}
|
2012-09-18 17:34:55 +00:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
2012-10-19 18:17:05 +00:00
|
|
|
removeMessageListener: function(aMsgNames, aMm) {
|
|
|
|
if (aMsgNames.length === 1 &&
|
|
|
|
aMsgNames[0] === "Webapps:Internal:AllMessages") {
|
2012-11-09 00:53:31 +00:00
|
|
|
for (let msgName in this.children) {
|
|
|
|
let msg = this.children[msgName];
|
2012-10-19 18:17:05 +00:00
|
|
|
|
|
|
|
for (let mmI = msg.length - 1; mmI >= 0; mmI -= 1) {
|
|
|
|
let mmRef = msg[mmI];
|
|
|
|
if (mmRef.mm === aMm) {
|
|
|
|
msg.splice(mmI, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.length === 0) {
|
2012-11-09 00:53:31 +00:00
|
|
|
delete this.children[msgName];
|
2012-10-19 18:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMsgNames.forEach(function(aMsgName) {
|
|
|
|
if (!(aMsgName in this.children)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-01 20:29:59 +00:00
|
|
|
|
2012-10-19 18:17:05 +00:00
|
|
|
let removeIndex;
|
|
|
|
this.children[aMsgName].some(function(mmRef, index) {
|
|
|
|
if (mmRef.mm === aMm) {
|
|
|
|
mmRef.refCount--;
|
|
|
|
if (mmRef.refCount === 0) {
|
|
|
|
removeIndex = index;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2012-09-18 17:34:55 +00:00
|
|
|
|
2012-10-19 18:17:05 +00:00
|
|
|
if (removeIndex) {
|
|
|
|
this.children[aMsgName].splice(removeIndex, 1);
|
2012-09-18 17:34:55 +00:00
|
|
|
}
|
2012-10-19 18:17:05 +00:00
|
|
|
}, this);
|
2012-09-18 17:34:55 +00:00
|
|
|
},
|
|
|
|
|
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-09-30 03:08:01 +00:00
|
|
|
// We need to check permissions for calls coming from mozApps.mgmt.
|
|
|
|
// These are: getAll(), getNotInstalled() and applyDownload()
|
|
|
|
if (["Webapps:GetAll",
|
|
|
|
"Webapps:GetNotInstalled",
|
2012-10-30 00:17:47 +00:00
|
|
|
"Webapps:ApplyDownload"].indexOf(aMessage.name) != -1) {
|
2012-09-30 03:08:01 +00:00
|
|
|
if (!aMessage.target.assertPermission("webapps-manage")) {
|
|
|
|
debug("mozApps message " + aMessage.name +
|
|
|
|
" from a content process with no 'webapps-manage' privileges.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-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
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:Install":
|
|
|
|
// always ask for UI to install
|
2012-12-10 23:48:59 +00:00
|
|
|
this.doInstall(msg, mm);
|
|
|
|
//Services.obs.notifyObservers(mm, "webapps-ask-install", JSON.stringify(msg));
|
2011-11-28 20:13:26 +00:00
|
|
|
break;
|
2012-03-06 19:50:58 +00:00
|
|
|
case "Webapps:GetSelf":
|
2012-09-18 17:34:55 +00:00
|
|
|
this.getSelf(msg, mm);
|
2012-03-06 19:50:58 +00:00
|
|
|
break;
|
2011-11-28 20:13:26 +00:00
|
|
|
case "Webapps:Uninstall":
|
2012-09-28 16:24:45 +00:00
|
|
|
this.uninstall(msg, mm);
|
2011-11-28 20:13:26 +00:00
|
|
|
break;
|
|
|
|
case "Webapps:Launch":
|
2012-09-27 01:01:20 +00:00
|
|
|
this.launchApp(msg, mm);
|
2011-11-28 20:13:26 +00:00
|
|
|
break;
|
2012-10-02 20:38:51 +00:00
|
|
|
case "Webapps:CheckInstalled":
|
|
|
|
this.checkInstalled(msg, mm);
|
2012-09-25 15:04:24 +00:00
|
|
|
break;
|
2012-03-06 19:50:58 +00:00
|
|
|
case "Webapps:GetInstalled":
|
2012-09-18 17:34:55 +00:00
|
|
|
this.getInstalled(msg, mm);
|
2011-12-15 17:20:57 +00:00
|
|
|
break;
|
2012-06-29 20:46:21 +00:00
|
|
|
case "Webapps:GetNotInstalled":
|
2012-09-18 17:34:55 +00:00
|
|
|
this.getNotInstalled(msg, mm);
|
2012-06-29 20:46:21 +00:00
|
|
|
break;
|
2012-03-06 19:50:58 +00:00
|
|
|
case "Webapps:GetAll":
|
|
|
|
if (msg.hasPrivileges)
|
2012-09-18 17:34:55 +00:00
|
|
|
this.getAll(msg, mm);
|
2012-03-06 19:50:58 +00:00
|
|
|
else
|
2012-09-18 17:34:55 +00:00
|
|
|
mm.sendAsyncMessage("Webapps:GetAll:Return:KO", msg);
|
2011-11-28 20:13:26 +00:00
|
|
|
break;
|
2012-07-11 15:38:33 +00:00
|
|
|
case "Webapps:InstallPackage":
|
2012-12-10 23:48:59 +00:00
|
|
|
this.doInstallPackage(msg, mm);
|
2012-07-11 15:38:33 +00:00
|
|
|
break;
|
2012-12-22 13:56:21 +00:00
|
|
|
case "Webapps:GetAppInfo":
|
2012-12-14 19:29:25 +00:00
|
|
|
if (!this.webapps[msg.id]) {
|
|
|
|
debug("No webapp for " + msg.id);
|
|
|
|
return null;
|
|
|
|
}
|
2012-12-22 13:56:21 +00:00
|
|
|
return { "basePath": this.webapps[msg.id].basePath + "/",
|
|
|
|
"isCoreApp": !this.webapps[msg.id].removable };
|
2012-07-11 15:38:36 +00:00
|
|
|
break;
|
2012-09-18 17:34:55 +00:00
|
|
|
case "Webapps:RegisterForMessages":
|
|
|
|
this.addMessageListener(msg, mm);
|
|
|
|
break;
|
2012-10-19 18:17:05 +00:00
|
|
|
case "Webapps:UnregisterForMessages":
|
|
|
|
this.removeMessageListener(msg, mm);
|
|
|
|
break;
|
|
|
|
case "child-process-shutdown":
|
|
|
|
this.removeMessageListener(["Webapps:Internal:AllMessages"], mm);
|
|
|
|
break;
|
2012-08-28 02:43:57 +00:00
|
|
|
case "Webapps:GetList":
|
2012-09-18 17:34:55 +00:00
|
|
|
this.addMessageListener(["Webapps:AddApp", "Webapps:RemoveApp"], mm);
|
2012-08-28 02:43:57 +00:00
|
|
|
return this.webapps;
|
2012-09-29 17:57:18 +00:00
|
|
|
case "Webapps:Download":
|
|
|
|
this.startDownload(msg.manifestURL);
|
|
|
|
break;
|
2012-09-27 01:01:20 +00:00
|
|
|
case "Webapps:CancelDownload":
|
2012-09-29 17:57:18 +00:00
|
|
|
this.cancelDownload(msg.manifestURL);
|
2012-09-27 01:01:20 +00:00
|
|
|
break;
|
|
|
|
case "Webapps:CheckForUpdate":
|
|
|
|
this.checkForUpdate(msg, mm);
|
|
|
|
break;
|
2012-10-30 00:17:47 +00:00
|
|
|
case "Webapps:ApplyDownload":
|
|
|
|
this.applyDownload(msg.manifestURL);
|
2012-09-29 17:57:18 +00:00
|
|
|
break;
|
2012-09-19 16:28:55 +00:00
|
|
|
case "Activities:Register:OK":
|
2012-10-18 07:32:15 +00:00
|
|
|
this.notifyAppsRegistryReady();
|
2012-09-19 16:28:55 +00:00
|
|
|
break;
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-18 17:34:55 +00:00
|
|
|
// Some messages can be listened by several content processes:
|
|
|
|
// Webapps:AddApp
|
|
|
|
// Webapps:RemoveApp
|
|
|
|
// Webapps:Install:Return:OK
|
|
|
|
// Webapps:Uninstall:Return:OK
|
|
|
|
// Webapps:OfflineCache
|
|
|
|
broadcastMessage: function broadcastMessage(aMsgName, aContent) {
|
|
|
|
if (!(aMsgName in this.children)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-19 18:17:05 +00:00
|
|
|
this.children[aMsgName].forEach(function(mmRef) {
|
|
|
|
mmRef.mm.sendAsyncMessage(aMsgName, aContent);
|
|
|
|
});
|
2012-09-18 17:34:55 +00:00
|
|
|
},
|
|
|
|
|
2012-08-29 21:20:03 +00:00
|
|
|
_getAppDir: function(aId) {
|
|
|
|
return FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
|
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
_writeFile: function ss_writeFile(aFile, aData, aCallbak) {
|
|
|
|
// Initialize the file output stream.
|
2011-12-06 04:22:01 +00:00
|
|
|
let ostream = FileUtils.openSafeFileOutputStream(aFile);
|
2011-11-28 20:13:26 +00:00
|
|
|
|
|
|
|
// Obtain a converter to convert our data to a UTF-8 encoded input stream.
|
2012-07-11 15:38:33 +00:00
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
2011-11-28 20:13:26 +00:00
|
|
|
converter.charset = "UTF-8";
|
|
|
|
|
|
|
|
// Asynchronously copy the data to the file.
|
|
|
|
let istream = converter.convertToInputStream(aData);
|
|
|
|
NetUtil.asyncCopy(istream, ostream, function(rc) {
|
|
|
|
if (aCallbak)
|
|
|
|
aCallbak();
|
|
|
|
});
|
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
launchApp: function launchApp(aData, aMm) {
|
|
|
|
let app = this.getAppByManifestURL(aData.manifestURL);
|
|
|
|
if (!app) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:Launch:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire an error when trying to launch an app that is not
|
|
|
|
// yet fully installed.
|
|
|
|
if (app.installState == "pending") {
|
|
|
|
aMm.sendAsyncMessage("Webapps:Launch:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(aMm, "webapps-launch", JSON.stringify(aData));
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-18 21:43:13 +00:00
|
|
|
if (download.cacheUpdate) {
|
|
|
|
// Cancel hosted app download.
|
|
|
|
try {
|
|
|
|
download.cacheUpdate.cancel();
|
|
|
|
} catch (e) { debug (e); }
|
|
|
|
} else if (download.channel) {
|
|
|
|
// Cancel packaged app download.
|
|
|
|
download.channel.cancel(Cr.NS_BINDING_ABORTED);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = this.webapps[download.appId];
|
2012-09-27 19:34:41 +00:00
|
|
|
app.progress = 0;
|
2012-11-29 14:19:37 +00:00
|
|
|
app.installState = download.previousState;
|
2012-09-29 17:57:18 +00:00
|
|
|
app.downloading = false;
|
2012-10-30 00:17:47 +00:00
|
|
|
app.downloadAvailable = false;
|
2012-09-27 19:34:41 +00:00
|
|
|
app.downloadSize = 0;
|
|
|
|
this._saveApps((function() {
|
|
|
|
this.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "canceled",
|
2012-11-21 18:09:10 +00:00
|
|
|
manifestURL: app.manifestURL,
|
2012-09-27 19:34:41 +00:00
|
|
|
app: app,
|
2012-12-21 18:28:55 +00:00
|
|
|
error: error });
|
2012-09-27 19:34:41 +00:00
|
|
|
}).bind(this));
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.remove(aManifestURL);
|
2012-09-27 01:01:20 +00:00
|
|
|
},
|
|
|
|
|
2012-10-30 00:17:47 +00:00
|
|
|
startDownload: function startDownload(aManifestURL) {
|
|
|
|
debug("startDownload for " + aManifestURL);
|
|
|
|
let id = this._appIdForManifestURL(aManifestURL);
|
|
|
|
let app = this.webapps[id];
|
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);
|
2012-09-29 17:57:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
let file = FileUtils.getFile(DIRECTORY_NAME,
|
|
|
|
["webapps", id, "update.webapp"], true);
|
|
|
|
|
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.
|
|
|
|
this._readManifests([{ id: id }], (function readManifest(aResults) {
|
|
|
|
let jsonManifest = aResults[0].manifest;
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, app.origin);
|
|
|
|
|
|
|
|
if (manifest.appcache_path) {
|
|
|
|
debug("appcache found");
|
2012-11-29 14:19:37 +00:00
|
|
|
this.startOfflineCacheDownload(manifest, app, null, null, isUpdate);
|
2012-11-27 04:03:43 +00:00
|
|
|
} else {
|
|
|
|
// hosted app with no appcache, nothing to do, but we fire a
|
|
|
|
// downloaded event
|
2012-12-03 19:38:09 +00:00
|
|
|
debug("No appcache found, sending 'downloaded' for " + aManifestURL);
|
|
|
|
app.downloadAvailable = false;
|
2012-11-27 04:03:43 +00:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "downloaded",
|
|
|
|
manifestURL: aManifestURL,
|
|
|
|
app: app,
|
|
|
|
manifest: jsonManifest });
|
|
|
|
}
|
|
|
|
}).bind(this));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:57:18 +00:00
|
|
|
this._loadJSONAsync(file, (function(aJSON) {
|
|
|
|
if (!aJSON) {
|
2012-10-30 00:17:47 +00:00
|
|
|
debug("startDownload: No update manifest found at " + file.path + " " + aManifestURL);
|
2012-09-29 17:57:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:17:47 +00:00
|
|
|
let manifest = new ManifestHelper(aJSON, app.installOrigin);
|
2012-12-11 20:58:38 +00:00
|
|
|
this.downloadPackage(manifest, {
|
|
|
|
manifestURL: aManifestURL,
|
|
|
|
origin: app.origin,
|
|
|
|
downloadSize: app.downloadSize
|
|
|
|
}, isUpdate, function(aId, aManifest) {
|
2012-09-29 17:57:18 +00:00
|
|
|
// Success! Keep the zip in of TmpD, we'll move it out when
|
|
|
|
// applyDownload() will be called.
|
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", aId], true, true);
|
|
|
|
|
|
|
|
// Save the manifest in TmpD also
|
|
|
|
let manFile = tmpDir.clone();
|
|
|
|
manFile.append("manifest.webapp");
|
|
|
|
DOMApplicationRegistry._writeFile(manFile,
|
|
|
|
JSON.stringify(aManifest),
|
|
|
|
function() { });
|
|
|
|
// Set state and fire events.
|
|
|
|
app.downloading = false;
|
2012-10-30 00:17:47 +00:00
|
|
|
app.downloadAvailable = false;
|
2012-09-29 17:57:18 +00:00
|
|
|
app.readyToApplyDownload = true;
|
2012-11-17 03:37:41 +00:00
|
|
|
app.updateTime = Date.now();
|
2012-09-29 17:57:18 +00:00
|
|
|
DOMApplicationRegistry._saveApps(function() {
|
|
|
|
debug("About to fire Webapps:PackageEvent");
|
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "downloaded",
|
2012-10-30 00:17:47 +00:00
|
|
|
manifestURL: aManifestURL,
|
2012-09-29 17:57:18 +00:00
|
|
|
app: app,
|
|
|
|
manifest: aManifest });
|
2012-11-27 16:40:18 +00:00
|
|
|
if (app.installState == "pending") {
|
|
|
|
// We restarted a failed download, apply it automatically.
|
|
|
|
DOMApplicationRegistry.applyDownload(aManifestURL);
|
|
|
|
}
|
2012-09-29 17:57:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
applyDownload: function applyDownload(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];
|
2012-09-29 17:57:18 +00:00
|
|
|
if (!app || (app && !app.readyToApplyDownload)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-03 03:28:32 +00:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
|
|
|
|
2012-09-29 17:57:18 +00:00
|
|
|
// Move the application.zip and manifest.webapp files out of TmpD
|
2012-10-30 00:17:47 +00:00
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", id], true, true);
|
2012-09-29 17:57:18 +00:00
|
|
|
let manFile = tmpDir.clone();
|
|
|
|
manFile.append("manifest.webapp");
|
|
|
|
let appFile = tmpDir.clone();
|
|
|
|
appFile.append("application.zip");
|
|
|
|
|
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
|
|
|
appFile.moveTo(dir, "application.zip");
|
|
|
|
manFile.moveTo(dir, "manifest.webapp");
|
|
|
|
|
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) { }
|
|
|
|
|
2013-01-07 21:19:22 +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-09-29 17:57:18 +00:00
|
|
|
// Get the manifest, and set properties.
|
2012-10-10 21:29:07 +00:00
|
|
|
this.getManifestFor(app.origin, (function(aData) {
|
2012-11-30 16:57:45 +00:00
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
app.downloadSize = 0;
|
|
|
|
app.installState = "installed";
|
2012-09-29 17:57:18 +00:00
|
|
|
app.readyToApplyDownload = false;
|
2012-11-30 16:57:45 +00:00
|
|
|
delete app.retryingDownload;
|
|
|
|
|
|
|
|
DOMApplicationRegistry._saveApps(function() {
|
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "applied",
|
|
|
|
manifestURL: app.manifestURL,
|
|
|
|
app: app,
|
|
|
|
manifest: aData });
|
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({ manifest: aData,
|
|
|
|
origin: app.origin,
|
|
|
|
manifestURL: app.manifestURL },
|
|
|
|
true);
|
|
|
|
});
|
2012-10-10 21:29:07 +00:00
|
|
|
}).bind(this));
|
2012-09-29 17:57:18 +00:00
|
|
|
},
|
|
|
|
|
2012-10-04 18:18:39 +00:00
|
|
|
startOfflineCacheDownload: function startOfflineCacheDownload(aManifest, aApp,
|
|
|
|
aProfileDir,
|
2012-11-29 14:19:37 +00:00
|
|
|
aOfflineCacheObserver,
|
|
|
|
aIsUpdate) {
|
2012-12-18 21:43:13 +00:00
|
|
|
if (!aManifest.appcache_path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the manifest has an appcache_path property, use it to populate the
|
|
|
|
// appcache.
|
|
|
|
let appcacheURI = Services.io.newURI(aManifest.fullAppcachePath(),
|
|
|
|
null, null);
|
|
|
|
let docURI = Services.io.newURI(aManifest.fullLaunchPath(), null, null);
|
|
|
|
|
|
|
|
// We determine the app's 'installState' according to its previous
|
|
|
|
// state. Cancelled downloads should remain as 'pending'. Successfully
|
|
|
|
// installed apps should morph to 'updating'.
|
|
|
|
if (aIsUpdate) {
|
|
|
|
aApp.installState = "updating";
|
|
|
|
}
|
|
|
|
|
|
|
|
// We set the 'downloading' flag right before starting the app
|
|
|
|
// download/update.
|
|
|
|
aApp.downloading = true;
|
|
|
|
let cacheUpdate = aProfileDir
|
|
|
|
? updateSvc.scheduleCustomProfileUpdate(appcacheURI, docURI, aProfileDir)
|
|
|
|
: updateSvc.scheduleAppUpdate(appcacheURI, docURI, aApp.localId, false);
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
};
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.add(aApp.manifestURL, download);
|
2012-12-18 21:43:13 +00:00
|
|
|
|
|
|
|
cacheUpdate.addObserver(new AppcacheObserver(aApp), false);
|
|
|
|
if (aOfflineCacheObserver) {
|
|
|
|
cacheUpdate.addObserver(aOfflineCacheObserver, false);
|
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);
|
|
|
|
let id = this._appIdForManifestURL(aData.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
2012-10-18 10:32:03 +00:00
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
if (!app) {
|
|
|
|
aData.error = "NO_SUCH_APP";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendError(aError) {
|
|
|
|
aData.error = aError;
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updatePackagedApp(aManifest) {
|
|
|
|
debug("updatePackagedApp");
|
2012-10-30 00:17:47 +00:00
|
|
|
|
|
|
|
// if the app manifestURL has a app:// scheme, we can't have an
|
|
|
|
// update.
|
|
|
|
if (app.manifestURL.startsWith("app://")) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-07 17:10:32 +00:00
|
|
|
// Store the new update manifest.
|
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
|
|
|
let manFile = dir.clone();
|
|
|
|
manFile.append("update.webapp");
|
|
|
|
this._writeFile(manFile, JSON.stringify(aManifest), function() { });
|
|
|
|
|
2012-10-03 05:38:03 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, app.manifestURL);
|
2012-09-29 17:57:18 +00:00
|
|
|
// A package is available: set downloadAvailable to fire the matching
|
|
|
|
// event.
|
|
|
|
app.downloadAvailable = true;
|
|
|
|
app.downloadSize = manifest.size;
|
|
|
|
aData.event = "downloadavailable";
|
|
|
|
aData.app = {
|
|
|
|
downloadAvailable: true,
|
2012-12-20 20:08:58 +00:00
|
|
|
downloadSize: manifest.size,
|
|
|
|
updateManifest: aManifest
|
2012-09-29 17:57:18 +00:00
|
|
|
}
|
|
|
|
DOMApplicationRegistry._saveApps(function() {
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:OK", aData);
|
2012-12-20 20:08:58 +00:00
|
|
|
delete aData.app.updateManifest;
|
2012-09-29 17:57:18 +00:00
|
|
|
});
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateHostedApp(aManifest) {
|
2012-12-03 19:38:09 +00:00
|
|
|
debug("updateHostedApp " + aData.manifestURL);
|
2012-09-27 01:01:20 +00:00
|
|
|
let id = this._appId(app.origin);
|
|
|
|
|
2012-12-05 03:49:26 +00:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
2012-11-03 03:28:32 +00:00
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
|
|
|
|
2012-10-17 04:30:43 +00:00
|
|
|
// Update the web apps' registration.
|
|
|
|
this.notifyAppsRegistryStart();
|
2012-09-27 01:01:20 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
|
|
|
this._readManifests([{ id: id }], (function unregisterManifest(aResult) {
|
|
|
|
this._unregisterActivities(aResult[0].manifest, app);
|
|
|
|
this._registerSystemMessages(aManifest, app);
|
2012-11-03 03:28:32 +00:00
|
|
|
this._registerActivities(aManifest, app, true);
|
2012-09-27 01:01:20 +00:00
|
|
|
}).bind(this));
|
2012-10-17 04:30:43 +00:00
|
|
|
#else
|
|
|
|
// Nothing else to do but notifying we're ready.
|
|
|
|
this.notifyAppsRegistryReady();
|
2012-09-27 01:01:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Store the new manifest.
|
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
|
|
|
let manFile = dir.clone();
|
|
|
|
manFile.append("manifest.webapp");
|
|
|
|
this._writeFile(manFile, JSON.stringify(aManifest), function() { });
|
|
|
|
|
2012-10-03 05:38:03 +00:00
|
|
|
let manifest = new ManifestHelper(aManifest, app.origin);
|
2012-09-27 01:01:20 +00:00
|
|
|
|
2012-12-03 19:38:09 +00:00
|
|
|
app.installState = "installed";
|
|
|
|
app.downloading = false;
|
2012-12-21 18:28:55 +00:00
|
|
|
app.downloadSize = 0;
|
2012-12-03 19:38:09 +00:00
|
|
|
app.readyToApplyDownload = false;
|
|
|
|
app.downloadAvailable = !!manifest.appcache_path;
|
2012-09-27 01:01:20 +00:00
|
|
|
|
|
|
|
app.name = aManifest.name;
|
2012-10-19 10:43:17 +00:00
|
|
|
app.csp = aManifest.csp || "";
|
2012-11-17 03:37:41 +00:00
|
|
|
app.updateTime = Date.now();
|
2012-09-27 01:01:20 +00:00
|
|
|
|
|
|
|
// Update the registry.
|
|
|
|
this.webapps[id] = app;
|
|
|
|
|
2012-10-30 00:17:47 +00:00
|
|
|
this._saveApps(function() {
|
2012-12-03 19:38:09 +00:00
|
|
|
aData.app = app;
|
2012-12-20 20:08:58 +00:00
|
|
|
app.manifest = aManifest;
|
2012-12-05 16:50:32 +00:00
|
|
|
if (!manifest.appcache_path) {
|
|
|
|
aData.event = "downloadapplied";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:OK", aData);
|
|
|
|
} else {
|
|
|
|
// Check if the appcache is updatable, and send "downloadavailable" or
|
|
|
|
// "downloadapplied".
|
|
|
|
let updateObserver = {
|
2012-12-14 19:29:25 +00:00
|
|
|
observe: function(aSubject, aTopic, aObsData) {
|
2012-12-05 16:50:32 +00:00
|
|
|
aData.event =
|
|
|
|
aTopic == "offline-cache-update-available" ? "downloadavailable"
|
|
|
|
: "downloadapplied";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:OK", aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateSvc.checkForUpdate(Services.io.newURI(aData.manifestURL, null, null),
|
|
|
|
app.localId, false, updateObserver);
|
|
|
|
}
|
2012-12-20 20:08:58 +00:00
|
|
|
delete app.manifest;
|
2012-10-30 00:17:47 +00:00
|
|
|
});
|
2012-09-27 01:01:20 +00:00
|
|
|
|
2012-10-10 21:29:07 +00:00
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({ manifest: aManifest,
|
|
|
|
origin: app.origin,
|
|
|
|
manifestURL: aData.manifestURL },
|
2012-12-05 03:49:26 +00:00
|
|
|
true);
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 19:11:05 +00:00
|
|
|
// For non-removable hosted apps, we just check the appcache.
|
|
|
|
if (!app.removable) {
|
|
|
|
// Bail out for packaged apps.
|
|
|
|
if (app.origin.startsWith("app://")) {
|
|
|
|
aData.error = "NOT_UPDATABLE";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need the manifest to check if we have an appcache.
|
|
|
|
let id = this._appId(app.origin);
|
|
|
|
this._readManifests([{ id: id }], function(aResult) {
|
|
|
|
let manifest = aResult[0].manifest;
|
|
|
|
if (!manifest.appcache_path) {
|
|
|
|
aData.error = "NOT_UPDATABLE";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Checking only appcache for " + aData.manifestURL);
|
|
|
|
// Check if the appcache is updatable, and send "downloadavailable" or
|
|
|
|
// "downloadapplied".
|
|
|
|
let updateObserver = {
|
|
|
|
observe: function(aSubject, aTopic, aObsData) {
|
|
|
|
debug("appcache result: " + aTopic);
|
|
|
|
if (aData.event == "offline-cache-update-available") {
|
|
|
|
aData.event = "downloadavailable";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:OK", aData);
|
|
|
|
} else {
|
|
|
|
aData.error = "NOT_UPDATABLE";
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:KO", aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateSvc.checkForUpdate(Services.io.newURI(aData.manifestURL, null, null),
|
|
|
|
app.localId, false, updateObserver);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to download a new manifest.
|
2012-09-27 01:01:20 +00:00
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", aData.manifestURL, true);
|
2012-12-03 19:38:09 +00:00
|
|
|
xhr.responseType = "json";
|
2012-10-30 00:17:47 +00:00
|
|
|
if (app.etag) {
|
|
|
|
xhr.setRequestHeader("If-None-Match", app.etag);
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
2012-12-10 23:49:02 +00:00
|
|
|
xhr.channel.notificationCallbacks =
|
|
|
|
this.createLoadContext(app.installerAppId, app.installerIsBrowser);
|
2012-09-27 01:01:20 +00:00
|
|
|
|
|
|
|
xhr.addEventListener("load", (function() {
|
|
|
|
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;
|
|
|
|
}
|
2012-11-18 22:43:38 +00:00
|
|
|
if (!AppsUtils.checkManifest(manifest)) {
|
2012-09-27 01:01:20 +00:00
|
|
|
sendError("INVALID_MANIFEST");
|
2012-11-18 22:43:38 +00:00
|
|
|
} else if (!AppsUtils.checkInstallAllowed(manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
2012-09-27 01:01:20 +00:00
|
|
|
} else {
|
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
|
|
|
app.lastCheckedUpdate = Date.now();
|
2012-10-30 00:17:47 +00:00
|
|
|
if (app.origin.startsWith("app://")) {
|
2013-01-07 17:10:32 +00:00
|
|
|
updatePackagedApp.call(this, manifest);
|
2012-09-27 01:01:20 +00:00
|
|
|
} else {
|
2012-10-30 00:17:47 +00:00
|
|
|
updateHostedApp.call(this, manifest);
|
2012-09-27 01:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._saveApps();
|
|
|
|
} else if (xhr.status == 304) {
|
|
|
|
// The manifest has not changed. We just update lastCheckedUpdate.
|
|
|
|
app.lastCheckedUpdate = Date.now();
|
|
|
|
aData.event = "downloadapplied";
|
|
|
|
aData.app = {
|
|
|
|
lastCheckedUpdate: app.lastCheckedUpdate
|
|
|
|
}
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckForUpdate:Return:OK", aData);
|
|
|
|
this._saveApps();
|
|
|
|
} else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.addEventListener("error", (function() {
|
2012-10-30 00:17:47 +00:00
|
|
|
sendError("NETWORK_ERROR");
|
2012-09-27 01:01:20 +00:00
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.send(null);
|
|
|
|
},
|
|
|
|
|
2012-12-10 23:49:02 +00:00
|
|
|
// Creates a nsILoadContext object with a given appId and isBrowser flag.
|
|
|
|
createLoadContext: function createLoadContext(aAppId, aIsBrowser) {
|
|
|
|
return {
|
|
|
|
associatedWindow: null,
|
|
|
|
topWindow : null,
|
|
|
|
appId: aAppId,
|
|
|
|
isInBrowserElement: aIsBrowser,
|
|
|
|
usePrivateBrowsing: false,
|
|
|
|
isContent: false,
|
|
|
|
|
|
|
|
isAppOfType: function(appType) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsILoadContext,
|
|
|
|
Ci.nsIInterfaceRequestor,
|
|
|
|
Ci.nsISupports]),
|
|
|
|
getInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsILoadContext))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
let sendError = function sendError(aError) {
|
|
|
|
aData.error = aError;
|
|
|
|
aMm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
|
|
|
|
Cu.reportError("Error installing app from: " + app.installOrigin +
|
|
|
|
": " + aError);
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
// Hosted apps can't be trusted or certified, so just check that the
|
|
|
|
// manifest doesn't ask for those.
|
|
|
|
function checkAppStatus(aManifest) {
|
|
|
|
let manifestStatus = aManifest.type || "web";
|
|
|
|
return (Services.prefs.getBoolPref("dom.mozApps.dev_mode") ||
|
|
|
|
manifestStatus === "web");
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = aData.app;
|
|
|
|
|
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", app.manifestURL, true);
|
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.VALIDATE_ALWAYS;
|
2012-12-10 23:49:02 +00:00
|
|
|
xhr.channel.notificationCallbacks = this.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"))) {
|
|
|
|
sendError("INVALID_MANIFEST");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.manifest = xhr.response;
|
|
|
|
if (!app.manifest) {
|
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.checkManifest(app.manifest)) {
|
|
|
|
sendError("INVALID_MANIFEST");
|
|
|
|
} else if (!AppsUtils.checkInstallAllowed(app.manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
|
|
|
} else if (!checkAppStatus(app.manifest)) {
|
|
|
|
sendError("INVALID_SECURITY_LEVEL");
|
|
|
|
} else {
|
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
2013-01-04 18:21:31 +00:00
|
|
|
// 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));
|
|
|
|
}
|
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) {
|
|
|
|
let sendError = function sendError(aError) {
|
|
|
|
aData.error = aError;
|
|
|
|
aMm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
|
|
|
|
Cu.reportError("Error installing packaged app from: " +
|
|
|
|
app.installOrigin + ": " + aError);
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let app = aData.app;
|
|
|
|
|
|
|
|
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
|
|
|
xhr.open("GET", app.manifestURL, true);
|
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.VALIDATE_ALWAYS;
|
2012-12-10 23:49:02 +00:00
|
|
|
xhr.channel.notificationCallbacks = this.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"))) {
|
|
|
|
sendError("INVALID_MANIFEST");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifest = app.updateManifest = xhr.response;
|
|
|
|
if (!manifest) {
|
|
|
|
sendError("MANIFEST_PARSE_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(AppsUtils.checkManifest(manifest) &&
|
|
|
|
manifest.package_path)) {
|
|
|
|
sendError("INVALID_MANIFEST");
|
|
|
|
} else if (!AppsUtils.checkInstallAllowed(manifest, app.installOrigin)) {
|
|
|
|
sendError("INSTALL_FROM_DENIED");
|
|
|
|
} else {
|
|
|
|
app.etag = xhr.getResponseHeader("Etag");
|
|
|
|
Services.obs.notifyObservers(aMm, "webapps-ask-install",
|
|
|
|
JSON.stringify(aData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("MANIFEST_URL_ERROR");
|
|
|
|
}
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.addEventListener("error", (function() {
|
|
|
|
sendError("NETWORK_ERROR");
|
|
|
|
}).bind(this), false);
|
|
|
|
|
|
|
|
xhr.send(null);
|
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
denyInstall: function(aData) {
|
2012-07-11 15:38:33 +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-09-18 17:34:55 +00:00
|
|
|
aData.mm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
confirmInstall: function(aData, aFromSync, aProfileDir, aOfflineCacheObserver) {
|
2012-09-28 22:16:29 +00:00
|
|
|
let isReinstall = false;
|
2011-11-28 20:13:26 +00:00
|
|
|
let app = aData.app;
|
2012-08-29 21:20:03 +00:00
|
|
|
app.removable = true;
|
2012-09-27 19:34:41 +00:00
|
|
|
|
|
|
|
let origin = Services.io.newURI(app.origin, null, null);
|
|
|
|
let manifestURL = origin.resolve(app.manifestURL);
|
|
|
|
|
2012-09-27 21:37:39 +00:00
|
|
|
let id = app.syncId || this._appId(app.origin);
|
2012-09-27 19:34:41 +00:00
|
|
|
let localId = this.getAppLocalIdByManifestURL(manifestURL);
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-07-11 15:38:33 +00:00
|
|
|
// Installing an application again is considered as an update.
|
2011-11-28 20:13:26 +00:00
|
|
|
if (id) {
|
2012-09-28 22:16:29 +00:00
|
|
|
isReinstall = true;
|
2012-08-29 21:20:03 +00:00
|
|
|
let dir = this._getAppDir(id);
|
2011-11-28 20:13:26 +00:00
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch(e) {
|
|
|
|
}
|
2012-04-28 07:10:08 +00:00
|
|
|
} else {
|
|
|
|
id = this.makeAppId();
|
2012-07-09 10:25:41 +00:00
|
|
|
localId = this._nextLocalId();
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
2012-12-22 13:56:21 +00:00
|
|
|
app.id = id;
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
let manifestName = "manifest.webapp";
|
|
|
|
if (aData.isPackage) {
|
2012-07-11 15:38:33 +00:00
|
|
|
// Override the origin with the correct id.
|
|
|
|
app.origin = "app://" + id;
|
2012-09-27 19:34:41 +00:00
|
|
|
|
|
|
|
// For packaged apps, keep the update manifest distinct from the app
|
|
|
|
// manifest.
|
|
|
|
manifestName = "update.webapp";
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2012-08-28 02:43:57 +00:00
|
|
|
let appObject = AppsUtils.cloneAppObject(app);
|
2012-08-28 02:43:57 +00:00
|
|
|
appObject.appStatus = app.appStatus || Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
2012-10-31 21:02:37 +00:00
|
|
|
// For hosted apps, allow application status override in dev mode.
|
|
|
|
if (!aData.isPackage &&
|
|
|
|
Services.prefs.getBoolPref("dom.mozApps.dev_mode")) {
|
|
|
|
appObject.appStatus = AppsUtils.getAppManifestStatus(app.manifest);
|
|
|
|
}
|
|
|
|
|
2012-06-18 19:49:35 +00:00
|
|
|
appObject.installTime = app.installTime = Date.now();
|
2012-09-27 01:01:20 +00:00
|
|
|
appObject.lastUpdateCheck = app.lastUpdateCheck = Date.now();
|
2012-04-28 07:10:08 +00:00
|
|
|
let appNote = JSON.stringify(appObject);
|
|
|
|
appNote.id = id;
|
2011-12-06 04:22:01 +00:00
|
|
|
|
2012-12-22 13:56:21 +00:00
|
|
|
appObject.id = id;
|
2012-07-09 10:25:41 +00:00
|
|
|
appObject.localId = localId;
|
2012-09-06 00:37:41 +00:00
|
|
|
appObject.basePath = FileUtils.getDir(DIRECTORY_NAME, ["webapps"], true, true).path;
|
2012-04-28 07:10:08 +00:00
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
2012-11-20 20:50:48 +00:00
|
|
|
dir.permissions = FileUtils.PERMS_DIRECTORY;
|
2011-11-28 20:13:26 +00:00
|
|
|
let manFile = dir.clone();
|
2012-09-27 19:34:41 +00:00
|
|
|
manFile.append(manifestName);
|
|
|
|
let jsonManifest = aData.isPackage ? app.updateManifest : app.manifest;
|
|
|
|
this._writeFile(manFile, JSON.stringify(jsonManifest), function() { });
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-10-03 05:38:03 +00:00
|
|
|
let manifest = new ManifestHelper(jsonManifest, app.origin);
|
2012-09-27 01:01:20 +00:00
|
|
|
|
|
|
|
if (manifest.appcache_path) {
|
|
|
|
appObject.installState = "pending";
|
|
|
|
appObject.downloadAvailable = true;
|
|
|
|
appObject.downloading = true;
|
2012-09-27 19:34:41 +00:00
|
|
|
appObject.downloadSize = 0;
|
|
|
|
appObject.readyToApplyDownload = false;
|
|
|
|
} else if (manifest.package_path) {
|
|
|
|
appObject.installState = "pending";
|
|
|
|
appObject.downloadAvailable = true;
|
|
|
|
appObject.downloading = true;
|
|
|
|
appObject.downloadSize = manifest.size;
|
2012-09-27 01:01:20 +00:00
|
|
|
appObject.readyToApplyDownload = false;
|
|
|
|
} else {
|
|
|
|
appObject.installState = "installed";
|
|
|
|
appObject.downloadAvailable = false;
|
|
|
|
appObject.downloading = false;
|
|
|
|
appObject.readyToApplyDownload = false;
|
|
|
|
}
|
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
appObject.name = manifest.name;
|
2012-10-19 10:43:17 +00:00
|
|
|
appObject.csp = manifest.csp || "";
|
2012-09-26 23:49:43 +00:00
|
|
|
|
2012-12-10 23:49:02 +00:00
|
|
|
appObject.installerAppId = aData.appId;
|
|
|
|
appObject.installerIsBrowser = aData.isBrowser;
|
|
|
|
|
2012-09-27 01:01:20 +00:00
|
|
|
this.webapps[id] = appObject;
|
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) {
|
2012-10-31 21:02:37 +00:00
|
|
|
PermissionsInstaller.installPermissions({ origin: appObject.origin,
|
|
|
|
manifestURL: appObject.manifestURL,
|
|
|
|
manifest: jsonManifest },
|
|
|
|
isReinstall, (function() {
|
2012-10-10 21:29:07 +00:00
|
|
|
this.uninstall(aData, aData.mm);
|
|
|
|
}).bind(this));
|
|
|
|
}
|
2012-10-03 05:38:06 +00:00
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
["installState", "downloadAvailable",
|
|
|
|
"downloading", "downloadSize", "readyToApplyDownload"].forEach(function(aProp) {
|
|
|
|
aData.app[aProp] = appObject[aProp];
|
|
|
|
});
|
2012-06-11 18:41:46 +00:00
|
|
|
|
2011-12-08 13:32:54 +00:00
|
|
|
if (!aFromSync)
|
|
|
|
this._saveApps((function() {
|
2012-09-18 17:34:55 +00:00
|
|
|
this.broadcastMessage("Webapps:Install:Return:OK", aData);
|
2012-04-28 07:10:08 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-sync-install", appNote);
|
2012-09-18 17:34:55 +00:00
|
|
|
this.broadcastMessage("Webapps:AddApp", { id: id, app: appObject });
|
2011-12-08 13:32:54 +00:00
|
|
|
}).bind(this));
|
2012-06-11 18:41:46 +00:00
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
if (!aData.isPackage) {
|
2012-10-17 04:30:43 +00:00
|
|
|
this.notifyAppsRegistryStart();
|
|
|
|
#ifdef MOZ_SYS_MSG
|
2012-09-27 19:34:41 +00:00
|
|
|
this._registerSystemMessages(app.manifest, app);
|
2012-11-03 03:28:32 +00:00
|
|
|
this._registerActivities(app.manifest, app, true);
|
2012-10-17 04:30:43 +00:00
|
|
|
#else
|
|
|
|
// Nothing else to do but notifying we're ready.
|
|
|
|
this.notifyAppsRegistryReady();
|
2012-07-03 00:16:55 +00:00
|
|
|
#endif
|
2012-10-17 04:30:43 +00:00
|
|
|
}
|
2012-07-03 00:16:55 +00:00
|
|
|
|
2012-10-04 18:18:39 +00:00
|
|
|
this.startOfflineCacheDownload(manifest, appObject, aProfileDir, aOfflineCacheObserver);
|
2012-09-27 19:34:41 +00:00
|
|
|
if (manifest.package_path) {
|
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.
|
2012-10-03 05:38:03 +00:00
|
|
|
manifest = new ManifestHelper(jsonManifest, app.manifestURL);
|
2012-09-29 17:57:18 +00:00
|
|
|
this.downloadPackage(manifest, appObject, false, function(aId, aManifest) {
|
|
|
|
// Success! Move the zip out of TmpD.
|
|
|
|
let app = DOMApplicationRegistry.webapps[id];
|
|
|
|
let zipFile = FileUtils.getFile("TmpD", ["webapps", aId, "application.zip"], true);
|
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
|
|
|
|
zipFile.moveTo(dir, "application.zip");
|
2012-11-20 20:50:48 +00:00
|
|
|
zipFile.permissions = FileUtils.PERMS_FILE;
|
2012-09-29 17:57:18 +00:00
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", aId], true, true);
|
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
// Save the manifest
|
|
|
|
let manFile = dir.clone();
|
|
|
|
manFile.append("manifest.webapp");
|
|
|
|
DOMApplicationRegistry._writeFile(manFile,
|
|
|
|
JSON.stringify(aManifest),
|
|
|
|
function() { });
|
|
|
|
// Set state and fire events.
|
|
|
|
app.installState = "installed";
|
|
|
|
app.downloading = false;
|
2012-10-30 00:17:47 +00:00
|
|
|
app.downloadAvailable = false;
|
2012-09-29 17:57:18 +00:00
|
|
|
DOMApplicationRegistry._saveApps(function() {
|
2012-10-10 21:29:07 +00:00
|
|
|
// Update the permissions for this app.
|
|
|
|
PermissionsInstaller.installPermissions({ manifest: aManifest,
|
|
|
|
origin: appObject.origin,
|
|
|
|
manifestURL: appObject.manifestURL },
|
|
|
|
true);
|
2012-10-30 00:17:47 +00:00
|
|
|
debug("About to fire Webapps:PackageEvent 'installed'");
|
2012-09-29 17:57:18 +00:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "installed",
|
|
|
|
manifestURL: appObject.manifestURL,
|
|
|
|
app: app,
|
|
|
|
manifest: aManifest });
|
|
|
|
});
|
|
|
|
});
|
2012-09-27 19:34:41 +00:00
|
|
|
}
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +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;
|
|
|
|
Services.prefs.setIntPref("dom.mozApps.maxLocalId", id);
|
|
|
|
return id;
|
2012-07-09 10:25:41 +00:00
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
_appId: function(aURI) {
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].origin == aURI)
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
|
2011-12-08 13:32:54 +00:00
|
|
|
_saveApps: function(aCallback) {
|
2012-09-27 01:01:20 +00:00
|
|
|
this._writeFile(this.appsFile, JSON.stringify(this.webapps, null, 2), function() {
|
2011-12-08 13:32:54 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
/**
|
|
|
|
* Asynchronously reads a list of manifests
|
|
|
|
*/
|
2012-11-03 03:28:32 +00:00
|
|
|
|
|
|
|
_manifestCache: {},
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
_readManifests: function(aData, aFinalCallback, aIndex) {
|
2011-12-15 17:20:57 +00:00
|
|
|
if (!aData.length) {
|
|
|
|
aFinalCallback(aData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
let index = aIndex || 0;
|
|
|
|
let id = aData[index].id;
|
2012-05-22 21:38:34 +00:00
|
|
|
|
2012-11-03 03:28:32 +00:00
|
|
|
// Use the cached manifest instead of reading the file again from disk.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
aData[index].manifest = this._manifestCache[id];
|
|
|
|
if (index == aData.length - 1)
|
|
|
|
aFinalCallback(aData);
|
|
|
|
else
|
|
|
|
this._readManifests(aData, aFinalCallback, index + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-22 21:38:34 +00:00
|
|
|
// the manifest file used to be named manifest.json, so fallback on this.
|
2012-08-29 21:20:03 +00:00
|
|
|
let baseDir = (this.webapps[id].removable ? DIRECTORY_NAME : "coreAppsDir");
|
|
|
|
let file = FileUtils.getFile(baseDir, ["webapps", id, "manifest.webapp"], true);
|
2012-09-27 19:34:41 +00:00
|
|
|
if (!file.exists()) {
|
|
|
|
file = FileUtils.getFile(baseDir, ["webapps", id, "update.webapp"], true);
|
|
|
|
}
|
2012-05-22 21:38:34 +00:00
|
|
|
if (!file.exists()) {
|
2012-08-29 21:20:03 +00:00
|
|
|
file = FileUtils.getFile(baseDir, ["webapps", id, "manifest.json"], true);
|
2012-05-22 21:38:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
this._loadJSONAsync(file, (function(aJSON) {
|
2012-11-03 03:28:32 +00:00
|
|
|
aData[index].manifest = this._manifestCache[id] = aJSON;
|
2011-12-05 23:26:18 +00:00
|
|
|
if (index == aData.length - 1)
|
|
|
|
aFinalCallback(aData);
|
|
|
|
else
|
|
|
|
this._readManifests(aData, aFinalCallback, index + 1);
|
2012-04-28 07:10:08 +00:00
|
|
|
}).bind(this));
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2012-09-29 17:57:18 +00:00
|
|
|
downloadPackage: function(aManifest, aApp, aIsUpdate, aOnSuccess) {
|
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.
|
|
|
|
// If we fail at any step, we backout the previous ones and return an error.
|
|
|
|
|
2012-11-29 14:19:37 +00:00
|
|
|
debug("downloadPackage " + JSON.stringify(aApp));
|
2012-07-11 15:38:33 +00:00
|
|
|
|
2012-09-27 19:34:41 +00:00
|
|
|
let id = this._appIdForManifestURL(aApp.manifestURL);
|
|
|
|
let app = this.webapps[id];
|
2012-07-11 15:38:33 +00:00
|
|
|
|
2012-11-15 09:35:37 +00:00
|
|
|
let self = this;
|
2012-07-11 15:38:33 +00:00
|
|
|
// Removes the directory we created, and sends an error to the DOM side.
|
|
|
|
function cleanup(aError) {
|
2012-09-27 19:34:41 +00:00
|
|
|
debug("Cleanup: " + aError);
|
|
|
|
let dir = FileUtils.getDir("TmpD", ["webapps", id], true, true);
|
2012-07-11 15:38:33 +00:00
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) { }
|
2012-11-21 18:09:10 +00:00
|
|
|
|
|
|
|
// We avoid notifying the error to the DOM side if the app download
|
|
|
|
// was cancelled via cancelDownload, which already sends its own
|
|
|
|
// notification.
|
|
|
|
if (!app.downloading && !app.downloadAvailable && !app.downloadSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-21 18:28:55 +00:00
|
|
|
let download = AppDownloadManager.get(aApp.manifestURL);
|
2012-11-29 14:19:37 +00:00
|
|
|
app.downloading = false;
|
2012-12-07 18:13:27 +00:00
|
|
|
// If there were not enough storage to download the packaged app we
|
|
|
|
// won't have a record of the download details, so we just set the
|
|
|
|
// installState to 'pending'.
|
|
|
|
app.installState = download ? download.previousState : "pending";
|
2012-11-21 18:09:10 +00:00
|
|
|
self.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "error",
|
|
|
|
manifestURL: aApp.manifestURL,
|
2012-11-29 14:19:37 +00:00
|
|
|
error: aError,
|
|
|
|
app: app });
|
2012-12-21 18:28:58 +00:00
|
|
|
self._saveApps();
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.remove(aApp.manifestURL);
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 09:35:37 +00:00
|
|
|
function download() {
|
|
|
|
debug("About to download " + aManifest.fullPackagePath());
|
|
|
|
|
|
|
|
let requestChannel = NetUtil.newChannel(aManifest.fullPackagePath())
|
|
|
|
.QueryInterface(Ci.nsIHttpChannel);
|
2012-12-21 18:28:58 +00:00
|
|
|
if (app.packageEtag) {
|
2012-12-31 08:29:31 +00:00
|
|
|
debug('Add If-None-Match header: ' + app.packageEtag);
|
|
|
|
requestChannel.setRequestHeader("If-None-Match", app.packageEtag, false);
|
2012-12-21 18:28:58 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.add(aApp.manifestURL,
|
|
|
|
{
|
|
|
|
channel: requestChannel,
|
|
|
|
appId: id,
|
|
|
|
previousState: aIsUpdate ? "installed" : "pending"
|
|
|
|
}
|
|
|
|
);
|
2012-12-13 17:54:49 +00:00
|
|
|
|
|
|
|
let lastProgressTime = 0;
|
2012-11-15 09:35:37 +00:00
|
|
|
requestChannel.notificationCallbacks = {
|
|
|
|
QueryInterface: function notifQI(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsISupports) ||
|
2012-12-10 23:49:02 +00:00
|
|
|
aIID.equals(Ci.nsIProgressEventSink) ||
|
|
|
|
aIID.equals(Ci.nsILoadContext))
|
2012-11-15 09:35:37 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
getInterface: function notifGI(aIID) {
|
|
|
|
return this.QueryInterface(aIID);
|
|
|
|
},
|
|
|
|
onProgress: function notifProgress(aRequest, aContext,
|
|
|
|
aProgress, aProgressMax) {
|
|
|
|
app.progress = aProgress;
|
2012-12-13 17:54:49 +00:00
|
|
|
let now = Date.now();
|
|
|
|
if (now - lastProgressTime > MIN_PROGRESS_EVENT_DELAY) {
|
2012-12-14 19:29:25 +00:00
|
|
|
debug("onProgress: " + aProgress + "/" + aProgressMax);
|
2012-12-13 17:54:49 +00:00
|
|
|
self.broadcastMessage("Webapps:PackageEvent",
|
|
|
|
{ type: "progress",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
app: app });
|
|
|
|
lastProgressTime = now;
|
|
|
|
}
|
2012-11-15 09:35:37 +00:00
|
|
|
},
|
2012-12-10 23:49:02 +00:00
|
|
|
onStatus: function notifStatus(aRequest, aContext, aStatus, aStatusArg) { },
|
|
|
|
|
|
|
|
// nsILoadContext
|
|
|
|
appId: app.installerAppId,
|
|
|
|
isInBrowserElement: app.installerIsBrowser,
|
|
|
|
usePrivateBrowsing: false,
|
|
|
|
isContent: false,
|
|
|
|
associatedWindow: null,
|
|
|
|
topWindow : null,
|
|
|
|
isAppOfType: function(appType) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
2012-11-29 14:19:37 +00:00
|
|
|
|
|
|
|
// We set the 'downloading' flag to true right before starting the fetch.
|
|
|
|
app.downloading = true;
|
|
|
|
// We determine the app's 'installState' according to its previous
|
|
|
|
// state. Cancelled download should remain as 'pending'. Successfully
|
|
|
|
// installed apps should morph to 'updating'.
|
|
|
|
app.installState = aIsUpdate ? "updating" : "pending";
|
|
|
|
|
2012-12-14 19:29:25 +00:00
|
|
|
// Staging the zip in TmpD until all the checks are done.
|
|
|
|
let zipFile = FileUtils.getFile("TmpD",
|
|
|
|
["webapps", id, "application.zip"], true);
|
|
|
|
|
|
|
|
// We need an output stream to write the channel content to the zip file.
|
|
|
|
let outputStream = Cc["@mozilla.org/network/file-output-stream;1"]
|
|
|
|
.createInstance(Ci.nsIFileOutputStream);
|
|
|
|
// write, create, truncate
|
|
|
|
outputStream.init(zipFile, 0x02 | 0x08 | 0x20, parseInt("0664", 8), 0);
|
|
|
|
let bufferedOutputStream = Cc['@mozilla.org/network/buffered-output-stream;1']
|
|
|
|
.createInstance(Ci.nsIBufferedOutputStream);
|
|
|
|
bufferedOutputStream.init(outputStream, 1024);
|
|
|
|
|
|
|
|
// Create a listener that will give data to the file output stream.
|
|
|
|
let listener = Cc["@mozilla.org/network/simple-stream-listener;1"]
|
|
|
|
.createInstance(Ci.nsISimpleStreamListener);
|
|
|
|
listener.init(bufferedOutputStream, {
|
2012-12-31 08:29:31 +00:00
|
|
|
onStartRequest: function(aRequest, aContext) {
|
|
|
|
// early check for ETag header
|
|
|
|
try {
|
|
|
|
requestChannel.getResponseHeader("Etag");
|
|
|
|
} catch (e) {
|
|
|
|
// in https://bugzilla.mozilla.org/show_bug.cgi?id=825218
|
|
|
|
// we might do something cleaner to have a proper user error
|
|
|
|
debug("We found no ETag Header, canceling the request");
|
|
|
|
requestChannel.cancel(Cr.NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
},
|
2012-12-14 19:29:25 +00:00
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
|
|
|
debug("onStopRequest " + aStatusCode);
|
|
|
|
bufferedOutputStream.close();
|
|
|
|
outputStream.close();
|
|
|
|
|
2012-12-21 18:28:58 +00:00
|
|
|
if (requestChannel.responseStatus == 304) {
|
|
|
|
// The package's Etag has not changed.
|
|
|
|
// We send a "applied" event right away.
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
app.downloadSize = 0;
|
|
|
|
app.installState = "installed";
|
|
|
|
app.readyToApplyDownload = false;
|
|
|
|
self.broadcastMessage("Webapps:PackageEvent", {
|
|
|
|
type: "applied",
|
|
|
|
manifestURL: aApp.manifestURL,
|
|
|
|
app: app });
|
|
|
|
// Save the updated registry, and cleanup the tmp directory.
|
|
|
|
self._saveApps();
|
|
|
|
let file = FileUtils.getFile("TmpD", ["webapps", id], false);
|
|
|
|
if (file && file.exists()) {
|
|
|
|
file.remove(true);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-21 18:28:55 +00:00
|
|
|
if (!Components.isSuccessCode(aStatusCode)) {
|
|
|
|
cleanup("NETWORK_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-14 19:29:25 +00:00
|
|
|
let certdb;
|
|
|
|
try {
|
|
|
|
certdb = Cc["@mozilla.org/security/x509certdb;1"]
|
|
|
|
.getService(Ci.nsIX509CertDB);
|
|
|
|
} catch (e) {
|
|
|
|
cleanup("CERTDB_ERROR");
|
2012-11-15 09:35:37 +00:00
|
|
|
return;
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 04:35:58 +00:00
|
|
|
certdb.openSignedJARFileAsync(zipFile, function(aRv, aZipReader) {
|
2012-12-14 19:29:25 +00:00
|
|
|
let zipReader;
|
2012-12-14 04:35:58 +00:00
|
|
|
try {
|
|
|
|
let isSigned;
|
|
|
|
if (Components.isSuccessCode(aRv)) {
|
|
|
|
isSigned = true;
|
|
|
|
zipReader = aZipReader;
|
|
|
|
} else if (aRv != Cr.NS_ERROR_SIGNED_JAR_NOT_SIGNED) {
|
|
|
|
throw "INVALID_SIGNATURE";
|
|
|
|
} else {
|
|
|
|
isSigned = false;
|
|
|
|
zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
|
|
|
|
.createInstance(Ci.nsIZipReader);
|
|
|
|
zipReader.open(zipFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!zipReader.hasEntry("manifest.webapp")) {
|
|
|
|
throw "MISSING_MANIFEST";
|
|
|
|
}
|
|
|
|
|
|
|
|
let istream = zipReader.getInputStream("manifest.webapp");
|
|
|
|
|
|
|
|
// 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 manifest = JSON.parse(converter.ConvertToUnicode(NetUtil.readInputStreamToString(istream,
|
|
|
|
istream.available()) || ""));
|
|
|
|
|
2012-12-19 19:37:13 +00:00
|
|
|
if (!AppsUtils.compareManifests(manifest,
|
|
|
|
aManifest._manifest)) {
|
|
|
|
throw "MANIFEST_MISMATCH";
|
|
|
|
}
|
|
|
|
|
2012-12-14 04:35:58 +00:00
|
|
|
if (!AppsUtils.checkManifest(manifest)) {
|
|
|
|
throw "INVALID_MANIFEST";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppsUtils.checkInstallAllowed(manifest, aApp.installOrigin)) {
|
|
|
|
throw "INSTALL_FROM_DENIED";
|
|
|
|
}
|
|
|
|
|
|
|
|
let isDevMode = Services.prefs.getBoolPref("dom.mozApps.dev_mode");
|
|
|
|
let maxStatus = isDevMode ? Ci.nsIPrincipal.APP_STATUS_CERTIFIED
|
|
|
|
: isSigned ? Ci.nsIPrincipal.APP_STATUS_PRIVILEGED
|
|
|
|
: Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
|
2012-12-20 18:52:33 +00:00
|
|
|
if (AppsUtils.getAppManifestStatus(manifest) > maxStatus) {
|
2012-12-14 04:35:58 +00:00
|
|
|
throw "INVALID_SECURITY_LEVEL";
|
|
|
|
}
|
2012-12-20 18:52:33 +00:00
|
|
|
aApp.appStatus = AppsUtils.getAppManifestStatus(manifest);
|
2012-12-31 08:32:14 +00:00
|
|
|
// Save the new Etag for the package.
|
|
|
|
try {
|
|
|
|
app.packageEtag = requestChannel.getResponseHeader("Etag");
|
|
|
|
debug("Package etag=" + app.packageEtag);
|
|
|
|
} catch (e) {
|
|
|
|
// in https://bugzilla.mozilla.org/show_bug.cgi?id=825218
|
|
|
|
// we'll fail gracefully in this case
|
|
|
|
// for now, just going on
|
|
|
|
app.packageEtag = null;
|
|
|
|
debug("Can't find an etag, this should not happen");
|
|
|
|
}
|
2012-12-14 04:35:58 +00:00
|
|
|
|
|
|
|
if (aOnSuccess) {
|
|
|
|
aOnSuccess(id, manifest);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Something bad happened when reading the package.
|
|
|
|
if (typeof e == 'object') {
|
2012-12-14 19:29:25 +00:00
|
|
|
debug(e);
|
2012-12-14 04:35:58 +00:00
|
|
|
cleanup("INVALID_PACKAGE");
|
|
|
|
} else {
|
|
|
|
cleanup(e);
|
|
|
|
}
|
|
|
|
} finally {
|
2012-12-21 18:28:55 +00:00
|
|
|
AppDownloadManager.remove(aApp.manifestURL);
|
|
|
|
if (zipReader)
|
|
|
|
zipReader.close();
|
2012-11-15 09:35:37 +00:00
|
|
|
}
|
2012-12-14 04:35:58 +00:00
|
|
|
});
|
2012-12-14 19:29:25 +00:00
|
|
|
}
|
2012-07-11 15:38:33 +00:00
|
|
|
});
|
2012-12-14 19:29:25 +00:00
|
|
|
|
|
|
|
requestChannel.asyncOpen(listener, null);
|
2012-11-15 09:35:37 +00:00
|
|
|
};
|
|
|
|
|
2012-11-16 20:16:46 +00:00
|
|
|
let deviceStorage = Services.wm.getMostRecentWindow("navigator:browser")
|
|
|
|
.navigator.getDeviceStorage("apps");
|
2012-11-15 09:35:37 +00:00
|
|
|
let req = deviceStorage.stat();
|
|
|
|
req.onsuccess = req.onerror = function statResult(e) {
|
|
|
|
// Even if we could not retrieve the device storage free space, we try
|
|
|
|
// to download the package.
|
|
|
|
if (!e.target.result) {
|
|
|
|
download();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let freeBytes = e.target.result.freeBytes;
|
|
|
|
if (freeBytes) {
|
|
|
|
debug("Free storage: " + freeBytes + ". Download size: " +
|
|
|
|
aApp.downloadSize);
|
2012-12-21 18:28:55 +00:00
|
|
|
if (freeBytes <=
|
|
|
|
aApp.downloadSize + AppDownloadManager.MIN_REMAINING_FREESPACE) {
|
2012-11-15 09:35:37 +00:00
|
|
|
cleanup("INSUFFICIENT_STORAGE");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
download();
|
|
|
|
}
|
2012-07-11 15:38:33 +00:00
|
|
|
},
|
|
|
|
|
2012-09-28 16:24:45 +00:00
|
|
|
uninstall: function(aData, aMm) {
|
2011-11-28 20:13:26 +00:00
|
|
|
for (let id in this.webapps) {
|
|
|
|
let app = this.webapps[id];
|
2012-07-20 15:41:30 +00:00
|
|
|
if (app.origin != aData.origin) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2012-12-03 22:46:57 +00:00
|
|
|
if (!app.removable)
|
2012-08-29 21:20:03 +00:00
|
|
|
return;
|
|
|
|
|
2012-11-03 03:28:32 +00:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[id];
|
|
|
|
}
|
|
|
|
|
2012-09-28 16:24:45 +00:00
|
|
|
// Clear private data first.
|
|
|
|
this._clearPrivateData(app.localId, false);
|
|
|
|
|
|
|
|
// Then notify observers.
|
|
|
|
Services.obs.notifyObservers(aMm, "webapps-uninstall", JSON.stringify(aData));
|
|
|
|
|
2012-08-28 02:43:57 +00:00
|
|
|
let appNote = JSON.stringify(AppsUtils.cloneAppObject(app));
|
2012-07-20 15:41:30 +00:00
|
|
|
appNote.id = id;
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2012-07-20 15:41:30 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
2012-09-27 01:01:20 +00:00
|
|
|
this._readManifests([{ id: id }], (function unregisterManifest(aResult) {
|
2012-07-20 15:41:30 +00:00
|
|
|
this._unregisterActivities(aResult[0].manifest, app);
|
|
|
|
}).bind(this));
|
2012-09-27 01:01:20 +00:00
|
|
|
#endif
|
2012-07-20 15:41:30 +00:00
|
|
|
|
2012-08-29 21:20:03 +00:00
|
|
|
let dir = this._getAppDir(id);
|
2012-07-20 15:41:30 +00:00
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
delete this.webapps[id];
|
|
|
|
|
|
|
|
this._saveApps((function() {
|
2012-12-03 22:46:57 +00:00
|
|
|
aData.manifestURL = app.manifestURL;
|
2012-11-29 03:14:04 +00:00
|
|
|
this.broadcastMessage("Webapps:Uninstall:Return:OK", aData);
|
2012-07-20 15:41:30 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-sync-uninstall", appNote);
|
2012-09-18 17:34:55 +00:00
|
|
|
this.broadcastMessage("Webapps:RemoveApp", { id: id });
|
2012-07-20 15:41:30 +00:00
|
|
|
}).bind(this));
|
|
|
|
|
2012-09-28 16:24:45 +00:00
|
|
|
return;
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
2012-09-28 16:24:45 +00:00
|
|
|
|
|
|
|
aMm.sendAsyncMessage("Webapps:Uninstall:Return:KO", aData);
|
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) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
|
|
|
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 &&
|
|
|
|
this._isLaunchable(this.webapps[id].origin)) {
|
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
|
|
|
aData.apps.push(app);
|
|
|
|
tmp.push({ id: id });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aData.apps.length) {
|
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
|
|
|
return;
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:50:58 +00:00
|
|
|
this._readManifests(tmp, (function(aResult) {
|
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 17:34:55 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
2012-03-06 19:50:58 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
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) {
|
|
|
|
if (this.webapps[appId].manifestURL == aData.manifestURL) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 20:38:51 +00:00
|
|
|
this._readManifests(tmp, (function(aResult) {
|
|
|
|
for (let i = 0; i < aResult.length; i++) {
|
|
|
|
aData.app.manifest = aResult[i].manifest;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
aMm.sendAsyncMessage("Webapps:CheckInstalled:Return:OK", aData);
|
|
|
|
}).bind(this));
|
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 &&
|
2012-07-03 03:45:00 +00:00
|
|
|
this._isLaunchable(this.webapps[id].origin)) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
this._readManifests(tmp, (function(aResult) {
|
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 17:34:55 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetInstalled:Return:OK", aData);
|
2011-12-05 23:26:18 +00:00
|
|
|
}).bind(this));
|
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) {
|
2012-08-20 17:19:56 +00:00
|
|
|
if (!this._isLaunchable(this.webapps[id].origin)) {
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._readManifests(tmp, (function(aResult) {
|
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 17:34:55 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetNotInstalled:Return:OK", aData);
|
2012-06-29 20:46:21 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2012-09-18 17:34:55 +00:00
|
|
|
getAll: function(aData, aMm) {
|
2011-11-28 20:13:26 +00:00
|
|
|
aData.apps = [];
|
2011-12-05 23:26:18 +00:00
|
|
|
let tmp = [];
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-04-28 07:10:08 +00:00
|
|
|
for (let id in this.webapps) {
|
2012-08-28 02:43:57 +00:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
2012-07-03 03:45:00 +00:00
|
|
|
if (!this._isLaunchable(app.origin))
|
2012-06-29 20:46:21 +00:00
|
|
|
continue;
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
aData.apps.push(app);
|
2011-12-05 23:26:18 +00:00
|
|
|
tmp.push({ id: id });
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
this._readManifests(tmp, (function(aResult) {
|
|
|
|
for (let i = 0; i < aResult.length; i++)
|
|
|
|
aData.apps[i].manifest = aResult[i].manifest;
|
2012-09-18 17:34:55 +00:00
|
|
|
aMm.sendAsyncMessage("Webapps:GetAll:Return:OK", aData);
|
2011-12-05 23:26:18 +00:00
|
|
|
}).bind(this));
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
getManifestFor: function(aOrigin, aCallback) {
|
|
|
|
if (!aCallback)
|
|
|
|
return;
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
let id = this._appId(aOrigin);
|
2012-11-30 16:57:45 +00:00
|
|
|
let app = this.webapps[id];
|
|
|
|
if (!id || (app.installState == "pending" && !app.retryingDownload)) {
|
2011-12-05 23:26:18 +00:00
|
|
|
aCallback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._readManifests([{ id: id }], function(aResult) {
|
|
|
|
aCallback(aResult[0].manifest);
|
|
|
|
});
|
2011-12-08 13:32:54 +00:00
|
|
|
},
|
|
|
|
|
2012-04-28 07:10:08 +00:00
|
|
|
/** Added to support AITC and classic sync */
|
|
|
|
itemExists: function(aId) {
|
|
|
|
return !!this.webapps[aId];
|
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
|
|
|
getAppById: function(aId) {
|
|
|
|
if (!this.webapps[aId])
|
|
|
|
return null;
|
2012-07-14 02:52:30 +00:00
|
|
|
|
2012-08-28 02:43:57 +00:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[aId]);
|
2011-12-08 13:32:54 +00:00
|
|
|
return app;
|
|
|
|
},
|
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
|
|
|
|
2012-10-19 10:43:17 +00:00
|
|
|
getCSPByLocalId: function(aLocalId) {
|
|
|
|
debug("getCSPByLocalId:" + aLocalId);
|
|
|
|
return AppsUtils.getCSPByLocalId(this.webapps, aLocalId);
|
|
|
|
},
|
|
|
|
|
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-08-31 14:34:28 +00:00
|
|
|
getAppFromObserverMessage: function(aMessage) {
|
|
|
|
return AppsUtils.getAppFromObserverMessage(this.webapps, aMessage);
|
|
|
|
},
|
|
|
|
|
2012-12-22 13:56:21 +00:00
|
|
|
getCoreAppsBasePath: function() {
|
|
|
|
return FileUtils.getDir("coreAppsDir", ["webapps"], false).path;
|
|
|
|
},
|
|
|
|
|
|
|
|
getWebAppsBasePath: function getWebAppsBasePath() {
|
|
|
|
return FileUtils.getDir(DIRECTORY_NAME, ["webapps"], false).path;
|
|
|
|
},
|
|
|
|
|
2012-04-28 07:10:08 +00:00
|
|
|
getAllWithoutManifests: function(aCallback) {
|
|
|
|
let result = {};
|
|
|
|
for (let id in this.webapps) {
|
2012-08-28 02:43:57 +00:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
2012-04-28 07:10:08 +00:00
|
|
|
result[id] = app;
|
|
|
|
}
|
|
|
|
aCallback(result);
|
2011-12-08 13:32:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateApps: function(aRecords, aCallback) {
|
|
|
|
for (let i = 0; i < aRecords.length; i++) {
|
|
|
|
let record = aRecords[i];
|
2012-06-23 17:22:53 +00:00
|
|
|
if (record.hidden) {
|
2012-08-29 21:20:03 +00:00
|
|
|
if (!this.webapps[record.id] || !this.webapps[record.id].removable)
|
2011-12-08 13:32:54 +00:00
|
|
|
continue;
|
2012-09-28 22:16:29 +00:00
|
|
|
|
2012-11-03 03:28:32 +00:00
|
|
|
// Clean up the deprecated manifest cache if needed.
|
|
|
|
if (record.id in this._manifestCache) {
|
|
|
|
delete this._manifestCache[record.id];
|
|
|
|
}
|
|
|
|
|
2011-12-08 13:32:54 +00:00
|
|
|
let origin = this.webapps[record.id].origin;
|
2012-12-03 22:46:57 +00:00
|
|
|
let manifestURL = this.webapps[record.id].manifestURL;
|
2011-12-08 13:32:54 +00:00
|
|
|
delete this.webapps[record.id];
|
2012-08-29 21:20:03 +00:00
|
|
|
let dir = this._getAppDir(record.id);
|
2011-12-08 13:32:54 +00:00
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2012-12-03 22:46:57 +00:00
|
|
|
this.broadcastMessage("Webapps:Uninstall:Return:OK", { origin: origin,
|
|
|
|
manifestURL: manifestURL });
|
2011-12-08 13:32:54 +00:00
|
|
|
} else {
|
2012-04-28 07:10:08 +00:00
|
|
|
if (this.webapps[record.id]) {
|
2011-12-08 13:32:54 +00:00
|
|
|
this.webapps[record.id] = record.value;
|
|
|
|
delete this.webapps[record.id].manifest;
|
2012-04-28 07:10:08 +00:00
|
|
|
} else {
|
2011-12-08 13:32:54 +00:00
|
|
|
let data = { app: record.value };
|
|
|
|
this.confirmInstall(data, true);
|
2012-09-18 17:34:55 +00:00
|
|
|
this.broadcastMessage("Webapps:Install:Return:OK", data);
|
2011-12-08 13:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._saveApps(aCallback);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllIDs: function() {
|
|
|
|
let apps = {};
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
// only sync http and https apps
|
|
|
|
if (this.webapps[id].origin.indexOf("http") == 0)
|
|
|
|
apps[id] = true;
|
|
|
|
}
|
|
|
|
return apps;
|
|
|
|
},
|
|
|
|
|
|
|
|
wipe: function(aCallback) {
|
|
|
|
let ids = this.getAllIDs();
|
|
|
|
for (let id in ids) {
|
2012-08-29 21:20:03 +00:00
|
|
|
if (!this.webapps[id].removable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-08 13:32:54 +00:00
|
|
|
delete this.webapps[id];
|
2012-08-29 21:20:03 +00:00
|
|
|
let dir = this._getAppDir(id);
|
2011-12-08 13:32:54 +00:00
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 03:28:32 +00:00
|
|
|
|
|
|
|
// Clear the manifest cache.
|
|
|
|
this._manifestCache = { };
|
|
|
|
|
2011-12-08 13:32:54 +00:00
|
|
|
this._saveApps(aCallback);
|
2012-06-29 20:46:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_isLaunchable: function(aOrigin) {
|
|
|
|
if (this.allAppsLaunchable)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
let uninstallKey = Cc["@mozilla.org/windows-registry-key;1"]
|
|
|
|
.createInstance(Ci.nsIWindowsRegKey);
|
|
|
|
try {
|
|
|
|
uninstallKey.open(uninstallKey.ROOT_KEY_CURRENT_USER,
|
|
|
|
"SOFTWARE\\Microsoft\\Windows\\" +
|
|
|
|
"CurrentVersion\\Uninstall\\" +
|
|
|
|
aOrigin,
|
|
|
|
uninstallKey.ACCESS_READ);
|
|
|
|
uninstallKey.close();
|
|
|
|
return true;
|
|
|
|
} catch (ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#elifdef XP_MACOSX
|
|
|
|
let mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"]
|
|
|
|
.createInstance(Ci.nsIMacWebAppUtils);
|
|
|
|
|
|
|
|
return !!mwaUtils.pathForAppWithIdentifier(aOrigin);
|
|
|
|
#elifdef XP_UNIX
|
|
|
|
let env = Cc["@mozilla.org/process/environment;1"]
|
|
|
|
.getService(Ci.nsIEnvironment);
|
|
|
|
let xdg_data_home_env = env.get("XDG_DATA_HOME");
|
|
|
|
|
|
|
|
let desktopINI;
|
|
|
|
if (xdg_data_home_env != "") {
|
|
|
|
desktopINI = Cc["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Ci.nsIFile);
|
|
|
|
desktopINI.initWithPath(xdg_data_home_env);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
desktopINI = Services.dirsvc.get("Home", Ci.nsIFile);
|
|
|
|
desktopINI.append(".local");
|
|
|
|
desktopINI.append("share");
|
|
|
|
}
|
|
|
|
desktopINI.append("applications");
|
|
|
|
|
|
|
|
let origin = Services.io.newURI(aOrigin, null, null);
|
|
|
|
let uniqueName = origin.scheme + ";" +
|
|
|
|
origin.host +
|
|
|
|
(origin.port != -1 ? ";" + origin.port : "");
|
|
|
|
|
|
|
|
desktopINI.append("owa-" + uniqueName + ".desktop");
|
|
|
|
|
|
|
|
return desktopINI.exists();
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
2012-09-26 22:03:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_notifyCategoryAndObservers: function(subject, topic, data) {
|
|
|
|
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) { }
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
registerBrowserElementParentForApp: function(bep, appId) {
|
|
|
|
let mm = bep._mm;
|
|
|
|
|
|
|
|
// Make a listener function that holds on to this appId.
|
|
|
|
let listener = this.receiveAppMessage.bind(this, appId);
|
|
|
|
|
|
|
|
this.frameMessages.forEach(function(msgName) {
|
|
|
|
mm.addMessageListener(msgName, listener);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveAppMessage: function(appId, message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "Webapps:ClearBrowserData":
|
2012-09-28 16:24:45 +00:00
|
|
|
this._clearPrivateData(appId, true);
|
2012-09-26 22:03:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-09-28 16:24:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_clearPrivateData: function(appId, browserOnly) {
|
|
|
|
let subject = {
|
|
|
|
appId: appId,
|
|
|
|
browserOnly: browserOnly,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams])
|
|
|
|
};
|
|
|
|
this._notifyCategoryAndObservers(subject, "webapps-clear-data", null);
|
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
|
|
|
|
*/
|
2012-07-03 00:16:55 +00:00
|
|
|
let 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;
|
2012-06-11 18:41:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AppcacheObserver.prototype = {
|
|
|
|
// nsIOfflineCacheUpdateObserver implementation
|
|
|
|
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);
|
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
let setStatus = function appObs_setStatus(aStatus) {
|
2012-11-27 04:03:43 +00:00
|
|
|
debug("Offlinecache setStatus to " + aStatus + " for " + app.origin);
|
2012-09-27 01:01:20 +00:00
|
|
|
mustSave = (app.installState != aStatus);
|
|
|
|
app.installState = aStatus;
|
2012-11-29 14:19:37 +00:00
|
|
|
if (aStatus == "installed") {
|
|
|
|
app.downloading = false;
|
|
|
|
app.downloadAvailable = false;
|
|
|
|
}
|
2012-09-27 01:01:20 +00:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:OfflineCache",
|
|
|
|
{ manifest: app.manifestURL,
|
|
|
|
installState: app.installState });
|
|
|
|
}
|
|
|
|
|
|
|
|
let setError = function appObs_setError(aError) {
|
2012-11-27 04:03:43 +00:00
|
|
|
debug("Offlinecache setError to " + aError);
|
2012-09-18 17:34:55 +00:00
|
|
|
DOMApplicationRegistry.broadcastMessage("Webapps:OfflineCache",
|
|
|
|
{ manifest: app.manifestURL,
|
2012-09-27 01:01:20 +00:00
|
|
|
error: aError });
|
2012-10-04 18:18:39 +00:00
|
|
|
app.downloading = false;
|
2012-11-29 14:19:37 +00:00
|
|
|
app.downloadAvailable = false;
|
2012-10-04 18:18:39 +00:00
|
|
|
mustSave = true;
|
2012-06-11 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (aState) {
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ERROR:
|
|
|
|
aUpdate.removeObserver(this);
|
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);
|
2012-09-27 01:01:20 +00:00
|
|
|
setStatus("installed");
|
2012-06-11 18:41:46 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_DOWNLOADING:
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMSTARTED:
|
2012-09-27 01:01:20 +00:00
|
|
|
setStatus(this.startStatus);
|
2012-06-11 18:41:46 +00:00
|
|
|
break;
|
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) {
|
|
|
|
setStatus(this.startStatus);
|
|
|
|
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();
|