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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 sendWithCallback mechanism to tie the callback |
| 317 * supplied to an invocation of sendWithCallback with the WebUI response | 317 * supplied to an invocation of sendWithCallback with the WebUI response |
| 318 * sent by the browser in response to the chrome.send call. The mapping is | 318 * sent by the browser in response to the chrome.send call. The mapping is |
| 319 * from ID to callback function; the ID is generated by sendWithCallback and | 319 * from ID to callback function; the ID is generated by sendWithCallback and |
| 320 * is unique across all invocations of said method. | 320 * is unique across all invocations of said method. |
| 321 * @type {!Map<string, Function>} | 321 * @type {!Map<string, Function>} |
|
Dan Beam
2015/07/09 21:03:27
^ fix
James Hawkins
2015/07/09 22:02:50
Done.
| |
| 322 */ | 322 */ |
| 323 var chromeSendCallbackMap = new Map(); | 323 var chromeSendCallbackMap = Object.create(null); |
| 324 | 324 |
| 325 /** | 325 /** |
| 326 * The named method the WebUI handler calls directly in response to a | 326 * 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 | 327 * chrome.send call that expects a callback. The handler requires no knowledge |
| 328 * of the specific name of this method, as the name is passed to the handler | 328 * 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 | 329 * 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 | 330 * 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 | 331 * first argument of the JS invocation; additionally, the handler may |
| 332 * supply any number of other arguments that will be forwarded to the | 332 * supply any number of other arguments that will be forwarded to the |
| 333 * callback. | 333 * callback. |
| 334 * @param {string} id The unique ID identifying the callback method this | 334 * @param {string} id The unique ID identifying the callback method this |
| 335 * response is tied to. | 335 * response is tied to. |
| 336 */ | 336 */ |
| 337 function webUIResponse(id) { | 337 function webUIResponse(id) { |
| 338 chromeSendCallbackMap.get(id).apply( | 338 chromeSendCallbackMap[id].apply( |
| 339 null, Array.prototype.slice.call(arguments, 1)); | 339 null, Array.prototype.slice.call(arguments, 1)); |
| 340 chromeSendCallbackMap.delete(id); | 340 delete chromeSendCallbackMap[id]; |
| 341 } | 341 } |
| 342 | 342 |
| 343 /** | 343 /** |
| 344 * A variation of chrome.send which allows the client to receive a direct | 344 * A variation of chrome.send which allows the client to receive a direct |
| 345 * callback without requiring the handler to have specific knowledge of any | 345 * callback without requiring the handler to have specific knowledge of any |
| 346 * JS internal method names or state. The callback will be removed from the | 346 * JS internal method names or state. The callback will be removed from the |
| 347 * mapping once it has fired. | 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 {Array|undefined} args Arguments for the method call sent to the |
| 350 * WebUI handler. Pass undefined if no args should be sent to the handler. | 350 * WebUI handler. Pass undefined if no args should be sent to the handler. |
| 351 * @param {Function} callback A callback function which is called (indirectly) | 351 * @param {Function} callback A callback function which is called (indirectly) |
| 352 * by the WebUI handler. | 352 * by the WebUI handler. |
| 353 */ | 353 */ |
| 354 function sendWithCallback(methodName, args, callback) { | 354 function sendWithCallback(methodName, args, callback) { |
| 355 var id = methodName + createUid(); | 355 var id = methodName + createUid(); |
| 356 chromeSendCallbackMap.set(id, callback); | 356 chromeSendCallbackMap[id] = callback; |
| 357 chrome.send(methodName, ['cr.webUIResponse', id].concat(args || [])); | 357 chrome.send(methodName, ['cr.webUIResponse', id].concat(args || [])); |
| 358 } | 358 } |
| 359 | 359 |
| 360 /** | 360 /** |
| 361 * A registry of callbacks keyed by event name. Used by addWebUIListener to | 361 * A registry of callbacks keyed by event name. Used by addWebUIListener to |
| 362 * register listeners. | 362 * register listeners. |
| 363 * @type {!Map<string, Array<Function>>} | 363 * @type {!Map<string, Array<Function>>} |
| 364 */ | 364 */ |
| 365 var webUIListenerMap = new Map(); | 365 var webUIListenerMap = Object.create(null); |
| 366 | 366 |
| 367 /** | 367 /** |
| 368 * The named method the WebUI handler calls directly when an event occurs. | 368 * The named method the WebUI handler calls directly when an event occurs. |
| 369 * The WebUI handler must supply the name of the event as the first argument | 369 * The WebUI handler must supply the name of the event as the first argument |
| 370 * of the JS invocation; additionally, the handler may supply any number of | 370 * of the JS invocation; additionally, the handler may supply any number of |
| 371 * other arguments that will be forwarded to the listener callbacks. | 371 * other arguments that will be forwarded to the listener callbacks. |
| 372 * @param {string} event The name of the event that has occurred. | 372 * @param {string} event The name of the event that has occurred. |
| 373 */ | 373 */ |
| 374 function webUIListenerCallback(event) { | 374 function webUIListenerCallback(event) { |
| 375 var listenerCallbacks = webUIListenerMap.get(event); | 375 var listenerCallbacks = webUIListenerMap[event]; |
| 376 for (var i = 0; i < listenerCallbacks.length; i++) { | 376 for (var i = 0; i < listenerCallbacks.length; i++) { |
|
Dan Beam
2015/07/09 21:03:27
wait, this doesn't make much sense...
chrome.send
James Hawkins
2015/07/09 22:02:50
The code you're commenting is the event listener c
Dan Beam
2015/07/09 22:11:15
ah, no.
btw, i just saw the createUid() that hand
| |
| 377 var callback = listenerCallbacks[i]; | 377 var callback = listenerCallbacks[i]; |
| 378 callback.apply(null, Array.prototype.slice.call(arguments, 1)); | 378 callback.apply(null, Array.prototype.slice.call(arguments, 1)); |
| 379 } | 379 } |
| 380 } | 380 } |
| 381 | 381 |
| 382 /** | 382 /** |
| 383 * Registers a listener for an event fired from WebUI handlers. Any number of | 383 * Registers a listener for an event fired from WebUI handlers. Any number of |
| 384 * listeners may register for a single event. | 384 * listeners may register for a single event. |
| 385 * @param {string} event The event to listen to. | 385 * @param {string} event The event to listen to. |
| 386 * @param {Function} callback The callback run when the event is fired. | 386 * @param {Function} callback The callback run when the event is fired. |
| 387 */ | 387 */ |
| 388 function addWebUIListener(event, callback) { | 388 function addWebUIListener(event, callback) { |
| 389 if (!webUIListenerMap.has(event)) | 389 if (event in webUIListenerMap) |
| 390 webUIListenerMap.set(event, [callback]); | 390 webUIListenerMap[event].push(callback); |
| 391 else | 391 else |
| 392 webUIListenerMap.get(event).push(callback); | 392 webUIListenerMap[event] = [callback]; |
| 393 } | 393 } |
| 394 | 394 |
| 395 return { | 395 return { |
| 396 addSingletonGetter: addSingletonGetter, | 396 addSingletonGetter: addSingletonGetter, |
| 397 createUid: createUid, | 397 createUid: createUid, |
| 398 define: define, | 398 define: define, |
| 399 defineProperty: defineProperty, | 399 defineProperty: defineProperty, |
| 400 dispatchPropertyChange: dispatchPropertyChange, | 400 dispatchPropertyChange: dispatchPropertyChange, |
| 401 dispatchSimpleEvent: dispatchSimpleEvent, | 401 dispatchSimpleEvent: dispatchSimpleEvent, |
| 402 exportPath: exportPath, | 402 exportPath: exportPath, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 426 get isChromeOS() { | 426 get isChromeOS() { |
| 427 return /CrOS/.test(navigator.userAgent); | 427 return /CrOS/.test(navigator.userAgent); |
| 428 }, | 428 }, |
| 429 | 429 |
| 430 /** Whether this is on vanilla Linux (not chromeOS). */ | 430 /** Whether this is on vanilla Linux (not chromeOS). */ |
| 431 get isLinux() { | 431 get isLinux() { |
| 432 return /Linux/.test(navigator.userAgent); | 432 return /Linux/.test(navigator.userAgent); |
| 433 }, | 433 }, |
| 434 }; | 434 }; |
| 435 }(); | 435 }(); |
| OLD | NEW |