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
|
// Copyright (c) 2012 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_recognition_bubble_controller.h"
#include "base/bind.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
using content::BrowserThread;
using content::WebContents;
namespace {
const int kInvalidSessionId = 0;
}
namespace speech {
SpeechRecognitionBubbleController::SpeechRecognitionBubbleController(
Delegate* delegate)
: delegate_(delegate),
current_bubble_session_id_(kInvalidSessionId),
current_bubble_render_process_id_(0),
current_bubble_render_view_id_(0),
last_request_issued_(REQUEST_CLOSE) {
}
SpeechRecognitionBubbleController::~SpeechRecognitionBubbleController() {
DCHECK_EQ(kInvalidSessionId, current_bubble_session_id_);
}
void SpeechRecognitionBubbleController::CreateBubble(
int session_id,
int render_process_id,
int render_view_id,
const gfx::Rect& element_rect) {
current_bubble_session_id_ = session_id;
current_bubble_render_process_id_ = render_process_id;
current_bubble_render_view_id_ = render_view_id;
UIRequest request(REQUEST_CREATE);
request.render_process_id = render_process_id;
request.render_view_id = render_view_id;
request.element_rect = element_rect;
ProcessRequestInUiThread(request);
}
void SpeechRecognitionBubbleController::SetBubbleRecordingMode() {
ProcessRequestInUiThread(UIRequest(REQUEST_SET_RECORDING_MODE));
}
void SpeechRecognitionBubbleController::SetBubbleRecognizingMode() {
ProcessRequestInUiThread(UIRequest(REQUEST_SET_RECOGNIZING_MODE));
}
void SpeechRecognitionBubbleController::SetBubbleMessage(const string16& text) {
UIRequest request(REQUEST_SET_MESSAGE);
request.message = text;
ProcessRequestInUiThread(request);
}
bool SpeechRecognitionBubbleController::IsShowingMessage() const {
return last_request_issued_ == REQUEST_SET_MESSAGE;
}
void SpeechRecognitionBubbleController::SetBubbleInputVolume(
float volume,
float noise_volume) {
UIRequest request(REQUEST_SET_INPUT_VOLUME);
request.volume = volume;
request.noise_volume = noise_volume;
ProcessRequestInUiThread(request);
}
void SpeechRecognitionBubbleController::CloseBubble() {
current_bubble_session_id_ = kInvalidSessionId;
ProcessRequestInUiThread(UIRequest(REQUEST_CLOSE));
}
int SpeechRecognitionBubbleController::GetActiveSessionID() const {
return current_bubble_session_id_;
}
bool SpeechRecognitionBubbleController::IsShowingBubbleForRenderView(
int render_process_id,
int render_view_id) {
return (current_bubble_session_id_ != kInvalidSessionId) &&
(current_bubble_render_process_id_ == render_process_id) &&
(current_bubble_render_view_id_ == render_view_id);
}
void SpeechRecognitionBubbleController::InfoBubbleButtonClicked(
SpeechRecognitionBubble::Button button) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(
&SpeechRecognitionBubbleController::InvokeDelegateButtonClicked, this,
button));
}
void SpeechRecognitionBubbleController::InfoBubbleFocusChanged() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&SpeechRecognitionBubbleController::InvokeDelegateFocusChanged,
this));
}
void SpeechRecognitionBubbleController::InvokeDelegateButtonClicked(
SpeechRecognitionBubble::Button button) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK_NE(kInvalidSessionId, current_bubble_session_id_);
delegate_->InfoBubbleButtonClicked(current_bubble_session_id_, button);
}
void SpeechRecognitionBubbleController::InvokeDelegateFocusChanged() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK_NE(kInvalidSessionId, current_bubble_session_id_);
delegate_->InfoBubbleFocusChanged(current_bubble_session_id_);
}
void SpeechRecognitionBubbleController::ProcessRequestInUiThread(
const UIRequest& request) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
last_request_issued_ = request.type;
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&SpeechRecognitionBubbleController::ProcessRequestInUiThread,
this, request));
return;
}
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
switch (request.type) {
case REQUEST_CREATE:
bubble_.reset(SpeechRecognitionBubble::Create(
tab_util::GetWebContentsByID(request.render_process_id,
request.render_view_id),
this, request.element_rect));
if (!bubble_.get()) {
// Could be null if tab or display rect were invalid.
// Simulate the cancel button being clicked to inform the delegate.
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(
&SpeechRecognitionBubbleController::InvokeDelegateButtonClicked,
this, SpeechRecognitionBubble::BUTTON_CANCEL));
return;
}
bubble_->Show();
bubble_->SetWarmUpMode();
break;
case REQUEST_SET_RECORDING_MODE:
if (!bubble_.get()) {
// We've seen this on crash/ but don't yet understand why this happen.
// TODO(tommi): It would be nice to figure this out properly and fix
// but since this feature is being deprecated, removing the code
// altogether is probably a simpler and more permanent fix :)
DLOG(ERROR) << "NULL bubble in REQUEST_SET_RECORDING_MODE!";
} else {
bubble_->SetRecordingMode();
}
break;
case REQUEST_SET_RECOGNIZING_MODE:
DCHECK(bubble_.get());
bubble_->SetRecognizingMode();
break;
case REQUEST_SET_MESSAGE:
DCHECK(bubble_.get());
bubble_->SetMessage(request.message);
break;
case REQUEST_SET_INPUT_VOLUME:
DCHECK(bubble_.get());
bubble_->SetInputVolume(request.volume, request.noise_volume);
break;
case REQUEST_CLOSE:
bubble_.reset();
break;
default:
NOTREACHED();
break;
}
}
SpeechRecognitionBubbleController::UIRequest::UIRequest(RequestType type_value)
: type(type_value),
volume(0.0F),
noise_volume(0.0F),
render_process_id(0),
render_view_id(0) {
}
SpeechRecognitionBubbleController::UIRequest::~UIRequest() {
}
} // namespace speech
|