Bug 836423 - Contacts API: Fix oncontactchange function. r=bent

This commit is contained in:
Gregor Wagner 2013-02-01 12:44:55 -08:00
parent 2c3e399404
commit a3425a057a
2 changed files with 56 additions and 17 deletions

View File

@ -160,9 +160,7 @@ const CONTACT_CONTRACTID = "@mozilla.org/contact;1";
const CONTACT_CID = Components.ID("{da0f7040-388b-11e1-b86c-0800200c9a66}");
const nsIDOMContact = Components.interfaces.nsIDOMContact;
function Contact() {
if (DEBUG) debug("Contact constr: ");
};
function Contact() { };
Contact.prototype = {
__exposedProps__: {
@ -334,6 +332,9 @@ ContactManager.prototype = {
set oncontactchange(aCallback) {
if (DEBUG) debug("set oncontactchange");
let allowCallback = function() {
if (!this._oncontactchange) {
cpmm.sendAsyncMessage("Contacts:RegisterForMessages");
}
this._oncontactchange = aCallback;
}.bind(this);
let cancelCallback = function() {
@ -399,15 +400,6 @@ ContactManager.prototype = {
req = this.getRequest(msg.requestID);
if (req)
Services.DOMRequest.fireSuccess(req.request, null);
// Fire oncontactchange event
if (this._oncontactchange) {
let event = new this._window.MozContactChangeEvent("contactchanged", {
contactID: msg.contactID,
reason: req.reason
});
this._oncontactchange.handleEvent(event);
}
break;
case "Contacts:Find:Return:KO":
case "Contact:Save:Return:KO":
@ -431,7 +423,18 @@ ContactManager.prototype = {
req.cancel();
}
break;
default:
case "Contact:Changed":
// Fire oncontactchange event
if (DEBUG) debug("Contacts:ContactChanged: " + msg.contactID + ", " + msg.reason);
if (this._oncontactchange) {
let event = new this._window.MozContactChangeEvent("contactchanged", {
contactID: msg.contactID,
reason: msg.reason
});
this._oncontactchange.handleEvent(event);
}
break;
default:
if (DEBUG) debug("Wrong message: " + aMessage.name);
}
this.removeRequest(msg.requestID);
@ -599,6 +602,7 @@ ContactManager.prototype = {
"Contact:Remove:Return:OK", "Contact:Remove:Return:KO",
"Contacts:GetSimContacts:Return:OK",
"Contacts:GetSimContacts:Return:KO",
"Contact:Changed",
"PermissionPromptHelper:AskPermission:OK"]);
},

View File

@ -39,7 +39,10 @@ let myGlobal = this;
this.DOMContactManager = {
init: function() {
if (DEBUG) debug("Init");
this._messages = ["Contacts:Find", "Contacts:Clear", "Contact:Save", "Contact:Remove", "Contacts:GetSimContacts"];
this._messages = ["Contacts:Find", "Contacts:Clear", "Contact:Save",
"Contact:Remove", "Contacts:GetSimContacts",
"Contacts:RegisterForMessages", "child-process-shutdown"];
this._children = [];
this._messages.forEach((function(msgName) {
ppmm.addMessageListener(msgName, this);
}).bind(this));
@ -74,6 +77,12 @@ this.DOMContactManager = {
return true;
},
broadcastMessage: function broadcastMessage(aMsgName, aContent) {
this._children.forEach(function(msgMgr) {
msgMgr.sendAsyncMessage(aMsgName, aContent);
});
},
receiveMessage: function(aMessage) {
if (DEBUG) debug("Fallback DOMContactManager::receiveMessage " + aMessage.name);
let mm = aMessage.target;
@ -155,7 +164,10 @@ this.DOMContactManager = {
}
this._db.saveContact(
msg.options.contact,
function() { mm.sendAsyncMessage("Contact:Save:Return:OK", { requestID: msg.requestID, contactID: msg.options.contact.id }); }.bind(this),
function() {
mm.sendAsyncMessage("Contact:Save:Return:OK", { requestID: msg.requestID, contactID: msg.options.contact.id });
this.broadcastMessage("Contact:Changed", { contactID: msg.options.contact.id, reason: msg.options.reason });
}.bind(this),
function(aErrorMsg) { mm.sendAsyncMessage("Contact:Save:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
);
break;
@ -165,7 +177,10 @@ this.DOMContactManager = {
}
this._db.removeContact(
msg.options.id,
function() { mm.sendAsyncMessage("Contact:Remove:Return:OK", { requestID: msg.requestID, contactID: msg.options.id }); }.bind(this),
function() {
mm.sendAsyncMessage("Contact:Remove:Return:OK", { requestID: msg.requestID, contactID: msg.options.id });
this.broadcastMessage("Contact:Changed", { contactID: msg.options.id, reason: "remove" });
}.bind(this),
function(aErrorMsg) { mm.sendAsyncMessage("Contact:Remove:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
);
break;
@ -174,7 +189,10 @@ this.DOMContactManager = {
return null;
}
this._db.clear(
function() { mm.sendAsyncMessage("Contacts:Clear:Return:OK", { requestID: msg.requestID }); }.bind(this),
function() {
mm.sendAsyncMessage("Contacts:Clear:Return:OK", { requestID: msg.requestID });
this.broadcastMessage("Contact:Changed", { reason: "remove" });
}.bind(this),
function(aErrorMsg) { mm.sendAsyncMessage("Contacts:Clear:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
);
break;
@ -196,6 +214,23 @@ this.DOMContactManager = {
}
}.bind(this));
break;
case "Contacts:RegisterForMessages":
if (!aMessage.target.assertPermission("contacts-read")) {
return null;
}
if (DEBUG) debug("Register!");
if (this._children.indexOf(mm) == -1) {
this._children.push(mm);
}
break;
case "child-process-shutdown":
if (DEBUG) debug("Unregister");
let index = this._children.indexOf(mm);
if (index != -1) {
if (DEBUG) debug("Unregister index: " + index);
this._children.splice(index, 1);
}
break;
default:
if (DEBUG) debug("WRONG MESSAGE NAME: " + aMessage.name);
}