Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var createClassWrapper = requireNative('utils').createClassWrapper; | 5 var createClassWrapper = requireNative('utils').createClassWrapper; |
| 6 var nativeDeepCopy = requireNative('utils').deepCopy; | 6 var nativeDeepCopy = requireNative('utils').deepCopy; |
| 7 var schemaRegistry = requireNative('schema_registry'); | 7 var schemaRegistry = requireNative('schema_registry'); |
| 8 var CHECK = requireNative('logging').CHECK; | 8 var CHECK = requireNative('logging').CHECK; |
| 9 var WARNING = requireNative('logging').WARNING; | 9 var WARNING = requireNative('logging').WARNING; |
| 10 var context = requireNative('v8_context'); | |
| 10 | 11 |
| 11 /** | 12 /** |
| 12 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using | 13 * An object forEach. Calls |f| with each (key, value) pair of |obj|, using |
| 13 * |self| as the target. | 14 * |self| as the target. |
| 14 * @param {Object} obj The object to iterate over. | 15 * @param {Object} obj The object to iterate over. |
| 15 * @param {function} f The function to call in each iteration. | 16 * @param {function} f The function to call in each iteration. |
| 16 * @param {Object} self The object to use as |this| in each function call. | 17 * @param {Object} self The object to use as |this| in each function call. |
| 17 */ | 18 */ |
| 18 function forEach(obj, f, self) { | 19 function forEach(obj, f, self) { |
| 19 for (var key in obj) { | 20 for (var key in obj) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 } | 127 } |
| 127 | 128 |
| 128 /** | 129 /** |
| 129 * Returns a deep copy of |value|. The copy will have no references to nested | 130 * Returns a deep copy of |value|. The copy will have no references to nested |
| 130 * values of |value|. | 131 * values of |value|. |
| 131 */ | 132 */ |
| 132 function deepCopy(value) { | 133 function deepCopy(value) { |
| 133 return nativeDeepCopy(value); | 134 return nativeDeepCopy(value); |
| 134 } | 135 } |
| 135 | 136 |
| 137 /** | |
| 138 * Create an async proxy for the resolved value of |targetPromise|. The returned | |
| 139 * object will contain a function for each name in |functionNames|. Calling this | |
| 140 * function will result in a call to the corresponding function on the resolved | |
| 141 * value of |targetPromise| and returns a promise to the result of that call. | |
| 142 */ | |
| 143 function createAsyncProxy(targetPromise, functionNames) { | |
| 144 var functionProxies = {}; | |
| 145 $Array.forEach(functionNames, function(name) { | |
| 146 functionProxies[name] = function() { | |
| 147 var args = arguments; | |
| 148 return targetPromise.then(function(target) { | |
| 149 return $Function.apply(target[name], target, args); | |
| 150 }); | |
| 151 }; | |
| 152 }); | |
| 153 return functionProxies; | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * Asynchronously require a module from the background page, loading the | |
| 158 * background page if necessary. | |
| 159 */ | |
| 160 function requireAsyncFromBackgroundPage(moduleName) { | |
| 161 return new Promise(function(resolve, reject) { | |
| 162 chrome.runtime.getBackgroundPage(resolve); | |
|
not at google - send to devlin
2014/08/18 21:22:11
Promise is neat, but strictly speaking you need to
Sam McNally
2014/08/19 01:44:56
Done. I'd prefer to keep requireAsyncFromBackgroun
| |
| 163 }).then(function(backgroundPage) { | |
| 164 if (!backgroundPage) | |
| 165 backgroundPage = window; | |
| 166 return context.GetModuleSystem(backgroundPage).requireAsync(moduleName); | |
| 167 }).catch(function(e) { | |
| 168 return requireAsync(moduleName); | |
| 169 }); | |
| 170 } | |
| 171 | |
| 136 exports.forEach = forEach; | 172 exports.forEach = forEach; |
| 137 exports.loadTypeSchema = loadTypeSchema; | 173 exports.loadTypeSchema = loadTypeSchema; |
| 138 exports.lookup = lookup; | 174 exports.lookup = lookup; |
| 139 exports.expose = expose; | 175 exports.expose = expose; |
| 140 exports.deepCopy = deepCopy; | 176 exports.deepCopy = deepCopy; |
| 177 exports.createAsyncProxy = createAsyncProxy; | |
| 178 exports.requireAsyncFromBackgroundPage = requireAsyncFromBackgroundPage; | |
| OLD | NEW |