summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/chromeos/chromevox/chromevox/background/braille_captions_background.js
blob: 3e4d386e2c465f402ec676a4498ae219eaa2fad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/** @fileoverview Sends braille content to a content script if the braille
 * captions feature is enabled.
 */

goog.provide('cvox.BrailleCaptionsBackground');

goog.require('cvox.BrailleDisplayState');
goog.require('cvox.ExtensionBridge');

/**
 * Key set in local storage when this feature is enabled.
 * @const
 */
cvox.BrailleCaptionsBackground.PREF_KEY = 'brailleCaptions';


/**
 * Unicode block of braille pattern characters.  A braille pattern is formed
 * from this value with the low order 8 bits set to the bits representing
 * the dots as per the ISO 11548-1 standard.
 * @const
 */
cvox.BrailleCaptionsBackground.BRAILLE_UNICODE_BLOCK_START = 0x2800;


/**
 * Called once to initialize the class.
 * @param {function()} stateCallback Called when the state of the captions
 *     feature changes.
 */
cvox.BrailleCaptionsBackground.init = function(stateCallback) {
  var self = cvox.BrailleCaptionsBackground;
  /**
   * @type {function()}
   * @private
   */
  self.stateCallback_ = stateCallback;
};


/**
 * Returns whether the braille captions feature is enabled.
 * @return {boolean}
 */
cvox.BrailleCaptionsBackground.isEnabled = function() {
  var self = cvox.BrailleCaptionsBackground;
  return localStorage[self.PREF_KEY] === String(true);
};


/**
 * @param {string} text Text of the shown braille.
 * @param {ArrayBuffer} cells Braille cells shown on the display.
 */
cvox.BrailleCaptionsBackground.setContent = function(text, cells) {
  var self = cvox.BrailleCaptionsBackground;
  // Convert the cells to Unicode braille pattern characters.
  var byteBuf = new Uint8Array(cells);
  var brailleChars = '';
  for (var i = 0; i < byteBuf.length; ++i) {
    brailleChars += String.fromCharCode(
        self.BRAILLE_UNICODE_BLOCK_START | byteBuf[i]);
  }
  cvox.ExtensionBridge.send({
    message: 'BRAILLE_CAPTION',
    text: text,
    brailleChars: brailleChars
  });
};


/**
 * Sets whether the overlay should be active.
 * @param {boolean} newValue The new value of the active flag.
 */
cvox.BrailleCaptionsBackground.setActive = function(newValue) {
  var self = cvox.BrailleCaptionsBackground;
  var oldValue = self.isEnabled();
  window['prefs'].setPref(self.PREF_KEY, String(newValue));
  if (oldValue != newValue) {
    if (self.stateCallback_) {
      self.stateCallback_();
    }
    var msg = newValue ?
        cvox.ChromeVox.msgs.getMsg('braille_captions_enabled') :
        cvox.ChromeVox.msgs.getMsg('braille_captions_disabled');
    cvox.ChromeVox.tts.speak(msg);
    cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(msg));
  }
};


/**
 * Returns a display state representing the state of the captions feature.
 * This is used when no actual hardware display is connected.
 * @return {cvox.BrailleDisplayState}
 */
cvox.BrailleCaptionsBackground.getVirtualDisplayState = function() {
  var self = cvox.BrailleCaptionsBackground;
  if (self.isEnabled()) {
    return {available: true, textCellCount: 40};  // 40, why not?
  } else {
    return {available: false};
  }
};