| 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 Script that runs on the background page. | 6 * @fileoverview Script that runs on the background page. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 goog.provide('cvox.ChromeVoxBackground'); | 9 goog.provide('cvox.ChromeVoxBackground'); |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 goog.require('cvox.TabsApiHandler'); | 28 goog.require('cvox.TabsApiHandler'); |
| 29 goog.require('cvox.TtsBackground'); | 29 goog.require('cvox.TtsBackground'); |
| 30 | 30 |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * This object manages the global and persistent state for ChromeVox. | 33 * This object manages the global and persistent state for ChromeVox. |
| 34 * It listens for messages from the content scripts on pages and | 34 * It listens for messages from the content scripts on pages and |
| 35 * interprets them. | 35 * interprets them. |
| 36 * @constructor | 36 * @constructor |
| 37 */ | 37 */ |
| 38 cvox.ChromeVoxBackground = function() { | 38 cvox.ChromeVoxBackground = function() {}; |
| 39 }; | |
| 40 | 39 |
| 41 | 40 |
| 42 /** | 41 /** |
| 43 * @param {string} pref | 42 * @param {string} pref |
| 44 * @param {*} value | 43 * @param {*} value |
| 45 * @param {boolean} announce | 44 * @param {boolean} announce |
| 46 */ | 45 */ |
| 47 cvox.ChromeVoxBackground.setPref = function(pref, value, announce) { | 46 cvox.ChromeVoxBackground.setPref = function(pref, value, announce) { |
| 48 if (pref == 'active' && value != cvox.ChromeVox.isActive) { | 47 if (pref == 'active' && value != cvox.ChromeVox.isActive) { |
| 49 if (cvox.ChromeVox.isActive) { | 48 if (cvox.ChromeVox.isActive) { |
| 50 cvox.ChromeVox.tts.speak(Msgs.getMsg('chromevox_inactive'), | 49 cvox.ChromeVox.tts.speak( |
| 51 cvox.QueueMode.FLUSH); | 50 Msgs.getMsg('chromevox_inactive'), cvox.QueueMode.FLUSH); |
| 52 chrome.accessibilityPrivate.setNativeAccessibilityEnabled(true); | 51 chrome.accessibilityPrivate.setNativeAccessibilityEnabled(true); |
| 53 } else { | 52 } else { |
| 54 chrome.accessibilityPrivate.setNativeAccessibilityEnabled(false); | 53 chrome.accessibilityPrivate.setNativeAccessibilityEnabled(false); |
| 55 } | 54 } |
| 56 } else if (pref == 'earcons') { | 55 } else if (pref == 'earcons') { |
| 57 cvox.AbstractEarcons.enabled = !!value; | 56 cvox.AbstractEarcons.enabled = !!value; |
| 58 } else if (pref == 'sticky' && announce) { | 57 } else if (pref == 'sticky' && announce) { |
| 59 if (value) { | 58 if (value) { |
| 60 cvox.ChromeVox.tts.speak(Msgs.getMsg('sticky_mode_enabled'), | 59 cvox.ChromeVox.tts.speak( |
| 61 cvox.QueueMode.FLUSH); | 60 Msgs.getMsg('sticky_mode_enabled'), cvox.QueueMode.FLUSH); |
| 62 } else { | 61 } else { |
| 63 cvox.ChromeVox.tts.speak( | 62 cvox.ChromeVox.tts.speak( |
| 64 Msgs.getMsg('sticky_mode_disabled'), | 63 Msgs.getMsg('sticky_mode_disabled'), cvox.QueueMode.FLUSH); |
| 65 cvox.QueueMode.FLUSH); | |
| 66 } | 64 } |
| 67 } else if (pref == 'typingEcho' && announce) { | 65 } else if (pref == 'typingEcho' && announce) { |
| 68 var announceStr = ''; | 66 var announceStr = ''; |
| 69 switch (value) { | 67 switch (value) { |
| 70 case cvox.TypingEcho.CHARACTER: | 68 case cvox.TypingEcho.CHARACTER: |
| 71 announceStr = Msgs.getMsg('character_echo'); | 69 announceStr = Msgs.getMsg('character_echo'); |
| 72 break; | 70 break; |
| 73 case cvox.TypingEcho.WORD: | 71 case cvox.TypingEcho.WORD: |
| 74 announceStr = Msgs.getMsg('word_echo'); | 72 announceStr = Msgs.getMsg('word_echo'); |
| 75 break; | 73 break; |
| 76 case cvox.TypingEcho.CHARACTER_AND_WORD: | 74 case cvox.TypingEcho.CHARACTER_AND_WORD: |
| 77 announceStr = Msgs.getMsg('character_and_word_echo'); | 75 announceStr = Msgs.getMsg('character_and_word_echo'); |
| 78 break; | 76 break; |
| 79 case cvox.TypingEcho.NONE: | 77 case cvox.TypingEcho.NONE: |
| 80 announceStr = Msgs.getMsg('none_echo'); | 78 announceStr = Msgs.getMsg('none_echo'); |
| 81 break; | 79 break; |
| 82 default: | 80 default: |
| 83 break; | 81 break; |
| 84 } | 82 } |
| 85 if (announceStr) { | 83 if (announceStr) { |
| 86 cvox.ChromeVox.tts.speak(announceStr, cvox.QueueMode.QUEUE); | 84 cvox.ChromeVox.tts.speak(announceStr, cvox.QueueMode.QUEUE); |
| 87 } | 85 } |
| 88 } else if (pref == 'brailleCaptions') { | 86 } else if (pref == 'brailleCaptions') { |
| 89 cvox.BrailleCaptionsBackground.setActive(!!value); | 87 cvox.BrailleCaptionsBackground.setActive(!!value); |
| 90 } else if (pref == 'position') { | 88 } else if (pref == 'position') { |
| 91 cvox.ChromeVox.position = | 89 cvox.ChromeVox.position = |
| 92 /** @type {Object<string, cvox.Point>} */(JSON.parse( | 90 /** @type {Object<string, cvox.Point>} */ (JSON.parse( |
| 93 /** @type {string} */(value))); | 91 /** @type {string} */ (value))); |
| 94 } | 92 } |
| 95 window['prefs'].setPref(pref, value); | 93 window['prefs'].setPref(pref, value); |
| 96 cvox.ChromeVoxBackground.readPrefs(); | 94 cvox.ChromeVoxBackground.readPrefs(); |
| 97 }; | 95 }; |
| 98 | 96 |
| 99 | 97 |
| 100 /** | 98 /** |
| 101 * Read and apply preferences that affect the background context. | 99 * Read and apply preferences that affect the background context. |
| 102 */ | 100 */ |
| 103 cvox.ChromeVoxBackground.readPrefs = function() { | 101 cvox.ChromeVoxBackground.readPrefs = function() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 /** | 134 /** |
| 137 * Chrome's actual TTS which knows and cares about pitch, volume, etc. | 135 * Chrome's actual TTS which knows and cares about pitch, volume, etc. |
| 138 * @type {cvox.TtsBackground} | 136 * @type {cvox.TtsBackground} |
| 139 * @private | 137 * @private |
| 140 */ | 138 */ |
| 141 this.backgroundTts_ = new cvox.TtsBackground(); | 139 this.backgroundTts_ = new cvox.TtsBackground(); |
| 142 | 140 |
| 143 /** | 141 /** |
| 144 * @type {cvox.TtsInterface} | 142 * @type {cvox.TtsInterface} |
| 145 */ | 143 */ |
| 146 this.tts = new cvox.CompositeTts() | 144 this.tts = new cvox.CompositeTts().add(this.backgroundTts_).add(consoleTts); |
| 147 .add(this.backgroundTts_) | |
| 148 .add(consoleTts); | |
| 149 | 145 |
| 150 this.addBridgeListener(); | 146 this.addBridgeListener(); |
| 151 | 147 |
| 152 /** | 148 /** |
| 153 * The actual Braille service. | 149 * The actual Braille service. |
| 154 * @type {cvox.BrailleBackground} | 150 * @type {cvox.BrailleBackground} |
| 155 * @private | 151 * @private |
| 156 */ | 152 */ |
| 157 this.backgroundBraille_ = cvox.BrailleBackground.getInstance(); | 153 this.backgroundBraille_ = cvox.BrailleBackground.getInstance(); |
| 158 | 154 |
| 159 this.tabsApiHandler_ = new cvox.TabsApiHandler(); | 155 this.tabsApiHandler_ = new cvox.TabsApiHandler(); |
| 160 | 156 |
| 161 // Export globals on cvox.ChromeVox. | 157 // Export globals on cvox.ChromeVox. |
| 162 cvox.ChromeVox.tts = this.tts; | 158 cvox.ChromeVox.tts = this.tts; |
| 163 cvox.ChromeVox.braille = this.backgroundBraille_; | 159 cvox.ChromeVox.braille = this.backgroundBraille_; |
| 164 | 160 |
| 165 if (!cvox.ChromeVox.earcons) | 161 if (!cvox.ChromeVox.earcons) |
| 166 cvox.ChromeVox.earcons = new cvox.ClassicEarcons(); | 162 cvox.ChromeVox.earcons = new cvox.ClassicEarcons(); |
| 167 | 163 |
| 168 if (cvox.ChromeVox.isChromeOS) { | 164 if (cvox.ChromeVox.isChromeOS) { |
| 169 chrome.accessibilityPrivate.onIntroduceChromeVox.addListener( | 165 chrome.accessibilityPrivate.onIntroduceChromeVox.addListener( |
| 170 this.onIntroduceChromeVox); | 166 this.onIntroduceChromeVox); |
| 171 } | 167 } |
| 172 | 168 |
| 173 // Set up a message passing system for goog.provide() calls from | 169 // Set up a message passing system for goog.provide() calls from |
| 174 // within the content scripts. | 170 // within the content scripts. |
| 175 chrome.extension.onMessage.addListener( | 171 chrome.extension.onMessage.addListener(function(request, sender, callback) { |
| 176 function(request, sender, callback) { | 172 if (request['srcFile']) { |
| 177 if (request['srcFile']) { | 173 var srcFile = request['srcFile']; |
| 178 var srcFile = request['srcFile']; | 174 cvox.InjectedScriptLoader.fetchCode([srcFile], function(code) { |
| 179 cvox.InjectedScriptLoader.fetchCode( | 175 callback({'code': code[srcFile]}); |
| 180 [srcFile], | |
| 181 function(code) { | |
| 182 callback({'code': code[srcFile]}); | |
| 183 }); | |
| 184 } | |
| 185 return true; | |
| 186 }); | 176 }); |
| 177 } |
| 178 return true; |
| 179 }); |
| 187 | 180 |
| 188 var self = this; | 181 var self = this; |
| 189 | 182 |
| 190 // Inject the content script into all running tabs. | 183 // Inject the content script into all running tabs. |
| 191 chrome.windows.getAll({'populate': true}, function(windows) { | 184 chrome.windows.getAll({'populate': true}, function(windows) { |
| 192 for (var i = 0; i < windows.length; i++) { | 185 for (var i = 0; i < windows.length; i++) { |
| 193 var tabs = windows[i].tabs; | 186 var tabs = windows[i].tabs; |
| 194 self.injectChromeVoxIntoTabs(tabs); | 187 self.injectChromeVoxIntoTabs(tabs); |
| 195 } | 188 } |
| 196 }); | 189 }); |
| 197 | 190 |
| 198 if (localStorage['active'] == 'false') { | 191 if (localStorage['active'] == 'false') { |
| 199 // Warn the user when the browser first starts if ChromeVox is inactive. | 192 // Warn the user when the browser first starts if ChromeVox is inactive. |
| 200 this.tts.speak(Msgs.getMsg('chromevox_inactive'), | 193 this.tts.speak(Msgs.getMsg('chromevox_inactive'), cvox.QueueMode.QUEUE); |
| 201 cvox.QueueMode.QUEUE); | |
| 202 } else if (cvox.PlatformUtil.matchesPlatform(cvox.PlatformFilter.WML)) { | 194 } else if (cvox.PlatformUtil.matchesPlatform(cvox.PlatformFilter.WML)) { |
| 203 // Introductory message. | 195 // Introductory message. |
| 204 this.tts.speak(Msgs.getMsg('chromevox_intro'), | 196 this.tts.speak(Msgs.getMsg('chromevox_intro'), cvox.QueueMode.QUEUE); |
| 205 cvox.QueueMode.QUEUE); | 197 cvox.ChromeVox.braille.write( |
| 206 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText( | 198 cvox.NavBraille.fromText(Msgs.getMsg('intro_brl'))); |
| 207 Msgs.getMsg('intro_brl'))); | |
| 208 } | 199 } |
| 209 }; | 200 }; |
| 210 | 201 |
| 211 | 202 |
| 212 /** | 203 /** |
| 213 * Inject ChromeVox into a tab. | 204 * Inject ChromeVox into a tab. |
| 214 * @param {Array<Tab>} tabs The tab where ChromeVox scripts should be injected. | 205 * @param {Array<Tab>} tabs The tab where ChromeVox scripts should be injected. |
| 215 */ | 206 */ |
| 216 cvox.ChromeVoxBackground.prototype.injectChromeVoxIntoTabs = function(tabs) { | 207 cvox.ChromeVoxBackground.prototype.injectChromeVoxIntoTabs = function(tabs) { |
| 217 var listOfFiles; | 208 var listOfFiles; |
| 218 | 209 |
| 219 // These lists of files must match the content_scripts section in | 210 // These lists of files must match the content_scripts section in |
| 220 // the manifest files. | 211 // the manifest files. |
| 221 if (COMPILED) { | 212 if (COMPILED) { |
| 222 listOfFiles = ['chromeVoxChromePageScript.js']; | 213 listOfFiles = ['chromeVoxChromePageScript.js']; |
| 223 } else { | 214 } else { |
| 224 listOfFiles = [ | 215 listOfFiles = [ |
| 225 'closure/closure_preinit.js', | 216 'closure/closure_preinit.js', 'closure/base.js', 'deps.js', |
| 226 'closure/base.js', | 217 'chromevox/injected/loader.js' |
| 227 'deps.js', | 218 ]; |
| 228 'chromevox/injected/loader.js']; | |
| 229 } | 219 } |
| 230 | 220 |
| 231 var stageTwo = function(code) { | 221 var stageTwo = function(code) { |
| 232 for (var i = 0, tab; tab = tabs[i]; i++) { | 222 for (var i = 0, tab; tab = tabs[i]; i++) { |
| 233 window.console.log('Injecting into ' + tab.id, tab); | 223 window.console.log('Injecting into ' + tab.id, tab); |
| 234 var sawError = false; | 224 var sawError = false; |
| 235 | 225 |
| 236 /** | 226 /** |
| 237 * A helper function which executes code. | 227 * A helper function which executes code. |
| 238 * @param {string} code The code to execute. | 228 * @param {string} code The code to execute. |
| 239 */ | 229 */ |
| 240 var executeScript = goog.bind(function(code) { | 230 var executeScript = goog.bind(function(code) { |
| 241 chrome.tabs.executeScript( | 231 chrome.tabs.executeScript( |
| 242 tab.id, | 232 tab.id, {'code': code, 'allFrames': true}, goog.bind(function() { |
| 243 {'code': code, | |
| 244 'allFrames': true}, | |
| 245 goog.bind(function() { | |
| 246 if (!chrome.extension.lastError) { | 233 if (!chrome.extension.lastError) { |
| 247 return; | 234 return; |
| 248 } | 235 } |
| 249 if (sawError) { | 236 if (sawError) { |
| 250 return; | 237 return; |
| 251 } | 238 } |
| 252 sawError = true; | 239 sawError = true; |
| 253 console.error('Could not inject into tab', tab); | 240 console.error('Could not inject into tab', tab); |
| 254 this.tts.speak('Error starting ChromeVox for ' + | 241 this.tts.speak( |
| 255 tab.title + ', ' + tab.url, cvox.QueueMode.QUEUE); | 242 'Error starting ChromeVox for ' + tab.title + ', ' + tab.url, |
| 243 cvox.QueueMode.QUEUE); |
| 256 }, this)); | 244 }, this)); |
| 257 }, this); | 245 }, this); |
| 258 | 246 |
| 259 // There is a scenario where two copies of the content script can get | 247 // There is a scenario where two copies of the content script can get |
| 260 // loaded into the same tab on browser startup - one automatically and one | 248 // loaded into the same tab on browser startup - one automatically and one |
| 261 // because the background page injects the content script into every tab | 249 // because the background page injects the content script into every tab |
| 262 // on startup. To work around potential bugs resulting from this, | 250 // on startup. To work around potential bugs resulting from this, |
| 263 // ChromeVox exports a global function called disableChromeVox() that can | 251 // ChromeVox exports a global function called disableChromeVox() that can |
| 264 // be used here to disable any existing running instance before we inject | 252 // be used here to disable any existing running instance before we inject |
| 265 // a new instance of the content script into this tab. | 253 // a new instance of the content script into this tab. |
| 266 // | 254 // |
| 267 // It's harmless if there wasn't a copy of ChromeVox already running. | 255 // It's harmless if there wasn't a copy of ChromeVox already running. |
| 268 // | 256 // |
| 269 // Also, set some variables so that Closure deps work correctly and so | 257 // Also, set some variables so that Closure deps work correctly and so |
| 270 // that ChromeVox knows not to announce feedback as if a page just loaded. | 258 // that ChromeVox knows not to announce feedback as if a page just loaded. |
| 271 executeScript('try { window.disableChromeVox(); } catch(e) { }\n' + | 259 executeScript( |
| 260 'try { window.disableChromeVox(); } catch(e) { }\n' + |
| 272 'window.INJECTED_AFTER_LOAD = true;\n' + | 261 'window.INJECTED_AFTER_LOAD = true;\n' + |
| 273 'window.CLOSURE_NO_DEPS = true\n'); | 262 'window.CLOSURE_NO_DEPS = true\n'); |
| 274 | 263 |
| 275 // Now inject the ChromeVox content script code into the tab. | 264 // Now inject the ChromeVox content script code into the tab. |
| 276 listOfFiles.forEach(function(file) { executeScript(code[file]); }); | 265 listOfFiles.forEach(function(file) { |
| 266 executeScript(code[file]); |
| 267 }); |
| 277 } | 268 } |
| 278 }; | 269 }; |
| 279 | 270 |
| 280 // We use fetchCode instead of chrome.extensions.executeFile because | 271 // We use fetchCode instead of chrome.extensions.executeFile because |
| 281 // executeFile doesn't propagate the file name to the content script | 272 // executeFile doesn't propagate the file name to the content script |
| 282 // which means that script is not visible in Dev Tools. | 273 // which means that script is not visible in Dev Tools. |
| 283 cvox.InjectedScriptLoader.fetchCode(listOfFiles, stageTwo); | 274 cvox.InjectedScriptLoader.fetchCode(listOfFiles, stageTwo); |
| 284 }; | 275 }; |
| 285 | 276 |
| 286 | 277 |
| 287 /** | 278 /** |
| 288 * Called when a TTS message is received from a page content script. | 279 * Called when a TTS message is received from a page content script. |
| 289 * @param {Object} msg The TTS message. | 280 * @param {Object} msg The TTS message. |
| 290 */ | 281 */ |
| 291 cvox.ChromeVoxBackground.prototype.onTtsMessage = function(msg) { | 282 cvox.ChromeVoxBackground.prototype.onTtsMessage = function(msg) { |
| 292 if (msg['action'] == 'speak') { | 283 if (msg['action'] == 'speak') { |
| 293 this.tts.speak(msg['text'], | 284 this.tts.speak( |
| 294 /** cvox.QueueMode */msg['queueMode'], | 285 msg['text'], |
| 295 msg['properties']); | 286 /** cvox.QueueMode */ msg['queueMode'], msg['properties']); |
| 296 } else if (msg['action'] == 'stop') { | 287 } else if (msg['action'] == 'stop') { |
| 297 this.tts.stop(); | 288 this.tts.stop(); |
| 298 } else if (msg['action'] == 'increaseOrDecrease') { | 289 } else if (msg['action'] == 'increaseOrDecrease') { |
| 299 this.tts.increaseOrDecreaseProperty(msg['property'], msg['increase']); | 290 this.tts.increaseOrDecreaseProperty(msg['property'], msg['increase']); |
| 300 var property = msg['property']; | 291 var property = msg['property']; |
| 301 var engine = this.backgroundTts_; | 292 var engine = this.backgroundTts_; |
| 302 var valueAsPercent = Math.round( | 293 var valueAsPercent = |
| 303 this.backgroundTts_.propertyToPercentage(property) * 100); | 294 Math.round(this.backgroundTts_.propertyToPercentage(property) * 100); |
| 304 var announcement; | 295 var announcement; |
| 305 switch (msg['property']) { | 296 switch (msg['property']) { |
| 306 case cvox.AbstractTts.RATE: | 297 case cvox.AbstractTts.RATE: |
| 307 announcement = Msgs.getMsg('announce_rate', | 298 announcement = Msgs.getMsg('announce_rate', [valueAsPercent]); |
| 308 [valueAsPercent]); | 299 break; |
| 309 break; | 300 case cvox.AbstractTts.PITCH: |
| 310 case cvox.AbstractTts.PITCH: | 301 announcement = Msgs.getMsg('announce_pitch', [valueAsPercent]); |
| 311 announcement = Msgs.getMsg('announce_pitch', | 302 break; |
| 312 [valueAsPercent]); | 303 case cvox.AbstractTts.VOLUME: |
| 313 break; | 304 announcement = Msgs.getMsg('announce_volume', [valueAsPercent]); |
| 314 case cvox.AbstractTts.VOLUME: | 305 break; |
| 315 announcement = Msgs.getMsg('announce_volume', | |
| 316 [valueAsPercent]); | |
| 317 break; | |
| 318 } | 306 } |
| 319 if (announcement) { | 307 if (announcement) { |
| 320 this.tts.speak(announcement, | 308 this.tts.speak( |
| 321 cvox.QueueMode.FLUSH, | 309 announcement, cvox.QueueMode.FLUSH, |
| 322 cvox.AbstractTts.PERSONALITY_ANNOTATION); | 310 cvox.AbstractTts.PERSONALITY_ANNOTATION); |
| 323 } | 311 } |
| 324 } else if (msg['action'] == 'cyclePunctuationEcho') { | 312 } else if (msg['action'] == 'cyclePunctuationEcho') { |
| 325 this.tts.speak(Msgs.getMsg( | 313 this.tts.speak( |
| 326 this.backgroundTts_.cyclePunctuationEcho()), | 314 Msgs.getMsg(this.backgroundTts_.cyclePunctuationEcho()), |
| 327 cvox.QueueMode.FLUSH); | 315 cvox.QueueMode.FLUSH); |
| 328 } | 316 } |
| 329 }; | 317 }; |
| 330 | 318 |
| 331 | 319 |
| 332 /** | 320 /** |
| 333 * Called when an earcon message is received from a page content script. | 321 * Called when an earcon message is received from a page content script. |
| 334 * @param {Object} msg The earcon message. | 322 * @param {Object} msg The earcon message. |
| 335 */ | 323 */ |
| 336 cvox.ChromeVoxBackground.prototype.onEarconMessage = function(msg) { | 324 cvox.ChromeVoxBackground.prototype.onEarconMessage = function(msg) { |
| 337 if (msg.action == 'play') { | 325 if (msg.action == 'play') { |
| 338 cvox.ChromeVox.earcons.playEarcon(msg['earcon']); | 326 cvox.ChromeVox.earcons.playEarcon(msg['earcon']); |
| 339 } | 327 } |
| 340 }; | 328 }; |
| 341 | 329 |
| 342 | 330 |
| 343 /** | 331 /** |
| 344 * Listen for connections from our content script bridges, and dispatch the | 332 * Listen for connections from our content script bridges, and dispatch the |
| 345 * messages to the proper destination. | 333 * messages to the proper destination. |
| 346 */ | 334 */ |
| 347 cvox.ChromeVoxBackground.prototype.addBridgeListener = function() { | 335 cvox.ChromeVoxBackground.prototype.addBridgeListener = function() { |
| 348 cvox.ExtensionBridge.addMessageListener(goog.bind(function(msg, port) { | 336 cvox.ExtensionBridge.addMessageListener(goog.bind(function(msg, port) { |
| 349 var target = msg['target']; | 337 var target = msg['target']; |
| 350 var action = msg['action']; | 338 var action = msg['action']; |
| 351 | 339 |
| 352 switch (target) { | 340 switch (target) { |
| 353 case 'OpenTab': | 341 case 'OpenTab': |
| 354 var destination = {url: msg['url']}; | 342 var destination = {url: msg['url']}; |
| 355 chrome.tabs.create(destination); | 343 chrome.tabs.create(destination); |
| 356 break; | 344 break; |
| 357 case 'KbExplorer': | 345 case 'KbExplorer': |
| 358 var explorerPage = {url: 'chromevox/background/kbexplorer.html'}; | 346 var explorerPage = {url: 'chromevox/background/kbexplorer.html'}; |
| 359 chrome.tabs.create(explorerPage); | 347 chrome.tabs.create(explorerPage); |
| 360 break; | 348 break; |
| 361 case 'HelpDocs': | 349 case 'HelpDocs': |
| 362 var helpPage = {url: 'http://chromevox.com/tutorial/index.html'}; | 350 var helpPage = {url: 'http://chromevox.com/tutorial/index.html'}; |
| 363 chrome.tabs.create(helpPage); | 351 chrome.tabs.create(helpPage); |
| 364 break; | 352 break; |
| 365 case 'Options': | 353 case 'Options': |
| 366 if (action == 'open') { | 354 if (action == 'open') { |
| 367 var optionsPage = {url: 'chromevox/background/options.html'}; | 355 var optionsPage = {url: 'chromevox/background/options.html'}; |
| 368 chrome.tabs.create(optionsPage); | 356 chrome.tabs.create(optionsPage); |
| 369 } | 357 } |
| 370 break; | 358 break; |
| 371 case 'Data': | 359 case 'Data': |
| 372 if (action == 'getHistory') { | 360 if (action == 'getHistory') { |
| 373 var results = {}; | 361 var results = {}; |
| 374 chrome.history.search({text: '', maxResults: 25}, function(items) { | 362 chrome.history.search({text: '', maxResults: 25}, function(items) { |
| 375 items.forEach(function(item) { | 363 items.forEach(function(item) { |
| 376 if (item.url) { | 364 if (item.url) { |
| 377 results[item.url] = true; | 365 results[item.url] = true; |
| 378 } | 366 } |
| 367 }); |
| 368 port.postMessage({'history': results}); |
| 379 }); | 369 }); |
| 370 } |
| 371 break; |
| 372 case 'Prefs': |
| 373 if (action == 'getPrefs') { |
| 374 this.prefs.sendPrefsToPort(port); |
| 375 } else if (action == 'setPref') { |
| 376 var pref = /** @type {string} */ (msg['pref']); |
| 377 var announce = !!msg['announce']; |
| 378 cvox.ChromeVoxBackground.setPref(pref, msg['value'], announce); |
| 379 } |
| 380 break; |
| 381 case 'Math': |
| 382 // TODO (sorge): Put the change of styles etc. here! |
| 383 if (msg['action'] == 'getDomains') { |
| 380 port.postMessage({ | 384 port.postMessage({ |
| 381 'history': results | 385 'message': 'DOMAINS_STYLES', |
| 386 'domains': this.backgroundTts_.mathmap.allDomains, |
| 387 'styles': this.backgroundTts_.mathmap.allStyles |
| 382 }); | 388 }); |
| 383 }); | 389 } |
| 384 } | 390 break; |
| 385 break; | 391 case 'TTS': |
| 386 case 'Prefs': | 392 if (msg['startCallbackId'] != undefined) { |
| 387 if (action == 'getPrefs') { | 393 msg['properties']['startCallback'] = function(opt_cleanupOnly) { |
| 388 this.prefs.sendPrefsToPort(port); | 394 port.postMessage({ |
| 389 } else if (action == 'setPref') { | 395 'message': 'TTS_CALLBACK', |
| 390 var pref = /** @type {string} */(msg['pref']); | 396 'cleanupOnly': opt_cleanupOnly, |
| 391 var announce = !!msg['announce']; | 397 'id': msg['startCallbackId'] |
| 392 cvox.ChromeVoxBackground.setPref( | 398 }); |
| 393 pref, msg['value'], announce); | 399 }; |
| 394 } | 400 } |
| 395 break; | 401 if (msg['endCallbackId'] != undefined) { |
| 396 case 'Math': | 402 msg['properties']['endCallback'] = function(opt_cleanupOnly) { |
| 397 // TODO (sorge): Put the change of styles etc. here! | 403 port.postMessage({ |
| 398 if (msg['action'] == 'getDomains') { | 404 'message': 'TTS_CALLBACK', |
| 399 port.postMessage({'message': 'DOMAINS_STYLES', | 405 'cleanupOnly': opt_cleanupOnly, |
| 400 'domains': this.backgroundTts_.mathmap.allDomains, | 406 'id': msg['endCallbackId'] |
| 401 'styles': this.backgroundTts_.mathmap.allStyles}); | 407 }); |
| 402 } | 408 }; |
| 403 break; | 409 } |
| 404 case 'TTS': | 410 try { |
| 405 if (msg['startCallbackId'] != undefined) { | 411 this.onTtsMessage(msg); |
| 406 msg['properties']['startCallback'] = function(opt_cleanupOnly) { | 412 } catch (err) { |
| 407 port.postMessage({'message': 'TTS_CALLBACK', | 413 console.log(err); |
| 408 'cleanupOnly': opt_cleanupOnly, | 414 } |
| 409 'id': msg['startCallbackId']}); | 415 break; |
| 410 }; | 416 case 'EARCON': |
| 411 } | 417 this.onEarconMessage(msg); |
| 412 if (msg['endCallbackId'] != undefined) { | 418 break; |
| 413 msg['properties']['endCallback'] = function(opt_cleanupOnly) { | 419 case 'BRAILLE': |
| 414 port.postMessage({'message': 'TTS_CALLBACK', | 420 try { |
| 415 'cleanupOnly': opt_cleanupOnly, | 421 this.backgroundBraille_.onBrailleMessage(msg); |
| 416 'id': msg['endCallbackId']}); | 422 } catch (err) { |
| 417 }; | 423 console.log(err); |
| 418 } | 424 } |
| 419 try { | 425 break; |
| 420 this.onTtsMessage(msg); | |
| 421 } catch (err) { | |
| 422 console.log(err); | |
| 423 } | |
| 424 break; | |
| 425 case 'EARCON': | |
| 426 this.onEarconMessage(msg); | |
| 427 break; | |
| 428 case 'BRAILLE': | |
| 429 try { | |
| 430 this.backgroundBraille_.onBrailleMessage(msg); | |
| 431 } catch (err) { | |
| 432 console.log(err); | |
| 433 } | |
| 434 break; | |
| 435 } | 426 } |
| 436 }, this)); | 427 }, this)); |
| 437 }; | 428 }; |
| 438 | 429 |
| 439 | 430 |
| 440 /** | 431 /** |
| 441 * Checks if we are currently in an incognito window. | 432 * Checks if we are currently in an incognito window. |
| 442 * @return {boolean} True if incognito or not within a tab context, false | 433 * @return {boolean} True if incognito or not within a tab context, false |
| 443 * otherwise. | 434 * otherwise. |
| 444 * @private | 435 * @private |
| 445 */ | 436 */ |
| 446 cvox.ChromeVoxBackground.prototype.isIncognito_ = function() { | 437 cvox.ChromeVoxBackground.prototype.isIncognito_ = function() { |
| 447 var incognito = false; | 438 var incognito = false; |
| 448 chrome.tabs.getCurrent(function(tab) { | 439 chrome.tabs.getCurrent(function(tab) { |
| 449 // Tab is null if not called from a tab context. In that case, also consider | 440 // Tab is null if not called from a tab context. In that case, also consider |
| 450 // it incognito. | 441 // it incognito. |
| 451 incognito = tab ? tab.incognito : true; | 442 incognito = tab ? tab.incognito : true; |
| 452 }); | 443 }); |
| 453 return incognito; | 444 return incognito; |
| 454 }; | 445 }; |
| 455 | 446 |
| 456 | 447 |
| 457 /** | 448 /** |
| 458 * Handles the onIntroduceChromeVox event. | 449 * Handles the onIntroduceChromeVox event. |
| 459 */ | 450 */ |
| 460 cvox.ChromeVoxBackground.prototype.onIntroduceChromeVox = function() { | 451 cvox.ChromeVoxBackground.prototype.onIntroduceChromeVox = function() { |
| 461 cvox.ChromeVox.tts.speak(Msgs.getMsg('chromevox_intro'), | 452 cvox.ChromeVox.tts.speak( |
| 462 cvox.QueueMode.QUEUE, | 453 Msgs.getMsg('chromevox_intro'), cvox.QueueMode.QUEUE, |
| 463 {doNotInterrupt: true}); | 454 {doNotInterrupt: true}); |
| 464 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText( | 455 cvox.ChromeVox.braille.write( |
| 465 Msgs.getMsg('intro_brl'))); | 456 cvox.NavBraille.fromText(Msgs.getMsg('intro_brl'))); |
| 466 }; | 457 }; |
| 467 | 458 |
| 468 | 459 |
| 469 /** | 460 /** |
| 470 * Gets the voice currently used by ChromeVox when calling tts. | 461 * Gets the voice currently used by ChromeVox when calling tts. |
| 471 * @return {string} | 462 * @return {string} |
| 472 */ | 463 */ |
| 473 cvox.ChromeVoxBackground.prototype.getCurrentVoice = function() { | 464 cvox.ChromeVoxBackground.prototype.getCurrentVoice = function() { |
| 474 return this.backgroundTts_.currentVoice; | 465 return this.backgroundTts_.currentVoice; |
| 475 }; | 466 }; |
| 476 | 467 |
| 477 | 468 |
| 478 // Create the background page object and export a function window['speak'] | 469 // Create the background page object and export a function window['speak'] |
| 479 // so that other background pages can access it. Also export the prefs object | 470 // so that other background pages can access it. Also export the prefs object |
| 480 // for access by the options page. | 471 // for access by the options page. |
| 481 (function() { | 472 (function() { |
| 482 var background = new cvox.ChromeVoxBackground(); | 473 var background = new cvox.ChromeVoxBackground(); |
| 483 background.init(); | 474 background.init(); |
| 484 window['speak'] = goog.bind(background.tts.speak, background.tts); | 475 window['speak'] = goog.bind(background.tts.speak, background.tts); |
| 485 ChromeVoxState.backgroundTts = background.backgroundTts_; | 476 ChromeVoxState.backgroundTts = background.backgroundTts_; |
| 486 | 477 |
| 487 // Export the prefs object for access by the options page. | 478 // Export the prefs object for access by the options page. |
| 488 window['prefs'] = background.prefs; | 479 window['prefs'] = background.prefs; |
| 489 | 480 |
| 490 // Export the braille translator manager for access by the options page. | 481 // Export the braille translator manager for access by the options page. |
| 491 window['braille_translator_manager'] = | 482 window['braille_translator_manager'] = |
| 492 background.backgroundBraille_.getTranslatorManager(); | 483 background.backgroundBraille_.getTranslatorManager(); |
| 493 | 484 |
| 494 window['getCurrentVoice'] = | 485 window['getCurrentVoice'] = background.getCurrentVoice.bind(background); |
| 495 background.getCurrentVoice.bind(background); | |
| 496 | 486 |
| 497 // Export injection for ChromeVox Next. | 487 // Export injection for ChromeVox Next. |
| 498 cvox.ChromeVox.injectChromeVoxIntoTabs = | 488 cvox.ChromeVox.injectChromeVoxIntoTabs = |
| 499 background.injectChromeVoxIntoTabs.bind(background); | 489 background.injectChromeVoxIntoTabs.bind(background); |
| 500 })(); | 490 })(); |
| OLD | NEW |