Bug 796217 - Refactor Webapps.jsm and related files - Part 1: move DOMApplicationManifest outside of Webapps.jsm [r=gwagner]

This commit is contained in:
Fabrice Desré 2012-10-02 22:38:03 -07:00
Родитель ebaf07fb9a
Коммит d782e1b6aa
7 изменённых файлов: 154 добавлений и 150 удалений

Просмотреть файл

@ -23,6 +23,7 @@ Cu.import('resource://gre/modules/PermissionSettings.jsm');
Cu.import('resource://gre/modules/ObjectWrapper.jsm');
Cu.import('resource://gre/modules/accessibility/AccessFu.jsm');
Cu.import('resource://gre/modules/Payment.jsm');
Cu.import("resource://gre/modules/AppsUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(Services, 'env',
'@mozilla.org/process/environment;1',
@ -651,7 +652,7 @@ var WebappsHelper = {
if (!aManifest)
return;
let manifest = new DOMApplicationManifest(aManifest, json.origin);
let manifest = new ManifestHelper(aManifest, json.origin);
shell.sendChromeEvent({
"type": "webapps-launch",
"url": manifest.fullLaunchPath(json.startPoint),

Просмотреть файл

@ -10,6 +10,7 @@ const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Webapps.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
function ContentPermissionPrompt() {}
@ -72,7 +73,7 @@ ContentPermissionPrompt.prototype = {
// When it's an app, get the manifest to add the l10n application name.
let app = DOMApplicationRegistry.getAppByLocalId(principal.appId);
DOMApplicationRegistry.getManifestFor(app.origin, function getManifest(aManifest) {
let helper = new DOMApplicationManifest(aManifest, app.origin);
let helper = new ManifestHelper(aManifest, app.origin);
details.appName = helper.name;
browser.shell.sendChromeEvent(details);
});

Просмотреть файл

@ -11,6 +11,7 @@ let Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Webapps.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
Cu.import("resource://gre/modules/WebappsInstaller.jsm");
Cu.import("resource://gre/modules/WebappOSUtils.jsm");
@ -20,7 +21,7 @@ let webappsUI = {
Services.obs.addObserver(this, "webapps-launch", false);
Services.obs.addObserver(this, "webapps-uninstall", false);
},
uninit: function webappsUI_uninit() {
Services.obs.removeObserver(this, "webapps-ask-install");
Services.obs.removeObserver(this, "webapps-launch");
@ -47,7 +48,7 @@ let webappsUI = {
},
openURL: function(aUrl, aOrigin) {
let browserEnumerator = Services.wm.getEnumerator("navigator:browser");
let browserEnumerator = Services.wm.getEnumerator("navigator:browser");
let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
// Check each browser instance for our URL
@ -127,7 +128,7 @@ let webappsUI = {
};
let requestingURI = aWindow.makeURI(aData.from);
let manifest = new DOMApplicationManifest(aData.app.manifest, aData.app.origin);
let manifest = new ManifestHelper(aData.app.manifest, aData.app.origin);
let host;
try {

Просмотреть файл

@ -14,7 +14,7 @@ Cu.import("resource://gre/modules/Services.jsm");
// Shared code for AppsServiceChild.jsm, Webapps.jsm and Webapps.js
let EXPORTED_SYMBOLS = ["AppsUtils"];
let EXPORTED_SYMBOLS = ["AppsUtils", "ManifestHelper"];
function debug(s) {
//dump("-*- AppsUtils.jsm: " + s + "\n");
@ -178,3 +178,130 @@ let AppsUtils = {
return true;
}
}
/**
* Helper object to access manifest information with locale support
*/
let ManifestHelper = function(aManifest, aOrigin) {
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;
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];
if (lang != locale && this._manifest.locales[lang])
this._localeRoot = this._manifest.locales[lang];
}
};
ManifestHelper.prototype = {
_localeProp: function(aProp) {
if (this._localeRoot[aProp] != undefined)
return this._localeRoot[aProp];
return this._manifest[aProp];
},
get name() {
return this._localeProp("name");
},
get description() {
return this._localeProp("description");
},
get version() {
return this._localeProp("version");
},
get launch_path() {
return this._localeProp("launch_path");
},
get developer() {
return this._localeProp("developer");
},
get icons() {
return this._localeProp("icons");
},
get appcache_path() {
return this._localeProp("appcache_path");
},
get orientation() {
return this._localeProp("orientation");
},
get package_path() {
return this._localeProp("package_path");
},
get size() {
return this._manifest["size"] || 0;
},
get permissions() {
if (this._manifest.permissions) {
return this._manifest.permissions;
}
return {};
},
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;
},
fullLaunchPath: function(aStartPoint) {
// 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;
},
resolveFromOrigin: function(aURI) {
return this._origin.resolve(aURI);
},
fullAppcachePath: function() {
let appcachePath = this._localeProp("appcache_path");
return this._origin.resolve(appcachePath ? appcachePath : "");
},
fullPackagePath: function() {
let packagePath = this._localeProp("package_path");
return this._origin.resolve(packagePath ? packagePath : "");
}
}

Просмотреть файл

@ -9,7 +9,7 @@ const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
let EXPORTED_SYMBOLS = ["DOMApplicationRegistry", "DOMApplicationManifest"];
let EXPORTED_SYMBOLS = ["DOMApplicationRegistry"];
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
@ -327,7 +327,7 @@ let DOMApplicationRegistry = {
return;
}
let manifest = new DOMApplicationManifest(aManifest, aApp.origin);
let manifest = new ManifestHelper(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) {
@ -367,7 +367,7 @@ let DOMApplicationRegistry = {
return;
}
let manifest = new DOMApplicationManifest(aManifest, aApp.origin);
let manifest = new ManifestHelper(aManifest, aApp.origin);
for (let activity in root.activities) {
let description = root.activities[activity];
if (!description.href) {
@ -719,7 +719,7 @@ let DOMApplicationRegistry = {
return;
}
let manifest = new DOMApplicationManifest(aJSON, app.origin);
let manifest = new ManifestHelper(aJSON, app.origin);
this.downloadPackage(manifest, { manifestURL: aManifestURL,
origin: app.origin }, true,
function(aId, aManifest) {
@ -813,8 +813,7 @@ let DOMApplicationRegistry = {
function installPermissions(aAppObject, aData, aIsReinstall)
{
try {
let newManifest = new DOMApplicationManifest(aData.app.manifest,
aData.app.origin);
let newManifest = new ManifestHelper(aData.app.manifest, aData.app.origin);
if (!newManifest.permissions && !aIsReinstall) {
return;
}
@ -915,7 +914,7 @@ let DOMApplicationRegistry = {
function updatePackagedApp(aManifest) {
debug("updatePackagedApp");
let manifest = new DOMApplicationManifest(aManifest, app.manifestURL);
let manifest = new ManifestHelper(aManifest, app.manifestURL);
// A package is available: set downloadAvailable to fire the matching
// event.
app.downloadAvailable = true;
@ -949,7 +948,7 @@ let DOMApplicationRegistry = {
manFile.append("manifest.webapp");
this._writeFile(manFile, JSON.stringify(aManifest), function() { });
let manifest = new DOMApplicationManifest(aManifest, app.origin);
let manifest = new ManifestHelper(aManifest, app.origin);
if (manifest.appcache_path) {
app.installState = "updating";
@ -1090,7 +1089,7 @@ let DOMApplicationRegistry = {
let jsonManifest = aData.isPackage ? app.updateManifest : app.manifest;
this._writeFile(manFile, JSON.stringify(jsonManifest), function() { });
let manifest = new DOMApplicationManifest(jsonManifest, app.origin);
let manifest = new ManifestHelper(jsonManifest, app.origin);
if (manifest.appcache_path) {
appObject.installState = "pending";
@ -1138,7 +1137,7 @@ let DOMApplicationRegistry = {
if (manifest.package_path) {
// origin for install apps is meaningless here, since it's app:// and this
// can't be used to resolve package paths.
manifest = new DOMApplicationManifest(jsonManifest, app.manifestURL);
manifest = new ManifestHelper(jsonManifest, app.manifestURL);
this.downloadPackage(manifest, appObject, false, function(aId, aManifest) {
// Success! Move the zip out of TmpD.
let app = DOMApplicationRegistry.webapps[id];
@ -1836,131 +1835,4 @@ AppcacheObserver.prototype = {
}
};
/**
* Helper object to access manifest information with locale support
*/
let DOMApplicationManifest = function(aManifest, aOrigin) {
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;
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];
if (lang != locale && this._manifest.locales[lang])
this._localeRoot = this._manifest.locales[lang];
}
};
DOMApplicationManifest.prototype = {
_localeProp: function(aProp) {
if (this._localeRoot[aProp] != undefined)
return this._localeRoot[aProp];
return this._manifest[aProp];
},
get name() {
return this._localeProp("name");
},
get description() {
return this._localeProp("description");
},
get version() {
return this._localeProp("version");
},
get launch_path() {
return this._localeProp("launch_path");
},
get developer() {
return this._localeProp("developer");
},
get icons() {
return this._localeProp("icons");
},
get appcache_path() {
return this._localeProp("appcache_path");
},
get orientation() {
return this._localeProp("orientation");
},
get package_path() {
return this._localeProp("package_path");
},
get size() {
return this._manifest["size"] || 0;
},
get permissions() {
if (this._manifest.permissions) {
return this._manifest.permissions;
}
return {};
},
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;
},
fullLaunchPath: function(aStartPoint) {
// 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;
},
resolveFromOrigin: function(aURI) {
return this._origin.resolve(aURI);
},
fullAppcachePath: function() {
let appcachePath = this._localeProp("appcache_path");
return this._origin.resolve(appcachePath ? appcachePath : "");
},
fullPackagePath: function() {
let packagePath = this._localeProp("package_path");
return this._origin.resolve(packagePath ? packagePath : "");
}
};
DOMApplicationRegistry.init();

Просмотреть файл

@ -10,7 +10,7 @@ let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm")
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Webapps.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutApps.properties");
@ -98,7 +98,7 @@ function updateList() {
function addApplication(aApp) {
let list = document.getElementById("appgrid");
let manifest = new DOMApplicationManifest(aApp.manifest, aApp.origin);
let manifest = new ManifestHelper(aApp.manifest, aApp.origin);
let container = document.createElement("div");
container.className = "app";

Просмотреть файл

@ -13,6 +13,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
#ifdef ACCESSIBILITY
Cu.import("resource://gre/modules/accessibility/AccessFu.jsm");
#endif
@ -6285,6 +6286,7 @@ var ActivityObserver = {
var WebappsUI = {
init: function init() {
Cu.import("resource://gre/modules/Webapps.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
DOMApplicationRegistry.allAppsLaunchable = true;
Services.obs.addObserver(this, "webapps-ask-install", false);
@ -6294,7 +6296,7 @@ var WebappsUI = {
Services.obs.addObserver(this, "webapps-install-error", false);
Services.obs.addObserver(this, "WebApps:InstallMarketplace", false);
},
uninit: function unint() {
Services.obs.removeObserver(this, "webapps-ask-install");
Services.obs.removeObserver(this, "webapps-launch");
@ -6337,7 +6339,7 @@ var WebappsUI = {
DOMApplicationRegistry.getManifestFor(data.origin, (function(aManifest) {
if (!aManifest)
return;
let manifest = new DOMApplicationManifest(aManifest, data.origin);
let manifest = new ManifestHelper(aManifest, data.origin);
this.openURL(manifest.fullLaunchPath(), data.origin);
}).bind(this));
break;
@ -6346,7 +6348,7 @@ var WebappsUI = {
DOMApplicationRegistry.getManifestFor(data.origin, (function(aManifest) {
if (!aManifest)
return;
let manifest = new DOMApplicationManifest(aManifest, data.origin);
let manifest = new ManifestHelper(aManifest, data.origin);
let observer = {
observe: function (aSubject, aTopic) {
@ -6460,7 +6462,7 @@ var WebappsUI = {
},
doInstall: function doInstall(aData) {
let manifest = new DOMApplicationManifest(aData.app.manifest, aData.app.origin);
let manifest = new ManifestHelper(aData.app.manifest, aData.app.origin);
let name = manifest.name ? manifest.name : manifest.fullLaunchPath();
let showPrompt = true;
@ -6482,7 +6484,7 @@ var WebappsUI = {
uniqueURI: aData.app.origin
}
});
// if java returned a profile path to us, try to use it to pre-populate the app cache
let file = null;
if (profilePath) {