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 /** Classic Chrome accessibility API. */
14 cvox2.global.accessibility =
15 chrome.accessibilityPrivate || chrome.experimental.accessibility;
18 * ChromeVox2 background page.
20 cvox2.Background = function() {
21 // Only needed with unmerged ChromeVox classic loaded before.
22 cvox2.global.accessibility.setAccessibilityEnabled(false);
24 // Register listeners for ...
26 chrome.automation.getDesktop(this.onGotTree.bind(this));
29 chrome.tabs.onUpdated.addListener(this.onTabUpdated.bind(this));
31 // Keyboard events (currently Messages from content script).
32 chrome.extension.onConnect.addListener(this.onConnect.bind(this));
35 cvox2.Background.prototype = {
37 * ID of the port used to communicate between content script and background
41 PORT_ID: 'chromevox2',
44 * Handles chrome.extension.onConnect.
45 * @param {Object} port The port.
47 onConnect: function(port) {
48 if (port.name != this.PORT_ID)
50 port.onMessage.addListener(this.onMessage.bind(this));
54 * Dispatches messages to specific handlers.
55 * @param {Object} message The message.
57 onMessage: function(message) {
59 this.onKeyDown(message);
63 * Handles key down messages from the content script.
64 * @param {Object} message The key down message.
66 onKeyDown: function(message) {
67 // TODO(dtseng): Implement.
71 * Handles chrome.tabs.onUpdate.
72 * @param {number} tabId The tab id.
73 * @param {Object.<string, (string|boolean)>} changeInfo Information about
76 onTabUpdated: function(tabId, changeInfo) {
77 chrome.automation.getTree(this.onGotTree.bind(this));
81 * Handles all setup once a new automation tree appears.
82 * @param {AutomationTree} tree The new automation tree.
84 onGotTree: function(root) {
85 // Register all automation event listeners.
86 root.addEventListener(chrome.automation.EventType.focus,
87 this.onAutomationEvent.bind(this),
92 * A generic handler for all desktop automation events.
93 * @param {AutomationEvent} evt The event.
95 onAutomationEvent: function(evt) {
96 var output = evt.target.attributes.name + ' ' + evt.target.role;
97 cvox.ChromeVox.tts.speak(output);
98 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(output));
102 /** @type {cvox2.Background} */
103 cvox2.global.backgroundObj = new cvox2.Background();