Chromium Code Reviews| 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.onLoadComplete, | |
| 99 true); | |
| 89 | 100 |
| 90 /** | 101 if (root.attributes.docLoaded) |
| 91 * A generic handler for all desktop automation events. | 102 this.onLoadComplete({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 }, | 103 }, |
| 99 | 104 |
| 100 /** | 105 /** |
| 101 * Handles chrome.commands.onCommand. | 106 * Handles chrome.commands.onCommand. |
| 102 * @param {string} command | 107 * @param {string} command |
| 103 */ | 108 */ |
| 104 onGotCommand: function(command) { | 109 onGotCommand: function(command) { |
| 110 if (!this.current_) | |
| 111 return; | |
| 112 | |
| 113 var previous = this.current_; | |
| 114 var current = this.current_; | |
| 115 | |
| 116 // Reverse? | |
| 117 var r = false; | |
| 118 var pred = null; | |
| 119 switch (command) { | |
| 120 case 'nextHeading': | |
| 121 r = false; | |
| 122 pred = cvox2.AutomationPredicates.heading; | |
| 123 break; | |
| 124 case 'previousHeading': | |
| 125 r = true; | |
| 126 pred = cvox2.AutomationPredicates.heading; | |
| 127 break; | |
| 128 case 'nextLine': | |
| 129 r = false; | |
| 130 pred = cvox2.AutomationPredicates.inlineTextBox; | |
| 131 break; | |
| 132 case 'previousLine': | |
| 133 r = true; | |
| 134 pred = cvox2.AutomationPredicates.inlineTextBox; | |
| 135 break; | |
| 136 case 'nextLink': | |
| 137 r = false; | |
| 138 pred = cvox2.AutomationPredicates.link; | |
| 139 break; | |
| 140 case 'previousLink': | |
| 141 r = true; | |
| 142 pred = cvox2.AutomationPredicates.link; | |
| 143 break; | |
| 144 case 'nextElement': | |
| 145 current = current.role == chrome.automation.RoleType.inlineTextBox ? | |
| 146 current.parent() : current; | |
| 147 current = cvox2.AutomationUtil.findNextNode(current, | |
| 148 false, | |
| 149 cvox2.AutomationPredicates.inlineTextBox); | |
| 150 current = current ? current.parent() : current; | |
| 151 break; | |
| 152 case 'previousElement': | |
| 153 current = current.role == chrome.automation.RoleType.inlineTextBox ? | |
| 154 current.parent() : current; | |
| 155 current = cvox2.AutomationUtil.findNextNode(current, | |
| 156 true, | |
| 157 cvox2.AutomationPredicates.inlineTextBox); | |
| 158 current = current ? current.parent() : current; | |
| 159 break; | |
| 160 } | |
| 161 | |
| 162 if (pred) | |
| 163 current = cvox2.AutomationUtil.findNextNode(current, r, pred); | |
| 164 | |
| 165 if (current) | |
| 166 current.focus(); | |
| 167 | |
| 168 this.onFocus({target: current || previous}); | |
| 105 }, | 169 }, |
| 106 | 170 |
| 107 /** | 171 /** |
| 172 * Provides all feedback once ChromeVox's focus changes. | |
| 173 * @param {Object} evt | |
|
dmazzoni
2014/09/29 06:06:18
Is there a specific automation api type rather tha
David Tseng
2014/09/29 17:05:23
See below.
| |
| 174 */ | |
| 175 onFocus: function(evt) { | |
| 176 var node = evt.target; | |
| 177 if (!node) | |
| 178 return; | |
| 179 var container = node; | |
| 180 while (container && (container.role == 'inlineTextBox' || | |
| 181 container.role == 'staticText')) | |
| 182 container = container.parent(); | |
| 183 | |
| 184 var role = container ? container.role : node.role; | |
| 185 | |
| 186 var output = | |
| 187 [node.attributes.name, node.attributes.value, role].join(', '); | |
| 188 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH); | |
| 189 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); | |
| 190 this.current_ = node; | |
| 191 }, | |
| 192 | |
| 193 /** | |
| 194 * Provides all feedback once a load complete event fires. | |
| 195 * @param {Object} evt | |
|
dmazzoni
2014/09/29 06:06:18
Is there a specific automation api type rather tha
David Tseng
2014/09/29 17:05:23
No; nothing defined on chrome.automation at least.
| |
| 196 */ | |
| 197 onLoadComplete: function(evt) { | |
| 198 this.current_ = cvox2.AutomationUtil.findNodePost(evt.target, | |
| 199 false, | |
| 200 cvox2.AutomationPredicates.inlineTextBox); | |
| 201 this.onFocus({target: this.current_}); | |
| 202 }, | |
| 203 | |
| 204 /** | |
| 108 * @private | 205 * @private |
| 109 * @param {string} url | 206 * @param {string} url |
| 110 * @return {boolean} Whether the given |url| is whitelisted. | 207 * @return {boolean} Whether the given |url| is whitelisted. |
| 111 */ | 208 */ |
| 112 isWhitelisted_: function(url) { | 209 isWhitelisted_: function(url) { |
| 113 return this.whitelist_.some(function(item) { | 210 return this.whitelist_.some(function(item) { |
| 114 return url.indexOf(item) != -1; | 211 return url.indexOf(item) != -1; |
| 115 }.bind(this)); | 212 }.bind(this)); |
| 116 }, | 213 }, |
| 117 | 214 |
| 118 /** | 215 /** |
| 119 * Disables classic ChromeVox. | 216 * Disables classic ChromeVox. |
| 120 * @param {number} tabId The tab where ChromeVox classic is running. | 217 * @param {number} tabId The tab where ChromeVox classic is running in. |
| 121 */ | 218 */ |
| 122 disableClassicChromeVox_: function(tabId) { | 219 disableClassicChromeVox_: function(tabId) { |
| 123 chrome.tabs.executeScript( | 220 chrome.tabs.executeScript( |
| 124 tabId, | 221 tabId, |
| 125 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', | 222 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', |
| 126 'allFrames': true}); | 223 'allFrames': true}); |
| 127 } | 224 } |
| 128 }; | 225 }; |
| 129 | 226 |
| 130 /** @type {cvox2.Background} */ | 227 /** @type {cvox2.Background} */ |
| 131 cvox2.global.backgroundObj = new cvox2.Background(); | 228 cvox2.global.backgroundObj = new cvox2.Background(); |
| OLD | NEW |