Bug 777226 - Alarm API - If an alarm cannot be fired when the device shuts down, it should be fired when powering up. r=vivien
authorGene Lian <clian@mozilla.com>
Thu, 16 Aug 2012 10:46:40 +0800
changeset 105054 d51a3bf72b9c549903868a998783bf3ba3f69130
parent 105053 3e2cbfcaccd444308bf24a7f28e6f4f6284f4013
child 105055 eb0deea718b3184c1056587a77612baa70721a4a
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersvivien
bugs777226
milestone17.0a1
Bug 777226 - Alarm API - If an alarm cannot be fired when the device shuts down, it should be fired when powering up. r=vivien
dom/alarm/AlarmService.jsm
--- a/dom/alarm/AlarmService.jsm
+++ b/dom/alarm/AlarmService.jsm
@@ -300,22 +300,26 @@ let AlarmService = {
       null,
       function getAllSuccessCb(aAlarms) {
         debug("Callback after getting alarms from database: " + JSON.stringify(aAlarms));
 
         // clear any alarms set or queued in the cache
         let alarmQueue = this._alarmQueue;
         alarmQueue.length = 0;
         this._currentAlarm = null;
-        
-        // only add the alarm that is valid
-        let nowTime = Date.now();
+
+        // Only restore the alarm that's not yet expired; otherwise,
+        // fire a system message for it and remove it from database.
         aAlarms.forEach(function addAlarm(aAlarm) {
-          if (this._getAlarmTime(aAlarm) > nowTime)
+          if (this._getAlarmTime(aAlarm) > Date.now()) {
             alarmQueue.push(aAlarm);
+          } else {
+            this._fireSystemMessage(aAlarm);
+            this._removeAlarmFromDb(aAlarm.id, null);
+          }
         }.bind(this));
 
         // set the next alarm from queue
         if (alarmQueue.length) {
           alarmQueue.sort(this._sortAlarmByTimeStamps.bind(this));
           this._currentAlarm = alarmQueue.shift();
         }
 
@@ -324,17 +328,17 @@ let AlarmService = {
       function getAllErrorCb(aErrorMsg) {
         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
       }
     );
   },
 
   _getAlarmTime: function _getAlarmTime(aAlarm) {
     let alarmTime = (new Date(aAlarm.date)).getTime();
-    
+
     // For an alarm specified with "ignoreTimezone",
     // it must be fired respect to the user's timezone.
     // Supposing an alarm was set at 7:00pm at Tokyo,
     // it must be gone off at 7:00pm respect to Paris' 
     // local time when the user is located at Paris.
     // We can adjust the alarm UTC time by calculating
     // the difference of the orginal timezone and the 
     // current timezone.