| 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 // Only needed with unmerged ChromeVox classic loaded before. | 21 // Only needed with unmerged ChromeVox classic loaded before. |
| 22 cvox2.global.accessibility.setAccessibilityEnabled(false); | 22 cvox2.global.accessibility.setAccessibilityEnabled(false); |
| 23 chrome.automation.getDesktop(this.onGotDesktop.bind(this)); | 23 |
| 24 // Register listeners for ... |
| 25 // Desktop. |
| 26 chrome.automation.getDesktop(this.onGotTree.bind(this)); |
| 27 |
| 28 // Tabs. |
| 29 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)); |
| 24 }; | 33 }; |
| 25 | 34 |
| 26 cvox2.Background.prototype = { | 35 cvox2.Background.prototype = { |
| 27 /** | 36 /** |
| 28 * ID of the port used to communicate between content script and background | 37 * ID of the port used to communicate between content script and background |
| 29 * page. | 38 * page. |
| 30 * @const {string} | 39 * @const {string} |
| 31 */ | 40 */ |
| 32 PORT_ID: 'chromevox2', | 41 PORT_ID: 'chromevox2', |
| 33 | 42 |
| 34 /** | 43 /** |
| 35 * Waits until a desktop automation tree becomes available. | 44 * Handles chrome.extension.onConnect. |
| 36 * Thereafter, registers a simple exploration mode for the desktop tree. | 45 * @param {Object} port The port. |
| 37 * @param {AutomationTree} tree The desktop automation tree. | |
| 38 */ | 46 */ |
| 39 onGotDesktop: function(tree) { | 47 onConnect: function(port) { |
| 40 if (!tree.root) { | 48 if (port.name != this.PORT_ID) |
| 41 window.setTimeout(this.onGotDesktop, 500); | |
| 42 return; | 49 return; |
| 43 } | 50 port.onMessage.addListener(this.onMessage.bind(this)); |
| 44 chrome.extension.onConnect.addListener(function(port) { | 51 }, |
| 45 if (port.name != this.PORT_ID) | |
| 46 return; | |
| 47 var cur = tree.root; | |
| 48 port.onMessage.addListener(function(message) { | |
| 49 switch (message.keydown) { | |
| 50 case 37: | |
| 51 cur = cur.previousSibling() || cur; | |
| 52 break; | |
| 53 case 38: | |
| 54 cur = cur.parent() || cur; | |
| 55 break; | |
| 56 case 39: | |
| 57 cur = cur.nextSibling() || cur; | |
| 58 break; | |
| 59 case 40: | |
| 60 cur = cur.firstChild() || cur; | |
| 61 break; | |
| 62 } | |
| 63 var index = 1; | |
| 64 if (cur.parent()) | |
| 65 index = cur.parent().children().indexOf(cur) + 1; | |
| 66 var name = ''; | |
| 67 if (cur.attributes && cur.attributes['ax_attr_name']) | |
| 68 name = cur.attributes['ax_attr_name']; | |
| 69 var utterance = index + ' ' + name + cur.role; | |
| 70 chrome.tts.speak(String(utterance), {lang: 'en-US'}); | |
| 71 }); | |
| 72 }.bind(this)); | |
| 73 | 52 |
| 53 /** |
| 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. |
| 82 * @param {AutomationTree} tree The new automation tree. |
| 83 */ |
| 84 onGotTree: function(root) { |
| 74 // Register all automation event listeners. | 85 // Register all automation event listeners. |
| 75 tree.root.addEventListener('focus', this.onDesktopEvent.bind(this), true); | 86 root.addEventListener(chrome.automation.EventType.focus, |
| 87 this.onAutomationEvent.bind(this), |
| 88 true); |
| 76 }, | 89 }, |
| 77 | 90 |
| 78 /** | 91 /** |
| 79 * A generic handler for all desktop automation events. | 92 * A generic handler for all desktop automation events. |
| 80 * @param {AutomationEvent} evt The event. | 93 * @param {AutomationEvent} evt The event. |
| 81 */ | 94 */ |
| 82 onDesktopEvent: function(evt) { | 95 onAutomationEvent: function(evt) { |
| 83 var output = evt.target.attributes.name + ' ' + evt.target.role; | 96 var output = evt.target.attributes.name + ' ' + evt.target.role; |
| 84 cvox.ChromeVox.tts.speak(output); | 97 cvox.ChromeVox.tts.speak(output); |
| 85 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); | 98 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); |
| 86 } | 99 } |
| 87 }; | 100 }; |
| 88 | 101 |
| 89 /** @type {cvox2.Background} */ | 102 /** @type {cvox2.Background} */ |
| 90 cvox2.global.backgroundObj = new cvox2.Background(); | 103 cvox2.global.backgroundObj = new cvox2.Background(); |
| OLD | NEW |