Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / 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 goog.require('cvox.TabsApiHandler');
14
15 /** Classic Chrome accessibility API. */
16 cvox2.global.accessibility =
17     chrome.accessibilityPrivate || chrome.experimental.accessibility;
18
19 /**
20  * ChromeVox2 background page.
21  */
22 cvox2.Background = function() {
23   /**
24    * A list of site substring patterns to use with ChromeVox next. Keep these
25    * strings relatively specific.
26    * @type {!Array.<string>}
27    */
28   this.whitelist_ = ['http://www.chromevox.com/', 'chromevox_next_test'];
29
30   /** @type {cvox.TabsApiHandler} @private */
31   this.tabsHandler_ = new cvox.TabsApiHandler(cvox.ChromeVox.tts,
32                                               cvox.ChromeVox.braille,
33                                               cvox.ChromeVox.earcons);
34
35   // Only needed with unmerged ChromeVox classic loaded before.
36   cvox2.global.accessibility.setAccessibilityEnabled(false);
37
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);
42   }
43
44   // Register listeners for ...
45   // Desktop.
46   chrome.automation.getDesktop(this.onGotTree);
47
48   // Tabs.
49   chrome.tabs.onUpdated.addListener(this.onTabUpdated);
50 };
51
52 cvox2.Background.prototype = {
53   /**
54    * Handles chrome.tabs.onUpdated.
55    * @param {number} tabId
56    * @param {Object} changeInfo
57    */
58   onTabUpdated: function(tabId, changeInfo) {
59     chrome.tabs.get(tabId, function(tab) {
60       if (!tab.url)
61         return;
62
63       if (!this.isWhitelisted_(tab.url)) {
64         chrome.commands.onCommand.removeListener(this.onGotCommand);
65         cvox.ChromeVox.background.injectChromeVoxIntoTabs([tab], true);
66         return;
67       }
68
69       if (!chrome.commands.onCommand.hasListeners()) {
70         chrome.commands.onCommand.addListener(this.onGotCommand);
71       }
72
73       this.disableClassicChromeVox_(tab.id);
74
75       chrome.automation.getTree(this.onGotTree.bind(this));
76     }.bind(this));
77   },
78
79   /**
80    * Handles all setup once a new automation tree appears.
81    * @param {AutomationTree} tree The new automation tree.
82    */
83   onGotTree: function(root) {
84     // Register all automation event listeners.
85     root.addEventListener(chrome.automation.EventType.focus,
86                           this.onAutomationEvent.bind(this),
87                           true);
88   },
89
90   /**
91    * A generic handler for all desktop automation events.
92    * @param {AutomationEvent} evt The event.
93    */
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]);
99   },
100
101   /**
102    * Handles chrome.commands.onCommand.
103    * @param {string} command
104    */
105   onGotCommand: function(command) {
106   },
107
108   /**
109    * @private
110    * @param {string} url
111    * @return {boolean} Whether the given |url| is whitelisted.
112    */
113   isWhitelisted_: function(url) {
114     return this.whitelist_.some(function(item) {
115       return url.indexOf(item) != -1;
116     }.bind(this));
117   },
118
119   /**
120    * Disables classic ChromeVox.
121    * @param {number} tabId The tab where ChromeVox classic is running.
122    */
123   disableClassicChromeVox_: function(tabId) {
124     chrome.tabs.executeScript(
125           tabId,
126           {'code': 'try { window.disableChromeVox(); } catch(e) { }\n',
127            'allFrames': true});
128   }
129 };
130
131 /** @type {cvox2.Background} */
132 cvox2.global.backgroundObj = new cvox2.Background();