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
|
// 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_bubble_controller.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"
#include "gfx/rect.h"
namespace speech_input {
SpeechInputBubbleController::SpeechInputBubbleController(Delegate* delegate)
: delegate_(delegate),
current_bubble_caller_id_(0),
registrar_(new NotificationRegistrar) {
}
SpeechInputBubbleController::~SpeechInputBubbleController() {
DCHECK(bubbles_.size() == 0);
}
void SpeechInputBubbleController::CreateBubble(int caller_id,
int render_process_id,
int render_view_id,
const gfx::Rect& element_rect) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this, &SpeechInputBubbleController::CreateBubble,
caller_id, render_process_id, render_view_id,
element_rect));
return;
}
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
TabContents* tab_contents = tab_util::GetTabContentsByID(render_process_id,
render_view_id);
DCHECK_EQ(0u, bubbles_.count(caller_id));
SpeechInputBubble* bubble = SpeechInputBubble::Create(tab_contents, this,
element_rect);
if (!bubble) // could be null if tab or display rect were invalid.
return;
bubbles_[caller_id] = bubble;
UpdateTabContentsSubscription(caller_id, BUBBLE_ADDED);
}
void SpeechInputBubbleController::CloseBubble(int caller_id) {
ProcessRequestInUiThread(caller_id, REQUEST_CLOSE, string16(), 0);
}
void SpeechInputBubbleController::SetBubbleRecordingMode(int caller_id) {
ProcessRequestInUiThread(caller_id, REQUEST_SET_RECORDING_MODE,
string16(), 0);
}
void SpeechInputBubbleController::SetBubbleRecognizingMode(int caller_id) {
ProcessRequestInUiThread(caller_id, REQUEST_SET_RECOGNIZING_MODE,
string16(), 0);
}
void SpeechInputBubbleController::SetBubbleInputVolume(int caller_id,
float volume) {
ProcessRequestInUiThread(caller_id, REQUEST_SET_INPUT_VOLUME, string16(),
volume);
}
void SpeechInputBubbleController::SetBubbleMessage(int caller_id,
const string16& text) {
ProcessRequestInUiThread(caller_id, REQUEST_SET_MESSAGE, text, 0);
}
void SpeechInputBubbleController::UpdateTabContentsSubscription(
int caller_id, ManageSubscriptionAction action) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// If there are any other bubbles existing for the same TabContents, we would
// have subscribed to tab close notifications on their behalf and we need to
// stay registered. So we don't change the subscription in such cases.
TabContents* tab_contents = bubbles_[caller_id]->tab_contents();
for (BubbleCallerIdMap::iterator iter = bubbles_.begin();
iter != bubbles_.end(); ++iter) {
if (iter->second->tab_contents() == tab_contents &&
iter->first != caller_id) {
// At least one other bubble exists for the same TabContents. So don't
// make any change to the subscription.
return;
}
}
if (action == BUBBLE_ADDED) {
registrar_->Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
Source<TabContents>(tab_contents));
} else {
registrar_->Remove(this, NotificationType::TAB_CONTENTS_DESTROYED,
Source<TabContents>(tab_contents));
}
}
void SpeechInputBubbleController::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::TAB_CONTENTS_DESTROYED) {
// Cancel all bubbles and active recognition sessions for this tab.
TabContents* tab_contents = Source<TabContents>(source).ptr();
BubbleCallerIdMap::iterator iter = bubbles_.begin();
while (iter != bubbles_.end()) {
if (iter->second->tab_contents() == tab_contents) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
this,
&SpeechInputBubbleController::InvokeDelegateButtonClicked,
iter->first, SpeechInputBubble::BUTTON_CANCEL));
CloseBubble(iter->first);
// We expect to have a very small number of items in this map so
// redo-ing from start is ok.
iter = bubbles_.begin();
} else {
++iter;
}
}
} else {
NOTREACHED() << "Unknown notification";
}
}
void SpeechInputBubbleController::ProcessRequestInUiThread(
int caller_id, RequestType type, const string16& text, float volume) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod(
this, &SpeechInputBubbleController::ProcessRequestInUiThread,
caller_id, type, text, volume));
return;
}
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// The bubble may have been closed before we got a chance to process this
// request. So check before proceeding.
if (!bubbles_.count(caller_id))
return;
bool change_active_bubble = (type == REQUEST_SET_RECORDING_MODE ||
type == REQUEST_SET_MESSAGE);
if (change_active_bubble) {
if (current_bubble_caller_id_ && current_bubble_caller_id_ != caller_id)
bubbles_[current_bubble_caller_id_]->Hide();
current_bubble_caller_id_ = caller_id;
}
SpeechInputBubble* bubble = bubbles_[caller_id];
switch (type) {
case REQUEST_SET_RECORDING_MODE:
bubble->SetRecordingMode();
break;
case REQUEST_SET_RECOGNIZING_MODE:
bubble->SetRecognizingMode();
break;
case REQUEST_SET_MESSAGE:
bubble->SetMessage(text);
break;
case REQUEST_SET_INPUT_VOLUME:
bubble->SetInputVolume(volume);
break;
case REQUEST_CLOSE:
if (current_bubble_caller_id_ == caller_id)
current_bubble_caller_id_ = 0;
UpdateTabContentsSubscription(caller_id, BUBBLE_REMOVED);
delete bubble;
bubbles_.erase(caller_id);
break;
default:
NOTREACHED();
break;
}
if (change_active_bubble)
bubble->Show();
}
void SpeechInputBubbleController::InfoBubbleButtonClicked(
SpeechInputBubble::Button button) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(current_bubble_caller_id_);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
this,
&SpeechInputBubbleController::InvokeDelegateButtonClicked,
current_bubble_caller_id_, button));
}
void SpeechInputBubbleController::InfoBubbleFocusChanged() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(current_bubble_caller_id_);
int old_bubble_caller_id = current_bubble_caller_id_;
current_bubble_caller_id_ = 0;
bubbles_[old_bubble_caller_id]->Hide();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
this,
&SpeechInputBubbleController::InvokeDelegateFocusChanged,
old_bubble_caller_id));
}
void SpeechInputBubbleController::InvokeDelegateButtonClicked(
int caller_id, SpeechInputBubble::Button button) {
delegate_->InfoBubbleButtonClicked(caller_id, button);
}
void SpeechInputBubbleController::InvokeDelegateFocusChanged(int caller_id) {
delegate_->InfoBubbleFocusChanged(caller_id);
}
} // namespace speech_input
|