1 // Copyright 2015 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.
6 * @fileoverview Provides a compatibility layer for ChromeVox Classic during the
7 * transition to ChromeVox Next.
10 goog.provide('ClassicCompatibility');
12 goog.require('cvox.ExtensionBridge');
13 goog.require('cvox.KeyMap');
14 goog.require('cvox.KeySequence');
15 goog.require('cvox.KeyUtil');
16 goog.require('cvox.SimpleKeyEvent');
21 var ClassicCompatibility = function() {
23 * @type {!Array<{description: string, name: string, shortcut: string}>}
28 // We grab the list of commands from the manifest because
29 // chrome.commands.getAll is buggy.
30 /** @type {!Object} */
31 var commands = chrome.runtime.getManifest()['commands'];
32 for (var key in commands) {
33 /** @type {{suggested_key: {chromeos: string}}} */
34 var command = commands[key];
35 this.commands_.push({name: key, shortcut: command.suggested_key.chromeos});
39 ClassicCompatibility.prototype = {
41 * Processes a ChromeVox Next command.
42 * @param {string} command
43 * @return {boolean} Whether the command was successfully processed.
45 onGotCommand: function(command) {
46 var evt = this.buildKeyEvent_(command);
48 this.simulateKeyDownNext_(evt);
51 cvox.KeyUtil.sequencing = false;
55 * Processes a ChromeVox Next command while in CLASSIC mode.
56 * @param {string} command
57 * @return {boolean} Whether the command was successfully processed.
59 onGotClassicCommand: function(command) {
60 var evt = this.buildKeyEvent_(command);
63 this.simulateKeyDownClassic_(evt);
68 * @param {string} command
69 * @return {cvox.SimpleKeyEvent?}
71 buildKeyEvent_: function(command) {
72 var commandInfo = this.commands_.filter(function(c) {
73 return c.name == command;
77 var shortcut = commandInfo.shortcut;
78 return this.convertCommandShortcutToKeyEvent_(shortcut);
82 * @param {cvox.SimpleKeyEvent} evt
85 simulateKeyDownNext_: function(evt) {
86 var keySequence = cvox.KeyUtil.keyEventToKeySequence(evt);
88 cvox.KeyMap.fromCurrentKeyMap().commandForKey(keySequence);
90 var nextCommand = this.getNextCommand_(classicCommand);
92 global.backgroundObj.onGotCommand(nextCommand, true);
97 * @param {cvox.SimpleKeyEvent} evt
100 simulateKeyDownClassic_: function(evt) {
101 var keySequence = cvox.KeyUtil.keyEventToKeySequence(evt);
103 cvox.KeyMap.fromCurrentKeyMap().commandForKey(keySequence);
104 if (classicCommand) {
105 cvox.ExtensionBridge.send({
106 'message': 'USER_COMMAND',
107 'command': classicCommand
113 * @param {string} shortcut
114 * @return {cvox.SimpleKeyEvent}
117 convertCommandShortcutToKeyEvent_: function(shortcut) {
119 shortcut.split('+').forEach(function(token) {
132 evt.searchKeyHeld = true;
150 evt.keyCode = token.charCodeAt(0);
158 * Maps a Classic command to an approximate equivalent in Next.
159 * @param {string} classicCommand
163 getNextCommand_: function(classicCommand) {
164 switch (classicCommand) {
166 return 'nextElement';
170 return 'previousElement';
172 return 'previousLine';
173 case 'forceClickOnCurrentItem':
176 return 'continuousRead';
178 return classicCommand;