Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/speech/speech_input_bubble_controller.h" | 5 #include "chrome/browser/speech/speech_input_bubble_controller.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_thread.h" | 7 #include "chrome/browser/chrome_thread.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents.h" | 8 #include "chrome/browser/tab_contents/tab_contents.h" |
| 9 #include "chrome/browser/tab_contents/tab_util.h" | 9 #include "chrome/browser/tab_contents/tab_util.h" |
| 10 #include "gfx/rect.h" | 10 #include "gfx/rect.h" |
| 11 | 11 |
| 12 namespace speech_input { | 12 namespace speech_input { |
| 13 | 13 |
| 14 SpeechInputBubbleController::SpeechInputBubbleController(Delegate* delegate) | 14 SpeechInputBubbleController::SpeechInputBubbleController(Delegate* delegate) |
| 15 : delegate_(delegate) { | 15 : delegate_(delegate), |
| 16 current_bubble_caller_id_(0) { | |
| 17 } | |
| 18 | |
| 19 SpeechInputBubbleController::~SpeechInputBubbleController() { | |
| 20 DCHECK(bubbles_.size() == 0); | |
| 16 } | 21 } |
| 17 | 22 |
| 18 void SpeechInputBubbleController::CreateBubble(int caller_id, | 23 void SpeechInputBubbleController::CreateBubble(int caller_id, |
| 19 int render_process_id, | 24 int render_process_id, |
| 20 int render_view_id, | 25 int render_view_id, |
| 21 const gfx::Rect& element_rect) { | 26 const gfx::Rect& element_rect) { |
| 22 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 27 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 23 ChromeThread::PostTask( | 28 ChromeThread::PostTask( |
| 24 ChromeThread::UI, FROM_HERE, | 29 ChromeThread::UI, FROM_HERE, |
| 25 NewRunnableMethod(this, &SpeechInputBubbleController::CreateBubble, | 30 NewRunnableMethod(this, &SpeechInputBubbleController::CreateBubble, |
| 26 caller_id, render_process_id, render_view_id, | 31 caller_id, render_process_id, render_view_id, |
| 27 element_rect)); | 32 element_rect)); |
| 28 return; | 33 return; |
| 29 } | 34 } |
| 30 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 35 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 31 TabContents* tab_contents = tab_util::GetTabContentsByID(render_process_id, | 36 TabContents* tab_contents = tab_util::GetTabContentsByID(render_process_id, |
| 32 render_view_id); | 37 render_view_id); |
| 33 | 38 |
| 34 DCHECK(!bubble_.get()); | 39 DCHECK_EQ(0u, bubbles_.count(caller_id)); |
| 35 bubble_.reset(SpeechInputBubble::Create(tab_contents, this, element_rect)); | 40 SpeechInputBubble* bubble = SpeechInputBubble::Create(tab_contents, this, |
| 36 if (!bubble_.get()) // could be null if tab or display rect were invalid. | 41 element_rect); |
| 42 if (!bubble) // could be null if tab or display rect were invalid. | |
| 37 return; | 43 return; |
| 38 | 44 |
| 39 current_bubble_caller_id_ = caller_id; | 45 bubbles_[caller_id] = bubble; |
| 40 } | 46 } |
| 41 | 47 |
| 42 void SpeechInputBubbleController::CloseBubble(int caller_id) { | 48 void SpeechInputBubbleController::CloseBubble(int caller_id) { |
| 43 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 49 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 44 ChromeThread::PostTask( | 50 ChromeThread::PostTask( |
| 45 ChromeThread::UI, FROM_HERE, | 51 ChromeThread::UI, FROM_HERE, |
| 46 NewRunnableMethod(this, &SpeechInputBubbleController::CloseBubble, | 52 NewRunnableMethod(this, &SpeechInputBubbleController::CloseBubble, |
| 47 caller_id)); | 53 caller_id)); |
| 48 return; | 54 return; |
| 49 } | 55 } |
| 50 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 56 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 51 if (current_bubble_caller_id_ != caller_id) | |
| 52 return; | |
| 53 | 57 |
| 54 current_bubble_caller_id_ = 0; | 58 if (current_bubble_caller_id_ == caller_id) |
| 55 | 59 current_bubble_caller_id_ = 0; |
| 56 DCHECK(bubble_.get()); | 60 delete bubbles_[caller_id]; |
| 57 bubble_.reset(); | 61 bubbles_.erase(caller_id); |
| 58 } | 62 } |
| 59 | 63 |
| 60 void SpeechInputBubbleController::SetBubbleToRecognizingMode(int caller_id) { | 64 void SpeechInputBubbleController::SetBubbleRecordingMode(int caller_id) { |
| 61 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 65 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 62 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( | 66 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( |
| 63 this, &SpeechInputBubbleController::SetBubbleToRecognizingMode, | 67 this, &SpeechInputBubbleController::SetBubbleRecordingMode, |
| 68 caller_id)); | |
| 69 return; | |
| 70 } | |
|
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
| |
| 71 SetBubbleRecordingModeOrMessage(caller_id, string16()); | |
| 72 } | |
| 73 | |
| 74 void SpeechInputBubbleController::SetBubbleRecognizingMode(int caller_id) { | |
| 75 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | |
| 76 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( | |
| 77 this, &SpeechInputBubbleController::SetBubbleRecognizingMode, | |
| 64 caller_id)); | 78 caller_id)); |
| 65 return; | 79 return; |
| 66 } | 80 } |
| 67 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 81 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 68 if (current_bubble_caller_id_ != caller_id) | 82 // The bubble may have been closed before we got a chance to process this |
| 83 // request. So check before proceeding. | |
| 84 if (!bubbles_.count(caller_id)) | |
| 69 return; | 85 return; |
| 70 | 86 |
| 71 DCHECK(bubble_.get()); | 87 bubbles_[caller_id]->SetRecognizingMode(); |
| 72 bubble_->SetRecognizingMode(); | |
| 73 } | 88 } |
| 74 | 89 |
| 75 void SpeechInputBubbleController::RecognitionCancelled() { | 90 void SpeechInputBubbleController::SetBubbleMessage(int caller_id, |
| 91 const string16& text) { | |
| 92 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | |
| 93 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod( | |
| 94 this, &SpeechInputBubbleController::SetBubbleMessage, | |
| 95 caller_id, text)); | |
| 96 return; | |
| 97 } | |
| 98 SetBubbleRecordingModeOrMessage(caller_id, text); | |
| 99 } | |
| 100 | |
| 101 void SpeechInputBubbleController::SetBubbleRecordingModeOrMessage( | |
| 102 int caller_id, const string16& text) { | |
| 76 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 103 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 104 // The bubble may have been closed before we got a chance to process this | |
| 105 // request. So check before proceeding. | |
| 106 if (!bubbles_.count(caller_id)) | |
| 107 return; | |
| 77 | 108 |
| 78 int old_bubble_caller_id = current_bubble_caller_id_; | 109 if (current_bubble_caller_id_ && current_bubble_caller_id_ != caller_id) |
| 79 current_bubble_caller_id_ = 0; | 110 bubbles_[current_bubble_caller_id_]->Hide(); |
| 80 bubble_.reset(); | 111 |
| 112 current_bubble_caller_id_ = caller_id; | |
| 113 SpeechInputBubble* bubble = bubbles_[caller_id]; | |
| 114 if (text.empty()) { | |
| 115 bubble->SetRecordingMode(); | |
| 116 } else { | |
| 117 bubble->SetMessage(text); | |
| 118 } | |
| 119 bubble->Show(); | |
| 120 } | |
| 121 | |
| 122 void SpeechInputBubbleController::InfoBubbleButtonClicked( | |
| 123 SpeechInputBubble::Button button) { | |
| 124 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 125 DCHECK(current_bubble_caller_id_); | |
| 81 | 126 |
| 82 ChromeThread::PostTask( | 127 ChromeThread::PostTask( |
| 83 ChromeThread::IO, FROM_HERE, | 128 ChromeThread::IO, FROM_HERE, |
| 84 NewRunnableMethod( | 129 NewRunnableMethod( |
| 85 this, | 130 this, |
| 86 &SpeechInputBubbleController::InvokeDelegateRecognitionCancelled, | 131 &SpeechInputBubbleController::InvokeDelegateButtonClicked, |
| 87 old_bubble_caller_id)); | 132 current_bubble_caller_id_, button)); |
| 88 } | 133 } |
| 89 | 134 |
| 90 void SpeechInputBubbleController::InfoBubbleClosed() { | 135 void SpeechInputBubbleController::InfoBubbleFocusChanged() { |
| 91 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 136 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 137 DCHECK(current_bubble_caller_id_); | |
| 92 | 138 |
| 93 int old_bubble_caller_id = current_bubble_caller_id_; | 139 int old_bubble_caller_id = current_bubble_caller_id_; |
| 94 current_bubble_caller_id_ = 0; | 140 current_bubble_caller_id_ = 0; |
| 95 bubble_.reset(); | 141 bubbles_[old_bubble_caller_id]->Hide(); |
| 96 | 142 |
| 97 ChromeThread::PostTask( | 143 ChromeThread::PostTask( |
| 98 ChromeThread::IO, FROM_HERE, | 144 ChromeThread::IO, FROM_HERE, |
| 99 NewRunnableMethod( | 145 NewRunnableMethod( |
| 100 this, | 146 this, |
| 101 &SpeechInputBubbleController::InvokeDelegateFocusChanged, | 147 &SpeechInputBubbleController::InvokeDelegateFocusChanged, |
| 102 old_bubble_caller_id)); | 148 old_bubble_caller_id)); |
| 103 } | 149 } |
| 104 | 150 |
| 105 void SpeechInputBubbleController::InvokeDelegateRecognitionCancelled( | 151 void SpeechInputBubbleController::InvokeDelegateButtonClicked( |
| 106 int caller_id) { | 152 int caller_id, SpeechInputBubble::Button button) { |
| 107 delegate_->RecognitionCancelled(caller_id); | 153 delegate_->InfoBubbleButtonClicked(caller_id, button); |
| 108 } | 154 } |
| 109 | 155 |
| 110 void SpeechInputBubbleController::InvokeDelegateFocusChanged(int caller_id) { | 156 void SpeechInputBubbleController::InvokeDelegateFocusChanged(int caller_id) { |
| 111 delegate_->SpeechInputFocusChanged(caller_id); | 157 delegate_->InfoBubbleFocusChanged(caller_id); |
| 112 } | 158 } |
| 113 | 159 |
| 114 } // namespace speech_input | 160 } // namespace speech_input |
| OLD | NEW |