Chromium Code Reviews| Index: chrome/browser/speech/speech_input_bubble_controller.cc |
| diff --git a/chrome/browser/speech/speech_input_bubble_controller.cc b/chrome/browser/speech/speech_input_bubble_controller.cc |
| index 6b7c6508b6a67af7468be90e56682ec941a4c65e..08647d491662573e31c09f97bd94a6153474a799 100644 |
| --- a/chrome/browser/speech/speech_input_bubble_controller.cc |
| +++ b/chrome/browser/speech/speech_input_bubble_controller.cc |
| @@ -12,7 +12,12 @@ |
| namespace speech_input { |
| SpeechInputBubbleController::SpeechInputBubbleController(Delegate* delegate) |
| - : delegate_(delegate) { |
| + : delegate_(delegate), |
| + current_bubble_caller_id_(0) { |
| +} |
| + |
| +SpeechInputBubbleController::~SpeechInputBubbleController() { |
| + DCHECK(bubbles_.size() == 0); |
| } |
| void SpeechInputBubbleController::CreateBubble(int caller_id, |
| @@ -31,12 +36,13 @@ void SpeechInputBubbleController::CreateBubble(int caller_id, |
| TabContents* tab_contents = tab_util::GetTabContentsByID(render_process_id, |
| render_view_id); |
| - DCHECK(!bubble_.get()); |
| - bubble_.reset(SpeechInputBubble::Create(tab_contents, this, element_rect)); |
| - if (!bubble_.get()) // could be null if tab or display rect were invalid. |
| + 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; |
| - current_bubble_caller_id_ = caller_id; |
| + bubbles_[caller_id] = bubble; |
| } |
| void SpeechInputBubbleController::CloseBubble(int caller_id) { |
| @@ -48,51 +54,91 @@ void SpeechInputBubbleController::CloseBubble(int caller_id) { |
| return; |
| } |
| DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| - if (current_bubble_caller_id_ != caller_id) |
| - return; |
| - current_bubble_caller_id_ = 0; |
| + if (current_bubble_caller_id_ == caller_id) |
| + current_bubble_caller_id_ = 0; |
| + delete bubbles_[caller_id]; |
| + bubbles_.erase(caller_id); |
| +} |
| - DCHECK(bubble_.get()); |
| - bubble_.reset(); |
| +void SpeechInputBubbleController::SetBubbleRecordingMode(int caller_id) { |
| + if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| + ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( |
| + this, &SpeechInputBubbleController::SetBubbleRecordingMode, |
| + caller_id)); |
| + return; |
| + } |
|
joth
2010/09/10 17:08:40
you could factor this into SetBubbleRecordingModeO
Satish
2010/09/10 17:12:11
I want readers of this code to recognise that this
|
| + SetBubbleRecordingModeOrMessage(caller_id, string16()); |
| } |
| -void SpeechInputBubbleController::SetBubbleToRecognizingMode(int caller_id) { |
| +void SpeechInputBubbleController::SetBubbleRecognizingMode(int caller_id) { |
| if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( |
| - this, &SpeechInputBubbleController::SetBubbleToRecognizingMode, |
| + this, &SpeechInputBubbleController::SetBubbleRecognizingMode, |
| caller_id)); |
| return; |
| } |
| DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| - if (current_bubble_caller_id_ != caller_id) |
| + // 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; |
| - DCHECK(bubble_.get()); |
| - bubble_->SetRecognizingMode(); |
| + bubbles_[caller_id]->SetRecognizingMode(); |
| } |
| -void SpeechInputBubbleController::RecognitionCancelled() { |
| +void SpeechInputBubbleController::SetBubbleMessage(int caller_id, |
| + const string16& text) { |
| + if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| + ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( |
| + this, &SpeechInputBubbleController::SetBubbleMessage, |
| + caller_id, text)); |
| + return; |
| + } |
| + SetBubbleRecordingModeOrMessage(caller_id, text); |
| +} |
| + |
| +void SpeechInputBubbleController::SetBubbleRecordingModeOrMessage( |
| + int caller_id, const string16& text) { |
| DCHECK(ChromeThread::CurrentlyOn(ChromeThread::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; |
| - int old_bubble_caller_id = current_bubble_caller_id_; |
| - current_bubble_caller_id_ = 0; |
| - bubble_.reset(); |
| + 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]; |
| + if (text.empty()) { |
| + bubble->SetRecordingMode(); |
| + } else { |
| + bubble->SetMessage(text); |
| + } |
| + bubble->Show(); |
| +} |
| + |
| +void SpeechInputBubbleController::InfoBubbleButtonClicked( |
| + SpeechInputBubble::Button button) { |
| + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| + DCHECK(current_bubble_caller_id_); |
| ChromeThread::PostTask( |
| ChromeThread::IO, FROM_HERE, |
| NewRunnableMethod( |
| this, |
| - &SpeechInputBubbleController::InvokeDelegateRecognitionCancelled, |
| - old_bubble_caller_id)); |
| + &SpeechInputBubbleController::InvokeDelegateButtonClicked, |
| + current_bubble_caller_id_, button)); |
| } |
| -void SpeechInputBubbleController::InfoBubbleClosed() { |
| +void SpeechInputBubbleController::InfoBubbleFocusChanged() { |
| DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| + DCHECK(current_bubble_caller_id_); |
| int old_bubble_caller_id = current_bubble_caller_id_; |
| current_bubble_caller_id_ = 0; |
| - bubble_.reset(); |
| + bubbles_[old_bubble_caller_id]->Hide(); |
| ChromeThread::PostTask( |
| ChromeThread::IO, FROM_HERE, |
| @@ -102,13 +148,13 @@ void SpeechInputBubbleController::InfoBubbleClosed() { |
| old_bubble_caller_id)); |
| } |
| -void SpeechInputBubbleController::InvokeDelegateRecognitionCancelled( |
| - int caller_id) { |
| - delegate_->RecognitionCancelled(caller_id); |
| +void SpeechInputBubbleController::InvokeDelegateButtonClicked( |
| + int caller_id, SpeechInputBubble::Button button) { |
| + delegate_->InfoBubbleButtonClicked(caller_id, button); |
| } |
| void SpeechInputBubbleController::InvokeDelegateFocusChanged(int caller_id) { |
| - delegate_->SpeechInputFocusChanged(caller_id); |
| + delegate_->InfoBubbleFocusChanged(caller_id); |
| } |
| } // namespace speech_input |