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
3 // found in the LICENSE file.
6 * @fileoverview The entry point for all ChromeVox2 related code for the
10 goog.provide('cvox2.Background');
11 goog.provide('cvox2.global');
13 goog.require('cvox.TabsApiHandler');
15 /** Classic Chrome accessibility API. */
16 cvox2.global.accessibility =
17 chrome.accessibilityPrivate || chrome.experimental.accessibility;
20 * ChromeVox2 background page.
22 cvox2.Background = function() {
24 * A list of site substring patterns to use with ChromeVox next. Keep these
25 * strings relatively specific.
26 * @type {!Array.<string>}
28 this.whitelist_ = ['http://www.chromevox.com/', 'chromevox_next_test'];
30 /** @type {cvox.TabsApiHandler} @private */
31 this.tabsHandler_ = new cvox.TabsApiHandler(cvox.ChromeVox.tts,
32 cvox.ChromeVox.braille,
33 cvox.ChromeVox.earcons);
35 // Only needed with unmerged ChromeVox classic loaded before.
36 cvox2.global.accessibility.setAccessibilityEnabled(false);
38 // Manually bind all functions to |this|.
39 for (var func in this) {
40 if (typeof(this[func]) == 'function')
41 this[func] = this[func].bind(this);
44 // Register listeners for ...
46 chrome.automation.getDesktop(this.onGotTree);
49 chrome.tabs.onUpdated.addListener(this.onTabUpdated);
52 cvox2.Background.prototype = {
54 * Handles chrome.tabs.onUpdated.
55 * @param {number} tabId
56 * @param {Object} changeInfo
58 onTabUpdated: function(tabId, changeInfo) {
59 chrome.tabs.get(tabId, function(tab) {
63 if (!this.isWhitelisted_(tab.url)) {
64 chrome.commands.onCommand.removeListener(this.onGotCommand);
65 cvox.ChromeVox.background.injectChromeVoxIntoTabs([tab], true);
69 if (!chrome.commands.onCommand.hasListeners()) {
70 chrome.commands.onCommand.addListener(this.onGotCommand);
73 this.disableClassicChromeVox_(tab.id);
75 chrome.automation.getTree(this.onGotTree.bind(this));
80 * Handles all setup once a new automation tree appears.
81 * @param {AutomationTree} tree The new automation tree.
83 onGotTree: function(root) {
84 // Register all automation event listeners.
85 root.addEventListener(chrome.automation.EventType.focus,
86 this.onAutomationEvent.bind(this),
91 * A generic handler for all desktop automation events.
92 * @param {AutomationEvent} evt The event.
94 onAutomationEvent: function(evt) {
95 var output = evt.target.attributes.name + ' ' + evt.target.role;
96 cvox.ChromeVox.tts.speak(output, cvox.AbstractTts.QUEUE_MODE_FLUSH);
97 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output));
98 chrome.accessibilityPrivate.setFocusRing([evt.target.location]);
102 * Handles chrome.commands.onCommand.
103 * @param {string} command
105 onGotCommand: function(command) {
110 * @param {string} url
111 * @return {boolean} Whether the given |url| is whitelisted.
113 isWhitelisted_: function(url) {
114 return this.whitelist_.some(function(item) {
115 return url.indexOf(item) != -1;
120 * Disables classic ChromeVox.
121 * @param {number} tabId The tab where ChromeVox classic is running.
123 disableClassicChromeVox_: function(tabId) {
124 chrome.tabs.executeScript(
126 {'code': 'try { window.disableChromeVox(); } catch(e) { }\n',
131 /** @type {cvox2.Background} */
132 cvox2.global.backgroundObj = new cvox2.Background();