From ba7e09e7782832b1153e3207ef5548aa0eb256ef Mon Sep 17 00:00:00 2001 From: Matthew Mecca Date: Sat, 23 Feb 2013 19:08:53 -0500 Subject: [PATCH] Bug 757762 - Lightning's alarms too late / internal time is wrong. r=philipp --- calendar/base/src/calAlarmService.js | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/calendar/base/src/calAlarmService.js b/calendar/base/src/calAlarmService.js index 36e7425dc8..54c792f56a 100644 --- a/calendar/base/src/calAlarmService.js +++ b/calendar/base/src/calAlarmService.js @@ -8,6 +8,8 @@ Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); const kHoursBetweenUpdates = 6; +const kSleepMonitorInterval = 60000; +const kSleepMonitorTolerance = 1000; function nowUTC() { return cal.jsDateToDateTime(new Date()).getInTimezone(cal.UTC()); @@ -30,6 +32,38 @@ function calAlarmService() { this.mTimerMap = {}; this.mObservers = new calListenerBag(Components.interfaces.calIAlarmServiceObserver); + this.mSleepMonitor = { + service: this, + interval: kSleepMonitorInterval, + timer: null, + expected: null, + + checkExpected: function sm_checkExpected() { + let now = Date.now(); + if (now - this.expected > kSleepMonitorTolerance) { + cal.LOG("[calAlarmService] Sleep cycle detected, reloading alarms"); + this.service.shutdown(); + this.service.startup(); + } else { + this.expected = now + this.interval; + } + }, + + start: function sm_start() { + this.stop(); + this.expected = Date.now() + this.interval; + this.timer = newTimerWithCallback(this.checkExpected.bind(this), + this.interval, true); + }, + + stop: function sm_stop() { + if (this.timer) { + this.timer.cancel(); + this.timer = null; + } + } + }; + this.calendarObserver = { alarmService: this, @@ -271,6 +305,12 @@ calAlarmService.prototype = { this.mUpdateTimer = newTimerWithCallback(timerCallback, kHoursBetweenUpdates * 3600000, true); + // The sleep monitor needs to be started on platforms that don't support wake_notification + if (Services.appinfo.OS != "WINNT" && Services.appinfo.OS != "Darwin") { + cal.LOG("[calAlarmService] Starting sleep monitor."); + this.mSleepMonitor.start(); + } + this.mStarted = true; }, @@ -303,6 +343,8 @@ calAlarmService.prototype = { Services.obs.removeObserver(this, "xpcom-shutdown"); Services.obs.removeObserver(this, "wake_notification"); + this.mSleepMonitor.stop(); + this.mStarted = false; },