braille_captions_background.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/** @fileoverview Sends braille content to a content script if the braille
6 * captions feature is enabled.
7 */
8
9goog.provide('cvox.BrailleCaptionsBackground');
10
11goog.require('cvox.BrailleDisplayState');
12goog.require('cvox.ExtensionBridge');
13
14/**
15 * Key set in local storage when this feature is enabled.
16 * @const
17 */
18cvox.BrailleCaptionsBackground.PREF_KEY = 'brailleCaptions';
19
20
21/**
22 * Unicode block of braille pattern characters.  A braille pattern is formed
23 * from this value with the low order 8 bits set to the bits representing
24 * the dots as per the ISO 11548-1 standard.
25 * @const
26 */
27cvox.BrailleCaptionsBackground.BRAILLE_UNICODE_BLOCK_START = 0x2800;
28
29
30/**
31 * Called once to initialize the class.
32 * @param {function()} stateCallback Called when the state of the captions
33 *     feature changes.
34 */
35cvox.BrailleCaptionsBackground.init = function(stateCallback) {
36  var self = cvox.BrailleCaptionsBackground;
37  /**
38   * @type {function()}
39   * @private
40   */
41  self.stateCallback_ = stateCallback;
42};
43
44
45/**
46 * Returns whether the braille captions feature is enabled.
47 * @return {boolean}
48 */
49cvox.BrailleCaptionsBackground.isEnabled = function() {
50  var self = cvox.BrailleCaptionsBackground;
51  return localStorage[self.PREF_KEY] === String(true);
52};
53
54
55/**
56 * @param {string} text Text of the shown braille.
57 * @param {ArrayBuffer} cells Braille cells shown on the display.
58 */
59cvox.BrailleCaptionsBackground.setContent = function(text, cells) {
60  var self = cvox.BrailleCaptionsBackground;
61  // Convert the cells to Unicode braille pattern characters.
62  var byteBuf = new Uint8Array(cells);
63  var brailleChars = '';
64  for (var i = 0; i < byteBuf.length; ++i) {
65    brailleChars += String.fromCharCode(
66        self.BRAILLE_UNICODE_BLOCK_START | byteBuf[i]);
67  }
68  cvox.ExtensionBridge.send({
69    message: 'BRAILLE_CAPTION',
70    text: text,
71    brailleChars: brailleChars
72  });
73};
74
75
76/**
77 * Sets whether the overlay should be active.
78 * @param {boolean} newValue The new value of the active flag.
79 */
80cvox.BrailleCaptionsBackground.setActive = function(newValue) {
81  var self = cvox.BrailleCaptionsBackground;
82  var oldValue = self.isEnabled();
83  window['prefs'].setPref(self.PREF_KEY, String(newValue));
84  if (oldValue != newValue) {
85    if (self.stateCallback_) {
86      self.stateCallback_();
87    }
88    var msg = newValue ?
89        cvox.ChromeVox.msgs.getMsg('braille_captions_enabled') :
90        cvox.ChromeVox.msgs.getMsg('braille_captions_disabled');
91    cvox.ChromeVox.tts.speak(msg);
92    cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(msg));
93  }
94};
95
96
97/**
98 * Returns a display state representing the state of the captions feature.
99 * This is used when no actual hardware display is connected.
100 * @return {cvox.BrailleDisplayState}
101 */
102cvox.BrailleCaptionsBackground.getVirtualDisplayState = function() {
103  var self = cvox.BrailleCaptionsBackground;
104  if (self.isEnabled()) {
105    return {available: true, textCellCount: 40};  // 40, why not?
106  } else {
107    return {available: false};
108  }
109};
110