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_number_conversions.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "net/base/escape.h" | 13 #include "net/base/escape.h" |
14 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
15 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
16 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
17 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char* const kDefaultSpeechRecognitionUrl = | 21 const char* const kDefaultSpeechRecognitionUrl = |
22 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&" | 22 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; |
23 "pfilter=2&"; | |
24 const char* const kHypothesesString = "hypotheses"; | 23 const char* const kHypothesesString = "hypotheses"; |
25 const char* const kUtteranceString = "utterance"; | 24 const char* const kUtteranceString = "utterance"; |
26 const char* const kConfidenceString = "confidence"; | 25 const char* const kConfidenceString = "confidence"; |
27 | 26 |
28 // TODO(satish): Remove this hardcoded value once the page is allowed to | 27 // TODO(satish): Remove this hardcoded value once the page is allowed to |
29 // set this via an attribute. | 28 // set this via an attribute. |
30 const int kMaxResults = 6; | 29 const int kMaxResults = 6; |
31 | 30 |
32 bool ParseServerResponse(const std::string& response_body, | 31 bool ParseServerResponse(const std::string& response_body, |
33 speech_input::SpeechInputResultArray* result) { | 32 speech_input::SpeechInputResultArray* result) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 net::URLRequestContextGetter* context, Delegate* delegate) | 115 net::URLRequestContextGetter* context, Delegate* delegate) |
117 : url_context_(context), | 116 : url_context_(context), |
118 delegate_(delegate) { | 117 delegate_(delegate) { |
119 DCHECK(delegate); | 118 DCHECK(delegate); |
120 } | 119 } |
121 | 120 |
122 SpeechRecognitionRequest::~SpeechRecognitionRequest() {} | 121 SpeechRecognitionRequest::~SpeechRecognitionRequest() {} |
123 | 122 |
124 void SpeechRecognitionRequest::Start(const std::string& language, | 123 void SpeechRecognitionRequest::Start(const std::string& language, |
125 const std::string& grammar, | 124 const std::string& grammar, |
| 125 bool censor_results, |
126 const std::string& hardware_info, | 126 const std::string& hardware_info, |
127 const std::string& origin_url, | 127 const std::string& origin_url, |
128 const std::string& content_type) { | 128 const std::string& content_type) { |
129 DCHECK(!url_fetcher_.get()); | 129 DCHECK(!url_fetcher_.get()); |
130 | 130 |
131 std::vector<std::string> parts; | 131 std::vector<std::string> parts; |
132 | 132 |
133 std::string lang_param = language; | 133 std::string lang_param = language; |
134 if (lang_param.empty() && url_context_) { | 134 if (lang_param.empty() && url_context_) { |
135 // If no language is provided then we use the first from the accepted | 135 // If no language is provided then we use the first from the accepted |
136 // language list. If this list is empty then it defaults to "en-US". | 136 // language list. If this list is empty then it defaults to "en-US". |
137 // Example of the contents of this list: "es,en-GB;q=0.8", "" | 137 // Example of the contents of this list: "es,en-GB;q=0.8", "" |
138 net::URLRequestContext* request_context = | 138 net::URLRequestContext* request_context = |
139 url_context_->GetURLRequestContext(); | 139 url_context_->GetURLRequestContext(); |
140 DCHECK(request_context); | 140 DCHECK(request_context); |
141 std::string accepted_language_list = request_context->accept_language(); | 141 std::string accepted_language_list = request_context->accept_language(); |
142 size_t separator = accepted_language_list.find_first_of(",;"); | 142 size_t separator = accepted_language_list.find_first_of(",;"); |
143 lang_param = accepted_language_list.substr(0, separator); | 143 lang_param = accepted_language_list.substr(0, separator); |
144 } | 144 } |
145 if (lang_param.empty()) | 145 if (lang_param.empty()) |
146 lang_param = "en-US"; | 146 lang_param = "en-US"; |
147 parts.push_back("lang=" + EscapeQueryParamValue(lang_param, true)); | 147 parts.push_back("lang=" + EscapeQueryParamValue(lang_param, true)); |
148 | 148 |
149 if (!grammar.empty()) | 149 if (!grammar.empty()) |
150 parts.push_back("lm=" + EscapeQueryParamValue(grammar, true)); | 150 parts.push_back("lm=" + EscapeQueryParamValue(grammar, true)); |
151 if (!hardware_info.empty()) | 151 if (!hardware_info.empty()) |
152 parts.push_back("xhw=" + EscapeQueryParamValue(hardware_info, true)); | 152 parts.push_back("xhw=" + EscapeQueryParamValue(hardware_info, true)); |
153 parts.push_back("maxresults=" + base::IntToString(kMaxResults)); | 153 parts.push_back("maxresults=" + base::IntToString(kMaxResults)); |
| 154 parts.push_back(censor_results ? "pfilter=2" : "pfilter=0"); |
154 | 155 |
155 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); | 156 GURL url(std::string(kDefaultSpeechRecognitionUrl) + JoinString(parts, '&')); |
156 | 157 |
157 url_fetcher_.reset(URLFetcher::Create(url_fetcher_id_for_tests, | 158 url_fetcher_.reset(URLFetcher::Create(url_fetcher_id_for_tests, |
158 url, | 159 url, |
159 URLFetcher::POST, | 160 URLFetcher::POST, |
160 this)); | 161 this)); |
161 url_fetcher_->set_chunked_upload(content_type); | 162 url_fetcher_->set_chunked_upload(content_type); |
162 url_fetcher_->set_request_context(url_context_); | 163 url_fetcher_->set_request_context(url_context_); |
163 url_fetcher_->set_referrer(origin_url); | 164 url_fetcher_->set_referrer(origin_url); |
(...skipping 27 matching lines...) Expand all Loading... |
191 SpeechInputResultArray result; | 192 SpeechInputResultArray result; |
192 if (!error) | 193 if (!error) |
193 error = !ParseServerResponse(data, &result); | 194 error = !ParseServerResponse(data, &result); |
194 url_fetcher_.reset(); | 195 url_fetcher_.reset(); |
195 | 196 |
196 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; | 197 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; |
197 delegate_->SetRecognitionResult(error, result); | 198 delegate_->SetRecognitionResult(error, result); |
198 } | 199 } |
199 | 200 |
200 } // namespace speech_input | 201 } // namespace speech_input |
OLD | NEW |