abstract_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 Abstract interface to methods that differ depending on the
7 * host platform.
8 *
9 */
10
11goog.provide('cvox.AbstractHost');
12
13
14/**
15 * @constructor
16 */
17cvox.AbstractHost = function() {
18};
19
20
21/**
22 * @enum {number}
23 */
24cvox.AbstractHost.State = {
25  ACTIVE: 0,
26  INACTIVE: 1,
27  KILLED: 2
28};
29
30
31/**
32 * Do all host-platform-specific initialization.
33 */
34cvox.AbstractHost.prototype.init = function() {
35};
36
37
38/**
39 * Used to reinitialize ChromeVox if initialization fails.
40 */
41cvox.AbstractHost.prototype.reinit = function() {
42};
43
44
45/**
46 * Executed on page load.
47 */
48cvox.AbstractHost.prototype.onPageLoad = function() {
49};
50
51
52/**
53 * Sends a message to the background page (if it exists) for this host.
54 * @param {Object} message The message to pass to the background page.
55 */
56cvox.AbstractHost.prototype.sendToBackgroundPage = function(message) {
57};
58
59
60/**
61 * Returns the absolute URL to the API source.
62 * @return {string} The URL.
63 */
64cvox.AbstractHost.prototype.getApiSrc = function() {
65  return '';
66};
67
68
69/**
70 * Return the absolute URL to the given file.
71 * @param {string} path The URL suffix.
72 * @return {string} The full URL.
73 */
74cvox.AbstractHost.prototype.getFileSrc = function(path) {
75  return '';
76};
77
78
79/**
80 * @return {boolean} True if the host has a Tts callback.
81 */
82cvox.AbstractHost.prototype.hasTtsCallback = function() {
83  return true;
84};
85
86
87/**
88 * @return {boolean} True if the TTS has been loaded.
89 */
90cvox.AbstractHost.prototype.ttsLoaded = function() {
91  return true;
92};
93
94
95/**
96 * @return {boolean} True if the ChromeVox is supposed to intercept and handle
97 * mouse clicks for the platform, instead of just letting the clicks fall
98 * through.
99 *
100 * Note: This behavior is only needed for Android because of the way touch
101 * exploration and double-tap to click is implemented by the platform.
102 */
103cvox.AbstractHost.prototype.mustRedispatchClickEvent = function() {
104  return false;
105};
106
107/**
108 * Activate or deactivate ChromeVox on this host.
109 * @param {boolean} active The desired state; true for active, false for
110 * inactive.
111 */
112cvox.AbstractHost.prototype.activateOrDeactivateChromeVox = function(active) {
113  this.onStateChanged_(active ? cvox.AbstractHost.State.ACTIVE :
114      cvox.AbstractHost.State.INACTIVE);
115};
116
117
118/**
119 * Kills ChromeVox on this host.
120 */
121cvox.AbstractHost.prototype.killChromeVox = function() {
122  this.onStateChanged_(cvox.AbstractHost.State.KILLED);
123};
124
125
126/**
127 * Helper managing the three states of ChromeVox --
128 * active: all event listeners registered
129 * inactive: only key down listener registered
130 * killed: no listeners registered
131 * @param {cvox.AbstractHost.State} state The new state.
132 * @private
133 */
134cvox.AbstractHost.prototype.onStateChanged_ = function(state) {
135  var active = state == cvox.AbstractHost.State.ACTIVE;
136  if (active == cvox.ChromeVox.isActive) {
137    return;
138  }
139  cvox.ChromeVoxEventWatcher.cleanup(window);
140  switch (state) {
141    case cvox.AbstractHost.State.ACTIVE:
142      cvox.ChromeVox.isActive = true;
143      cvox.ChromeVox.navigationManager.showOrHideIndicator(true);
144      cvox.ChromeVoxEventWatcher.init(window);
145      if (document.activeElement) {
146        var speakNodeAlso = document.hasFocus() && !document.webkitHidden;
147        cvox.ApiImplementation.syncToNode(
148            document.activeElement, speakNodeAlso);
149      } else {
150        cvox.ChromeVox.navigationManager.updateIndicator();
151      }
152      break;
153    case cvox.AbstractHost.State.INACTIVE:
154      cvox.ChromeVox.isActive = false;
155      cvox.ChromeVox.navigationManager.showOrHideIndicator(false);
156      // If ChromeVox is inactive, the event watcher will only listen for key
157      // down events.
158      cvox.ChromeVoxEventWatcher.init(window);
159      break;
160    case cvox.AbstractHost.State.KILLED:
161      cvox.ChromeVox.isActive = false;
162      cvox.ChromeVox.navigationManager.showOrHideIndicator(false);
163      break;
164  }
165};
166