OLD | NEW |
1 // Copyright (c) 2012 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/google_one_shot_remote_engine.h" | 5 #include "content/browser/speech/google_one_shot_remote_engine.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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // Parse the response, ignoring comments. | 48 // Parse the response, ignoring comments. |
49 std::string error_msg; | 49 std::string error_msg; |
50 scoped_ptr<base::Value> response_value(base::JSONReader::ReadAndReturnError( | 50 scoped_ptr<base::Value> response_value(base::JSONReader::ReadAndReturnError( |
51 response_body, base::JSON_PARSE_RFC, NULL, &error_msg)); | 51 response_body, base::JSON_PARSE_RFC, NULL, &error_msg)); |
52 if (response_value == NULL) { | 52 if (response_value == NULL) { |
53 LOG(WARNING) << "ParseServerResponse: JSONReader failed : " << error_msg; | 53 LOG(WARNING) << "ParseServerResponse: JSONReader failed : " << error_msg; |
54 return false; | 54 return false; |
55 } | 55 } |
56 | 56 |
57 if (!response_value->IsType(base::Value::TYPE_DICTIONARY)) { | 57 if (!response_value->IsType(base::Value::TYPE_DICTIONARY)) { |
58 VLOG(1) << "ParseServerResponse: Unexpected response type " | 58 DVLOG(1) << "ParseServerResponse: Unexpected response type " |
59 << response_value->GetType(); | 59 << response_value->GetType(); |
60 return false; | 60 return false; |
61 } | 61 } |
62 const base::DictionaryValue* response_object = | 62 const base::DictionaryValue* response_object = |
63 static_cast<const base::DictionaryValue*>(response_value.get()); | 63 static_cast<const base::DictionaryValue*>(response_value.get()); |
64 | 64 |
65 // Get the status. | 65 // Get the status. |
66 int status; | 66 int status; |
67 if (!response_object->GetInteger(kStatusString, &status)) { | 67 if (!response_object->GetInteger(kStatusString, &status)) { |
68 VLOG(1) << "ParseServerResponse: " << kStatusString | 68 DVLOG(1) << "ParseServerResponse: " << kStatusString |
69 << " is not a valid integer value."; | 69 << " is not a valid integer value."; |
70 return false; | 70 return false; |
71 } | 71 } |
72 | 72 |
73 // Process the status. | 73 // Process the status. |
74 switch (status) { | 74 switch (status) { |
75 case kWebServiceStatusNoError: | 75 case kWebServiceStatusNoError: |
76 break; | 76 break; |
77 case kWebServiceStatusNoSpeech: | 77 case kWebServiceStatusNoSpeech: |
78 error->code = SPEECH_RECOGNITION_ERROR_NO_SPEECH; | 78 error->code = SPEECH_RECOGNITION_ERROR_NO_SPEECH; |
79 return false; | 79 return false; |
80 case kWebServiceStatusNoMatch: | 80 case kWebServiceStatusNoMatch: |
81 error->code = SPEECH_RECOGNITION_ERROR_NO_MATCH; | 81 error->code = SPEECH_RECOGNITION_ERROR_NO_MATCH; |
82 return false; | 82 return false; |
83 default: | 83 default: |
84 error->code = SPEECH_RECOGNITION_ERROR_NETWORK; | 84 error->code = SPEECH_RECOGNITION_ERROR_NETWORK; |
85 // Other status codes should not be returned by the server. | 85 // Other status codes should not be returned by the server. |
86 VLOG(1) << "ParseServerResponse: unexpected status code " << status; | 86 DVLOG(1) << "ParseServerResponse: unexpected status code " << status; |
87 return false; | 87 return false; |
88 } | 88 } |
89 | 89 |
90 // Get the hypotheses. | 90 // Get the hypotheses. |
91 const base::Value* hypotheses_value = NULL; | 91 const base::Value* hypotheses_value = NULL; |
92 if (!response_object->Get(kHypothesesString, &hypotheses_value)) { | 92 if (!response_object->Get(kHypothesesString, &hypotheses_value)) { |
93 VLOG(1) << "ParseServerResponse: Missing hypotheses attribute."; | 93 DVLOG(1) << "ParseServerResponse: Missing hypotheses attribute."; |
94 return false; | 94 return false; |
95 } | 95 } |
96 | 96 |
97 DCHECK(hypotheses_value); | 97 DCHECK(hypotheses_value); |
98 if (!hypotheses_value->IsType(base::Value::TYPE_LIST)) { | 98 if (!hypotheses_value->IsType(base::Value::TYPE_LIST)) { |
99 VLOG(1) << "ParseServerResponse: Unexpected hypotheses type " | 99 DVLOG(1) << "ParseServerResponse: Unexpected hypotheses type " |
100 << hypotheses_value->GetType(); | 100 << hypotheses_value->GetType(); |
101 return false; | 101 return false; |
102 } | 102 } |
103 | 103 |
104 const base::ListValue* hypotheses_list = | 104 const base::ListValue* hypotheses_list = |
105 static_cast<const base::ListValue*>(hypotheses_value); | 105 static_cast<const base::ListValue*>(hypotheses_value); |
106 | 106 |
107 // For now we support only single shot recognition, so we are giving only a | 107 // For now we support only single shot recognition, so we are giving only a |
108 // final result, consisting of one fragment (with one or more hypotheses). | 108 // final result, consisting of one fragment (with one or more hypotheses). |
109 size_t index = 0; | 109 size_t index = 0; |
110 for (; index < hypotheses_list->GetSize(); ++index) { | 110 for (; index < hypotheses_list->GetSize(); ++index) { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 290 |
291 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { | 291 bool GoogleOneShotRemoteEngine::IsRecognitionPending() const { |
292 return url_fetcher_ != NULL; | 292 return url_fetcher_ != NULL; |
293 } | 293 } |
294 | 294 |
295 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { | 295 int GoogleOneShotRemoteEngine::GetDesiredAudioChunkDurationMs() const { |
296 return kAudioPacketIntervalMs; | 296 return kAudioPacketIntervalMs; |
297 } | 297 } |
298 | 298 |
299 } // namespace content | 299 } // namespace content |
OLD | NEW |