Chromium Code Reviews| 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..2e62e975d2a7bc02ae853eb04aa969c33b3098e5 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 {!Map<string, !Function>} |
|
Dan Beam
2016/01/23 02:48:51
pretty sure this breaks safari, which is why jhawk
dpapad
2016/01/25 18:34:22
Reverted.
I guess by "Safari" you mean Webkit-bas
|
| */ |
| - var chromeSendCallbackMap = Object.create(null); |
| + var chromeSendResolverMap = new Map(); |
| /** |
| * 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.get(id); |
| + // If no response arguments where passed from C++, resolving the Promise |
| + // without any arguments, else wraping 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); |
| + chromeSendResolverMap.delete(id); |
| } |
| /** |
| - * 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, opt_args) { |
| + var args = Array.prototype.slice.call(arguments, 1); |
| + return new Promise(function(resolve, reject) { |
| + var id = methodName + createUid(); |
| + chromeSendResolverMap.set(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, |