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 "content/browser/speech/speech_recognition_request.h" | 5 #include "content/browser/speech/speech_recognition_request.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "chrome/common/net/url_request_context_getter.h" | 13 #include "chrome/common/net/url_request_context_getter.h" |
13 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
14 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
15 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
16 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const char* const kDefaultSpeechRecognitionUrl = | 22 const char* const kDefaultSpeechRecognitionUrl = |
22 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; | 23 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; |
23 const char* const kHypothesesString = "hypotheses"; | 24 const char* const kHypothesesString = "hypotheses"; |
24 const char* const kUtteranceString = "utterance"; | 25 const char* const kUtteranceString = "utterance"; |
25 const char* const kConfidenceString = "confidence"; | 26 const char* const kConfidenceString = "confidence"; |
26 | 27 |
| 28 // TODO(satish): Remove this hardcoded value once the page is allowed to |
| 29 // set this via an attribute. |
| 30 const int kMaxResults = 5; |
| 31 |
27 bool ParseServerResponse(const std::string& response_body, | 32 bool ParseServerResponse(const std::string& response_body, |
28 speech_input::SpeechInputResultArray* result) { | 33 speech_input::SpeechInputResultArray* result) { |
29 if (response_body.empty()) { | 34 if (response_body.empty()) { |
30 LOG(WARNING) << "ParseServerResponse: Response was empty."; | 35 LOG(WARNING) << "ParseServerResponse: Response was empty."; |
31 return false; | 36 return false; |
32 } | 37 } |
33 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; | 38 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; |
34 | 39 |
35 // Parse the response, ignoring comments. | 40 // Parse the response, ignoring comments. |
36 std::string error_msg; | 41 std::string error_msg; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 lang_param = accepted_language_list.substr(0, separator); | 143 lang_param = accepted_language_list.substr(0, separator); |
139 } | 144 } |
140 if (lang_param.empty()) | 145 if (lang_param.empty()) |
141 lang_param = "en-US"; | 146 lang_param = "en-US"; |
142 parts.push_back("lang=" + EscapeQueryParamValue(lang_param, true)); | 147 parts.push_back("lang=" + EscapeQueryParamValue(lang_param, true)); |
143 | 148 |
144 if (!grammar.empty()) | 149 if (!grammar.empty()) |
145 parts.push_back("lm=" + EscapeQueryParamValue(grammar, true)); | 150 parts.push_back("lm=" + EscapeQueryParamValue(grammar, true)); |
146 if (!hardware_info.empty()) | 151 if (!hardware_info.empty()) |
147 parts.push_back("xhw=" + EscapeQueryParamValue(hardware_info, true)); | 152 parts.push_back("xhw=" + EscapeQueryParamValue(hardware_info, true)); |
148 // TODO(satish): Remove this hardcoded value once the page is allowed to | 153 parts.push_back("maxresults=" + base::IntToString(kMaxResults)); |
149 // set this via an attribute. | |
150 parts.push_back("maxresults=3"); | |
151 | 154 |
152 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); | 155 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); |
153 | 156 |
154 url_fetcher_.reset(URLFetcher::Create(url_fetcher_id_for_tests, | 157 url_fetcher_.reset(URLFetcher::Create(url_fetcher_id_for_tests, |
155 url, | 158 url, |
156 URLFetcher::POST, | 159 URLFetcher::POST, |
157 this)); | 160 this)); |
158 url_fetcher_->set_chunked_upload(content_type); | 161 url_fetcher_->set_chunked_upload(content_type); |
159 url_fetcher_->set_request_context(url_context_); | 162 url_fetcher_->set_request_context(url_context_); |
160 url_fetcher_->set_referrer(origin_url); | 163 url_fetcher_->set_referrer(origin_url); |
(...skipping 27 matching lines...) Expand all Loading... |
188 SpeechInputResultArray result; | 191 SpeechInputResultArray result; |
189 if (!error) | 192 if (!error) |
190 error = !ParseServerResponse(data, &result); | 193 error = !ParseServerResponse(data, &result); |
191 url_fetcher_.reset(); | 194 url_fetcher_.reset(); |
192 | 195 |
193 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; | 196 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; |
194 delegate_->SetRecognitionResult(error, result); | 197 delegate_->SetRecognitionResult(error, result); |
195 } | 198 } |
196 | 199 |
197 } // namespace speech_input | 200 } // namespace speech_input |
OLD | NEW |