Bug 784746 - Contacts API: optimize debug statements. r=bent

This commit is contained in:
Gregor Wagner 2012-08-22 14:34:57 -07:00
parent 1733e65710
commit 3aaa485604
3 changed files with 62 additions and 75 deletions

View File

@ -4,12 +4,8 @@
"use strict";
/* static functions */
let DEBUG = 0;
if (DEBUG)
debug = function (s) { dump("-*- ContactManager: " + s + "\n"); }
else
debug = function (s) {}
const DEBUG = false;
function debug(s) { dump("-*- ContactManager: " + s + "\n"); }
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -37,7 +33,7 @@ const nsIDOMContactProperties = Ci.nsIDOMContactProperties;
// ContactProperties is not directly instantiated. It is used as interface.
function ContactProperties(aProp) { debug("ContactProperties Constructor"); }
function ContactProperties(aProp) { if (DEBUG) debug("ContactProperties Constructor"); }
ContactProperties.prototype = {
@ -168,10 +164,9 @@ const CONTACT_CONTRACTID = "@mozilla.org/contact;1";
const CONTACT_CID = Components.ID("{da0f7040-388b-11e1-b86c-0800200c9a66}");
const nsIDOMContact = Components.interfaces.nsIDOMContact;
// The wrappedJSObject magic here allows callers to get at the underlying JS object
// of the XPCOM component. We use this below to modify properties that are read-only
// per-idl. See https://developer.mozilla.org/en-US/docs/wrappedJSObject.
function Contact() { debug("Contact constr: "); this.wrappedJSObject = this; };
function Contact() {
if (DEBUG) debug("Contact constr: ");
};
Contact.prototype = {
__exposedProps__: {
@ -333,7 +328,7 @@ const nsIDOMContactManager = Components.interfaces.nsIDOMContactManager;
function ContactManager()
{
debug("Constructor");
if (DEBUG) debug("Constructor");
}
ContactManager.prototype = {
@ -341,7 +336,7 @@ ContactManager.prototype = {
_oncontactchange: null,
set oncontactchange(aCallback) {
debug("set oncontactchange");
if (DEBUG) debug("set oncontactchange");
let allowCallback = function() {
this._oncontactchange = aCallback;
}.bind(this);
@ -373,7 +368,7 @@ ContactManager.prototype = {
},
receiveMessage: function(aMessage) {
debug("Contactmanager::receiveMessage: " + aMessage.name);
if (DEBUG) debug("Contactmanager::receiveMessage: " + aMessage.name);
let msg = aMessage.json;
let contacts = msg.contacts;
@ -384,7 +379,7 @@ ContactManager.prototype = {
let result = this._convertContactsArray(contacts);
Services.DOMRequest.fireSuccess(req.request, result);
} else {
debug("no request stored!" + msg.requestID);
if (DEBUG) debug("no request stored!" + msg.requestID);
}
break;
case "Contact:Save:Return:OK":
@ -412,7 +407,7 @@ ContactManager.prototype = {
Services.DOMRequest.fireError(req.request, msg.errorMsg);
break;
case "PermissionPromptHelper:AskPermission:OK":
debug("id: " + msg.requestID);
if (DEBUG) debug("id: " + msg.requestID);
req = this.getRequest(msg.requestID);
if (!req) {
break;
@ -425,13 +420,13 @@ ContactManager.prototype = {
}
break;
default:
debug("Wrong message: " + aMessage.name);
if (DEBUG) debug("Wrong message: " + aMessage.name);
}
this.removeRequest(msg.requestID);
},
askPermission: function (aAccess, aReqeust, aAllowCallback, aCancelCallback) {
debug("askPermission for contacts");
if (DEBUG) debug("askPermission for contacts");
let requestID = this.getRequestId({
request: aReqeust,
allow: function() {
@ -459,7 +454,7 @@ ContactManager.prototype = {
save: function save(aContact) {
let request;
debug("save: " + JSON.stringify(aContact) + " :" + aContact.id);
if (DEBUG) debug("save: " + JSON.stringify(aContact) + " :" + aContact.id);
let newContact = {};
newContact.properties = {
name: [],
@ -499,7 +494,7 @@ ContactManager.prototype = {
}
this._setMetaData(newContact, aContact);
debug("send: " + JSON.stringify(newContact));
if (DEBUG) debug("send: " + JSON.stringify(newContact));
request = this.createRequest();
let options = { contact: newContact };
let allowCallback = function() {
@ -510,7 +505,7 @@ ContactManager.prototype = {
},
find: function(aOptions) {
debug("find! " + JSON.stringify(aOptions));
if (DEBUG) debug("find! " + JSON.stringify(aOptions));
let request;
request = this.createRequest();
let options = { findOptions: aOptions };
@ -533,7 +528,7 @@ ContactManager.prototype = {
},
clear: function() {
debug("clear");
if (DEBUG) debug("clear");
let request;
request = this.createRequest();
let options = {};
@ -550,16 +545,16 @@ ContactManager.prototype = {
let allowCallback = function() {
let callback = function(aType, aContacts) {
debug("got SIM contacts: " + aType + " " + JSON.stringify(aContacts));
if (DEBUG) debug("got SIM contacts: " + aType + " " + JSON.stringify(aContacts));
let result = aContacts.map(function(c) {
var contact = new Contact();
contact.init( { name: [c.alphaId], tel: [ { number: c.number } ] } );
return contact;
});
debug("result: " + JSON.stringify(result));
if (DEBUG) debug("result: " + JSON.stringify(result));
Services.DOMRequest.fireSuccess(request, result);
};
debug("getSimContacts " + aType);
if (DEBUG) debug("getSimContacts " + aType);
mRIL.getICCContacts(aType, callback);
}.bind(this);
@ -585,7 +580,7 @@ ContactManager.prototype = {
// Called from DOMRequestIpcHelper
uninit: function uninit() {
debug("uninit call");
if (DEBUG) debug("uninit call");
if (this._oncontactchange)
this._oncontactchange = null;
},

View File

@ -6,13 +6,8 @@
const EXPORTED_SYMBOLS = ['ContactDB'];
let DEBUG = 0;
/* static functions */
if (DEBUG) {
debug = function (s) { dump("-*- ContactDB component: " + s + "\n"); }
} else {
debug = function (s) {}
}
const DEBUG = false;
function debug(s) { dump("-*- ContactDB component: " + s + "\n"); }
const Cu = Components.utils;
const Cc = Components.classes;
@ -26,7 +21,7 @@ const DB_VERSION = 4;
const STORE_NAME = "contacts";
function ContactDB(aGlobal) {
debug("Constructor");
if (DEBUG) debug("Constructor");
this._global = aGlobal;
}
@ -34,7 +29,7 @@ ContactDB.prototype = {
__proto__: IndexedDBHelper.prototype,
upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
debug("upgrade schema from: " + aOldVersion + " to " + aNewVersion + " called!");
if (DEBUG) debug("upgrade schema from: " + aOldVersion + " to " + aNewVersion + " called!");
let db = aDb;
let objectStore;
for (let currVersion = aOldVersion; currVersion < aNewVersion; currVersion++) {
@ -50,7 +45,7 @@ ContactDB.prototype = {
* properties: {...} // Object holding the ContactProperties
* }
*/
debug("create schema");
if (DEBUG) debug("create schema");
objectStore = db.createObjectStore(this.dbStoreName, {keyPath: "id"});
// Metadata indexes
@ -74,7 +69,7 @@ ContactDB.prototype = {
objectStore.createIndex("emailLowerCase", "search.email", { unique: false, multiEntry: true });
objectStore.createIndex("noteLowerCase", "search.note", { unique: false, multiEntry: true });
} else if (currVersion == 1) {
debug("upgrade 1");
if (DEBUG) debug("upgrade 1");
// Create a new scheme for the tel field. We move from an array of tel-numbers to an array of
// ContactTelephone.
@ -88,12 +83,12 @@ ContactDB.prototype = {
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
debug("upgrade tel1: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade tel1: " + JSON.stringify(cursor.value));
for (let number in cursor.value.properties.tel) {
cursor.value.properties.tel[number] = {number: number};
}
cursor.update(cursor.value);
debug("upgrade tel2: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade tel2: " + JSON.stringify(cursor.value));
cursor.continue();
}
};
@ -102,7 +97,7 @@ ContactDB.prototype = {
objectStore.createIndex("tel", "search.tel", { unique: false, multiEntry: true });
objectStore.createIndex("category", "properties.category", { unique: false, multiEntry: true });
} else if (currVersion == 2) {
debug("upgrade 2");
if (DEBUG) debug("upgrade 2");
// Create a new scheme for the email field. We move from an array of emailaddresses to an array of
// ContactEmail.
if (!objectStore) {
@ -115,11 +110,11 @@ ContactDB.prototype = {
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
debug("upgrade email1: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade email1: " + JSON.stringify(cursor.value));
cursor.value.properties.email =
cursor.value.properties.email.map(function(address) { return { address: address }; });
cursor.update(cursor.value);
debug("upgrade email2: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade email2: " + JSON.stringify(cursor.value));
cursor.continue();
}
};
@ -127,7 +122,7 @@ ContactDB.prototype = {
// Create new searchable indexes.
objectStore.createIndex("email", "search.email", { unique: false, multiEntry: true });
} else if (currVersion == 3) {
debug("upgrade 3");
if (DEBUG) debug("upgrade 3");
if (!objectStore) {
objectStore = aTransaction.objectStore(STORE_NAME);
@ -137,11 +132,11 @@ ContactDB.prototype = {
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
debug("upgrade impp1: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade impp1: " + JSON.stringify(cursor.value));
cursor.value.properties.impp =
cursor.value.properties.impp.map(function(value) { return { value: value }; });
cursor.update(cursor.value);
debug("upgrade impp2: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
cursor.continue();
}
};
@ -149,11 +144,11 @@ ContactDB.prototype = {
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
debug("upgrade url1: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade url1: " + JSON.stringify(cursor.value));
cursor.value.properties.url =
cursor.value.properties.url.map(function(value) { return { value: value }; });
cursor.update(cursor.value);
debug("upgrade impp2: " + JSON.stringify(cursor.value));
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
cursor.continue();
}
};
@ -228,7 +223,7 @@ ContactDB.prototype = {
contact.search[field].push(digits.substring(i, digits.length));
}
}
debug("lookup: " + JSON.stringify(contact.search[field]));
if (DEBUG) debug("lookup: " + JSON.stringify(contact.search[field]));
}
} else if (field == "email") {
let address = aContact.properties[field][i].value;
@ -250,7 +245,7 @@ ContactDB.prototype = {
}
}
}
debug("contact:" + JSON.stringify(contact));
if (DEBUG) debug("contact:" + JSON.stringify(contact));
contact.updated = aContact.updated;
contact.published = aContact.published;
@ -285,24 +280,24 @@ ContactDB.prototype = {
saveContact: function saveContact(aContact, successCb, errorCb) {
let contact = this.makeImport(aContact);
this.newTxn("readwrite", function (txn, store) {
debug("Going to update" + JSON.stringify(contact));
if (DEBUG) debug("Going to update" + JSON.stringify(contact));
// Look up the existing record and compare the update timestamp.
// If no record exists, just add the new entry.
let newRequest = store.get(contact.id);
newRequest.onsuccess = function (event) {
if (!event.target.result) {
debug("new record!")
if (DEBUG) debug("new record!")
this.updateRecordMetadata(contact);
store.put(contact);
} else {
debug("old record!")
if (DEBUG) debug("old record!")
if (new Date(typeof contact.updated === "undefined" ? 0 : contact.updated) < new Date(event.target.result.updated)) {
debug("rev check fail!");
if (DEBUG) debug("rev check fail!");
txn.abort();
return;
} else {
debug("rev check OK");
if (DEBUG) debug("rev check OK");
contact.published = event.target.result.published;
contact.updated = new Date();
store.put(contact);
@ -314,14 +309,14 @@ ContactDB.prototype = {
removeContact: function removeContact(aId, aSuccessCb, aErrorCb) {
this.newTxn("readwrite", function (txn, store) {
debug("Going to delete" + aId);
if (DEBUG) debug("Going to delete" + aId);
store.delete(aId);
}, aSuccessCb, aErrorCb);
},
clear: function clear(aSuccessCb, aErrorCb) {
this.newTxn("readwrite", function (txn, store) {
debug("Going to clear all!");
if (DEBUG) debug("Going to clear all!");
store.clear();
}, aSuccessCb, aErrorCb);
},
@ -339,7 +334,7 @@ ContactDB.prototype = {
* - count
*/
find: function find(aSuccessCb, aFailureCb, aOptions) {
debug("ContactDB:find val:" + aOptions.filterValue + " by: " + aOptions.filterBy + " op: " + aOptions.filterOp + "\n");
if (DEBUG) debug("ContactDB:find val:" + aOptions.filterValue + " by: " + aOptions.filterBy + " op: " + aOptions.filterOp + "\n");
let self = this;
this.newTxn("readonly", function (txn, store) {
if (aOptions && (aOptions.filterOp == "equals" || aOptions.filterOp == "contains")) {
@ -351,12 +346,12 @@ ContactDB.prototype = {
},
_findWithIndex: function _findWithIndex(txn, store, options) {
debug("_findWithIndex: " + options.filterValue +" " + options.filterOp + " " + options.filterBy + " ");
if (DEBUG) debug("_findWithIndex: " + options.filterValue +" " + options.filterOp + " " + options.filterBy + " ");
let fields = options.filterBy;
for (let key in fields) {
debug("key: " + fields[key]);
if (DEBUG) debug("key: " + fields[key]);
if (!store.indexNames.contains(fields[key]) && !fields[key] == "id") {
debug("Key not valid!" + fields[key] + ", " + store.indexNames);
if (DEBUG) debug("Key not valid!" + fields[key] + ", " + store.indexNames);
txn.abort();
return;
}
@ -364,7 +359,7 @@ ContactDB.prototype = {
// lookup for all keys
if (options.filterBy.length == 0) {
debug("search in all fields!" + JSON.stringify(store.indexNames));
if (DEBUG) debug("search in all fields!" + JSON.stringify(store.indexNames));
for(let myIndex = 0; myIndex < store.indexNames.length; myIndex++) {
fields = Array.concat(fields, store.indexNames[myIndex])
}
@ -383,7 +378,7 @@ ContactDB.prototype = {
let index = store.index(key);
request = index.mozGetAll(options.filterValue, limit);
} else if (options.filterOp == "equals") {
debug("Getting index: " + key);
if (DEBUG) debug("Getting index: " + key);
// case sensitive
let index = store.index(key);
request = index.mozGetAll(options.filterValue, limit);
@ -400,7 +395,7 @@ ContactDB.prototype = {
txn.result = {};
request.onsuccess = function (event) {
debug("Request successful. Record count:" + event.target.result.length);
if (DEBUG) debug("Request successful. Record count:" + event.target.result.length);
for (let i in event.target.result)
txn.result[event.target.result[i].id] = this.makeExport(event.target.result[i]);
}.bind(this);
@ -408,13 +403,13 @@ ContactDB.prototype = {
},
_findAll: function _findAll(txn, store, options) {
debug("ContactDB:_findAll: " + JSON.stringify(options));
if (DEBUG) debug("ContactDB:_findAll: " + JSON.stringify(options));
if (!txn.result)
txn.result = {};
// Sorting functions takes care of limit if set.
let limit = options.sortBy === 'undefined' ? options.filterLimit : null;
store.mozGetAll(null, limit).onsuccess = function (event) {
debug("Request successful. Record count:", event.target.result.length);
if (DEBUG) debug("Request successful. Record count:", event.target.result.length);
for (let i in event.target.result)
txn.result[event.target.result[i].id] = this.makeExport(event.target.result[i]);
}.bind(this);

View File

@ -4,11 +4,8 @@
"use strict";
let DEBUG = 0;
if (DEBUG)
debug = function (s) { dump("-*- Fallback ContactService component: " + s + "\n"); }
else
debug = function (s) {}
const DEBUG = false;
function debug(s) { dump("-*- Fallback ContactService component: " + s + "\n"); }
const Cu = Components.utils;
const Cc = Components.classes;
@ -28,7 +25,7 @@ let myGlobal = this;
let DOMContactManager = {
init: function() {
debug("Init");
if (DEBUG) debug("Init");
this._messages = ["Contacts:Find", "Contacts:Clear", "Contact:Save", "Contact:Remove"];
this._messages.forEach((function(msgName) {
ppmm.addMessageListener(msgName, this);
@ -56,7 +53,7 @@ let DOMContactManager = {
},
receiveMessage: function(aMessage) {
debug("Fallback DOMContactManager::receiveMessage " + aMessage.name);
if (DEBUG) debug("Fallback DOMContactManager::receiveMessage " + aMessage.name);
let mm = aMessage.target.QueryInterface(Ci.nsIFrameMessageManager);
let msg = aMessage.data;
@ -112,14 +109,14 @@ let DOMContactManager = {
if (msg.options && msg.options.findOptions) {
let findOptions = msg.options.findOptions;
if (findOptions.sortOrder !== 'undefined' && findOptions.sortBy !== 'undefined') {
debug('sortBy: ' + findOptions.sortBy + ', sortOrder: ' + findOptions.sortOrder );
if (DEBUG) debug('sortBy: ' + findOptions.sortBy + ', sortOrder: ' + findOptions.sortOrder );
result.sort(sortfunction);
if (findOptions.filterLimit)
result = result.slice(0, findOptions.filterLimit);
}
}
debug("result:" + JSON.stringify(result));
if (DEBUG) debug("result:" + JSON.stringify(result));
mm.sendAsyncMessage("Contacts:Find:Return:OK", {requestID: msg.requestID, contacts: result});
}.bind(this),
function(aErrorMsg) { mm.sendAsyncMessage("Contacts:Find:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }) }.bind(this),
@ -145,7 +142,7 @@ let DOMContactManager = {
function(aErrorMsg) { mm.sendAsyncMessage("Contacts:Clear:Return:KO", { requestID: msg.requestID, errorMsg: aErrorMsg }); }.bind(this)
);
default:
debug("WRONG MESSAGE NAME: " + aMessage.name);
if (DEBUG) debug("WRONG MESSAGE NAME: " + aMessage.name);
}
}
}