Bug 1246732 - Add in alarms schema. r=kmag

MozReview-Commit-ID: BQTVhvqgilb

--HG--
extra : rebase_source : 512932f9e770052df13d8cba13e0365ca0eab44b
This commit is contained in:
Andy McKay 2016-03-02 16:06:10 -08:00
Родитель 40eeb3fa24
Коммит 58af3a5ffe
4 изменённых файлов: 163 добавлений и 39 удалений

Просмотреть файл

@ -75,6 +75,7 @@ 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");

Просмотреть файл

@ -90,55 +90,33 @@ extensions.on("shutdown", (type, 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;
}
let promise = new Promise((resolve, reject) => {
for (let alarm of alarmsMap.get(extension)) {
if (alarm.name == name) {
return resolve(alarm.data);
}
get: function(name) {
name = name || "";
for (let alarm of alarmsMap.get(extension)) {
if (alarm.name === name) {
return Promise.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) {
@ -149,17 +127,17 @@ extensions.registerPrivilegedAPI("alarms", (extension, context) => {
}
}
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 => {

Просмотреть файл

@ -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."
}
]
}
]
}
]

Просмотреть файл

@ -4,6 +4,7 @@
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