1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var createClassWrapper = requireNative('utils').createClassWrapper;
6 var nativeDeepCopy = requireNative('utils').deepCopy;
7 var schemaRegistry = requireNative('schema_registry');
8 var CHECK = requireNative('logging').CHECK;
9 var DCHECK = requireNative('logging').DCHECK;
10 var WARNING = requireNative('logging').WARNING;
13 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using
14 * |self| as the target.
15 * @param {Object} obj The object to iterate over.
16 * @param {function} f The function to call in each iteration.
17 * @param {Object} self The object to use as |this| in each function call.
19 function forEach(obj, f, self) {
20 for (var key in obj) {
21 if ($Object.hasOwnProperty(obj, key))
22 $Function.call(f, self, key, obj[key]);
27 * Assuming |array_of_dictionaries| is structured like this:
28 * [{id: 1, ... }, {id: 2, ...}, ...], you can use
29 * lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2.
30 * @param {Array<Object<string, ?>>} array_of_dictionaries
31 * @param {string} field
34 function lookup(array_of_dictionaries, field, value) {
35 var filter = function (dict) {return dict[field] == value;};
36 var matches = array_of_dictionaries.filter(filter);
37 if (matches.length == 0) {
39 } else if (matches.length == 1) {
42 throw new Error("Failed lookup of field '" + field + "' with value '" +
47 function loadTypeSchema(typeName, defaultSchema) {
48 var parts = $String.split(typeName, '.');
49 if (parts.length == 1) {
50 if (defaultSchema == null) {
51 WARNING('Trying to reference "' + typeName + '" ' +
52 'with neither namespace nor default schema.');
55 var types = defaultSchema.types;
57 var schemaName = $Array.join($Array.slice(parts, 0, parts.length - 1), '.');
58 var types = schemaRegistry.GetSchema(schemaName).types;
60 for (var i = 0; i < types.length; ++i) {
61 if (types[i].id == typeName)
68 * Takes a private class implementation |cls| and exposes a subset of its
69 * methods |functions| and properties |properties| and |readonly| in a public
70 * wrapper class that it returns. Within bindings code, you can access the
71 * implementation from an instance of the wrapper class using
72 * privates(instance).impl, and from the implementation class you can access
73 * the wrapper using this.wrapper (or implInstance.wrapper if you have another
74 * instance of the implementation class).
75 * @param {string} name The name of the exposed wrapper class.
76 * @param {Object} cls The class implementation.
77 * @param {{superclass: ?Function,
78 * functions: ?Array<string>,
79 * properties: ?Array<string>,
80 * readonly: ?Array<string>}} exposed The names of properties on the
81 * implementation class to be exposed. |superclass| represents the
82 * constructor of the class to be used as the superclass of the exposed
83 * class; |functions| represents the names of functions which should be
84 * delegated to the implementation; |properties| are gettable/settable
85 * properties and |readonly| are read-only properties.
87 function expose(name, cls, exposed) {
88 var publicClass = createClassWrapper(name, cls, exposed.superclass);
90 if ('functions' in exposed) {
91 $Array.forEach(exposed.functions, function(func) {
92 publicClass.prototype[func] = function() {
93 var impl = privates(this).impl;
94 return $Function.apply(impl[func], impl, arguments);
99 if ('properties' in exposed) {
100 $Array.forEach(exposed.properties, function(prop) {
101 $Object.defineProperty(publicClass.prototype, prop, {
104 return privates(this).impl[prop];
106 set: function(value) {
107 var impl = privates(this).impl;
115 if ('readonly' in exposed) {
116 $Array.forEach(exposed.readonly, function(readonly) {
117 $Object.defineProperty(publicClass.prototype, readonly, {
120 return privates(this).impl[readonly];
130 * Returns a deep copy of |value|. The copy will have no references to nested
133 function deepCopy(value) {
134 return nativeDeepCopy(value);
138 * Wrap an asynchronous API call to a function |func| in a promise. The
139 * remaining arguments will be passed to |func|. Returns a promise that will be
140 * resolved to the result passed to the callback or rejected if an error occurs
141 * (if chrome.runtime.lastError is set). If there are multiple results, the
142 * promise will be resolved with an array containing those results.
145 * promise(chrome.storage.get, 'a').then(function(result) {
147 * }).catch(function(error) {
148 * // Report error.message.
151 function promise(func) {
152 var args = $Array.slice(arguments, 1);
153 DCHECK(typeof func == 'function');
154 return new Promise(function(resolve, reject) {
155 args.push(function() {
156 if (chrome.runtime.lastError) {
157 reject(new Error(chrome.runtime.lastError));
160 if (arguments.length <= 1)
161 resolve(arguments[0]);
163 resolve($Array.slice(arguments));
165 $Function.apply(func, null, args);
169 exports.forEach = forEach;
170 exports.loadTypeSchema = loadTypeSchema;
171 exports.lookup = lookup;
172 exports.expose = expose;
173 exports.deepCopy = deepCopy;
174 exports.promise = promise;