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

Side by Side Diff: chrome/browser/resources/chromeos/chromevox/cvox2/background/base_automation_handler.js

Issue 1942683005: Refactor event handler classes to make it easier to add new event type listeners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
OLDNEW
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.scope(function() { 12 goog.scope(function() {
13 var AutomationEvent = chrome.automation.AutomationEvent; 13 var AutomationEvent = chrome.automation.AutomationEvent;
14 var AutomationNode = chrome.automation.AutomationNode; 14 var AutomationNode = chrome.automation.AutomationNode;
15 var EventType = chrome.automation.EventType; 15 var EventType = chrome.automation.EventType;
16 16
17 /** 17 /**
18 * @param {!AutomationNode} node 18 * @param {!AutomationNode} node
19 * @constructor 19 * @constructor
20 */ 20 */
21 BaseAutomationHandler = function(node) { 21 BaseAutomationHandler = function(node) {
22 /** 22 /**
23 * @type {!AutomationNode} 23 * @type {!AutomationNode}
24 */ 24 */
25 this.node_ = node; 25 this.node_ = node;
26 26
27 /** 27 /** @type {!Object<EventType, function(!AutomationEvent): void>} @private */
28 * Maps an automation event to its listener.
29 * @type {!Object<EventType, function(!AutomationEvent) : void>}
30 */
31 this.listenerMap_ = {
32 alert: this.onAlert,
33 ariaAttributeChanged: this.onEventIfInRange,
34 checkedStateChanged: this.onEventIfInRange,
35 focus: this.onFocus,
36 hover: this.onEventWithFlushedOutput,
37 loadComplete: this.onLoadComplete,
38 menuListItemSelected: this.onEventDefault,
39 menuStart: this.onMenuStart,
40 menuEnd: this.onMenuEnd,
41 scrollPositionChanged: this.onScrollPositionChanged,
42 selection: this.onEventWithFlushedOutput,
43 textChanged: this.onTextChanged,
44 textSelectionChanged: this.onTextSelectionChanged,
45 valueChanged: this.onValueChanged
46 };
47
48 /** @type {!Object<string, function(!AutomationEvent): void>} @private */
49 this.listeners_ = {}; 28 this.listeners_ = {};
50
51 this.register_();
52 }; 29 };
53 30
54 BaseAutomationHandler.prototype = { 31 BaseAutomationHandler.prototype = {
55 /** 32 /**
56 * Registers event listeners. Can be called repeatedly without duplicate 33 * Adds an event listener to this handler.
57 * listeners. 34 * @param {chrome.automation.EventType} eventType
58 * @private 35 * @param {!function(!AutomationEvent): void} eventCallback
36 * @protected
59 */ 37 */
60 register_: function() { 38 addListener_: function(eventType, eventCallback) {
61 for (var eventType in this.listenerMap_) { 39 if (this.listeners_[eventType])
62 var listener = 40 throw 'Listener already added: ' + eventType;
63 this.makeListener_(this.listenerMap_[eventType].bind(this)); 41
64 this.node_.addEventListener(eventType, listener, true); 42 var listener = this.makeListener_(eventCallback.bind(this));
65 this.listeners_[eventType] = listener; 43 this.node_.addEventListener(eventType, listener, true);
66 } 44 this.listeners_[eventType] = listener;
67 }, 45 },
68 46
69 /** 47 /**
70 * Unregisters listeners. 48 * Removes all listeners from this handler.
71 */ 49 */
72 unregister: function() { 50 removeAllListeners: function() {
73 for (var eventType in this.listenerMap_) { 51 for (var eventType in this.listeners_) {
74 this.node_.removeEventListener( 52 this.node_.removeEventListener(
75 eventType, this.listeners_[eventType], true); 53 eventType, this.listeners_[eventType], true);
76 } 54 }
77 }, 55 },
78 56
79 /** 57 /**
80 * @return {!function(!AutomationEvent): void} 58 * @return {!function(!AutomationEvent): void}
81 * @private 59 * @private
82 */ 60 */
83 makeListener_: function(callback) { 61 makeListener_: function(callback) {
(...skipping 12 matching lines...) Expand all
96 */ 74 */
97 willHandleEvent_: function(evt) { 75 willHandleEvent_: function(evt) {
98 return false; 76 return false;
99 }, 77 },
100 78
101 /** 79 /**
102 * Called after the event |evt| is handled. 80 * Called after the event |evt| is handled.
103 * @protected 81 * @protected
104 */ 82 */
105 didHandleEvent_: function(evt) { 83 didHandleEvent_: function(evt) {
106 }, 84 }
107
108 /**
109 * @param {!AutomationEvent} evt
110 */
111 onAlert: function(evt) {},
112
113 /**
114 * @param {!AutomationEvent} evt
115 */
116 onFocus: function(evt) {},
117
118 /**
119 * @param {!AutomationEvent} evt
120 */
121 onLoadComplete: function(evt) {},
122
123 /**
124 * @param {!AutomationEvent} evt
125 */
126 onEventDefault: function(evt) {},
127
128 /**
129 * @param {!AutomationEvent} evt
130 */
131 onEventIfInRange: function(evt) {
132 // TODO(dtseng): Consider the end of the current range as well.
133 if (AutomationUtil.isDescendantOf(
134 global.backgroundObj.currentRange.start.node, evt.target) ||
135 evt.target.state.focused)
136 this.onEventDefault(evt);
137 },
138
139 /**
140 * @param {!AutomationEvent} evt
141 */
142 onEventWithFlushedOutput: function(evt) {
143 Output.flushNextSpeechUtterance();
144 this.onEventDefault(evt);
145 },
146
147 /**
148 * @param {!AutomationEvent} evt
149 */
150 onMenuStart: function(evt) {},
151
152 /**
153 * @param {!AutomationEvent} evt
154 */
155 onMenuEnd: function(evt) {},
156
157 /**
158 * @param {!AutomationEvent} evt
159 */
160 onScrollPositionChanged: function(evt) {},
161
162 /**
163 * @param {!AutomationEvent} evt
164 */
165 onTextChanged: function(evt) {},
166
167 /**
168 * @param {!AutomationEvent} evt
169 */
170 onTextSelectionChanged: function(evt) {},
171
172 /**
173 * @param {!AutomationEvent} evt
174 */
175 onValueChanged: function(evt) {}
176 }; 85 };
177 86
178 }); // goog.scope 87 }); // goog.scope
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698