dom/alarm/AlarmsManager.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/alarm/AlarmsManager.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,206 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +/* static functions */
    1.11 +const DEBUG = false;
    1.12 +
    1.13 +function debug(aStr) {
    1.14 +  if (DEBUG)
    1.15 +    dump("AlarmsManager: " + aStr + "\n");
    1.16 +}
    1.17 +
    1.18 +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
    1.19 +
    1.20 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.21 +Cu.import("resource://gre/modules/Services.jsm");
    1.22 +Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
    1.23 +
    1.24 +const ALARMSMANAGER_CONTRACTID = "@mozilla.org/alarmsManager;1";
    1.25 +const ALARMSMANAGER_CID        = Components.ID("{fea1e884-9b05-11e1-9b64-87a7016c3860}");
    1.26 +const nsIDOMMozAlarmsManager   = Ci.nsIDOMMozAlarmsManager;
    1.27 +const nsIClassInfo             = Ci.nsIClassInfo;
    1.28 +
    1.29 +function AlarmsManager()
    1.30 +{
    1.31 +  debug("Constructor");
    1.32 +}
    1.33 +
    1.34 +AlarmsManager.prototype = {
    1.35 +
    1.36 +  __proto__: DOMRequestIpcHelper.prototype,
    1.37 +
    1.38 +  classID : ALARMSMANAGER_CID,
    1.39 +
    1.40 +  QueryInterface : XPCOMUtils.generateQI([nsIDOMMozAlarmsManager,
    1.41 +                                          Ci.nsIDOMGlobalPropertyInitializer,
    1.42 +                                          Ci.nsISupportsWeakReference,
    1.43 +                                          Ci.nsIObserver]),
    1.44 +
    1.45 +  classInfo : XPCOMUtils.generateCI({ classID: ALARMSMANAGER_CID,
    1.46 +                                      contractID: ALARMSMANAGER_CONTRACTID,
    1.47 +                                      classDescription: "AlarmsManager",
    1.48 +                                      interfaces: [nsIDOMMozAlarmsManager],
    1.49 +                                      flags: nsIClassInfo.DOM_OBJECT }),
    1.50 +
    1.51 +  add: function add(aDate, aRespectTimezone, aData) {
    1.52 +    debug("add()");
    1.53 +
    1.54 +    if (!this._manifestURL) {
    1.55 +      debug("Cannot add alarms for non-installed apps.");
    1.56 +      throw Components.results.NS_ERROR_FAILURE;
    1.57 +    }
    1.58 +
    1.59 +    if (!aDate) {
    1.60 +      throw Components.results.NS_ERROR_INVALID_ARG;
    1.61 +    }
    1.62 +
    1.63 +    let isIgnoreTimezone = true;
    1.64 +    switch (aRespectTimezone) {
    1.65 +      case "honorTimezone":
    1.66 +        isIgnoreTimezone = false;
    1.67 +        break;
    1.68 +
    1.69 +      case "ignoreTimezone":
    1.70 +        isIgnoreTimezone = true;
    1.71 +        break;
    1.72 +
    1.73 +      default:
    1.74 +        throw Components.results.NS_ERROR_INVALID_ARG;
    1.75 +        break;
    1.76 +    }
    1.77 +
    1.78 +    // Run JSON.stringify() in the sand box with the principal of the calling
    1.79 +    // web page to ensure no cross-origin object is involved. A "Permission
    1.80 +    // Denied" error will be thrown in case of privilege violation.
    1.81 +    let sandbox = new Cu.Sandbox(this._window.document.nodePrincipal);
    1.82 +    sandbox.data = aData;
    1.83 +    let data = Cu.evalInSandbox("JSON.stringify(data)", sandbox);
    1.84 +    let request = this.createRequest();
    1.85 +    this._cpmm.sendAsyncMessage(
    1.86 +      "AlarmsManager:Add",
    1.87 +      { requestId: this.getRequestId(request),
    1.88 +        date: aDate,
    1.89 +        ignoreTimezone: isIgnoreTimezone,
    1.90 +        data: JSON.parse(data),
    1.91 +        pageURL: this._pageURL,
    1.92 +        manifestURL: this._manifestURL }
    1.93 +    );
    1.94 +    return request;
    1.95 +  },
    1.96 +
    1.97 +  remove: function remove(aId) {
    1.98 +    debug("remove()");
    1.99 +
   1.100 +    this._cpmm.sendAsyncMessage(
   1.101 +      "AlarmsManager:Remove",
   1.102 +      { id: aId, manifestURL: this._manifestURL }
   1.103 +    );
   1.104 +  },
   1.105 +
   1.106 +  getAll: function getAll() {
   1.107 +    debug("getAll()");
   1.108 +
   1.109 +    let request = this.createRequest();
   1.110 +    this._cpmm.sendAsyncMessage(
   1.111 +      "AlarmsManager:GetAll",
   1.112 +      { requestId: this.getRequestId(request), manifestURL: this._manifestURL }
   1.113 +    );
   1.114 +    return request;
   1.115 +  },
   1.116 +
   1.117 +  receiveMessage: function receiveMessage(aMessage) {
   1.118 +    debug("receiveMessage(): " + aMessage.name);
   1.119 +
   1.120 +    let json = aMessage.json;
   1.121 +    let request = this.getRequest(json.requestId);
   1.122 +
   1.123 +    if (!request) {
   1.124 +      debug("No request stored! " + json.requestId);
   1.125 +      return;
   1.126 +    }
   1.127 +
   1.128 +    switch (aMessage.name) {
   1.129 +      case "AlarmsManager:Add:Return:OK":
   1.130 +        Services.DOMRequest.fireSuccess(request, json.id);
   1.131 +        break;
   1.132 +
   1.133 +      case "AlarmsManager:GetAll:Return:OK":
   1.134 +        // We don't need to expose everything to the web content.
   1.135 +        let alarms = [];
   1.136 +        json.alarms.forEach(function trimAlarmInfo(aAlarm) {
   1.137 +          let alarm = { "id":              aAlarm.id,
   1.138 +                        "date":            aAlarm.date,
   1.139 +                        "respectTimezone": aAlarm.ignoreTimezone ?
   1.140 +                                             "ignoreTimezone" : "honorTimezone",
   1.141 +                        "data":            aAlarm.data };
   1.142 +          alarms.push(alarm);
   1.143 +        });
   1.144 +        Services.DOMRequest.fireSuccess(request,
   1.145 +                                        Cu.cloneInto(alarms, this._window));
   1.146 +        break;
   1.147 +
   1.148 +      case "AlarmsManager:Add:Return:KO":
   1.149 +        Services.DOMRequest.fireError(request, json.errorMsg);
   1.150 +        break;
   1.151 +
   1.152 +      case "AlarmsManager:GetAll:Return:KO":
   1.153 +        Services.DOMRequest.fireError(request, json.errorMsg);
   1.154 +        break;
   1.155 +
   1.156 +      default:
   1.157 +        debug("Wrong message: " + aMessage.name);
   1.158 +        break;
   1.159 +    }
   1.160 +    this.removeRequest(json.requestId);
   1.161 +   },
   1.162 +
   1.163 +  // nsIDOMGlobalPropertyInitializer implementation
   1.164 +  init: function init(aWindow) {
   1.165 +    debug("init()");
   1.166 +
   1.167 +    // Set navigator.mozAlarms to null.
   1.168 +    if (!Services.prefs.getBoolPref("dom.mozAlarms.enabled")) {
   1.169 +      return null;
   1.170 +    }
   1.171 +
   1.172 +    // Only pages with perm set can use the alarms.
   1.173 +    let principal = aWindow.document.nodePrincipal;
   1.174 +    let perm =
   1.175 +      Services.perms.testExactPermissionFromPrincipal(principal, "alarms");
   1.176 +    if (perm != Ci.nsIPermissionManager.ALLOW_ACTION) {
   1.177 +      return null;
   1.178 +    }
   1.179 +
   1.180 +    // SystemPrincipal documents do not have any origin.
   1.181 +    // Reject them for now.
   1.182 +    if (!principal.URI) {
   1.183 +      return null;
   1.184 +    }
   1.185 +
   1.186 +    this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
   1.187 +                   .getService(Ci.nsISyncMessageSender);
   1.188 +
   1.189 +    // Add the valid messages to be listened.
   1.190 +    this.initDOMRequestHelper(aWindow, ["AlarmsManager:Add:Return:OK",
   1.191 +                                        "AlarmsManager:Add:Return:KO",
   1.192 +                                        "AlarmsManager:GetAll:Return:OK",
   1.193 +                                        "AlarmsManager:GetAll:Return:KO"]);
   1.194 +
   1.195 +    // Get the manifest URL if this is an installed app
   1.196 +    let appsService = Cc["@mozilla.org/AppsService;1"]
   1.197 +                        .getService(Ci.nsIAppsService);
   1.198 +    this._pageURL = principal.URI.spec;
   1.199 +    this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
   1.200 +    this._window = aWindow;
   1.201 +  },
   1.202 +
   1.203 +  // Called from DOMRequestIpcHelper.
   1.204 +  uninit: function uninit() {
   1.205 +    debug("uninit()");
   1.206 +  },
   1.207 +}
   1.208 +
   1.209 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlarmsManager])

mercurial