Chromium Code Reviews| Index: chrome/browser/extensions/extension_speech_input_api.cc |
| diff --git a/chrome/browser/extensions/extension_speech_input_api.cc b/chrome/browser/extensions/extension_speech_input_api.cc |
| index df2bbd1934cc29da29d034685305afefcfabaa1b..906f437c00ba5a04ca36cca59f0966d6a5524228 100644 |
| --- a/chrome/browser/extensions/extension_speech_input_api.cc |
| +++ b/chrome/browser/extensions/extension_speech_input_api.cc |
| @@ -5,17 +5,35 @@ |
| #include "chrome/browser/extensions/extension_speech_input_api.h" |
| #include "base/values.h" |
| +#include "chrome/browser/extensions/extension_speech_input_api_constants.h" |
| #include "chrome/browser/extensions/extension_speech_input_manager.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| -SpeechInputAsyncFunction::SpeechInputAsyncFunction() { |
| +namespace constants = extension_speech_input_api_constants; |
| + |
| +SpeechInputAsyncFunction::SpeechInputAsyncFunction() |
| + : failed_(false) { |
| } |
| void SpeechInputAsyncFunction::Run() { |
| + ExtensionSpeechInputManager::State state = |
| + ExtensionSpeechInputManager::GetForProfile(profile())->state(); |
| + |
| + if (failed_ || (state == StartState() && !RunImpl())) |
| + SendResponse(false); |
| + |
| + state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); |
| + |
| + if (state == TransitionState()) { |
| + registrar_.Add(this, TransitionNotification(), |
|
Satish
2011/10/08 23:15:07
when do you remove as a notification listener? sho
Leandro Graciá Gil
2011/10/09 10:29:21
Removed on destruction with a flag to ensure we're
|
| + Source<const Extension>(GetExtension())); |
| + AddRef(); // Balanced in Observe(). |
| + return; |
| + } |
| - // TODO(leandrogracia): implement the management of the asynchronous |
| - // operations involved and the corresponding state transitions. |
| - RunImpl(); |
| + if (state != EndState()) |
| + return; |
| SendResponse(true); |
| } |
| @@ -23,21 +41,94 @@ void SpeechInputAsyncFunction::Run() { |
| void SpeechInputAsyncFunction::Observe(int type, |
| const NotificationSource& source, |
| const NotificationDetails& details) { |
| - // TODO(leandrogracia): to be implemented. |
| + DCHECK_EQ(GetExtension(), Source<const Extension>(source).ptr()); |
| + if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) |
|
Satish
2011/10/08 23:15:07
suggest using braces for if-else blocks
Leandro Graciá Gil
2011/10/09 10:29:21
Done.
|
| + failed_ = true; |
| + else |
| + DCHECK_EQ(type, TransitionNotification()); |
| + |
| + Run(); |
|
Satish
2011/10/08 23:15:07
is there a possibility that in this call to Run()
Leandro Graciá Gil
2011/10/09 10:29:21
It should never be the case, as the transition not
|
| + Release(); // Balanced in Run(). |
| +} |
| + |
| +int StartSpeechInputFunction::StartState() { |
| + return ExtensionSpeechInputManager::kIdle; |
| +} |
| + |
| +int StartSpeechInputFunction::TransitionState() { |
| + return ExtensionSpeechInputManager::kStarting; |
| +} |
| + |
| +int StartSpeechInputFunction::EndState() { |
| + return ExtensionSpeechInputManager::kRecording; |
| +} |
| + |
| +int StartSpeechInputFunction::TransitionNotification() { |
| + return chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_STARTED; |
| } |
| bool StartSpeechInputFunction::RunImpl() { |
| - ExtensionSpeechInputManager::GetForProfile(profile())->Start(GetExtension()); |
| + std::string language = constants::kDefaultLanguage; |
| + std::string grammar = constants::kDefaultGrammar; |
| + bool filter_profanities = constants::kDefaultFilterProfanities; |
| + |
| + if (!args_->empty()) { |
| + DictionaryValue *options; |
| + if (!args_->GetDictionary(0, &options)) |
| + return false; |
|
Satish
2011/10/08 23:15:07
DCHECK(options) after this
Leandro Graciá Gil
2011/10/09 10:29:21
Done.
|
| + |
| + if (options->HasKey(constants::kLanguageKey)) |
| + options->GetString(constants::kLanguageKey, &language); |
|
Satish
2011/10/08 23:15:07
should you check the return value of these calls?
Leandro Graciá Gil
2011/10/09 10:29:21
The intention is that if they fail the variables w
|
| + if (options->HasKey(constants::kGrammarKey)) |
| + options->GetString(constants::kGrammarKey, &grammar); |
| + |
| + if (options->HasKey(constants::kFilterProfanitiesKey)) { |
| + options->GetBoolean(constants::kFilterProfanitiesKey, |
| + &filter_profanities); |
| + } |
| + } |
| + |
| + ExtensionSpeechInputManager::GetForProfile(profile())->Start( |
| + extension_id(), language, grammar, filter_profanities); |
| return true; |
| } |
| +int StopSpeechInputFunction::StartState() { |
| + return ExtensionSpeechInputManager::kRecording; |
| +} |
| + |
| +int StopSpeechInputFunction::TransitionState() { |
| + return ExtensionSpeechInputManager::kStopping; |
| +} |
| + |
| +int StopSpeechInputFunction::EndState() { |
| + return ExtensionSpeechInputManager::kIdle; |
| +} |
| + |
| +int StopSpeechInputFunction::TransitionNotification() { |
| + return chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_STOPPED; |
| +} |
| + |
| bool StopSpeechInputFunction::RunImpl() { |
| - ExtensionSpeechInputManager::GetForProfile(profile())->Stop(GetExtension()); |
| + ExtensionSpeechInputManager::GetForProfile(profile())->Stop(extension_id()); |
| return true; |
| } |
| bool IsRecordingSpeechInputFunction::RunImpl() { |
| - // TODO(leandrogracia): to be implemented. |
| - result_.reset(Value::CreateBooleanValue(false)); |
| + switch (ExtensionSpeechInputManager::GetForProfile(profile())->state()) { |
|
Satish
2011/10/08 23:15:07
This only allows you to tell if another extension
Leandro Graciá Gil
2011/10/09 10:29:21
Will take a look into this after the next patch to
|
| + case ExtensionSpeechInputManager::kIdle: |
|
Satish
2011/10/08 23:15:07
case statements indented to right by 2 spaces
http
Leandro Graciá Gil
2011/10/09 10:29:21
Good to know. I completely missed that style point
|
| + case ExtensionSpeechInputManager::kStarting: |
| + result_.reset(Value::CreateBooleanValue(false)); |
| + break; |
| + |
| + case ExtensionSpeechInputManager::kRecording: |
| + case ExtensionSpeechInputManager::kStopping: |
| + result_.reset(Value::CreateBooleanValue(true)); |
| + break; |
| + |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| return true; |
| } |