OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Script that runs on the background page. |
| 7 */ |
| 8 |
| 9 goog.provide('cvox.ChromeVoxBackground'); |
| 10 |
| 11 goog.require('cvox.BuildConfig'); |
| 12 |
| 13 goog.require('cvox.ChromeVoxAccessibilityApiHandler'); |
| 14 goog.require('cvox.ChromeVoxChromeOsTtsEngine'); |
| 15 goog.require('cvox.ChromeVoxEarcons'); |
| 16 goog.require('cvox.ChromeVoxEmacspeakTtsServerEngine'); |
| 17 goog.require('cvox.ChromeVoxExtensionTtsEngine'); |
| 18 goog.require('cvox.ChromeVoxFlashGoossEngine'); |
| 19 goog.require('cvox.ChromeVoxGoossEngine'); |
| 20 goog.require('cvox.ChromeVoxLocalTtsServerEngine'); |
| 21 goog.require('cvox.ExtensionBridge'); |
| 22 goog.require('cvox.LocalEarconsManager'); |
| 23 goog.require('cvox.LocalTtsManager'); |
| 24 |
| 25 /** |
| 26 * This object manages the global and persistent state for ChromeVox. |
| 27 * It listens for messages from the content scripts on pages and |
| 28 * interprets them. |
| 29 * @constructor |
| 30 */ |
| 31 cvox.ChromeVoxBackground = function() { |
| 32 }; |
| 33 |
| 34 /** |
| 35 * Initialize the background page: set up TTS and bridge listeners. |
| 36 */ |
| 37 cvox.ChromeVoxBackground.prototype.init = function() { |
| 38 // TODO (clchen): Implement the options page - but make sure the default |
| 39 // is set on the background page, because the options page doesn't actually |
| 40 // load unless the user opens it. |
| 41 localStorage['keyBindings'] = JSON.stringify({ |
| 42 // Stop TTS |
| 43 '#17' : 'stopSpeech', // Ctrl |
| 44 |
| 45 // TAB/Shift+TAB |
| 46 '#9' : 'handleTab', // Tab |
| 47 'Shift+#9' : 'handleTab', |
| 48 |
| 49 // Basic navigation |
| 50 'Ctrl+Alt+#38' : 'backward', |
| 51 'Ctrl+Alt+#40' : 'forward', |
| 52 'Ctrl+Alt+#37' : 'previousGranularity', |
| 53 'Ctrl+Alt+#39' : 'nextGranularity', |
| 54 '#13' : 'actOnCurrentItem', // ENTER |
| 55 |
| 56 // General commands |
| 57 'Ctrl+Alt+#191' : 'toggleSearchWidget', // '/' |
| 58 'Ctrl+Alt+B' : 'showBookmarkManager', |
| 59 'Ctrl+Alt+A' : 'nextTtsEngine', |
| 60 'Ctrl+Alt+#189' : 'decreaseTtsRate', // '-' |
| 61 'Ctrl+Alt+#187' : 'increaseTtsRate', // '=' |
| 62 'Ctrl+Alt+Shift+#189' : 'decreaseTtsPitch', // '-' |
| 63 'Ctrl+Alt+Shift+#187' : 'increaseTtsPitch', // '=' |
| 64 'Ctrl+Alt+#219' : 'decreaseTtsVolume', // '[' |
| 65 'Ctrl+Alt+#221' : 'increaseTtsVolume', // ']' |
| 66 |
| 67 // Jump commands |
| 68 'Ctrl+Alt+1' : 'nextHeading1', |
| 69 'Ctrl+Alt+Shift+1' : 'previousHeading1', |
| 70 'Ctrl+Alt+2' : 'nextHeading2', |
| 71 'Ctrl+Alt+Shift+2' : 'previousHeading2', |
| 72 'Ctrl+Alt+3' : 'nextHeading3', |
| 73 'Ctrl+Alt+Shift+3' : 'previousHeading3', |
| 74 'Ctrl+Alt+4' : 'nextHeading4', |
| 75 'Ctrl+Alt+Shift+4' : 'previousHeading4', |
| 76 'Ctrl+Alt+5' : 'nextHeading5', |
| 77 'Ctrl+Alt+Shift+5' : 'previousHeading5', |
| 78 'Ctrl+Alt+6' : 'nextHeading6', |
| 79 'Ctrl+Alt+Shift+6' : 'previousHeading6', |
| 80 'Ctrl+Alt+C' : 'nextComboBox', |
| 81 'Ctrl+Alt+Shift+C' : 'previousComboBox', |
| 82 'Ctrl+Alt+E' : 'nextEditText', |
| 83 'Ctrl+Alt+Shift+E' : 'previousEditText', |
| 84 'Ctrl+Alt+F' : 'nextFormField', |
| 85 'Ctrl+Alt+Shift+F' : 'previousFormField', |
| 86 'Ctrl+Alt+G' : 'nextGraphic', |
| 87 'Ctrl+Alt+Shift+G' : 'previousGraphic', |
| 88 'Ctrl+Alt+H' : 'nextHeading', |
| 89 'Ctrl+Alt+Shift+H' : 'previousHeading', |
| 90 'Ctrl+Alt+I' : 'nextListItem', |
| 91 'Ctrl+Alt+Shift+I' : 'previousListItem', |
| 92 'Ctrl+Alt+L' : 'nextLink', |
| 93 'Ctrl+Alt+Shift+L' : 'previousLink', |
| 94 'Ctrl+Alt+N' : 'nextNotLink', |
| 95 'Ctrl+Alt+Shift+N' : 'previousNotLink', |
| 96 'Ctrl+Alt+O' : 'nextList', |
| 97 'Ctrl+Alt+Shift+O' : 'previousList', |
| 98 'Ctrl+Alt+Q' : 'nextBlockquote', |
| 99 'Ctrl+Alt+Shift+Q' : 'previousBlockquote', |
| 100 'Ctrl+Alt+R' : 'nextRadio', |
| 101 'Ctrl+Alt+Shift+R' : 'previousRadio', |
| 102 'Ctrl+Alt+S' : 'nextSlider', |
| 103 'Ctrl+Alt+Shift+S' : 'previousSlider', |
| 104 'Ctrl+Alt+T' : 'nextTable', |
| 105 'Ctrl+Alt+Shift+T' : 'previousTable', |
| 106 'Ctrl+Alt+U' : 'nextButton', |
| 107 'Ctrl+Alt+Shift+U' : 'previousButton', |
| 108 'Ctrl+Alt+X' : 'nextCheckbox', |
| 109 'Ctrl+Alt+Shift+X' : 'previousCheckbox' |
| 110 }); |
| 111 |
| 112 this.ttsManager = this.createTtsManager(); |
| 113 this.earconsManager = this.createEarconsManager(this.ttsManager); |
| 114 this.addBridgeListener(); |
| 115 |
| 116 cvox.ChromeVoxAccessibilityApiHandler.init(this.ttsManager, |
| 117 this.earconsManager); |
| 118 }; |
| 119 |
| 120 /** |
| 121 * @return {cvox.AbstractTtsManager} New initialized TTS manager. |
| 122 */ |
| 123 cvox.ChromeVoxBackground.prototype.createTtsManager = function() { |
| 124 var ttsEngines = [cvox.ChromeVoxFlashGoossEngine, |
| 125 cvox.ChromeVoxChromeOsTtsEngine, |
| 126 cvox.ChromeVoxGoossEngine, |
| 127 cvox.ChromeVoxEmacspeakTtsServerEngine]; |
| 128 return new cvox.LocalTtsManager(ttsEngines, null); |
| 129 }; |
| 130 |
| 131 /** |
| 132 * @param {cvox.AbstractTtsManager} ttsManager A TTS Manager. |
| 133 * @return {Object} New initialized earcons manager. |
| 134 */ |
| 135 cvox.ChromeVoxBackground.prototype.createEarconsManager = function(ttsManager) { |
| 136 return new cvox.LocalEarconsManager([cvox.ChromeVoxEarcons], ttsManager); |
| 137 }; |
| 138 |
| 139 /** |
| 140 * Send all of the settings to all active tabs. |
| 141 */ |
| 142 cvox.ChromeVoxBackground.prototype.sendSettingsToActiveTab = function() { |
| 143 cvox.ExtensionBridge.send({'keyBindings': |
| 144 JSON.parse(localStorage['keyBindings'])}); |
| 145 }; |
| 146 |
| 147 /** |
| 148 * Called when a TTS message is received from a page content script. |
| 149 * @param {Object} msg The TTS message. |
| 150 */ |
| 151 cvox.ChromeVoxBackground.prototype.onTtsMessage = function(msg) { |
| 152 if (msg.action == 'speak') { |
| 153 this.ttsManager.speak(msg.text, msg.queueMode, msg.properties); |
| 154 } else if (msg.action == 'stop') { |
| 155 this.ttsManager.stop(); |
| 156 } else if (msg.action == 'nextEngine') { |
| 157 this.ttsManager.nextTtsEngine(true); |
| 158 } else if (msg.action == 'increaseRate') { |
| 159 this.ttsManager.increaseProperty( |
| 160 cvox.AbstractTtsManager.TTS_PROPERTY_RATE); |
| 161 } else if (msg.action == 'decreaseRate') { |
| 162 this.ttsManager.decreaseProperty( |
| 163 cvox.AbstractTtsManager.TTS_PROPERTY_RATE); |
| 164 } else if (msg.action == 'increasePitch') { |
| 165 this.ttsManager.increaseProperty( |
| 166 cvox.AbstractTtsManager.TTS_PROPERTY_PITCH); |
| 167 } else if (msg.action == 'decreasePitch') { |
| 168 this.ttsManager.decreaseProperty( |
| 169 cvox.AbstractTtsManager.TTS_PROPERTY_PITCH); |
| 170 } else if (msg.action == 'increaseVolume') { |
| 171 this.ttsManager.increaseProperty( |
| 172 cvox.AbstractTtsManager.TTS_PROPERTY_VOLUME); |
| 173 } else if (msg.action == 'decreaseVolume') { |
| 174 this.ttsManager.decreaseProperty( |
| 175 cvox.AbstractTtsManager.TTS_PROPERTY_VOLUME); |
| 176 } |
| 177 }; |
| 178 |
| 179 /** |
| 180 * Called when an earcon message is received from a page content script. |
| 181 * @param {Object} msg The earcon message. |
| 182 */ |
| 183 cvox.ChromeVoxBackground.prototype.onEarconMessage = function(msg) { |
| 184 if (msg.action == 'play') { |
| 185 this.earconsManager.playEarcon(msg.earcon); |
| 186 } else if (msg.action == 'nextEacons') { |
| 187 this.earconsManager.nextEarcons(); |
| 188 } |
| 189 }; |
| 190 |
| 191 /** |
| 192 * Listen for connections from our content script bridges, and dispatch the |
| 193 * messages to the proper destination. |
| 194 */ |
| 195 cvox.ChromeVoxBackground.prototype.addBridgeListener = function() { |
| 196 var context = this; |
| 197 cvox.ExtensionBridge.addMessageListener(function(msg) { |
| 198 if (msg.target == 'BookmarkManager') { |
| 199 var createDataObj = new Object(); |
| 200 createDataObj.url = |
| 201 'chromevox/background/bookmark_manager.html'; |
| 202 chrome.windows.create(createDataObj); |
| 203 } else if (msg.target == 'Options') { |
| 204 if (msg.action == 'getSettings') { |
| 205 context.sendSettingsToActiveTab(); |
| 206 } else if (msg.action == 'open') { |
| 207 console.log('Options open called'); |
| 208 var optionsPage = new Object(); |
| 209 optionsPage.url = 'options.html'; |
| 210 chrome.windows.create(optionsPage); |
| 211 } |
| 212 } else if (msg.target == 'TTS') { |
| 213 context.onTtsMessage(msg); |
| 214 } else if (msg.target == 'EARCON') { |
| 215 context.onEarconMessage(msg); |
| 216 } |
| 217 }); |
| 218 }; |
| 219 |
| 220 var background = new cvox.ChromeVoxBackground(); |
| 221 background.init(); |
OLD | NEW |