2012-06-22 07:39:07 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/* static functions */
|
|
|
|
const DEBUG = false;
|
|
|
|
|
|
|
|
function debug(aStr) {
|
|
|
|
if (DEBUG)
|
|
|
|
dump("AlarmService: " + aStr + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
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/AlarmDB.jsm");
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["AlarmService"];
|
2012-06-22 07:39:07 +00:00
|
|
|
|
2013-05-28 17:29:02 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "appsService", function() {
|
|
|
|
return Cc["@mozilla.org/AppsService;1"].getService(Ci.nsIAppsService);
|
|
|
|
});
|
|
|
|
|
2012-08-27 14:13:02 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
2012-06-22 07:39:07 +00:00
|
|
|
|
2012-07-05 07:53:08 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "messenger", function() {
|
|
|
|
return Cc["@mozilla.org/system-message-internal;1"].getService(Ci.nsISystemMessagesInternal);
|
|
|
|
});
|
|
|
|
|
2012-11-01 02:30:18 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "powerManagerService", function() {
|
|
|
|
return Cc["@mozilla.org/power/powermanagerservice;1"].getService(Ci.nsIPowerManagerService);
|
|
|
|
});
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
/**
|
|
|
|
* AlarmService provides an API to schedule alarms using the device's RTC.
|
|
|
|
*
|
|
|
|
* AlarmService is primarily used by the mozAlarms API (navigator.mozAlarms)
|
|
|
|
* which uses IPC to communicate with the service.
|
|
|
|
*
|
|
|
|
* AlarmService can also be used by Gecko code by importing the module and then
|
|
|
|
* using AlarmService.add() and AlarmService.remove(). Only Gecko code running
|
|
|
|
* in the parent process should do this.
|
|
|
|
*/
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.AlarmService = {
|
2012-06-22 07:39:07 +00:00
|
|
|
init: function init() {
|
|
|
|
debug("init()");
|
2013-05-12 17:10:00 +00:00
|
|
|
Services.obs.addObserver(this, "profile-change-teardown", false);
|
2013-05-28 17:29:02 +00:00
|
|
|
Services.obs.addObserver(this, "webapps-clear-data",false);
|
2012-06-22 07:39:07 +00:00
|
|
|
|
|
|
|
this._currentTimezoneOffset = (new Date()).getTimezoneOffset();
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
let alarmHalService =
|
|
|
|
this._alarmHalService = Cc["@mozilla.org/alarmHalService;1"]
|
|
|
|
.getService(Ci.nsIAlarmHalService);
|
|
|
|
|
2012-06-22 07:39:07 +00:00
|
|
|
alarmHalService.setAlarmFiredCb(this._onAlarmFired.bind(this));
|
|
|
|
alarmHalService.setTimezoneChangedCb(this._onTimezoneChanged.bind(this));
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Add the messages to be listened to.
|
2013-05-12 17:10:00 +00:00
|
|
|
this._messages = ["AlarmsManager:GetAll",
|
2012-11-01 02:30:18 +00:00
|
|
|
"AlarmsManager:Add",
|
2013-02-14 20:41:30 +00:00
|
|
|
"AlarmsManager:Remove"];
|
2013-05-12 17:10:00 +00:00
|
|
|
this._messages.forEach(function addMessage(msgName) {
|
|
|
|
ppmm.addMessageListener(msgName, this);
|
2012-06-22 07:39:07 +00:00
|
|
|
}.bind(this));
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Set the indexeddb database.
|
2013-09-28 11:25:46 +00:00
|
|
|
this._db = new AlarmDB();
|
|
|
|
this._db.init();
|
2012-06-22 07:39:07 +00:00
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Variable to save alarms waiting to be set.
|
2012-06-22 07:39:07 +00:00
|
|
|
this._alarmQueue = [];
|
|
|
|
|
|
|
|
this._restoreAlarmsFromDb();
|
|
|
|
},
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Getter/setter to access the current alarm set in system.
|
2012-06-22 07:39:07 +00:00
|
|
|
_alarm: null,
|
|
|
|
get _currentAlarm() {
|
|
|
|
return this._alarm;
|
|
|
|
},
|
|
|
|
set _currentAlarm(aAlarm) {
|
|
|
|
this._alarm = aAlarm;
|
2013-05-30 15:08:07 +00:00
|
|
|
if (!aAlarm) {
|
2012-06-22 07:39:07 +00:00
|
|
|
return;
|
2013-05-30 15:08:07 +00:00
|
|
|
}
|
2012-06-22 07:39:07 +00:00
|
|
|
|
2013-05-30 15:08:07 +00:00
|
|
|
let alarmTimeInMs = this._getAlarmTime(aAlarm);
|
|
|
|
let ns = (alarmTimeInMs % 1000) * 1000000;
|
|
|
|
if (!this._alarmHalService.setAlarm(alarmTimeInMs / 1000, ns)) {
|
2012-06-22 07:39:07 +00:00
|
|
|
throw Components.results.NS_ERROR_FAILURE;
|
2013-05-30 15:08:07 +00:00
|
|
|
}
|
2012-06-22 07:39:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function receiveMessage(aMessage) {
|
|
|
|
debug("receiveMessage(): " + aMessage.name);
|
2012-12-22 11:47:23 +00:00
|
|
|
let json = aMessage.json;
|
2012-06-22 07:39:07 +00:00
|
|
|
|
2012-12-22 11:47:23 +00:00
|
|
|
// To prevent the hacked child process from sending commands to parent
|
|
|
|
// to schedule alarms, we need to check its permission and manifest URL.
|
2013-05-12 17:10:00 +00:00
|
|
|
if (this._messages.indexOf(aMessage.name) != -1) {
|
2012-11-28 11:13:58 +00:00
|
|
|
if (!aMessage.target.assertPermission("alarms")) {
|
|
|
|
debug("Got message from a child process with no 'alarms' permission.");
|
|
|
|
return null;
|
|
|
|
}
|
2012-12-22 11:47:23 +00:00
|
|
|
if (!aMessage.target.assertContainApp(json.manifestURL)) {
|
|
|
|
debug("Got message from a child process containing illegal manifest URL.");
|
|
|
|
return null;
|
|
|
|
}
|
2012-11-28 11:13:58 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 14:13:02 +00:00
|
|
|
let mm = aMessage.target.QueryInterface(Ci.nsIMessageSender);
|
2012-06-22 07:39:07 +00:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "AlarmsManager:GetAll":
|
|
|
|
this._db.getAll(
|
2012-08-01 05:58:49 +00:00
|
|
|
json.manifestURL,
|
2012-06-22 07:39:07 +00:00
|
|
|
function getAllSuccessCb(aAlarms) {
|
2013-05-01 16:02:49 +00:00
|
|
|
debug("Callback after getting alarms from database: " +
|
|
|
|
JSON.stringify(aAlarms));
|
2012-07-26 01:59:20 +00:00
|
|
|
this._sendAsyncMessage(mm, "GetAll", true, json.requestId, aAlarms);
|
2012-06-22 07:39:07 +00:00
|
|
|
}.bind(this),
|
|
|
|
function getAllErrorCb(aErrorMsg) {
|
2012-07-26 01:59:20 +00:00
|
|
|
this._sendAsyncMessage(mm, "GetAll", false, json.requestId, aErrorMsg);
|
2012-06-22 07:39:07 +00:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "AlarmsManager:Add":
|
2013-05-01 16:02:49 +00:00
|
|
|
// Prepare a record for the new alarm to be added.
|
2012-06-22 07:39:07 +00:00
|
|
|
let newAlarm = {
|
2013-05-01 16:02:49 +00:00
|
|
|
date: json.date,
|
|
|
|
ignoreTimezone: json.ignoreTimezone,
|
2012-07-05 07:53:08 +00:00
|
|
|
data: json.data,
|
2012-08-15 02:40:12 +00:00
|
|
|
pageURL: json.pageURL,
|
2012-07-05 07:53:08 +00:00
|
|
|
manifestURL: json.manifestURL
|
2012-06-22 07:39:07 +00:00
|
|
|
};
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
this.add(newAlarm, null,
|
|
|
|
// Receives the alarm ID as the last argument.
|
|
|
|
this._sendAsyncMessage.bind(this, mm, "Add", true, json.requestId),
|
|
|
|
// Receives the error message as the last argument.
|
|
|
|
this._sendAsyncMessage.bind(this, mm, "Add", false, json.requestId)
|
2012-06-22 07:39:07 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "AlarmsManager:Remove":
|
2013-05-01 16:02:49 +00:00
|
|
|
this.remove(json.id, json.manifestURL);
|
2012-06-22 07:39:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
_sendAsyncMessage: function _sendAsyncMessage(aMessageManager, aMessageName,
|
|
|
|
aSuccess, aRequestId, aData) {
|
2012-07-13 12:01:43 +00:00
|
|
|
debug("_sendAsyncMessage()");
|
|
|
|
|
2012-07-26 01:59:20 +00:00
|
|
|
if (!aMessageManager) {
|
|
|
|
debug("Invalid message manager: null");
|
|
|
|
throw Components.results.NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-07-13 12:01:43 +00:00
|
|
|
let json = null;
|
|
|
|
switch (aMessageName)
|
|
|
|
{
|
2012-07-26 01:59:20 +00:00
|
|
|
case "Add":
|
2013-05-01 16:02:49 +00:00
|
|
|
json = aSuccess ?
|
|
|
|
{ requestId: aRequestId, id: aData } :
|
2012-12-14 12:04:04 +00:00
|
|
|
{ requestId: aRequestId, errorMsg: aData };
|
2012-07-13 12:01:43 +00:00
|
|
|
break;
|
|
|
|
|
2012-07-26 01:59:20 +00:00
|
|
|
case "GetAll":
|
2013-05-01 16:02:49 +00:00
|
|
|
json = aSuccess ?
|
|
|
|
{ requestId: aRequestId, alarms: aData } :
|
2012-12-14 12:04:04 +00:00
|
|
|
{ requestId: aRequestId, errorMsg: aData };
|
2012-07-13 12:01:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
aMessageManager.sendAsyncMessage("AlarmsManager:" + aMessageName +
|
|
|
|
":Return:" + (aSuccess ? "OK" : "KO"), json);
|
2012-07-13 12:01:43 +00:00
|
|
|
},
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
_removeAlarmFromDb: function _removeAlarmFromDb(aId, aManifestURL,
|
|
|
|
aRemoveSuccessCb) {
|
2012-07-30 11:17:35 +00:00
|
|
|
debug("_removeAlarmFromDb()");
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// If the aRemoveSuccessCb is undefined or null, set a dummy callback for
|
|
|
|
// it which is needed for _db.remove().
|
2012-07-30 11:17:35 +00:00
|
|
|
if (!aRemoveSuccessCb) {
|
|
|
|
aRemoveSuccessCb = function removeSuccessCb() {
|
|
|
|
debug("Remove alarm from DB successfully.");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.remove(
|
|
|
|
aId,
|
2012-08-01 05:58:49 +00:00
|
|
|
aManifestURL,
|
2012-07-30 11:17:35 +00:00
|
|
|
aRemoveSuccessCb,
|
|
|
|
function removeErrorCb(aErrorMsg) {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
/**
|
|
|
|
* Create a copy of the alarm that does not expose internal fields to
|
|
|
|
* receivers and sticks to the public |respectTimezone| API rather than the
|
|
|
|
* boolean |ignoreTimezone| field.
|
|
|
|
*/
|
|
|
|
_publicAlarm: function _publicAlarm(aAlarm) {
|
|
|
|
let alarm = {
|
|
|
|
"id": aAlarm.id,
|
|
|
|
"date": aAlarm.date,
|
|
|
|
"respectTimezone": aAlarm.ignoreTimezone ?
|
|
|
|
"ignoreTimezone" : "honorTimezone",
|
|
|
|
"data": aAlarm.data
|
|
|
|
};
|
|
|
|
|
|
|
|
return alarm;
|
|
|
|
},
|
|
|
|
|
2012-07-30 11:17:35 +00:00
|
|
|
_fireSystemMessage: function _fireSystemMessage(aAlarm) {
|
|
|
|
debug("Fire system message: " + JSON.stringify(aAlarm));
|
2012-11-01 02:30:18 +00:00
|
|
|
|
2012-07-30 11:17:35 +00:00
|
|
|
let manifestURI = Services.io.newURI(aAlarm.manifestURL, null, null);
|
2012-08-15 02:40:12 +00:00
|
|
|
let pageURI = Services.io.newURI(aAlarm.pageURL, null, null);
|
2012-12-14 12:04:04 +00:00
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
messenger.sendMessage("alarm", this._publicAlarm(aAlarm),
|
|
|
|
pageURI, manifestURI);
|
|
|
|
},
|
|
|
|
|
|
|
|
_notifyAlarmObserver: function _notifyAlarmObserver(aAlarm) {
|
|
|
|
debug("_notifyAlarmObserver()");
|
2012-12-14 12:04:04 +00:00
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
if (aAlarm.manifestURL) {
|
|
|
|
this._fireSystemMessage(aAlarm);
|
|
|
|
} else if (typeof aAlarm.alarmFiredCb === "function") {
|
|
|
|
aAlarm.alarmFiredCb(this._publicAlarm(aAlarm));
|
|
|
|
}
|
2012-07-30 11:17:35 +00:00
|
|
|
},
|
|
|
|
|
2012-06-22 07:39:07 +00:00
|
|
|
_onAlarmFired: function _onAlarmFired() {
|
|
|
|
debug("_onAlarmFired()");
|
|
|
|
|
|
|
|
if (this._currentAlarm) {
|
2012-08-01 05:58:49 +00:00
|
|
|
this._removeAlarmFromDb(this._currentAlarm.id, null);
|
2013-05-01 16:02:49 +00:00
|
|
|
this._notifyAlarmObserver(this._currentAlarm);
|
2012-06-22 07:39:07 +00:00
|
|
|
this._currentAlarm = null;
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:17:35 +00:00
|
|
|
// Reset the next alarm from the queue.
|
2012-06-22 07:39:07 +00:00
|
|
|
let alarmQueue = this._alarmQueue;
|
|
|
|
while (alarmQueue.length > 0) {
|
|
|
|
let nextAlarm = alarmQueue.shift();
|
|
|
|
let nextAlarmTime = this._getAlarmTime(nextAlarm);
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// If the next alarm has been expired, directly notify the observer.
|
|
|
|
// it instead of setting it.
|
2012-07-30 11:17:35 +00:00
|
|
|
if (nextAlarmTime <= Date.now()) {
|
2012-08-01 05:58:49 +00:00
|
|
|
this._removeAlarmFromDb(nextAlarm.id, null);
|
2013-05-01 16:02:49 +00:00
|
|
|
this._notifyAlarmObserver(nextAlarm);
|
2012-06-22 07:39:07 +00:00
|
|
|
} else {
|
|
|
|
this._currentAlarm = nextAlarm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onTimezoneChanged: function _onTimezoneChanged(aTimezoneOffset) {
|
|
|
|
debug("_onTimezoneChanged()");
|
|
|
|
|
|
|
|
this._currentTimezoneOffset = aTimezoneOffset;
|
|
|
|
this._restoreAlarmsFromDb();
|
|
|
|
},
|
|
|
|
|
|
|
|
_restoreAlarmsFromDb: function _restoreAlarmsFromDb() {
|
|
|
|
debug("_restoreAlarmsFromDb()");
|
|
|
|
|
|
|
|
this._db.getAll(
|
2012-08-01 05:58:49 +00:00
|
|
|
null,
|
2012-06-22 07:39:07 +00:00
|
|
|
function getAllSuccessCb(aAlarms) {
|
2013-05-01 16:02:49 +00:00
|
|
|
debug("Callback after getting alarms from database: " +
|
|
|
|
JSON.stringify(aAlarms));
|
2012-06-22 07:39:07 +00:00
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Clear any alarms set or queued in the cache.
|
2012-06-22 07:39:07 +00:00
|
|
|
let alarmQueue = this._alarmQueue;
|
|
|
|
alarmQueue.length = 0;
|
|
|
|
this._currentAlarm = null;
|
2012-08-16 02:46:40 +00:00
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Only restore the alarm that's not yet expired; otherwise, remove it
|
|
|
|
// from the database and notify the observer.
|
2012-06-22 07:39:07 +00:00
|
|
|
aAlarms.forEach(function addAlarm(aAlarm) {
|
2012-08-16 02:46:40 +00:00
|
|
|
if (this._getAlarmTime(aAlarm) > Date.now()) {
|
2012-06-22 07:39:07 +00:00
|
|
|
alarmQueue.push(aAlarm);
|
2012-08-16 02:46:40 +00:00
|
|
|
} else {
|
|
|
|
this._removeAlarmFromDb(aAlarm.id, null);
|
2013-05-01 16:02:49 +00:00
|
|
|
this._notifyAlarmObserver(aAlarm);
|
2012-08-16 02:46:40 +00:00
|
|
|
}
|
2012-06-22 07:39:07 +00:00
|
|
|
}.bind(this));
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// Set the next alarm from queue.
|
2012-06-22 07:39:07 +00:00
|
|
|
if (alarmQueue.length) {
|
|
|
|
alarmQueue.sort(this._sortAlarmByTimeStamps.bind(this));
|
|
|
|
this._currentAlarm = alarmQueue.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
}.bind(this),
|
|
|
|
function getAllErrorCb(aErrorMsg) {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_getAlarmTime: function _getAlarmTime(aAlarm) {
|
2013-07-02 18:29:58 +00:00
|
|
|
// Avoid casting a Date object to a Date again to
|
|
|
|
// preserve milliseconds. See bug 810973.
|
|
|
|
let alarmTime;
|
|
|
|
if (aAlarm.date instanceof Date) {
|
|
|
|
alarmTime = aAlarm.date.getTime();
|
|
|
|
} else {
|
|
|
|
alarmTime = (new Date(aAlarm.date)).getTime();
|
|
|
|
}
|
2012-08-16 02:46:40 +00:00
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
// 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.
|
2013-07-02 18:29:58 +00:00
|
|
|
if (aAlarm.ignoreTimezone) {
|
2012-06-22 07:39:07 +00:00
|
|
|
alarmTime += (this._currentTimezoneOffset - aAlarm.timezoneOffset) * 60000;
|
2013-07-02 18:29:58 +00:00
|
|
|
}
|
2012-06-22 07:39:07 +00:00
|
|
|
return alarmTime;
|
|
|
|
},
|
|
|
|
|
|
|
|
_sortAlarmByTimeStamps: function _sortAlarmByTimeStamps(aAlarm1, aAlarm2) {
|
|
|
|
return this._getAlarmTime(aAlarm1) - this._getAlarmTime(aAlarm2);
|
|
|
|
},
|
|
|
|
|
|
|
|
_debugCurrentAlarm: function _debugCurrentAlarm() {
|
|
|
|
debug("Current alarm: " + JSON.stringify(this._currentAlarm));
|
|
|
|
debug("Alarm queue: " + JSON.stringify(this._alarmQueue));
|
|
|
|
},
|
2013-05-01 16:02:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Add a new alarm. This will set the RTC to fire at the selected date and
|
|
|
|
* notify the caller. Notifications are delivered via System Messages if the
|
|
|
|
* alarm is added on behalf of a app. Otherwise aAlarmFiredCb is called.
|
|
|
|
*
|
|
|
|
* @param object aNewAlarm
|
|
|
|
* Should contain the following literal properties:
|
|
|
|
* - |date| date: when the alarm should timeout.
|
|
|
|
* - |ignoreTimezone| boolean: See [1] for the details.
|
|
|
|
* - |manifestURL| string: Manifest of app on whose behalf the alarm
|
|
|
|
* is added.
|
|
|
|
* - |pageURL| string: The page in the app that receives the system
|
|
|
|
* message.
|
|
|
|
* - |data| object [optional]: Data that can be stored in DB.
|
|
|
|
* @param function aAlarmFiredCb
|
|
|
|
* Callback function invoked when the alarm is fired.
|
|
|
|
* It receives a single argument, the alarm object.
|
|
|
|
* May be null.
|
|
|
|
* @param function aSuccessCb
|
|
|
|
* Callback function to receive an alarm ID (number).
|
|
|
|
* @param function aErrorCb
|
|
|
|
* Callback function to receive an error message (string).
|
|
|
|
* @returns void
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* [1] https://wiki.mozilla.org/WebAPI/AlarmAPI#Proposed_API
|
|
|
|
*/
|
|
|
|
|
|
|
|
add: function(aNewAlarm, aAlarmFiredCb, aSuccessCb, aErrorCb) {
|
|
|
|
debug("add(" + aNewAlarm.date + ")");
|
|
|
|
|
|
|
|
aSuccessCb = aSuccessCb || function() {};
|
|
|
|
aErrorCb = aErrorCb || function() {};
|
|
|
|
|
|
|
|
if (!aNewAlarm) {
|
|
|
|
aErrorCb("alarm is null");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-04 16:13:16 +00:00
|
|
|
if (!aNewAlarm.date) {
|
|
|
|
aErrorCb("alarm.date is null");
|
2013-06-04 08:37:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-05-01 16:02:49 +00:00
|
|
|
|
2013-06-04 16:13:16 +00:00
|
|
|
aNewAlarm['timezoneOffset'] = this._currentTimezoneOffset;
|
|
|
|
|
2013-05-01 16:02:49 +00:00
|
|
|
this._db.add(
|
|
|
|
aNewAlarm,
|
|
|
|
function addSuccessCb(aNewId) {
|
|
|
|
debug("Callback after adding alarm in database.");
|
|
|
|
|
|
|
|
aNewAlarm['id'] = aNewId;
|
|
|
|
|
|
|
|
// Now that the alarm has been added to the database, we can tack on
|
|
|
|
// the non-serializable callback to the in-memory object.
|
|
|
|
aNewAlarm['alarmFiredCb'] = aAlarmFiredCb;
|
|
|
|
|
|
|
|
// If there is no alarm being set in system, set the new alarm.
|
|
|
|
if (this._currentAlarm == null) {
|
|
|
|
this._currentAlarm = aNewAlarm;
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
aSuccessCb(aNewId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the new alarm is earlier than the current alarm, swap them and
|
|
|
|
// push the previous alarm back to queue.
|
|
|
|
let alarmQueue = this._alarmQueue;
|
2013-06-04 16:13:16 +00:00
|
|
|
let aNewAlarmTime = this._getAlarmTime(aNewAlarm);
|
2013-05-01 16:02:49 +00:00
|
|
|
let currentAlarmTime = this._getAlarmTime(this._currentAlarm);
|
|
|
|
if (aNewAlarmTime < currentAlarmTime) {
|
|
|
|
alarmQueue.unshift(this._currentAlarm);
|
|
|
|
this._currentAlarm = aNewAlarm;
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
aSuccessCb(aNewId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the new alarm in the queue.
|
|
|
|
alarmQueue.push(aNewAlarm);
|
|
|
|
alarmQueue.sort(this._sortAlarmByTimeStamps.bind(this));
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
aSuccessCb(aNewId);
|
|
|
|
}.bind(this),
|
|
|
|
function addErrorCb(aErrorMsg) {
|
|
|
|
aErrorCb(aErrorMsg);
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the alarm associated with an ID.
|
|
|
|
*
|
|
|
|
* @param number aAlarmId
|
|
|
|
* The ID of the alarm to be removed.
|
|
|
|
* @param string aManifestURL
|
|
|
|
* Manifest URL for application which added the alarm. (Optional)
|
|
|
|
* @returns void
|
|
|
|
*/
|
|
|
|
remove: function(aAlarmId, aManifestURL) {
|
|
|
|
debug("remove(" + aAlarmId + ", " + aManifestURL + ")");
|
|
|
|
this._removeAlarmFromDb(
|
|
|
|
aAlarmId,
|
|
|
|
aManifestURL,
|
|
|
|
function removeSuccessCb() {
|
|
|
|
debug("Callback after removing alarm from database.");
|
|
|
|
|
|
|
|
// If there are no alarms set, nothing to do.
|
|
|
|
if (!this._currentAlarm) {
|
|
|
|
debug("No alarms set.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the alarm to be removed is in the queue and whether it
|
|
|
|
// belongs to the requesting app.
|
|
|
|
let alarmQueue = this._alarmQueue;
|
|
|
|
if (this._currentAlarm.id != aAlarmId ||
|
|
|
|
this._currentAlarm.manifestURL != aManifestURL) {
|
|
|
|
|
|
|
|
for (let i = 0; i < alarmQueue.length; i++) {
|
|
|
|
if (alarmQueue[i].id == aAlarmId &&
|
|
|
|
alarmQueue[i].manifestURL == aManifestURL) {
|
|
|
|
|
|
|
|
alarmQueue.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The alarm to be removed is the current alarm reset the next alarm
|
|
|
|
// from queue if any.
|
|
|
|
if (alarmQueue.length) {
|
|
|
|
this._currentAlarm = alarmQueue.shift();
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No alarm waiting to be set in the queue.
|
|
|
|
this._currentAlarm = null;
|
|
|
|
this._debugCurrentAlarm();
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2013-05-12 17:10:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case "profile-change-teardown":
|
|
|
|
this.uninit();
|
2013-05-28 17:29:02 +00:00
|
|
|
break;
|
|
|
|
case "webapps-clear-data":
|
|
|
|
let params =
|
|
|
|
aSubject.QueryInterface(Ci.mozIApplicationClearPrivateDataParams);
|
2013-06-21 09:30:40 +00:00
|
|
|
if (!params) {
|
|
|
|
debug("Error! Fail to remove alarms for an uninstalled app.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-28 17:29:02 +00:00
|
|
|
let manifestURL = appsService.getManifestURLByLocalId(params.appId);
|
2013-06-21 09:30:40 +00:00
|
|
|
if (!manifestURL) {
|
|
|
|
debug("Error! Fail to remove alarms for an uninstalled app.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-28 17:29:02 +00:00
|
|
|
this._db.getAll(
|
|
|
|
manifestURL,
|
|
|
|
function getAllSuccessCb(aAlarms) {
|
|
|
|
aAlarms.forEach(function removeAlarm(aAlarm) {
|
|
|
|
this.remove(aAlarm.id, manifestURL);
|
|
|
|
}, this);
|
|
|
|
}.bind(this),
|
|
|
|
function getAllErrorCb(aErrorMsg) {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
2013-05-12 17:10:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function uninit() {
|
|
|
|
debug("uninit()");
|
|
|
|
Services.obs.removeObserver(this, "profile-change-teardown");
|
2013-05-28 17:29:02 +00:00
|
|
|
Services.obs.removeObserver(this, "webapps-clear-data");
|
2013-05-12 17:10:00 +00:00
|
|
|
|
|
|
|
this._messages.forEach(function(aMsgName) {
|
|
|
|
ppmm.removeMessageListener(aMsgName, this);
|
|
|
|
}.bind(this));
|
|
|
|
ppmm = null;
|
|
|
|
|
|
|
|
if (this._db) {
|
|
|
|
this._db.close();
|
|
|
|
}
|
|
|
|
this._db = null;
|
|
|
|
|
|
|
|
this._alarmHalService = null;
|
2013-05-01 16:02:49 +00:00
|
|
|
}
|
2012-06-22 07:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AlarmService.init();
|