OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/common/net/url_fetcher_impl.h" | 13 #include "content/common/net/url_fetcher_impl.h" |
14 #include "content/public/common/speech_input_result.h" | 14 #include "content/public/common/speech_recognition_result.h" |
15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
17 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
18 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
19 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char* const kDefaultSpeechRecognitionUrl = | 23 const char* const kDefaultSpeechRecognitionUrl = |
24 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; | 24 "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; |
25 const char* const kStatusString = "status"; | 25 const char* const kStatusString = "status"; |
26 const char* const kHypothesesString = "hypotheses"; | 26 const char* const kHypothesesString = "hypotheses"; |
27 const char* const kUtteranceString = "utterance"; | 27 const char* const kUtteranceString = "utterance"; |
28 const char* const kConfidenceString = "confidence"; | 28 const char* const kConfidenceString = "confidence"; |
29 | 29 |
30 // TODO(satish): Remove this hardcoded value once the page is allowed to | 30 // TODO(satish): Remove this hardcoded value once the page is allowed to |
31 // set this via an attribute. | 31 // set this via an attribute. |
32 const int kMaxResults = 6; | 32 const int kMaxResults = 6; |
33 | 33 |
34 bool ParseServerResponse(const std::string& response_body, | 34 bool ParseServerResponse(const std::string& response_body, |
35 content::SpeechInputResult* result) { | 35 content::SpeechRecognitionResult* result) { |
36 if (response_body.empty()) { | 36 if (response_body.empty()) { |
37 LOG(WARNING) << "ParseServerResponse: Response was empty."; | 37 LOG(WARNING) << "ParseServerResponse: Response was empty."; |
38 return false; | 38 return false; |
39 } | 39 } |
40 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; | 40 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; |
41 | 41 |
42 // Parse the response, ignoring comments. | 42 // Parse the response, ignoring comments. |
43 std::string error_msg; | 43 std::string error_msg; |
44 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( | 44 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( |
45 response_body, false, NULL, &error_msg)); | 45 response_body, false, NULL, &error_msg)); |
(...skipping 13 matching lines...) Expand all Loading... |
59 // Get the status. | 59 // Get the status. |
60 int status; | 60 int status; |
61 if (!response_object->GetInteger(kStatusString, &status)) { | 61 if (!response_object->GetInteger(kStatusString, &status)) { |
62 VLOG(1) << "ParseServerResponse: " << kStatusString | 62 VLOG(1) << "ParseServerResponse: " << kStatusString |
63 << " is not a valid integer value."; | 63 << " is not a valid integer value."; |
64 return false; | 64 return false; |
65 } | 65 } |
66 | 66 |
67 // Process the status. | 67 // Process the status. |
68 switch (status) { | 68 switch (status) { |
69 case content::SPEECH_INPUT_ERROR_NONE: | 69 case content::SPEECH_RECOGNITION_ERROR_NONE: |
70 case content::SPEECH_INPUT_ERROR_NO_SPEECH: | 70 case content::SPEECH_RECOGNITION_ERROR_NO_SPEECH: |
71 case content::SPEECH_INPUT_ERROR_NO_MATCH: | 71 case content::SPEECH_RECOGNITION_ERROR_NO_MATCH: |
72 break; | 72 break; |
73 | 73 |
74 default: | 74 default: |
75 // Other status codes should not be returned by the server. | 75 // Other status codes should not be returned by the server. |
76 VLOG(1) << "ParseServerResponse: unexpected status code " << status; | 76 VLOG(1) << "ParseServerResponse: unexpected status code " << status; |
77 return false; | 77 return false; |
78 } | 78 } |
79 | 79 |
80 result->error = static_cast<content::SpeechInputError>(status); | 80 result->error = static_cast<content::SpeechRecognitionErrorCode>(status); |
81 | 81 |
82 // Get the hypotheses. | 82 // Get the hypotheses. |
83 Value* hypotheses_value = NULL; | 83 Value* hypotheses_value = NULL; |
84 if (!response_object->Get(kHypothesesString, &hypotheses_value)) { | 84 if (!response_object->Get(kHypothesesString, &hypotheses_value)) { |
85 VLOG(1) << "ParseServerResponse: Missing hypotheses attribute."; | 85 VLOG(1) << "ParseServerResponse: Missing hypotheses attribute."; |
86 return false; | 86 return false; |
87 } | 87 } |
88 | 88 |
89 DCHECK(hypotheses_value); | 89 DCHECK(hypotheses_value); |
90 if (!hypotheses_value->IsType(Value::TYPE_LIST)) { | 90 if (!hypotheses_value->IsType(Value::TYPE_LIST)) { |
(...skipping 23 matching lines...) Expand all Loading... |
114 string16 utterance; | 114 string16 utterance; |
115 if (!hypothesis_value->GetString(kUtteranceString, &utterance)) { | 115 if (!hypothesis_value->GetString(kUtteranceString, &utterance)) { |
116 LOG(WARNING) << "ParseServerResponse: Missing utterance value."; | 116 LOG(WARNING) << "ParseServerResponse: Missing utterance value."; |
117 break; | 117 break; |
118 } | 118 } |
119 | 119 |
120 // It is not an error if the 'confidence' field is missing. | 120 // It is not an error if the 'confidence' field is missing. |
121 double confidence = 0.0; | 121 double confidence = 0.0; |
122 hypothesis_value->GetDouble(kConfidenceString, &confidence); | 122 hypothesis_value->GetDouble(kConfidenceString, &confidence); |
123 | 123 |
124 result->hypotheses.push_back(content::SpeechInputHypothesis( | 124 result->hypotheses.push_back(content::SpeechRecognitionHypothesis( |
125 utterance, confidence)); | 125 utterance, confidence)); |
126 } | 126 } |
127 | 127 |
128 if (index < hypotheses_list->GetSize()) { | 128 if (index < hypotheses_list->GetSize()) { |
129 result->hypotheses.clear(); | 129 result->hypotheses.clear(); |
130 return false; | 130 return false; |
131 } | 131 } |
132 | 132 |
133 return true; | 133 return true; |
134 } | 134 } |
135 | 135 |
136 } // namespace | 136 } // namespace |
137 | 137 |
138 namespace speech_input { | 138 namespace speech { |
139 | 139 |
140 int SpeechRecognitionRequest::url_fetcher_id_for_tests = 0; | 140 int SpeechRecognitionRequest::url_fetcher_id_for_tests = 0; |
141 | 141 |
142 SpeechRecognitionRequest::SpeechRecognitionRequest( | 142 SpeechRecognitionRequest::SpeechRecognitionRequest( |
143 net::URLRequestContextGetter* context, Delegate* delegate) | 143 net::URLRequestContextGetter* context, Delegate* delegate) |
144 : url_context_(context), | 144 : url_context_(context), |
145 delegate_(delegate) { | 145 delegate_(delegate) { |
146 DCHECK(delegate); | 146 DCHECK(delegate); |
147 } | 147 } |
148 | 148 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 void SpeechRecognitionRequest::UploadAudioChunk(const std::string& audio_data, | 204 void SpeechRecognitionRequest::UploadAudioChunk(const std::string& audio_data, |
205 bool is_last_chunk) { | 205 bool is_last_chunk) { |
206 DCHECK(url_fetcher_.get()); | 206 DCHECK(url_fetcher_.get()); |
207 url_fetcher_->AppendChunkToUpload(audio_data, is_last_chunk); | 207 url_fetcher_->AppendChunkToUpload(audio_data, is_last_chunk); |
208 } | 208 } |
209 | 209 |
210 void SpeechRecognitionRequest::OnURLFetchComplete( | 210 void SpeechRecognitionRequest::OnURLFetchComplete( |
211 const content::URLFetcher* source) { | 211 const content::URLFetcher* source) { |
212 DCHECK_EQ(url_fetcher_.get(), source); | 212 DCHECK_EQ(url_fetcher_.get(), source); |
213 | 213 |
214 content::SpeechInputResult result; | 214 content::SpeechRecognitionResult result; |
215 std::string data; | 215 std::string data; |
216 if (!source->GetStatus().is_success() || source->GetResponseCode() != 200 || | 216 if (!source->GetStatus().is_success() || source->GetResponseCode() != 200 || |
217 !source->GetResponseAsString(&data) || | 217 !source->GetResponseAsString(&data) || |
218 !ParseServerResponse(data, &result)) { | 218 !ParseServerResponse(data, &result)) { |
219 result.error = content::SPEECH_INPUT_ERROR_NETWORK; | 219 result.error = content::SPEECH_RECOGNITION_ERROR_NETWORK; |
220 } | 220 } |
221 | 221 |
222 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; | 222 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; |
223 url_fetcher_.reset(); | 223 url_fetcher_.reset(); |
224 delegate_->SetRecognitionResult(result); | 224 delegate_->SetRecognitionResult(result); |
225 } | 225 } |
226 | 226 |
227 } // namespace speech_input | 227 } // namespace speech |
OLD | NEW |