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.
5 /** @fileoverview Sends braille content to a content script if the braille
6 * captions feature is enabled.
9 goog.provide('cvox.BrailleCaptionsBackground');
11 goog.require('cvox.BrailleDisplayState');
12 goog.require('cvox.ExtensionBridge');
15 * Key set in local storage when this feature is enabled.
18 cvox.BrailleCaptionsBackground.PREF_KEY = 'brailleCaptions';
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.
27 cvox.BrailleCaptionsBackground.BRAILLE_UNICODE_BLOCK_START = 0x2800;
31 * Called once to initialize the class.
32 * @param {function()} stateCallback Called when the state of the captions
35 cvox.BrailleCaptionsBackground.init = function(stateCallback) {
36 var self = cvox.BrailleCaptionsBackground;
41 self.stateCallback_ = stateCallback;
46 * Returns whether the braille captions feature is enabled.
49 cvox.BrailleCaptionsBackground.isEnabled = function() {
50 var self = cvox.BrailleCaptionsBackground;
51 return localStorage[self.PREF_KEY] === String(true);
56 * @param {string} text Text of the shown braille.
57 * @param {ArrayBuffer} cells Braille cells shown on the display.
59 cvox.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]);
68 cvox.ExtensionBridge.send({
69 message: 'BRAILLE_CAPTION',
71 brailleChars: brailleChars
77 * Sets whether the overlay should be active.
78 * @param {boolean} newValue The new value of the active flag.
80 cvox.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_();
89 cvox.ChromeVox.msgs.getMsg('braille_captions_enabled') :
90 cvox.ChromeVox.msgs.getMsg('braille_captions_disabled');
91 cvox.ChromeVox.tts.speak(msg, cvox.QueueMode.QUEUE);
92 cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(msg));
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}
102 cvox.BrailleCaptionsBackground.getVirtualDisplayState = function() {
103 var self = cvox.BrailleCaptionsBackground;
104 if (self.isEnabled()) {
105 return {available: true, textCellCount: 40}; // 40, why not?
107 return {available: false};