Bug 832936 - Implement mozContacts.getCount. r=gwagner sr=sicking
authorReuben Morais <reuben.morais@gmail.com>
Mon, 03 Jun 2013 20:45:10 -0700
changeset 145355 2c19b63b23d9dbb397784292722f116ef45852df
parent 145354 a608b5d810e66f6a11fc752aa2bd253c5897a770
child 145356 0cd289fcea166731e947883d303dae45778bc88e
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgwagner, sicking
bugs832936
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 832936 - Implement mozContacts.getCount. r=gwagner sr=sicking
dom/contacts/ContactManager.js
dom/contacts/ContactManager.manifest
dom/contacts/fallback/ContactDB.jsm
dom/contacts/fallback/ContactService.jsm
dom/contacts/tests/test_contacts_basics.html
dom/interfaces/contacts/nsIDOMContactManager.idl
--- a/dom/contacts/ContactManager.js
+++ b/dom/contacts/ContactManager.js
@@ -508,17 +508,17 @@ Contact.prototype = {
                                      flags: nsIClassInfo.DOM_OBJECT}),
 
   QueryInterface : XPCOMUtils.generateQI([nsIDOMContact, nsIContactProperties])
 }
 
 // ContactManager
 
 const CONTACTMANAGER_CONTRACTID = "@mozilla.org/contactManager;1";
-const CONTACTMANAGER_CID        = Components.ID("{7bfb6481-f946-4254-afc5-d7fe9f5c45a3}");
+const CONTACTMANAGER_CID        = Components.ID("{8beb3a66-d70a-4111-b216-b8e995ad3aff}");
 const nsIDOMContactManager      = Components.interfaces.nsIDOMContactManager;
 
 function ContactManager()
 {
   if (DEBUG) debug("Constructor");
 }
 
 ContactManager.prototype = {
@@ -653,16 +653,23 @@ ContactManager.prototype = {
         break;
       case "Contacts:Revision":
         if (DEBUG) debug("new revision: " + msg.revision);
         req = this.getRequest(msg.requestID);
         if (req) {
           Services.DOMRequest.fireSuccess(req, msg.revision);
         }
         break;
+      case "Contacts:Count":
+        if (DEBUG) debug("count: " + msg.count);
+        req = this.getRequest(msg.requestID);
+        if (req) {
+          Services.DOMRequest.fireSuccess(req, msg.count);
+        }
+        break;
       default:
         if (DEBUG) debug("Wrong message: " + aMessage.name);
     }
     this.removeRequest(msg.requestID);
   },
 
   askPermission: function (aAccess, aRequest, aAllowCallback, aCancelCallback) {
     if (DEBUG) debug("askPermission for contacts");
@@ -673,16 +680,17 @@ ContactManager.prototype = {
         break;
       case "update":
       case "remove":
         access = "write";
         break;
       case "find":
       case "listen":
       case "revision":
+      case "count":
         access = "read";
         break;
       default:
         access = "unknown";
       }
 
     // Shortcut for ALLOW_ACTION so we avoid a parent roundtrip
     let type = "contacts-" + access;
@@ -862,25 +870,42 @@ ContactManager.prototype = {
     let cancelCallback = function() {
       Services.DOMRequest.fireError(request);
     };
 
     this.askPermission("revision", request, allowCallback, cancelCallback);
     return request;
   },
 
+  getCount: function() {
+    let request = this.createRequest();
+
+    let allowCallback = function() {
+      cpmm.sendAsyncMessage("Contacts:GetCount", {
+        requestID: this.getRequestId(request)
+      });
+    }.bind(this);
+
+    let cancelCallback = function() {
+      Services.DOMRequest.fireError(request);
+    };
+
+    this.askPermission("count", request, allowCallback, cancelCallback);
+    return request;
+  },
+
   init: function(aWindow) {
     this.initHelper(aWindow, ["Contacts:Find:Return:OK", "Contacts:Find:Return:KO",
                               "Contacts:Clear:Return:OK", "Contacts:Clear:Return:KO",
                               "Contact:Save:Return:OK", "Contact:Save:Return:KO",
                               "Contact:Remove:Return:OK", "Contact:Remove:Return:KO",
                               "Contact:Changed",
                               "PermissionPromptHelper:AskPermission:OK",
-                              "Contacts:GetAll:Next",
-                              "Contacts:Revision"]);
+                              "Contacts:GetAll:Next", "Contacts:Revision",
+                              "Contacts:Count"]);
   },
 
   // Called from DOMRequestIpcHelper
   uninit: function uninit() {
     if (DEBUG) debug("uninit call");
     if (this._oncontactchange)
       this._oncontactchange = null;
   },
