Bug 777204 - Webapps.jsm broadcasts information to all content processes [r=gwagner]

This commit is contained in:
Fabrice Desré 2012-09-18 10:34:55 -07:00
parent f0c74ac7ab
commit d82a273a26
7 changed files with 102 additions and 37 deletions

View File

@ -594,6 +594,8 @@ var WebappsHelper = {
observe: function webapps_observe(subject, topic, data) {
let json = JSON.parse(data);
json.mm = subject;
switch(topic) {
case "webapps-launch":
DOMApplicationRegistry.getManifestFor(json.origin, function(aManifest) {

View File

@ -29,6 +29,7 @@ let webappsUI = {
observe: function webappsUI_observe(aSubject, aTopic, aData) {
let data = JSON.parse(aData);
data.mm = aSubject;
switch(aTopic) {
case "webapps-ask-install":

View File

@ -68,6 +68,7 @@ WebappsRegistry.prototype = {
Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app));
break;
case "Webapps:Install:Return:KO":
dump("XxXxX Webapps:Install:Return:KO\n");
Services.DOMRequest.fireError(req, msg.error || "DENIED");
break;
case "Webapps:GetSelf:Return:OK":
@ -168,6 +169,8 @@ WebappsRegistry.prototype = {
uninit: function() {
this._mgmt = null;
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
["Webapps:Install:Return:OK"]);
},
// mozIDOMApplicationRegistry2 implementation
@ -198,6 +201,8 @@ WebappsRegistry.prototype = {
let util = this._window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
this._id = util.outerWindowID;
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
["Webapps:Install:Return:OK"]);
},
classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
@ -270,6 +275,8 @@ WebappsApplication.prototype = {
this.initHelper(aWindow, ["Webapps:Uninstall:Return:OK",
"Webapps:Uninstall:Return:KO",
"Webapps:OfflineCache"]);
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
["Webapps:Uninstall:Return:OK", "Webapps:OfflineCache"]);
},
set onprogress(aCallback) {
@ -300,6 +307,8 @@ WebappsApplication.prototype = {
uninit: function() {
this._onprogress = null;
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
["Webapps:Uninstall:Return:OK", "Webapps:OfflineCache"]);
},
receiveMessage: function(aMessage) {
@ -356,6 +365,9 @@ function WebappsApplicationMgmt(aWindow) {
"Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK",
"Webapps:GetNotInstalled:Return:OK"]);
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
["Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK"]);
this._oninstall = null;
this._onuninstall = null;
}
@ -372,6 +384,8 @@ WebappsApplicationMgmt.prototype = {
uninit: function() {
this._oninstall = null;
this._onuninstall = null;
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
["Webapps:Install:Return:OK", "Webapps:Uninstall:Return:OK"]);
},
getAll: function() {

View File

@ -60,7 +60,8 @@ let DOMApplicationRegistry = {
"Webapps:GetInstalled", "Webapps:GetNotInstalled",
"Webapps:Launch", "Webapps:GetAll",
"Webapps:InstallPackage", "Webapps:GetBasePath",
"Webapps:GetList"];
"Webapps:GetList", "Webapps:RegisterForMessages",
"Webapps:UnregisterForMessages"];
this.messages.forEach((function(msgName) {
ppmm.addMessageListener(msgName, this);
@ -319,52 +320,98 @@ let DOMApplicationRegistry = {
}
},
addMessageListener: function(aMsgNames, aMm) {
aMsgNames.forEach(function (aMsgName) {
if (!(aMsgName in this.children)) {
this.children[aMsgName] = [];
}
this.children[aMsgName].push(aMm);
}, this);
},
removeMessageListener: function(aMsgNames, aMm) {
aMsgNames.forEach(function (aMsgName) {
if (!(aMsgName in this.children)) {
return;
}
let index;
if ((index = this.children[aMsgName].indexOf(aMm)) != -1) {
this.children[aMsgName].splice(index, 1);
}
}, this);
},
receiveMessage: function(aMessage) {
// 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);
let msg = aMessage.json;
let mm = aMessage.target;
msg.mm = mm;
switch (aMessage.name) {
case "Webapps:Install":
// always ask for UI to install
Services.obs.notifyObservers(this, "webapps-ask-install", JSON.stringify(msg));
Services.obs.notifyObservers(mm, "webapps-ask-install", JSON.stringify(msg));
break;
case "Webapps:GetSelf":
this.getSelf(msg);
this.getSelf(msg, mm);
break;
case "Webapps:Uninstall":
Services.obs.notifyObservers(this, "webapps-uninstall", JSON.stringify(msg));
Services.obs.notifyObservers(mm, "webapps-uninstall", JSON.stringify(msg));
this.uninstall(msg);
break;
case "Webapps:Launch":
Services.obs.notifyObservers(this, "webapps-launch", JSON.stringify(msg));
Services.obs.notifyObservers(mm, "webapps-launch", JSON.stringify(msg));
break;
case "Webapps:GetInstalled":
this.getInstalled(msg);
this.getInstalled(msg, mm);
break;
case "Webapps:GetNotInstalled":
this.getNotInstalled(msg);
this.getNotInstalled(msg, mm);
break;
case "Webapps:GetAll":
if (msg.hasPrivileges)
this.getAll(msg);
this.getAll(msg, mm);
else
ppmm.broadcastAsyncMessage("Webapps:GetAll:Return:KO", msg);
mm.sendAsyncMessage("Webapps:GetAll:Return:KO", msg);
break;
case "Webapps:InstallPackage":
this.installPackage(msg);
this.installPackage(msg, mm);
break;
case "Webapps:GetBasePath":
return this.webapps[msg.id].basePath;
break;
case "Webapps:RegisterForMessages":
this.addMessageListener(msg, mm);
break;
case "Webapps:UnregisterForMessages":
this.removeMessageListener(msg, mm);
break;
case "Webapps:GetList":
this.children.push(aMessage.target);
this.addMessageListener(["Webapps:AddApp", "Webapps:RemoveApp"], mm);
return this.webapps;
}
},
// 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;
}
this.children[aMsgName].forEach(function _doBroadcast(aMsgMgr) {
aMsgMgr.sendAsyncMessage(aMsgName, aContent);
});
},
_getAppDir: function(aId) {
return FileUtils.getDir(DIRECTORY_NAME, ["webapps", aId], true, true);
},
@ -396,7 +443,7 @@ let DOMApplicationRegistry = {
} catch(e) {
}
}
ppmm.broadcastAsyncMessage("Webapps:Install:Return:KO", aData);
aData.mm.sendAsyncMessage("Webapps:Install:Return:KO", aData);
},
confirmInstall: function(aData, aFromSync, aProfileDir, aOfflineCacheObserver) {
@ -458,11 +505,9 @@ let DOMApplicationRegistry = {
if (!aFromSync)
this._saveApps((function() {
ppmm.broadcastAsyncMessage("Webapps:Install:Return:OK", aData);
this.broadcastMessage("Webapps:Install:Return:OK", aData);
Services.obs.notifyObservers(this, "webapps-sync-install", appNote);
this.children.forEach(function(aMsgMgr) {
aMsgMgr.sendAsyncMessage("Webapps:AddApp", { id: id, app: appObject });
});
this.broadcastMessage("Webapps:AddApp", { id: id, app: appObject });
}).bind(this));
#ifdef MOZ_SYS_MSG
@ -539,7 +584,7 @@ let DOMApplicationRegistry = {
}).bind(this));
},
installPackage: function(aData) {
installPackage: function(aData, aMm) {
// Here are the steps when installing a package:
// - create a temp directory where to store the app.
// - download the zip in this directory.
@ -584,10 +629,10 @@ let DOMApplicationRegistry = {
try {
dir.remove(true);
} catch (e) { }
ppmm.broadcastAsyncMessage("Webapps:Install:Return:KO",
{ oid: aData.oid,
requestID: aData.requestID,
error: aError });
aMm.sendAsyncMessage("Webapps:Install:Return:KO",
{ oid: aData.oid,
requestID: aData.requestID,
error: aError });
}
function getInferedStatus() {
@ -725,20 +770,18 @@ let DOMApplicationRegistry = {
delete this.webapps[id];
this._saveApps((function() {
ppmm.broadcastAsyncMessage("Webapps:Uninstall:Return:OK", aData);
this.broadcastMessage("Webapps:Uninstall:Return:OK", aData);
Services.obs.notifyObservers(this, "webapps-sync-uninstall", appNote);
this.children.forEach(function(aMsgMgr) {
aMsgMgr.broadcastAsyncMessage("Webapps:RemoveApp", { id: id });
});
this.broadcastMessage("Webapps:RemoveApp", { id: id });
}).bind(this));
}
if (!found) {
ppmm.broadcastAsyncMessage("Webapps:Uninstall:Return:KO", aData);
aData.mm.broadcastMessage("Webapps:Uninstall:Return:KO", aData);
}
},
getSelf: function(aData) {
getSelf: function(aData, aMm) {
aData.apps = [];
let tmp = [];
let id = this._appId(aData.origin);
@ -752,11 +795,11 @@ let DOMApplicationRegistry = {
this._readManifests(tmp, (function(aResult) {
for (let i = 0; i < aResult.length; i++)
aData.apps[i].manifest = aResult[i].manifest;
ppmm.broadcastAsyncMessage("Webapps:GetSelf:Return:OK", aData);
aMm.sendAsyncMessage("Webapps:GetSelf:Return:OK", aData);
}).bind(this));
},
getInstalled: function(aData) {
getInstalled: function(aData, aMm) {
aData.apps = [];
let tmp = [];
@ -771,11 +814,11 @@ let DOMApplicationRegistry = {
this._readManifests(tmp, (function(aResult) {
for (let i = 0; i < aResult.length; i++)
aData.apps[i].manifest = aResult[i].manifest;
ppmm.broadcastAsyncMessage("Webapps:GetInstalled:Return:OK", aData);
aMm.sendAsyncMessage("Webapps:GetInstalled:Return:OK", aData);
}).bind(this));
},
getNotInstalled: function(aData) {
getNotInstalled: function(aData, aMm) {
aData.apps = [];
let tmp = [];
@ -789,11 +832,11 @@ let DOMApplicationRegistry = {
this._readManifests(tmp, (function(aResult) {
for (let i = 0; i < aResult.length; i++)
aData.apps[i].manifest = aResult[i].manifest;
ppmm.broadcastAsyncMessage("Webapps:GetNotInstalled:Return:OK", aData);
aMm.sendAsyncMessage("Webapps:GetNotInstalled:Return:OK", aData);
}).bind(this));
},
getAll: function(aData) {
getAll: function(aData, aMm) {
aData.apps = [];
let tmp = [];
@ -809,7 +852,7 @@ let DOMApplicationRegistry = {
this._readManifests(tmp, (function(aResult) {
for (let i = 0; i < aResult.length; i++)
aData.apps[i].manifest = aResult[i].manifest;
ppmm.broadcastAsyncMessage("Webapps:GetAll:Return:OK", aData);
aMm.sendAsyncMessage("Webapps:GetAll:Return:OK", aData);
}).bind(this));
},
@ -883,7 +926,7 @@ let DOMApplicationRegistry = {
dir.remove(true);
} catch (e) {
}
ppmm.broadcastAsyncMessage("Webapps:Uninstall:Return:OK", { origin: origin });
this.broadcastMessage("Webapps:Uninstall:Return:OK", { origin: origin });
} else {
if (this.webapps[record.id]) {
this.webapps[record.id] = record.value;
@ -891,7 +934,7 @@ let DOMApplicationRegistry = {
} else {
let data = { app: record.value };
this.confirmInstall(data, true);
ppmm.broadcastAsyncMessage("Webapps:Install:Return:OK", data);
this.broadcastMessage("Webapps:Install:Return:OK", data);
}
}
}
@ -997,7 +1040,9 @@ AppcacheObserver.prototype = {
let setStatus = function appObs_setStatus(aStatus) {
mustSave = (app.status != aStatus);
app.status = aStatus;
ppmm.broadcastAsyncMessage("Webapps:OfflineCache", { manifest: app.manifestURL, status: aStatus });
DOMApplicationRegistry.broadcastMessage("Webapps:OfflineCache",
{ manifest: app.manifestURL,
status: aStatus });
}
switch (aState) {

View File

@ -6160,6 +6160,7 @@ var WebappsUI = {
observe: function observe(aSubject, aTopic, aData) {
let data = {};
try { data = JSON.parse(aData); }
data.mm = aSubject;
catch(ex) { }
switch (aTopic) {
case "webapps-install-error":

View File

@ -24,6 +24,7 @@ let WebappsHandler = {
observe: function(subject, topic, data) {
data = JSON.parse(data);
data.mm = subject;
switch (topic) {
case "webapps-ask-install":

View File

@ -61,6 +61,7 @@ function becomeWebapp(manifestURL, parameters, onBecome) {
scope.DOMApplicationRegistry.confirmInstall(JSON.parse(data));
let installRecord = JSON.parse(data);
installRecord.mm = subj;
installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
WebappRT.config = installRecord;