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

Unified 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 side-by-side diff with in-line comments
Download patch
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..3a2ad7cb46c6bc39409fa9a2e68dcd45539c27a7 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,43 @@ bool ParseServerResponse(const std::string& response_body,
const DictionaryValue* response_object =
static_cast<DictionaryValue*>(response_value.get());
- // Get the hypotheses
+ // Get the status.
+ int status;
+ if (!response_object->GetInteger(kStatusString, &status)) {
+ VLOG(1) << "ParseServerResponse: " << kStatusString
+ << " is not a valid 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 +119,12 @@ 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();
return false;
}
@@ -182,16 +208,15 @@ void SpeechRecognitionRequest::UploadAudioChunk(const std::string& audio_data,
void SpeechRecognitionRequest::OnURLFetchComplete(const URLFetcher* source) {
DCHECK_EQ(url_fetcher_.get(), source);
- bool error =
- !source->status().is_success() || source->response_code() != 200;
-
- SpeechInputResultArray result;
- if (!error)
- error = !ParseServerResponse(source->GetResponseStringRef(), &result);
- url_fetcher_.reset();
+ SpeechInputResult result;
+ if (!source->status().is_success() || source->response_code() != 200 ||
+ !ParseServerResponse(source->GetResponseStringRef(), &result)) {
+ result.error = kErrorNetwork;
+ }
DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result.";
- delegate_->SetRecognitionResult(error, result);
+ url_fetcher_.reset();
+ delegate_->SetRecognitionResult(result);
}
} // namespace speech_input
« 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