| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/speech_input/extension_speech_input_api.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/extensions/speech_input/extension_speech_input_api_cons
tants.h" | |
| 10 #include "chrome/browser/extensions/speech_input/extension_speech_input_manager.
h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/common/chrome_notification_types.h" | |
| 13 #include "content/public/browser/notification_details.h" | |
| 14 #include "content/public/browser/notification_source.h" | |
| 15 | |
| 16 namespace constants = extension_speech_input_api_constants; | |
| 17 | |
| 18 SpeechInputAsyncFunction::SpeechInputAsyncFunction( | |
| 19 int start_state, | |
| 20 int transition_state, | |
| 21 int end_state, | |
| 22 int transition_notification) | |
| 23 : start_state_(start_state), | |
| 24 transition_state_(transition_state), | |
| 25 end_state_(end_state), | |
| 26 transition_notification_(transition_notification), | |
| 27 expecting_transition_(false), | |
| 28 failed_(false) { | |
| 29 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, | |
| 30 content::Source<Profile>(profile())); | |
| 31 } | |
| 32 | |
| 33 SpeechInputAsyncFunction::~SpeechInputAsyncFunction() { | |
| 34 } | |
| 35 | |
| 36 void SpeechInputAsyncFunction::Run() { | |
| 37 if (failed_) { | |
| 38 registrar_.RemoveAll(); | |
| 39 SendResponse(false); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 ExtensionSpeechInputManager::State state = | |
| 44 ExtensionSpeechInputManager::GetForProfile(profile())->state(); | |
| 45 | |
| 46 // RunImpl should be always called once independently of the state we're in, | |
| 47 // otherwise we might miss requestDenied error situations. | |
| 48 if (!expecting_transition_) { | |
| 49 ExtensionSpeechInputManager::State state_before_call = state; | |
| 50 | |
| 51 // Register before RunImpl to ensure it's received if generated. | |
| 52 if (state_before_call == start_state_) { | |
| 53 registrar_.Add(this, transition_notification_, | |
| 54 content::Source<Profile>(profile())); | |
| 55 AddRef(); // Balanced in Observe(). | |
| 56 } | |
| 57 | |
| 58 if (!RunImpl()) { | |
| 59 registrar_.RemoveAll(); | |
| 60 SendResponse(false); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 // RunImpl should always return false and set the appropriate error code | |
| 65 // when called in a state different to the start one. | |
| 66 DCHECK_EQ(state_before_call, start_state_); | |
| 67 | |
| 68 state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); | |
| 69 DCHECK_EQ(state, transition_state_); | |
| 70 expecting_transition_ = true; | |
| 71 } | |
| 72 | |
| 73 if (state == transition_state_) | |
| 74 return; | |
| 75 | |
| 76 DCHECK_EQ(state, end_state_); | |
| 77 registrar_.RemoveAll(); | |
| 78 SendResponse(true); | |
| 79 } | |
| 80 | |
| 81 void SpeechInputAsyncFunction::Observe( | |
| 82 int type, | |
| 83 const content::NotificationSource& source, | |
| 84 const content::NotificationDetails& details) { | |
| 85 DCHECK_EQ(profile(), content::Source<Profile>(source).ptr()); | |
| 86 | |
| 87 if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) { | |
| 88 ExtensionSpeechInputManager::ExtensionError* error_details = | |
| 89 content::Details<ExtensionSpeechInputManager::ExtensionError>( | |
| 90 details).ptr(); | |
| 91 if (error_details->extension_id_ != extension_id()) | |
| 92 return; | |
| 93 | |
| 94 error_ = error_details->error_; | |
| 95 failed_ = true; | |
| 96 } else { | |
| 97 DCHECK_EQ(type, transition_notification_); | |
| 98 if (*content::Details<std::string>(details).ptr() != extension_id()) | |
| 99 return; | |
| 100 DCHECK(expecting_transition_); | |
| 101 } | |
| 102 | |
| 103 Run(); | |
| 104 Release(); // Balanced in Run(). | |
| 105 } | |
| 106 | |
| 107 StartSpeechInputFunction::StartSpeechInputFunction() | |
| 108 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kIdle, | |
| 109 ExtensionSpeechInputManager::kStarting, | |
| 110 ExtensionSpeechInputManager::kRecording, | |
| 111 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) { | |
| 112 } | |
| 113 | |
| 114 bool StartSpeechInputFunction::RunImpl() { | |
| 115 std::string language; | |
| 116 std::string grammar = constants::kDefaultGrammar; | |
| 117 bool filter_profanities = constants::kDefaultFilterProfanities; | |
| 118 | |
| 119 if (!args_->empty()) { | |
| 120 DictionaryValue *options; | |
| 121 if (!args_->GetDictionary(0, &options)) | |
| 122 return false; | |
| 123 DCHECK(options); | |
| 124 | |
| 125 if (options->HasKey(constants::kLanguageKey)) | |
| 126 options->GetString(constants::kLanguageKey, &language); | |
| 127 if (options->HasKey(constants::kGrammarKey)) | |
| 128 options->GetString(constants::kGrammarKey, &grammar); | |
| 129 | |
| 130 if (options->HasKey(constants::kFilterProfanitiesKey)) { | |
| 131 options->GetBoolean(constants::kFilterProfanitiesKey, | |
| 132 &filter_profanities); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // Use the application locale if the language is empty or not provided. | |
| 137 if (language.empty()) { | |
| 138 language = g_browser_process->GetApplicationLocale(); | |
| 139 VLOG(1) << "Language not specified. Using application locale " << language; | |
| 140 } | |
| 141 | |
| 142 return ExtensionSpeechInputManager::GetForProfile(profile())->Start( | |
| 143 extension_id(), language, grammar, filter_profanities, &error_); | |
| 144 } | |
| 145 | |
| 146 StopSpeechInputFunction::StopSpeechInputFunction() | |
| 147 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording, | |
| 148 ExtensionSpeechInputManager::kStopping, | |
| 149 ExtensionSpeechInputManager::kIdle, | |
| 150 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) { | |
| 151 } | |
| 152 | |
| 153 bool StopSpeechInputFunction::RunImpl() { | |
| 154 return ExtensionSpeechInputManager::GetForProfile( | |
| 155 profile())->Stop(extension_id(), &error_); | |
| 156 } | |
| 157 | |
| 158 bool IsRecordingSpeechInputFunction::RunImpl() { | |
| 159 // Do not access the AudioManager directly here to ensure the proper | |
| 160 // IsRecording behaviour in the API tests. | |
| 161 result_.reset(Value::CreateBooleanValue( | |
| 162 ExtensionSpeechInputManager::GetForProfile(profile())->IsRecording())); | |
| 163 return true; | |
| 164 } | |
| OLD | NEW |