| 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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 var target = opt_target ? document.getElementById(opt_target) : | 311 var target = opt_target ? document.getElementById(opt_target) : |
| 312 ctor.getInstance(); | 312 ctor.getInstance(); |
| 313 return target[method + '_'].apply(target, arguments); | 313 return target[method + '_'].apply(target, arguments); |
| 314 }; | 314 }; |
| 315 }); | 315 }); |
| 316 } | 316 } |
| 317 | 317 |
| 318 /** | 318 /** |
| 319 * The mapping used by the sendWithPromise mechanism to tie the Promise | 319 * The mapping used by the sendWithPromise mechanism to tie the Promise |
| 320 * returned to callers with the corresponding WebUI response. The mapping is | 320 * returned to callers with the corresponding WebUI response. The mapping is |
| 321 * from ID to the Promise resolver function; the ID is generated by | 321 * from ID to the PromiseResolver helper; the ID is generated by |
| 322 * sendWithPromise and is unique across all invocations of said method. | 322 * sendWithPromise and is unique across all invocations of said method. |
| 323 * @type {!Object<!Function>} | 323 * @type {!Object<!PromiseResolver>} |
| 324 */ | 324 */ |
| 325 var chromeSendResolverMap = {}; | 325 var chromeSendResolverMap = {}; |
| 326 | 326 |
| 327 /** | 327 /** |
| 328 * The named method the WebUI handler calls directly in response to a | 328 * The named method the WebUI handler calls directly in response to a |
| 329 * chrome.send call that expects a response. The handler requires no knowledge | 329 * chrome.send call that expects a response. The handler requires no knowledge |
| 330 * of the specific name of this method, as the name is passed to the handler | 330 * of the specific name of this method, as the name is passed to the handler |
| 331 * as the first argument in the arguments list of chrome.send. The handler | 331 * as the first argument in the arguments list of chrome.send. The handler |
| 332 * must pass the ID, also sent via the chrome.send arguments list, as the | 332 * must pass the ID, also sent via the chrome.send arguments list, as the |
| 333 * first argument of the JS invocation; additionally, the handler may | 333 * first argument of the JS invocation; additionally, the handler may |
| 334 * supply any number of other arguments that will be included in the response. | 334 * supply any number of other arguments that will be included in the response. |
| 335 * @param {string} id The unique ID identifying the Promise this response is | 335 * @param {string} id The unique ID identifying the Promise this response is |
| 336 * tied to. | 336 * tied to. |
| 337 * @param {boolean} isSuccess Whether the request was successful. |
| 337 * @param {*} response The response as sent from C++. | 338 * @param {*} response The response as sent from C++. |
| 338 */ | 339 */ |
| 339 function webUIResponse(id, response) { | 340 function webUIResponse(id, isSuccess, response) { |
| 340 var resolverFn = chromeSendResolverMap[id]; | 341 var resolver = chromeSendResolverMap[id]; |
| 341 delete chromeSendResolverMap[id]; | 342 delete chromeSendResolverMap[id]; |
| 342 resolverFn(response); | 343 |
| 344 if (isSuccess) |
| 345 resolver.resolve(response); |
| 346 else |
| 347 resolver.reject(response); |
| 343 } | 348 } |
| 344 | 349 |
| 345 /** | 350 /** |
| 346 * A variation of chrome.send, suitable for messages that expect a single | 351 * A variation of chrome.send, suitable for messages that expect a single |
| 347 * response from C++. | 352 * response from C++. |
| 348 * @param {string} methodName The name of the WebUI handler API. | 353 * @param {string} methodName The name of the WebUI handler API. |
| 349 * @param {...*} var_args Varibale number of arguments to be forwarded to the | 354 * @param {...*} var_args Varibale number of arguments to be forwarded to the |
| 350 * C++ call. | 355 * C++ call. |
| 351 * @return {!Promise} | 356 * @return {!Promise} |
| 352 */ | 357 */ |
| 353 function sendWithPromise(methodName, var_args) { | 358 function sendWithPromise(methodName, var_args) { |
| 354 var args = Array.prototype.slice.call(arguments, 1); | 359 var args = Array.prototype.slice.call(arguments, 1); |
| 355 return new Promise(function(resolve, reject) { | 360 var promiseResolver = new PromiseResolver(); |
| 356 var id = methodName + '_' + createUid(); | 361 var id = methodName + '_' + createUid(); |
| 357 chromeSendResolverMap[id] = resolve; | 362 chromeSendResolverMap[id] = promiseResolver; |
| 358 chrome.send(methodName, [id].concat(args)); | 363 chrome.send(methodName, [id].concat(args)); |
| 359 }); | 364 return promiseResolver.promise; |
| 360 } | 365 } |
| 361 | 366 |
| 362 /** | 367 /** |
| 363 * A map of maps associating event names with listeners. The 2nd level map | 368 * A map of maps associating event names with listeners. The 2nd level map |
| 364 * associates a listener ID with the callback function, such that individual | 369 * associates a listener ID with the callback function, such that individual |
| 365 * listeners can be removed from an event without affecting other listeners of | 370 * listeners can be removed from an event without affecting other listeners of |
| 366 * the same event. | 371 * the same event. |
| 367 * @type {!Object<!Object<!Function>>} | 372 * @type {!Object<!Object<!Function>>} |
| 368 */ | 373 */ |
| 369 var webUIListenerMap = {}; | 374 var webUIListenerMap = {}; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 get isLinux() { | 469 get isLinux() { |
| 465 return /Linux/.test(navigator.userAgent); | 470 return /Linux/.test(navigator.userAgent); |
| 466 }, | 471 }, |
| 467 | 472 |
| 468 /** Whether this is on Android. */ | 473 /** Whether this is on Android. */ |
| 469 get isAndroid() { | 474 get isAndroid() { |
| 470 return /Android/.test(navigator.userAgent); | 475 return /Android/.test(navigator.userAgent); |
| 471 } | 476 } |
| 472 }; | 477 }; |
| 473 }(); | 478 }(); |
| OLD | NEW |