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 "chrome/browser/autocomplete/zero_suggest_provider.h" | 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) { | 126 void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
127 have_pending_request_ = false; | 127 have_pending_request_ = false; |
128 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); | 128 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
129 | 129 |
130 std::string json_data; | 130 std::string json_data; |
131 source->GetResponseAsString(&json_data); | 131 source->GetResponseAsString(&json_data); |
132 const bool request_succeeded = | 132 const bool request_succeeded = |
133 source->GetStatus().is_success() && source->GetResponseCode() == 200; | 133 source->GetStatus().is_success() && source->GetResponseCode() == 200; |
134 | 134 |
135 if (request_succeeded) { | 135 if (request_succeeded) { |
136 JSONStringValueSerializer deserializer(json_data); | 136 // The JSON response should be an array. |
137 deserializer.set_allow_trailing_comma(true); | 137 for (size_t response_start_index = json_data.find("["), i = 0; |
H Fung
2013/11/30 00:46:27
I'm not sure why we try up to 5 times and why we i
| |
138 scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL)); | 138 response_start_index != std::string::npos && i < 5; |
139 if (data.get()) | 139 response_start_index = json_data.find("[", 1), i++) { |
140 ParseSuggestResults(*data.get()); | 140 // Remove any XSSI guards to allow for JSON parsing. |
141 if (response_start_index > 0) | |
142 json_data.erase(0, response_start_index); | |
143 | |
144 JSONStringValueSerializer deserializer(json_data); | |
145 deserializer.set_allow_trailing_comma(true); | |
146 int error_code = 0; | |
147 scoped_ptr<Value> data(deserializer.Deserialize(&error_code, NULL)); | |
148 if (error_code == 0) { | |
149 if (data.get()) | |
150 ParseSuggestResults(*data.get()); | |
151 break; | |
152 } | |
153 } | |
141 } | 154 } |
142 done_ = true; | 155 done_ = true; |
143 | 156 |
144 ConvertResultsToAutocompleteMatches(); | 157 ConvertResultsToAutocompleteMatches(); |
145 if (!matches_.empty()) | 158 if (!matches_.empty()) |
146 listener_->OnProviderUpdate(true); | 159 listener_->OnProviderUpdate(true); |
147 } | 160 } |
148 | 161 |
149 void ZeroSuggestProvider::StartZeroSuggest( | 162 void ZeroSuggestProvider::StartZeroSuggest( |
150 const GURL& current_page_url, | 163 const GURL& current_page_url, |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
455 match.is_history_what_you_typed_match = false; | 468 match.is_history_what_you_typed_match = false; |
456 match.allowed_to_be_default_match = true; | 469 match.allowed_to_be_default_match = true; |
457 | 470 |
458 // The placeholder suggestion for the current URL has high relevance so | 471 // The placeholder suggestion for the current URL has high relevance so |
459 // that it is in the first suggestion slot and inline autocompleted. It | 472 // that it is in the first suggestion slot and inline autocompleted. It |
460 // gets dropped as soon as the user types something. | 473 // gets dropped as soon as the user types something. |
461 match.relevance = verbatim_relevance_; | 474 match.relevance = verbatim_relevance_; |
462 | 475 |
463 return match; | 476 return match; |
464 } | 477 } |
OLD | NEW |