summaryrefslogtreecommitdiffstats
path: root/chrome/browser/speech/speech_input_manager.cc
blob: 707f85516b342740bfa63588cda15b024f2173ad (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
// Copyright (c) 2010 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.

#include "chrome/browser/speech/speech_input_manager.h"

#include "app/l10n_util.h"
#include "base/ref_counted.h"
#include "base/singleton.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/speech/speech_input_bubble_controller.h"
#include "chrome/browser/speech/speech_recognizer.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "grit/generated_resources.h"
#include "media/audio/audio_manager.h"
#include <map>

namespace speech_input {

class SpeechInputManagerImpl : public SpeechInputManager,
                               public SpeechInputBubbleControllerDelegate,
                               public SpeechRecognizerDelegate {
 public:
  // SpeechInputManager methods.
  virtual void StartRecognition(SpeechInputManagerDelegate* delegate,
                                int caller_id,
                                int render_process_id,
                                int render_view_id,
                                const gfx::Rect& element_rect);
  virtual void CancelRecognition(int caller_id);
  virtual void StopRecording(int caller_id);

  // SpeechRecognizer::Delegate methods.
  virtual void SetRecognitionResult(int caller_id, bool error,
                                    const string16& value);
  virtual void DidCompleteRecording(int caller_id);
  virtual void DidCompleteRecognition(int caller_id);
  virtual void OnRecognizerError(int caller_id);

  // SpeechInputBubbleController::Delegate methods.
  virtual void RecognitionCancelled(int caller_id);
  virtual void SpeechInputFocusChanged(int caller_id);

 private:
  struct SpeechInputRequest {
    SpeechInputManagerDelegate* delegate;
    scoped_refptr<SpeechRecognizer> recognizer;
    int render_process_id;
    int render_view_id;
  };

  // Private constructor to enforce singleton.
  friend struct DefaultSingletonTraits<SpeechInputManagerImpl>;
  SpeechInputManagerImpl();
  virtual ~SpeechInputManagerImpl();

  bool HasPendingRequest(int caller_id) const;
  SpeechRecognizer* GetRecognizer(int caller_id) const;
  SpeechInputManagerDelegate* GetDelegate(int caller_id) const;

  void CancelRecognitionAndInformDelegate(int caller_id);

  // Shows an error message string as a simple alert info bar for the tab
  // identified by |render_process_id| and |render_view_id|.
  void ShowErrorMessage(int render_process_id, int render_view_id,
                        const string16& message);
  static void ShowErrorMessageInUIThread(
      int render_process_id, int render_view_id,
      const string16& message);

  SpeechInputManagerDelegate* delegate_;
  typedef std::map<int, SpeechInputRequest> SpeechRecognizerMap;
  SpeechRecognizerMap requests_;
  int recording_caller_id_;
  scoped_refptr<SpeechInputBubbleController> bubble_controller_;
};

SpeechInputManager* SpeechInputManager::Get() {
  return Singleton<SpeechInputManagerImpl>::get();
}

SpeechInputManagerImpl::SpeechInputManagerImpl()
    : recording_caller_id_(0),
      bubble_controller_(new SpeechInputBubbleController(
          ALLOW_THIS_IN_INITIALIZER_LIST(this))) {
}

SpeechInputManagerImpl::~SpeechInputManagerImpl() {
  while (requests_.begin() != requests_.end())
    CancelRecognition(requests_.begin()->first);
}

bool SpeechInputManagerImpl::HasPendingRequest(int caller_id) const {
  return requests_.find(caller_id) != requests_.end();
}

SpeechInputManagerDelegate* SpeechInputManagerImpl::GetDelegate(
    int caller_id) const {
  return requests_.find(caller_id)->second.delegate;
}

SpeechRecognizer* SpeechInputManagerImpl::GetRecognizer(int caller_id) const {
  return requests_.find(caller_id)->second.recognizer;
}

void SpeechInputManagerImpl::StartRecognition(
    SpeechInputManagerDelegate* delegate,
    int caller_id,
    int render_process_id,
    int render_view_id,
    const gfx::Rect& element_rect) {
  DCHECK(!HasPendingRequest(caller_id));

  // If we are currently recording audio for another caller, abort that cleanly.
  if (recording_caller_id_)
    CancelRecognitionAndInformDelegate(recording_caller_id_);

  if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) {
    ShowErrorMessage(render_process_id, render_view_id,
                     l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_NO_MIC));
    delegate->DidCompleteRecognition(caller_id);
    return;
  }

  bubble_controller_->CreateBubble(caller_id, render_process_id,
                                   render_view_id, element_rect);

  recording_caller_id_ = caller_id;
  SpeechInputRequest request;
  request.delegate = delegate;
  request.recognizer = new SpeechRecognizer(this, caller_id);
  request.render_process_id = render_process_id;
  request.render_view_id = render_view_id;
  requests_[caller_id] = request;
  request.recognizer->StartRecording();
}

