Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox2 / cvox2 / background / background.js
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.
4
5 /**
6  * @fileoverview The entry point for all ChromeVox2 related code for the
7  * background page.
8  */
9
10 goog.provide('cvox2.Background');
11 goog.provide('cvox2.global');
12
13 /** Classic Chrome accessibility API. */
14 cvox2.global.accessibility =
15     chrome.accessibilityPrivate || chrome.experimental.accessibility;
16
17 /**
18  * ChromeVox2 background page.
19  */
20 cvox2.Background = function() {
21   // Only needed with unmerged ChromeVox classic loaded before.
22   cvox2.global.accessibility.setAccessibilityEnabled(false);
23
24   // Register listeners for ...
25   // Desktop.
26   chrome.automation.getDesktop(this.onGotTree.bind(this));
27
28   // Tabs.
29   chrome.tabs.onUpdated.addListener(this.onTabUpdated.bind(this));
30
31   // Keyboard events (currently Messages from content script).
32   chrome.extension.onConnect.addListener(this.onConnect.bind(this));
33 };
34
35 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
43   /**
44    * Handles chrome.extension.onConnect.
45    * @param {Object} port The port.
46    */
47   onConnect: function(port) {
48     if (port.name != this.PORT_ID)
49       return;
50     port.onMessage.addListener(this.onMessage.bind(this));
51   },
52
53   /**
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.
82    * @param {AutomationTree} tree The new automation tree.
83    */
84   onGotTree: function(root) {
85     // Register all automation event listeners.
86     root.addEventListener(chrome.automation.EventType.focus,
87                           this.onAutomationEvent.bind(this),
88                           true);
89   },
90
91   /**
92    * A generic handler for all desktop automation events.
93    * @param {AutomationEvent} evt The event.
94    */
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));
99   }
100 };
101
102 /** @type {cvox2.Background} */
103 cvox2.global.backgroundObj = new cvox2.Background();