1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
10 function debug(aStr) {
12 dump("AlarmsManager: " + aStr + "\n");
15 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
17 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
18 Cu.import("resource://gre/modules/Services.jsm");
19 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
21 function AlarmsManager() {
25 AlarmsManager.prototype = {
26 __proto__: DOMRequestIpcHelper.prototype,
28 contractID : "@mozilla.org/alarmsManager;1",
30 classID : Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}"),
32 QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
33 Ci.nsISupportsWeakReference,
36 add: function add(aDate, aRespectTimezone, aData) {
39 if (!this._manifestURL) {
40 debug("Cannot add alarms for non-installed apps.");
41 throw Components.results.NS_ERROR_FAILURE;
45 throw Components.results.NS_ERROR_INVALID_ARG;
48 let isIgnoreTimezone = true;
50 switch (aRespectTimezone) {
52 isIgnoreTimezone = false;
55 case "ignoreTimezone":
56 isIgnoreTimezone = true;
60 throw Components.results.NS_ERROR_INVALID_ARG;
66 // Run JSON.stringify() in the sand box with the principal of the calling
67 // web page to ensure no cross-origin object is involved. A "Permission
68 // Denied" error will be thrown in case of privilege violation.
69 let sandbox = new Cu.Sandbox(Cu.getWebIDLCallerPrincipal());
71 data = JSON.parse(Cu.evalInSandbox("JSON.stringify(data)", sandbox));
73 let request = this.createRequest();
74 this._cpmm.sendAsyncMessage("AlarmsManager:Add",
75 { requestId: this.getRequestId(request),
77 ignoreTimezone: isIgnoreTimezone,
79 pageURL: this._pageURL,
80 manifestURL: this._manifestURL });
84 remove: function remove(aId) {
87 this._cpmm.sendAsyncMessage("AlarmsManager:Remove",
88 { id: aId, manifestURL: this._manifestURL });
91 getAll: function getAll() {
94 let request = this.createRequest();
95 this._cpmm.sendAsyncMessage("AlarmsManager:GetAll",
96 { requestId: this.getRequestId(request),
97 manifestURL: this._manifestURL });
101 receiveMessage: function receiveMessage(aMessage) {
102 debug("receiveMessage(): " + aMessage.name);
104 let json = aMessage.json;
105 let request = this.getRequest(json.requestId);
108 debug("No request stored! " + json.requestId);
112 switch (aMessage.name) {
113 case "AlarmsManager:Add:Return:OK":
114 Services.DOMRequest.fireSuccess(request, json.id);
117 case "AlarmsManager:GetAll:Return:OK":
118 // We don't need to expose everything to the web content.
120 json.alarms.forEach(function trimAlarmInfo(aAlarm) {
121 let alarm = { "id": aAlarm.id,
123 "respectTimezone": aAlarm.ignoreTimezone ?
124 "ignoreTimezone" : "honorTimezone",
125 "data": aAlarm.data };
129 Services.DOMRequest.fireSuccess(request,
130 Cu.cloneInto(alarms, this._window));
133 case "AlarmsManager:Add:Return:KO":
134 Services.DOMRequest.fireError(request, json.errorMsg);
137 case "AlarmsManager:GetAll:Return:KO":
138 Services.DOMRequest.fireError(request, json.errorMsg);
142 debug("Wrong message: " + aMessage.name);
146 this.removeRequest(json.requestId);
149 // nsIDOMGlobalPropertyInitializer implementation
150 init: function init(aWindow) {
153 this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
154 .getService(Ci.nsISyncMessageSender);
156 // Add the valid messages to be listened.
157 this.initDOMRequestHelper(aWindow, ["AlarmsManager:Add:Return:OK",
158 "AlarmsManager:Add:Return:KO",
159 "AlarmsManager:GetAll:Return:OK",
160 "AlarmsManager:GetAll:Return:KO"]);
162 // Get the manifest URL if this is an installed app
163 let appsService = Cc["@mozilla.org/AppsService;1"]
164 .getService(Ci.nsIAppsService);
165 let principal = aWindow.document.nodePrincipal;
166 this._pageURL = principal.URI.spec;
167 this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
168 this._window = aWindow;
171 // Called from DOMRequestIpcHelper.
172 uninit: function uninit() {
177 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlarmsManager])