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 // If no response arguments were passed from C++, resolve the Promise |
| 341 // without any arguments, else wrap all response arguments in an array |
| 342 // since Promise#resolve accepts only one value. |
| 343 var responseArgs = Array.prototype.slice.call(arguments, 1); |
| 344 resolverFn(responseArgs.length == 0 ? undefined : responseArgs); |
341 } | 345 } |
342 | 346 |
343 /** | 347 /** |
344 * A variation of chrome.send which allows the client to receive a direct | 348 * A variation of chrome.send, suitable for messages that expect a single |
345 * callback without requiring the handler to have specific knowledge of any | 349 * 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. | 350 * @param {string} methodName The name of the WebUI handler API. |
349 * @param {Array|undefined} args Arguments for the method call sent to the | 351 * @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. | 352 * C++ call. |
351 * @param {Function} callback A callback function which is called (indirectly) | 353 * @return {!Promise} |
352 * by the WebUI handler. | |
353 */ | 354 */ |
354 function sendWithCallback(methodName, args, callback) { | 355 function sendWithPromise(methodName, var_args) { |
355 var id = methodName + createUid(); | 356 var args = Array.prototype.slice.call(arguments, 1); |
356 chromeSendCallbackMap[id] = callback; | 357 return new Promise(function(resolve, reject) { |
357 chrome.send(methodName, ['cr.webUIResponse', id].concat(args || [])); | 358 var id = methodName + '_' + createUid(); |
| 359 chromeSendResolverMap[id] = resolve; |
| 360 chrome.send(methodName, ['cr.webUIResponse', id].concat(args)); |
| 361 }); |
358 } | 362 } |
359 | 363 |
360 /** | 364 /** |
361 * A registry of callbacks keyed by event name. Used by addWebUIListener to | 365 * A registry of callbacks keyed by event name. Used by addWebUIListener to |
362 * register listeners. | 366 * register listeners. |
363 * @type {!Object<Array<Function>>} | 367 * @type {!Object<Array<Function>>} |
364 */ | 368 */ |
365 var webUIListenerMap = Object.create(null); | 369 var webUIListenerMap = Object.create(null); |
366 | 370 |
367 /** | 371 /** |
(...skipping 28 matching lines...) Expand all Loading... |
396 addSingletonGetter: addSingletonGetter, | 400 addSingletonGetter: addSingletonGetter, |
397 createUid: createUid, | 401 createUid: createUid, |
398 define: define, | 402 define: define, |
399 defineProperty: defineProperty, | 403 defineProperty: defineProperty, |
400 dispatchPropertyChange: dispatchPropertyChange, | 404 dispatchPropertyChange: dispatchPropertyChange, |
401 dispatchSimpleEvent: dispatchSimpleEvent, | 405 dispatchSimpleEvent: dispatchSimpleEvent, |
402 exportPath: exportPath, | 406 exportPath: exportPath, |
403 getUid: getUid, | 407 getUid: getUid, |
404 makePublic: makePublic, | 408 makePublic: makePublic, |
405 webUIResponse: webUIResponse, | 409 webUIResponse: webUIResponse, |
406 sendWithCallback: sendWithCallback, | 410 sendWithPromise: sendWithPromise, |
407 webUIListenerCallback: webUIListenerCallback, | 411 webUIListenerCallback: webUIListenerCallback, |
408 addWebUIListener: addWebUIListener, | 412 addWebUIListener: addWebUIListener, |
409 PropertyKind: PropertyKind, | 413 PropertyKind: PropertyKind, |
410 | 414 |
411 get doc() { | 415 get doc() { |
412 return document; | 416 return document; |
413 }, | 417 }, |
414 | 418 |
415 /** Whether we are using a Mac or not. */ | 419 /** Whether we are using a Mac or not. */ |
416 get isMac() { | 420 get isMac() { |
417 return /Mac/.test(navigator.platform); | 421 return /Mac/.test(navigator.platform); |
418 }, | 422 }, |
419 | 423 |
420 /** Whether this is on the Windows platform or not. */ | 424 /** Whether this is on the Windows platform or not. */ |
421 get isWindows() { | 425 get isWindows() { |
422 return /Win/.test(navigator.platform); | 426 return /Win/.test(navigator.platform); |
423 }, | 427 }, |
424 | 428 |
425 /** Whether this is on chromeOS or not. */ | 429 /** Whether this is on chromeOS or not. */ |
426 get isChromeOS() { | 430 get isChromeOS() { |
427 return /CrOS/.test(navigator.userAgent); | 431 return /CrOS/.test(navigator.userAgent); |
428 }, | 432 }, |
429 | 433 |
430 /** Whether this is on vanilla Linux (not chromeOS). */ | 434 /** Whether this is on vanilla Linux (not chromeOS). */ |
431 get isLinux() { | 435 get isLinux() { |
432 return /Linux/.test(navigator.userAgent); | 436 return /Linux/.test(navigator.userAgent); |
433 }, | 437 }, |
434 }; | 438 }; |
435 }(); | 439 }(); |
OLD | NEW |