bug 1246732 add in alarms schema r?kmag draft
authorAndy McKay <amckay@mozilla.com>
Wed, 02 Mar 2016 16:06:10 -0800
changeset 336289 c21fada927d0fe627848c080e9fc37acfb4af2ae
parent 336288 ebe5512e508966480a445231bc5728fb67313846
child 515360 d316d67d6133a313ca4ab0b75e678d5b18b5787c
push id12028
push userbmo:amckay@mozilla.com
push dateThu, 03 Mar 2016 00:09:51 +0000
reviewerskmag
bugs1246732
milestone47.0a1
bug 1246732 add in alarms schema r?kmag MozReview-Commit-ID: BQTVhvqgilb
toolkit/components/extensions/Extension.jsm
toolkit/components/extensions/ext-alarms.js
toolkit/components/extensions/schemas/alarms.json
toolkit/components/extensions/schemas/jar.mn
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -70,16 +70,17 @@ ExtensionManagement.registerScript("chro
 ExtensionManagement.registerScript("chrome://extensions/content/ext-extension.js");
 ExtensionManagement.registerScript("chrome://extensions/content/ext-webNavigation.js");
 ExtensionManagement.registerScript("chrome://extensions/content/ext-webRequest.js");
 ExtensionManagement.registerScript("chrome://extensions/content/ext-storage.js");
 ExtensionManagement.registerScript("chrome://extensions/content/ext-test.js");
 
 const BASE_SCHEMA = "chrome://extensions/content/schemas/manifest.json";
 
+ExtensionManagement.registerSchema("chrome://extensions/content/schemas/alarms.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/cookies.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/downloads.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/extension.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/extension_types.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/i18n.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/idle.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/runtime.json");
 ExtensionManagement.registerSchema("chrome://extensions/content/schemas/storage.json");
--- a/toolkit/components/extensions/ext-alarms.js
+++ b/toolkit/components/extensions/ext-alarms.js
@@ -85,86 +85,64 @@ extensions.on("shutdown", (type, extensi
   for (let alarm of alarmsMap.get(extension)) {
     alarm.clear();
   }
   alarmsMap.delete(extension);
   alarmCallbacksMap.delete(extension);
 });
 /* eslint-enable mozilla/balanced-listeners */
 
-extensions.registerPrivilegedAPI("alarms", (extension, context) => {
+extensions.registerSchemaAPI("alarms", "alarms", (extension, context) => {
   return {
     alarms: {
-      create: function(...args) {
-        let name = "", alarmInfo;
-        if (args.length == 1) {
-          alarmInfo = args[0];
-        } else {
-          [name, alarmInfo] = args;
-        }
-
+      create: function(name, alarmInfo) {
+        name = name || "";
         let alarm = new Alarm(extension, name, alarmInfo);
         alarmsMap.get(extension).add(alarm);
       },
 
-      get: function(...args) {
-        let name = "", callback;
-        if (args.length == 1) {
-          callback = args[0];
-        } else {
-          [name, callback] = args;
+      get: function(name) {
+        name = name || "";
+        for (let alarm of alarmsMap.get(extension)) {
+          if (alarm.name === name) {
+            return Promise.resolve(alarm.data);
+          }
         }
-
-        let promise = new Promise((resolve, reject) => {
-          for (let alarm of alarmsMap.get(extension)) {
-            if (alarm.name == name) {
-              return resolve(alarm.data);
-            }
-          }
-          reject("No matching alarm");
-        });
-
-        return context.wrapPromise(promise, callback);
+        return Promise.reject({message: `Alarm ${name} not found.`});
       },
 
-      getAll: function(callback) {
+      getAll: function() {
         let alarms = alarmsMap.get(extension);
         let result = Array.from(alarms, alarm => alarm.data);
-        return context.wrapPromise(Promise.resolve(result), callback);
+        return Promise.resolve(result);
       },
 
-      clear: function(...args) {
-        let name = "", callback;
-        if (args.length == 1) {
-          callback = args[0];
-        } else {
-          [name, callback] = args;
-        }
-
+      clear: function(name) {
+        name = name || "";
         let alarms = alarmsMap.get(extension);
         let cleared = false;
         for (let alarm of alarms) {
           if (alarm.name == name) {
             alarm.clear();
             cleared = true;
             break;
           }
         }
 
-        return context.wrapPromise(Promise.resolve(cleared), callback);
+        return Promise.resolve(cleared);
       },
 
-      clearAll: function(callback) {
+      clearAll: function() {
         let alarms = alarmsMap.get(extension);
         let cleared = false;
         for (let alarm of alarms) {
           alarm.clear();
           cleared = true;
         }
-        return context.wrapPromise(Promise.resolve(cleared), callback);
+        return Promise.resolve(cleared);
       },
 
       onAlarm: new EventManager(context, "alarms.onAlarm", fire => {
         let callback = alarm => {
           fire(alarm.data);
         };
 
         alarmCallbacksMap.get(extension).add(callback);
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/schemas/alarms.json
@@ -0,0 +1,144 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+  {
+    "namespace": "alarms",
+    "types": [
+      {
+        "id": "Alarm",
+        "type": "object",
+        "properties": {
+          "name": {
+            "type": "string",
+            "description": "Name of this alarm."
+          },
+          "scheduledTime": {
+            "type": "number",
+            "description": "Time when the alarm is scheduled to fire, in milliseconds past the epoch."
+          },
+          "periodInMinutes": {
+            "type": "number",
+            "optional": true,
+            "description": "When present, signals that the alarm triggers periodically after so many minutes."
+          }
+        }
+      }
+    ],
+    "functions": [
+      {
+        "name": "create",
+        "type": "function",
+        "description": "Creates an alarm. After the delay is expired, the onAlarm event is fired. If there is another alarm with the same name (or no name if none is specified), it will be cancelled and replaced by this alarm.",
+        "parameters": [
+          {
+            "type": "string",
+            "name": "name",
+            "optional": true,
+            "description": "Optional name to identify this alarm. Defaults to the empty string."
+          },
+          {
+            "type": "object",
+            "name": "alarmInfo",
+            "description": "Details about the alarm. The alarm first fires either at 'when' milliseconds past the epoch (if 'when' is provided), after 'delayInMinutes' minutes from the current time (if 'delayInMinutes' is provided instead), or after 'periodInMinutes' minutes from the current time (if only 'periodInMinutes' is provided). Users should never provide both 'when' and 'delayInMinutes'. If 'periodInMinutes' is provided, then the alarm recurs repeatedly after that many minutes.",
+            "properties": {
+              "when": {"type": "number", "optional": true,
+                "description": "Time when the alarm is scheduled to first fire, in milliseconds past the epoch."},
+              "delayInMinutes": {"type": "number", "optional": true,
+                "description": "Number of minutes from the current time after which the alarm should first fire."},
+              "periodInMinutes": {"type": "number", "optional": true,
+                "description": "Number of minutes after which the alarm should recur repeatedly."}
+            }
+          }
+        ]
+      },
+      {
+        "name": "get",
+        "type": "function",
+        "description": "Retrieves details about the specified alarm.",
+        "async": "callback",
+        "parameters": [
+          {
+            "type": "string",
+            "name": "name",
+            "optional": true,
+            "description": "The name of the alarm to get. Defaults to the empty string."
+          },
+          {
+            "type": "function",
+            "name": "callback",
+            "parameters": [
+              { "name": "alarm", "$ref": "Alarm" }
+            ]
+          }
+        ]
+      },
+      {
+        "name": "getAll",
+        "type": "function",
+        "description": "Gets an array of all the alarms.",
+        "async": "callback",
+        "parameters": [
+          {
+            "type": "function",
+            "name": "callback",
+            "parameters": [
+              { "name": "alarms", "type": "array", "items": { "$ref": "Alarm" } }
+            ]
+          }
+        ]
+      },
+      {
+        "name": "clear",
+        "type": "function",
+        "description": "Clears the alarm with the given name.",
+        "async": "callback",
+        "parameters": [
+          {
+            "type": "string",
+            "name": "name",
+            "optional": true,
+            "description": "The name of the alarm to clear. Defaults to the empty string."
+          },
+          {
+            "type": "function",
+            "name": "callback",
+            "parameters": [
+              { "name": "wasCleared", "type": "boolean", "description": "Whether an alarm of the given name was found to clear." }
+            ]
+          }
+        ]
+      },
+      {
+        "name": "clearAll",
+        "type": "function",
+        "description": "Clears all alarms.",
+        "async": "callback",
+        "parameters": [
+          {
+            "type": "function",
+            "name": "callback",
+            "parameters": [
+              { "name": "wasCleared", "type": "boolean", "description": "Whether any alarm was found to clear." }
+            ]
+          }
+        ]
+      }
+    ],
+    "events": [
+      {
+        "name": "onAlarm",
+        "type": "function",
+        "description": "Fired when an alarm has expired. Useful for transient background pages.",
+        "parameters": [
+          {
+            "name": "name",
+            "$ref": "Alarm",
+            "description": "The alarm that has expired."
+          }
+        ]
+      }
+    ]
+  }
+]
--- a/toolkit/components/extensions/schemas/jar.mn
+++ b/toolkit/components/extensions/schemas/jar.mn
@@ -1,14 +1,15 @@
 # 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/.
 
 toolkit.jar:
 % content extensions %content/extensions/
+    content/extensions/schemas/alarms.json
     content/extensions/schemas/cookies.json
     content/extensions/schemas/downloads.json
     content/extensions/schemas/extension.json
     content/extensions/schemas/extension_types.json
     content/extensions/schemas/i18n.json
     content/extensions/schemas/idle.json
     content/extensions/schemas/manifest.json
     content/extensions/schemas/runtime.json