OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_extension_manager.h" | 5 #include "chrome/browser/speech/speech_input_extension_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 extensions::ExtensionSystem::Get(profile_)->process_manager(); | 256 extensions::ExtensionSystem::Get(profile_)->process_manager(); |
257 DCHECK(epm); | 257 DCHECK(epm); |
258 extensions::ExtensionHost* eh = | 258 extensions::ExtensionHost* eh = |
259 epm->GetBackgroundHostForExtension(extension_id); | 259 epm->GetBackgroundHostForExtension(extension_id); |
260 DCHECK(eh); | 260 DCHECK(eh); |
261 content::RenderProcessHost* rph = eh->render_process_host(); | 261 content::RenderProcessHost* rph = eh->render_process_host(); |
262 DCHECK(rph); | 262 DCHECK(rph); |
263 return rph->GetID(); | 263 return rph->GetID(); |
264 } | 264 } |
265 | 265 |
266 void SpeechInputExtensionManager::OnRecognitionResults( | 266 void SpeechInputExtensionManager::OnRecognitionResult( |
267 int session_id, | 267 int session_id, |
268 const content::SpeechRecognitionResults& results) { | 268 const content::SpeechRecognitionResult& result) { |
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
270 DCHECK_EQ(session_id, speech_recognition_session_id_); | 270 DCHECK_EQ(session_id, speech_recognition_session_id_); |
271 | 271 |
272 // Stopping will start the disassociation with the extension. | 272 // Stopping will start the disassociation with the extension. |
273 // Make a copy to report the results to the proper one. | 273 // Make a copy to report the results to the proper one. |
274 std::string extension_id = extension_id_in_use_; | 274 std::string extension_id = extension_id_in_use_; |
275 ForceStopOnIOThread(); | 275 ForceStopOnIOThread(); |
276 | 276 |
277 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 277 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
278 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultsOnUIThread, | 278 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread, |
279 this, results, extension_id)); | 279 this, result, extension_id)); |
280 } | 280 } |
281 | 281 |
282 void SpeechInputExtensionManager::SetRecognitionResultsOnUIThread( | 282 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( |
283 const content::SpeechRecognitionResults& results, | 283 const content::SpeechRecognitionResult& result, |
284 const std::string& extension_id) { | 284 const std::string& extension_id) { |
285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
286 | 286 |
287 content::SpeechRecognitionResults::const_iterator it = results.begin(); | 287 scoped_ptr<ListValue> args(new ListValue()); |
288 for (; it != results.end(); ++it) { | 288 DictionaryValue* js_event = new DictionaryValue(); |
289 const content::SpeechRecognitionResult& result = (*it); | 289 args->Append(js_event); |
290 | 290 |
291 scoped_ptr<ListValue> args(new ListValue()); | 291 ListValue* js_hypothesis_array = new ListValue(); |
292 DictionaryValue* js_event = new DictionaryValue(); | 292 js_event->Set(kHypothesesKey, js_hypothesis_array); |
293 args->Append(js_event); | |
294 | 293 |
295 ListValue* js_hypothesis_array = new ListValue(); | 294 for (size_t i = 0; i < result.hypotheses.size(); ++i) { |
296 js_event->Set(kHypothesesKey, js_hypothesis_array); | 295 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; |
297 | 296 |
298 for (size_t i = 0; i < result.hypotheses.size(); ++i) { | 297 DictionaryValue* js_hypothesis_object = new DictionaryValue(); |
299 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; | 298 js_hypothesis_array->Append(js_hypothesis_object); |
300 | 299 |
301 DictionaryValue* js_hypothesis_object = new DictionaryValue(); | 300 js_hypothesis_object->SetString(kUtteranceKey, |
302 js_hypothesis_array->Append(js_hypothesis_object); | 301 UTF16ToUTF8(hypothesis.utterance)); |
| 302 js_hypothesis_object->SetDouble(kConfidenceKey, |
| 303 hypothesis.confidence); |
| 304 } |
303 | 305 |
304 js_hypothesis_object->SetString(kUtteranceKey, | 306 DispatchEventToExtension(extension_id, kOnResultEvent, args.Pass()); |
305 UTF16ToUTF8(hypothesis.utterance)); | |
306 js_hypothesis_object->SetDouble(kConfidenceKey, | |
307 hypothesis.confidence); | |
308 } | |
309 | |
310 DispatchEventToExtension(extension_id, kOnResultEvent, args.Pass()); | |
311 } | |
312 } | 307 } |
313 | 308 |
314 void SpeechInputExtensionManager::OnRecognitionStart(int session_id) { | 309 void SpeechInputExtensionManager::OnRecognitionStart(int session_id) { |
315 DCHECK_EQ(session_id, speech_recognition_session_id_); | 310 DCHECK_EQ(session_id, speech_recognition_session_id_); |
316 } | 311 } |
317 | 312 |
318 void SpeechInputExtensionManager::OnAudioStart(int session_id) { | 313 void SpeechInputExtensionManager::OnAudioStart(int session_id) { |
319 VLOG(1) << "OnAudioStart"; | 314 VLOG(1) << "OnAudioStart"; |
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
321 DCHECK_EQ(session_id, speech_recognition_session_id_); | 316 DCHECK_EQ(session_id, speech_recognition_session_id_); |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 content::NotificationService::current()->Notify( | 751 content::NotificationService::current()->Notify( |
757 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, | 752 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, |
758 // Guarded by the state_ == kShutdown check. | 753 // Guarded by the state_ == kShutdown check. |
759 content::Source<Profile>(profile_), | 754 content::Source<Profile>(profile_), |
760 content::Details<std::string>(&extension_id)); | 755 content::Details<std::string>(&extension_id)); |
761 } | 756 } |
762 | 757 |
763 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, | 758 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, |
764 float volume, | 759 float volume, |
765 float noise_volume) {} | 760 float noise_volume) {} |
OLD | NEW |