tabs_api_handler.js revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5/**
6 * @fileoverview Accesses Chrome's tabs extension API and gives
7 * feedback for events that happen in the "Chrome of Chrome".
8 */
9
10goog.provide('cvox.TabsApiHandler');
11
12goog.require('cvox.AbstractEarcons');
13goog.require('cvox.AbstractTts');
14goog.require('cvox.BrailleInterface');
15goog.require('cvox.BrailleUtil');
16goog.require('cvox.NavBraille');
17
18
19/**
20 * Class that adds listeners and handles events from the tabs API.
21 * @constructor
22 * @param {cvox.TtsInterface} tts The TTS to use for speaking.
23 * @param {cvox.BrailleInterface} braille The braille interface to use for
24 * brailling.
25 * @param {cvox.AbstractEarcons} earcons The earcons object to use for playing
26 *        earcons.
27 */
28cvox.TabsApiHandler = function(tts, braille, earcons) {
29  /** @type {cvox.TtsInterface} @private */
30  this.tts_ = tts;
31  /** @type {cvox.BrailleInterface} @private */
32  this.braille_ = braille;
33  /** @type {cvox.AbstractEarcons} @private */
34  this.earcons_ = earcons;
35  /** @type {function(string)} @private */
36  this.msg_ = cvox.ChromeVox.msgs.getMsg.bind(cvox.ChromeVox.msgs);
37
38  chrome.tabs.onCreated.addListener(this.onCreated.bind(this));
39  chrome.tabs.onRemoved.addListener(this.onRemoved.bind(this));
40  chrome.tabs.onActivated.addListener(this.onActivated.bind(this));
41  chrome.tabs.onUpdated.addListener(this.onUpdated.bind(this));
42  chrome.windows.onFocusChanged.addListener(this.onFocusChanged.bind(this));
43};
44
45cvox.TabsApiHandler.prototype = {
46  /**
47   * Handles chrome.tabs.onCreated.
48   * @param {Object} tab
49   */
50  onCreated: function(tab) {
51    if (!cvox.ChromeVox.isActive) {
52      return;
53    }
54    this.tts_.speak(this.msg_('chrome_tab_created'),
55                   cvox.AbstractTts.QUEUE_MODE_FLUSH,
56                   cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT);
57    this.braille_.write(
58        cvox.NavBraille.fromText(this.msg_('chrome_tab_created')));
59    this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_OPEN);
60  },
61
62  /**
63   * Handles chrome.tabs.onRemoved.
64   * @param {Object} tab
65   */
66  onRemoved: function(tab) {
67    if (!cvox.ChromeVox.isActive) {
68      return;
69    }
70    this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_CLOSE);
71  },
72
73  /**
74   * Handles chrome.tabs.onActivated.
75   * @param {Object} activeInfo
76   */
77  onActivated: function(activeInfo) {
78    if (!cvox.ChromeVox.isActive) {
79      return;
80    }
81    chrome.tabs.get(activeInfo.tabId, function(tab) {
82      if (tab.status == 'loading') {
83        return;
84      }
85      var title = tab.title ? tab.title : tab.url;
86      this.tts_.speak(this.msg_('chrome_tab_selected',
87                         [title]),
88                     cvox.AbstractTts.QUEUE_MODE_FLUSH,
89                     cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT);
90      this.braille_.write(
91          cvox.NavBraille.fromText(this.msg_('chrome_tab_selected', [title])));
92      this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_SELECT);
93    }.bind(this));
94  },
95
96  /**
97   * Handles chrome.tabs.onUpdated.
98   * @param {number} tabId
99   * @param {Object} selectInfo
100   */
101  onUpdated: function(tabId, selectInfo) {
102    if (!cvox.ChromeVox.isActive) {
103      return;
104    }
105    chrome.tabs.get(tabId, function(tab) {
106      if (!tab.active) {
107        return;
108      }
109      if (tab.status == 'loading') {
110        this.earcons_.playEarcon(cvox.AbstractEarcons.BUSY_PROGRESS_LOOP);
111      } else {
112        this.earcons_.playEarcon(cvox.AbstractEarcons.TASK_SUCCESS);
113      }
114    }.bind(this));
115  },
116
117  /**
118   * Handles chrome.windows.onFocusChanged.
119   * @param {number} windowId
120   */
121  onFocusChanged: function(windowId) {
122    if (!cvox.ChromeVox.isActive) {
123      return;
124    }
125    if (windowId == chrome.windows.WINDOW_ID_NONE) {
126      return;
127    }
128    chrome.windows.get(windowId, function(window) {
129      chrome.tabs.getSelected(windowId, function(tab) {
130        var msgId = window.incognito ? 'chrome_incognito_window_selected' :
131          'chrome_normal_window_selected';
132        var title = tab.title ? tab.title : tab.url;
133        this.tts_.speak(this.msg_(msgId, [title]),
134                       cvox.AbstractTts.QUEUE_MODE_FLUSH,
135                       cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT);
136        this.braille_.write(
137            cvox.NavBraille.fromText(this.msg_(msgId, [title])));
138        this.earcons_.playEarcon(cvox.AbstractEarcons.OBJECT_SELECT);
139      }.bind(this));
140    }.bind(this));
141  }
142};
143