2012-02-29 02:01:48 +04: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 22:00:53 +04:00
|
|
|
"use strict";
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2013-07-02 10:14:32 +04:00
|
|
|
const DEBUG = false;
|
2012-08-23 01:34:57 +04:00
|
|
|
function debug(s) { dump("-*- ContactManager: " + s + "\n"); }
|
2012-02-29 02:01:48 +04: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-08 01:45:24 +04:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "DOMRequest",
|
|
|
|
"@mozilla.org/dom/dom-request-service;1",
|
|
|
|
"nsIDOMRequestService");
|
|
|
|
|
2012-08-27 18:13:02 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2013-03-28 02:24:15 +04:00
|
|
|
const CONTACTS_SENDMORE_MINIMUM = 5;
|
|
|
|
|
2013-10-18 01:29:56 +04: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-07 05:14:00 +04:00
|
|
|
"phoneticGivenName", "phoneticFamilyName",
|
2013-10-18 01:29:56 +04:00
|
|
|
"honorificSuffix", "nickname", "photo", "category", "org", "jobTitle",
|
2013-12-29 21:41:35 +04:00
|
|
|
"bday", "note", "anniversary", "sex", "genderIdentity", "key", "adr", "email",
|
|
|
|
"url", "impp", "tel"
|
2013-10-18 01:29:56 +04:00
|
|
|
];
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2014-01-17 22:00:35 +04:00
|
|
|
let mozContactInitWarned = false;
|
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
function Contact() { }
|
2013-06-04 05:50:31 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
Contact.prototype = {
|
|
|
|
__init: function(aProp) {
|
2013-12-29 21:41:35 +04:00
|
|
|
for (let prop in aProp) {
|
|
|
|
this[prop] = aProp[prop];
|
|
|
|
}
|
2012-02-29 02:01:48 +04:00
|
|
|
},
|
|
|
|
|
2014-01-17 22:00:35 +04: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-18 01:29:56 +04:00
|
|
|
setMetadata: function(aId, aPublished, aUpdated) {
|
|
|
|
this.id = aId;
|
|
|
|
if (aPublished) {
|
|
|
|
this.published = aPublished;
|
|
|
|
}
|
|
|
|
if (aUpdated) {
|
|
|
|
this.updated = aUpdated;
|
|
|
|
}
|
2012-02-29 02:01:48 +04:00
|
|
|
},
|
2012-05-11 08:51:48 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
classID: Components.ID("{72a5ee28-81d8-4af8-90b3-ae935396cc66}"),
|
|
|
|
contractID: "@mozilla.org/contact;1",
|
2013-12-29 21:41:35 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
|
2013-10-18 01:29:56 +04:00
|
|
|
};
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
function ContactManager() { }
|
2012-02-29 02:01:48 +04:00
|
|
|
|
|
|
|
ContactManager.prototype = {
|
2012-03-08 01:45:24 +04:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2013-10-18 01:29:56 +04:00
|
|
|
hasListenPermission: false,
|
2013-07-19 02:01:00 +04:00
|
|
|
_cachedContacts: [] ,
|
2012-05-08 22:42:41 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
set oncontactchange(aHandler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oncontactchange", aHandler);
|
2012-05-08 22:42:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get oncontactchange() {
|
2013-10-18 01:29:56 +04:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("oncontactchange");
|
2012-02-29 02:01:48 +04:00
|
|
|
},
|
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
_convertContact: function(aContact) {
|
|
|
|
let newContact = new this._window.mozContact(aContact.properties);
|
|
|
|
newContact.setMetadata(aContact.id, aContact.published, aContact.updated);
|
2013-02-03 16:05:51 +04:00
|
|
|
return newContact;
|
|
|
|
},
|
|
|
|
|
|
|
|
_convertContacts: function(aContacts) {
|
2013-12-10 01:01:42 +04:00
|
|
|
let contacts = new this._window.Array();
|
2012-02-29 02:01:48 +04:00
|
|
|
for (let i in aContacts) {
|
2013-02-03 16:05:51 +04:00
|
|
|
contacts.push(this._convertContact(aContacts[i]));
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
|
|
|
return contacts;
|
|
|
|
},
|
|
|
|
|
2013-02-25 07:53:55 +04:00
|
|
|
_fireSuccessOrDone: function(aCursor, aResult) {
|
|
|
|
if (aResult == null) {
|
|
|
|
Services.DOMRequest.fireDone(aCursor);
|
|
|
|
} else {
|
|
|
|
Services.DOMRequest.fireSuccess(aCursor, aResult);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-02-23 15:41:41 +04:00
|
|
|
_pushArray: function(aArr1, aArr2) {
|
|
|
|
aArr1.push.apply(aArr1, aArr2);
|
|
|
|
},
|
|
|
|
|
2012-02-29 02:01:48 +04:00
|
|
|
receiveMessage: function(aMessage) {
|
2013-02-03 16:05:51 +04:00
|
|
|
if (DEBUG) debug("receiveMessage: " + aMessage.name);
|
2012-02-29 02:01:48 +04:00
|
|
|
let msg = aMessage.json;
|
|
|
|
let contacts = msg.contacts;
|
|
|
|
|
2012-08-31 17:54:48 +04:00
|
|
|
let req;
|
2012-02-29 02:01:48 +04:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Contacts:Find:Return:OK":
|
2012-08-31 17:54:48 +04:00
|
|
|
req = this.getRequest(msg.requestID);
|
2012-02-29 02:01:48 +04:00
|
|
|
if (req) {
|
2013-02-03 16:05:51 +04:00
|
|
|
let result = this._convertContacts(contacts);
|
2012-05-08 22:42:41 +04:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, result);
|
2012-02-29 02:01:48 +04:00
|
|
|
} else {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("no request stored!" + msg.requestID);
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-02-03 16:05:51 +04:00
|
|
|
case "Contacts:GetAll:Next":
|
2013-04-18 21:44:38 +04:00
|
|
|
let data = this.getRequest(msg.cursorId);
|
|
|
|
if (!data) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-23 15:41:41 +04:00
|
|
|
let result = contacts ? this._convertContacts(contacts) : [null];
|
2013-02-25 07:53:55 +04:00
|
|
|
if (data.waitingForNext) {
|
|
|
|
if (DEBUG) debug("cursor waiting for contact, sending");
|
|
|
|
data.waitingForNext = false;
|
2013-02-23 15:41:41 +04:00
|
|
|
let contact = result.shift();
|
|
|
|
this._pushArray(data.cachedContacts, result);
|
2013-03-16 07:40:03 +04:00
|
|
|
this.nextTick(this._fireSuccessOrDone.bind(this, data.cursor, contact));
|
2013-05-03 20:15:55 +04:00
|
|
|
if (!contact) {
|
|
|
|
this.removeRequest(msg.cursorId);
|
|
|
|
}
|
2013-02-03 16:05:51 +04:00
|
|
|
} else {
|
2013-02-25 07:53:55 +04:00
|
|
|
if (DEBUG) debug("cursor not waiting, saving");
|
2013-02-23 15:41:41 +04:00
|
|
|
this._pushArray(data.cachedContacts, result);
|
2013-02-03 16:05:51 +04:00
|
|
|
}
|
|
|
|
break;
|
2012-02-29 02:01:48 +04:00
|
|
|
case "Contact:Save:Return:OK":
|
2013-07-19 02:01:00 +04: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-29 02:01:48 +04:00
|
|
|
case "Contacts:Clear:Return:OK":
|
|
|
|
case "Contact:Remove:Return:OK":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req)
|
2012-05-08 22:42:41 +04:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, null);
|
2012-02-29 02:01:48 +04: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 21:16:33 +04:00
|
|
|
case "Contacts:GetRevision:Return:KO":
|
|
|
|
case "Contacts:Count:Return:KO":
|
2012-02-29 02:01:48 +04:00
|
|
|
req = this.getRequest(msg.requestID);
|
2013-07-19 02:01:00 +04:00
|
|
|
if (req) {
|
|
|
|
if (req.request) {
|
|
|
|
req = req.request;
|
|
|
|
}
|
|
|
|
Services.DOMRequest.fireError(req, msg.errorMsg);
|
|
|
|
}
|
2012-02-29 02:01:48 +04:00
|
|
|
break;
|
2013-08-22 23:05:01 +04:00
|
|
|
case "Contacts:GetAll:Return:KO":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
|
|
|
Services.DOMRequest.fireError(req.cursor, msg.errorMsg);
|
|
|
|
}
|
|
|
|
break;
|
2013-02-02 00:44:55 +04:00
|
|
|
case "Contact:Changed":
|
|
|
|
// Fire oncontactchange event
|
|
|
|
if (DEBUG) debug("Contacts:ContactChanged: " + msg.contactID + ", " + msg.reason);
|
2013-10-18 01:29:56 +04:00
|
|
|
let event = new this._window.MozContactChangeEvent("contactchange", {
|
|
|
|
contactID: msg.contactID,
|
|
|
|
reason: msg.reason
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2013-02-02 00:44:55 +04:00
|
|
|
break;
|
2013-04-25 21:15:47 +04:00
|
|
|
case "Contacts:Revision":
|
|
|
|
if (DEBUG) debug("new revision: " + msg.revision);
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
2013-08-22 23:05:01 +04:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, msg.revision);
|
2013-04-25 21:15:47 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-06-04 07:45:10 +04:00
|
|
|
case "Contacts:Count":
|
|
|
|
if (DEBUG) debug("count: " + msg.count);
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
2013-08-22 23:05:01 +04:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, msg.count);
|
2013-06-04 07:45:10 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-02-02 00:44:55 +04:00
|
|
|
default:
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Wrong message: " + aMessage.name);
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
|
|
|
this.removeRequest(msg.requestID);
|
|
|
|
},
|
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
dispatchEvent: function(event) {
|
|
|
|
if (this.hasListenPermission) {
|
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-13 22:57:59 +04:00
|
|
|
askPermission: function (aAccess, aRequest, aAllowCallback, aCancelCallback) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("askPermission for contacts");
|
2014-07-08 08:45:23 +04:00
|
|
|
|
2012-11-01 04:26:05 +04:00
|
|
|
let access;
|
|
|
|
switch(aAccess) {
|
|
|
|
case "create":
|
|
|
|
access = "create";
|
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
case "remove":
|
|
|
|
access = "write";
|
|
|
|
break;
|
|
|
|
case "find":
|
|
|
|
case "listen":
|
2013-04-25 21:15:47 +04:00
|
|
|
case "revision":
|
2013-06-04 07:45:10 +04:00
|
|
|
case "count":
|
2012-11-01 04:26:05 +04:00
|
|
|
access = "read";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
access = "unknown";
|
|
|
|
}
|
2013-02-03 16:05:51 +04:00
|
|
|
|
2013-04-17 15:51:51 +04:00
|
|
|
// Shortcut for ALLOW_ACTION so we avoid a parent roundtrip
|
2014-07-08 08:45:23 +04:00
|
|
|
let principal = this._window.document.nodePrincipal;
|
2013-04-17 15:51:51 +04:00
|
|
|
let type = "contacts-" + access;
|
|
|
|
let permValue =
|
2014-07-08 08:45:23 +04:00
|
|
|
Services.perms.testExactPermissionFromPrincipal(principal, type);
|
2013-04-17 15:51:51 +04:00
|
|
|
if (permValue == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
2014-07-14 21:31:16 +04:00
|
|
|
if (aAllowCallback) {
|
|
|
|
aAllowCallback();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (permValue == Ci.nsIPermissionManager.DENY_ACTION ||
|
|
|
|
permValue == Ci.nsIPermissionManager.UNKNOWN_ACTION) {
|
|
|
|
if (aCancelCallback) {
|
|
|
|
aCancelCallback();
|
|
|
|
}
|
2013-04-17 15:51:51 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-08 08:45:23 +04:00
|
|
|
// Create an array with a single nsIContentPermissionType element.
|
|
|
|
let type = {
|
2012-08-09 22:34:57 +04:00
|
|
|
type: "contacts",
|
2012-11-01 04:26:05 +04:00
|
|
|
access: access,
|
2014-07-14 21:31:15 +04:00
|
|
|
options: [],
|
2014-07-08 08:45:23 +04: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-07-14 21:31:15 +04:00
|
|
|
allow: aAllowCallback ||
|
|
|
|
function() {
|
|
|
|
if (DEBUG)
|
|
|
|
debug("Default allow contacts callback. " + access +"\n");
|
|
|
|
},
|
|
|
|
cancel: aCancelCallback ||
|
|
|
|
function() {
|
|
|
|
if (DEBUG)
|
|
|
|
debug("Default cancel contacts callback. " + access +"\n");
|
|
|
|
},
|
2014-07-08 08:45:23 +04: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 22:34:57 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
save: function save(aContact) {
|
2013-10-18 01:29:56 +04: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 21:41:35 +04:00
|
|
|
try {
|
|
|
|
for (let field of PROPERTIES) {
|
|
|
|
// This hack makes sure modifications to the sequence attributes get validated.
|
|
|
|
aContact[field] = aContact[field];
|
2013-10-18 01:29:56 +04:00
|
|
|
newContact.properties[field] = aContact[field];
|
|
|
|
}
|
2013-12-29 21:41:35 +04: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-18 01:29:56 +04:00
|
|
|
}
|
|
|
|
|
2013-07-19 02:01:00 +04:00
|
|
|
let request = this.createRequest();
|
2013-12-29 21:41:35 +04:00
|
|
|
let requestID = this.getRequestId({request: request});
|
2012-08-09 22:34:57 +04:00
|
|
|
|
|
|
|
let reason;
|
|
|
|
if (aContact.id == "undefined") {
|
|
|
|
// for example {25c00f01-90e5-c545-b4d4-21E2ddbab9e0} becomes
|
|
|
|
// 25c00f0190e5c545b4d421E2ddbab9e0
|
2013-10-18 01:29:56 +04:00
|
|
|
aContact.id = this._getRandomId().replace(/[{}-]/g, "");
|
2013-07-19 02:01:00 +04:00
|
|
|
// Cache the contact so that its ID may be updated later if necessary
|
|
|
|
this._cachedContacts[requestID] = aContact;
|
2012-08-09 22:34:57 +04:00
|
|
|
reason = "create";
|
2012-02-29 02:01:48 +04:00
|
|
|
} else {
|
2012-08-09 22:34:57 +04:00
|
|
|
reason = "update";
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
2012-08-09 22:34:57 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
newContact.id = aContact.id;
|
|
|
|
newContact.published = aContact.published;
|
|
|
|
newContact.updated = aContact.updated;
|
|
|
|
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("send: " + JSON.stringify(newContact));
|
2013-10-18 01:29:56 +04:00
|
|
|
|
2012-11-01 04:26:05 +04:00
|
|
|
let options = { contact: newContact, reason: reason };
|
2012-08-09 22:34:57 +04:00
|
|
|
let allowCallback = function() {
|
2013-07-19 02:01:00 +04:00
|
|
|
cpmm.sendAsyncMessage("Contact:Save", {requestID: requestID, options: options});
|
2012-08-09 22:34:57 +04:00
|
|
|
}.bind(this)
|
|
|
|
this.askPermission(reason, request, allowCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(aOptions) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("find! " + JSON.stringify(aOptions));
|
2013-02-03 16:05:51 +04:00
|
|
|
let request = this.createRequest();
|
2012-08-09 22:34:57 +04:00
|
|
|
let options = { findOptions: aOptions };
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:Find", {requestID: this.getRequestId({request: request, reason: "find"}), options: options});
|
|
|
|
}.bind(this)
|
|
|
|
this.askPermission("find", request, allowCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2013-02-03 16:05:51 +04:00
|
|
|
createCursor: function CM_createCursor(aRequest) {
|
2013-02-25 07:53:55 +04:00
|
|
|
let data = {
|
|
|
|
cursor: Services.DOMRequest.createCursor(this._window, function() {
|
|
|
|
this.handleContinue(id);
|
|
|
|
}.bind(this)),
|
|
|
|
cachedContacts: [],
|
|
|
|
waitingForNext: true,
|
|
|
|
};
|
2013-04-18 21:44:38 +04:00
|
|
|
let id = this.getRequestId(data);
|
2013-02-03 16:05:51 +04:00
|
|
|
if (DEBUG) debug("saved cursor id: " + id);
|
2013-02-25 07:53:55 +04:00
|
|
|
return [id, data.cursor];
|
2013-02-03 16:05:51 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getAll: function CM_getAll(aOptions) {
|
|
|
|
if (DEBUG) debug("getAll: " + JSON.stringify(aOptions));
|
|
|
|
let [cursorId, cursor] = this.createCursor();
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetAll", {
|
|
|
|
cursorId: cursorId, findOptions: aOptions});
|
|
|
|
}.bind(this);
|
|
|
|
this.askPermission("find", cursor, allowCallback);
|
|
|
|
return cursor;
|
|
|
|
},
|
|
|
|
|
2013-03-16 07:40:03 +04:00
|
|
|
nextTick: function nextTick(aCallback) {
|
|
|
|
Services.tm.currentThread.dispatch(aCallback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
2013-02-03 16:05:51 +04:00
|
|
|
handleContinue: function CM_handleContinue(aCursorId) {
|
|
|
|
if (DEBUG) debug("handleContinue: " + aCursorId);
|
2013-04-18 21:44:38 +04:00
|
|
|
let data = this.getRequest(aCursorId);
|
2013-02-25 07:53:55 +04:00
|
|
|
if (data.cachedContacts.length > 0) {
|
|
|
|
if (DEBUG) debug("contact in cache");
|
2013-02-23 15:41:41 +04:00
|
|
|
let contact = data.cachedContacts.shift();
|
2013-03-16 07:40:03 +04:00
|
|
|
this.nextTick(this._fireSuccessOrDone.bind(this, data.cursor, contact));
|
2013-05-03 20:15:55 +04:00
|
|
|
if (!contact) {
|
|
|
|
this.removeRequest(aCursorId);
|
|
|
|
} else if (data.cachedContacts.length === CONTACTS_SENDMORE_MINIMUM) {
|
2013-03-28 02:24:15 +04:00
|
|
|
cpmm.sendAsyncMessage("Contacts:GetAll:SendNow", { cursorId: aCursorId });
|
|
|
|
}
|
2013-02-25 07:53:55 +04:00
|
|
|
} else {
|
|
|
|
if (DEBUG) debug("waiting for contact");
|
|
|
|
data.waitingForNext = true;
|
|
|
|
}
|
2013-02-03 16:05:51 +04:00
|
|
|
},
|
|
|
|
|
2014-01-29 17:20:40 +04:00
|
|
|
remove: function removeContact(aRecordOrId) {
|
2013-08-22 23:05:01 +04:00
|
|
|
let request = this.createRequest();
|
2014-01-29 17:20:40 +04:00
|
|
|
let id;
|
|
|
|
if (typeof aRecordOrId === "string") {
|
|
|
|
id = aRecordOrId;
|
|
|
|
} else if (!aRecordOrId || !aRecordOrId.id) {
|
2013-08-22 23:05:01 +04:00
|
|
|
Services.DOMRequest.fireErrorAsync(request, true);
|
|
|
|
return request;
|
2014-01-29 17:20:40 +04:00
|
|
|
} else {
|
|
|
|
id = aRecordOrId.id;
|
2013-08-22 23:05:01 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 17:20:40 +04:00
|
|
|
let options = { id: id };
|
2012-08-09 22:34:57 +04:00
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contact:Remove", {requestID: this.getRequestId({request: request, reason: "remove"}), options: options});
|
2013-10-18 01:29:56 +04:00
|
|
|
}.bind(this);
|
2012-08-09 22:34:57 +04:00
|
|
|
this.askPermission("remove", request, allowCallback);
|
|
|
|
return request;
|
2012-02-29 02:01:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("clear");
|
2013-10-18 01:29:56 +04:00
|
|
|
let request = this.createRequest();
|
2012-08-09 22:34:57 +04:00
|
|
|
let options = {};
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:Clear", {requestID: this.getRequestId({request: request, reason: "remove"}), options: options});
|
2013-10-18 01:29:56 +04:00
|
|
|
}.bind(this);
|
2012-08-09 22:34:57 +04:00
|
|
|
this.askPermission("remove", request, allowCallback);
|
|
|
|
return request;
|
2012-02-29 02:01:48 +04:00
|
|
|
},
|
|
|
|
|
2013-04-25 21:15:47 +04:00
|
|
|
getRevision: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetRevision", {
|
2013-08-22 23:05:01 +04:00
|
|
|
requestID: this.getRequestId({ request: request })
|
2013-04-25 21:15:47 +04:00
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let cancelCallback = function() {
|
2014-07-14 21:31:16 +04:00
|
|
|
Services.DOMRequest.fireError(request, "");
|
2013-04-25 21:15:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("revision", request, allowCallback, cancelCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2013-06-04 07:45:10 +04:00
|
|
|
getCount: function() {
|
|
|
|
let request = this.createRequest();
|
|
|
|
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetCount", {
|
2013-08-22 23:05:01 +04:00
|
|
|
requestID: this.getRequestId({ request: request })
|
2013-06-04 07:45:10 +04:00
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let cancelCallback = function() {
|
2014-07-14 21:31:16 +04:00
|
|
|
Services.DOMRequest.fireError(request, "");
|
2013-06-04 07:45:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.askPermission("count", request, allowCallback, cancelCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-02-29 02:01:48 +04:00
|
|
|
init: function(aWindow) {
|
2013-10-18 01:29:56 +04:00
|
|
|
// DOMRequestIpcHelper.initHelper sets this._window
|
2013-07-09 01:55:42 +04:00
|
|
|
this.initDOMRequestHelper(aWindow, ["Contacts:Find:Return:OK", "Contacts:Find:Return:KO",
|
2012-05-11 08:51:48 +04:00
|
|
|
"Contacts:Clear:Return:OK", "Contacts:Clear:Return:KO",
|
|
|
|
"Contact:Save:Return:OK", "Contact:Save:Return:KO",
|
2012-08-09 22:34:57 +04:00
|
|
|
"Contact:Remove:Return:OK", "Contact:Remove:Return:KO",
|
2013-02-02 00:44:55 +04:00
|
|
|
"Contact:Changed",
|
2013-08-22 23:05:01 +04:00
|
|
|
"Contacts:GetAll:Next", "Contacts:GetAll:Return:KO",
|
|
|
|
"Contacts:Count",
|
2013-07-19 02:01:00 +04:00
|
|
|
"Contacts:Revision", "Contacts:GetRevision:Return:KO",]);
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2012-05-08 22:42:41 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:RegisterForMessages");
|
|
|
|
this.hasListenPermission = true;
|
|
|
|
}.bind(this);
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2013-10-18 01:29:56 +04: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-20 09:33:10 +04:00
|
|
|
Ci.nsIObserver,
|
2013-10-18 01:29:56 +04:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
};
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2013-10-18 01:29:56 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([
|
2013-12-29 21:41:35 +04:00
|
|
|
Contact, ContactManager
|
2013-10-18 01:29:56 +04:00
|
|
|
]);
|