2012-02-28 14:01:48 -08: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 11:00:53 -07:00
|
|
|
"use strict";
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2013-07-01 23:14:32 -07:00
|
|
|
const DEBUG = false;
|
2012-08-22 14:34:57 -07:00
|
|
|
function debug(s) { dump("-*- ContactManager: " + s + "\n"); }
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-03-07 13:45:24 -08:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "DOMRequest",
|
|
|
|
"@mozilla.org/dom/dom-request-service;1",
|
|
|
|
"nsIDOMRequestService");
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2013-03-27 15:24:15 -07:00
|
|
|
const CONTACTS_SENDMORE_MINIMUM = 5;
|
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
// We need this to create a copy of the mozContact object in ContactManager.save
|
|
|
|
// Keep in sync with the interfaces.
|
|
|
|
const PROPERTIES = [
|
|
|
|
"name", "honorificPrefix", "givenName", "additionalName", "familyName",
|
2014-04-06 18:14:00 -07:00
|
|
|
"phoneticGivenName", "phoneticFamilyName",
|
2013-10-17 14:29:56 -07:00
|
|
|
"honorificSuffix", "nickname", "photo", "category", "org", "jobTitle",
|
2013-12-29 09:41:35 -08:00
|
|
|
"bday", "note", "anniversary", "sex", "genderIdentity", "key", "adr", "email",
|
|
|
|
"url", "impp", "tel"
|
2013-10-17 14:29:56 -07:00
|
|
|
];
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2014-01-17 10:00:35 -08:00
|
|
|
let mozContactInitWarned = false;
|
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
function Contact() { }
|
2013-06-03 18:50:31 -07:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
Contact.prototype = {
|
|
|
|
__init: function(aProp) {
|
2013-12-29 09:41:35 -08:00
|
|
|
for (let prop in aProp) {
|
|
|
|
this[prop] = aProp[prop];
|
|
|
|
}
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
2014-01-17 10:00:35 -08:00
|
|
|
init: function(aProp) {
|
|
|
|
// init is deprecated, warn once in the console if it's used
|
|
|
|
if (!mozContactInitWarned) {
|
|
|
|
mozContactInitWarned = true;
|
|
|
|
Cu.reportError("mozContact.init is DEPRECATED. Use the mozContact constructor instead. " +
|
|
|
|
"See https://developer.mozilla.org/docs/WebAPI/Contacts for details.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let prop of PROPERTIES) {
|
|
|
|
this[prop] = aProp[prop];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
setMetadata: function(aId, aPublished, aUpdated) {
|
|
|
|
this.id = aId;
|
|
|
|
if (aPublished) {
|
|
|
|
this.published = aPublished;
|
|
|
|
}
|
|
|
|
if (aUpdated) {
|
|
|
|
this.updated = aUpdated;
|
|
|
|
}
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
2012-05-10 21:51:48 -07:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
classID: Components.ID("{72a5ee28-81d8-4af8-90b3-ae935396cc66}"),
|
|
|
|
contractID: "@mozilla.org/contact;1",
|
2013-12-29 09:41:35 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
|
2013-10-17 14:29:56 -07:00
|
|
|
};
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
function ContactManager() { }
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
ContactManager.prototype = {
|
2012-03-07 13:45:24 -08:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2013-10-17 14:29:56 -07:00
|
|
|
hasListenPermission: false,
|
2013-07-18 15:01:00 -07:00
|
|
|
_cachedContacts: [] ,
|
2012-05-08 11:42:41 -07:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
set oncontactchange(aHandler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oncontactchange", aHandler);
|
2012-05-08 11:42:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get oncontactchange() {
|
2013-10-17 14:29:56 -07:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("oncontactchange");
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
_convertContact: function(aContact) {
|
2014-10-08 09:15:22 -07:00
|
|
|
let contact = Cu.cloneInto(aContact, this._window);
|
|
|
|
let newContact = new this._window.mozContact(contact.properties);
|
|
|
|
newContact.setMetadata(contact.id, contact.published, contact.updated);
|
2013-02-03 04:05:51 -08:00
|
|
|
return newContact;
|
|
|
|
},
|
|
|
|
|
|
|
|
_convertContacts: function(aContacts) {
|
2013-12-09 13:01:42 -08:00
|
|
|
let contacts = new this._window.Array();
|
2012-02-28 14:01:48 -08:00
|
|
|
for (let i in aContacts) {
|
2013-02-03 04:05:51 -08:00
|
|
|
contacts.push(this._convertContact(aContacts[i]));
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
return contacts;
|
|
|
|
},
|
|
|
|
|
2013-02-24 19:53:55 -08:00
|
|
|
_fireSuccessOrDone: function(aCursor, aResult) {
|
|
|
|
if (aResult == null) {
|
|
|
|
Services.DOMRequest.fireDone(aCursor);
|
|
|
|
} else {
|
|
|
|
Services.DOMRequest.fireSuccess(aCursor, aResult);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-02-23 03:41:41 -08:00
|
|
|
_pushArray: function(aArr1, aArr2) {
|
|
|
|
aArr1.push.apply(aArr1, aArr2);
|
|
|
|
},
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
receiveMessage: function(aMessage) {
|
2013-02-03 04:05:51 -08:00
|
|
|
if (DEBUG) debug("receiveMessage: " + aMessage.name);
|
2012-02-28 14:01:48 -08:00
|
|
|
let msg = aMessage.json;
|
|
|
|
let contacts = msg.contacts;
|
|
|
|
|
2012-08-31 06:54:48 -07:00
|
|
|
let req;
|
2012-02-28 14:01:48 -08:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Contacts:Find:Return:OK":
|
2012-08-31 06:54:48 -07:00
|
|
|
req = this.getRequest(msg.requestID);
|
2012-02-28 14:01:48 -08:00
|
|
|
if (req) {
|
2013-02-03 04:05:51 -08:00
|
|
|
let result = this._convertContacts(contacts);
|
2012-05-08 11:42:41 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, result);
|
2012-02-28 14:01:48 -08:00
|
|
|
} else {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("no request stored!" + msg.requestID);
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
break;
|
2013-02-03 04:05:51 -08:00
|
|
|
case "Contacts:GetAll:Next":
|
2013-04-18 10:44:38 -07:00
|
|
|
let data = this.getRequest(msg.cursorId);
|
|
|
|
if (!data) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-23 03:41:41 -08:00
|
|
|
let result = contacts ? this._convertContacts(contacts) : [null];
|
2013-02-24 19:53:55 -08:00
|
|
|
if (data.waitingForNext) {
|
|
|
|
if (DEBUG) debug("cursor waiting for contact, sending");
|
|
|
|
data.waitingForNext = false;
|
2013-02-23 03:41:41 -08:00
|
|
|
let contact = result.shift();
|
|
|
|
this._pushArray(data.cachedContacts, result);
|
2013-03-15 20:40:03 -07:00
|
|
|
this.nextTick(this._fireSuccessOrDone.bind(this, data.cursor, contact));
|
2013-05-03 09:15:55 -07:00
|
|
|
if (!contact) {
|
|
|
|
this.removeRequest(msg.cursorId);
|
|
|
|
}
|
2013-02-03 04:05:51 -08:00
|
|
|
} else {
|
2013-02-24 19:53:55 -08:00
|
|
|
if (DEBUG) debug("cursor not waiting, saving");
|
2013-02-23 03:41:41 -08:00
|
|
|
this._pushArray(data.cachedContacts, result);
|
2013-02-03 04:05:51 -08:00
|
|
|
}
|
|
|
|
break;
|
2012-02-28 14:01:48 -08:00
|
|
|
case "Contact:Save:Return:OK":
|
2013-07-18 15:01:00 -07:00
|
|
|
// If a cached contact was saved and a new contact ID was returned, update the contact's ID
|
|
|
|
if (this._cachedContacts[msg.requestID]) {
|
|
|
|
if (msg.contactID) {
|
|
|
|
this._cachedContacts[msg.requestID].id = msg.contactID;
|
|
|
|
}
|
|
|
|
delete this._cachedContacts[msg.requestID];
|
|
|
|
}
|
2012-02-28 14:01:48 -08:00
|
|
|
case "Contacts:Clear:Return:OK":
|
|
|
|
case "Contact:Remove:Return:OK":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req)
|
2012-05-08 11:42:41 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, null);
|
2012-02-28 14:01:48 -08:00
|
|
|
break;
|
|
|
|
case "Contacts:Find:Return:KO":
|
|
|
|
case "Contact:Save:Return:KO":
|
|
|
|
case "Contact:Remove:Return:KO":
|
|
|
|
case "Contacts:Clear:Return:KO":
|
2013-07-15 10:16:33 -07:00
|
|
|
case "Contacts:GetRevision:Return:KO":
|
|
|
|
case "Contacts:Count:Return:KO":
|
2012-02-28 14:01:48 -08:00
|
|
|
req = this.getRequest(msg.requestID);
|
2013-07-18 15:01:00 -07:00
|
|
|
if (req) {
|
|
|
|
if (req.request) {
|
|
|
|
req = req.request;
|
|
|
|
}
|
|
|
|
Services.DOMRequest.fireError(req, msg.errorMsg);
|
|
|
|
}
|
2012-02-28 14:01:48 -08:00
|
|
|
break;
|
2013-08-22 12:05:01 -07:00
|
|
|
case "Contacts:GetAll:Return:KO":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
|
|
|
Services.DOMRequest.fireError(req.cursor, msg.errorMsg);
|
|
|
|
}
|
|
|
|
break;
|
2013-02-01 12:44:55 -08:00
|
|
|
case "Contact:Changed":
|
|
|
|
// Fire oncontactchange event
|
|
|
|
if (DEBUG) debug("Contacts:ContactChanged: " + msg.contactID + ", " + msg.reason);
|
2013-10-17 14:29:56 -07:00
|
|
|
let event = new this._window.MozContactChangeEvent("contactchange", {
|
|
|
|
contactID: msg.contactID,
|
|
|
|
reason: msg.reason
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2013-02-01 12:44:55 -08:00
|
|
|
break;
|
2013-04-25 10:15:47 -07:00
|
|
|
case "Contacts:Revision":
|
|
|
|
if (DEBUG) debug("new revision: " + msg.revision);
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
2013-08-22 12:05:01 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, msg.revision);
|
2013-04-25 10:15:47 -07:00
|
|
|
}
|
|
|
|
break;
|
2013-06-03 20:45:10 -07:00
|
|
|
case "Contacts:Count":
|
|
|
|
if (DEBUG) debug("count: " + msg.count);
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
2013-08-22 12:05:01 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, msg.count);
|
2013-06-03 20:45:10 -07:00
|
|
|
}
|
|
|
|
break;
|
2013-02-01 12:44:55 -08:00
|
|
|
default:
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("Wrong message: " + aMessage.name);
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
this.removeRequest(msg.requestID);
|
|
|
|
},
|
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
dispatchEvent: function(event) {
|
|
|
|
if (this.hasListenPermission) {
|
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-13 11:57:59 -07:00
|
|
|
askPermission: function (aAccess, aRequest, aAllowCallback, aCancelCallback) {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("askPermission for contacts");
|
2014-07-07 21:45:23 -07:00
|
|
|
|
2012-10-31 17:26:05 -07:00
|
|
|
let access;
|
|
|
|
switch(aAccess) {
|
|
|
|
case "create":
|
|
|
|
access = "create";
|
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
case "remove":
|
|
|
|
access = "write";
|
|
|
|
break;
|
|
|
|
case "find":
|
|
|
|
case "listen":
|
2013-04-25 10:15:47 -07:00
|
|
|
case "revision":
|
2013-06-03 20:45:10 -07:00
|
|
|
case "count":
|
2012-10-31 17:26:05 -07:00
|
|
|
access = "read";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
access = "unknown";
|
|
|
|
}
|
2013-02-03 04:05:51 -08:00
|
|
|
|
2013-04-17 04:51:51 -07:00
|
|
|
// Shortcut for ALLOW_ACTION so we avoid a parent roundtrip
|
2014-07-07 21:45:23 -07:00
|
|
|
let principal = this._window.document.nodePrincipal;
|
2013-04-17 04:51:51 -07:00
|
|
|
let type = "contacts-" + access;
|
|
|
|
let permValue =
|
2014-07-07 21:45:23 -07:00
|
|
|
Services.perms.testExactPermissionFromPrincipal(principal, type);
|
2014-10-20 11:05:54 -07:00
|
|
|
DEBUG && debug("Existing permission " + permValue);
|
2013-04-17 04:51:51 -07:00
|
|
|
if (permValue == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
2014-07-14 10:31:16 -07:00
|
|
|
if (aAllowCallback) {
|
|
|
|
aAllowCallback();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (permValue == Ci.nsIPermissionManager.DENY_ACTION ||
|
|
|
|
permValue == Ci.nsIPermissionManager.UNKNOWN_ACTION) {
|
|
|
|
if (aCancelCallback) {
|
2014-10-20 11:05:54 -07:00
|
|
|
aCancelCallback("PERMISSION_DENIED");
|
2014-07-14 10:31:16 -07:00
|
|
|
}
|
2013-04-17 04:51:51 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-07 21:45:23 -07:00
|
|
|
// Create an array with a single nsIContentPermissionType element.
|
Bug 1001090 - Part 4: Fix errors in chrome code. (r=zombie,gavin,fitzgen,dcamp,bgrins,fabrice,gwagner,margaret,mrbkap,mak,njn,vicamo)
2014-09-15 16:30:46 -07:00
|
|
|
type = {
|
2012-08-09 11:34:57 -07:00
|
|
|
type: "contacts",
|
2012-10-31 17:26:05 -07:00
|
|
|
access: access,
|
2014-07-14 10:31:15 -07:00
|
|
|
options: [],
|
2014-07-07 21:45:23 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionType])
|
|
|
|
};
|
|
|
|
let typeArray = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
|
|
|
|
typeArray.appendElement(type, false);
|
|
|
|
|
|
|
|
// create a nsIContentPermissionRequest
|
|
|
|
let request = {
|
|
|
|
types: typeArray,
|
|
|
|
principal: principal,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionRequest]),
|
2014-10-20 11:05:54 -07:00
|
|
|
allow: function() {
|
|
|
|
aAllowCallback && aAllowCallback();
|
|
|
|
DEBUG && debug("Permission granted. Access " + access +"\n");
|
|
|
|
},
|
|
|
|
cancel: function() {
|
|
|
|
aCancelCallback && aCancelCallback("PERMISSION_DENIED");
|
|
|
|
DEBUG && debug("Permission denied. Access " + access +"\n");
|
|
|
|
},
|
2014-07-07 21:45:23 -07:00
|
|
|
window: this._window
|
|
|
|
};
|
|
|
|
|
|
|
|
// Using askPermission from nsIDOMWindowUtils that takes care of the
|
|
|
|
// remoting if needed.
|
|
|
|
let windowUtils = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
windowUtils.askPermission(request);
|
2012-08-09 11:34:57 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
save: function save(aContact) {
|
2013-10-17 14:29:56 -07:00
|
|
|
// We have to do a deep copy of the contact manually here because
|
|
|
|
// nsFrameMessageManager doesn't know how to create a structured clone of a
|
|
|
|
// mozContact object.
|
|
|
|
let newContact = {properties: {}};
|
|
|
|
|
2013-12-29 09:41:35 -08:00
|
|
|
try {
|
|
|
|
for (let field of PROPERTIES) {
|
|
|
|
// This hack makes sure modifications to the sequence attributes get validated.
|
|
|
|
aContact[field] = aContact[field];
|
2013-10-17 14:29:56 -07:00
|
|
|
newContact.properties[field] = aContact[field];
|
|
|
|
}
|
2013-12-29 09:41:35 -08:00
|
|
|
} catch (e) {
|
|
|
|
// And then make sure we throw a proper error message (no internal file and line #)
|
|
|
|
throw new this._window.DOMError(e.name, e.message);
|
2013-10-17 14:29:56 -07:00
|
|
|
}
|
|
|
|
|
2013-07-18 15:01:00 -07:00
|
|
|
let request = this.createRequest();
|
2013-12-29 09:41:35 -08:00
|
|
|
let requestID = this.getRequestId({request: request});
|
2012-08-09 11:34:57 -07:00
|
|
|
|
|
|
|
let reason;
|
|
|
|
if (aContact.id == "undefined") {
|
|
|
|
// for example {25c00f01-90e5-c545-b4d4-21E2ddbab9e0} becomes
|
|
|
|
// 25c00f0190e5c545b4d421E2ddbab9e0
|
2013-10-17 14:29:56 -07:00
|
|
|
aContact.id = this._getRandomId().replace(/[{}-]/g, "");
|
2013-07-18 15:01:00 -07:00
|
|
|
// Cache the contact so that its ID may be updated later if necessary
|
|
|
|
this._cachedContacts[requestID] = aContact;
|
2012-08-09 11:34:57 -07:00
|
|
|
reason = "create";
|
2012-02-28 14:01:48 -08:00
|
|
|
} else {
|
2012-08-09 11:34:57 -07:00
|
|
|
reason = "update";
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
2012-08-09 11:34:57 -07:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
newContact.id = aContact.id;
|
|
|
|
newContact.published = aContact.published;
|
|
|
|
newContact.updated = aContact.updated;
|
|
|
|
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("send: " + JSON.stringify(newContact));
|
2013-10-17 14:29:56 -07:00
|
|
|
|
2012-10-31 17:26:05 -07:00
|
|
|
let options = { contact: newContact, reason: reason };
|
2012-08-09 11:34:57 -07:00
|
|
|
let allowCallback = function() {
|
2014-10-20 11:05:54 -07:00
|
|
|
cpmm.sendAsyncMessage("Contact:Save", {
|
|
|
|
requestID: requestID,
|
|
|
|
options: options
|
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(request, reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission(reason, request, allowCallback, cancelCallback);
|
2012-08-09 11:34:57 -07:00
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(aOptions) {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("find! " + JSON.stringify(aOptions));
|
2013-02-03 04:05:51 -08:00
|
|
|
let request = this.createRequest();
|
2012-08-09 11:34:57 -07:00
|
|
|
let options = { findOptions: aOptions };
|
2014-10-20 11:05:54 -07:00
|
|
|
|
2012-08-09 11:34:57 -07:00
|
|
|
let allowCallback = function() {
|
2014-10-20 11:05:54 -07:00
|
|
|
cpmm.sendAsyncMessage("Contacts:Find", {
|
|
|
|
requestID: this.getRequestId({request: request, reason: "find"}),
|
|
|
|
options: options
|
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(request, reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("find", request, allowCallback, cancelCallback);
|
2012-08-09 11:34:57 -07:00
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
createCursor: function CM_createCursor(aRequest) {
|
2013-02-24 19:53:55 -08:00
|
|
|
let data = {
|
|
|
|
cursor: Services.DOMRequest.createCursor(this._window, function() {
|
|
|
|
this.handleContinue(id);
|
|
|
|
}.bind(this)),
|
|
|
|
cachedContacts: [],
|
|
|
|
waitingForNext: true,
|
|
|
|
};
|
2013-04-18 10:44:38 -07:00
|
|
|
let id = this.getRequestId(data);
|
2013-02-03 04:05:51 -08:00
|
|
|
if (DEBUG) debug("saved cursor id: " + id);
|
2013-02-24 19:53:55 -08:00
|
|
|
return [id, data.cursor];
|
2013-02-03 04:05:51 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
getAll: function CM_getAll(aOptions) {
|
|
|
|
if (DEBUG) debug("getAll: " + JSON.stringify(aOptions));
|
|
|
|
let [cursorId, cursor] = this.createCursor();
|
2014-10-20 11:05:54 -07:00
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetAll", {
|
2014-10-20 11:05:54 -07:00
|
|
|
cursorId: cursorId,
|
|
|
|
findOptions: aOptions
|
|
|
|
});
|
2013-02-03 04:05:51 -08:00
|
|
|
}.bind(this);
|
2014-10-20 11:05:54 -07:00
|
|
|
|
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(cursor, reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("find", cursor, allowCallback, cancelCallback);
|
2013-02-03 04:05:51 -08:00
|
|
|
return cursor;
|
|
|
|
},
|
|
|
|
|
2013-03-15 20:40:03 -07:00
|
|
|
nextTick: function nextTick(aCallback) {
|
|
|
|
Services.tm.currentThread.dispatch(aCallback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
handleContinue: function CM_handleContinue(aCursorId) {
|
|
|
|
if (DEBUG) debug("handleContinue: " + aCursorId);
|
2013-04-18 10:44:38 -07:00
|
|
|
let data = this.getRequest(aCursorId);
|
2013-02-24 19:53:55 -08:00
|
|
|
if (data.cachedContacts.length > 0) {
|
|
|
|
if (DEBUG) debug("contact in cache");
|
2013-02-23 03:41:41 -08:00
|
|
|
let contact = data.cachedContacts.shift();
|
2013-03-15 20:40:03 -07:00
|
|
|
this.nextTick(this._fireSuccessOrDone.bind(this, data.cursor, contact));
|
2013-05-03 09:15:55 -07:00
|
|
|
if (!contact) {
|
|
|
|
this.removeRequest(aCursorId);
|
|
|
|
} else if (data.cachedContacts.length === CONTACTS_SENDMORE_MINIMUM) {
|
2013-03-27 15:24:15 -07:00
|
|
|
cpmm.sendAsyncMessage("Contacts:GetAll:SendNow", { cursorId: aCursorId });
|
|
|
|
}
|
2013-02-24 19:53:55 -08:00
|
|
|
} else {
|
|
|
|
if (DEBUG) debug("waiting for contact");
|
|
|
|
data.waitingForNext = true;
|
|
|
|
}
|
2013-02-03 04:05:51 -08:00
|
|
|
},
|
|
|
|
|
2014-01-29 05:20:40 -08:00
|
|
|
remove: function removeContact(aRecordOrId) {
|
2013-08-22 12:05:01 -07:00
|
|
|
let request = this.createRequest();
|
2014-01-29 05:20:40 -08:00
|
|
|
let id;
|
|
|
|
if (typeof aRecordOrId === "string") {
|
|
|
|
id = aRecordOrId;
|
|
|
|
} else if (!aRecordOrId || !aRecordOrId.id) {
|
2013-08-22 12:05:01 -07:00
|
|
|
Services.DOMRequest.fireErrorAsync(request, true);
|
|
|
|
return request;
|
2014-01-29 05:20:40 -08:00
|
|
|
} else {
|
|
|
|
id = aRecordOrId.id;
|
2013-08-22 12:05:01 -07:00
|
|
|
}
|
|
|
|
|
2014-01-29 05:20:40 -08:00
|
|
|
let options = { id: id };
|
2014-10-20 11:05:54 -07:00
|
|
|
|
2012-08-09 11:34:57 -07:00
|
|
|
let allowCallback = function() {
|
2014-10-20 11:05:54 -07:00
|
|
|
cpmm.sendAsyncMessage("Contact:Remove", {
|
|
|
|
requestID: this.getRequestId({request: request, reason: "remove"}),
|
|
|
|
options: options
|
|
|
|
});
|
2013-10-17 14:29:56 -07:00
|
|
|
}.bind(this);
|
2014-10-20 11:05:54 -07:00
|
|
|
|
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(request, reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("remove", request, allowCallback, cancelCallback);
|
2012-08-09 11:34:57 -07:00
|
|
|
return request;
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("clear");
|
2013-10-17 14:29:56 -07:00
|
|
|
let request = this.createRequest();
|
2012-08-09 11:34:57 -07:00
|
|
|
let options = {};
|
2014-10-20 11:05:54 -07:00
|
|
|
|
2012-08-09 11:34:57 -07:00
|
|
|
let allowCallback = function() {
|
2014-10-20 11:05:54 -07:00
|
|
|
cpmm.sendAsyncMessage("Contacts:Clear", {
|
|
|
|
requestID: this.getRequestId({request: request, reason: "remove"}),
|
|
|
|
options: options
|
|
|
|
});
|
2013-10-17 14:29:56 -07:00
|
|
|
}.bind(this);
|
2014-10-20 11:05:54 -07:00
|
|
|
|
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(request, reason);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("remove", request, allowCallback, cancelCallback);
|
2012-08-09 11:34:57 -07:00
|
|
|
return request;
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
2013-04-25 10:15:47 -07:00
|
|
|
getRevision: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetRevision", {
|
2013-08-22 12:05:01 -07:00
|
|
|
requestID: this.getRequestId({ request: request })
|
2013-04-25 10:15:47 -07:00
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
|
2014-10-20 11:05:54 -07:00
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(request, reason);
|
2013-04-25 10:15:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("revision", request, allowCallback, cancelCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2013-06-03 20:45:10 -07:00
|
|
|
getCount: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetCount", {
|
2013-08-22 12:05:01 -07:00
|
|
|
requestID: this.getRequestId({ request: request })
|
2013-06-03 20:45:10 -07:00
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
|
2014-10-20 11:05:54 -07:00
|
|
|
let cancelCallback = function(reason) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(request, reason);
|
2013-06-03 20:45:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("count", request, allowCallback, cancelCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
init: function(aWindow) {
|
2013-10-17 14:29:56 -07:00
|
|
|
// DOMRequestIpcHelper.initHelper sets this._window
|
2013-07-08 14:55:42 -07:00
|
|
|
this.initDOMRequestHelper(aWindow, ["Contacts:Find:Return:OK", "Contacts:Find:Return:KO",
|
2012-05-10 21:51:48 -07:00
|
|
|
"Contacts:Clear:Return:OK", "Contacts:Clear:Return:KO",
|
|
|
|
"Contact:Save:Return:OK", "Contact:Save:Return:KO",
|
2012-08-09 11:34:57 -07:00
|
|
|
"Contact:Remove:Return:OK", "Contact:Remove:Return:KO",
|
2013-02-01 12:44:55 -08:00
|
|
|
"Contact:Changed",
|
2013-08-22 12:05:01 -07:00
|
|
|
"Contacts:GetAll:Next", "Contacts:GetAll:Return:KO",
|
|
|
|
"Contacts:Count",
|
2013-07-18 15:01:00 -07:00
|
|
|
"Contacts:Revision", "Contacts:GetRevision:Return:KO",]);
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2012-05-08 11:42:41 -07:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:RegisterForMessages");
|
|
|
|
this.hasListenPermission = true;
|
|
|
|
}.bind(this);
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
this.askPermission("listen", null, allowCallback);
|
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{8beb3a66-d70a-4111-b216-b8e995ad3aff}"),
|
|
|
|
contractID: "@mozilla.org/contactManager;1",
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
|
2013-11-19 21:33:10 -08:00
|
|
|
Ci.nsIObserver,
|
2013-10-17 14:29:56 -07:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
};
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2013-10-17 14:29:56 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([
|
2013-12-29 09:41:35 -08:00
|
|
|
Contact, ContactManager
|
2013-10-17 14:29:56 -07:00
|
|
|
]);
|