Bug 811538 - B2G SMS: Applying PhoneNumberJS to SMS DB. r=bent
☠☠ backed out by 56f34c1cc509 ☠ ☠
authorGregor Wagner <anygregor@gmail.com>
Tue, 04 Dec 2012 15:40:41 -0800
changeset 120547 d29847ff05c441552ccf63e139061359582863a8
parent 120546 b104b77e8b257f679e75a74d1d1aff039e09b150
child 120548 a11004852dacf2223d71a5ded30b518d18308cce
push idunknown
push userunknown
push dateunknown
reviewersbent
bugs811538
milestone20.0a1
Bug 811538 - B2G SMS: Applying PhoneNumberJS to SMS DB. r=bent
dom/sms/src/ril/SmsDatabaseService.js
--- a/dom/sms/src/ril/SmsDatabaseService.js
+++ b/dom/sms/src/ril/SmsDatabaseService.js
@@ -3,23 +3,24 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
 
 const RIL_SMSDATABASESERVICE_CONTRACTID = "@mozilla.org/sms/rilsmsdatabaseservice;1";
 const RIL_SMSDATABASESERVICE_CID = Components.ID("{a1fa610c-eb6c-4ac2-878f-b005d5e89249}");
 
 const DEBUG = false;
 const DB_NAME = "sms";
-const DB_VERSION = 5;
+const DB_VERSION = 6;
 const STORE_NAME = "sms";
 const MOST_RECENT_STORE_NAME = "most-recent";
 
 const DELIVERY_SENT = "sent";
 const DELIVERY_RECEIVED = "received";
 
 const DELIVERY_STATUS_NOT_APPLICABLE = "not-applicable";
 const DELIVERY_STATUS_SUCCESS = "success";
@@ -184,16 +185,20 @@ SmsDatabaseService.prototype = {
           case 3:
             if (DEBUG) debug("Upgrade to version 4. Add quick threads view.")
             self.upgradeSchema3(db, event.target.transaction);
             break;
           case 4:
             if (DEBUG) debug("Upgrade to version 5. Populate quick threads view.")
             self.upgradeSchema4(event.target.transaction);
             break;
+          case 5:
+            if (DEBUG) debug("Upgrade to version 6. Use PhonenumberJS.")
+            self.upgradeSchema5(event.target.transaction);
+            break;
           default:
             event.target.transaction.abort();
             callback("Old database version: " + event.oldVersion, null);
             break;
         }
         currentVersion++;
       }
     }
@@ -352,16 +357,86 @@ SmsDatabaseService.prototype = {
           body: message.body,
           unreadCount: message.read ? 0 : 1
         }
       }
       cursor.continue();
     }
   },
 
+  upgradeSchema5: function upgradeSchema5(transaction) {
+    let smsStore = transaction.objectStore(STORE_NAME);
+    let mostRecentStore = transaction.objectStore(MOST_RECENT_STORE_NAME);
+
+    smsStore.openCursor().onsuccess = function(event) {
+      let cursor = event.target.result;
+      if (!cursor) {
+        return;
+      }
+
+      let needsUpdate = false;
+      let message = cursor.value;
+      if (message.receiver) {
+        if (message.receiver !== "undefined") {
+          if (DEBUG) debug("upgrade message.receiver from: " + message.receiver + "\n");
+          let parsedNumber = PhoneNumberUtils.parse(message.receiver.toString());
+          if (parsedNumber && parsedNumber.internationalNumber) {
+            message.receiver = parsedNumber.internationalNumber;
+            needsUpdate = true;
+          }
+
+          if (DEBUG) debug("upgrade message.receiver to: " + message.receiver + "\n");
+        } else {
+          message.receiver = null;
+          needsUpdate = true;
+        }
+      }
+
+      if (message.sender) {
+        if (message.sender !== "undefined") {
+          if (DEBUG) debug("upgrade message.sender from: " + message.sender + "\n");
+          let parsedNumber = PhoneNumberUtils.parse(message.sender.toString());
+          if (parsedNumber && parsedNumber.internationalNumber) {
+            message.sender = parsedNumber.internationalNumber;
+            needsUpdate = true;
+            if (DEBUG) debug("upgrade message.sender to: " + message.sender + "\n");
+          }
+        } else {
+          message.sender = null;
+          needsUpdate = true;
+        }
+      }
+
+      if (needsUpdate) {
+        cursor.update(message);
+      }
+      cursor.continue();
+    }
+
+    mostRecentStore.openCursor().onsuccess = function(event) {
+      let cursor = event.target.result;
+      if (!cursor) {
+        return;
+      }
+
+      let entry = cursor.value;
+      if (entry.senderOrReceiver) {
+        if (DEBUG) debug("upgrade mostRecentStore from: " + entry.senderOrReceiver + "\n");
+        let parsedNumber = PhoneNumberUtils.parse(entry.senderOrReceiver);
+        if (parsedNumber && parsedNumber.internationalNumber) {
+          entry.senderOrReceiver = parsedNumber.internationalNumber;
+          cursor.update(entry);
+          if (DEBUG) debug("upgrade mostRecentStore to: " + entry.senderOrReceiver + "\n");
+        }
+      }
+
+      cursor.continue();
+    }
+  },
+
   /**
    * Helper function to make the intersection of the partial result arrays
    * obtained within createMessageList.
    *
    * @param keys
    *        Object containing the partial result arrays.
    * @param fiter
    *        Object containing the filter search criteria used to retrieved the
@@ -488,52 +563,82 @@ SmsDatabaseService.prototype = {
     return message.id;
   },
 
 
   /**
    * nsISmsDatabaseService API
    */
 
