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
|
|
|
|
|
|
|
let EXPORTED_SYMBOLS = ["DOMApplicationRegistry", "DOMApplicationManifest"];
|
|
|
|
|
|
|
|
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");
|
2011-11-28 20:13:26 +00:00
|
|
|
|
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-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
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
let DOMApplicationRegistry = {
|
|
|
|
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-08-28 02:43:57 +00:00
|
|
|
"Webapps:GetSelf",
|
|
|
|
"Webapps:GetInstalled", "Webapps:GetNotInstalled",
|
|
|
|
"Webapps:Launch", "Webapps:GetAll",
|
|
|
|
"Webapps:InstallPackage", "Webapps:GetBasePath",
|
|
|
|
"Webapps:GetList"];
|
2011-11-28 20:13:26 +00:00
|
|
|
|
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-03-13 00:33:10 +00:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
|
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-08-29 21:20:03 +00:00
|
|
|
let dirList = [DIRECTORY_NAME];
|
2012-08-29 22:58:31 +00:00
|
|
|
|
2012-08-29 21:20:03 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
dirList.push("coreAppsDir");
|
|
|
|
#endif
|
|
|
|
let currentId = 1;
|
2012-09-12 01:17:01 +00:00
|
|
|
this.dirsToLoad = dirList.length;
|
|
|
|
this.dirsLoaded = 0;
|
2012-08-29 21:20:03 +00:00
|
|
|
dirList.forEach((function(dir) {
|
2012-09-03 23:33:39 +00:00
|
|
|
let curFile;
|
|
|
|
try {
|
|
|
|
// getFile calls getDir with |shouldCreate = true|, so we have
|
|
|
|
// to wrap in a try..catch in case the file does not exist on a
|
|
|
|
// read-only partition.
|
|
|
|
curFile = FileUtils.getFile(dir, ["webapps", "webapps.json"], false);
|
|
|
|
} catch(e) { }
|
|
|
|
if (curFile && curFile.exists()) {
|
|
|
|
let appDir = FileUtils.getDir(dir, ["webapps"], false);
|
2012-08-29 21:20:03 +00:00
|
|
|
this._loadJSONAsync(curFile, (function(aData) {
|
|
|
|
if (!aData) {
|
|
|
|
return;
|
2012-08-29 22:58:31 +00:00
|
|
|
}
|
2012-09-12 01:17:01 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
|
|
|
let ids = [];
|
|
|
|
#endif
|
2012-08-29 21:20:03 +00:00
|
|
|
// Add new apps to the merged list.
|
|
|
|
for (let id in aData) {
|
|
|
|
this.webapps[id] = aData[id];
|
|
|
|
this.webapps[id].basePath = appDir.path;
|
|
|
|
this.webapps[id].removable = (dir == DIRECTORY_NAME);
|
|
|
|
#ifdef MOZ_SYS_MSG
|
2012-09-12 01:17:01 +00:00
|
|
|
ids.push({ id: id });
|
2012-08-29 21:20:03 +00:00
|
|
|
#endif
|
|
|
|
// local ids must be stable between restarts.
|
|
|
|
// We partition the ids in two buckets:
|
|
|
|
// - 1 to 1000 for the core apps.
|
|
|
|
// - 1001 to Inf for installed apps.
|
|
|
|
// This way, a gecko update with new core apps will not lead to
|
|
|
|
// changes for installed apps ids.
|
|
|
|
if (!this.webapps[id].removable) {
|
|
|
|
this.webapps[id].localId = currentId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to a non privileged status.
|
|
|
|
if (this.webapps[id].appStatus === undefined) {
|
|
|
|
this.webapps[id].appStatus = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 01:17:01 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
|
|
|
this._processManifestForIds(ids);
|
|
|
|
#endif
|
2012-08-29 21:20:03 +00:00
|
|
|
}).bind(this));
|
2012-09-12 01:17:01 +00:00
|
|
|
} else {
|
|
|
|
// The directory we're trying to load from doesn't exist.
|
|
|
|
this.dirsToLoad--;
|
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-09-13 18:47:34 +00:00
|
|
|
|
|
|
|
// aEntryPoint is either the entry_point name or the null, in which case we
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifest = new DOMApplicationManifest(aManifest, aApp.origin);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
msgmgr.registerPage(messageName, href, manifestURL);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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-09-13 18:47:34 +00:00
|
|
|
// aEntryPoint is either the entry_point name or the null, in which case we
|
|
|
|
// use the root of the manifest.
|
|
|
|
_registerActivitiesForEntryPoint: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.activities) {
|
2012-07-20 15:41:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifest = new DOMApplicationManifest(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-07-20 15:41:30 +00:00
|
|
|
let json = {
|
|
|
|
"manifest": aApp.manifestURL,
|
|
|
|
"name": activity,
|
|
|
|
"title": manifest.name,
|
|
|
|
"icon": manifest.iconURLForSize(128),
|
|
|
|
"description": description
|
|
|
|
}
|
|
|
|
cpmm.sendAsyncMessage("Activities:Register", json);
|
|
|
|
|
|
|
|
let launchPath =
|
2012-08-09 15:39:52 +00:00
|
|
|
Services.io.newURI(manifest.resolveFromOrigin(description.href), null, null);
|
2012-07-20 15:41:30 +00:00
|
|
|
let manifestURL = Services.io.newURI(aApp.manifestURL, null, null);
|
|
|
|
msgmgr.registerPage("activity", launchPath, manifestURL);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-13 18:47:34 +00:00
|
|
|
_registerActivities: function(aManifest, aApp) {
|
|
|
|
this._registerActivitiesForEntryPoint(aManifest, aApp, null);
|
|
|
|
|
|
|
|
if (!aManifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let entryPoint in aManifest.entry_points) {
|
|
|
|
this._registerActivitiesForEntryPoint(aManifest, aApp, entryPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_unregisterActivitiesForEntryPoint: function(aManifest, aApp, aEntryPoint) {
|
|
|
|
let root = aManifest;
|
|
|
|
if (aEntryPoint && aManifest.entry_points[aEntryPoint]) {
|
|
|
|
root = aManifest.entry_points[aEntryPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root.activities) {
|
2012-07-20 15:41:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-13 18:47:34 +00:00
|
|
|
for (let activity in root.activities) {
|
|
|
|
let description = root.activities[activity];
|
2012-07-20 15:41:30 +00:00
|
|
|
let json = {
|
|
|
|
"manifest": aApp.manifestURL,
|
|
|
|
"name": activity
|
|
|
|
}
|
|
|
|
cpmm.sendAsyncMessage("Activities:Unregister", json);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-13 18:47:34 +00:00
|
|
|
_unregisterActivities: function(aManifest, aApp) {
|
|
|
|
this._unregisterActivitiesForEntryPoint(aManifest, aApp, null);
|
|
|
|
|
|
|
|
if (!aManifest.entry_points) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let entryPoint in aManifest.entry_points) {
|
|
|
|
this._unregisterActivitiesForEntryPoint(aManifest, aApp, entryPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-12 01:17:01 +00:00
|
|
|
_processManifestForIds: function(aIds) {
|
|
|
|
this._readManifests(aIds, (function registerManifests(aResults) {
|
|
|
|
aResults.forEach(function registerManifest(aResult) {
|
|
|
|
let app = this.webapps[aResult.id];
|
|
|
|
let manifest = aResult.manifest;
|
|
|
|
app.name = manifest.name;
|
|
|
|
this._registerSystemMessages(manifest, app);
|
|
|
|
this._registerActivities(manifest, app);
|
|
|
|
}, this);
|
|
|
|
this.dirsLoaded++;
|
|
|
|
if (this.dirsLoaded == this.dirsToLoad) {
|
|
|
|
Services.obs.notifyObservers(this, "webapps-registry-ready", null);
|
|
|
|
}
|
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) {
|
|
|
|
Cu.reportError("DOMApplicationRegistry: Could not parse JSON: " + ex);
|
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 " +
|
|
|
|
aFile.path + " : " + ex);
|
2011-12-05 23:26:18 +00:00
|
|
|
if (aCallback)
|
|
|
|
aCallback(null);
|
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);
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
let msg = aMessage.json;
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:Install":
|
|
|
|
// always ask for UI to install
|
|
|
|
Services.obs.notifyObservers(this, "webapps-ask-install", JSON.stringify(msg));
|
|
|
|
break;
|
2012-03-06 19:50:58 +00:00
|
|
|
case "Webapps:GetSelf":
|
|
|
|
this.getSelf(msg);
|
|
|
|
break;
|
2011-11-28 20:13:26 +00:00
|
|
|
case "Webapps:Uninstall":
|
2012-08-09 01:04:48 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-uninstall", JSON.stringify(msg));
|
2012-03-06 19:50:58 +00:00
|
|
|
this.uninstall(msg);
|
2011-11-28 20:13:26 +00:00
|
|
|
break;
|
|
|
|
case "Webapps:Launch":
|
2012-07-16 18:22:16 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-launch", JSON.stringify(msg));
|
2011-11-28 20:13:26 +00:00
|
|
|
break;
|
2012-03-06 19:50:58 +00:00
|
|
|
case "Webapps:GetInstalled":
|
|
|
|
this.getInstalled(msg);
|
2011-12-15 17:20:57 +00:00
|
|
|
break;
|
2012-06-29 20:46:21 +00:00
|
|
|
case "Webapps:GetNotInstalled":
|
|
|
|
this.getNotInstalled(msg);
|
|
|
|
break;
|
2012-03-06 19:50:58 +00:00
|
|
|
case "Webapps:GetAll":
|
|
|
|
if (msg.hasPrivileges)
|
|
|
|
this.getAll(msg);
|
|
|
|
else
|
2012-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("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":
|
|
|
|
this.installPackage(msg);
|
|
|
|
break;
|
2012-07-11 15:38:36 +00:00
|
|
|
case "Webapps:GetBasePath":
|
2012-08-29 21:20:03 +00:00
|
|
|
return this.webapps[msg.id].basePath;
|
2012-07-11 15:38:36 +00:00
|
|
|
break;
|
2012-08-28 02:43:57 +00:00
|
|
|
case "Webapps:GetList":
|
|
|
|
this.children.push(aMessage.target);
|
|
|
|
return this.webapps;
|
2011-11-28 20:13:26 +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
|
|
|
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("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) {
|
2011-11-28 20:13:26 +00:00
|
|
|
let app = aData.app;
|
2012-08-29 21:20:03 +00:00
|
|
|
app.removable = true;
|
2011-12-08 13:32:54 +00:00
|
|
|
let id = app.syncId || this._appId(app.origin);
|
2012-07-09 10:25:41 +00:00
|
|
|
let localId = this.getAppLocalIdByManifestURL(app.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-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-07-11 15:38:33 +00:00
|
|
|
if (app.packageId) {
|
|
|
|
// Override the origin with the correct id.
|
|
|
|
app.origin = "app://" + id;
|
|
|
|
}
|
|
|
|
|
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-06-18 19:49:35 +00:00
|
|
|
appObject.installTime = app.installTime = 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-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-07-09 10:25:41 +00:00
|
|
|
|
2012-04-28 07:10:08 +00:00
|
|
|
let dir = FileUtils.getDir(DIRECTORY_NAME, ["webapps", id], true, true);
|
2011-11-28 20:13:26 +00:00
|
|
|
let manFile = dir.clone();
|
2012-05-22 21:38:34 +00:00
|
|
|
manFile.append("manifest.webapp");
|
2012-07-11 15:38:33 +00:00
|
|
|
this._writeFile(manFile, JSON.stringify(app.manifest), function() {
|
|
|
|
// If this a packaged app, move the zip file from the temp directory,
|
|
|
|
// and delete the temp directory.
|
|
|
|
if (app.packageId) {
|
2012-08-28 02:43:57 +00:00
|
|
|
let appFile = FileUtils.getFile("TmpD", ["webapps", app.packageId, "application.zip"],
|
2012-07-11 15:38:33 +00:00
|
|
|
true, true);
|
|
|
|
appFile.moveTo(dir, "application.zip");
|
2012-08-28 02:43:57 +00:00
|
|
|
let tmpDir = FileUtils.getDir("TmpD", ["webapps", app.packageId],
|
2012-07-11 15:38:33 +00:00
|
|
|
true, true);
|
|
|
|
try {
|
|
|
|
tmpDir.remove(true);
|
|
|
|
} catch(e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-04-28 07:10:08 +00:00
|
|
|
this.webapps[id] = appObject;
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
appObject.status = "installed";
|
2012-09-11 04:30:10 +00:00
|
|
|
appObject.name = app.manifest.name;
|
2012-08-28 02:43:57 +00:00
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
let manifest = new DOMApplicationManifest(app.manifest, app.origin);
|
|
|
|
|
2011-12-08 13:32:54 +00:00
|
|
|
if (!aFromSync)
|
|
|
|
this._saveApps((function() {
|
2012-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:Install:Return:OK", aData);
|
2012-04-28 07:10:08 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-sync-install", appNote);
|
2012-08-28 02:43:57 +00:00
|
|
|
this.children.forEach(function(aMsgMgr) {
|
2012-09-06 00:37:41 +00:00
|
|
|
aMsgMgr.sendAsyncMessage("Webapps:AddApp", { id: id, app: appObject });
|
2012-08-28 02:43:57 +00:00
|
|
|
});
|
2011-12-08 13:32:54 +00:00
|
|
|
}).bind(this));
|
2012-06-11 18:41:46 +00:00
|
|
|
|
2012-07-03 00:16:55 +00:00
|
|
|
#ifdef MOZ_SYS_MSG
|
2012-09-13 18:47:34 +00:00
|
|
|
this._registerSystemMessages(app.manifest, app);
|
|
|
|
this._registerActivities(app.manifest, app);
|
2012-07-03 00:16:55 +00:00
|
|
|
#endif
|
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
// if the manifest has an appcache_path property, use it to populate the appcache
|
|
|
|
if (manifest.appcache_path) {
|
|
|
|
let appcacheURI = Services.io.newURI(manifest.fullAppcachePath(), null, null);
|
|
|
|
let updateService = Cc["@mozilla.org/offlinecacheupdate-service;1"]
|
|
|
|
.getService(Ci.nsIOfflineCacheUpdateService);
|
|
|
|
let docURI = Services.io.newURI(manifest.fullLaunchPath(), null, null);
|
|
|
|
let cacheUpdate = aProfileDir ? updateService.scheduleCustomProfileUpdate(appcacheURI, docURI, aProfileDir)
|
|
|
|
: updateService.scheduleUpdate(appcacheURI, docURI, null);
|
|
|
|
cacheUpdate.addObserver(new AppcacheObserver(appObject), false);
|
|
|
|
if (aOfflineCacheObserver) {
|
|
|
|
cacheUpdate.addObserver(aOfflineCacheObserver, false);
|
|
|
|
}
|
|
|
|
}
|
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-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) {
|
|
|
|
this._writeFile(this.appsFile, JSON.stringify(this.webapps), function() {
|
|
|
|
if (aCallback)
|
|
|
|
aCallback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-12-05 23:26:18 +00:00
|
|
|
/**
|
|
|
|
* Asynchronously reads a list of manifests
|
|
|
|
*/
|
|
|
|
_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
|
|
|
|
|
|
|
// 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-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) {
|
|
|
|
aData[index].manifest = aJSON;
|
|
|
|
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-07-11 15:38:33 +00:00
|
|
|
installPackage: function(aData) {
|
|
|
|
// Here are the steps when installing a package:
|
|
|
|
// - create a temp directory where to store the app.
|
|
|
|
// - download the zip in this directory.
|
|
|
|
// - 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.
|
|
|
|
|
|
|
|
let id;
|
|
|
|
let manifestURL = "jar:" + aData.url + "!manifest.webapp";
|
|
|
|
// Check if we reinstall a known application.
|
|
|
|
for (let appId in this.webapps) {
|
|
|
|
if (this.webapps[appId].manifestURL == manifestURL) {
|
|
|
|
id = appId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New application.
|
|
|
|
if (!id) {
|
|
|
|
id = this.makeAppId();
|
|
|
|
}
|
|
|
|
|
|
|
|
let dir = FileUtils.getDir("TmpD", ["webapps", id], true, true);
|
|
|
|
|
|
|
|
/** from https://developer.mozilla.org/en/OpenWebApps/The_Manifest
|
|
|
|
* only the name property is mandatory
|
|
|
|
*/
|
|
|
|
function checkManifest(aManifest) {
|
|
|
|
if (aManifest.name == undefined)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (aManifest.installs_allowed_from) {
|
|
|
|
return aManifest.installs_allowed_from.some(function(aOrigin) {
|
|
|
|
return aOrigin == "*" || aOrigin == aData.installOrigin;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the directory we created, and sends an error to the DOM side.
|
|
|
|
function cleanup(aError) {
|
|
|
|
try {
|
|
|
|
dir.remove(true);
|
|
|
|
} catch (e) { }
|
2012-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:Install:Return:KO",
|
2012-07-11 15:38:33 +00:00
|
|
|
{ oid: aData.oid,
|
|
|
|
requestID: aData.requestID,
|
|
|
|
error: aError });
|
|
|
|
}
|
|
|
|
|
2012-08-28 02:43:57 +00:00
|
|
|
function getInferedStatus() {
|
|
|
|
// XXX Update once we have digital signatures (bug 772365)
|
|
|
|
return Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAppManifestStatus(aManifest) {
|
|
|
|
let type = aManifest.type || "web";
|
|
|
|
let manifestStatus = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case "web":
|
|
|
|
manifestStatus = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
break;
|
|
|
|
case "privileged":
|
|
|
|
manifestStatus = Ci.nsIPrincipal.APP_STATUS_PRIVILEGED;
|
|
|
|
break
|
|
|
|
case "certified":
|
|
|
|
manifestStatus = Ci.nsIPrincipal.APP_STATUS_CERTIFIED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAppStatus(aManifest) {
|
|
|
|
let manifestStatus = getAppManifestStatus(aManifest);
|
|
|
|
let inferedStatus = getInferedStatus();
|
|
|
|
|
|
|
|
return (Services.prefs.getBoolPref("dom.mozApps.dev_mode") ? manifestStatus
|
|
|
|
: inferedStatus);
|
|
|
|
}
|
|
|
|
// Returns true if the privilege level from the manifest
|
|
|
|
// is lower or equal to the one we infered for the app.
|
|
|
|
function checkAppStatus(aManifest) {
|
|
|
|
if (Services.prefs.getBoolPref("dom.mozApps.dev_mode")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (getAppManifestStatus(aManifest) <= getInferedStatus());
|
|
|
|
}
|
|
|
|
|
2012-07-11 15:38:33 +00:00
|
|
|
NetUtil.asyncFetch(aData.url, function(aInput, aResult, aRequest) {
|
|
|
|
if (!Components.isSuccessCode(aResult)) {
|
|
|
|
// We failed to fetch the zip.
|
|
|
|
cleanup("NETWORK_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Copy the zip on disk.
|
|
|
|
let zipFile = FileUtils.getFile("TmpD",
|
|
|
|
["webapps", id, "application.zip"], true);
|
|
|
|
let ostream = FileUtils.openSafeFileOutputStream(zipFile);
|
|
|
|
NetUtil.asyncCopy(aInput, ostream, function (aResult) {
|
|
|
|
if (!Components.isSuccessCode(aResult)) {
|
|
|
|
// We failed to save the zip.
|
|
|
|
cleanup("DOWNLOAD_ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Build a data structure to call the webapps confirmation dialog :
|
|
|
|
// - load the manifest from the zip
|
2012-08-07 01:16:00 +00:00
|
|
|
// - set data.app.(origin, install_origin, manifestURL, manifest, receipts, categories)
|
2012-07-11 15:38:33 +00:00
|
|
|
// - call notifyObservers(this, "webapps-ask-install", JSON.stringify(msg));
|
|
|
|
let msg = {
|
|
|
|
from: aData.from,
|
|
|
|
oid: aData.oid,
|
2012-09-12 23:12:59 +00:00
|
|
|
requestID: aData.requestID,
|
2012-07-11 15:38:33 +00:00
|
|
|
app: {
|
|
|
|
packageId: id,
|
|
|
|
installOrigin: aData.installOrigin,
|
|
|
|
origin: "app://" + id,
|
|
|
|
manifestURL: manifestURL,
|
2012-08-07 01:16:00 +00:00
|
|
|
receipts: aData.receipts,
|
|
|
|
categories: aData.categories
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
|
|
|
|
.createInstance(Ci.nsIZipReader);
|
|
|
|
try {
|
|
|
|
zipReader.open(zipFile);
|
|
|
|
if (!zipReader.hasEntry("manifest.webapp")) {
|
|
|
|
throw "No manifest.webapp found.";
|
|
|
|
}
|
|
|
|
|
|
|
|
let istream = zipReader.getInputStream("manifest.webapp");
|
|
|
|
msg.app.manifest = JSON.parse(NetUtil.readInputStreamToString(istream,
|
|
|
|
istream.available()) || "");
|
|
|
|
if (!checkManifest(msg.app.manifest)) {
|
2012-08-28 02:43:57 +00:00
|
|
|
throw "INVALID_MANIFEST";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkAppStatus(msg.app.manifest)) {
|
|
|
|
throw "INVALID_SECURITY_LEVEL";
|
2012-07-11 15:38:33 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 00:52:03 +00:00
|
|
|
msg.app.appStatus = getAppStatus(msg.app.manifest);
|
2012-07-11 15:38:33 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-ask-install",
|
|
|
|
JSON.stringify(msg));
|
|
|
|
} catch (e) {
|
|
|
|
// XXX we may need new error messages.
|
2012-08-28 02:43:57 +00:00
|
|
|
cleanup(e);
|
2012-07-11 15:38:33 +00:00
|
|
|
} finally {
|
|
|
|
zipReader.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
uninstall: function(aData) {
|
2012-03-06 19:50:58 +00:00
|
|
|
let found = false;
|
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-08-29 21:20:03 +00:00
|
|
|
if (!this.webapps[id].removable)
|
|
|
|
return;
|
|
|
|
|
2012-07-20 15:41:30 +00:00
|
|
|
found = true;
|
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
|
|
|
this._readManifests([{ id: id }], (function unregisterManifest(aResult) {
|
|
|
|
#ifdef MOZ_SYS_MSG
|
|
|
|
this._unregisterActivities(aResult[0].manifest, app);
|
|
|
|
#endif
|
|
|
|
}).bind(this));
|
|
|
|
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:Uninstall:Return:OK", aData);
|
2012-07-20 15:41:30 +00:00
|
|
|
Services.obs.notifyObservers(this, "webapps-sync-uninstall", appNote);
|
2012-08-28 02:43:57 +00:00
|
|
|
this.children.forEach(function(aMsgMgr) {
|
|
|
|
aMsgMgr.broadcastAsyncMessage("Webapps:RemoveApp", { id: id });
|
|
|
|
});
|
2012-07-20 15:41:30 +00:00
|
|
|
}).bind(this));
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
2012-07-20 15:41:30 +00:00
|
|
|
|
|
|
|
if (!found) {
|
2012-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:Uninstall:Return:KO", aData);
|
2012-07-20 15:41:30 +00:00
|
|
|
}
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
2011-12-08 13:32:54 +00:00
|
|
|
|
2012-03-06 19:50:58 +00:00
|
|
|
getSelf: function(aData) {
|
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
|
|
|
let id = this._appId(aData.origin);
|
2012-03-06 19:50:58 +00:00
|
|
|
|
2012-07-03 03:45:00 +00:00
|
|
|
if (id && this._isLaunchable(this.webapps[id].origin)) {
|
2012-08-28 02:43:57 +00:00
|
|
|
let app = AppsUtils.cloneAppObject(this.webapps[id]);
|
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
|
|
|
}
|
|
|
|
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:GetSelf:Return:OK", aData);
|
2012-03-06 19:50:58 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
2011-11-28 20:13:26 +00:00
|
|
|
|
2012-03-06 19:50:58 +00:00
|
|
|
getInstalled: function(aData) {
|
|
|
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:GetInstalled:Return:OK", aData);
|
2011-12-05 23:26:18 +00:00
|
|
|
}).bind(this));
|
2011-11-28 20:13:26 +00:00
|
|
|
},
|
|
|
|
|
2012-06-29 20:46:21 +00:00
|
|
|
getNotInstalled: function(aData) {
|
|
|
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:GetNotInstalled:Return:OK", aData);
|
2012-06-29 20:46:21 +00:00
|
|
|
}).bind(this));
|
|
|
|
},
|
|
|
|
|
2012-03-06 19:50:58 +00:00
|
|
|
getAll: function(aData) {
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("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);
|
2011-12-05 23:26:18 +00:00
|
|
|
if (!id) {
|
|
|
|
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-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-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;
|
|
|
|
let origin = this.webapps[record.id].origin;
|
|
|
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:Uninstall:Return:OK", { origin: origin });
|
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-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("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) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-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-06-11 18:41:46 +00:00
|
|
|
this.app = aApp;
|
|
|
|
};
|
|
|
|
|
|
|
|
AppcacheObserver.prototype = {
|
|
|
|
// nsIOfflineCacheUpdateObserver implementation
|
|
|
|
updateStateChanged: function appObs_Update(aUpdate, aState) {
|
|
|
|
let mustSave = false;
|
|
|
|
let app = this.app;
|
|
|
|
|
|
|
|
let setStatus = function appObs_setStatus(aStatus) {
|
|
|
|
mustSave = (app.status != aStatus);
|
|
|
|
app.status = aStatus;
|
2012-08-27 14:13:02 +00:00
|
|
|
ppmm.broadcastAsyncMessage("Webapps:OfflineCache", { manifest: app.manifestURL, status: aStatus });
|
2012-06-11 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (aState) {
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ERROR:
|
|
|
|
aUpdate.removeObserver(this);
|
|
|
|
setStatus("cache-error");
|
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_NOUPDATE:
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_FINISHED:
|
|
|
|
aUpdate.removeObserver(this);
|
|
|
|
setStatus("cached");
|
|
|
|
break;
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_DOWNLOADING:
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMSTARTED:
|
|
|
|
case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMPROGRESS:
|
|
|
|
setStatus("downloading")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
/**
|
|
|
|
* Helper object to access manifest information with locale support
|
|
|
|
*/
|
2012-07-03 00:16:55 +00:00
|
|
|
let DOMApplicationManifest = function(aManifest, aOrigin) {
|
2011-11-28 20:13:26 +00:00
|
|
|
this._origin = Services.io.newURI(aOrigin, null, null);
|
|
|
|
this._manifest = aManifest;
|
|
|
|
let chrome = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry)
|
|
|
|
.QueryInterface(Ci.nsIToolkitChromeRegistry);
|
|
|
|
let locale = chrome.getSelectedLocale("browser").toLowerCase();
|
|
|
|
this._localeRoot = this._manifest;
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
if (this._manifest.locales && this._manifest.locales[locale]) {
|
|
|
|
this._localeRoot = this._manifest.locales[locale];
|
|
|
|
}
|
|
|
|
else if (this._manifest.locales) {
|
|
|
|
// try with the language part of the locale ("en" for en-GB) only
|
|
|
|
let lang = locale.split('-')[0];
|
2012-03-22 21:26:36 +00:00
|
|
|
if (lang != locale && this._manifest.locales[lang])
|
2011-11-28 20:13:26 +00:00
|
|
|
this._localeRoot = this._manifest.locales[lang];
|
|
|
|
}
|
2012-04-28 07:10:08 +00:00
|
|
|
};
|
2011-11-28 20:13:26 +00:00
|
|
|
|
|
|
|
DOMApplicationManifest.prototype = {
|
|
|
|
_localeProp: function(aProp) {
|
|
|
|
if (this._localeRoot[aProp] != undefined)
|
|
|
|
return this._localeRoot[aProp];
|
|
|
|
return this._manifest[aProp];
|
|
|
|
},
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._localeProp("name");
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
get description() {
|
|
|
|
return this._localeProp("description");
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
get version() {
|
|
|
|
return this._localeProp("version");
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
get launch_path() {
|
|
|
|
return this._localeProp("launch_path");
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
get developer() {
|
|
|
|
return this._localeProp("developer");
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
get icons() {
|
|
|
|
return this._localeProp("icons");
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
get appcache_path() {
|
|
|
|
return this._localeProp("appcache_path");
|
|
|
|
},
|
|
|
|
|
2012-08-08 19:35:15 +00:00
|
|
|
get orientation() {
|
|
|
|
return this._localeProp("orientation");
|
|
|
|
},
|
|
|
|
|
2011-11-28 20:13:26 +00:00
|
|
|
iconURLForSize: function(aSize) {
|
|
|
|
let icons = this._localeProp("icons");
|
|
|
|
if (!icons)
|
|
|
|
return null;
|
|
|
|
let dist = 100000;
|
|
|
|
let icon = null;
|
|
|
|
for (let size in icons) {
|
|
|
|
let iSize = parseInt(size);
|
|
|
|
if (Math.abs(iSize - aSize) < dist) {
|
|
|
|
icon = this._origin.resolve(icons[size]);
|
|
|
|
dist = Math.abs(iSize - aSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
},
|
2012-04-28 07:10:08 +00:00
|
|
|
|
2012-05-04 18:02:05 +00:00
|
|
|
fullLaunchPath: function(aStartPoint) {
|
2012-08-30 22:02:00 +00:00
|
|
|
// If no start point is specified, we use the root launch path.
|
|
|
|
// In all error cases, we just return null.
|
|
|
|
if ((aStartPoint || "") === "") {
|
|
|
|
return this._origin.resolve(this._localeProp("launch_path") || "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the l10n entry_points property.
|
|
|
|
let entryPoints = this._localeProp("entry_points");
|
|
|
|
if (!entryPoints) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entryPoints[aStartPoint]) {
|
|
|
|
return this._origin.resolve(entryPoints[aStartPoint].launch_path || "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2012-06-11 18:41:46 +00:00
|
|
|
},
|
|
|
|
|
2012-08-09 15:39:52 +00:00
|
|
|
resolveFromOrigin: function(aURI) {
|
|
|
|
return this._origin.resolve(aURI);
|
|
|
|
},
|
|
|
|
|
2012-06-11 18:41:46 +00:00
|
|
|
fullAppcachePath: function() {
|
|
|
|
let appcachePath = this._localeProp("appcache_path");
|
|
|
|
return this._origin.resolve(appcachePath ? appcachePath : "");
|
2011-11-28 20:13:26 +00:00
|
|
|
}
|
2012-04-28 07:10:08 +00:00
|
|
|
};
|
2011-11-28 20:13:26 +00:00
|
|
|
|
|
|
|
DOMApplicationRegistry.init();
|