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.
4
5var createClassWrapper = requireNative('utils').createClassWrapper;
6var nativeDeepCopy = requireNative('utils').deepCopy;
7var schemaRegistry = requireNative('schema_registry');
8var CHECK = requireNative('logging').CHECK;
9var DCHECK = requireNative('logging').DCHECK;
10var WARNING = requireNative('logging').WARNING;
11
12/**
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.
18 */
19function forEach(obj, f, self) {
20  for (var key in obj) {
21    if ($Object.hasOwnProperty(obj, key))
22      $Function.call(f, self, key, obj[key]);
23  }
24}
25
26/**
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
32 * @param {?} value
33 */
34function 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) {
38    return undefined;
39  } else if (matches.length == 1) {
40    return matches[0]
41  } else {
42    throw new Error("Failed lookup of field '" + field + "' with value '" +
43                    value + "'");
44  }
45}
46
47function 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.');
53      return null;
54    }
55    var types = defaultSchema.types;
56  } else {
57    var schemaName = $Array.join($Array.slice(parts, 0, parts.length - 1), '.');
58    var types = schemaRegistry.GetSchema(schemaName).types;
59  }
60  for (var i = 0; i < types.length; ++i) {
61    if (types[i].id == typeName)
62      return types[i];
63  }
64  return null;
65}
66
67/**
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.
86 */
87function expose(name, cls, exposed) {
88  var publicClass = createClassWrapper(name, cls, exposed.superclass);
89
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);
95      };
96    });
97  }
98
99  if ('properties' in exposed) {
100    $Array.forEach(exposed.properties, function(prop) {
101      $Object.defineProperty(publicClass.prototype, prop, {
102        enumerable: true,
103        get: function() {
104          return privates(this).impl[prop];
105        },
106        set: function(value) {
107          var impl = privates(this).impl;
108          delete impl[prop];
109          impl[prop] = value;
110        }
111      });
112    });
113  }
114
115  if ('readonly' in exposed) {
116    $Array.forEach(exposed.readonly, function(readonly) {
117      $Object.defineProperty(publicClass.prototype, readonly, {
118        enumerable: true,
119        get: function() {
120          return privates(this).impl[readonly];
121        },
122      });
123    });
124  }
125
126  return publicClass;
127}
128
129/**
130 * Returns a deep copy of |value|. The copy will have no references to nested
131 * values of |value|.
132 */
133function deepCopy(value) {
134  return nativeDeepCopy(value);
135}
136
137/**
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.
143 *
144 * For example,
145 * promise(chrome.storage.get, 'a').then(function(result) {
146 *   // Use result.
147 * }).catch(function(error) {
148 *   // Report error.message.
149 * });
150 */
151function 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));
158        return;
159      }
160      if (arguments.length <= 1)
161        resolve(arguments[0]);
162      else
163        resolve($Array.slice(arguments));
164    });
165    $Function.apply(func, null, args);
166  });
167}
168
169exports.forEach = forEach;
170exports.loadTypeSchema = loadTypeSchema;
171exports.lookup = lookup;
172exports.expose = expose;
173exports.deepCopy = deepCopy;
174exports.promise = promise;
175