OLD | NEW |
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 The entry point for all ChromeVox2 related code for the | 6 * @fileoverview The entry point for all ChromeVox2 related code for the |
7 * background page. | 7 * background page. |
8 */ | 8 */ |
9 | 9 |
10 goog.provide('cvox2.Background'); | 10 goog.provide('cvox2.Background'); |
11 goog.provide('cvox2.global'); | 11 goog.provide('cvox2.global'); |
12 | 12 |
13 goog.require('cvox.TabsApiHandler'); | 13 goog.require('cvox.TabsApiHandler'); |
| 14 goog.require('cvox2.AutomationPredicates'); |
| 15 goog.require('cvox2.AutomationUtil'); |
14 | 16 |
15 /** Classic Chrome accessibility API. */ | 17 /** Classic Chrome accessibility API. */ |
16 cvox2.global.accessibility = | 18 cvox2.global.accessibility = |
17 chrome.accessibilityPrivate || chrome.experimental.accessibility; | 19 chrome.accessibilityPrivate || chrome.experimental.accessibility; |
18 | 20 |
19 /** | 21 /** |
20 * ChromeVox2 background page. | 22 * ChromeVox2 background page. |
21 */ | 23 */ |
22 cvox2.Background = function() { | 24 cvox2.Background = function() { |
23 /** | 25 /** |
24 * A list of site substring patterns to use with ChromeVox next. Keep these | 26 * A list of site substring patterns to use with ChromeVox next. Keep these |
25 * strings relatively specific. | 27 * strings relatively specific. |
26 * @type {!Array.<string>} | 28 * @type {!Array.<string>} |
27 */ | 29 */ |
28 this.whitelist_ = ['http://www.chromevox.com/', 'chromevox_next_test']; | 30 this.whitelist_ = ['http://www.chromevox.com/', 'chromevox_next_test']; |
29 | 31 |
30 /** @type {cvox.TabsApiHandler} @private */ | 32 /** @type {cvox.TabsApiHandler} @private */ |
31 this.tabsHandler_ = new cvox.TabsApiHandler(cvox.ChromeVox.tts, | 33 this.tabsHandler_ = new cvox.TabsApiHandler(cvox.ChromeVox.tts, |
32 cvox.ChromeVox.braille, | 34 cvox.ChromeVox.braille, |
33 cvox.ChromeVox.earcons); | 35 cvox.ChromeVox.earcons); |
34 | 36 |
| 37 /** @type {AutomationNode} @private */ |
| 38 this.currentNode_ = null; |
| 39 |
| 40 /** @type {cvox.TabsApiHandler} @private */ |
| 41 this.tabsHandler_ = new cvox.TabsApiHandler(cvox.ChromeVox.tts, |
| 42 cvox.ChromeVox.braille, |
| 43 cvox.ChromeVox.earcons); |
| 44 |
35 // Only needed with unmerged ChromeVox classic loaded before. | 45 // Only needed with unmerged ChromeVox classic loaded before. |
36 cvox2.global.accessibility.setAccessibilityEnabled(false); | 46 cvox2.global.accessibility.setAccessibilityEnabled(false); |
37 | 47 |
38 // Manually bind all functions to |this|. | 48 // Manually bind all functions to |this|. |
39 for (var func in this) { | 49 for (var func in this) { |
40 if (typeof(this[func]) == 'function') | 50 if (typeof(this[func]) == 'function') |
41 this[func] = this[func].bind(this); | 51 this[func] = this[func].bind(this); |
42 } | 52 } |
43 | 53 |
44 // Register listeners for ... | 54 // Register listeners for ... |
(...skipping 14 matching lines...) Expand all Loading... |
59 chrome.tabs.get(tabId, function(tab) { | 69 chrome.tabs.get(tabId, function(tab) { |
60 if (!tab.url) | 70 if (!tab.url) |
61 return; | 71 return; |
62 | 72 |
63 if (!this.isWhitelisted_(tab.url)) { | 73 if (!this.isWhitelisted_(tab.url)) { |
64 chrome.commands.onCommand.removeListener(this.onGotCommand); | 74 chrome.commands.onCommand.removeListener(this.onGotCommand); |
65 cvox.ChromeVox.background.injectChromeVoxIntoTabs([tab], true); | 75 cvox.ChromeVox.background.injectChromeVoxIntoTabs([tab], true); |
66 return; | 76 return; |
67 } | 77 } |
68 | 78 |
69 if (!chrome.commands.onCommand.hasListeners()) { | 79 if (!chrome.commands.onCommand.hasListeners()) |
70 chrome.commands.onCommand.addListener(this.onGotCommand); | 80 chrome.commands.onCommand.addListener(this.onGotCommand); |
71 } | |
72 | 81 |
73 this.disableClassicChromeVox_(tab.id); | 82 this.disableClassicChromeVox_(tab.id); |
74 | 83 |
75 chrome.automation.getTree(this.onGotTree.bind(this)); | 84 chrome.automation.getTree(this.onGotTree.bind(this)); |
76 }.bind(this)); | 85 }.bind(this)); |
77 }, | 86 }, |
78 | 87 |
79 /** | 88 /** |
80 * Handles all setup once a new automation tree appears. | 89 * Handles all setup once a new automation tree appears. |
81 * @param {AutomationTree} tree The new automation tree. | 90 * @param {AutomationTree} tree The new automation tree. |
82 */ | 91 */ |
83 onGotTree: function(root) { | 92 onGotTree: function(root) { |
84 // Register all automation event listeners. | 93 // Register all automation event listeners. |
85 root.addEventListener(chrome.automation.EventType.focus, | 94 root.addEventListener(chrome.automation.EventType.focus, |
86 this.onAutomationEvent.bind(this), | 95 this.onFocus, |
87 true); | 96 true); |
88 }, | 97 root.addEventListener(chrome.automation.EventType.loadComplete, |
| 98 this.onLayoutOrLoadComplete, |
| 99 true); |
| 100 root.addEventListener(chrome.automation.EventType.layoutComplete, |
| 101 this.onLayoutOrLoadComplete, |
| 102 true); |
89 | 103 |
90 /** | 104 if (root.attributes.docLoaded) |
91 * A generic handler for all desktop automation events. | 105 this.onLayoutOrLoadComplete({target: root}); |
92 * @param {AutomationEvent} evt The event. | |
93 */ | |
94 onAutomationEvent: function(evt) { | |
95 var output = evt.target.attributes.name + ' ' + evt.target.role; | |
96 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH); | |
97 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); | |
98 }, | 106 }, |
99 | 107 |
100 /** | 108 /** |
101 * Handles chrome.commands.onCommand. | 109 * Handles chrome.commands.onCommand. |
102 * @param {string} command | 110 * @param {string} command |
103 */ | 111 */ |
104 onGotCommand: function(command) { | 112 onGotCommand: function(command) { |
| 113 if (!this.current_) |
| 114 return; |
| 115 |
| 116 var previous = this.current_; |
| 117 var current = this.current_; |
| 118 |
| 119 // Reverse? |
| 120 var r = false; |
| 121 var pred = null; |
| 122 switch (command) { |
| 123 case 'nextHeading': |
| 124 r = false; |
| 125 pred = cvox2.AutomationPredicates.heading; |
| 126 break; |
| 127 case 'previousHeading': |
| 128 r = true; |
| 129 pred = cvox2.AutomationPredicates.heading; |
| 130 break; |
| 131 case 'nextLine': |
| 132 r = false; |
| 133 pred = cvox2.AutomationPredicates.inlineTextBox; |
| 134 break; |
| 135 case 'previousLine': |
| 136 r = true; |
| 137 pred = cvox2.AutomationPredicates.inlineTextBox; |
| 138 break; |
| 139 case 'nextLink': |
| 140 r = false; |
| 141 pred = cvox2.AutomationPredicates.link; |
| 142 break; |
| 143 case 'previousLink': |
| 144 r = true; |
| 145 pred = cvox2.AutomationPredicates.link; |
| 146 break; |
| 147 case 'nextElement': |
| 148 current = current.role == chrome.automation.RoleType.inlineTextBox ? |
| 149 current.parent() : current; |
| 150 current = cvox2.AutomationUtil.findNextNode(current, |
| 151 false, |
| 152 cvox2.AutomationPredicates.inlineTextBox); |
| 153 current = current ? current.parent() : current; |
| 154 break; |
| 155 case 'previousElement': |
| 156 current = current.role == chrome.automation.RoleType.inlineTextBox ? |
| 157 current.parent() : current; |
| 158 current = cvox2.AutomationUtil.findNextNode(current, |
| 159 true, |
| 160 cvox2.AutomationPredicates.inlineTextBox); |
| 161 current = current ? current.parent() : current; |
| 162 break; |
| 163 } |
| 164 |
| 165 if (pred) |
| 166 current = cvox2.AutomationUtil.findNextNode(current, r, pred); |
| 167 |
| 168 if (current) |
| 169 current.focus(); |
| 170 |
| 171 this.onFocus({target: current || previous}); |
105 }, | 172 }, |
106 | 173 |
107 /** | 174 /** |
| 175 * Provides all feedback once ChromeVox's focus changes. |
| 176 * @param {Object} evt |
| 177 */ |
| 178 onFocus: function(evt) { |
| 179 var node = evt.target; |
| 180 if (!node) |
| 181 return; |
| 182 var container = node; |
| 183 while (container && (container.role == 'inlineTextBox' || |
| 184 container.role == 'staticText')) |
| 185 container = container.parent(); |
| 186 |
| 187 var role = container ? container.role : node.role; |
| 188 |
| 189 var output = |
| 190 [node.attributes.name, node.attributes.value, role].join(', '); |
| 191 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH); |
| 192 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); |
| 193 this.current_ = node; |
| 194 }, |
| 195 |
| 196 /** |
| 197 * Provides all feedback once a load complete event fires. |
| 198 * @param {Object} evt |
| 199 */ |
| 200 onLayoutOrLoadComplete: function(evt) { |
| 201 this.current_ = cvox2.AutomationUtil.findDeepestNode(evt.target, |
| 202 false, |
| 203 cvox2.AutomationPredicates.inlineTextBox); |
| 204 this.onFocus({target: this.current_}); |
| 205 }, |
| 206 |
| 207 /** |
108 * @private | 208 * @private |
109 * @param {string} url | 209 * @param {string} url |
110 * @return {boolean} Whether the given |url| is whitelisted. | 210 * @return {boolean} Whether the given |url| is whitelisted. |
111 */ | 211 */ |
112 isWhitelisted_: function(url) { | 212 isWhitelisted_: function(url) { |
113 return this.whitelist_.some(function(item) { | 213 return this.whitelist_.some(function(item) { |
114 return url.indexOf(item) != -1; | 214 return url.indexOf(item) != -1; |
115 }.bind(this)); | 215 }.bind(this)); |
116 }, | 216 }, |
117 | 217 |
118 /** | 218 /** |
119 * Disables classic ChromeVox. | 219 * Disables classic ChromeVox. |
120 * @param {number} tabId The tab where ChromeVox classic is running. | 220 * @param {number} tabId The tab where ChromeVox classic is running in. |
121 */ | 221 */ |
122 disableClassicChromeVox_: function(tabId) { | 222 disableClassicChromeVox_: function(tabId) { |
123 chrome.tabs.executeScript( | 223 chrome.tabs.executeScript( |
124 tabId, | 224 tabId, |
125 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', | 225 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', |
126 'allFrames': true}); | 226 'allFrames': true}); |
127 } | 227 } |
128 }; | 228 }; |
129 | 229 |
130 /** @type {cvox2.Background} */ | 230 /** @type {cvox2.Background} */ |
131 cvox2.global.backgroundObj = new cvox2.Background(); | 231 cvox2.global.backgroundObj = new cvox2.Background(); |
OLD | NEW |