-  saveReceivedMessage: function saveReceivedMessage(sender, body, messageClass, date) {
+  saveReceivedMessage: function saveReceivedMessage(aSender, aBody, aMessageClass, aDate) {
     let receiver = this.mRIL.rilContext.icc ? this.mRIL.rilContext.icc.msisdn : null;
 
     // Workaround an xpconnect issue with undefined string objects.
     // See bug 808220
     if (receiver === undefined || receiver === "undefined") {
       receiver = null;
     }
 
+    if (receiver) {
+      let parsedNumber = PhoneNumberUtils.parse(receiver);
+      receiver = (parsedNumber && parsedNumber.internationalNumber)
+                 ? parsedNumber.internationalNumber
+                 : receiver;
+    }
+
+    let sender = aSender;
+    if (sender) {
+      let parsedNumber = PhoneNumberUtils.parse(sender);
+      sender = (parsedNumber && parsedNumber.internationalNumber)
+               ? parsedNumber.internationalNumber
+               : sender;
+    }
+
     let message = {delivery:       DELIVERY_RECEIVED,
                    deliveryStatus: DELIVERY_STATUS_SUCCESS,
                    sender:         sender,
                    receiver:       receiver,
-                   body:           body,
-                   messageClass:   messageClass,
-                   timestamp:      date,
+                   body:           aBody,
+                   messageClass:   aMessageClass,
+                   timestamp:      aDate,
                    read:           FILTER_READ_UNREAD};
     return this.saveMessage(message);
   },
 
-  saveSentMessage: function saveSentMessage(receiver, body, date) {
+  saveSentMessage: function saveSentMessage(aReceiver, aBody, aDate) {
     let sender = this.mRIL.rilContext.icc ? this.mRIL.rilContext.icc.msisdn : null;
 
     // Workaround an xpconnect issue with undefined string objects.
     // See bug 808220
     if (sender === undefined || sender === "undefined") {
       sender = null;
     }
 
+    let receiver = aReceiver
+    if (receiver) {
+      let parsedNumber = PhoneNumberUtils.parse(receiver.toString());
+      receiver = (parsedNumber && parsedNumber.internationalNumber)
+                 ? parsedNumber.internationalNumber
+                 : receiver;
+    }
+
+    if (sender) {
+      let parsedNumber = PhoneNumberUtils.parse(sender.toString());
+      sender = (parsedNumber && parsedNumber.internationalNumber)
+               ? parsedNumber.internationalNumber
+               : sender;
+    }
+
     let message = {delivery:       DELIVERY_SENT,
                    deliveryStatus: DELIVERY_STATUS_PENDING,
                    sender:         sender,
                    receiver:       receiver,
-                   body:           body,
+                   body:           aBody,
                    messageClass:   MESSAGE_CLASS_NORMAL,
-                   timestamp:      date,
+                   timestamp:      aDate,
                    read:           FILTER_READ_READ};
     return this.saveMessage(message);
   },
 
   setMessageDeliveryStatus: function setMessageDeliveryStatus(messageId, deliveryStatus) {
     if ((deliveryStatus != DELIVERY_STATUS_SUCCESS)
         && (deliveryStatus != DELIVERY_STATUS_ERROR)) {
       if (DEBUG) {
@@ -620,17 +725,20 @@ SmsDatabaseService.prototype = {
                                                    data.body,
                                                    data.messageClass,
                                                    data.timestamp,
                                                    data.read);
         aRequest.notifyMessageGot(message);
       };
 
       txn.onerror = function onerror(event) {
-        if (DEBUG) debug("Caught error on transaction", event.target.errorCode);
+        if (DEBUG) {
+          if (event.target)
+            debug("Caught error on transaction", event.target.errorCode);
+        }
         //TODO look at event.target.errorCode, pick appropriate error constant
         aRequest.notifyGetMessageFailed(Ci.nsISmsRequest.INTERNAL_ERROR);
       };
     });
   },
 
   deleteMessage: function deleteMessage(messageId, aRequest) {
     let deleted = false;