host.js revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 Chrome-specific implementation of methods that differ
7 * depending on the host platform.
8 *
9 */
10
11goog.provide('cvox.ChromeHost');
12
13goog.require('cvox.AbstractHost');
14goog.require('cvox.ApiImplementation');
15goog.require('cvox.BrailleOverlayWidget');
16goog.require('cvox.ChromeVox');
17goog.require('cvox.ChromeVoxEventWatcher');
18goog.require('cvox.ChromeVoxKbHandler');
19goog.require('cvox.ExtensionBridge');
20goog.require('cvox.HostFactory');
21goog.require('cvox.InitialSpeech');
22goog.require('cvox.PdfProcessor');
23goog.require('cvox.SearchLoader');
24goog.require('cvox.TraverseMath');
25
26/**
27 * @constructor
28 * @extends {cvox.AbstractHost}
29 */
30cvox.ChromeHost = function() {
31  goog.base(this);
32
33  /** @private {boolean} */
34  this.gotPrefsAtLeastOnce_ = false;
35};
36goog.inherits(cvox.ChromeHost, cvox.AbstractHost);
37
38
39/** @override */
40cvox.ChromeHost.prototype.init = function() {
41  // TODO(deboer): This pattern is relatively painful since it
42  // must be duplicated in all host.js files. It also causes odd
43  // dependencies.
44  // TODO (stoarca): Not using goog.bind because for some reason it gets
45  // compiled to native code and not possible to debug.
46  var self = this;
47  var listener = function(message) {
48    if (message['history']) {
49      cvox.ChromeVox.visitedUrls = message['history'];
50    }
51
52    if (message['keyBindings']) {
53      cvox.ChromeVoxKbHandler.loadKeyToFunctionsTable(message['keyBindings']);
54    }
55    if (message['prefs']) {
56      var prefs = message['prefs'];
57      cvox.ChromeVoxEditableTextBase.useIBeamCursor =
58          (prefs['useIBeamCursor'] == 'true');
59      cvox.ChromeVoxEditableTextBase.eventTypingEcho = true;
60      cvox.ChromeVoxEventWatcher.focusFollowsMouse =
61          (prefs['focusFollowsMouse'] == 'true');
62
63      cvox.ChromeVox.version = prefs['version'];
64
65      cvox.ChromeVox.earcons.enabled =
66          /** @type {boolean} */(JSON.parse(prefs['earcons']));
67
68      cvox.ChromeVox.typingEcho =
69          /** @type {number} */(JSON.parse(prefs['typingEcho']));
70
71      if (prefs['position']) {
72        cvox.ChromeVox.position =
73            /** @type {Object.<string, {x:number, y:number}>} */ (
74                JSON.parse(prefs['position']));
75      }
76
77      if (prefs['granularity'] != 'undefined') {
78        cvox.ChromeVox.navigationManager.setGranularity(
79            /** @type {number} */ (JSON.parse(prefs['granularity'])));
80      }
81
82      self.activateOrDeactivateChromeVox(prefs['active'] == 'true');
83      self.activateOrDeactivateStickyMode(prefs['sticky'] == 'true');
84      if (!self.gotPrefsAtLeastOnce_) {
85        cvox.InitialSpeech.speak();
86      }
87      self.gotPrefsAtLeastOnce_ = true;
88
89      if (prefs['useVerboseMode'] == 'false') {
90        cvox.ChromeVox.verbosity = cvox.VERBOSITY_BRIEF;
91      } else {
92        cvox.ChromeVox.verbosity = cvox.VERBOSITY_VERBOSE;
93      }
94      if (prefs['cvoxKey']) {
95        cvox.ChromeVox.modKeyStr = prefs['cvoxKey'];
96      }
97
98      var apiPrefsChanged = (
99          prefs['siteSpecificScriptLoader'] !=
100              cvox.ApiImplementation.siteSpecificScriptLoader ||
101                  prefs['siteSpecificScriptBase'] !=
102                      cvox.ApiImplementation.siteSpecificScriptBase);
103      cvox.ApiImplementation.siteSpecificScriptLoader =
104          prefs['siteSpecificScriptLoader'];
105      cvox.ApiImplementation.siteSpecificScriptBase =
106          prefs['siteSpecificScriptBase'];
107      if (apiPrefsChanged) {
108        var searchInit = prefs['siteSpecificEnhancements'] === 'true' ?
109            cvox.SearchLoader.init : undefined;
110        cvox.ApiImplementation.init(searchInit);
111      }
112      cvox.BrailleOverlayWidget.getInstance().setActive(
113          prefs['brailleCaptions'] == 'true');
114    }
115  };
116  cvox.ExtensionBridge.addMessageListener(listener);
117
118  cvox.ExtensionBridge.addMessageListener(function(msg, port) {
119    if (msg['message'] == 'DOMAINS_STYLES') {
120      cvox.TraverseMath.getInstance().addDomainsAndStyles(
121          msg['domains'], msg['styles']);
122    }});
123
124  cvox.ExtensionBridge.addMessageListener(function(msg, port) {
125    var message = msg['message'];
126    if (message == 'USER_COMMAND') {
127      var cmd = msg['command'];
128      cvox.ChromeVoxUserCommands.commands[cmd](msg);
129    }
130  });
131
132  cvox.ExtensionBridge.send({
133      'target': 'Prefs',
134      'action': 'getPrefs'
135    });
136
137  cvox.ExtensionBridge.send({
138      'target': 'Data',
139      'action': 'getHistory'
140    });
141};
142
143
144/** @override */
145cvox.ChromeHost.prototype.reinit = function() {
146  cvox.ExtensionBridge.init();
147};
148
149
150/** @override */
151cvox.ChromeHost.prototype.onPageLoad = function() {
152  cvox.PdfProcessor.processEmbeddedPdfs();
153
154  cvox.ExtensionBridge.addDisconnectListener(goog.bind(function() {
155    cvox.ChromeVox.isActive = false;
156    cvox.ChromeVoxEventWatcher.cleanup(window);
157    // TODO(stoarca): Huh?? Why are we resetting during disconnect?
158    // This is not appropriate behavior!
159    cvox.ChromeVox.navigationManager.reset();
160  }, this));
161};
162
163
164/** @override */
165cvox.ChromeHost.prototype.sendToBackgroundPage = function(message) {
166  cvox.ExtensionBridge.send(message);
167};
168
169
170/** @override */
171cvox.ChromeHost.prototype.getApiSrc = function() {
172  return this.getFileSrc('chromevox/injected/api.js');
173};
174
175
176/** @override */
177cvox.ChromeHost.prototype.getFileSrc = function(file) {
178  return window.chrome.extension.getURL(file);
179};
180
181
182/** @override */
183cvox.ChromeHost.prototype.killChromeVox = function() {
184  goog.base(this, 'killChromeVox');
185  cvox.ExtensionBridge.removeMessageListeners();
186};
187
188
189/**
190 * Activates or deactivates Sticky Mode.
191 * @param {boolean} sticky Whether sticky mode should be active.
192 */
193cvox.ChromeHost.prototype.activateOrDeactivateStickyMode = function(sticky) {
194  cvox.ChromeVox.isStickyOn = sticky;
195};
196
197cvox.HostFactory.hostConstructor = cvox.ChromeHost;
198