Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(528)

Side by Side Diff: remoting/webapp/base/js/base.js

Issue 863863003: base.EventHook (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/remoting_webapp_files.gypi ('k') | remoting/webapp/unittests/event_hook_unittest.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 * @fileoverview 6 * @fileoverview
7 * A module that contains basic utility components and methods for the 7 * A module that contains basic utility components and methods for the
8 * chromoting project 8 * chromoting project
9 * 9 *
10 */ 10 */
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 return callstack.join('\n'); 55 return callstack.join('\n');
56 }; 56 };
57 57
58 /** 58 /**
59 * @interface 59 * @interface
60 */ 60 */
61 base.Disposable = function() {}; 61 base.Disposable = function() {};
62 base.Disposable.prototype.dispose = function() {}; 62 base.Disposable.prototype.dispose = function() {};
63 63
64 /** 64 /**
65 * @constructor
66 * @param {...base.Disposable} var_args
67 * @implements {base.Disposable}
68 */
69 base.Disposables = function(var_args) {
70 /**
71 * @type {Array.<base.Disposable>}
72 * @private
73 */
74 this.disposables_ = Array.prototype.slice.call(arguments, 0);
75 };
76
77 base.Disposables.prototype.dispose = function() {
78 for (var i = 0; i < this.disposables_.length; i++) {
79 this.disposables_[i].dispose();
80 }
81 };
82
83 /**
65 * A utility function to invoke |obj|.dispose without a null check on |obj|. 84 * A utility function to invoke |obj|.dispose without a null check on |obj|.
66 * @param {base.Disposable} obj 85 * @param {base.Disposable} obj
67 */ 86 */
68 base.dispose = function(obj) { 87 base.dispose = function(obj) {
69 if (obj) { 88 if (obj) {
70 base.debug.assert(typeof obj.dispose == 'function'); 89 base.debug.assert(typeof obj.dispose == 'function');
71 obj.dispose(); 90 obj.dispose();
72 } 91 }
73 }; 92 };
74 93
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 /** @param {function(*=):void} listener */ 445 /** @param {function(*=):void} listener */
427 function(listener){ 446 function(listener){
428 if (listener) { 447 if (listener) {
429 listener(opt_details); 448 listener(opt_details);
430 } 449 }
431 }); 450 });
432 } 451 }
433 }; 452 };
434 453
435 /** 454 /**
455 * An light weight object that helps managing the lifetime of an event
Jamie 2015/02/02 23:46:19 s/An/A/ s/light weight/lightweight/ s/managing/man
456 * listener.
457 *
458 * For example, do the following if you want to automatically unhook events
459 * when your object is disposed:
460 *
461 * var MyConstructor = function(domElement) {
462 * this.eventHooks_ = new base.Disposables(
463 * new base.EventHook(domElement, 'click', this.onClick_.bind(this)),
464 * new base.EventHook(domElement, 'keydown', this.onClick_.bind(this)),
465 * new base.ChromeEventHook(chrome.runtime.onMessage,
466 * this.onMessage_.bind(this))
467 * );
468 * }
469 *
470 * MyConstructor.prototype.dispose = function () {
Jamie 2015/02/02 23:46:20 No space before ()--even for comments :)
kelvinp 2015/02/03 01:42:01 Done.
471 * this.eventHooks_.dispose();
472 * this.eventHooks_ = null;
473 * }
474 *
475 * @param {base.EventSource | HTMLElement} src
476 * @param {string} eventName
477 * @param {Function} listener
kelvinp 2015/02/02 23:08:34 Function is the JavaScript official type name of f
478 * @param {boolean=} opt_capture
479 *
480 * @constructor
481 * @implements {base.Disposable}
482 */
483 base.EventHook = function(src, eventName, listener, opt_capture) {
484 this.src_ = src;
485 this.eventName_ = eventName;
486 this.listener_ = listener;
487 this.capture_ = opt_capture;
488 base.debug.assert(
489 (typeof opt_capture === 'boolean') === (src instanceof HTMLElement),
490 'opt_capture is only valid for DOM Events.');
Jamie 2015/02/02 23:46:19 I think this would be better guarded against by se
491 src.addEventListener(eventName, listener, opt_capture);
492 };
493
494 base.EventHook.prototype.dispose = function() {
495 this.src_.removeEventListener(this.eventName_, this.listener_, this.capture_);
496 };
497
498 /**
499 * An event hook implementation for Chrome Events.
500 *
501 * @param {chrome.Event} src
502 * @param {Function} listener
503 *
504 * @constructor
505 * @implements {base.Disposable}
506 */
507 base.ChromeEventHook = function(src, listener) {
508 this.src_ = src;
509 this.listener_ = listener;
510 src.addListener(listener);
511 };
512
513 base.ChromeEventHook.prototype.dispose = function() {
514 this.src_.removeListener(this.listener_);
515 };
516
517 /**
436 * Converts UTF-8 string to ArrayBuffer. 518 * Converts UTF-8 string to ArrayBuffer.
437 * 519 *
438 * @param {string} string 520 * @param {string} string
439 * @return {ArrayBuffer} 521 * @return {ArrayBuffer}
440 */ 522 */
441 base.encodeUtf8 = function(string) { 523 base.encodeUtf8 = function(string) {
442 var utf8String = unescape(encodeURIComponent(string)); 524 var utf8String = unescape(encodeURIComponent(string));
443 var result = new Uint8Array(utf8String.length); 525 var result = new Uint8Array(utf8String.length);
444 for (var i = 0; i < utf8String.length; i++) 526 for (var i = 0; i < utf8String.length; i++)
445 result[i] = utf8String.charCodeAt(i); 527 result[i] = utf8String.charCodeAt(i);
446 return result.buffer; 528 return result.buffer;
447 } 529 };
448 530
449 /** 531 /**
450 * Decodes UTF-8 string from ArrayBuffer. 532 * Decodes UTF-8 string from ArrayBuffer.
451 * 533 *
452 * @param {ArrayBuffer} buffer 534 * @param {ArrayBuffer} buffer
453 * @return {string} 535 * @return {string}
454 */ 536 */
455 base.decodeUtf8 = function(buffer) { 537 base.decodeUtf8 = function(buffer) {
456 return decodeURIComponent( 538 return decodeURIComponent(
457 escape(String.fromCharCode.apply(null, new Uint8Array(buffer)))); 539 escape(String.fromCharCode.apply(null, new Uint8Array(buffer))));
458 } 540 };
459 541
460 /** 542 /**
461 * Generate a nonce, to be used as an xsrf protection token. 543 * Generate a nonce, to be used as an xsrf protection token.
462 * 544 *
463 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ 545 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */
464 base.generateXsrfToken = function() { 546 base.generateXsrfToken = function() {
465 var random = new Uint8Array(16); 547 var random = new Uint8Array(16);
466 window.crypto.getRandomValues(random); 548 window.crypto.getRandomValues(random);
467 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); 549 var base64Token = window.btoa(String.fromCharCode.apply(null, random));
468 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); 550 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
469 }; 551 };
470 552
471 /** 553 /**
472 * @param {string} jsonString A JSON-encoded string. 554 * @param {string} jsonString A JSON-encoded string.
473 * @return {Object|undefined} The decoded object, or undefined if the string 555 * @return {Object|undefined} The decoded object, or undefined if the string
474 * cannot be parsed. 556 * cannot be parsed.
475 */ 557 */
476 base.jsonParseSafe = function(jsonString) { 558 base.jsonParseSafe = function(jsonString) {
477 try { 559 try {
478 return /** @type {Object} */ (JSON.parse(jsonString)); 560 return /** @type {Object} */ (JSON.parse(jsonString));
479 } catch (err) { 561 } catch (err) {
480 return undefined; 562 return undefined;
481 } 563 }
482 } 564 };
OLDNEW
« no previous file with comments | « remoting/remoting_webapp_files.gypi ('k') | remoting/webapp/unittests/event_hook_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698