void SpeechInputManagerImpl::CancelRecognition(int caller_id) {
  DCHECK(HasPendingRequest(caller_id));
  GetRecognizer(caller_id)->CancelRecognition();
  requests_.erase(caller_id);
  if (recording_caller_id_ == caller_id)
    recording_caller_id_ = 0;
  bubble_controller_->CloseBubble(caller_id);
}

void SpeechInputManagerImpl::StopRecording(int caller_id) {
  DCHECK(HasPendingRequest(caller_id));
  GetRecognizer(caller_id)->StopRecording();
}

void SpeechInputManagerImpl::SetRecognitionResult(int caller_id,
                                                  bool error,
                                                  const string16& value) {
  DCHECK(HasPendingRequest(caller_id));
  GetDelegate(caller_id)->SetRecognitionResult(caller_id,
                                               (error ? string16() : value));
}

void SpeechInputManagerImpl::DidCompleteRecording(int caller_id) {
  DCHECK(recording_caller_id_ == caller_id);
  DCHECK(HasPendingRequest(caller_id));
  recording_caller_id_ = 0;
  GetDelegate(caller_id)->DidCompleteRecording(caller_id);
  bubble_controller_->SetBubbleToRecognizingMode(caller_id);
}

void SpeechInputManagerImpl::DidCompleteRecognition(int caller_id) {
  GetDelegate(caller_id)->DidCompleteRecognition(caller_id);
  requests_.erase(caller_id);
  bubble_controller_->CloseBubble(caller_id);
}

void SpeechInputManagerImpl::OnRecognizerError(int caller_id) {
  const SpeechInputRequest& request = requests_.find(caller_id)->second;
  ShowErrorMessage(request.render_process_id, request.render_view_id,
                   l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_ERROR));
}

void SpeechInputManagerImpl::CancelRecognitionAndInformDelegate(int caller_id) {
  SpeechInputManagerDelegate* cur_delegate = GetDelegate(caller_id);
  CancelRecognition(caller_id);
  cur_delegate->DidCompleteRecording(caller_id);
  cur_delegate->DidCompleteRecognition(caller_id);
}

void SpeechInputManagerImpl::RecognitionCancelled(int caller_id) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  // Ignore if the caller id was not in our active recognizers list because the
  // user might have clicked more than once, or recognition could have been
  // cancelled due to other reasons before the user click was processed.
  if (HasPendingRequest(caller_id))
    CancelRecognitionAndInformDelegate(caller_id);
}

void SpeechInputManagerImpl::SpeechInputFocusChanged(int caller_id) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  // Ignore if the caller id was not in our active recognizers list because the
  // user might have clicked more than once, or recognition could have been
  // ended due to other reasons before the user click was processed.
  if (HasPendingRequest(caller_id)) {
    // If this is an ongoing recording, abort it since user has switched focus.
    // Otherwise recognition has started and keep that going so user can start
    // speaking to another element while this gets the results in parallel.
    if (recording_caller_id_ == caller_id)
      CancelRecognitionAndInformDelegate(caller_id);
  }
}

void SpeechInputManagerImpl::ShowErrorMessage(
    int render_process_id, int render_view_id,
    const string16& message) {
  ChromeThread::PostTask(
      ChromeThread::UI, FROM_HERE,
      NewRunnableFunction(&SpeechInputManagerImpl::ShowErrorMessageInUIThread,
                          render_process_id, render_view_id, message));
}

void SpeechInputManagerImpl::ShowErrorMessageInUIThread(
    int render_process_id, int render_view_id,
    const string16& message) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  TabContents* tab = tab_util::GetTabContentsByID(render_process_id,
                                                  render_view_id);
  if (tab)  // Check in case the tab was closed before we got this request.
    tab->AddInfoBar(new SimpleAlertInfoBarDelegate(tab, message, NULL, true));
}

}  // namespace speech_input