dom/alarm/AlarmsManager.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 /* static functions */
michael@0 8 const DEBUG = false;
michael@0 9
michael@0 10 function debug(aStr) {
michael@0 11 if (DEBUG)
michael@0 12 dump("AlarmsManager: " + aStr + "\n");
michael@0 13 }
michael@0 14
michael@0 15 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
michael@0 16
michael@0 17 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 18 Cu.import("resource://gre/modules/Services.jsm");
michael@0 19 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
michael@0 20
michael@0 21 const ALARMSMANAGER_CONTRACTID = "@mozilla.org/alarmsManager;1";
michael@0 22 const ALARMSMANAGER_CID = Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}");
michael@0 23 const nsIDOMMozAlarmsManager = Ci.nsIDOMMozAlarmsManager;
michael@0 24 const nsIClassInfo = Ci.nsIClassInfo;
michael@0 25
michael@0 26 function AlarmsManager()
michael@0 27 {
michael@0 28 debug("Constructor");
michael@0 29 }
michael@0 30
michael@0 31 AlarmsManager.prototype = {
michael@0 32
michael@0 33 __proto__: DOMRequestIpcHelper.prototype,
michael@0 34
michael@0 35 classID : ALARMSMANAGER_CID,
michael@0 36
michael@0 37 QueryInterface : XPCOMUtils.generateQI([nsIDOMMozAlarmsManager,
michael@0 38 Ci.nsIDOMGlobalPropertyInitializer,
michael@0 39 Ci.nsISupportsWeakReference,
michael@0 40 Ci.nsIObserver]),
michael@0 41
michael@0 42 classInfo : XPCOMUtils.generateCI({ classID: ALARMSMANAGER_CID,
michael@0 43 contractID: ALARMSMANAGER_CONTRACTID,
michael@0 44 classDescription: "AlarmsManager",
michael@0 45 interfaces: [nsIDOMMozAlarmsManager],
michael@0 46 flags: nsIClassInfo.DOM_OBJECT }),
michael@0 47
michael@0 48 add: function add(aDate, aRespectTimezone, aData) {
michael@0 49 debug("add()");
michael@0 50
michael@0 51 if (!this._manifestURL) {
michael@0 52 debug("Cannot add alarms for non-installed apps.");
michael@0 53 throw Components.results.NS_ERROR_FAILURE;
michael@0 54 }
michael@0 55
michael@0 56 if (!aDate) {
michael@0 57 throw Components.results.NS_ERROR_INVALID_ARG;
michael@0 58 }
michael@0 59
michael@0 60 let isIgnoreTimezone = true;
michael@0 61 switch (aRespectTimezone) {
michael@0 62 case "honorTimezone":
michael@0 63 isIgnoreTimezone = false;
michael@0 64 break;
michael@0 65
michael@0 66 case "ignoreTimezone":
michael@0 67 isIgnoreTimezone = true;
michael@0 68 break;
michael@0 69
michael@0 70 default:
michael@0 71 throw Components.results.NS_ERROR_INVALID_ARG;
michael@0 72 break;
michael@0 73 }
michael@0 74
michael@0 75 // Run JSON.stringify() in the sand box with the principal of the calling
michael@0 76 // web page to ensure no cross-origin object is involved. A "Permission
michael@0 77 // Denied" error will be thrown in case of privilege violation.
michael@0 78 let sandbox = new Cu.Sandbox(this._window.document.nodePrincipal);
michael@0 79 sandbox.data = aData;
michael@0 80 let data = Cu.evalInSandbox("JSON.stringify(data)", sandbox);
michael@0 81 let request = this.createRequest();
michael@0 82 this._cpmm.sendAsyncMessage(
michael@0 83 "AlarmsManager:Add",
michael@0 84 { requestId: this.getRequestId(request),
michael@0 85 date: aDate,
michael@0 86 ignoreTimezone: isIgnoreTimezone,
michael@0 87 data: JSON.parse(data),
michael@0 88 pageURL: this._pageURL,
michael@0 89 manifestURL: this._manifestURL }
michael@0 90 );
michael@0 91 return request;
michael@0 92 },
michael@0 93
michael@0 94 remove: function remove(aId) {
michael@0 95 debug("remove()");
michael@0 96
michael@0 97 this._cpmm.sendAsyncMessage(
michael@0 98 "AlarmsManager:Remove",
michael@0 99 { id: aId, manifestURL: this._manifestURL }
michael@0 100 );
michael@0 101 },
michael@0 102
michael@0 103 getAll: function getAll() {
michael@0 104 debug("getAll()");
michael@0 105
michael@0 106 let request = this.createRequest();
michael@0 107 this._cpmm.sendAsyncMessage(
michael@0 108 "AlarmsManager:GetAll",
michael@0 109 { requestId: this.getRequestId(request), manifestURL: this._manifestURL }
michael@0 110 );
michael@0 111 return request;
michael@0 112 },
michael@0 113
michael@0 114 receiveMessage: function receiveMessage(aMessage) {
michael@0 115 debug("receiveMessage(): " + aMessage.name);
michael@0 116
michael@0 117 let json = aMessage.json;
michael@0 118 let request = this.getRequest(json.requestId);
michael@0 119
michael@0 120 if (!request) {
michael@0 121 debug("No request stored! " + json.requestId);
michael@0 122 return;
michael@0 123 }
michael@0 124
michael@0 125 switch (aMessage.name) {
michael@0 126 case "AlarmsManager:Add:Return:OK":
michael@0 127 Services.DOMRequest.fireSuccess(request, json.id);
michael@0 128 break;
michael@0 129
michael@0 130 case "AlarmsManager:GetAll:Return:OK":
michael@0 131 // We don't need to expose everything to the web content.
michael@0 132 let alarms = [];
michael@0 133 json.alarms.forEach(function trimAlarmInfo(aAlarm) {
michael@0 134 let alarm = { "id": aAlarm.id,
michael@0 135 "date": aAlarm.date,
michael@0 136 "respectTimezone": aAlarm.ignoreTimezone ?
michael@0 137 "ignoreTimezone" : "honorTimezone",
michael@0 138 "data": aAlarm.data };
michael@0 139 alarms.push(alarm);
michael@0 140 });
michael@0 141 Services.DOMRequest.fireSuccess(request,
michael@0 142 Cu.cloneInto(alarms, this._window));
michael@0 143 break;
michael@0 144
michael@0 145 case "AlarmsManager:Add:Return:KO":
michael@0 146 Services.DOMRequest.fireError(request, json.errorMsg);
michael@0 147 break;
michael@0 148
michael@0 149 case "AlarmsManager:GetAll:Return:KO":
michael@0 150 Services.DOMRequest.fireError(request, json.errorMsg);
michael@0 151 break;
michael@0 152
michael@0 153 default:
michael@0 154 debug("Wrong message: " + aMessage.name);
michael@0 155 break;
michael@0 156 }
michael@0 157 this.removeRequest(json.requestId);
michael@0 158 },
michael@0 159
michael@0 160 // nsIDOMGlobalPropertyInitializer implementation
michael@0 161 init: function init(aWindow) {
michael@0 162 debug("init()");
michael@0 163
michael@0 164 // Set navigator.mozAlarms to null.
michael@0 165 if (!Services.prefs.getBoolPref("dom.mozAlarms.enabled")) {
michael@0 166 return null;
michael@0 167 }
michael@0 168
michael@0 169 // Only pages with perm set can use the alarms.
michael@0 170 let principal = aWindow.document.nodePrincipal;
michael@0 171 let perm =
michael@0 172 Services.perms.testExactPermissionFromPrincipal(principal, "alarms");
michael@0 173 if (perm != Ci.nsIPermissionManager.ALLOW_ACTION) {
michael@0 174 return null;
michael@0 175 }
michael@0 176
michael@0 177 // SystemPrincipal documents do not have any origin.
michael@0 178 // Reject them for now.
michael@0 179 if (!principal.URI) {
michael@0 180 return null;
michael@0 181 }
michael@0 182
michael@0 183 this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
michael@0 184 .getService(Ci.nsISyncMessageSender);
michael@0 185
michael@0 186 // Add the valid messages to be listened.
michael@0 187 this.initDOMRequestHelper(aWindow, ["AlarmsManager:Add:Return:OK",
michael@0 188 "AlarmsManager:Add:Return:KO",
michael@0 189 "AlarmsManager:GetAll:Return:OK",
michael@0 190 "AlarmsManager:GetAll:Return:KO"]);
michael@0 191
michael@0 192 // Get the manifest URL if this is an installed app
michael@0 193 let appsService = Cc["@mozilla.org/AppsService;1"]
michael@0 194 .getService(Ci.nsIAppsService);
michael@0 195 this._pageURL = principal.URI.spec;
michael@0 196 this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
michael@0 197 this._window = aWindow;
michael@0 198 },
michael@0 199
michael@0 200 // Called from DOMRequestIpcHelper.
michael@0 201 uninit: function uninit() {
michael@0 202 debug("uninit()");
michael@0 203 },
michael@0 204 }
michael@0 205
michael@0 206 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlarmsManager])

mercurial