OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/omnibox/search_suggestion_parser.h" | 5 #include "components/omnibox/search_suggestion_parser.h" |
6 | 6 |
7 #include "base/i18n/icu_string_conversions.h" | 7 #include "base/i18n/icu_string_conversions.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 base::OnStringConversionError::FAIL, | 336 base::OnStringConversionError::FAIL, |
337 &data_16)) | 337 &data_16)) |
338 json_data = base::UTF16ToUTF8(data_16); | 338 json_data = base::UTF16ToUTF8(data_16); |
339 } | 339 } |
340 } | 340 } |
341 return json_data; | 341 return json_data; |
342 } | 342 } |
343 | 343 |
344 // static | 344 // static |
345 scoped_ptr<base::Value> SearchSuggestionParser::DeserializeJsonData( | 345 scoped_ptr<base::Value> SearchSuggestionParser::DeserializeJsonData( |
346 std::string json_data) { | 346 base::StringPiece json_data) { |
347 // The JSON response should be an array. | 347 // The JSON response should be an array. |
348 for (size_t response_start_index = json_data.find("["), i = 0; | 348 for (size_t response_start_index = json_data.find("["), i = 0; |
349 response_start_index != std::string::npos && i < 5; | 349 response_start_index != base::StringPiece::npos && i < 5; |
350 response_start_index = json_data.find("[", 1), i++) { | 350 response_start_index = json_data.find("[", 1), i++) { |
351 // Remove any XSSI guards to allow for JSON parsing. | 351 // Remove any XSSI guards to allow for JSON parsing. |
352 if (response_start_index > 0) | 352 json_data.remove_prefix(response_start_index); |
353 json_data.erase(0, response_start_index); | |
354 | 353 |
355 JSONStringValueSerializer deserializer(json_data); | 354 JSONStringValueSerializer deserializer(json_data); |
356 deserializer.set_allow_trailing_comma(true); | 355 deserializer.set_allow_trailing_comma(true); |
357 int error_code = 0; | 356 int error_code = 0; |
358 scoped_ptr<base::Value> data(deserializer.Deserialize(&error_code, NULL)); | 357 scoped_ptr<base::Value> data(deserializer.Deserialize(&error_code, NULL)); |
359 if (error_code == 0) | 358 if (error_code == 0) |
360 return data.Pass(); | 359 return data.Pass(); |
361 } | 360 } |
362 return scoped_ptr<base::Value>(); | 361 return scoped_ptr<base::Value>(); |
363 } | 362 } |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 base::CollapseWhitespace(suggestion, false), match_type, | 521 base::CollapseWhitespace(suggestion, false), match_type, |
523 base::CollapseWhitespace(match_contents, false), | 522 base::CollapseWhitespace(match_contents, false), |
524 match_contents_prefix, annotation, answer_contents, answer_type_str, | 523 match_contents_prefix, annotation, answer_contents, answer_type_str, |
525 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, | 524 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, |
526 relevance, relevances != NULL, should_prefetch, trimmed_input)); | 525 relevance, relevances != NULL, should_prefetch, trimmed_input)); |
527 } | 526 } |
528 } | 527 } |
529 results->relevances_from_server = relevances != NULL; | 528 results->relevances_from_server = relevances != NULL; |
530 return true; | 529 return true; |
531 } | 530 } |
OLD | NEW |