| Index: ui/webui/resources/js/cr.js
|
| diff --git a/ui/webui/resources/js/cr.js b/ui/webui/resources/js/cr.js
|
| index a38c59f84732e820c00407287b3ef00c37b41bb2..eefaa6b938008133d7778e3b0667a4d09861fe97 100644
|
| --- a/ui/webui/resources/js/cr.js
|
| +++ b/ui/webui/resources/js/cr.js
|
| @@ -313,48 +313,52 @@ var cr = function() {
|
| }
|
|
|
| /**
|
| - * The mapping used by the sendWithCallback mechanism to tie the callback
|
| - * supplied to an invocation of sendWithCallback with the WebUI response
|
| - * sent by the browser in response to the chrome.send call. The mapping is
|
| - * from ID to callback function; the ID is generated by sendWithCallback and
|
| - * is unique across all invocations of said method.
|
| - * @type {!Object<Function>}
|
| + * The mapping used by the sendWithPromise mechanism to tie the Promise
|
| + * returned to callers with the corresponding WebUI response. The mapping is
|
| + * from ID to the Promise resolver function; the ID is generated by
|
| + * sendWithPromise and is unique across all invocations of said method.
|
| + * @type {!Object<!Function>}
|
| */
|
| - var chromeSendCallbackMap = Object.create(null);
|
| + var chromeSendResolverMap = {};
|
|
|
| /**
|
| * The named method the WebUI handler calls directly in response to a
|
| - * chrome.send call that expects a callback. The handler requires no knowledge
|
| + * chrome.send call that expects a response. The handler requires no knowledge
|
| * of the specific name of this method, as the name is passed to the handler
|
| * as the first argument in the arguments list of chrome.send. The handler
|
| * must pass the ID, also sent via the chrome.send arguments list, as the
|
| * first argument of the JS invocation; additionally, the handler may
|
| - * supply any number of other arguments that will be forwarded to the
|
| - * callback.
|
| - * @param {string} id The unique ID identifying the callback method this
|
| - * response is tied to.
|
| + * supply any number of other arguments that will be included in the response.
|
| + * @param {string} id The unique ID identifying the Promise this response is
|
| + * tied to.
|
| + * @param {...*} var_args Variable number of arguments to be included in the
|
| + * response.
|
| */
|
| - function webUIResponse(id) {
|
| - chromeSendCallbackMap[id].apply(
|
| - null, Array.prototype.slice.call(arguments, 1));
|
| - delete chromeSendCallbackMap[id];
|
| + function webUIResponse(id, var_args) {
|
| + var resolverFn = chromeSendResolverMap[id];
|
| + delete chromeSendResolverMap[id];
|
| + // If no response arguments were passed from C++, resolve the Promise
|
| + // without any arguments, else wrap all response arguments in an array
|
| + // since Promise#resolve accepts only one value.
|
| + var responseArgs = Array.prototype.slice.call(arguments, 1);
|
| + resolverFn(responseArgs.length == 0 ? undefined : responseArgs);
|
| }
|
|
|
| /**
|
| - * A variation of chrome.send which allows the client to receive a direct
|
| - * callback without requiring the handler to have specific knowledge of any
|
| - * JS internal method names or state. The callback will be removed from the
|
| - * mapping once it has fired.
|
| + * A variation of chrome.send, suitable for messages that expect a single
|
| + * response from C++.
|
| * @param {string} methodName The name of the WebUI handler API.
|
| - * @param {Array|undefined} args Arguments for the method call sent to the
|
| - * WebUI handler. Pass undefined if no args should be sent to the handler.
|
| - * @param {Function} callback A callback function which is called (indirectly)
|
| - * by the WebUI handler.
|
| + * @param {...*} var_args Varibale number of arguments to be forwarded to the
|
| + * C++ call.
|
| + * @return {!Promise}
|
| */
|
| - function sendWithCallback(methodName, args, callback) {
|
| - var id = methodName + createUid();
|
| - chromeSendCallbackMap[id] = callback;
|
| - chrome.send(methodName, ['cr.webUIResponse', id].concat(args || []));
|
| + function sendWithPromise(methodName, var_args) {
|
| + var args = Array.prototype.slice.call(arguments, 1);
|
| + return new Promise(function(resolve, reject) {
|
| + var id = methodName + '_' + createUid();
|
| + chromeSendResolverMap[id] = resolve;
|
| + chrome.send(methodName, ['cr.webUIResponse', id].concat(args));
|
| + });
|
| }
|
|
|
| /**
|
| @@ -403,7 +407,7 @@ var cr = function() {
|
| getUid: getUid,
|
| makePublic: makePublic,
|
| webUIResponse: webUIResponse,
|
| - sendWithCallback: sendWithCallback,
|
| + sendWithPromise: sendWithPromise,
|
| webUIListenerCallback: webUIListenerCallback,
|
| addWebUIListener: addWebUIListener,
|
| PropertyKind: PropertyKind,
|
|
|