Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/extension_speech_input_api.h" | 5 #include "chrome/browser/extensions/extension_speech_input_api.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/extensions/extension_speech_input_api_constants.h" | |
| 8 #include "chrome/browser/extensions/extension_speech_input_manager.h" | 9 #include "chrome/browser/extensions/extension_speech_input_manager.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/common/chrome_notification_types.h" | |
| 10 | 12 |
| 11 SpeechInputAsyncFunction::SpeechInputAsyncFunction() { | 13 namespace constants = extension_speech_input_api_constants; |
| 14 | |
| 15 SpeechInputAsyncFunction::SpeechInputAsyncFunction(int start_state, | |
| 16 int transition_state, int end_state, int transition_notification) | |
| 17 : kStartState_(start_state), | |
| 18 kTransitionState_(transition_state), | |
| 19 kEndState_(end_state), | |
| 20 kTransitionNotification_(transition_notification), | |
| 21 listening_(false), | |
| 22 failed_(false) { | |
| 23 } | |
| 24 | |
| 25 SpeechInputAsyncFunction::~SpeechInputAsyncFunction() { | |
| 26 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
| |
| 27 registrar_.RemoveAll(); | |
| 12 } | 28 } |
| 13 | 29 |
| 14 void SpeechInputAsyncFunction::Run() { | 30 void SpeechInputAsyncFunction::Run() { |
| 31 ExtensionSpeechInputManager::State state = | |
| 32 ExtensionSpeechInputManager::GetForProfile(profile())->state(); | |
| 15 | 33 |
| 16 // TODO(leandrogracia): implement the management of the asynchronous | 34 if (failed_) |
| 17 // operations involved and the corresponding state transitions. | 35 SendResponse(false); |
| 18 RunImpl(); | 36 |
| 37 if (state == kStartState_) { | |
| 38 if (!RunImpl()) | |
| 39 SendResponse(false); | |
| 40 state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); | |
| 41 } | |
| 42 | |
| 43 if (state == kTransitionState_) { | |
| 44 if (!listening_) { | |
| 45 listening_ = true; | |
| 46 registrar_.Add(this, kTransitionNotification_, | |
| 47 Source<const Extension>(GetExtension())); | |
| 48 AddRef(); // Balanced in Observe(). | |
| 49 } | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 if (state != kEndState_) | |
| 54 return; | |
| 19 | 55 |
| 20 SendResponse(true); | 56 SendResponse(true); |
| 21 } | 57 } |
| 22 | 58 |
| 23 void SpeechInputAsyncFunction::Observe(int type, | 59 void SpeechInputAsyncFunction::Observe(int type, |
| 24 const NotificationSource& source, | 60 const NotificationSource& source, |
| 25 const NotificationDetails& details) { | 61 const NotificationDetails& details) { |
| 26 // TODO(leandrogracia): to be implemented. | 62 DCHECK_EQ(GetExtension(), Source<const Extension>(source).ptr()); |
| 63 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
| |
| 64 failed_ = true; | |
| 65 } else { | |
| 66 DCHECK_EQ(type, kTransitionNotification_); | |
| 67 } | |
| 68 | |
| 69 Run(); | |
| 70 Release(); // Balanced in Run(). | |
| 71 } | |
| 72 | |
| 73 StartSpeechInputFunction::StartSpeechInputFunction() | |
| 74 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kIdle, | |
| 75 ExtensionSpeechInputManager::kStarting, | |
| 76 ExtensionSpeechInputManager::kRecording, | |
| 77 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) { | |
| 27 } | 78 } |
| 28 | 79 |
| 29 bool StartSpeechInputFunction::RunImpl() { | 80 bool StartSpeechInputFunction::RunImpl() { |
| 30 ExtensionSpeechInputManager::GetForProfile(profile())->Start(GetExtension()); | 81 std::string language = constants::kDefaultLanguage; |
| 82 std::string grammar = constants::kDefaultGrammar; | |
| 83 bool filter_profanities = constants::kDefaultFilterProfanities; | |
| 84 | |
| 85 if (!args_->empty()) { | |
| 86 DictionaryValue *options; | |
| 87 if (!args_->GetDictionary(0, &options)) | |
| 88 return false; | |
| 89 DCHECK(options); | |
| 90 | |
| 91 if (options->HasKey(constants::kLanguageKey)) | |
| 92 options->GetString(constants::kLanguageKey, &language); | |
| 93 if (options->HasKey(constants::kGrammarKey)) | |
| 94 options->GetString(constants::kGrammarKey, &grammar); | |
| 95 | |
| 96 if (options->HasKey(constants::kFilterProfanitiesKey)) { | |
| 97 options->GetBoolean(constants::kFilterProfanitiesKey, | |
| 98 &filter_profanities); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 ExtensionSpeechInputManager::GetForProfile(profile())->Start( | |
| 103 extension_id(), language, grammar, filter_profanities); | |
| 31 return true; | 104 return true; |
| 32 } | 105 } |
| 33 | 106 |
| 107 StopSpeechInputFunction::StopSpeechInputFunction() | |
| 108 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording, | |
| 109 ExtensionSpeechInputManager::kStopping, | |
| 110 ExtensionSpeechInputManager::kIdle, | |
| 111 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) { | |
| 112 } | |
| 113 | |
| 34 bool StopSpeechInputFunction::RunImpl() { | 114 bool StopSpeechInputFunction::RunImpl() { |
| 35 ExtensionSpeechInputManager::GetForProfile(profile())->Stop(GetExtension()); | 115 ExtensionSpeechInputManager::GetForProfile(profile())->Stop(extension_id()); |
| 36 return true; | 116 return true; |
| 37 } | 117 } |
| 38 | 118 |
| 39 bool IsRecordingSpeechInputFunction::RunImpl() { | 119 bool IsRecordingSpeechInputFunction::RunImpl() { |
| 40 // TODO(leandrogracia): to be implemented. | 120 switch (ExtensionSpeechInputManager::GetForProfile(profile())->state()) { |
| 41 result_.reset(Value::CreateBooleanValue(false)); | 121 case ExtensionSpeechInputManager::kIdle: |
| 122 case ExtensionSpeechInputManager::kStarting: | |
| 123 result_.reset(Value::CreateBooleanValue(false)); | |
| 124 break; | |
| 125 | |
| 126 case ExtensionSpeechInputManager::kRecording: | |
| 127 case ExtensionSpeechInputManager::kStopping: | |
| 128 result_.reset(Value::CreateBooleanValue(true)); | |
| 129 break; | |
| 130 | |
| 131 default: | |
| 132 NOTREACHED(); | |
| 133 } | |
| 134 | |
| 42 return true; | 135 return true; |
| 43 } | 136 } |
| OLD | NEW |