OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 Basic facillities to handle events from a single automation | 6 * @fileoverview Basic facillities to handle events from a single automation |
7 * node. | 7 * node. |
8 */ | 8 */ |
9 | 9 |
10 goog.provide('BaseAutomationHandler'); | 10 goog.provide('BaseAutomationHandler'); |
11 | 11 |
| 12 goog.require('ChromeVoxAutomationEvent'); |
| 13 |
12 goog.scope(function() { | 14 goog.scope(function() { |
13 var AutomationEvent = chrome.automation.AutomationEvent; | 15 var AutomationEvent = chrome.automation.AutomationEvent; |
14 var AutomationNode = chrome.automation.AutomationNode; | 16 var AutomationNode = chrome.automation.AutomationNode; |
15 var EventType = chrome.automation.EventType; | 17 var EventType = chrome.automation.EventType; |
16 | 18 |
17 /** | 19 /** |
18 * @param {!AutomationNode} node | 20 * @param {!AutomationNode} node |
19 * @constructor | 21 * @constructor |
20 */ | 22 */ |
21 BaseAutomationHandler = function(node) { | 23 BaseAutomationHandler = function(node) { |
22 /** | 24 /** |
23 * @type {!AutomationNode} | 25 * @type {!AutomationNode} |
| 26 * @private |
24 */ | 27 */ |
25 this.node_ = node; | 28 this.node_ = node; |
26 | 29 |
27 /** @type {!Object<EventType, function(!AutomationEvent): void>} @private */ | 30 /** @type {!Object<EventType, function(!AutomationEvent): void>} @private */ |
28 this.listeners_ = {}; | 31 this.listeners_ = {}; |
29 }; | 32 }; |
30 | 33 |
31 BaseAutomationHandler.prototype = { | 34 BaseAutomationHandler.prototype = { |
32 /** | 35 /** |
33 * Adds an event listener to this handler. | 36 * Adds an event listener to this handler. |
34 * @param {chrome.automation.EventType} eventType | 37 * @param {chrome.automation.EventType} eventType |
35 * @param {!function(!AutomationEvent): void} eventCallback | 38 * @param {function((!AutomationEvent|!ChromeVoxAutomationEvent)): void} |
| 39 * eventCallback |
36 * @protected | 40 * @protected |
37 */ | 41 */ |
38 addListener_: function(eventType, eventCallback) { | 42 addListener_: function(eventType, eventCallback) { |
39 if (this.listeners_[eventType]) | 43 if (this.listeners_[eventType]) |
40 throw 'Listener already added: ' + eventType; | 44 throw 'Listener already added: ' + eventType; |
41 | 45 |
42 var listener = this.makeListener_(eventCallback.bind(this)); | 46 var listener = this.makeListener_(eventCallback.bind(this)); |
43 this.node_.addEventListener(eventType, listener, true); | 47 this.node_.addEventListener(eventType, listener, true); |
44 this.listeners_[eventType] = listener; | 48 this.listeners_[eventType] = listener; |
45 }, | 49 }, |
46 | 50 |
47 /** | 51 /** |
48 * Removes all listeners from this handler. | 52 * Removes all listeners from this handler. |
49 */ | 53 */ |
50 removeAllListeners: function() { | 54 removeAllListeners: function() { |
51 for (var eventType in this.listeners_) { | 55 for (var eventType in this.listeners_) { |
52 this.node_.removeEventListener( | 56 this.node_.removeEventListener( |
53 eventType, this.listeners_[eventType], true); | 57 eventType, this.listeners_[eventType], true); |
54 } | 58 } |
55 }, | 59 }, |
56 | 60 |
57 /** | 61 /** |
58 * @return {!function(!AutomationEvent): void} | 62 * @return {!function((!AutomationEvent|!ChromeVoxAutomationEvent)): void} |
59 * @private | 63 * @private |
60 */ | 64 */ |
61 makeListener_: function(callback) { | 65 makeListener_: function(callback) { |
62 return function(evt) { | 66 return function(evt) { |
63 if (this.willHandleEvent_(evt)) | 67 if (this.willHandleEvent_(evt)) |
64 return; | 68 return; |
65 callback(evt); | 69 callback(evt); |
66 this.didHandleEvent_(evt); | 70 this.didHandleEvent_(evt); |
67 }.bind(this); | 71 }.bind(this); |
68 }, | 72 }, |
69 | 73 |
70 /** | 74 /** |
71 * Called before the event |evt| is handled. | 75 * Called before the event |evt| is handled. |
72 * @return {boolean} True to skip processing this event. | 76 * @return {boolean} True to skip processing this event. |
73 * @protected | 77 * @protected |
74 */ | 78 */ |
75 willHandleEvent_: function(evt) { | 79 willHandleEvent_: function(evt) { |
76 return false; | 80 return false; |
77 }, | 81 }, |
78 | 82 |
79 /** | 83 /** |
80 * Called after the event |evt| is handled. | 84 * Called after the event |evt| is handled. |
81 * @protected | 85 * @protected |
82 */ | 86 */ |
83 didHandleEvent_: function(evt) { | 87 didHandleEvent_: function(evt) { |
84 } | 88 } |
85 }; | 89 }; |
86 | 90 |
87 }); // goog.scope | 91 }); // goog.scope |
OLD | NEW |