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..9cd159c9ed7b24e80826130dbffa1d3c0518efff 100644 |
| --- a/chrome/browser/extensions/extension_speech_input_api.cc |
| +++ b/chrome/browser/extensions/extension_speech_input_api.cc |
| @@ -5,17 +5,53 @@ |
| #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) |
| + : kStartState_(start_state), |
| + kTransitionState_(transition_state), |
| + kEndState_(end_state), |
| + kTransitionNotification_(transition_notification), |
| + listening_(false), |
| + failed_(false) { |
| +} |
| + |
| +SpeechInputAsyncFunction::~SpeechInputAsyncFunction() { |
| + if (listening_) |
|
Satish
2011/10/09 20:01:47
I looked at NotificationRegistrar's destructor and
Leandro Graciá Gil
2011/10/10 18:59:38
The idea was that the object would be destroyed af
|
| + registrar_.RemoveAll(); |
| } |
| void SpeechInputAsyncFunction::Run() { |
| + ExtensionSpeechInputManager::State state = |
| + ExtensionSpeechInputManager::GetForProfile(profile())->state(); |
| + |
| + if (failed_) |
| + SendResponse(false); |
| + |
| + if (state == kStartState_) { |
| + if (!RunImpl()) |
| + SendResponse(false); |
| + state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); |
| + } |
| - // TODO(leandrogracia): implement the management of the asynchronous |
| - // operations involved and the corresponding state transitions. |
| - RunImpl(); |
| + if (state == kTransitionState_) { |
| + if (!listening_) { |
| + listening_ = true; |
| + registrar_.Add(this, kTransitionNotification_, |
| + Source<const Extension>(GetExtension())); |
| + AddRef(); // Balanced in Observe(). |
| + } |
| + return; |
| + } |
| + |
| + if (state != kEndState_) |
| + return; |
| SendResponse(true); |
| } |
| @@ -23,21 +59,78 @@ 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_RECORDING_FAILED) { |
|
Satish
2011/10/09 20:01:47
You aren't registering for this notification type
Leandro Graciá Gil
2011/10/10 18:59:38
You've got a point. I'll fix it.
On 2011/10/09 20
|
| + failed_ = true; |
| + } else { |
| + DCHECK_EQ(type, kTransitionNotification_); |
| + } |
| + |
| + 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()); |
| + 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); |
| + } |
| + } |
| + |
| + ExtensionSpeechInputManager::GetForProfile(profile())->Start( |
| + extension_id(), language, grammar, filter_profanities); |
| return true; |
| } |
| +StopSpeechInputFunction::StopSpeechInputFunction() |
| + : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording, |
| + ExtensionSpeechInputManager::kStopping, |
| + ExtensionSpeechInputManager::kIdle, |
| + chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_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()) { |
| + 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; |
| } |