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 /** Classic Chrome accessibility API. */ | 13 /** Classic Chrome accessibility API. */ |
14 cvox2.global.accessibility = | 14 cvox2.global.accessibility = |
15 chrome.accessibilityPrivate || chrome.experimental.accessibility; | 15 chrome.accessibilityPrivate || chrome.experimental.accessibility; |
16 | 16 |
17 /** | 17 /** |
18 * ChromeVox2 background page. | 18 * ChromeVox2 background page. |
19 */ | 19 */ |
20 cvox2.Background = function() { | 20 cvox2.Background = function() { |
| 21 /** |
| 22 * A list of site substring patterns to use with ChromeVox next. Keep these |
| 23 * strings relatively specific. |
| 24 * @type {!Array.<string>} |
| 25 */ |
| 26 this.whitelist_ = ['http://www.chromevox.com/', 'chromevox_next_test']; |
| 27 |
21 // Only needed with unmerged ChromeVox classic loaded before. | 28 // Only needed with unmerged ChromeVox classic loaded before. |
| 29 // TODO(dtseng): Refactor all tabs handlers out of |
| 30 // accessibility_api_handler.js. |
22 cvox2.global.accessibility.setAccessibilityEnabled(false); | 31 cvox2.global.accessibility.setAccessibilityEnabled(false); |
23 | 32 |
24 // Register listeners for ... | 33 // Register listeners for ... |
25 // Desktop. | 34 // Desktop. |
26 chrome.automation.getDesktop(this.onGotTree.bind(this)); | 35 chrome.automation.getDesktop(this.onGotTree.bind(this)); |
27 | 36 |
28 // Tabs. | 37 // Tabs. |
29 chrome.tabs.onUpdated.addListener(this.onTabUpdated.bind(this)); | 38 chrome.tabs.onUpdated.addListener(this.onTabUpdated.bind(this)); |
30 | |
31 // Keyboard events (currently Messages from content script). | |
32 chrome.extension.onConnect.addListener(this.onConnect.bind(this)); | |
33 }; | 39 }; |
34 | 40 |
35 cvox2.Background.prototype = { | 41 cvox2.Background.prototype = { |
36 /** | 42 /** |
37 * ID of the port used to communicate between content script and background | 43 * Handles chrome.tabs.onUpdated. |
38 * page. | 44 * @param {number} tabId |
39 * @const {string} | 45 * @param {Object} changeInfo |
40 */ | 46 */ |
41 PORT_ID: 'chromevox2', | 47 onTabUpdated: function(tabId, changeInfo) { |
| 48 chrome.tabs.get(tabId, function(tab) { |
| 49 if (!tab.url || tab.status == 'loading') |
| 50 return; |
42 | 51 |
43 /** | 52 if (!this.isWhitelisted_(tab.url)) { |
44 * Handles chrome.extension.onConnect. | 53 chrome.commands.onCommand.removeListener(this.onGotCommand); |
45 * @param {Object} port The port. | 54 return; |
46 */ | 55 } |
47 onConnect: function(port) { | 56 |
48 if (port.name != this.PORT_ID) | 57 if (!chrome.commands.onCommand.hasListener(this.onGotCommand)) |
49 return; | 58 chrome.commands.onCommand.addListener(this.onGotCommand.bind(this)); |
50 port.onMessage.addListener(this.onMessage.bind(this)); | 59 |
| 60 this.disableClassicChromeVox_(tab.id); |
| 61 |
| 62 chrome.automation.getTree(this.onGotTree.bind(this)); |
| 63 }.bind(this)); |
51 }, | 64 }, |
52 | 65 |
53 /** | 66 /** |
54 * Dispatches messages to specific handlers. | |
55 * @param {Object} message The message. | |
56 */ | |
57 onMessage: function(message) { | |
58 if (message.keyDown) | |
59 this.onKeyDown(message); | |
60 }, | |
61 | |
62 /** | |
63 * Handles key down messages from the content script. | |
64 * @param {Object} message The key down message. | |
65 */ | |
66 onKeyDown: function(message) { | |
67 // TODO(dtseng): Implement. | |
68 }, | |
69 | |
70 /** | |
71 * Handles chrome.tabs.onUpdate. | |
72 * @param {number} tabId The tab id. | |
73 * @param {Object.<string, (string|boolean)>} changeInfo Information about | |
74 * the updated tab. | |
75 */ | |
76 onTabUpdated: function(tabId, changeInfo) { | |
77 chrome.automation.getTree(this.onGotTree.bind(this)); | |
78 }, | |
79 | |
80 /** | |
81 * Handles all setup once a new automation tree appears. | 67 * Handles all setup once a new automation tree appears. |
82 * @param {AutomationTree} tree The new automation tree. | 68 * @param {AutomationTree} tree The new automation tree. |
83 */ | 69 */ |
84 onGotTree: function(root) { | 70 onGotTree: function(root) { |
85 // Register all automation event listeners. | 71 // Register all automation event listeners. |
86 root.addEventListener(chrome.automation.EventType.focus, | 72 root.addEventListener(chrome.automation.EventType.focus, |
87 this.onAutomationEvent.bind(this), | 73 this.onAutomationEvent.bind(this), |
88 true); | 74 true); |
89 }, | 75 }, |
90 | 76 |
91 /** | 77 /** |
92 * A generic handler for all desktop automation events. | 78 * A generic handler for all desktop automation events. |
93 * @param {AutomationEvent} evt The event. | 79 * @param {AutomationEvent} evt The event. |
94 */ | 80 */ |
95 onAutomationEvent: function(evt) { | 81 onAutomationEvent: function(evt) { |
96 var output = evt.target.attributes.name + ' ' + evt.target.role; | 82 var output = evt.target.attributes.name + ' ' + evt.target.role; |
97 cvox.ChromeVox.tts.speak(output); | 83 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH); |
98 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); | 84 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); |
| 85 }, |
| 86 |
| 87 /** |
| 88 * Handles chrome.commands.onCommand. |
| 89 * @param {string} command |
| 90 */ |
| 91 onGotCommand: function(command) { |
| 92 }, |
| 93 |
| 94 /** |
| 95 * @private |
| 96 * @param {string} url |
| 97 * @return {boolean} Whether the given |url| is whitelisted. |
| 98 */ |
| 99 isWhitelisted_: function(url) { |
| 100 return this.whitelist_.some(function(item) { |
| 101 return url.indexOf(item) != -1; |
| 102 }); |
| 103 }, |
| 104 |
| 105 /** |
| 106 * Disables classic ChromeVox. |
| 107 * @param {number} tabId The tab where ChromeVox classic is running. |
| 108 */ |
| 109 disableClassicChromeVox_: function(tabId) { |
| 110 chrome.tabs.executeScript( |
| 111 tabId, |
| 112 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', |
| 113 'allFrames': true}); |
| 114 |
99 } | 115 } |
100 }; | 116 }; |
101 | 117 |
102 /** @type {cvox2.Background} */ | 118 /** @type {cvox2.Background} */ |
103 cvox2.global.backgroundObj = new cvox2.Background(); | 119 cvox2.global.backgroundObj = new cvox2.Background(); |
OLD | NEW |