--- a/dom/contacts/ContactManager.manifest
+++ b/dom/contacts/ContactManager.manifest
@@ -15,11 +15,11 @@ contract @mozilla.org/contactFindSortOpt
 
 component {28ce07d0-45d9-4b7a-8843-521df4edd8bc} ContactManager.js
 contract @mozilla.org/contactFindOptions;1 {28ce07d0-45d9-4b7a-8843-521df4edd8bc}
 
 component {72a5ee28-81d8-4af8-90b3-ae935396cc66} ContactManager.js
 contract @mozilla.org/contact;1 {72a5ee28-81d8-4af8-90b3-ae935396cc66}
 category JavaScript-global-constructor mozContact @mozilla.org/contact;1
 
-component {7bfb6481-f946-4254-afc5-d7fe9f5c45a3} ContactManager.js
-contract @mozilla.org/contactManager;1 {7bfb6481-f946-4254-afc5-d7fe9f5c45a3}
+component {8beb3a66-d70a-4111-b216-b8e995ad3aff} ContactManager.js
+contract @mozilla.org/contactManager;1 {8beb3a66-d70a-4111-b216-b8e995ad3aff}
 category JavaScript-navigator-property mozContacts @mozilla.org/contactManager;1
--- a/dom/contacts/fallback/ContactDB.jsm
+++ b/dom/contacts/fallback/ContactDB.jsm
@@ -722,17 +722,26 @@ ContactDB.prototype = {
     }.bind(this));
   },
 
   getRevision: function CDB_getRevision(aSuccessCb) {
     if (DEBUG) debug("getRevision");
     this.newTxn("readonly", REVISION_STORE, function (txn, store) {
       store.get(REVISION_KEY).onsuccess = function (e) {
         aSuccessCb(e.target.result);
-      }
+      };
+    });
+  },
+
+  getCount: function CDB_getCount(aSuccessCb) {
+    if (DEBUG) debug("getCount");
+    this.newTxn("readonly", STORE_NAME, function (txn, store) {
+      store.count().onsuccess = function (e) {
+        aSuccessCb(e.target.result);
+      };
     });
   },
 
   /*
    * Sorting the contacts by sortBy field. aSortBy can either be familyName or givenName.
    * If 2 entries have the same sortyBy field or no sortBy field is present, we continue
    * sorting with the other sortyBy field.
    */
--- a/dom/contacts/fallback/ContactService.jsm
+++ b/dom/contacts/fallback/ContactService.jsm
@@ -24,17 +24,18 @@ XPCOMUtils.defineLazyServiceGetter(this,
 let myGlobal = this;
 
 let ContactService = {
   init: function() {
     if (DEBUG) debug("Init");
     this._messages = ["Contacts:Find", "Contacts:GetAll", "Contacts:GetAll:SendNow",
                       "Contacts:Clear", "Contact:Save",
                       "Contact:Remove", "Contacts:RegisterForMessages",
-                      "child-process-shutdown", "Contacts:GetRevision"];
+                      "child-process-shutdown", "Contacts:GetRevision",
+                      "Contacts:GetCount"];
     this._children = [];
     this._cursors = {};
     this._messages.forEach(function(msgName) {
       ppmm.addMessageListener(msgName, this);
     }.bind(this));
 
     var idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"].getService(Ci.nsIIndexedDatabaseManager);
     idbManager.initWindowless(myGlobal);
@@ -179,16 +180,29 @@ let ContactService = {
           function(revision) {
             mm.sendAsyncMessage("Contacts:Revision", {
               requestID: msg.requestID,
               revision: revision
             });
           }
         );
         break;
+      case "Contacts:GetCount":
+        if (!this.assertPermission(aMessage, "contacts-read")) {
+          return null;
+        }
+        this._db.getCount(
+          function(count) {
+            mm.sendAsyncMessage("Contacts:Count", {
+              requestID: msg.requestID,
+              count: count
+            });
+          }
+        );
+        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);
         }
