Bug 1246787 - [webext] Add schema storage.json (r=kmag)

This commit is contained in:
Bill McCloskey 2015-11-26 10:21:15 -08:00
parent c2f1b59c40
commit cc7eba7dde
7 changed files with 391 additions and 63 deletions

View File

@ -570,6 +570,7 @@ SpecialPowersObserverAPI.prototype = {
}).then(() => { }).then(() => {
this._sendReply(aMessage, "SPExtensionMessage", {id, type: "extensionStarted", args: []}); this._sendReply(aMessage, "SPExtensionMessage", {id, type: "extensionStarted", args: []});
}).catch(e => { }).catch(e => {
dump(`Extension startup failed: ${e}\n${e.stack}`);
this._sendReply(aMessage, "SPExtensionMessage", {id, type: "extensionFailed", args: []}); this._sendReply(aMessage, "SPExtensionMessage", {id, type: "extensionFailed", args: []});
}); });
return undefined; return undefined;

View File

@ -82,6 +82,7 @@ ExtensionManagement.registerSchema("chrome://extensions/content/schemas/extensio
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/i18n.json"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/i18n.json");
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/idle.json"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/idle.json");
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/runtime.json"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/runtime.json");
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/storage.json");
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/test.json"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/test.json");
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/web_navigation.json"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/web_navigation.json");
ExtensionManagement.registerSchema("chrome://extensions/content/schemas/web_request.json"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/web_request.json");
@ -383,16 +384,23 @@ GlobalManager = {
injectAPI(api, browserObj); injectAPI(api, browserObj);
let schemaApi = Management.generateAPIs(extension, context, Management.schemaApis); let schemaApi = Management.generateAPIs(extension, context, Management.schemaApis);
function findPath(path) {
let obj = schemaApi;
for (let elt of path) {
obj = obj[elt];
}
return obj;
};
let schemaWrapper = { let schemaWrapper = {
get cloneScope() { get cloneScope() {
return context.cloneScope; return context.cloneScope;
}, },
callFunction(ns, name, args) { callFunction(path, name, args) {
return schemaApi[ns][name](...args); return findPath(path)[name](...args);
}, },
callAsyncFunction(ns, name, args, callback) { callAsyncFunction(path, name, args, callback) {
// We pass an empty stub function as a default callback for // We pass an empty stub function as a default callback for
// the `chrome` API, so promise objects are not returned, // the `chrome` API, so promise objects are not returned,
// and lastError values are reported immediately. // and lastError values are reported immediately.
@ -402,7 +410,7 @@ GlobalManager = {
let promise; let promise;
try { try {
promise = schemaApi[ns][name](...args); promise = findPath(path)[name](...args);
} catch (e) { } catch (e) {
if (e instanceof context.cloneScope.Error) { if (e instanceof context.cloneScope.Error) {
promise = Promise.reject(e); promise = Promise.reject(e);
@ -415,22 +423,22 @@ GlobalManager = {
return context.wrapPromise(promise || Promise.resolve(), callback); return context.wrapPromise(promise || Promise.resolve(), callback);
}, },
getProperty(ns, name) { getProperty(path, name) {
return schemaApi[ns][name]; return findPath(path)[name];
}, },
setProperty(ns, name, value) { setProperty(path, name, value) {
schemaApi[ns][name] = value; findPath(path)[name] = value;
}, },
addListener(ns, name, listener, args) { addListener(path, name, listener, args) {
return schemaApi[ns][name].addListener.call(null, listener, ...args); return findPath(path)[name].addListener.call(null, listener, ...args);
}, },
removeListener(ns, name, listener) { removeListener(path, name, listener) {
return schemaApi[ns][name].removeListener.call(null, listener); return findPath(path)[name].removeListener.call(null, listener);
}, },
hasListener(ns, name, listener) { hasListener(path, name, listener) {
return schemaApi[ns][name].hasListener.call(null, listener); return findPath(path)[name].hasListener.call(null, listener);
}, },
}; };
Schemas.inject(browserObj, schemaWrapper); Schemas.inject(browserObj, schemaWrapper);

View File

@ -305,7 +305,7 @@ class Entry {
// nothing. |context| is used to call the actual implementation // nothing. |context| is used to call the actual implementation
// of a given function or event. It's an object with properties // of a given function or event. It's an object with properties
// callFunction, addListener, removeListener, and hasListener. // callFunction, addListener, removeListener, and hasListener.
inject(name, dest, context) { inject(path, name, dest, context) {
} }
} }
@ -468,7 +468,7 @@ class StringType extends Type {
return baseType == "string"; return baseType == "string";
} }
inject(name, dest, context) { inject(path, name, dest, context) {
if (this.enumeration) { if (this.enumeration) {
let obj = Cu.createObjectIn(dest, {defineAs: name}); let obj = Cu.createObjectIn(dest, {defineAs: name});
for (let e of this.enumeration) { for (let e of this.enumeration) {
@ -623,6 +623,15 @@ class ObjectType extends Type {
} }
} }
// This type is just a placeholder to be referred to by
// SubModuleProperty. No value is ever expected to have this type.
class SubModuleType extends Type {
constructor(functions) {
super();
this.functions = functions;
}
};
class NumberType extends Type { class NumberType extends Type {
normalize(value, context) { normalize(value, context) {
let r = this.normalizeBase("number", value, context); let r = this.normalizeBase("number", value, context);
@ -749,7 +758,7 @@ class ValueProperty extends Entry {
this.value = value; this.value = value;
} }
inject(name, dest, context) { inject(path, name, dest, context) {
dest[name] = this.value; dest[name] = this.value;
} }
} }
@ -769,14 +778,14 @@ class TypeProperty extends Entry {
throw context.makeError(`${msg} for ${this.namespaceName}.${this.name}.`); throw context.makeError(`${msg} for ${this.namespaceName}.${this.name}.`);
} }
inject(name, dest, context) { inject(path, name, dest, context) {
if (this.unsupported) { if (this.unsupported) {
return; return;
} }
let getStub = () => { let getStub = () => {
this.checkDeprecated(context); this.checkDeprecated(context);
return context.getProperty(this.namespaceName, name); return context.getProperty(path, name);
}; };
let desc = { let desc = {
@ -803,20 +812,57 @@ class TypeProperty extends Entry {
} }
} }
class SubModuleProperty extends Entry {
// A SubModuleProperty represents a tree of objects and properties
// to expose to an extension. Currently we support only a limited
// form of sub-module properties, where "$ref" points to a
// SubModuleType containing a list of functions and "properties" is
// a list of additional simple properties.
//
// name: Name of the property stuff is being added to.
// namespaceName: Namespace in which the property lives.
// reference: Name of the type defining the functions to add to the property.
// properties: Additional properties to add to the module (unsupported).
constructor(name, namespaceName, reference, properties) {
super();
this.name = name;
this.namespaceName = namespaceName;
this.reference = reference;
this.properties = properties;
}
inject(path, name, dest, wrapperFuncs) {
let obj = Cu.createObjectIn(dest, {defineAs: name});
let ns = Schemas.namespaces.get(this.namespaceName);
let type = ns.get(this.reference);
if (!type || !(type instanceof SubModuleType)) {
throw new Error(`Internal error: ${this.namespaceName}.${this.reference} is not a sub-module`);
}
let functions = type.functions;
for (let fun of functions) {
fun.inject(path.concat(name), fun.name, obj, wrapperFuncs);
}
// TODO: Inject this.properties.
}
};
// This class is a base class for FunctionEntrys and Events. It takes // This class is a base class for FunctionEntrys and Events. It takes
// care of validating parameter lists (i.e., handling of optional // care of validating parameter lists (i.e., handling of optional
// parameters and parameter type checking). // parameters and parameter type checking).
class CallEntry extends Entry { class CallEntry extends Entry {
constructor(schema, namespaceName, name, parameters, allowAmbiguousOptionalArguments) { constructor(schema, path, name, parameters, allowAmbiguousOptionalArguments) {
super(schema); super(schema);
this.namespaceName = namespaceName; this.path = path;
this.name = name; this.name = name;
this.parameters = parameters; this.parameters = parameters;
this.allowAmbiguousOptionalArguments = allowAmbiguousOptionalArguments; this.allowAmbiguousOptionalArguments = allowAmbiguousOptionalArguments;
} }
throwError(context, msg) { throwError(context, msg) {
throw context.makeError(`${msg} for ${this.namespaceName}.${this.name}.`); throw context.makeError(`${msg} for ${this.path.join('.')}.${this.name}.`);
} }
checkParameters(args, context) { checkParameters(args, context) {
@ -891,15 +937,15 @@ class CallEntry extends Entry {
// Represents a "function" defined in a schema namespace. // Represents a "function" defined in a schema namespace.
class FunctionEntry extends CallEntry { class FunctionEntry extends CallEntry {
constructor(schema, namespaceName, name, type, unsupported, allowAmbiguousOptionalArguments, returns) { constructor(schema, path, name, type, unsupported, allowAmbiguousOptionalArguments, returns) {
super(schema, namespaceName, name, type.parameters, allowAmbiguousOptionalArguments); super(schema, path, name, type.parameters, allowAmbiguousOptionalArguments);
this.unsupported = unsupported; this.unsupported = unsupported;
this.returns = returns; this.returns = returns;
this.isAsync = type.isAsync; this.isAsync = type.isAsync;
} }
inject(name, dest, context) { inject(path, name, dest, context) {
if (this.unsupported) { if (this.unsupported) {
return; return;
} }
@ -910,13 +956,13 @@ class FunctionEntry extends CallEntry {
this.checkDeprecated(context); this.checkDeprecated(context);
let actuals = this.checkParameters(args, context); let actuals = this.checkParameters(args, context);
let callback = actuals.pop(); let callback = actuals.pop();
return context.callAsyncFunction(this.namespaceName, name, actuals, callback); return context.callAsyncFunction(path, name, actuals, callback);
}; };
} else { } else {
stub = (...args) => { stub = (...args) => {
this.checkDeprecated(context); this.checkDeprecated(context);
let actuals = this.checkParameters(args, context); let actuals = this.checkParameters(args, context);
return context.callFunction(this.namespaceName, name, actuals); return context.callFunction(path, name, actuals);
}; };
} }
Cu.exportFunction(stub, dest, {defineAs: name}); Cu.exportFunction(stub, dest, {defineAs: name});
@ -925,8 +971,8 @@ class FunctionEntry extends CallEntry {
// Represents an "event" defined in a schema namespace. // Represents an "event" defined in a schema namespace.
class Event extends CallEntry { class Event extends CallEntry {
constructor(schema, namespaceName, name, type, extraParameters, unsupported) { constructor(schema, path, name, type, extraParameters, unsupported) {
super(schema, namespaceName, name, extraParameters); super(schema, path, name, extraParameters);
this.type = type; this.type = type;
this.unsupported = unsupported; this.unsupported = unsupported;
} }
@ -939,7 +985,7 @@ class Event extends CallEntry {
return r.value; return r.value;
} }
inject(name, dest, context) { inject(path, name, dest, context) {
if (this.unsupported) { if (this.unsupported) {
return; return;
} }
@ -947,17 +993,17 @@ class Event extends CallEntry {
let addStub = (listener, ...args) => { let addStub = (listener, ...args) => {
listener = this.checkListener(listener, context); listener = this.checkListener(listener, context);
let actuals = this.checkParameters(args, context); let actuals = this.checkParameters(args, context);
return context.addListener(this.namespaceName, name, listener, actuals); return context.addListener(this.path, name, listener, actuals);
}; };
let removeStub = (listener) => { let removeStub = (listener) => {
listener = this.checkListener(listener, context); listener = this.checkListener(listener, context);
return context.removeListener(this.namespaceName, name, listener); return context.removeListener(this.path, name, listener);
}; };
let hasStub = (listener) => { let hasStub = (listener) => {
listener = this.checkListener(listener, context); listener = this.checkListener(listener, context);
return context.hasListener(this.namespaceName, name, listener); return context.hasListener(this.path, name, listener);
}; };
let obj = Cu.createObjectIn(dest, {defineAs: name}); let obj = Cu.createObjectIn(dest, {defineAs: name});
@ -981,7 +1027,7 @@ this.Schemas = {
ns.set(symbol, value); ns.set(symbol, value);
}, },
parseType(namespaceName, type, extraProperties = []) { parseType(path, type, extraProperties = []) {
let allowedProperties = new Set(extraProperties); let allowedProperties = new Set(extraProperties);
// Do some simple validation of our own schemas. // Do some simple validation of our own schemas.
@ -989,7 +1035,7 @@ this.Schemas = {
let allowedSet = new Set([...allowedProperties, ...extra, "description", "deprecated"]); let allowedSet = new Set([...allowedProperties, ...extra, "description", "deprecated"]);
for (let prop of Object.keys(type)) { for (let prop of Object.keys(type)) {
if (!allowedSet.has(prop)) { if (!allowedSet.has(prop)) {
throw new Error(`Internal error: Namespace ${namespaceName} has invalid type property "${prop}" in type "${type.id || JSON.stringify(type)}"`); throw new Error(`Internal error: Namespace ${path.join('.')} has invalid type property "${prop}" in type "${type.id || JSON.stringify(type)}"`);
} }
} }
} }
@ -997,12 +1043,12 @@ this.Schemas = {
if ("choices" in type) { if ("choices" in type) {
checkTypeProperties("choices"); checkTypeProperties("choices");
let choices = type.choices.map(t => this.parseType(namespaceName, t)); let choices = type.choices.map(t => this.parseType(path, t));
return new ChoiceType(type, choices); return new ChoiceType(type, choices);
} else if ("$ref" in type) { } else if ("$ref" in type) {
checkTypeProperties("$ref"); checkTypeProperties("$ref");
let ref = type.$ref; let ref = type.$ref;
let ns = namespaceName; let ns = path[0];
if (ref.includes(".")) { if (ref.includes(".")) {
[ns, ref] = ref.split("."); [ns, ref] = ref.split(".");
} }
@ -1054,10 +1100,17 @@ this.Schemas = {
type.maxLength || Infinity, type.maxLength || Infinity,
pattern, pattern,
format); format);
} else if (type.type == "object" && "functions" in type) {
checkTypeProperties("functions");
// The path we pass in here is only used for error messages.
let functions = type.functions.map(fun => this.parseFunction(path.concat(type.id), fun));
return new SubModuleType(functions);
} else if (type.type == "object") { } else if (type.type == "object") {
let parseProperty = (type, extraProps = []) => { let parseProperty = (type, extraProps = []) => {
return { return {
type: this.parseType(namespaceName, type, type: this.parseType(path, type,
["unsupported", ...extraProps]), ["unsupported", ...extraProps]),
optional: type.optional || false, optional: type.optional || false,
unsupported: type.unsupported || false, unsupported: type.unsupported || false,
@ -1086,7 +1139,7 @@ this.Schemas = {
let additionalProperties = null; let additionalProperties = null;
if (type.additionalProperties) { if (type.additionalProperties) {
additionalProperties = this.parseType(namespaceName, type.additionalProperties); additionalProperties = this.parseType(path, type.additionalProperties);
} }
if ("$extend" in type) { if ("$extend" in type) {
@ -1098,7 +1151,7 @@ this.Schemas = {
return new ObjectType(type, properties, additionalProperties, patternProperties, type.isInstanceOf || null); return new ObjectType(type, properties, additionalProperties, patternProperties, type.isInstanceOf || null);
} else if (type.type == "array") { } else if (type.type == "array") {
checkTypeProperties("items", "minItems", "maxItems"); checkTypeProperties("items", "minItems", "maxItems");
return new ArrayType(type, this.parseType(namespaceName, type.items), return new ArrayType(type, this.parseType(path, type.items),
type.minItems || 0, type.maxItems || Infinity); type.minItems || 0, type.maxItems || Infinity);
} else if (type.type == "number") { } else if (type.type == "number") {
checkTypeProperties(); checkTypeProperties();
@ -1121,7 +1174,7 @@ this.Schemas = {
let isCallback = isAsync && param.name == type.async; let isCallback = isAsync && param.name == type.async;
parameters.push({ parameters.push({
type: this.parseType(namespaceName, param, ["name", "optional"]), type: this.parseType(path, param, ["name", "optional"]),
name: param.name, name: param.name,
optional: param.optional == null ? isCallback : param.optional, optional: param.optional == null ? isCallback : param.optional,
}); });
@ -1148,11 +1201,22 @@ this.Schemas = {
} }
}, },
parseFunction(path, fun) {
let f = new FunctionEntry(fun, path, fun.name,
this.parseType(path, fun,
["name", "unsupported", "returns",
"allowAmbiguousOptionalArguments"]),
fun.unsupported || false,
fun.allowAmbiguousOptionalArguments || false,
fun.returns || null);
return f;
},
loadType(namespaceName, type) { loadType(namespaceName, type) {
if ("$extend" in type) { if ("$extend" in type) {
this.extendType(namespaceName, type); this.extendType(namespaceName, type);
} else { } else {
this.register(namespaceName, type.id, this.parseType(namespaceName, type, ["id"])); this.register(namespaceName, type.id, this.parseType([namespaceName], type, ["id"]));
} }
}, },
@ -1169,7 +1233,7 @@ this.Schemas = {
throw new Error(`Internal error: Attempt to extend a non-extensible type ${type.$extend}`); throw new Error(`Internal error: Attempt to extend a non-extensible type ${type.$extend}`);
} }
let parsed = this.parseType(namespaceName, type, ["$extend"]); let parsed = this.parseType([namespaceName], type, ["$extend"]);
if (parsed.constructor !== targetType.constructor) { if (parsed.constructor !== targetType.constructor) {
throw new Error(`Internal error: Bad attempt to extend ${type.$extend}`); throw new Error(`Internal error: Bad attempt to extend ${type.$extend}`);
} }
@ -1178,24 +1242,23 @@ this.Schemas = {
}, },
loadProperty(namespaceName, name, prop) { loadProperty(namespaceName, name, prop) {
if ("value" in prop) { if ("$ref" in prop) {
if (!prop.unsupported) {
this.register(namespaceName, name, new SubModuleProperty(name, namespaceName, prop["$ref"],
prop.properties || {}));
}
} else if ("value" in prop) {
this.register(namespaceName, name, new ValueProperty(prop, name, prop.value)); this.register(namespaceName, name, new ValueProperty(prop, name, prop.value));
} else { } else {
// We ignore the "optional" attribute on properties since we // We ignore the "optional" attribute on properties since we
// don't inject anything here anyway. // don't inject anything here anyway.
let type = this.parseType(namespaceName, prop, ["optional", "writable"]); let type = this.parseType([namespaceName], prop, ["optional", "writable"]);
this.register(namespaceName, name, new TypeProperty(prop, namespaceName, name, type, prop.writable || false)); this.register(namespaceName, name, new TypeProperty(prop, namespaceName, name, type, prop.writable || false));
} }
}, },
loadFunction(namespaceName, fun) { loadFunction(namespaceName, fun) {
let f = new FunctionEntry(fun, namespaceName, fun.name, let f = this.parseFunction([namespaceName], fun);
this.parseType(namespaceName, fun,
["name", "unsupported", "returns",
"allowAmbiguousOptionalArguments"]),
fun.unsupported || false,
fun.allowAmbiguousOptionalArguments || false,
fun.returns || null);
this.register(namespaceName, fun.name, f); this.register(namespaceName, fun.name, f);
}, },
@ -1203,7 +1266,7 @@ this.Schemas = {
let extras = event.extraParameters || []; let extras = event.extraParameters || [];
extras = extras.map(param => { extras = extras.map(param => {
return { return {
type: this.parseType(namespaceName, param, ["name", "optional"]), type: this.parseType([namespaceName], param, ["name", "optional"]),
name: param.name, name: param.name,
optional: param.optional || false, optional: param.optional || false,
}; };
@ -1215,11 +1278,11 @@ this.Schemas = {
let filters = event.filters; let filters = event.filters;
/* eslint-enable no-unused-vars */ /* eslint-enable no-unused-vars */
let type = this.parseType(namespaceName, event, let type = this.parseType([namespaceName], event,
["name", "unsupported", ["name", "unsupported",
"extraParameters", "returns", "filters"]); "extraParameters", "returns", "filters"]);
let e = new Event(event, namespaceName, event.name, type, extras, let e = new Event(event, [namespaceName], event.name, type, extras,
event.unsupported || false); event.unsupported || false);
this.register(namespaceName, event.name, e); this.register(namespaceName, event.name, e);
}, },
@ -1256,7 +1319,7 @@ this.Schemas = {
for (let [namespace, ns] of this.namespaces) { for (let [namespace, ns] of this.namespaces) {
let obj = Cu.createObjectIn(dest, {defineAs: namespace}); let obj = Cu.createObjectIn(dest, {defineAs: namespace});
for (let [name, entry] of ns) { for (let [name, entry] of ns) {
entry.inject(name, obj, new Context(wrapperFuncs)); entry.inject([namespace], name, obj, new Context(wrapperFuncs));
} }
if (!Object.keys(obj).length) { if (!Object.keys(obj).length) {

View File

@ -10,7 +10,7 @@ var {
EventManager, EventManager,
} = ExtensionUtils; } = ExtensionUtils;
extensions.registerPrivilegedAPI("storage", (extension, context) => { extensions.registerSchemaAPI("storage", "storage", (extension, context) => {
return { return {
storage: { storage: {
local: { local: {

View File

@ -12,6 +12,7 @@ toolkit.jar:
content/extensions/schemas/idle.json content/extensions/schemas/idle.json
content/extensions/schemas/manifest.json content/extensions/schemas/manifest.json
content/extensions/schemas/runtime.json content/extensions/schemas/runtime.json
content/extensions/schemas/storage.json
content/extensions/schemas/test.json content/extensions/schemas/test.json
content/extensions/schemas/web_navigation.json content/extensions/schemas/web_navigation.json
content/extensions/schemas/web_request.json content/extensions/schemas/web_request.json

View File

@ -0,0 +1,222 @@
// Copyright 2014 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": "storage",
"description": "Use the <code>browser.storage</code> API to store, retrieve, and track changes to user data.",
"types": [
{
"id": "StorageChange",
"type": "object",
"properties": {
"oldValue": {
"type": "any",
"description": "The old value of the item, if there was an old value.",
"optional": true
},
"newValue": {
"type": "any",
"description": "The new value of the item, if there is a new value.",
"optional": true
}
}
},
{
"id": "StorageArea",
"type": "object",
"functions": [
{
"name": "get",
"type": "function",
"description": "Gets one or more items from storage.",
"parameters": [
{
"name": "keys",
"choices": [
{ "type": "string" },
{ "type": "array", "items": { "type": "string" } },
{
"type": "object",
"description": "Storage items to return in the callback, where the values are replaced with those from storage if they exist.",
"additionalProperties": { "type": "any" }
}
],
"description": "A single key to get, list of keys to get, or a dictionary specifying default values (see description of the object). An empty list or object will return an empty result object. Pass in <code>null</code> to get the entire contents of storage.",
"optional": true
},
{
"name": "callback",
"type": "function",
"description": "Callback with storage items, or on failure (in which case $(ref:runtime.lastError) will be set).",
"parameters": [
{
"name": "items",
"type": "object",
"additionalProperties": { "type": "any" },
"description": "Object with items in their key-value mappings."
}
]
}
]
},
{
"name": "getBytesInUse",
"unsupported": true,
"type": "function",
"description": "Gets the amount of space (in bytes) being used by one or more items.",
"parameters": [
{
"name": "keys",
"choices": [
{ "type": "string" },
{ "type": "array", "items": { "type": "string" } }
],
"description": "A single key or list of keys to get the total usage for. An empty list will return 0. Pass in <code>null</code> to get the total usage of all of storage.",
"optional": true
},
{
"name": "callback",
"type": "function",
"description": "Callback with the amount of space being used by storage, or on failure (in which case $(ref:runtime.lastError) will be set).",
"parameters": [
{
"name": "bytesInUse",
"type": "integer",
"description": "Amount of space being used in storage, in bytes."
}
]
}
]
},
{
"name": "set",
"type": "function",
"description": "Sets multiple items.",
"parameters": [
{
"name": "items",
"type": "object",
"additionalProperties": { "type": "any" },
"description": "<p>An object which gives each key/value pair to update storage with. Any other key/value pairs in storage will not be affected.</p><p>Primitive values such as numbers will serialize as expected. Values with a <code>typeof</code> <code>\"object\"</code> and <code>\"function\"</code> will typically serialize to <code>{}</code>, with the exception of <code>Array</code> (serializes as expected), <code>Date</code>, and <code>Regex</code> (serialize using their <code>String</code> representation).</p>"
},
{
"name": "callback",
"type": "function",
"description": "Callback on success, or on failure (in which case $(ref:runtime.lastError) will be set).",
"parameters": [],
"optional": true
}
]
},
{
"name": "remove",
"type": "function",
"description": "Removes one or more items from storage.",
"parameters": [
{
"name": "keys",
"choices": [
{"type": "string"},
{"type": "array", "items": {"type": "string"}}
],
"description": "A single key or a list of keys for items to remove."
},
{
"name": "callback",
"type": "function",
"description": "Callback on success, or on failure (in which case $(ref:runtime.lastError) will be set).",
"parameters": [],
"optional": true
}
]
},
{
"name": "clear",
"type": "function",
"description": "Removes all items from storage.",
"parameters": [
{
"name": "callback",
"type": "function",
"description": "Callback on success, or on failure (in which case $(ref:runtime.lastError) will be set).",
"parameters": [],
"optional": true
}
]
}
]
}
],
"events": [
{
"name": "onChanged",
"type": "function",
"description": "Fired when one or more items change.",
"parameters": [
{
"name": "changes",
"type": "object",
"additionalProperties": { "$ref": "StorageChange" },
"description": "Object mapping each key that changed to its corresponding $(ref:storage.StorageChange) for that item."
},
{
"name": "areaName",
"type": "string",
"description": "The name of the storage area (<code>\"sync\"</code>, <code>\"local\"</code> or <code>\"managed\"</code>) the changes are for."
}
]
}
],
"properties": {
"sync": {
"unsupported": true,
"$ref": "StorageArea",
"description": "Items in the <code>sync</code> storage area are synced by the browser.",
"properties": {
"QUOTA_BYTES": {
"value": 102400,
"description": "The maximum total amount (in bytes) of data that can be stored in sync storage, as measured by the JSON stringification of every value plus every key's length. Updates that would cause this limit to be exceeded fail immediately and set $(ref:runtime.lastError)."
},
"QUOTA_BYTES_PER_ITEM": {
"value": 8192,
"description": "The maximum size (in bytes) of each individual item in sync storage, as measured by the JSON stringification of its value plus its key length. Updates containing items larger than this limit will fail immediately and set $(ref:runtime.lastError)."
},
"MAX_ITEMS": {
"value": 512,
"description": "The maximum number of items that can be stored in sync storage. Updates that would cause this limit to be exceeded will fail immediately and set $(ref:runtime.lastError)."
},
"MAX_WRITE_OPERATIONS_PER_HOUR": {
"value": 1800,
"description": "<p>The maximum number of <code>set</code>, <code>remove</code>, or <code>clear</code> operations that can be performed each hour. This is 1 every 2 seconds, a lower ceiling than the short term higher writes-per-minute limit.</p><p>Updates that would cause this limit to be exceeded fail immediately and set $(ref:runtime.lastError).</p>"
},
"MAX_WRITE_OPERATIONS_PER_MINUTE": {
"value": 120,
"description": "<p>The maximum number of <code>set</code>, <code>remove</code>, or <code>clear</code> operations that can be performed each minute. This is 2 per second, providing higher throughput than writes-per-hour over a shorter period of time.</p><p>Updates that would cause this limit to be exceeded fail immediately and set $(ref:runtime.lastError).</p>"
},
"MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE": {
"value": 1000000,
"deprecated": "The storage.sync API no longer has a sustained write operation quota.",
"description": ""
}
}
},
"local": {
"$ref": "StorageArea",
"description": "Items in the <code>local</code> storage area are local to each machine.",
"properties": {
"QUOTA_BYTES": {
"value": 5242880,
"description": "The maximum amount (in bytes) of data that can be stored in local storage, as measured by the JSON stringification of every value plus every key's length. This value will be ignored if the extension has the <code>unlimitedStorage</code> permission. Updates that would cause this limit to be exceeded fail immediately and set $(ref:runtime.lastError)."
}
}
},
"managed": {
"unsupported": true,
"$ref": "StorageArea",
"description": "Items in the <code>managed</code> storage area are set by the domain administrator, and are read-only for the extension; trying to modify this namespace results in an error."
}
}
}
]

View File

@ -9,6 +9,13 @@ let json = [
properties: { properties: {
PROP1: {value: 20}, PROP1: {value: 20},
prop2: {type: "string"}, prop2: {type: "string"},
prop3: {
$ref: "submodule",
},
prop4: {
$ref: "submodule",
unsupported: true,
},
}, },
types: [ types: [
@ -55,6 +62,18 @@ let json = [
{type: "string"}, {type: "string"},
], ],
}, },
{
id: "submodule",
type: "object",
functions: [
{
name: "sub_foo",
type: "function",
parameters: []
},
],
},
], ],
functions: [ functions: [
@ -298,25 +317,31 @@ let wrapper = {
talliedErrors.push(message); talliedErrors.push(message);
}, },
callFunction(ns, name, args) { callFunction(path, name, args) {
let ns = path.join(".");
tally("call", ns, name, args); tally("call", ns, name, args);
}, },
getProperty(ns, name) { getProperty(path, name) {
let ns = path.join(".");
tally("get", ns, name); tally("get", ns, name);
}, },
setProperty(ns, name, value) { setProperty(path, name, value) {
let ns = path.join(".");
tally("set", ns, name, value); tally("set", ns, name, value);
}, },
addListener(ns, name, listener, args) { addListener(path, name, listener, args) {
let ns = path.join(".");
tally("addListener", ns, name, [listener, args]); tally("addListener", ns, name, [listener, args]);
}, },
removeListener(ns, name, listener) { removeListener(path, name, listener) {
let ns = path.join(".");
tally("removeListener", ns, name, [listener]); tally("removeListener", ns, name, [listener]);
}, },
hasListener(ns, name, listener) { hasListener(path, name, listener) {
let ns = path.join(".");
tally("hasListener", ns, name, [listener]); tally("hasListener", ns, name, [listener]);
}, },
}; };
@ -588,6 +613,14 @@ add_task(function* () {
Assert.throws(() => root.testing.extended2(true), Assert.throws(() => root.testing.extended2(true),
/Incorrect argument types/, /Incorrect argument types/,
"should throw for wrong argument type"); "should throw for wrong argument type");
root.testing.prop3.sub_foo();
verify("call", "testing.prop3", "sub_foo", []);
tallied = null;
Assert.throws(() => root.testing.prop4.sub_foo(),
/root.testing.prop4 is undefined/,
"should throw for unsupported submodule");
}); });
let deprecatedJson = [ let deprecatedJson = [