1 // Copyright (c) 2012 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.
5 #include "chrome/browser/speech/speech_recognition_bubble_controller.h"
8 #include "chrome/browser/tab_contents/tab_util.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/notification_registrar.h"
11 #include "content/public/browser/notification_source.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
17 using content::BrowserThread
;
18 using content::WebContents
;
21 const int kInvalidSessionId
= 0;
26 SpeechRecognitionBubbleController::SpeechRecognitionBubbleController(
28 : delegate_(delegate
),
29 current_bubble_session_id_(kInvalidSessionId
),
30 current_bubble_render_process_id_(0),
31 current_bubble_render_view_id_(0),
32 last_request_issued_(REQUEST_CLOSE
) {
35 SpeechRecognitionBubbleController::~SpeechRecognitionBubbleController() {
36 DCHECK_EQ(kInvalidSessionId
, current_bubble_session_id_
);
39 void SpeechRecognitionBubbleController::CreateBubble(
41 int render_process_id
,
43 const gfx::Rect
& element_rect
) {
44 current_bubble_session_id_
= session_id
;
45 current_bubble_render_process_id_
= render_process_id
;
46 current_bubble_render_view_id_
= render_view_id
;
48 UIRequest
request(REQUEST_CREATE
);
49 request
.render_process_id
= render_process_id
;
50 request
.render_view_id
= render_view_id
;
51 request
.element_rect
= element_rect
;
52 ProcessRequestInUiThread(request
);
55 void SpeechRecognitionBubbleController::SetBubbleRecordingMode() {
56 ProcessRequestInUiThread(UIRequest(REQUEST_SET_RECORDING_MODE
));
59 void SpeechRecognitionBubbleController::SetBubbleRecognizingMode() {
60 ProcessRequestInUiThread(UIRequest(REQUEST_SET_RECOGNIZING_MODE
));
63 void SpeechRecognitionBubbleController::SetBubbleMessage(const string16
& text
) {
64 UIRequest
request(REQUEST_SET_MESSAGE
);
65 request
.message
= text
;
66 ProcessRequestInUiThread(request
);
69 bool SpeechRecognitionBubbleController::IsShowingMessage() const {
70 return last_request_issued_
== REQUEST_SET_MESSAGE
;
73 void SpeechRecognitionBubbleController::SetBubbleInputVolume(
76 UIRequest
request(REQUEST_SET_INPUT_VOLUME
);
77 request
.volume
= volume
;
78 request
.noise_volume
= noise_volume
;
79 ProcessRequestInUiThread(request
);
82 void SpeechRecognitionBubbleController::CloseBubble() {
83 current_bubble_session_id_
= kInvalidSessionId
;
84 ProcessRequestInUiThread(UIRequest(REQUEST_CLOSE
));
87 int SpeechRecognitionBubbleController::GetActiveSessionID() const {
88 return current_bubble_session_id_
;
91 bool SpeechRecognitionBubbleController::IsShowingBubbleForRenderView(
92 int render_process_id
,
94 return (current_bubble_session_id_
!= kInvalidSessionId
) &&
95 (current_bubble_render_process_id_
== render_process_id
) &&
96 (current_bubble_render_view_id_
== render_view_id
);
99 void SpeechRecognitionBubbleController::InfoBubbleButtonClicked(
100 SpeechRecognitionBubble::Button button
) {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
102 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
104 &SpeechRecognitionBubbleController::InvokeDelegateButtonClicked
, this,
108 void SpeechRecognitionBubbleController::InfoBubbleFocusChanged() {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
110 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
111 base::Bind(&SpeechRecognitionBubbleController::InvokeDelegateFocusChanged
,
115 void SpeechRecognitionBubbleController::InvokeDelegateButtonClicked(
116 SpeechRecognitionBubble::Button button
) {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
118 DCHECK_NE(kInvalidSessionId
, current_bubble_session_id_
);
119 delegate_
->InfoBubbleButtonClicked(current_bubble_session_id_
, button
);
122 void SpeechRecognitionBubbleController::InvokeDelegateFocusChanged() {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
124 DCHECK_NE(kInvalidSessionId
, current_bubble_session_id_
);
125 delegate_
->InfoBubbleFocusChanged(current_bubble_session_id_
);
128 void SpeechRecognitionBubbleController::ProcessRequestInUiThread(
129 const UIRequest
& request
) {
130 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
131 last_request_issued_
= request
.type
;
132 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
133 base::Bind(&SpeechRecognitionBubbleController::ProcessRequestInUiThread
,
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
140 switch (request
.type
) {
142 bubble_
.reset(SpeechRecognitionBubble::Create(
143 tab_util::GetWebContentsByID(request
.render_process_id
,
144 request
.render_view_id
),
145 this, request
.element_rect
));
147 if (!bubble_
.get()) {
148 // Could be null if tab or display rect were invalid.
149 // Simulate the cancel button being clicked to inform the delegate.
150 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
152 &SpeechRecognitionBubbleController::InvokeDelegateButtonClicked
,
153 this, SpeechRecognitionBubble::BUTTON_CANCEL
));
157 bubble_
->SetWarmUpMode();
159 case REQUEST_SET_RECORDING_MODE
:
160 DCHECK(bubble_
.get());
161 bubble_
->SetRecordingMode();
163 case REQUEST_SET_RECOGNIZING_MODE
:
164 DCHECK(bubble_
.get());
165 bubble_
->SetRecognizingMode();
167 case REQUEST_SET_MESSAGE
:
168 DCHECK(bubble_
.get());
169 bubble_
->SetMessage(request
.message
);
171 case REQUEST_SET_INPUT_VOLUME
:
172 DCHECK(bubble_
.get());
173 bubble_
->SetInputVolume(request
.volume
, request
.noise_volume
);
184 SpeechRecognitionBubbleController::UIRequest::UIRequest(RequestType type_value
)
188 render_process_id(0),
192 SpeechRecognitionBubbleController::UIRequest::~UIRequest() {
195 } // namespace speech