summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/chromeos/chromevox/braille/braille_translator_manager.js
blob: 2a152b228372ae2e03701b77057e5dd1556c14f2 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 Keeps track of the current braille translators.
 */

goog.provide('cvox.BrailleTranslatorManager');

goog.require('cvox.BrailleTable');
goog.require('cvox.ExpandingBrailleTranslator');
goog.require('cvox.LibLouis');

/**
 * @param {cvox.LibLouis=} opt_liblouisForTest Liblouis instance to use
 *     for testing.
 * @constructor
 */
cvox.BrailleTranslatorManager = function(opt_liblouisForTest) {
  /**
   * @type {!cvox.LibLouis}
   * @private
   */
  this.liblouis_ = opt_liblouisForTest || new cvox.LibLouis(
      chrome.extension.getURL('braille/liblouis_nacl.nmf'),
      chrome.extension.getURL('braille/tables'));
  /**
   * @type {!Array<function()>}
   * @private
   */
  this.changeListeners_ = [];
  /**
   * @type {!Array<cvox.BrailleTable.Table>}
   * @private
   */
  this.tables_ = [];
  /**
   * @type {cvox.ExpandingBrailleTranslator}
   * @private
   */
  this.expandingTranslator_ = null;
  /**
   * @type {cvox.LibLouis.Translator}
   * @private
   */
  this.defaultTranslator_ = null;
  /**
   * @type {string?}
   * @private
   */
  this.defaultTableId_ = null;
  /**
   * @type {cvox.LibLouis.Translator}
   * @private
   */
  this.uncontractedTranslator_ = null;
  /**
   * @type {string?}
   * @private
   */
  this.uncontractedTableId_ = null;

  if (!opt_liblouisForTest) {
    document.addEventListener('DOMContentLoaded',
                              this.loadLiblouis_.bind(this),
                              false);
  }
};

cvox.BrailleTranslatorManager.prototype = {
  /**
   * Adds a listener to be called whenever there is a change in the
   * translator(s) returned by other methods of this instance.
   * @param {function()} listener The listener.
   */
  addChangeListener: function(listener) {
    this.changeListeners_.push(listener);
  },

  /**
   * Refreshes the braille translator(s) used for input and output.  This
   * should be called when something has changed (such as a preference) to
   * make sure that the correct translator is used.
   */
  refresh: function() {
    var tables = this.tables_;
    if (tables.length == 0)
      return;

    // First, see if we have a braille table set previously.
    var table = cvox.BrailleTable.forId(tables, localStorage['brailleTable']);
    if (!table) {
      // Match table against current locale.
      var currentLocale = chrome.i18n.getMessage('@@ui_locale').split(/[_-]/);
      var major = currentLocale[0];
      var minor = currentLocale[1];
      var firstPass = tables.filter(function(table) {
        return table.locale.split(/[_-]/)[0] == major;
      });
      if (firstPass.length > 0) {
        table = firstPass[0];
        if (minor) {
          var secondPass = firstPass.filter(function(table) {
            return table.locale.split(/[_-]/)[1] == minor;
          });
          if (secondPass.length > 0)
            table = secondPass[0];
        }
      }
    }
    if (!table)
      table = cvox.BrailleTable.forId(tables, 'en-US-comp8');

    // TODO(plundblad): Only update when user explicitly selects a table
    // so that switching locales changes table by default.  crbug.com/441206.
    localStorage['brailleTable'] = table.id;
    if (!localStorage['brailleTable6'])
      localStorage['brailleTable6'] = 'en-US-g1';
    if (!localStorage['brailleTable8'])
      localStorage['brailleTable8'] = 'en-US-comp8';

    if (table.dots == '6') {
      localStorage['brailleTableType'] = 'brailleTable6';
      localStorage['brailleTable6'] = table.id;
    } else {
      localStorage['brailleTableType'] = 'brailleTable8';
      localStorage['brailleTable8'] = table.id;
    }

    // If the user explicitly set an 8 dot table, use that when looking
    // for an uncontracted table.  Otherwise, use the current table and let
    // getUncontracted find an appropriate corresponding table.
    var table8Dot = cvox.BrailleTable.forId(tables,
                                            localStorage['brailleTable8']);
    var uncontractedTable = cvox.BrailleTable.getUncontracted(
        tables, table8Dot || table);

    var newDefaultTableId = table.id;
    var newUncontractedTableId = table.id === uncontractedTable.id ?
        null : uncontractedTable.id;
    if (newDefaultTableId === this.defaultTableId_ &&
        newUncontractedTableId === this.uncontractedTableId_) {
      return;
    }

    var finishRefresh = function(defaultTranslator, uncontractedTranslator) {
      this.defaultTableId_ = newDefaultTableId;
      this.uncontractedTableId_ = newUncontractedTableId;
      this.expandingTranslator_ = new cvox.ExpandingBrailleTranslator(
          defaultTranslator, uncontractedTranslator);
      this.defaultTranslator_ = defaultTranslator;
      this.uncontractedTranslator_ = uncontractedTranslator;
      this.changeListeners_.forEach(function(listener) { listener(); });
    }.bind(this);

    this.liblouis_.getTranslator(table.fileNames, function(translator) {
      if (!newUncontractedTableId) {
        finishRefresh(translator, null);
      } else {
        this.liblouis_.getTranslator(
            uncontractedTable.fileNames,
            function(uncontractedTranslator) {
              finishRefresh(translator, uncontractedTranslator);
            });
          }
    }.bind(this));
  },

  /**
   * @return {cvox.ExpandingBrailleTranslator} The current expanding braille
   *     translator, or {@code null} if none is available.
   */
  getExpandingTranslator: function() {
    return this.expandingTranslator_;
  },

  /**
   * @return {cvox.LibLouis.Translator} The current braille translator to use
   *     by default, or {@code null} if none is available.
   */
  getDefaultTranslator: function() {
    return this.defaultTranslator_;
  },

  /**
   * @return {cvox.LibLouis.Translator} The current uncontracted braille
   *     translator, or {@code null} if it is the same as the default
   *     translator.
   */
  getUncontractedTranslator: function() {
    return this.uncontractedTranslator_;
  },

  /**
   * Asynchronously fetches the list of braille tables and refreshes the
   * translators when done.
   * @private
   */
  fetchTables_: function() {
    cvox.BrailleTable.getAll(function(tables) {
      this.tables_ = tables;
      this.refresh();
    }.bind(this));
  },

  /**
   * Loads the liblouis instance by attaching it to the document.
   * @private
   */
  loadLiblouis_: function() {
    // Cast away nullability.  When the document is loaded, it will always
    // have a body.
    this.liblouis_.attachToElement(
        /** @type {!HTMLBodyElement} */ (document.body));
    this.fetchTables_();
  },

  /**
   * @return {!cvox.LibLouis} The liblouis instance used by this object.
   */
  getLibLouisForTest: function() {
    return this.liblouis_;
  },

  /**
   * @return {!Array<cvox.BrailleTable.Table>} The currently loaded braille
   *     tables, or an empty array if they are not yet loaded.
   */
  getTablesForTest: function() {
    return this.tables_;
  }
};