Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
| 6 * The global object. | 6 * The global object. |
| 7 * @type {!Object} | 7 * @type {!Object} |
| 8 * @const | 8 * @const |
| 9 */ | 9 */ |
| 10 var global = this; | 10 var global = this; |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 methods.forEach(function(method) { | 306 methods.forEach(function(method) { |
| 307 ctor[method] = function() { | 307 ctor[method] = function() { |
| 308 var target = opt_target ? document.getElementById(opt_target) : | 308 var target = opt_target ? document.getElementById(opt_target) : |
| 309 ctor.getInstance(); | 309 ctor.getInstance(); |
| 310 return target[method + '_'].apply(target, arguments); | 310 return target[method + '_'].apply(target, arguments); |
| 311 }; | 311 }; |
| 312 }); | 312 }); |
| 313 } | 313 } |
| 314 | 314 |
| 315 /** | 315 /** |
| 316 * The mapping used by the sendWithCallback mechanism to tie the callback | 316 * The mapping used by the sendWithPromise mechanism to tie the Promise |
| 317 * supplied to an invocation of sendWithCallback with the WebUI response | 317 * returned to callers with the corresponding WebUI response. The mapping is |
| 318 * sent by the browser in response to the chrome.send call. The mapping is | 318 * from ID to the Promise resolver function; the ID is generated by |
| 319 * from ID to callback function; the ID is generated by sendWithCallback and | 319 * sendWithPromise and is unique across all invocations of said method. |
| 320 * is unique across all invocations of said method. | 320 * @type {!Object<!Function>} |
| 321 * @type {!Object<Function>} | |
| 322 */ | 321 */ |
| 323 var chromeSendCallbackMap = Object.create(null); | 322 var chromeSendResolverMap = {}; |
| 324 | 323 |
| 325 /** | 324 /** |
| 326 * The named method the WebUI handler calls directly in response to a | 325 * The named method the WebUI handler calls directly in response to a |
| 327 * chrome.send call that expects a callback. The handler requires no knowledge | 326 * chrome.send call that expects a response. The handler requires no knowledge |
| 328 * of the specific name of this method, as the name is passed to the handler | 327 * of the specific name of this method, as the name is passed to the handler |
| 329 * as the first argument in the arguments list of chrome.send. The handler | 328 * as the first argument in the arguments list of chrome.send. The handler |
| 330 * must pass the ID, also sent via the chrome.send arguments list, as the | 329 * must pass the ID, also sent via the chrome.send arguments list, as the |
| 331 * first argument of the JS invocation; additionally, the handler may | 330 * first argument of the JS invocation; additionally, the handler may |
| 332 * supply any number of other arguments that will be forwarded to the | 331 * supply any number of other arguments that will be included in the response. |
| 333 * callback. | 332 * @param {string} id The unique ID identifying the Promise this response is |
| 334 * @param {string} id The unique ID identifying the callback method this | 333 * tied to. |
| 335 * response is tied to. | 334 * @param {...*} var_args Variable number of arguments to be included in the |
| 335 * response. | |
| 336 */ | 336 */ |
| 337 function webUIResponse(id) { | 337 function webUIResponse(id, var_args) { |
| 338 chromeSendCallbackMap[id].apply( | 338 var resolverFn = chromeSendResolverMap[id]; |
| 339 null, Array.prototype.slice.call(arguments, 1)); | 339 delete chromeSendResolverMap[id]; |
| 340 delete chromeSendCallbackMap[id]; | 340 // Promise#resolve accepts only one value, therefore wrapping all arguments |
| 341 // passed from C++ to JS in an array. | |
| 342 resolverFn(Array.prototype.slice.call(arguments, 1)); | |
|
Dan Beam
2016/01/26 00:54:52
this seems saner and simpler, double win
| |
| 341 } | 343 } |
| 342 | 344 |
| 343 /** | 345 /** |
| 344 * A variation of chrome.send which allows the client to receive a direct | 346 * A variation of chrome.send, suitable for messages that expect a single |
| 345 * callback without requiring the handler to have specific knowledge of any | 347 * response from C++. |
| 346 * JS internal method names or state. The callback will be removed from the | |
| 347 * mapping once it has fired. | |
| 348 * @param {string} methodName The name of the WebUI handler API. | 348 * @param {string} methodName The name of the WebUI handler API. |
| 349 * @param {Array|undefined} args Arguments for the method call sent to the | 349 * @param {...*} var_args Varibale number of arguments to be forwarded to the |
| 350 * WebUI handler. Pass undefined if no args should be sent to the handler. | 350 * C++ call. |
| 351 * @param {Function} callback A callback function which is called (indirectly) | 351 * @return {!Promise} |
| 352 * by the WebUI handler. | |
| 353 */ | 352 */ |
| 354 function sendWithCallback(methodName, args, callback) { | 353 function sendWithPromise(methodName, var_args) { |
| 355 var id = methodName + createUid(); | 354 var args = Array.prototype.slice.call(arguments, 1); |
| 356 chromeSendCallbackMap[id] = callback; | 355 return new Promise(function(resolve, reject) { |
| 357 chrome.send(methodName, ['cr.webUIResponse', id].concat(args || [])); | 356 var id = methodName + '_' + createUid(); |
| 357 chromeSendResolverMap[id] = resolve; | |
| 358 chrome.send(methodName, ['cr.webUIResponse', id].concat(args)); | |
| 359 }); | |
| 358 } | 360 } |
| 359 | 361 |
| 360 /** | 362 /** |
| 361 * A registry of callbacks keyed by event name. Used by addWebUIListener to | 363 * A registry of callbacks keyed by event name. Used by addWebUIListener to |
| 362 * register listeners. | 364 * register listeners. |
| 363 * @type {!Object<Array<Function>>} | 365 * @type {!Object<Array<Function>>} |
| 364 */ | 366 */ |
| 365 var webUIListenerMap = Object.create(null); | 367 var webUIListenerMap = Object.create(null); |
| 366 | 368 |
| 367 /** | 369 /** |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 396 addSingletonGetter: addSingletonGetter, | 398 addSingletonGetter: addSingletonGetter, |
| 397 createUid: createUid, | 399 createUid: createUid, |
| 398 define: define, | 400 define: define, |
| 399 defineProperty: defineProperty, | 401 defineProperty: defineProperty, |
| 400 dispatchPropertyChange: dispatchPropertyChange, | 402 dispatchPropertyChange: dispatchPropertyChange, |
| 401 dispatchSimpleEvent: dispatchSimpleEvent, | 403 dispatchSimpleEvent: dispatchSimpleEvent, |
| 402 exportPath: exportPath, | 404 exportPath: exportPath, |
| 403 getUid: getUid, | 405 getUid: getUid, |
| 404 makePublic: makePublic, | 406 makePublic: makePublic, |
| 405 webUIResponse: webUIResponse, | 407 webUIResponse: webUIResponse, |
| 406 sendWithCallback: sendWithCallback, | 408 sendWithPromise: sendWithPromise, |
| 407 webUIListenerCallback: webUIListenerCallback, | 409 webUIListenerCallback: webUIListenerCallback, |
| 408 addWebUIListener: addWebUIListener, | 410 addWebUIListener: addWebUIListener, |
| 409 PropertyKind: PropertyKind, | 411 PropertyKind: PropertyKind, |
| 410 | 412 |
| 411 get doc() { | 413 get doc() { |
| 412 return document; | 414 return document; |
| 413 }, | 415 }, |
| 414 | 416 |
| 415 /** Whether we are using a Mac or not. */ | 417 /** Whether we are using a Mac or not. */ |
| 416 get isMac() { | 418 get isMac() { |
| 417 return /Mac/.test(navigator.platform); | 419 return /Mac/.test(navigator.platform); |
| 418 }, | 420 }, |
| 419 | 421 |
| 420 /** Whether this is on the Windows platform or not. */ | 422 /** Whether this is on the Windows platform or not. */ |
| 421 get isWindows() { | 423 get isWindows() { |
| 422 return /Win/.test(navigator.platform); | 424 return /Win/.test(navigator.platform); |
| 423 }, | 425 }, |
| 424 | 426 |
| 425 /** Whether this is on chromeOS or not. */ | 427 /** Whether this is on chromeOS or not. */ |
| 426 get isChromeOS() { | 428 get isChromeOS() { |
| 427 return /CrOS/.test(navigator.userAgent); | 429 return /CrOS/.test(navigator.userAgent); |
| 428 }, | 430 }, |
| 429 | 431 |
| 430 /** Whether this is on vanilla Linux (not chromeOS). */ | 432 /** Whether this is on vanilla Linux (not chromeOS). */ |
| 431 get isLinux() { | 433 get isLinux() { |
| 432 return /Linux/.test(navigator.userAgent); | 434 return /Linux/.test(navigator.userAgent); |
| 433 }, | 435 }, |
| 434 }; | 436 }; |
| 435 }(); | 437 }(); |
| OLD | NEW |