2012-02-28 22:01:48 +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/. */
|
|
|
|
|
2012-07-03 18:00:53 +00:00
|
|
|
"use strict";
|
2012-02-28 22:01:48 +00:00
|
|
|
|
2012-08-22 21:34:57 +00:00
|
|
|
const DEBUG = false;
|
|
|
|
function debug(s) { dump("-*- Fallback ContactService component: " + s + "\n"); }
|
2012-02-28 22:01:48 +00:00
|
|
|
|
2013-02-03 12:05:51 +00:00
|
|
|
const Cu = Components.utils;
|
2012-02-28 22:01:48 +00:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMContactManager"];
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/ContactDB.jsm");
|
|
|
|
|
2012-08-27 14:13:02 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
2012-03-07 21:45:24 +00:00
|
|
|
|
2012-08-31 13:54:48 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "mRIL", function () {
|
2013-01-24 06:42:10 +00:00
|
|
|
let telephony = Cc["@mozilla.org/ril;1"];
|
2012-10-23 22:09:29 +00:00
|
|
|
if (!telephony) {
|
|
|
|
// Return a mock RIL because B2G Desktop build does not support telephony.
|
|
|
|
return {
|
|
|
|
getICCContacts: function(aContactType, aCallback) {
|
|
|
|
aCallback("!telephony", null, null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2013-01-24 06:42:10 +00:00
|
|
|
return telephony.getService(Ci.nsIRadioInterfaceLayer);
|
2012-08-31 13:54:48 +00:00
|
|
|
});
|
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
let myGlobal = this;
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.DOMContactManager = {
|
2012-02-28 22:01:48 +00:00
|
|
|
init: function() {
|
2012-08-22 21:34:57 +00:00
|
|
|
if (DEBUG) debug("Init");
|
2013-02-25 03:53:55 +00:00
|
|
|
this._messages = ["Contacts:Find", "Contacts:GetAll", "Contacts:Clear", "Contact:Save",
|
2013-02-01 20:44:55 +00:00
|
|
|
"Contact:Remove", "Contacts:GetSimContacts",
|
|
|
|
"Contacts:RegisterForMessages", "child-process-shutdown"];
|
|
|
|
this._children = [];
|
2013-02-03 12:05:51 +00:00
|
|
|
this._messages.forEach(function(msgName) {
|
2012-03-07 21:45:24 +00:00
|
|
|
ppmm.addMessageListener(msgName, this);
|
2013-02-03 12:05:51 +00:00
|
|
|
}.bind(this));
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
var idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"].getService(Ci.nsIIndexedDatabaseManager);
|
|
|
|
idbManager.initWindowless(myGlobal);
|
|
|
|
this._db = new ContactDB(myGlobal);
|
2012-04-26 22:10:04 +00:00
|
|
|
this._db.init(myGlobal);
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
Services.obs.addObserver(this, "profile-before-change", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
myGlobal = null;
|
2013-02-03 12:05:51 +00:00
|
|
|
this._messages.forEach(function(msgName) {
|
2012-03-07 21:45:24 +00:00
|
|
|
ppmm.removeMessageListener(msgName, this);
|
2013-02-03 12:05:51 +00:00
|
|
|
}.bind(this));
|
2012-02-28 22:01:48 +00:00
|
|
|
Services.obs.removeObserver(this, "profile-before-change");
|
2012-03-07 21:45:24 +00:00
|
|
|
ppmm = null;
|
2012-02-28 22:01:48 +00:00
|
|
|
this._messages = null;
|
|
|
|
if (this._db)
|
|
|
|
this._db.close();
|
2012-04-26 01:31:48 +00:00
|
|
|
this._db = null;
|
2012-02-28 22:01:48 +00:00
|
|
|
},
|
|
|
|
|
2012-11-01 00:26:05 +00:00
|
|
|
assertPermission: function(aMessage, aPerm) {
|
|
|
|
if (!aMessage.target.assertPermission(aPerm)) {
|
2013-02-03 12:05:51 +00:00
|
|
|
Cu.reportError("Contacts message " + aMessage.name +
|
2012-11-01 00:26:05 +00:00
|
|
|
" from a content process with no" + aPerm + " privileges.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2013-02-01 20:44:55 +00:00
|
|
|
broadcastMessage: function broadcastMessage(aMsgName, aContent) {
|
|
|
|
this._children.forEach(function(msgMgr) {
|
|
|
|
msgMgr.sendAsyncMessage(aMsgName, aContent);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
receiveMessage: function(aMessage) {
|
2013-02-03 12:05:51 +00:00
|
|
|
if (DEBUG) debug("receiveMessage " + aMessage.name);
|
2012-08-27 14:13:02 +00:00
|
|
|
let mm = aMessage.target;
|
2012-08-09 18:34:57 +00:00
|
|
|
let msg = aMessage.data;
|
2012-05-23 23:58:47 +00:00
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Contacts:Find":
|
2012-11-01 00:26:05 +00:00
|
|
|
if (!this.assertPermission(aMessage, "contacts-read")) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-03 12:05:51 +00:00
|
|
|
let result = [];
|
2012-02-28 22:01:48 +00:00
|
|
|
this._db.find(
|
|
|
|
function(contacts) {
|
2013-02-03 12:05:51 +00:00
|
|
|
for (let i in contacts) {
|
2012-02-28 22:01:48 +00:00
|
|
|
result.push(contacts[i]);
|
2012-04-02 23:39:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 21:34:57 +00:00
|
|
|
if (DEBUG) debug("result:" + JSON.stringify(result));
|
2012-07-25 17:27:27 +00:00
|
|
|
mm.sendAsyncMessage("Contacts:Find:Return:OK", {requestID: msg.requestID, contacts: result});
|
2012-02-28 22:01:48 +00:00
|
|
|
}.bind(this),
|
2013-02-03 12:05:51 +00:00
|
|
|
function(aErrorMsg) { mm.sendAsyncMessage("Contacts:Find:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this),
|
2012-08-09 18:34:57 +00:00
|
|
|
msg.options.findOptions);
|
2012-02-28 22:01:48 +00:00
|
|
|
break;
|
2013-02-03 12:05:51 +00:00
|
|
|
case "Contacts:GetAll":
|
|
|
|
if (!this.assertPermission(aMessage, "contacts-read")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this._db.getAll(
|
2013-02-23 11:41:41 +00:00
|
|
|
function(aContacts) {
|
|
|
|
mm.sendAsyncMessage("Contacts:GetAll:Next", {cursorId: msg.cursorId, contacts: aContacts});
|
2013-02-03 12:05:51 +00:00
|
|
|
},
|
|
|
|
function(aErrorMsg) { mm.sendAsyncMessage("Contacts:Find:Return:KO", { errorMsg: aErrorMsg }); },
|
2013-02-23 11:41:41 +00:00
|
|
|
msg.findOptions);
|
2013-02-03 12:05:51 +00:00
|
|
|
break;
|
2012-02-28 22:01:48 +00:00
|
|
|
case "Contact:Save":
|
2012-11-01 00:26:05 +00:00
|
|
|
if (msg.options.reason === "create") {
|
|
|
|
if (!this.assertPermission(aMessage, "contacts-create")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!this.assertPermission(aMessage, "contacts-write")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 18:42:41 +00:00
|
|
|
this._db.saveContact(
|
2012-08-09 18:34:57 +00:00
|
|
|
msg.options.contact,
|
2013-02-01 20:44:55 +00:00
|
|
|
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),
|
2012-07-25 17:27:27 +00:00
|
|
|
function(aErrorMsg) { mm.sendAsyncMessage("Contact:Save:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
|
2012-05-08 18:42:41 +00:00
|
|
|
);
|
2012-02-28 22:01:48 +00:00
|
|
|
break;
|
|
|
|
case "Contact:Remove":
|
2012-11-01 00:26:05 +00:00
|
|
|
if (!this.assertPermission(aMessage, "contacts-write")) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-05-08 18:42:41 +00:00
|
|
|
this._db.removeContact(
|
2012-08-09 18:34:57 +00:00
|
|
|
msg.options.id,
|
2013-02-01 20:44:55 +00:00
|
|
|
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),
|
2012-07-25 17:27:27 +00:00
|
|
|
function(aErrorMsg) { mm.sendAsyncMessage("Contact:Remove:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
|
2012-05-08 18:42:41 +00:00
|
|
|
);
|
2012-02-28 22:01:48 +00:00
|
|
|
break;
|
|
|
|
case "Contacts:Clear":
|
2012-11-01 00:26:05 +00:00
|
|
|
if (!this.assertPermission(aMessage, "contacts-write")) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-05-08 18:42:41 +00:00
|
|
|
this._db.clear(
|
2013-02-01 20:44:55 +00:00
|
|
|
function() {
|
|
|
|
mm.sendAsyncMessage("Contacts:Clear:Return:OK", { requestID: msg.requestID });
|
|
|
|
this.broadcastMessage("Contact:Changed", { reason: "remove" });
|
|
|
|
}.bind(this),
|
2012-07-25 17:27:27 +00:00
|
|
|
function(aErrorMsg) { mm.sendAsyncMessage("Contacts:Clear:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
|
2012-05-08 18:42:41 +00:00
|
|
|
);
|
2012-08-31 13:54:48 +00:00
|
|
|
break;
|
|
|
|
case "Contacts:GetSimContacts":
|
2012-11-01 00:26:05 +00:00
|
|
|
if (!this.assertPermission(aMessage, "contacts-read")) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-09-07 04:31:20 +00:00
|
|
|
mRIL.getICCContacts(
|
|
|
|
msg.options.contactType,
|
|
|
|
function (aErrorMsg, aType, aContacts) {
|
2012-09-27 17:37:39 +00:00
|
|
|
if (aErrorMsg !== 'undefined') {
|
2012-09-07 04:31:20 +00:00
|
|
|
mm.sendAsyncMessage("Contacts:GetSimContacts:Return:KO",
|
|
|
|
{requestID: msg.requestID,
|
|
|
|
errorMsg: aErrorMsg});
|
|
|
|
} else {
|
|
|
|
mm.sendAsyncMessage("Contacts:GetSimContacts:Return:OK",
|
|
|
|
{requestID: msg.requestID,
|
|
|
|
contacts: aContacts});
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-08-31 13:54:48 +00:00
|
|
|
break;
|
2013-02-01 20:44:55 +00:00
|
|
|
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;
|
2012-08-09 18:34:57 +00:00
|
|
|
default:
|
2012-08-22 21:34:57 +00:00
|
|
|
if (DEBUG) debug("WRONG MESSAGE NAME: " + aMessage.name);
|
2012-02-28 22:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMContactManager.init();
|