Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2162)

Unified Diff: extensions/renderer/resources/utils.js

Issue 481853002: Add support for asynchronously loading modules from the background page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/renderer/resources/serial_custom_bindings.js ('k') | extensions/renderer/utils_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/resources/utils.js
diff --git a/extensions/renderer/resources/utils.js b/extensions/renderer/resources/utils.js
index 803a3518d753b271bf740dd982d51d8af86c4720..edf7703b3d1f9383c1a9b39034ed273fc704a915 100644
--- a/extensions/renderer/resources/utils.js
+++ b/extensions/renderer/resources/utils.js
@@ -7,6 +7,7 @@ var nativeDeepCopy = requireNative('utils').deepCopy;
var schemaRegistry = requireNative('schema_registry');
var CHECK = requireNative('logging').CHECK;
var WARNING = requireNative('logging').WARNING;
+var context = requireNative('v8_context');
/**
* An object forEach. Calls |f| with each (key, value) pair of |obj|, using
@@ -133,8 +134,62 @@ function deepCopy(value) {
return nativeDeepCopy(value);
}
+/**
+ * Wrap an asynchronous API call in a promise.
not at google - send to devlin 2014/08/19 19:23:38 Explain how the call happens. I presume that |arg|
Sam McNally 2014/08/20 01:27:18 Done.
+ */
+function promise(func) {
+ var args = $Array.slice(arguments, 1);
+ return new Promise(function(resolve, reject) {
+ args.push(function() {
+ if (chrome.runtime.lastError) {
+ reject(new Error(chrome.runtime.lastError));
+ return;
+ }
+ if (arguments.length <= 1)
+ resolve(arguments[0]);
+ else
+ resolve($Array.slice(arguments));
not at google - send to devlin 2014/08/19 19:23:38 This doesn't seem right; so if there are multiple
Sam McNally 2014/08/20 01:27:18 Promises only support one argument.
not at google - send to devlin 2014/08/20 01:47:22 Yikes. THE MORE YOU KNOW.
+ });
+ $Function.apply(func, null, args);
+ });
+}
+
+/**
+ * Asynchronously require a module from the background page, loading the
+ * background page if necessary.
not at google - send to devlin 2014/08/19 19:23:38 You might actually want to explain why you need th
Sam McNally 2014/08/20 01:27:18 Done and moved it into serial_custom_bindings.
+ */
+function requireAsyncFromBackgroundPage(moduleName) {
+ return promise(chrome.runtime.getBackgroundPage).then(function(bgPage) {
+ return context.GetModuleSystem(bgPage).requireAsync(moduleName);
+ }).catch(function() {
+ return requireAsync(moduleName);
+ });
+}
+
+/**
+ * Create an async proxy for the resolved value of |targetPromise|. The returned
+ * object will contain a function for each name in |functionNames|. Calling this
+ * function will result in a call to the corresponding function on the resolved
+ * value of |targetPromise| and returns a promise to the result of that call.
+ */
+function createAsyncProxy(targetPromise, functionNames) {
not at google - send to devlin 2014/08/19 19:23:38 I'm having a hard time grasping what this method d
Sam McNally 2014/08/20 01:27:18 Fair enough. Removed it and inlined the uses.
+ var functionProxies = {};
+ $Array.forEach(functionNames, function(name) {
+ functionProxies[name] = function() {
+ var args = arguments;
+ return targetPromise.then(function(target) {
+ return $Function.apply(target[name], target, args);
+ });
+ };
+ });
+ return functionProxies;
+}
+
exports.forEach = forEach;
exports.loadTypeSchema = loadTypeSchema;
exports.lookup = lookup;
exports.expose = expose;
exports.deepCopy = deepCopy;
+exports.promise = promise;
+exports.requireAsyncFromBackgroundPage = requireAsyncFromBackgroundPage;
+exports.createAsyncProxy = createAsyncProxy;
« no previous file with comments | « extensions/renderer/resources/serial_custom_bindings.js ('k') | extensions/renderer/utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698