Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: content/browser/speech/speech_recognition_request.cc

Issue 8137005: Applying changes to the existing speech input code to support the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: mac bot fix. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 const char* const kStatusString = "status";
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
27 // TODO(satish): Remove this hardcoded value once the page is allowed to 28 // TODO(satish): Remove this hardcoded value once the page is allowed to
28 // set this via an attribute. 29 // set this via an attribute.
29 const int kMaxResults = 6; 30 const int kMaxResults = 6;
30 31
31 bool ParseServerResponse(const std::string& response_body, 32 bool ParseServerResponse(const std::string& response_body,
32 speech_input::SpeechInputResultArray* result) { 33 speech_input::SpeechInputResult* result) {
33 if (response_body.empty()) { 34 if (response_body.empty()) {
34 LOG(WARNING) << "ParseServerResponse: Response was empty."; 35 LOG(WARNING) << "ParseServerResponse: Response was empty.";
35 return false; 36 return false;
36 } 37 }
37 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; 38 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body;
38 39
39 // Parse the response, ignoring comments. 40 // Parse the response, ignoring comments.
40 std::string error_msg; 41 std::string error_msg;
41 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( 42 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError(
42 response_body, false, NULL, &error_msg)); 43 response_body, false, NULL, &error_msg));
43 if (response_value == NULL) { 44 if (response_value == NULL) {
44 LOG(WARNING) << "ParseServerResponse: JSONReader failed : " << error_msg; 45 LOG(WARNING) << "ParseServerResponse: JSONReader failed : " << error_msg;
45 return false; 46 return false;
46 } 47 }
47 48
48 if (!response_value->IsType(Value::TYPE_DICTIONARY)) { 49 if (!response_value->IsType(Value::TYPE_DICTIONARY)) {
49 VLOG(1) << "ParseServerResponse: Unexpected response type " 50 VLOG(1) << "ParseServerResponse: Unexpected response type "
50 << response_value->GetType(); 51 << response_value->GetType();
51 return false; 52 return false;
52 } 53 }
53 const DictionaryValue* response_object = 54 const DictionaryValue* response_object =
54 static_cast<DictionaryValue*>(response_value.get()); 55 static_cast<DictionaryValue*>(response_value.get());
55 56
56 // Get the hypotheses 57 // Get the status.
58 int status;
59 if (!response_object->GetInteger(kStatusString, &status)) {
60 VLOG(1) << "ParseServerResponse: " << kStatusString
61 << " is not a valid integer value.";
62 return false;
63 }
64
65 // Process the status.
66 switch (status) {
67 case speech_input::kErrorNone:
68 case speech_input::kErrorNoSpeech:
69 case speech_input::kErrorNoMatch:
70 break;
71
72 default:
73 // Other status codes should not be returned by the server.
74 VLOG(1) << "ParseServerResponse: unexpected status code " << status;
75 return false;
76 }
77
78 result->error = static_cast<speech_input::SpeechInputError>(status);
79
80 // Get the hypotheses.
57 Value* hypotheses_value = NULL; 81 Value* hypotheses_value = NULL;
58 if (!response_object->Get(kHypothesesString, &hypotheses_value)) { 82 if (!response_object->Get(kHypothesesString, &hypotheses_value)) {
59 VLOG(1) << "ParseServerResponse: Missing hypotheses attribute."; 83 VLOG(1) << "ParseServerResponse: Missing hypotheses attribute.";
60 return false; 84 return false;
61 } 85 }
86
62 DCHECK(hypotheses_value); 87 DCHECK(hypotheses_value);
63 if (!hypotheses_value->IsType(Value::TYPE_LIST)) { 88 if (!hypotheses_value->IsType(Value::TYPE_LIST)) {
64 VLOG(1) << "ParseServerResponse: Unexpected hypotheses type " 89 VLOG(1) << "ParseServerResponse: Unexpected hypotheses type "
65 << hypotheses_value->GetType(); 90 << hypotheses_value->GetType();
66 return false; 91 return false;
67 } 92 }
93
68 const ListValue* hypotheses_list = static_cast<ListValue*>(hypotheses_value); 94 const ListValue* hypotheses_list = static_cast<ListValue*>(hypotheses_value);
69 95
70 size_t index = 0; 96 size_t index = 0;
71 for (; index < hypotheses_list->GetSize(); ++index) { 97 for (; index < hypotheses_list->GetSize(); ++index) {
72 Value* hypothesis = NULL; 98 Value* hypothesis = NULL;
73 if (!hypotheses_list->Get(index, &hypothesis)) { 99 if (!hypotheses_list->Get(index, &hypothesis)) {
74 LOG(WARNING) << "ParseServerResponse: Unable to read hypothesis value."; 100 LOG(WARNING) << "ParseServerResponse: Unable to read hypothesis value.";
75 break; 101 break;
76 } 102 }
77 DCHECK(hypothesis); 103 DCHECK(hypothesis);
78 if (!hypothesis->IsType(Value::TYPE_DICTIONARY)) { 104 if (!hypothesis->IsType(Value::TYPE_DICTIONARY)) {
79 LOG(WARNING) << "ParseServerResponse: Unexpected value type " 105 LOG(WARNING) << "ParseServerResponse: Unexpected value type "
80 << hypothesis->GetType(); 106 << hypothesis->GetType();
81 break; 107 break;
82 } 108 }
83 109
84 const DictionaryValue* hypothesis_value = 110 const DictionaryValue* hypothesis_value =
85 static_cast<DictionaryValue*>(hypothesis); 111 static_cast<DictionaryValue*>(hypothesis);
86 string16 utterance; 112 string16 utterance;
87 if (!hypothesis_value->GetString(kUtteranceString, &utterance)) { 113 if (!hypothesis_value->GetString(kUtteranceString, &utterance)) {
88 LOG(WARNING) << "ParseServerResponse: Missing utterance value."; 114 LOG(WARNING) << "ParseServerResponse: Missing utterance value.";
89 break; 115 break;
90 } 116 }
91 117
92 // It is not an error if the 'confidence' field is missing. 118 // It is not an error if the 'confidence' field is missing.
93 double confidence = 0.0; 119 double confidence = 0.0;
94 hypothesis_value->GetDouble(kConfidenceString, &confidence); 120 hypothesis_value->GetDouble(kConfidenceString, &confidence);
95 121
96 result->push_back(speech_input::SpeechInputResultItem(utterance, 122 result->hypotheses.push_back(speech_input::SpeechInputHypothesis(
97 confidence)); 123 utterance, confidence));
98 } 124 }
99 125
100 if (index < hypotheses_list->GetSize()) { 126 if (index < hypotheses_list->GetSize()) {
101 result->clear(); 127 result->hypotheses.clear();
102 return false; 128 return false;
103 } 129 }
104 130
105 return true; 131 return true;
106 } 132 }
107 133
108 } // namespace 134 } // namespace
109 135
110 namespace speech_input { 136 namespace speech_input {
111 137
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 201
176 void SpeechRecognitionRequest::UploadAudioChunk(const std::string& audio_data, 202 void SpeechRecognitionRequest::UploadAudioChunk(const std::string& audio_data,
177 bool is_last_chunk) { 203 bool is_last_chunk) {
178 DCHECK(url_fetcher_.get()); 204 DCHECK(url_fetcher_.get());
179 url_fetcher_->AppendChunkToUpload(audio_data, is_last_chunk); 205 url_fetcher_->AppendChunkToUpload(audio_data, is_last_chunk);
180 } 206 }
181 207
182 void SpeechRecognitionRequest::OnURLFetchComplete(const URLFetcher* source) { 208 void SpeechRecognitionRequest::OnURLFetchComplete(const URLFetcher* source) {
183 DCHECK_EQ(url_fetcher_.get(), source); 209 DCHECK_EQ(url_fetcher_.get(), source);
184 210
185 bool error = 211 SpeechInputResult result;
186 !source->status().is_success() || source->response_code() != 200; 212 if (!source->status().is_success() || source->response_code() != 200 ||
187 213 !ParseServerResponse(source->GetResponseStringRef(), &result)) {
188 SpeechInputResultArray result; 214 result.error = kErrorNetwork;
189 if (!error) 215 }
190 error = !ParseServerResponse(source->GetResponseStringRef(), &result);
191 url_fetcher_.reset();
192 216
193 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; 217 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result.";
194 delegate_->SetRecognitionResult(error, result); 218 url_fetcher_.reset();
219 delegate_->SetRecognitionResult(result);
195 } 220 }
196 221
197 } // namespace speech_input 222 } // namespace speech_input
OLDNEW
« no previous file with comments | « content/browser/speech/speech_recognition_request.h ('k') | content/browser/speech/speech_recognition_request_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698