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;
|
|
|
|
|
|
|
|
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");
|
2012-12-14 04:04:04 -08:00
|
|
|
Cu.import("resource://gre/modules/ObjectWrapper.jsm");
|
2012-06-22 00:37:49 -07:00
|
|
|
|
|
|
|
const ALARMSMANAGER_CONTRACTID = "@mozilla.org/alarmsManager;1";
|
|
|
|
const ALARMSMANAGER_CID = Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}");
|
|
|
|
const nsIDOMMozAlarmsManager = Ci.nsIDOMMozAlarmsManager;
|
|
|
|
const nsIClassInfo = Ci.nsIClassInfo;
|
|
|
|
|
|
|
|
function AlarmsManager()
|
|
|
|
{
|
2012-06-22 00:39:07 -07:00
|
|
|
debug("Constructor");
|
2012-06-22 00:37:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AlarmsManager.prototype = {
|
|
|
|
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
|
|
|
classID : ALARMSMANAGER_CID,
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMMozAlarmsManager, Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
|
|
|
|
classInfo : XPCOMUtils.generateCI({ classID: ALARMSMANAGER_CID,
|
|
|
|
contractID: ALARMSMANAGER_CONTRACTID,
|
|
|
|
classDescription: "AlarmsManager",
|
|
|
|
interfaces: [nsIDOMMozAlarmsManager],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT }),
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-22 00:39:07 -07:00
|
|
|
let isIgnoreTimezone = true;
|
|
|
|
switch (aRespectTimezone) {
|
|
|
|
case "honorTimezone":
|
|
|
|
isIgnoreTimezone = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ignoreTimezone":
|
|
|
|
isIgnoreTimezone = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let request = this.createRequest();
|
|
|
|
this._cpmm.sendAsyncMessage(
|
|
|
|
"AlarmsManager:Add",
|
2012-08-14 19:40:12 -07:00
|
|
|
{ requestId: this.getRequestId(request),
|
|
|
|
date: aDate,
|
|
|
|
ignoreTimezone: isIgnoreTimezone,
|
|
|
|
data: aData,
|
|
|
|
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()");
|
|
|
|
|
|
|
|
return this._cpmm.sendSyncMessage(
|
|
|
|
"AlarmsManager:Remove",
|
2012-07-31 22:58:49 -07:00
|
|
|
{ id: aId, manifestURL: this._manifestURL }
|
2012-06-22 00:39:07 -07:00
|
|
|
);
|
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();
|
|
|
|
this._cpmm.sendAsyncMessage(
|
|
|
|
"AlarmsManager:GetAll",
|
2012-07-31 22:58:49 -07:00
|
|
|
{ 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":
|
|
|
|
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) {
|
|
|
|
let alarm = { "id": aAlarm.id,
|
|
|
|
"date": aAlarm.date,
|
|
|
|
"respectTimezone": aAlarm.ignoreTimezone ?
|
|
|
|
"ignoreTimezone" : "honorTimezone",
|
|
|
|
"data": aAlarm.data };
|
|
|
|
alarms.push(alarm);
|
|
|
|
});
|
|
|
|
Services.DOMRequest.fireSuccess(request,
|
|
|
|
ObjectWrapper.wrap(alarms, this._window));
|
2012-06-22 00:39:07 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "AlarmsManager:Add:Return:KO":
|
|
|
|
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;
|
|
|
|
}
|
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()");
|
|
|
|
|
2012-06-22 00:37:49 -07:00
|
|
|
// Set navigator.mozAlarms to null.
|
|
|
|
if (!Services.prefs.getBoolPref("dom.mozAlarms.enabled"))
|
|
|
|
return null;
|
|
|
|
|
2012-11-28 03:13:58 -08:00
|
|
|
// Only pages with perm set can use the alarms.
|
2012-06-22 00:37:49 -07:00
|
|
|
let principal = aWindow.document.nodePrincipal;
|
2012-07-20 18:08:18 -07:00
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(principal, "alarms");
|
2012-11-28 03:13:58 -08:00
|
|
|
if (perm != Ci.nsIPermissionManager.ALLOW_ACTION)
|
2012-06-22 00:37:49 -07:00
|
|
|
return null;
|
|
|
|
|
2012-06-22 00:39:07 -07:00
|
|
|
this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender);
|
|
|
|
|
2012-06-22 00:37:49 -07:00
|
|
|
// Add the valid messages to be listened.
|
2012-06-22 00:39:07 -07:00
|
|
|
this.initHelper(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);
|
2012-08-14 19:40:12 -07:00
|
|
|
this._pageURL = principal.URI.spec;
|
2012-08-13 09:58:38 -07:00
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlarmsManager])
|