--- a/dom/contacts/tests/test_contacts_basics.html
+++ b/dom/contacts/tests/test_contacts_basics.html
@@ -236,33 +236,44 @@ function checkRevision(revision, msg, th
   var revReq = mozContacts.getRevision();
   revReq.onsuccess = function(e) {
     is(e.target.result, initialRev+revision, msg);
     then();
   };
   revReq.onerror = onFailure;
 }
 
+function checkCount(count, msg, then) {
+  var request = navigator.mozContacts.getCount();
+  request.onsuccess = function(e) {
+    is(e.target.result, count, msg);
+    then();
+  };
+  request.onerror = onFailure;
+}
+
 var mozContacts = window.navigator.mozContacts;
 ok(mozContacts, "mozContacts exists");
 ok("mozContact" in window, "mozContact exists");
 var steps = [
   function() {
     mozContacts.getRevision().onsuccess = function(e) {
       initialRev = e.target.result;
       next();
     };
   },
   function () {
     ok(true, "Deleting database");
     checkRevision(0, "Initial revision is 0", function() {
       req = mozContacts.clear();
       req.onsuccess = function () {
         ok(true, "Deleted the database");
-        checkRevision(1, "Revision was incremented on clear", next);
+        checkCount(0, "No contacts after clear", function() {
+          checkRevision(1, "Revision was incremented on clear", next);
+        });
       };
       req.onerror = onFailure;
     });
   },
   function () {
     ok(true, "Retrieving all contacts");
     req = mozContacts.find({});
     req.onsuccess = function () {
@@ -274,17 +285,19 @@ var steps = [
   function () {
     ok(true, "Adding empty contact");
     createResult1 = new mozContact();
     createResult1.init({});
     req = navigator.mozContacts.save(createResult1);
     req.onsuccess = function () {
       ok(createResult1.id, "The contact now has an ID.");
       sample_id1 = createResult1.id;
-      checkRevision(2, "Revision was incremented on save", next);
+      checkCount(1, "1 contact after adding empty contact", function() {
+        checkRevision(2, "Revision was incremented on save", next);
+      });
     };
     req.onerror = onFailure;
   },
   function () {
     ok(true, "Retrieving all contacts");
     req = mozContacts.find({});
     req.onsuccess = function () {
       is(req.result.length, 1, "One contact.");
@@ -985,17 +998,17 @@ var steps = [
       req.onerror = onFailure;
     };
     createResult1 = new mozContact();
     createResult1.init(properties1);
     req = mozContacts.save(createResult1);
     req.onsuccess = function () {
       ok(createResult1.id, "The contact now has an ID.");
       ok(createResult1.name == properties1.name, "Same Name");
-      next();
+      checkCount(20, "20 contacts in DB", next);
     };
     req.onerror = onFailure;
   },
   function () {
     ok(true, "Retrieving all contacts");
     req = mozContacts.find({});
     req.onsuccess = function () {
       is(req.result.length, 20, "20 Entries.");
--- a/dom/interfaces/contacts/nsIDOMContactManager.idl
+++ b/dom/interfaces/contacts/nsIDOMContactManager.idl
@@ -15,25 +15,27 @@ interface nsIDOMContact : nsIContactProp
 {
   attribute DOMString id;
   readonly attribute jsval     published;
   readonly attribute jsval     updated;
 
   void init(in nsIContactProperties properties);  // Workaround BUG 723206
 };
 
-[scriptable, uuid(e01ebfe7-e972-4e01-b04b-1d162dc74983)]
+[scriptable, uuid(8beb3a66-d70a-4111-b216-b8e995ad3aff)]
 interface nsIDOMContactManager : nsISupports
 {
   nsIDOMDOMRequest find(in nsIContactFindOptions options);
 
   nsIDOMDOMCursor getAll(in nsIContactFindSortOptions options);
 
   nsIDOMDOMRequest clear();
 
   nsIDOMDOMRequest save(in nsIDOMContact contact);
 
   nsIDOMDOMRequest remove(in nsIDOMContact contact);
 
   attribute nsIDOMEventListener oncontactchange;
 
   nsIDOMDOMRequest getRevision();
+
+  nsIDOMDOMRequest getCount();
 };