summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/chromevox/background/tabs_api_handler.js
blob: baa004dc9cbcf71fe4ef1acffc2d075bae04edcb (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
// 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 Accesses Chrome's tabs extension API and gives
 * feedback for events that happen in the "Chrome of Chrome".
 */

goog.provide('cvox.TabsApiHandler');

goog.require('cvox.AbstractEarcons');
goog.require('cvox.AbstractTts');
goog.require('cvox.BrailleInterface');
goog.require('cvox.ChromeVox');
goog.require('cvox.NavBraille');


/**
 * Class that adds listeners and handles events from the tabs API.
 * @constructor
 * @param {cvox.TtsInterface} tts The TTS to use for speaking.
 * @param {cvox.BrailleInterface} braille The braille interface to use for
 * brailling.
 * @param {cvox.AbstractEarcons} earcons The earcons object to use for playing
 *        earcons.
 */
cvox.TabsApiHandler = function(tts, braille, earcons) {
  /** @type {cvox.TtsInterface} @private */
  this.tts_ = tts;
  /** @type {cvox.BrailleInterface} @private */
  this.braille_ = braille;
  /** @type {cvox.AbstractEarcons} @private */
  this.earcons_ = earcons;
  /** @type {function(string, Array<string>=)} @private */
  this.msg_ = cvox.ChromeVox.msgs.getMsg.bind(cvox.ChromeVox.msgs);
  /**
   * Tracks whether the active tab has finished loading.
   * @type {boolean}
   * @private
   */
  this.lastActiveTabLoaded_ = false;

  chrome.tabs.onCreated.addListener(this.onCreated.bind(this));
  chrome.tabs.onRemoved.addListener(this.onRemoved.bind(this));
  chrome.tabs.onActivated.addListener(this.onActivated.bind(this));
  chrome.tabs.onUpdated.addListener(this.onUpdated.bind(this));
  chrome.windows.onFocusChanged.addListener(this.onFocusChanged.bind(this));
};

cvox.TabsApiHandler.prototype = {
  /**
   * Handles chrome.tabs.onCreated.
   * @param {Object} tab
   */
  onCreated: function(tab) {
    if (!cvox.ChromeVox.isActive) {
      return;
    }
    this.tts_.speak(this.msg_('chrome_tab_created'),
                   cvox.QueueMode.FLUSH,
                   cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT);
    this.braille_.write(
        cvox.NavBraille.fromText(this.msg_('chrome_tab_created')));
    this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_OPEN);
  },

  /**
   * Handles chrome.tabs.onRemoved.
   * @param {Object} tab
   */
  onRemoved: function(tab) {
    if (!cvox.ChromeVox.isActive) {
      return;
    }
    this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_CLOSE);
  },

  /**
   * Handles chrome.tabs.onActivated.
   * @param {Object} activeInfo
   */
  onActivated: function(activeInfo) {
    if (!cvox.ChromeVox.isActive) {
      return;
    }
    chrome.tabs.get(activeInfo.tabId, function(tab) {
      this.lastActiveTabLoaded_ = tab.status == 'complete';
      if (tab.status == 'loading') {
        return;
      }
      var title = tab.title ? tab.title : tab.url;
      this.tts_.speak(this.msg_('chrome_tab_selected',
                         [title]),
                     cvox.QueueMode.FLUSH,
                     cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT);
      this.braille_.write(
          cvox.NavBraille.fromText(this.msg_('chrome_tab_selected', [title])));
      this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_SELECT);
    }.bind(this));
  },

  /**
   * Handles chrome.tabs.onUpdated.
   * @param {number} tabId
   * @param {Object} selectInfo
   */
  onUpdated: function(tabId, selectInfo) {
    if (!cvox.ChromeVox.isActive) {
      return;
    }
    chrome.tabs.get(tabId, function(tab) {
      if (!tab.active) {
        return;
      }
      if (tab.status == 'loading') {
        this.lastActiveTabLoaded_ = false;
        this.earcons_.playEarcon(cvox.AbstractEarcons.BUSY_PROGRESS_LOOP);
      } else if (!this.lastActiveTabLoaded_) {
        this.lastActiveTabLoaded_ = true;
        this.earcons_.playEarcon(cvox.AbstractEarcons.TASK_SUCCESS);
      }
    }.bind(this));
  },

  /**
   * Handles chrome.windows.onFocusChanged.
   * @param {number} windowId
   */
  onFocusChanged: function(windowId) {
    if (!cvox.ChromeVox.isActive) {
      return;
    }
    if (windowId == chrome.windows.WINDOW_ID_NONE) {
      return;
    }
    chrome.windows.get(windowId, function(window) {
      chrome.tabs.getSelected(windowId, function(tab) {
        var msgId = window.incognito ? 'chrome_incognito_window_selected' :
          'chrome_normal_window_selected';
        var title = tab.title ? tab.title : tab.url;
        this.tts_.speak(this.msg_(msgId, [title]),
                       cvox.QueueMode.FLUSH,
                       cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT);
        this.braille_.write(
            cvox.NavBraille.fromText(this.msg_(msgId, [title])));
        this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_SELECT);
      }.bind(this));
    }.bind(this));
  }
};