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

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: 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') | no next file » | 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..afcde50cd24999643b7f1648e53f0cbea1d4cae3 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,45 @@ function deepCopy(value) {
return nativeDeepCopy(value);
}
+/**
+ * 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) {
+ 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;
+}
+
+/**
+ * Asynchronously require a module from the background page, loading the
+ * background page if necessary.
+ */
+function requireAsyncFromBackgroundPage(moduleName) {
+ return new Promise(function(resolve, reject) {
+ 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
+ }).then(function(backgroundPage) {
+ if (!backgroundPage)
+ backgroundPage = window;
+ return context.GetModuleSystem(backgroundPage).requireAsync(moduleName);
+ }).catch(function(e) {
+ return requireAsync(moduleName);
+ });
+}
+
exports.forEach = forEach;
exports.loadTypeSchema = loadTypeSchema;
exports.lookup = lookup;
exports.expose = expose;
exports.deepCopy = deepCopy;
+exports.createAsyncProxy = createAsyncProxy;
+exports.requireAsyncFromBackgroundPage = requireAsyncFromBackgroundPage;
« no previous file with comments | « extensions/renderer/resources/serial_custom_bindings.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698