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

Unified Diff: chrome/browser/autocomplete/search_provider.cc

Issue 96753004: Ensure zero suggest can handle XSSI-escaped output. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added a unit test for invalid response case Created 7 years 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: chrome/browser/autocomplete/search_provider.cc
diff --git a/chrome/browser/autocomplete/search_provider.cc b/chrome/browser/autocomplete/search_provider.cc
index e4f35813feb91cacb699c4ff33e352afe0dd1845..1382ce38263055ed7e1759d62810cc6c5b499d0e 100644
--- a/chrome/browser/autocomplete/search_provider.cc
+++ b/chrome/browser/autocomplete/search_provider.cc
@@ -653,24 +653,8 @@ void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
}
}
- // The JSON response should be an array.
- for (size_t response_start_index = json_data.find("["), i = 0;
- response_start_index != std::string::npos && i < 5;
- response_start_index = json_data.find("[", 1), i++) {
- // Remove any XSSI guards to allow for JSON parsing.
- if (response_start_index > 0)
- json_data.erase(0, response_start_index);
-
- JSONStringValueSerializer deserializer(json_data);
- deserializer.set_allow_trailing_comma(true);
- int error_code = 0;
- scoped_ptr<Value> data(deserializer.Deserialize(&error_code, NULL));
- if (error_code == 0) {
- results_updated = data.get() &&
- ParseSuggestResults(data.get(), is_keyword);
- break;
- }
- }
+ scoped_ptr<Value> data(DeserializeJsonData(json_data));
+ results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword);
}
UpdateMatches();
@@ -1040,6 +1024,25 @@ net::URLFetcher* SearchProvider::CreateSuggestFetcher(
return fetcher;
}
+scoped_ptr<Value> SearchProvider::DeserializeJsonData(std::string json_data) {
+ // The JSON response should be an array.
+ for (size_t response_start_index = json_data.find("["), i = 0;
+ response_start_index != std::string::npos && i < 5;
+ response_start_index = json_data.find("[", 1), i++) {
+ // Remove any XSSI guards to allow for JSON parsing.
+ if (response_start_index > 0)
+ json_data.erase(0, response_start_index);
+
+ JSONStringValueSerializer deserializer(json_data);
+ deserializer.set_allow_trailing_comma(true);
+ int error_code = 0;
+ scoped_ptr<Value> data(deserializer.Deserialize(&error_code, NULL));
+ if (error_code == 0)
+ return data.Pass();
+ }
+ return scoped_ptr<Value>();
+}
+
bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) {
string16 query;
ListValue* root_list = NULL;
« no previous file with comments | « chrome/browser/autocomplete/search_provider.h ('k') | chrome/browser/autocomplete/search_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698