2012-06-22 00:37:49 -07: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";
|
|
|
|
|
2012-06-22 00:39:07 -07:00
|
|
|
/* static functions */
|
|
|
|
const DEBUG = false;
|
2015-03-08 20:22:29 -07:00
|
|
|
const REQUEST_CPU_LOCK_TIMEOUT = 10 * 1000; // 10 seconds.
|
2012-06-22 00:39:07 -07:00
|
|
|
|
|
|
|
function debug(aStr) {
|
|
|
|
if (DEBUG)
|
|
|
|
dump("AlarmsManager: " + aStr + "\n");
|
|
|
|
}
|
|
|
|
|
2012-06-22 00:37:49 -07:00
|
|
|
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/DOMRequestHelper.jsm");
|
|
|
|
|
2015-03-08 20:22:29 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gPowerManagerService",
|
|
|
|
"@mozilla.org/power/powermanagerservice;1",
|
|
|
|
"nsIPowerManagerService");
|
|
|
|
|
2014-06-12 01:33:56 -07:00
|
|
|
function AlarmsManager() {
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("Constructor");
|
2015-03-08 20:22:29 -07:00
|
|
|
|
|
|
|
// A <requestId, {cpuLock, timer}> map.
|
|
|
|
this._cpuLockDict = new Map();
|
2012-06-22 00:37:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AlarmsManager.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2014-06-12 23:19:11 -07:00
|
|
|
contractID : "@mozilla.org/alarmsManager;1",
|
|
|
|
|
|
|
|
classID : Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}"),
|
2012-06-22 00:37:49 -07:00
|
|
|
|
2014-06-12 23:19:11 -07:00
|
|
|
QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
|
2013-11-19 21:33:10 -08:00
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIObserver]),
|
2012-06-22 00:37:49 -07:00
|
|
|
|
|
|
|
add: function add(aDate, aRespectTimezone, aData) {
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("add()");
|
|
|
|
|
2012-07-17 03:38:24 -07:00
|
|
|
if (!this._manifestURL) {
|
|
|
|
debug("Cannot add alarms for non-installed apps.");
|
|
|
|
throw Components.results.NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:46:59 -07:00
|
|
|
if (!aDate) {
|
|
|
|
throw Components.results.NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2012-06-22 00:39:07 -07:00
|
|
|
let isIgnoreTimezone = true;
|
2014-06-12 01:33:56 -07:00
|
|
|
|
2012-06-22 00:39:07 -07:00
|
|
|
switch (aRespectTimezone) {
|
|
|
|
case "honorTimezone":
|
|
|
|
isIgnoreTimezone = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ignoreTimezone":
|
|
|
|
isIgnoreTimezone = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-07-24 10:46:59 -07:00
|
|
|
throw Components.results.NS_ERROR_INVALID_ARG;
|
2012-06-22 00:39:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:49:10 -07:00
|
|
|
let data = aData;
|
|
|
|
if (aData) {
|
|
|
|
// Run JSON.stringify() in the sand box with the principal of the calling
|
|
|
|
// web page to ensure no cross-origin object is involved. A "Permission
|
|
|
|
// Denied" error will be thrown in case of privilege violation.
|
|
|
|
let sandbox = new Cu.Sandbox(Cu.getWebIDLCallerPrincipal());
|
|
|
|
sandbox.data = aData;
|
|
|
|
data = JSON.parse(Cu.evalInSandbox("JSON.stringify(data)", sandbox));
|
|
|
|
}
|
2012-06-22 00:39:07 -07:00
|
|
|
let request = this.createRequest();
|
2015-03-08 20:22:29 -07:00
|
|
|
let requestId = this.getRequestId(request);
|
|
|
|
this._lockCpuForRequest(requestId);
|
2014-06-12 01:33:56 -07:00
|
|
|
this._cpmm.sendAsyncMessage("AlarmsManager:Add",
|
2015-03-08 20:22:29 -07:00
|
|
|
{ requestId: requestId,
|
2014-06-12 01:33:56 -07:00
|
|
|
date: aDate,
|
|
|
|
ignoreTimezone: isIgnoreTimezone,
|
2014-10-29 20:49:10 -07:00
|
|
|
data: data,
|
2014-06-12 01:33:56 -07:00
|
|
|
pageURL: this._pageURL,
|
|
|
|
manifestURL: this._manifestURL });
|
2012-06-22 00:39:07 -07:00
|
|
|
return request;
|
2012-06-22 00:37:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: function remove(aId) {
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("remove()");
|
|
|
|
|
2014-06-12 01:33:56 -07:00
|
|
|
this._cpmm.sendAsyncMessage("AlarmsManager:Remove",
|
|
|
|
{ id: aId, manifestURL: this._manifestURL });
|
2012-06-22 00:37:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getAll: function getAll() {
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("getAll()");
|
|
|
|
|
|
|
|
let request = this.createRequest();
|
2014-06-12 01:33:56 -07:00
|
|
|
this._cpmm.sendAsyncMessage("AlarmsManager:GetAll",
|
|
|
|
{ requestId: this.getRequestId(request),
|
|
|
|
manifestURL: this._manifestURL });
|
2012-06-22 00:39:07 -07:00
|
|
|
return request;
|
2012-06-22 00:37:49 -07:00
|
|
|
},
|
|
|
|
|
2012-06-22 00:39:07 -07:00
|
|
|
receiveMessage: function receiveMessage(aMessage) {
|
|
|
|
debug("receiveMessage(): " + aMessage.name);
|
|
|
|
|
|
|
|
let json = aMessage.json;
|
2012-07-13 05:01:43 -07:00
|
|
|
let request = this.getRequest(json.requestId);
|
2012-06-22 00:39:07 -07:00
|
|
|
|
|
|
|
if (!request) {
|
2012-07-13 05:01:43 -07:00
|
|
|
debug("No request stored! " + json.requestId);
|
2012-06-22 00:39:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "AlarmsManager:Add:Return:OK":
|
2015-03-08 20:22:29 -07:00
|
|
|
this._unlockCpuForRequest(json.requestId);
|
2012-06-22 00:39:07 -07:00
|
|
|
Services.DOMRequest.fireSuccess(request, json.id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "AlarmsManager:GetAll:Return:OK":
|
2012-12-14 04:04:04 -08:00
|
|
|
// We don't need to expose everything to the web content.
|
|
|
|
let alarms = [];
|
|
|
|
json.alarms.forEach(function trimAlarmInfo(aAlarm) {
|
2014-06-12 01:33:56 -07:00
|
|
|
let alarm = { "id": aAlarm.id,
|
|
|
|
"date": aAlarm.date,
|
2012-12-14 04:04:04 -08:00
|
|
|
"respectTimezone": aAlarm.ignoreTimezone ?
|
2013-08-02 05:34:54 -07:00
|
|
|
"ignoreTimezone" : "honorTimezone",
|
2014-07-13 19:46:09 -07:00
|
|
|
"data": aAlarm.data };
|
2012-12-14 04:04:04 -08:00
|
|
|
alarms.push(alarm);
|
2014-07-13 19:46:09 -07:00
|
|
|
});
|
2014-06-12 01:33:56 -07:00
|
|
|
|
2012-12-14 04:04:04 -08:00
|
|
|
Services.DOMRequest.fireSuccess(request,
|
2014-02-01 11:06:59 -08:00
|
|
|
Cu.cloneInto(alarms, this._window));
|
2012-06-22 00:39:07 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "AlarmsManager:Add:Return:KO":
|
2015-03-08 20:22:29 -07:00
|
|
|
this._unlockCpuForRequest(json.requestId);
|
2012-06-22 00:39:07 -07:00
|
|
|
Services.DOMRequest.fireError(request, json.errorMsg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "AlarmsManager:GetAll:Return:KO":
|
|
|
|
Services.DOMRequest.fireError(request, json.errorMsg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
debug("Wrong message: " + aMessage.name);
|
|
|
|
break;
|
|
|
|
}
|
2014-06-12 01:33:56 -07:00
|
|
|
|
2012-07-13 05:01:43 -07:00
|
|
|
this.removeRequest(json.requestId);
|
2012-06-22 00:39:07 -07:00
|
|
|
},
|
|
|
|
|
2012-06-22 00:37:49 -07:00
|
|
|
// nsIDOMGlobalPropertyInitializer implementation
|
|
|
|
init: function init(aWindow) {
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("init()");
|
|
|
|
|
2013-08-02 05:34:54 -07:00
|
|
|
this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsISyncMessageSender);
|
2012-06-22 00:39:07 -07:00
|
|
|
|
2012-06-22 00:37:49 -07:00
|
|
|
// Add the valid messages to be listened.
|
2013-08-02 05:34:54 -07:00
|
|
|
this.initDOMRequestHelper(aWindow, ["AlarmsManager:Add:Return:OK",
|
|
|
|
"AlarmsManager:Add:Return:KO",
|
|
|
|
"AlarmsManager:GetAll:Return:OK",
|
|
|
|
"AlarmsManager:GetAll:Return:KO"]);
|
2012-07-05 00:53:08 -07:00
|
|
|
|
|
|
|
// Get the manifest URL if this is an installed app
|
2012-08-13 09:58:38 -07:00
|
|
|
let appsService = Cc["@mozilla.org/AppsService;1"]
|
|
|
|
.getService(Ci.nsIAppsService);
|
2014-07-13 19:46:09 -07:00
|
|
|
let principal = aWindow.document.nodePrincipal;
|
|
|
|
this._pageURL = principal.URI.spec;
|
|
|
|
this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
|
2012-12-14 04:04:04 -08:00
|
|
|
this._window = aWindow;
|
2012-06-22 00:37:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Called from DOMRequestIpcHelper.
|
|
|
|
uninit: function uninit() {
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("uninit()");
|
2012-06-22 00:37:49 -07:00
|
|
|
},
|
2015-03-08 20:22:29 -07:00
|
|
|
|
|
|
|
_lockCpuForRequest: function (aRequestId) {
|
|
|
|
if (this._cpuLockDict.has(aRequestId)) {
|
|
|
|
debug('Cpu wakelock for request ' + aRequestId + ' has been acquired. ' +
|
|
|
|
'You may call this function repeatedly or requestId is collision.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire a lock for given request and save for lookup lately.
|
|
|
|
debug('Acquire cpu lock for request ' + aRequestId);
|
|
|
|
let cpuLockInfo = {
|
|
|
|
cpuLock: gPowerManagerService.newWakeLock("cpu"),
|
|
|
|
timer: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer)
|
|
|
|
};
|
|
|
|
this._cpuLockDict.set(aRequestId, cpuLockInfo);
|
|
|
|
|
|
|
|
// Start a timer to prevent from non-responding request.
|
|
|
|
cpuLockInfo.timer.initWithCallback(() => {
|
|
|
|
debug('Request timeout! Release the cpu lock');
|
|
|
|
this._unlockCpuForRequest(aRequestId);
|
|
|
|
}, REQUEST_CPU_LOCK_TIMEOUT, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
},
|
|
|
|
|
|
|
|
_unlockCpuForRequest: function(aRequestId) {
|
|
|
|
let cpuLockInfo = this._cpuLockDict.get(aRequestId);
|
|
|
|
if (!cpuLockInfo) {
|
|
|
|
debug('The cpu lock for requestId ' + aRequestId + ' is either invalid ' +
|
|
|
|
'or has been released.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the cpu lock and cancel the timer.
|
|
|
|
debug('Release the cpu lock for ' + aRequestId);
|
|
|
|
cpuLockInfo.cpuLock.unlock();
|
|
|
|
cpuLockInfo.timer.cancel();
|
|
|
|
this._cpuLockDict.delete(aRequestId);
|
|
|
|
},
|
|
|
|
|
2012-06-22 00:37:49 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlarmsManager])
|