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() | |
| 16 : failed_(false) { | |
| 12 } | 17 } |
| 13 | 18 |
| 14 void SpeechInputAsyncFunction::Run() { | 19 void SpeechInputAsyncFunction::Run() { |
| 20 ExtensionSpeechInputManager::State state = | |
| 21 ExtensionSpeechInputManager::GetForProfile(profile())->state(); | |
| 15 | 22 |
| 16 // TODO(leandrogracia): implement the management of the asynchronous | 23 if (failed_ || (state == StartState() && !RunImpl())) |
| 17 // operations involved and the corresponding state transitions. | 24 SendResponse(false); |
| 18 RunImpl(); | 25 |
| 26 state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); | |
| 27 | |
| 28 if (state == TransitionState()) { | |
| 29 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
| |
| 30 Source<const Extension>(GetExtension())); | |
| 31 AddRef(); // Balanced in Observe(). | |
| 32 return; | |
| 33 } | |
| 34 | |
| 35 if (state != EndState()) | |
| 36 return; | |
| 19 | 37 |
| 20 SendResponse(true); | 38 SendResponse(true); |
| 21 } | 39 } |
| 22 | 40 |
| 23 void SpeechInputAsyncFunction::Observe(int type, | 41 void SpeechInputAsyncFunction::Observe(int type, |
| 24 const NotificationSource& source, | 42 const NotificationSource& source, |
| 25 const NotificationDetails& details) { | 43 const NotificationDetails& details) { |
| 26 // TODO(leandrogracia): to be implemented. | 44 DCHECK_EQ(GetExtension(), Source<const Extension>(source).ptr()); |
| 45 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.
| |
| 46 failed_ = true; | |
| 47 else | |
| 48 DCHECK_EQ(type, TransitionNotification()); | |
| 49 | |
| 50 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
| |
| 51 Release(); // Balanced in Run(). | |
| 52 } | |
| 53 | |
| 54 int StartSpeechInputFunction::StartState() { | |
| 55 return ExtensionSpeechInputManager::kIdle; | |
| 56 } | |
| 57 | |
| 58 int StartSpeechInputFunction::TransitionState() { | |
| 59 return ExtensionSpeechInputManager::kStarting; | |
| 60 } | |
| 61 | |
| 62 int StartSpeechInputFunction::EndState() { | |
| 63 return ExtensionSpeechInputManager::kRecording; | |
| 64 } | |
| 65 | |
| 66 int StartSpeechInputFunction::TransitionNotification() { | |
| 67 return chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_STARTED; | |
| 27 } | 68 } |
| 28 | 69 |
| 29 bool StartSpeechInputFunction::RunImpl() { | 70 bool StartSpeechInputFunction::RunImpl() { |
| 30 ExtensionSpeechInputManager::GetForProfile(profile())->Start(GetExtension()); | 71 std::string language = constants::kDefaultLanguage; |
| 72 std::string grammar = constants::kDefaultGrammar; | |
| 73 bool filter_profanities = constants::kDefaultFilterProfanities; | |
| 74 | |
| 75 if (!args_->empty()) { | |
| 76 DictionaryValue *options; | |
| 77 if (!args_->GetDictionary(0, &options)) | |
| 78 return false; | |
|
Satish
2011/10/08 23:15:07
DCHECK(options) after this
Leandro Graciá Gil
2011/10/09 10:29:21
Done.
| |
| 79 | |
| 80 if (options->HasKey(constants::kLanguageKey)) | |
| 81 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
| |
| 82 if (options->HasKey(constants::kGrammarKey)) | |
| 83 options->GetString(constants::kGrammarKey, &grammar); | |
| 84 | |
| 85 if (options->HasKey(constants::kFilterProfanitiesKey)) { | |
| 86 options->GetBoolean(constants::kFilterProfanitiesKey, | |
| 87 &filter_profanities); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 ExtensionSpeechInputManager::GetForProfile(profile())->Start( | |
| 92 extension_id(), language, grammar, filter_profanities); | |
| 31 return true; | 93 return true; |
| 32 } | 94 } |
| 33 | 95 |
| 96 int StopSpeechInputFunction::StartState() { | |
| 97 return ExtensionSpeechInputManager::kRecording; | |
| 98 } | |
| 99 | |
| 100 int StopSpeechInputFunction::TransitionState() { | |
| 101 return ExtensionSpeechInputManager::kStopping; | |
| 102 } | |
| 103 | |
| 104 int StopSpeechInputFunction::EndState() { | |
| 105 return ExtensionSpeechInputManager::kIdle; | |
| 106 } | |
| 107 | |
| 108 int StopSpeechInputFunction::TransitionNotification() { | |
| 109 return chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_STOPPED; | |
| 110 } | |
| 111 | |
| 34 bool StopSpeechInputFunction::RunImpl() { | 112 bool StopSpeechInputFunction::RunImpl() { |
| 35 ExtensionSpeechInputManager::GetForProfile(profile())->Stop(GetExtension()); | 113 ExtensionSpeechInputManager::GetForProfile(profile())->Stop(extension_id()); |
| 36 return true; | 114 return true; |
| 37 } | 115 } |
| 38 | 116 |
| 39 bool IsRecordingSpeechInputFunction::RunImpl() { | 117 bool IsRecordingSpeechInputFunction::RunImpl() { |
| 40 // TODO(leandrogracia): to be implemented. | 118 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
| |
| 41 result_.reset(Value::CreateBooleanValue(false)); | 119 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
| |
| 120 case ExtensionSpeechInputManager::kStarting: | |
| 121 result_.reset(Value::CreateBooleanValue(false)); | |
| 122 break; | |
| 123 | |
| 124 case ExtensionSpeechInputManager::kRecording: | |
| 125 case ExtensionSpeechInputManager::kStopping: | |
| 126 result_.reset(Value::CreateBooleanValue(true)); | |
| 127 break; | |
| 128 | |
| 129 default: | |
| 130 NOTREACHED(); | |
| 131 } | |
| 132 | |
| 42 return true; | 133 return true; |
| 43 } | 134 } |
| OLD | NEW |