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..39c05b69ffabf8956f69475e3533ecab9c297e63 100644 |
| --- a/chrome/browser/extensions/extension_speech_input_api.cc |
| +++ b/chrome/browser/extensions/extension_speech_input_api.cc |
| @@ -5,39 +5,158 @@ |
| #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(int start_state, |
| + int transition_state, int end_state, int transition_notification) |
| + : start_state_(start_state), |
| + transition_state_(transition_state), |
| + end_state_(end_state), |
| + transition_notification_(transition_notification), |
| + expecting_transition_(false), |
| + failed_(false) { |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, |
| + Source<Profile>(profile())); |
| +} |
| + |
| +SpeechInputAsyncFunction::~SpeechInputAsyncFunction() { |
| } |
| void SpeechInputAsyncFunction::Run() { |
| + if (failed_) { |
| + registrar_.RemoveAll(); |
| + SendResponse(false); |
| + return; |
| + } |
| + |
| + ExtensionSpeechInputManager::State state = |
| + ExtensionSpeechInputManager::GetForProfile(profile())->state(); |
| + |
| + // RunImpl should be always called once independently of the state we're in, |
| + // otherwise we might miss requestDenied error situations. |
| + if (!expecting_transition_) { |
| + ExtensionSpeechInputManager::State state_before_call = state; |
| + |
| + // Register before RunImpl to ensure it's received if generated. |
| + if (state_before_call == start_state_) { |
| + registrar_.Add(this, transition_notification_, |
| + Source<Profile>(profile())); |
| + AddRef(); // Balanced in Observe(). |
| + } |
| + |
| + if (!RunImpl()) { |
| + registrar_.RemoveAll(); |
| + SendResponse(false); |
| + return; |
| + } |
| - // TODO(leandrogracia): implement the management of the asynchronous |
| - // operations involved and the corresponding state transitions. |
| - RunImpl(); |
| + // RunImpl should always return false and set the appropriate error code |
| + // when called in a state different to the start one. |
| + DCHECK_EQ(state_before_call, start_state_); |
| + state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); |
| + DCHECK_EQ(state, transition_state_); |
| + expecting_transition_ = true; |
| + } |
| + |
| + if (state == transition_state_) |
| + return; |
| + |
| + DCHECK_EQ(state, end_state_); |
| + registrar_.RemoveAll(); |
| SendResponse(true); |
| } |
| void SpeechInputAsyncFunction::Observe(int type, |
| const NotificationSource& source, |
| const NotificationDetails& details) { |
| - // TODO(leandrogracia): to be implemented. |
| + DCHECK_EQ(profile(), Source<Profile>(source).ptr()); |
| + |
| + if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) { |
| + ExtensionSpeechInputManager::ExtensionError *error_details = |
|
Satish
2011/10/11 13:37:16
move '*' to be before the space
Leandro Graciá Gil
2011/10/11 18:08:00
Done.
|
| + Details<ExtensionSpeechInputManager::ExtensionError>(details).ptr(); |
|
Satish
2011/10/11 13:37:16
indent by 2 more spaces
Leandro Graciá Gil
2011/10/11 18:08:00
Done.
|
| + if (error_details->extension_id_ != extension_id()) |
| + return; |
| + |
| + error_ = error_details->error_; |
| + failed_ = true; |
| + } else { |
| + DCHECK_EQ(type, transition_notification_); |
| + if (*Details<std::string>(details).ptr() != extension_id() || |
| + !expecting_transition_) { |
|
Satish
2011/10/11 13:37:16
should you do a DCHECK(expecting_transition_) if t
Leandro Graciá Gil
2011/10/11 18:08:00
Done.
|
| + return; |
| + } |
| + } |
| + |
| + Run(); |
| + Release(); // Balanced in Run(). |
| +} |
| + |
| +StartSpeechInputFunction::StartSpeechInputFunction() |
| + : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kIdle, |
| + ExtensionSpeechInputManager::kStarting, |
| + ExtensionSpeechInputManager::kRecording, |
| + chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) { |
| } |
| bool StartSpeechInputFunction::RunImpl() { |
| - ExtensionSpeechInputManager::GetForProfile(profile())->Start(GetExtension()); |
| - return true; |
| + 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; |
| + DCHECK(options); |
| + |
| + if (options->HasKey(constants::kLanguageKey)) |
| + options->GetString(constants::kLanguageKey, &language); |
| + if (options->HasKey(constants::kGrammarKey)) |
| + options->GetString(constants::kGrammarKey, &grammar); |
| + |
| + if (options->HasKey(constants::kFilterProfanitiesKey)) { |
| + options->GetBoolean(constants::kFilterProfanitiesKey, |
| + &filter_profanities); |
| + } |
| + } |
| + |
| + return ExtensionSpeechInputManager::GetForProfile(profile())->Start( |
| + extension_id(), language, grammar, filter_profanities, &error_); |
| +} |
| + |
| +StopSpeechInputFunction::StopSpeechInputFunction() |
| + : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording, |
| + ExtensionSpeechInputManager::kStopping, |
| + ExtensionSpeechInputManager::kIdle, |
| + chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) { |
| } |
| bool StopSpeechInputFunction::RunImpl() { |
| - ExtensionSpeechInputManager::GetForProfile(profile())->Stop(GetExtension()); |
| - return true; |
| + return ExtensionSpeechInputManager::GetForProfile( |
| + profile())->Stop(extension_id(), &error_); |
| } |
| bool IsRecordingSpeechInputFunction::RunImpl() { |
| - // TODO(leandrogracia): to be implemented. |
| - result_.reset(Value::CreateBooleanValue(false)); |
| + switch (ExtensionSpeechInputManager::GetForProfile(profile())->state()) { |
| + case ExtensionSpeechInputManager::kIdle: |
| + 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; |
| } |