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 |
| 33 // Manually bind all functions to |this|. |
| 34 for (var func in this) { |
| 35 if (typeof(this[func]) == 'function') |
| 36 this[func] = this[func].bind(this); |
| 37 } |
| 38 |
24 // Register listeners for ... | 39 // Register listeners for ... |
25 // Desktop. | 40 // Desktop. |
26 chrome.automation.getDesktop(this.onGotTree.bind(this)); | 41 chrome.automation.getDesktop(this.onGotTree); |
27 | 42 |
28 // Tabs. | 43 // Tabs. |
29 chrome.tabs.onUpdated.addListener(this.onTabUpdated.bind(this)); | 44 chrome.tabs.onUpdated.addListener(this.onTabUpdated); |
30 | |
31 // Keyboard events (currently Messages from content script). | |
32 chrome.extension.onConnect.addListener(this.onConnect.bind(this)); | |
33 }; | 45 }; |
34 | 46 |
35 cvox2.Background.prototype = { | 47 cvox2.Background.prototype = { |
36 /** | 48 /** |
37 * ID of the port used to communicate between content script and background | 49 * Handles chrome.tabs.onUpdated. |
38 * page. | 50 * @param {number} tabId |
39 * @const {string} | 51 * @param {Object} changeInfo |
40 */ | 52 */ |
41 PORT_ID: 'chromevox2', | 53 onTabUpdated: function(tabId, changeInfo) { |
| 54 chrome.tabs.get(tabId, function(tab) { |
| 55 if (!tab.url) |
| 56 return; |
42 | 57 |
43 /** | 58 if (!this.isWhitelisted_(tab.url)) { |
44 * Handles chrome.extension.onConnect. | 59 chrome.commands.onCommand.removeListener(this.onGotCommand); |
45 * @param {Object} port The port. | 60 return; |
46 */ | 61 } |
47 onConnect: function(port) { | 62 |
48 if (port.name != this.PORT_ID) | 63 if (!chrome.commands.onCommand.hasListeners()) { |
49 return; | 64 chrome.commands.onCommand.addListener(this.onGotCommand); |
50 port.onMessage.addListener(this.onMessage.bind(this)); | 65 } |
| 66 |
| 67 this.disableClassicChromeVox_(tab.id); |
| 68 |
| 69 chrome.automation.getTree(this.onGotTree.bind(this)); |
| 70 }.bind(this)); |
51 }, | 71 }, |
52 | 72 |
53 /** | 73 /** |
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. | 74 * Handles all setup once a new automation tree appears. |
82 * @param {AutomationTree} tree The new automation tree. | 75 * @param {AutomationTree} tree The new automation tree. |
83 */ | 76 */ |
84 onGotTree: function(root) { | 77 onGotTree: function(root) { |
85 // Register all automation event listeners. | 78 // Register all automation event listeners. |
86 root.addEventListener(chrome.automation.EventType.focus, | 79 root.addEventListener(chrome.automation.EventType.focus, |
87 this.onAutomationEvent.bind(this), | 80 this.onAutomationEvent.bind(this), |
88 true); | 81 true); |
89 }, | 82 }, |
90 | 83 |
91 /** | 84 /** |
92 * A generic handler for all desktop automation events. | 85 * A generic handler for all desktop automation events. |
93 * @param {AutomationEvent} evt The event. | 86 * @param {AutomationEvent} evt The event. |
94 */ | 87 */ |
95 onAutomationEvent: function(evt) { | 88 onAutomationEvent: function(evt) { |
96 var output = evt.target.attributes.name + ' ' + evt.target.role; | 89 var output = evt.target.attributes.name + ' ' + evt.target.role; |
97 cvox.ChromeVox.tts.speak(output); | 90 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH); |
98 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); | 91 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); |
| 92 }, |
| 93 |
| 94 /** |
| 95 * Handles chrome.commands.onCommand. |
| 96 * @param {string} command |
| 97 */ |
| 98 onGotCommand: function(command) { |
| 99 }, |
| 100 |
| 101 /** |
| 102 * @private |
| 103 * @param {string} url |
| 104 * @return {boolean} Whether the given |url| is whitelisted. |
| 105 */ |
| 106 isWhitelisted_: function(url) { |
| 107 return this.whitelist_.some(function(item) { |
| 108 return url.indexOf(item) != -1; |
| 109 }.bind(this)); |
| 110 }, |
| 111 |
| 112 /** |
| 113 * Disables classic ChromeVox. |
| 114 * @param {number} tabId The tab where ChromeVox classic is running. |
| 115 */ |
| 116 disableClassicChromeVox_: function(tabId) { |
| 117 chrome.tabs.executeScript( |
| 118 tabId, |
| 119 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', |
| 120 'allFrames': true}); |
99 } | 121 } |
100 }; | 122 }; |
101 | 123 |
102 /** @type {cvox2.Background} */ | 124 /** @type {cvox2.Background} */ |
103 cvox2.global.backgroundObj = new cvox2.Background(); | 125 cvox2.global.backgroundObj = new cvox2.Background(); |
OLD | NEW |