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 /** 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 sites to use with ChromeVox next. | |
|
Peter Lundblad
2014/09/05 07:21:54
Define more precisely what the string should be.
David Tseng
2014/09/08 19:24:34
Done.
| |
| 23 * @type {!Array.<string>} | |
| 24 */ | |
| 25 this.whitelist_ = ['http://www.chromevox.com/', 'chromevox_next']; | |
|
Peter Lundblad
2014/09/05 07:21:54
What site is 'chromevox_next'?
David Tseng
2014/09/08 19:24:34
See new comments. Matches substrings now, so chrom
| |
| 26 | |
| 21 // Only needed with unmerged ChromeVox classic loaded before. | 27 // Only needed with unmerged ChromeVox classic loaded before. |
| 28 // TODO(dtseng): Refactor all tabs handlers out of | |
| 29 //accessibility_api_handler.js. | |
|
Peter Lundblad
2014/09/05 07:21:54
nit: space after //
David Tseng
2014/09/08 19:24:34
Done.
| |
| 22 cvox2.global.accessibility.setAccessibilityEnabled(false); | 30 cvox2.global.accessibility.setAccessibilityEnabled(false); |
| 23 | 31 |
| 24 // Register listeners for ... | 32 // Register listeners for ... |
| 25 // Desktop. | 33 // Desktop. |
| 26 chrome.automation.getDesktop(this.onGotTree.bind(this)); | 34 chrome.automation.getDesktop(this.onGotTree.bind(this)); |
| 27 | 35 |
| 28 // Tabs. | 36 // Tabs. |
| 29 chrome.tabs.onUpdated.addListener(this.onTabUpdated.bind(this)); | 37 chrome.tabs.onActivated.addListener(this.onTabActivated.bind(this)); |
| 30 | |
| 31 // Keyboard events (currently Messages from content script). | |
| 32 chrome.extension.onConnect.addListener(this.onConnect.bind(this)); | |
| 33 }; | 38 }; |
| 34 | 39 |
| 35 cvox2.Background.prototype = { | 40 cvox2.Background.prototype = { |
| 36 /** | |
| 37 * ID of the port used to communicate between content script and background | |
| 38 * page. | |
| 39 * @const {string} | |
| 40 */ | |
| 41 PORT_ID: 'chromevox2', | |
| 42 | 41 |
| 43 /** | 42 /** |
|
Peter Lundblad
2014/09/05 07:21:54
nit: indent
David Tseng
2014/09/08 19:24:34
Done.
| |
| 44 * Handles chrome.extension.onConnect. | 43 * Handles chrome.tabs.onActivated. |
| 45 * @param {Object} port The port. | 44 * @param {number} tabId |
| 46 */ | 45 * @param {Object} activatedInfo |
| 47 onConnect: function(port) { | 46 */ |
| 48 if (port.name != this.PORT_ID) | 47 onTabActivated: function(activatedInfo) { |
|
Peter Lundblad
2014/09/05 07:21:54
Does this logic work in web content that are not t
David Tseng
2014/09/08 19:24:34
Nope; but it should still fall back to classic Chr
| |
| 49 return; | 48 chrome.tabs.get(activatedInfo.tabId, function(tab) { |
| 50 port.onMessage.addListener(this.onMessage.bind(this)); | 49 if (!tab.url) |
| 50 return; | |
| 51 | |
| 52 if (!tab.active || !this.isWhitelisted_(tab.url)) { | |
| 53 chrome.commands.onCommand.removeListener(this.onGotCommand); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 if (!chrome.commands.onCommand.hasListener(this.onGotCommand)) | |
| 58 chrome.commands.onCommand.addListener(this.onGotCommand.bind(this)); | |
|
Peter Lundblad
2014/09/05 07:21:54
nit: indent
David Tseng
2014/09/08 19:24:34
Done.
| |
| 59 | |
| 60 chrome.tabs.executeScript( | |
| 61 tab.id, | |
| 62 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n', | |
| 63 'allFrames': true}); | |
| 64 | |
| 65 chrome.automation.getTree(this.onGotTree.bind(this)); | |
| 66 }.bind(this)); | |
| 51 }, | 67 }, |
| 52 | 68 |
| 53 /** | 69 /** |
| 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. | 70 * Handles all setup once a new automation tree appears. |
| 82 * @param {AutomationTree} tree The new automation tree. | 71 * @param {AutomationTree} tree The new automation tree. |
| 83 */ | 72 */ |
| 84 onGotTree: function(root) { | 73 onGotTree: function(root) { |
| 85 // Register all automation event listeners. | 74 // Register all automation event listeners. |
| 86 root.addEventListener(chrome.automation.EventType.focus, | 75 root.addEventListener(chrome.automation.EventType.focus, |
| 87 this.onAutomationEvent.bind(this), | 76 this.onAutomationEvent.bind(this), |
| 88 true); | 77 true); |
| 89 }, | 78 }, |
| 90 | 79 |
| 91 /** | 80 /** |
| 92 * A generic handler for all desktop automation events. | 81 * A generic handler for all desktop automation events. |
| 93 * @param {AutomationEvent} evt The event. | 82 * @param {AutomationEvent} evt The event. |
| 94 */ | 83 */ |
| 95 onAutomationEvent: function(evt) { | 84 onAutomationEvent: function(evt) { |
| 96 var output = evt.target.attributes.name + ' ' + evt.target.role; | 85 var output = evt.target.attributes.name + ' ' + evt.target.role; |
| 97 cvox.ChromeVox.tts.speak(output); | 86 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH); |
| 98 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); | 87 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output)); |
| 88 }, | |
| 89 | |
| 90 /** | |
| 91 * Handles chrome.commands.onCommand. | |
| 92 * @param {string} command | |
| 93 */ | |
| 94 onGotCommand: function(command) { | |
| 95 }, | |
| 96 | |
| 97 /** | |
| 98 * @private | |
| 99 * @param {string} url | |
| 100 * @return {boolean} Whether the given |url| is whitelisted. | |
|
Peter Lundblad
2014/09/05 07:21:54
nit: indent
David Tseng
2014/09/08 19:24:34
Done.
| |
| 101 */ | |
| 102 isWhitelisted_: function(url) { | |
| 103 return this.whitelist_.some(function(item) { | |
| 104 return url.indexOf(item) != -1; | |
| 105 }); | |
| 99 } | 106 } |
| 100 }; | 107 }; |
| 101 | 108 |
| 102 /** @type {cvox2.Background} */ | 109 /** @type {cvox2.Background} */ |
| 103 cvox2.global.backgroundObj = new cvox2.Background(); | 110 cvox2.global.backgroundObj = new cvox2.Background(); |
| OLD | NEW |