Bug 777228 - Alarm API - The back-end needs to remove alarms that have been fired from DB, so that .getAll() won't return them. r=vivien
authorGene Lian <clian@mozilla.com>
Mon, 30 Jul 2012 19:17:35 +0800
changeset 106335 3e1924bf49c28e8586390d384aa0d6a85e9d9f4f
parent 106334 090fd1585e341ccc0f18b02c809e066a61fe3e77
child 106336 e0779a303e3118e58f0646ce3f534e311f222671
push id1490
push userakeybl@mozilla.com
push dateMon, 08 Oct 2012 18:29:50 +0000
treeherdermozilla-beta@f335e7dacdc1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvivien
bugs777228
milestone17.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 777228 - Alarm API - The back-end needs to remove alarms that have been fired from DB, so that .getAll() won't return them. r=vivien
dom/alarm/AlarmService.jsm
--- a/dom/alarm/AlarmService.jsm
+++ b/dom/alarm/AlarmService.jsm
@@ -42,17 +42,17 @@ let AlarmService = {
 
     // add the messages to be listened
     const messages = ["AlarmsManager:GetAll", "AlarmsManager:Add", "AlarmsManager:Remove"];
     messages.forEach(function addMessage(msgName) {
         ppmm.addMessageListener(msgName, this);
     }.bind(this));
 
     // set the indexeddb database
-    let idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"].getService(Ci.nsIIndexedDatabaseManager);
+    let idbManager = Cc["@mozilla.org/dom/indexeddb/manager;1"].getService(Ci.nsIIndexedDatabaseManager);
     idbManager.initWindowless(myGlobal);
     this._db = new AlarmDB(myGlobal);
     this._db.init(myGlobal);
 
     // variable to save alarms waiting to be set
     this._alarmQueue = [];
 
     this._restoreAlarmsFromDb();
@@ -95,30 +95,30 @@ let AlarmService = {
         let newAlarm = {
           date: json.date, 
           ignoreTimezone: json.ignoreTimezone, 
           timezoneOffset: this._currentTimezoneOffset, 
           data: json.data,
           manifestURL: json.manifestURL
         };
 
+        let newAlarmTime = this._getAlarmTime(newAlarm);
+        if (newAlarmTime <= Date.now()) {
+          debug("Adding a alarm that has past time. Return DOMError.");
+          this._debugCurrentAlarm();
+          this._sendAsyncMessage(mm, "Add", false, json.requestId, "InvalidStateError");
+          break;
+        }
+
         this._db.add(
           newAlarm,
           function addSuccessCb(aNewId) {
             debug("Callback after adding alarm in database.");
 
             newAlarm['id'] = aNewId;
-            let newAlarmTime = this._getAlarmTime(newAlarm);
-
-            if (newAlarmTime <= Date.now()) {
-              debug("Adding a alarm that has past time. Don't set it in system.");
-              this._debugCurrentAlarm();
-              this._sendAsyncMessage(mm, "Add", true, json.requestId, aNewId);
-              return;
-            }
 
             // if there is no alarm being set in system, set the new alarm
             if (this._currentAlarm == null) {
               this._currentAlarm = newAlarm;
               this._debugCurrentAlarm();
               this._sendAsyncMessage(mm, "Add", true, json.requestId, aNewId);
               return;
             }
@@ -143,17 +143,17 @@ let AlarmService = {
           }.bind(this),
           function addErrorCb(aErrorMsg) {
             this._sendAsyncMessage(mm, "Add", false, json.requestId, aErrorMsg);
           }.bind(this)
         );
         break;
 
       case "AlarmsManager:Remove":
-        this._db.remove(
+        this._removeAlarmFromDb(
           json.id,
           function removeSuccessCb() {
             debug("Callback after removing alarm from database.");
 
             // if there is no alarm being set
             if (!this._currentAlarm) {
               this._debugCurrentAlarm();
               return;
@@ -178,20 +178,17 @@ let AlarmService = {
               this._currentAlarm = alarmQueue.shift();
               this._debugCurrentAlarm();
               return;
             }
 
             // no alarm waiting to be set in the queue
             this._currentAlarm = null;
             this._debugCurrentAlarm();
-          }.bind(this),
-          function removeErrorCb(aErrorMsg) {
-            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-          }
+          }.bind(this)
         );
         break;
 
       default:
         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
         break;
     }
   },
@@ -222,39 +219,62 @@ let AlarmService = {
       default:
         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
         break;
     }
 
     aMessageManager.sendAsyncMessage("AlarmsManager:" + aMessageName + ":Return:" + (aSuccess ? "OK" : "KO"), json);
   },
 
+  _removeAlarmFromDb: function _removeAlarmFromDb(aId, aRemoveSuccessCb) {
+    debug("_removeAlarmFromDb()");
+
+    // If the aRemoveSuccessCb is undefined or null, set a 
+    // dummy callback for it which is needed for _db.remove()
+    if (!aRemoveSuccessCb) {
+      aRemoveSuccessCb = function removeSuccessCb() {
+        debug("Remove alarm from DB successfully.");
+      };
+    }
+
+    this._db.remove(
+      aId,
+      aRemoveSuccessCb,
+      function removeErrorCb(aErrorMsg) {
+        throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
+      }
+    );
+  },
+
+  _fireSystemMessage: function _fireSystemMessage(aAlarm) {
+    debug("Fire system message: " + JSON.stringify(aAlarm));
+    let manifestURI = Services.io.newURI(aAlarm.manifestURL, null, null);
+    messenger.sendMessage("alarm", aAlarm, manifestURI);
+  },
+
   _onAlarmFired: function _onAlarmFired() {
     debug("_onAlarmFired()");
 
     if (this._currentAlarm) {
-      debug("Fire system intent: " + JSON.stringify(this._currentAlarm));
-      let manifestURI = Services.io.newURI(this._currentAlarm.manifestURL, null, null);
-      messenger.sendMessage("alarm", this._currentAlarm, manifestURI);
+      this._fireSystemMessage(this._currentAlarm);
+      this._removeAlarmFromDb(this._currentAlarm.id);
       this._currentAlarm = null;
     }
 
-    // reset the next alarm from the queue
-    let nowTime = Date.now();
+    // Reset the next alarm from the queue.
     let alarmQueue = this._alarmQueue;
     while (alarmQueue.length > 0) {
       let nextAlarm = alarmQueue.shift();
       let nextAlarmTime = this._getAlarmTime(nextAlarm);
 
-      // if the next alarm has been expired, directly 
-      // fire system intent for it instead of setting it
-      if (nextAlarmTime <= nowTime) {
-        debug("Fire system intent: " + JSON.stringify(nextAlarm));
-        let manifestURI = Services.io.newURI(nextAlarm.manifestURL, null, null);
-        messenger.sendMessage("alarm", nextAlarm, manifestURI);
+      // If the next alarm has been expired, directly 
+      // fire system message for it instead of setting it.
+      if (nextAlarmTime <= Date.now()) {
+        this._fireSystemMessage(nextAlarm);
+        this._removeAlarmFromDb(nextAlarm.id);
       } else {
         this._currentAlarm = nextAlarm;
         break;
       }
     }
     this._debugCurrentAlarm();
   },