Index: content/browser/speech/speech_recognition_request.cc |
diff --git a/content/browser/speech/speech_recognition_request.cc b/content/browser/speech/speech_recognition_request.cc |
index 31b7f1ff45312a9d37cea9ae2e19e90f589faac7..ecc88a9f87408e62772e5ab60ea454d482b1917f 100644 |
--- a/content/browser/speech/speech_recognition_request.cc |
+++ b/content/browser/speech/speech_recognition_request.cc |
@@ -20,6 +20,7 @@ namespace { |
const char* const kDefaultSpeechRecognitionUrl = |
"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&"; |
+const char* const kStatusString = "status"; |
const char* const kHypothesesString = "hypotheses"; |
const char* const kUtteranceString = "utterance"; |
const char* const kConfidenceString = "confidence"; |
@@ -29,7 +30,7 @@ const char* const kConfidenceString = "confidence"; |
const int kMaxResults = 6; |
bool ParseServerResponse(const std::string& response_body, |
- speech_input::SpeechInputResultArray* result) { |
+ speech_input::SpeechInputResult* result) { |
if (response_body.empty()) { |
LOG(WARNING) << "ParseServerResponse: Response was empty."; |
return false; |
@@ -53,18 +54,49 @@ bool ParseServerResponse(const std::string& response_body, |
const DictionaryValue* response_object = |
static_cast<DictionaryValue*>(response_value.get()); |
- // Get the hypotheses |
+ // Get the status. |
+ if (!response_object->HasKey(kStatusString)) { |
Satish
2011/10/06 09:09:06
Is this check required or is GetInteger returning
Leandro Graciá Gil
2011/10/06 18:26:25
Done.
|
+ VLOG(1) << "ParseServerResponse: cannot find " << kStatusString |
+ << " in the response object."; |
+ return false; |
+ } |
+ |
+ int status; |
+ if (!response_object->GetInteger(kStatusString, &status)) { |
+ VLOG(1) << "ParseServerResponse: get " << kStatusString |
Satish
2011/10/06 09:09:06
could print something like
kStatusString << " is
Leandro Graciá Gil
2011/10/06 18:26:25
Done.
|
+ << " integer value."; |
+ return false; |
+ } |
+ |
+ // Process the status. |
+ switch (status) { |
+ case speech_input::kErrorNone: |
+ case speech_input::kErrorNoSpeech: |
+ case speech_input::kErrorNoMatch: |
+ break; |
+ |
+ default: |
+ // Other status codes should not be returned by the server. |
+ VLOG(1) << "ParseServerResponse: unexpected status code " << status; |
+ return false; |
+ } |
+ |
+ result->error = static_cast<speech_input::SpeechInputError>(status); |
+ |
+ // Get the hypotheses. |
Value* hypotheses_value = NULL; |
if (!response_object->Get(kHypothesesString, &hypotheses_value)) { |
VLOG(1) << "ParseServerResponse: Missing hypotheses attribute."; |
return false; |
} |
+ |
DCHECK(hypotheses_value); |
if (!hypotheses_value->IsType(Value::TYPE_LIST)) { |
VLOG(1) << "ParseServerResponse: Unexpected hypotheses type " |
<< hypotheses_value->GetType(); |
return false; |
} |
+ |
const ListValue* hypotheses_list = static_cast<ListValue*>(hypotheses_value); |
size_t index = 0; |
@@ -93,12 +125,13 @@ bool ParseServerResponse(const std::string& response_body, |
double confidence = 0.0; |
hypothesis_value->GetDouble(kConfidenceString, &confidence); |
- result->push_back(speech_input::SpeechInputResultItem(utterance, |
- confidence)); |
+ result->hypotheses.push_back(speech_input::SpeechInputHypothesis( |
+ utterance, confidence)); |
} |
if (index < hypotheses_list->GetSize()) { |
- result->clear(); |
+ result->hypotheses.clear(); |
+ result->error = speech_input::kErrorNetwork; |
Satish
2011/10/06 09:09:06
See the next comment, if you go with that you can
Leandro Graciá Gil
2011/10/06 18:26:25
Done.
|
return false; |
} |
@@ -185,7 +218,7 @@ void SpeechRecognitionRequest::OnURLFetchComplete(const URLFetcher* source) { |
bool error = |
!source->status().is_success() || source->response_code() != 200; |
- SpeechInputResultArray result; |
+ SpeechInputResult result; |
if (!error) |
Satish
2011/10/06 09:09:06
We could remove the 'bool error' parameter in the
Leandro Graciá Gil
2011/10/06 18:26:25
Done.
|
error = !ParseServerResponse(source->GetResponseStringRef(), &result); |
url_fetcher_.reset(); |