Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/speech/speech_input_extension_manager.cc

Issue 10694085: Refactor extension event distribution to use Values instead of JSON strings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and review changes. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 276 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
277 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread, 277 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread,
278 this, result, extension_id)); 278 this, result, extension_id));
279 } 279 }
280 280
281 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( 281 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread(
282 const content::SpeechRecognitionResult& result, 282 const content::SpeechRecognitionResult& result,
283 const std::string& extension_id) { 283 const std::string& extension_id) {
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
285 285
286 ListValue args; 286 ListValue* args = new ListValue();
287 DictionaryValue* js_event = new DictionaryValue(); 287 DictionaryValue* js_event = new DictionaryValue();
288 args.Append(js_event); 288 args->Append(js_event);
289 289
290 ListValue* js_hypothesis_array = new ListValue(); 290 ListValue* js_hypothesis_array = new ListValue();
291 js_event->Set(kHypothesesKey, js_hypothesis_array); 291 js_event->Set(kHypothesesKey, js_hypothesis_array);
292 292
293 for (size_t i = 0; i < result.hypotheses.size(); ++i) { 293 for (size_t i = 0; i < result.hypotheses.size(); ++i) {
294 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; 294 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i];
295 295
296 DictionaryValue* js_hypothesis_object = new DictionaryValue(); 296 DictionaryValue* js_hypothesis_object = new DictionaryValue();
297 js_hypothesis_array->Append(js_hypothesis_object); 297 js_hypothesis_array->Append(js_hypothesis_object);
298 298
299 js_hypothesis_object->SetString(kUtteranceKey, 299 js_hypothesis_object->SetString(kUtteranceKey,
300 UTF16ToUTF8(hypothesis.utterance)); 300 UTF16ToUTF8(hypothesis.utterance));
301 js_hypothesis_object->SetDouble(kConfidenceKey, 301 js_hypothesis_object->SetDouble(kConfidenceKey,
302 hypothesis.confidence); 302 hypothesis.confidence);
303 } 303 }
304 304
305 std::string json_args; 305 DispatchEventToExtension(extension_id, kOnResultEvent, args);
306 base::JSONWriter::Write(&args, &json_args);
307 VLOG(1) << "Results: " << json_args;
308 DispatchEventToExtension(extension_id, kOnResultEvent, json_args);
309 } 306 }
310 307
311 void SpeechInputExtensionManager::OnRecognitionStart(int session_id) { 308 void SpeechInputExtensionManager::OnRecognitionStart(int session_id) {
312 DCHECK_EQ(session_id, speech_recognition_session_id_); 309 DCHECK_EQ(session_id, speech_recognition_session_id_);
313 } 310 }
314 311
315 void SpeechInputExtensionManager::OnAudioStart(int session_id) { 312 void SpeechInputExtensionManager::OnAudioStart(int session_id) {
316 VLOG(1) << "OnAudioStart"; 313 VLOG(1) << "OnAudioStart";
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
318 DCHECK_EQ(session_id, speech_recognition_session_id_); 315 DCHECK_EQ(session_id, speech_recognition_session_id_);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 void SpeechInputExtensionManager::OnEnvironmentEstimationComplete( 423 void SpeechInputExtensionManager::OnEnvironmentEstimationComplete(
427 int session_id) { 424 int session_id) {
428 DCHECK_EQ(session_id, speech_recognition_session_id_); 425 DCHECK_EQ(session_id, speech_recognition_session_id_);
429 } 426 }
430 427
431 void SpeechInputExtensionManager::OnSoundStart(int session_id) { 428 void SpeechInputExtensionManager::OnSoundStart(int session_id) {
432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
433 DCHECK_EQ(session_id, speech_recognition_session_id_); 430 DCHECK_EQ(session_id, speech_recognition_session_id_);
434 VLOG(1) << "OnSoundStart"; 431 VLOG(1) << "OnSoundStart";
435 432
436 std::string json_args;
437 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 433 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
438 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, 434 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension,
439 this, extension_id_in_use_, std::string(kOnSoundStartEvent), 435 this, extension_id_in_use_, std::string(kOnSoundStartEvent),
440 json_args)); 436 new ListValue()));
miket_OOO 2012/07/10 22:33:19 Would NULL work here? (I honestly don't know, but
441 } 437 }
442 438
443 void SpeechInputExtensionManager::OnSoundEnd(int session_id) { 439 void SpeechInputExtensionManager::OnSoundEnd(int session_id) {
444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
445 VLOG(1) << "OnSoundEnd"; 441 VLOG(1) << "OnSoundEnd";
446 442
447 std::string json_args;
448 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 443 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
449 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, 444 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension,
450 this, extension_id_in_use_, std::string(kOnSoundEndEvent), 445 this, extension_id_in_use_, std::string(kOnSoundEndEvent),
451 json_args)); 446 new ListValue()));
452 } 447 }
453 448
454 void SpeechInputExtensionManager::DispatchEventToExtension( 449 void SpeechInputExtensionManager::DispatchEventToExtension(
455 const std::string& extension_id, const std::string& event, 450 const std::string& extension_id, const std::string& event,
456 const std::string& json_args) { 451 ListValue* event_args) {
457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
458 453
459 base::AutoLock auto_lock(state_lock_); 454 base::AutoLock auto_lock(state_lock_);
460 if (state_ == kShutdown) 455 if (state_ == kShutdown)
461 return; 456 return;
462 457
463 if (profile_ && profile_->GetExtensionEventRouter()) { 458 if (profile_ && profile_->GetExtensionEventRouter()) {
464 std::string final_args;
465 if (json_args.empty()) {
466 ListValue args;
467 base::JSONWriter::Write(&args, &final_args);
468 } else {
469 final_args = json_args;
470 }
471
472 profile_->GetExtensionEventRouter()->DispatchEventToExtension( 459 profile_->GetExtensionEventRouter()->DispatchEventToExtension(
473 extension_id, event, final_args, profile_, GURL()); 460 extension_id, event, event_args, profile_, GURL());
474 } 461 }
475 } 462 }
476 463
477 void SpeechInputExtensionManager::DispatchError( 464 void SpeechInputExtensionManager::DispatchError(
478 const std::string& error, bool dispatch_event) { 465 const std::string& error, bool dispatch_event) {
479 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
480 467
481 std::string extension_id; 468 std::string extension_id;
482 { 469 {
483 base::AutoLock auto_lock(state_lock_); 470 base::AutoLock auto_lock(state_lock_);
484 if (state_ == kShutdown) 471 if (state_ == kShutdown)
485 return; 472 return;
486 473
487 extension_id = extension_id_in_use_; 474 extension_id = extension_id_in_use_;
488 ResetToIdleState(); 475 ResetToIdleState();
489 476
490 // Will set the error property in the ongoing extension function calls. 477 // Will set the error property in the ongoing extension function calls.
491 ExtensionError details(extension_id, error); 478 ExtensionError details(extension_id, error);
492 content::NotificationService::current()->Notify( 479 content::NotificationService::current()->Notify(
493 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, 480 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED,
494 content::Source<Profile>(profile_), 481 content::Source<Profile>(profile_),
495 content::Details<ExtensionError>(&details)); 482 content::Details<ExtensionError>(&details));
496 } 483 }
497 484
498 // Used for errors that are also reported via the onError event. 485 // Used for errors that are also reported via the onError event.
499 if (dispatch_event) { 486 if (dispatch_event) {
500 ListValue args; 487 ListValue* args = new ListValue();
501 DictionaryValue* js_error = new DictionaryValue(); 488 DictionaryValue* js_error = new DictionaryValue();
502 args.Append(js_error); 489 args->Append(js_error);
503 js_error->SetString(kErrorCodeKey, error); 490 js_error->SetString(kErrorCodeKey, error);
504 std::string json_args; 491 DispatchEventToExtension(extension_id, kOnErrorEvent, args);
505 base::JSONWriter::Write(&args, &json_args);
506 DispatchEventToExtension(extension_id,
507 kOnErrorEvent, json_args);
508 } 492 }
509 } 493 }
510 494
511 bool SpeechInputExtensionManager::Start( 495 bool SpeechInputExtensionManager::Start(
512 const std::string& extension_id, const std::string& language, 496 const std::string& extension_id, const std::string& language,
513 const std::string& grammar, bool filter_profanities, std::string* error) { 497 const std::string& grammar, bool filter_profanities, std::string* error) {
514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 498 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
515 DCHECK(error); 499 DCHECK(error);
516 VLOG(1) << "Requesting start (UI thread)"; 500 VLOG(1) << "Requesting start (UI thread)";
517 501
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 content::NotificationService::current()->Notify( 749 content::NotificationService::current()->Notify(
766 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, 750 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED,
767 // Guarded by the state_ == kShutdown check. 751 // Guarded by the state_ == kShutdown check.
768 content::Source<Profile>(profile_), 752 content::Source<Profile>(profile_),
769 content::Details<std::string>(&extension_id)); 753 content::Details<std::string>(&extension_id));
770 } 754 }
771 755
772 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, 756 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id,
773 float volume, 757 float volume,
774 float noise_volume) {} 758 float noise_volume) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698