| 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::OnRecognitionResult( | 266 void SpeechInputExtensionManager::OnRecognitionResults( |
| 267 int session_id, | 267 int session_id, |
| 268 const content::SpeechRecognitionResult& result) { | 268 const content::SpeechRecognitionResults& results) { |
| 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::SetRecognitionResultOnUIThread, | 278 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultsOnUIThread, |
| 279 this, result, extension_id)); | 279 this, results, extension_id)); |
| 280 } | 280 } |
| 281 | 281 |
| 282 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( | 282 void SpeechInputExtensionManager::SetRecognitionResultsOnUIThread( |
| 283 const content::SpeechRecognitionResult& result, | 283 const content::SpeechRecognitionResults& results, |
| 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 scoped_ptr<ListValue> args(new ListValue()); | 287 content::SpeechRecognitionResults::const_iterator it = results.begin(); |
| 288 DictionaryValue* js_event = new DictionaryValue(); | 288 for (; it != results.end(); ++it) { |
| 289 args->Append(js_event); | 289 const content::SpeechRecognitionResult& result = (*it); |
| 290 | 290 |
| 291 ListValue* js_hypothesis_array = new ListValue(); | 291 scoped_ptr<ListValue> args(new ListValue()); |
| 292 js_event->Set(kHypothesesKey, js_hypothesis_array); | 292 DictionaryValue* js_event = new DictionaryValue(); |
| 293 args->Append(js_event); |
| 293 | 294 |
| 294 for (size_t i = 0; i < result.hypotheses.size(); ++i) { | 295 ListValue* js_hypothesis_array = new ListValue(); |
| 295 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; | 296 js_event->Set(kHypothesesKey, js_hypothesis_array); |
| 296 | 297 |
| 297 DictionaryValue* js_hypothesis_object = new DictionaryValue(); | 298 for (size_t i = 0; i < result.hypotheses.size(); ++i) { |
| 298 js_hypothesis_array->Append(js_hypothesis_object); | 299 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; |
| 299 | 300 |
| 300 js_hypothesis_object->SetString(kUtteranceKey, | 301 DictionaryValue* js_hypothesis_object = new DictionaryValue(); |
| 301 UTF16ToUTF8(hypothesis.utterance)); | 302 js_hypothesis_array->Append(js_hypothesis_object); |
| 302 js_hypothesis_object->SetDouble(kConfidenceKey, | 303 |
| 303 hypothesis.confidence); | 304 js_hypothesis_object->SetString(kUtteranceKey, |
| 305 UTF16ToUTF8(hypothesis.utterance)); |
| 306 js_hypothesis_object->SetDouble(kConfidenceKey, |
| 307 hypothesis.confidence); |
| 308 } |
| 309 |
| 310 DispatchEventToExtension(extension_id, kOnResultEvent, args.Pass()); |
| 304 } | 311 } |
| 305 | |
| 306 DispatchEventToExtension(extension_id, kOnResultEvent, args.Pass()); | |
| 307 } | 312 } |
| 308 | 313 |
| 309 void SpeechInputExtensionManager::OnRecognitionStart(int session_id) { | 314 void SpeechInputExtensionManager::OnRecognitionStart(int session_id) { |
| 310 DCHECK_EQ(session_id, speech_recognition_session_id_); | 315 DCHECK_EQ(session_id, speech_recognition_session_id_); |
| 311 } | 316 } |
| 312 | 317 |
| 313 void SpeechInputExtensionManager::OnAudioStart(int session_id) { | 318 void SpeechInputExtensionManager::OnAudioStart(int session_id) { |
| 314 VLOG(1) << "OnAudioStart"; | 319 VLOG(1) << "OnAudioStart"; |
| 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 316 DCHECK_EQ(session_id, speech_recognition_session_id_); | 321 DCHECK_EQ(session_id, speech_recognition_session_id_); |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 content::NotificationService::current()->Notify( | 756 content::NotificationService::current()->Notify( |
| 752 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, | 757 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, |
| 753 // Guarded by the state_ == kShutdown check. | 758 // Guarded by the state_ == kShutdown check. |
| 754 content::Source<Profile>(profile_), | 759 content::Source<Profile>(profile_), |
| 755 content::Details<std::string>(&extension_id)); | 760 content::Details<std::string>(&extension_id)); |
| 756 } | 761 } |
| 757 | 762 |
| 758 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, | 763 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, |
| 759 float volume, | 764 float volume, |
| 760 float noise_volume) {} | 765 float noise_volume) {} |
| OLD | NEW |