Bumping gaia.json for 4 gaia revision(s) a=gaia-bump
[gecko.git] / dom / alarm / AlarmsManager.js
blob101d3f47e568af771a73a5a4c2ca7598799b0005
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/. */
5 "use strict";
7 /* static functions */
8 const DEBUG = false;
10 function debug(aStr) {
11   if (DEBUG)
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() {
22   debug("Constructor");
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,
34                                           Ci.nsIObserver]),
36   add: function add(aDate, aRespectTimezone, aData) {
37     debug("add()");
39     if (!this._manifestURL) {
40       debug("Cannot add alarms for non-installed apps.");
41       throw Components.results.NS_ERROR_FAILURE;
42     }
44     if (!aDate) {
45       throw Components.results.NS_ERROR_INVALID_ARG;
46     }
48     let isIgnoreTimezone = true;
50     switch (aRespectTimezone) {
51       case "honorTimezone":
52         isIgnoreTimezone = false;
53         break;
55       case "ignoreTimezone":
56         isIgnoreTimezone = true;
57         break;
59       default:
60         throw Components.results.NS_ERROR_INVALID_ARG;
61         break;
62     }
64     let data = aData;
65     if (aData) {
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());
70       sandbox.data = aData;
71       data = JSON.parse(Cu.evalInSandbox("JSON.stringify(data)", sandbox));
72     }
73     let request = this.createRequest();
74     this._cpmm.sendAsyncMessage("AlarmsManager:Add",
75                                 { requestId: this.getRequestId(request),
76                                   date: aDate,
77                                   ignoreTimezone: isIgnoreTimezone,
78                                   data: data,
79                                   pageURL: this._pageURL,
80                                   manifestURL: this._manifestURL });
81     return request;
82   },
84   remove: function remove(aId) {
85     debug("remove()");
87     this._cpmm.sendAsyncMessage("AlarmsManager:Remove",
88                                 { id: aId, manifestURL: this._manifestURL });
89   },
91   getAll: function getAll() {
92     debug("getAll()");
94     let request = this.createRequest();
95     this._cpmm.sendAsyncMessage("AlarmsManager:GetAll",
96                                 { requestId: this.getRequestId(request),
97                                   manifestURL: this._manifestURL });
98     return request;
99   },
101   receiveMessage: function receiveMessage(aMessage) {
102     debug("receiveMessage(): " + aMessage.name);
104     let json = aMessage.json;
105     let request = this.getRequest(json.requestId);
107     if (!request) {
108       debug("No request stored! " + json.requestId);
109       return;
110     }
112     switch (aMessage.name) {
113       case "AlarmsManager:Add:Return:OK":
114         Services.DOMRequest.fireSuccess(request, json.id);
115         break;
117       case "AlarmsManager:GetAll:Return:OK":
118         // We don't need to expose everything to the web content.
119         let alarms = [];
120         json.alarms.forEach(function trimAlarmInfo(aAlarm) {
121           let alarm = { "id": aAlarm.id,
122                         "date": aAlarm.date,
123                         "respectTimezone": aAlarm.ignoreTimezone ?
124                                              "ignoreTimezone" : "honorTimezone",
125                         "data": aAlarm.data };
126           alarms.push(alarm);
127         });
129         Services.DOMRequest.fireSuccess(request,
130                                         Cu.cloneInto(alarms, this._window));
131         break;
133       case "AlarmsManager:Add:Return:KO":
134         Services.DOMRequest.fireError(request, json.errorMsg);
135         break;
137       case "AlarmsManager:GetAll:Return:KO":
138         Services.DOMRequest.fireError(request, json.errorMsg);
139         break;
141       default:
142         debug("Wrong message: " + aMessage.name);
143         break;
144     }
146     this.removeRequest(json.requestId);
147    },
149   // nsIDOMGlobalPropertyInitializer implementation
150   init: function init(aWindow) {
151     debug("init()");
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;
169   },
171   // Called from DOMRequestIpcHelper.
172   uninit: function uninit() {
173     debug("uninit()");
174   },
177 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlarmsManager])