OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/speech/speech_recognition_request.h" | 5 #include "chrome/browser/speech/speech_recognition_request.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
8 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
9 #include "base/string_util.h" | 11 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 13 #include "base/values.h" |
12 #include "chrome/common/net/url_request_context_getter.h" | 14 #include "chrome/common/net/url_request_context_getter.h" |
13 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
14 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
17 #include "net/url_request/url_request_context.h" | |
15 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 const char* const kDefaultSpeechRecognitionUrl = | 22 const char* const kDefaultSpeechRecognitionUrl = |
20 "http://www.google.com/speech-api/v1/recognize?client=chromium&"; | 23 "http://www.google.com/speech-api/v1/recognize?client=chromium&"; |
21 const char* const kHypothesesString = "hypotheses"; | 24 const char* const kHypothesesString = "hypotheses"; |
22 const char* const kUtteranceString = "utterance"; | 25 const char* const kUtteranceString = "utterance"; |
23 const char* const kConfidenceString = "confidence"; | 26 const char* const kConfidenceString = "confidence"; |
24 | 27 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 | 121 |
119 SpeechRecognitionRequest::~SpeechRecognitionRequest() {} | 122 SpeechRecognitionRequest::~SpeechRecognitionRequest() {} |
120 | 123 |
121 bool SpeechRecognitionRequest::Send(const std::string& language, | 124 bool SpeechRecognitionRequest::Send(const std::string& language, |
122 const std::string& grammar, | 125 const std::string& grammar, |
123 const std::string& content_type, | 126 const std::string& content_type, |
124 const std::string& audio_data) { | 127 const std::string& audio_data) { |
125 DCHECK(!url_fetcher_.get()); | 128 DCHECK(!url_fetcher_.get()); |
126 | 129 |
127 std::vector<std::string> parts; | 130 std::vector<std::string> parts; |
128 if (!language.empty()) { | 131 |
129 parts.push_back("lang=" + EscapeQueryParamValue(language, true)); | 132 std::string lang_param = language; |
130 } else { | 133 if (lang_param.empty() && url_context_) { |
131 std::string app_locale = l10n_util::GetApplicationLocale(""); | 134 // If no language is provided then we use the first from the accepted |
132 parts.push_back("lang=" + EscapeQueryParamValue(app_locale, true)); | 135 // language list. If this list is empty then it defaults to "en-US". |
136 // Example of the contents of this list: "es,en-GB;q=0.8", "" | |
137 URLRequestContext* request_context = url_context_->GetURLRequestContext(); | |
138 DCHECK(request_context); | |
139 std::string accepted_language_list = request_context->accept_language(); | |
140 size_t separator = accepted_language_list.find_first_of(",;"); | |
141 lang_param = accepted_language_list.substr(0, separator); | |
133 } | 142 } |
143 if (lang_param.empty()) lang_param = "en-US"; | |
jam
2010/11/17 17:58:59
nit: while this isn't against the style guide, it
| |
144 parts.push_back("lang=" + EscapeQueryParamValue(lang_param, true)); | |
134 | 145 |
135 if (!grammar.empty()) | 146 if (!grammar.empty()) |
136 parts.push_back("grammar=" + EscapeQueryParamValue(grammar, true)); | 147 parts.push_back("grammar=" + EscapeQueryParamValue(grammar, true)); |
137 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); | 148 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); |
138 | 149 |
139 url_fetcher_.reset(URLFetcher::Create(url_fetcher_id_for_tests, | 150 url_fetcher_.reset(URLFetcher::Create(url_fetcher_id_for_tests, |
140 url, | 151 url, |
141 URLFetcher::POST, | 152 URLFetcher::POST, |
142 this)); | 153 this)); |
143 url_fetcher_->set_upload_data(content_type, audio_data); | 154 url_fetcher_->set_upload_data(content_type, audio_data); |
(...skipping 23 matching lines...) Expand all Loading... | |
167 SpeechInputResultArray result; | 178 SpeechInputResultArray result; |
168 if (!error) | 179 if (!error) |
169 error = !ParseServerResponse(data, &result); | 180 error = !ParseServerResponse(data, &result); |
170 url_fetcher_.reset(); | 181 url_fetcher_.reset(); |
171 | 182 |
172 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; | 183 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; |
173 delegate_->SetRecognitionResult(error, result); | 184 delegate_->SetRecognitionResult(error, result); |
174 } | 185 } |
175 | 186 |
176 } // namespace speech_input | 187 } // namespace speech_input |
OLD | NEW |