| 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_dispatcher_host.h" | 5 #include "chrome/browser/speech/speech_input_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "chrome/browser/renderer_host/render_process_host.h" | |
| 9 #include "chrome/browser/renderer_host/render_view_host.h" | |
| 10 #include "chrome/browser/renderer_host/render_view_host_notification_task.h" | |
| 11 #include "chrome/browser/renderer_host/resource_message_filter.h" | |
| 12 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
| 13 | 9 |
| 14 namespace speech_input { | 10 namespace speech_input { |
| 15 | 11 |
| 16 //----------------------------- SpeechInputCallers ----------------------------- | 12 //----------------------------- SpeechInputCallers ----------------------------- |
| 17 | 13 |
| 18 // A singleton class to map the tuple | 14 // A singleton class to map the tuple |
| 19 // (render-process-id, render-view-id, requestid) to a single ID which is passed | 15 // (render-process-id, render-view-id, requestid) to a single ID which is passed |
| 20 // through rest of the speech code. | 16 // through rest of the speech code. |
| 21 class SpeechInputDispatcherHost::SpeechInputCallers { | 17 class SpeechInputDispatcherHost::SpeechInputCallers { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 97 |
| 102 int SpeechInputDispatcherHost::SpeechInputCallers::request_id(int id) { | 98 int SpeechInputDispatcherHost::SpeechInputCallers::request_id(int id) { |
| 103 return callers_[id].request_id; | 99 return callers_[id].request_id; |
| 104 } | 100 } |
| 105 | 101 |
| 106 //-------------------------- SpeechInputDispatcherHost ------------------------- | 102 //-------------------------- SpeechInputDispatcherHost ------------------------- |
| 107 | 103 |
| 108 SpeechInputManager::AccessorMethod* | 104 SpeechInputManager::AccessorMethod* |
| 109 SpeechInputDispatcherHost::manager_accessor_ = &SpeechInputManager::Get; | 105 SpeechInputDispatcherHost::manager_accessor_ = &SpeechInputManager::Get; |
| 110 | 106 |
| 111 SpeechInputDispatcherHost::SpeechInputDispatcherHost( | 107 SpeechInputDispatcherHost::SpeechInputDispatcherHost(int render_process_id) |
| 112 int resource_message_filter_process_id) | 108 : render_process_id_(render_process_id) { |
| 113 : resource_message_filter_process_id_(resource_message_filter_process_id) { | 109 // This is initialized by Browser. Do not add any non-trivial |
| 114 // This is initialized by ResourceMessageFilter. Do not add any non-trivial | |
| 115 // initialization here, instead do it lazily when required (e.g. see the | 110 // initialization here, instead do it lazily when required (e.g. see the |
| 116 // method |manager()|) or add an Init() method. | 111 // method |manager()|) or add an Init() method. |
| 117 } | 112 } |
| 118 | 113 |
| 119 SpeechInputDispatcherHost::~SpeechInputDispatcherHost() { | 114 SpeechInputDispatcherHost::~SpeechInputDispatcherHost() { |
| 120 } | 115 } |
| 121 | 116 |
| 122 SpeechInputManager* SpeechInputDispatcherHost::manager() { | 117 SpeechInputManager* SpeechInputDispatcherHost::manager() { |
| 123 return (*manager_accessor_)(); | 118 return (*manager_accessor_)(); |
| 124 } | 119 } |
| 125 | 120 |
| 126 bool SpeechInputDispatcherHost::OnMessageReceived( | 121 bool SpeechInputDispatcherHost::OnMessageReceived( |
| 127 const IPC::Message& msg, bool* msg_was_ok) { | 122 const IPC::Message& message, bool* message_was_ok) { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 129 bool handled = true; | 124 bool handled = true; |
| 130 IPC_BEGIN_MESSAGE_MAP_EX(SpeechInputDispatcherHost, msg, *msg_was_ok) | 125 IPC_BEGIN_MESSAGE_MAP_EX(SpeechInputDispatcherHost, message, *message_was_ok) |
| 131 IPC_MESSAGE_HANDLER(ViewHostMsg_SpeechInput_StartRecognition, | 126 IPC_MESSAGE_HANDLER(ViewHostMsg_SpeechInput_StartRecognition, |
| 132 OnStartRecognition) | 127 OnStartRecognition) |
| 133 IPC_MESSAGE_HANDLER(ViewHostMsg_SpeechInput_CancelRecognition, | 128 IPC_MESSAGE_HANDLER(ViewHostMsg_SpeechInput_CancelRecognition, |
| 134 OnCancelRecognition) | 129 OnCancelRecognition) |
| 135 IPC_MESSAGE_HANDLER(ViewHostMsg_SpeechInput_StopRecording, | 130 IPC_MESSAGE_HANDLER(ViewHostMsg_SpeechInput_StopRecording, |
| 136 OnStopRecording) | 131 OnStopRecording) |
| 137 IPC_MESSAGE_UNHANDLED(handled = false) | 132 IPC_MESSAGE_UNHANDLED(handled = false) |
| 138 IPC_END_MESSAGE_MAP() | 133 IPC_END_MESSAGE_MAP() |
| 139 return handled; | 134 return handled; |
| 140 } | 135 } |
| 141 | 136 |
| 142 void SpeechInputDispatcherHost::OnStartRecognition( | 137 void SpeechInputDispatcherHost::OnStartRecognition( |
| 143 int render_view_id, | 138 int render_view_id, |
| 144 int request_id, | 139 int request_id, |
| 145 const gfx::Rect& element_rect, | 140 const gfx::Rect& element_rect, |
| 146 const std::string& language, | 141 const std::string& language, |
| 147 const std::string& grammar) { | 142 const std::string& grammar) { |
| 148 int caller_id = g_speech_input_callers.Get().CreateId( | 143 int caller_id = g_speech_input_callers.Get().CreateId( |
| 149 resource_message_filter_process_id_, render_view_id, request_id); | 144 render_process_id_, render_view_id, request_id); |
| 150 manager()->StartRecognition(this, caller_id, | 145 manager()->StartRecognition(this, caller_id, |
| 151 resource_message_filter_process_id_, | 146 render_process_id_, |
| 152 render_view_id, element_rect, | 147 render_view_id, element_rect, |
| 153 language, grammar); | 148 language, grammar); |
| 154 } | 149 } |
| 155 | 150 |
| 156 void SpeechInputDispatcherHost::OnCancelRecognition(int render_view_id, | 151 void SpeechInputDispatcherHost::OnCancelRecognition(int render_view_id, |
| 157 int request_id) { | 152 int request_id) { |
| 158 int caller_id = g_speech_input_callers.Get().GetId( | 153 int caller_id = g_speech_input_callers.Get().GetId( |
| 159 resource_message_filter_process_id_, render_view_id, request_id); | 154 render_process_id_, render_view_id, request_id); |
| 160 if (caller_id) { | 155 if (caller_id) { |
| 161 manager()->CancelRecognition(caller_id); | 156 manager()->CancelRecognition(caller_id); |
| 162 // Request sequence ended so remove mapping. | 157 // Request sequence ended so remove mapping. |
| 163 g_speech_input_callers.Get().RemoveId(caller_id); | 158 g_speech_input_callers.Get().RemoveId(caller_id); |
| 164 } | 159 } |
| 165 } | 160 } |
| 166 | 161 |
| 167 void SpeechInputDispatcherHost::OnStopRecording(int render_view_id, | 162 void SpeechInputDispatcherHost::OnStopRecording(int render_view_id, |
| 168 int request_id) { | 163 int request_id) { |
| 169 int caller_id = g_speech_input_callers.Get().GetId( | 164 int caller_id = g_speech_input_callers.Get().GetId( |
| 170 resource_message_filter_process_id_, render_view_id, request_id); | 165 render_process_id_, render_view_id, request_id); |
| 171 if (caller_id) | 166 if (caller_id) |
| 172 manager()->StopRecording(caller_id); | 167 manager()->StopRecording(caller_id); |
| 173 } | 168 } |
| 174 | 169 |
| 175 void SpeechInputDispatcherHost::SendMessageToRenderView(IPC::Message* message, | |
| 176 int render_view_id) { | |
| 177 CallRenderViewHost( | |
| 178 resource_message_filter_process_id_, render_view_id, | |
| 179 &RenderViewHost::Send, message); | |
| 180 } | |
| 181 | |
| 182 void SpeechInputDispatcherHost::SetRecognitionResult( | 170 void SpeechInputDispatcherHost::SetRecognitionResult( |
| 183 int caller_id, const SpeechInputResultArray& result) { | 171 int caller_id, const SpeechInputResultArray& result) { |
| 184 VLOG(1) << "SpeechInputDispatcherHost::SetRecognitionResult enter"; | 172 VLOG(1) << "SpeechInputDispatcherHost::SetRecognitionResult enter"; |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 186 int caller_render_view_id = | 174 int caller_render_view_id = |
| 187 g_speech_input_callers.Get().render_view_id(caller_id); | 175 g_speech_input_callers.Get().render_view_id(caller_id); |
| 188 int caller_request_id = g_speech_input_callers.Get().request_id(caller_id); | 176 int caller_request_id = g_speech_input_callers.Get().request_id(caller_id); |
| 189 SendMessageToRenderView( | 177 Send(new ViewMsg_SpeechInput_SetRecognitionResult(caller_render_view_id, |
| 190 new ViewMsg_SpeechInput_SetRecognitionResult(caller_render_view_id, | 178 caller_request_id, |
| 191 caller_request_id, | 179 result)); |
| 192 result), | |
| 193 caller_render_view_id); | |
| 194 VLOG(1) << "SpeechInputDispatcherHost::SetRecognitionResult exit"; | 180 VLOG(1) << "SpeechInputDispatcherHost::SetRecognitionResult exit"; |
| 195 } | 181 } |
| 196 | 182 |
| 197 void SpeechInputDispatcherHost::DidCompleteRecording(int caller_id) { | 183 void SpeechInputDispatcherHost::DidCompleteRecording(int caller_id) { |
| 198 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecording enter"; | 184 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecording enter"; |
| 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 200 int caller_render_view_id = | 186 int caller_render_view_id = |
| 201 g_speech_input_callers.Get().render_view_id(caller_id); | 187 g_speech_input_callers.Get().render_view_id(caller_id); |
| 202 int caller_request_id = g_speech_input_callers.Get().request_id(caller_id); | 188 int caller_request_id = g_speech_input_callers.Get().request_id(caller_id); |
| 203 SendMessageToRenderView( | 189 Send(new ViewMsg_SpeechInput_RecordingComplete(caller_render_view_id, |
| 204 new ViewMsg_SpeechInput_RecordingComplete(caller_render_view_id, | 190 caller_request_id)); |
| 205 caller_request_id), | |
| 206 caller_render_view_id); | |
| 207 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecording exit"; | 191 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecording exit"; |
| 208 } | 192 } |
| 209 | 193 |
| 210 void SpeechInputDispatcherHost::DidCompleteRecognition(int caller_id) { | 194 void SpeechInputDispatcherHost::DidCompleteRecognition(int caller_id) { |
| 211 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecognition enter"; | 195 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecognition enter"; |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 213 int caller_render_view_id = | 197 int caller_render_view_id = |
| 214 g_speech_input_callers.Get().render_view_id(caller_id); | 198 g_speech_input_callers.Get().render_view_id(caller_id); |
| 215 int caller_request_id = g_speech_input_callers.Get().request_id(caller_id); | 199 int caller_request_id = g_speech_input_callers.Get().request_id(caller_id); |
| 216 SendMessageToRenderView( | 200 Send(new ViewMsg_SpeechInput_RecognitionComplete(caller_render_view_id, |
| 217 new ViewMsg_SpeechInput_RecognitionComplete(caller_render_view_id, | 201 caller_request_id)); |
| 218 caller_request_id), | |
| 219 caller_render_view_id); | |
| 220 // Request sequence ended, so remove mapping. | 202 // Request sequence ended, so remove mapping. |
| 221 g_speech_input_callers.Get().RemoveId(caller_id); | 203 g_speech_input_callers.Get().RemoveId(caller_id); |
| 222 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecognition exit"; | 204 VLOG(1) << "SpeechInputDispatcherHost::DidCompleteRecognition exit"; |
| 223 } | 205 } |
| 224 | 206 |
| 225 } // namespace speech_input | 207 } // namespace speech_input |
| OLD | NEW |