Bug 706545 - Implement a sync engine for apps exposed by navigator.mozApps : Part 1: Webapps.jsm support [r=philikon]

This commit is contained in:
Fabrice Desré 2011-12-08 05:32:54 -08:00
Родитель bd52e7bdd6
Коммит 4af2594c50
1 изменённых файлов: 95 добавлений и 15 удалений

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

@ -148,7 +148,7 @@ let DOMApplicationRegistry = {
aCallbak(); aCallbak();
}); });
}, },
// clones a app object, without the manifest // clones a app object, without the manifest
_cloneAppObject: function(aApp) { _cloneAppObject: function(aApp) {
let clone = { let clone = {
@ -159,14 +159,14 @@ let DOMApplicationRegistry = {
}; };
return clone; return clone;
}, },
denyInstall: function(aData) { denyInstall: function(aData) {
this.mm.sendAsyncMessage("Webapps:Install:Return:KO", aData); this.mm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
}, },
confirmInstall: function(aData) { confirmInstall: function(aData, aFromSync) {
let app = aData.app; let app = aData.app;
let id = this._appId(app.origin); let id = app.syncId || this._appId(app.origin);
// install an application again is considered as an update // install an application again is considered as an update
if (id) { if (id) {
@ -191,11 +191,14 @@ let DOMApplicationRegistry = {
delete this.webapps[id].manifest; delete this.webapps[id].manifest;
this.webapps[id].installTime = (new Date()).getTime() this.webapps[id].installTime = (new Date()).getTime()
this._writeFile(this.appsFile, JSON.stringify(this.webapps), (function() {
this.mm.sendAsyncMessage("Webapps:Install:Return:OK", aData); if (!aFromSync)
}).bind(this)); this._saveApps((function() {
this.mm.sendAsyncMessage("Webapps:Install:Return:OK", aData);
Services.obs.notifyObservers(this, "webapps-sync-install", id);
}).bind(this));
}, },
_appId: function(aURI) { _appId: function(aURI) {
for (let id in this.webapps) { for (let id in this.webapps) {
if (this.webapps[id].origin == aURI) if (this.webapps[id].origin == aURI)
@ -204,6 +207,13 @@ let DOMApplicationRegistry = {
return null; return null;
}, },
_saveApps: function(aCallback) {
this._writeFile(this.appsFile, JSON.stringify(this.webapps), function() {
if (aCallback)
aCallback();
});
},
/** /**
* Asynchronously reads a list of manifests * Asynchronously reads a list of manifests
*/ */
@ -219,23 +229,25 @@ let DOMApplicationRegistry = {
this._readManifests(aData, aFinalCallback, index + 1); this._readManifests(aData, aFinalCallback, index + 1);
}).bind(this)); }).bind(this));
}, },
uninstall: function(aData) { uninstall: function(aData) {
for (let id in this.webapps) { for (let id in this.webapps) {
let app = this.webapps[id]; let app = this.webapps[id];
if (app.origin == aData.origin) { if (app.origin == aData.origin) {
delete this.webapps[id]; delete this.webapps[id];
this._writeFile(this.appsFile, JSON.stringify(this.webapps));
let dir = FileUtils.getDir("ProfD", ["webapps", id], true, true); let dir = FileUtils.getDir("ProfD", ["webapps", id], true, true);
try { try {
dir.remove(true); dir.remove(true);
} catch (e) { } catch (e) {
} }
this.mm.sendAsyncMessage("Webapps:Uninstall:Return:OK", aData); this._saveApps((function() {
this.mm.sendAsyncMessage("Webapps:Uninstall:Return:OK", aData);
Services.obs.notifyObservers(this, "webapps-sync-uninstall", id);
}).bind(this));
} }
} }
}, },
enumerate: function(aData) { enumerate: function(aData) {
aData.apps = []; aData.apps = [];
let tmp = []; let tmp = [];
@ -302,7 +314,6 @@ let DOMApplicationRegistry = {
return; return;
let id = this._appId(aOrigin); let id = this._appId(aOrigin);
if (!id) { if (!id) {
aCallback(null); aCallback(null);
return; return;
@ -311,7 +322,76 @@ let DOMApplicationRegistry = {
this._readManifests([{ id: id }], function(aResult) { this._readManifests([{ id: id }], function(aResult) {
aCallback(aResult[0].manifest); aCallback(aResult[0].manifest);
}); });
} },
/** added to support the sync engine */
getAppById: function(aId) {
if (!this.webapps[aId])
return null;
let app = this._cloneAppObject(this.webapps[aId]);
return app;
},
itemExists: function(aId) {
return !!this.webapps[aId];
},
updateApps: function(aRecords, aCallback) {
for (let i = 0; i < aRecords.length; i++) {
let record = aRecords[i];
if (record.deleted) {
if (!this.webapps[record.id])
continue;
let origin = this.webapps[record.id].origin;
delete this.webapps[record.id];
let dir = FileUtils.getDir("ProfD", ["webapps", record.id], true, true);
try {
dir.remove(true);
} catch (e) {
}
this.mm.sendAsyncMessage("Webapps:Uninstall:Return:OK", { origin: origin });
} else {
if (!!this.webapps[record.id]) {
this.webapps[record.id] = record.value;
delete this.webapps[record.id].manifest;
}
else {
let data = { app: record.value };
this.confirmInstall(data, true);
this.mm.sendAsyncMessage("Webapps:Install:Return:OK", data);
}
}
}
this._saveApps(aCallback);
},
/*
* May be removed once sync API change
*/
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) {
delete this.webapps[id];
let dir = FileUtils.getDir("ProfD", ["webapps", id], true, true);
try {
dir.remove(true);
} catch (e) {
}
}
this._saveApps(aCallback);
}
}; };
/** /**