NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / utils.js
blob4ffb6d7bd56a7a08857a50470f781c497e508286
1 // Copyright (c) 2012 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 schemaRegistry = requireNative('schema_registry');
6 var CHECK = requireNative('logging').CHECK;
7 var WARNING = requireNative('logging').WARNING;
9 // An object forEach. Calls |f| with each (key, value) pair of |obj|, using
10 // |self| as the target.
11 function forEach(obj, f, self) {
12 for (var key in obj) {
13 if ($Object.hasOwnProperty(obj, key))
14 $Function.call(f, self, key, obj[key]);
18 // Assuming |array_of_dictionaries| is structured like this:
19 // [{id: 1, ... }, {id: 2, ...}, ...], you can use
20 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2.
21 function lookup(array_of_dictionaries, field, value) {
22 var filter = function (dict) {return dict[field] == value;};
23 var matches = array_of_dictionaries.filter(filter);
24 if (matches.length == 0) {
25 return undefined;
26 } else if (matches.length == 1) {
27 return matches[0]
28 } else {
29 throw new Error("Failed lookup of field '" + field + "' with value '" +
30 value + "'");
34 function loadTypeSchema(typeName, defaultSchema) {
35 var parts = $String.split(typeName, '.');
36 if (parts.length == 1) {
37 if (defaultSchema == null) {
38 WARNING('Trying to reference "' + typeName + '" ' +
39 'with neither namespace nor default schema.');
40 return null;
42 var types = defaultSchema.types;
43 } else {
44 var schemaName = $Array.join($Array.slice(parts, 0, parts.length - 1), '.');
45 var types = schemaRegistry.GetSchema(schemaName).types;
47 for (var i = 0; i < types.length; ++i) {
48 if (types[i].id == typeName)
49 return types[i];
51 return null;
54 // expose takes a private class implementation |cls| and exposes a subset of its
55 // methods |funcs| and properties |props| in a public wrapper class that it
56 // returns.
57 function expose(cls, funcs, props) {
58 function publicClass() {
59 var privateObj = $Object.create(cls.prototype);
60 $Function.apply(cls, privateObj, arguments);
61 privateObj.wrapper = this;
62 privates(this).impl = privateObj;
65 if (funcs) {
66 $Array.forEach(funcs, function(func) {
67 publicClass.prototype[func] = function() {
68 var impl = privates(this).impl;
69 return $Function.apply(impl[func], impl, arguments);
71 });
74 if (props) {
75 $Array.forEach(props, function(prop) {
76 $Object.defineProperty(publicClass.prototype, prop, {
77 enumerable: true,
78 get: function() {
79 return privates(this).impl[prop];
81 set: function(value) {
82 var impl = privates(this).impl;
83 delete impl[prop];
84 impl[prop] = value;
86 });
87 });
90 return publicClass;
93 exports.forEach = forEach;
94 exports.loadTypeSchema = loadTypeSchema;
95 exports.lookup = lookup;
96 exports.expose = expose;