OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "chrome/browser/history/history.h" | 21 #include "chrome/browser/history/history.h" |
22 #include "chrome/browser/instant/instant_controller.h" | 22 #include "chrome/browser/instant/instant_controller.h" |
23 #include "chrome/browser/net/url_fixer_upper.h" | 23 #include "chrome/browser/net/url_fixer_upper.h" |
24 #include "chrome/browser/prefs/pref_service.h" | 24 #include "chrome/browser/prefs/pref_service.h" |
25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/history/in_memory_database.h" | 26 #include "chrome/browser/history/in_memory_database.h" |
27 #include "chrome/browser/search_engines/template_url_service.h" | 27 #include "chrome/browser/search_engines/template_url_service.h" |
28 #include "chrome/browser/search_engines/template_url_service_factory.h" | 28 #include "chrome/browser/search_engines/template_url_service_factory.h" |
29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
30 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 31 #include "content/common/net/url_fetcher.h" |
31 #include "googleurl/src/url_util.h" | 32 #include "googleurl/src/url_util.h" |
32 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
33 #include "net/base/escape.h" | 34 #include "net/base/escape.h" |
34 #include "net/http/http_response_headers.h" | 35 #include "net/http/http_response_headers.h" |
35 #include "net/url_request/url_request_status.h" | 36 #include "net/url_request/url_request_status.h" |
36 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
37 | 38 |
38 using base::Time; | 39 using base::Time; |
39 using base::TimeDelta; | 40 using base::TimeDelta; |
40 | 41 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // providers. | 237 // providers. |
237 DCHECK_GT(suggest_results_pending_, 0); | 238 DCHECK_GT(suggest_results_pending_, 0); |
238 } | 239 } |
239 | 240 |
240 void SearchProvider::Stop() { | 241 void SearchProvider::Stop() { |
241 StopSuggest(); | 242 StopSuggest(); |
242 done_ = true; | 243 done_ = true; |
243 default_provider_suggest_text_.clear(); | 244 default_provider_suggest_text_.clear(); |
244 } | 245 } |
245 | 246 |
246 void SearchProvider::OnURLFetchComplete(const URLFetcher* source, | 247 void SearchProvider::OnURLFetchComplete(const URLFetcher* source) { |
247 const GURL& url, | |
248 const net::URLRequestStatus& status, | |
249 int response_code, | |
250 const net::ResponseCookies& cookie, | |
251 const std::string& data) { | |
252 DCHECK(!done_); | 248 DCHECK(!done_); |
253 suggest_results_pending_--; | 249 suggest_results_pending_--; |
254 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. | 250 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. |
255 const net::HttpResponseHeaders* const response_headers = | 251 const net::HttpResponseHeaders* const response_headers = |
256 source->response_headers(); | 252 source->response_headers(); |
257 std::string json_data(data); | 253 std::string json_data; |
| 254 source->GetResponseAsString(&json_data); |
258 // JSON is supposed to be UTF-8, but some suggest service providers send JSON | 255 // JSON is supposed to be UTF-8, but some suggest service providers send JSON |
259 // files in non-UTF-8 encodings. The actual encoding is usually specified in | 256 // files in non-UTF-8 encodings. The actual encoding is usually specified in |
260 // the Content-Type header field. | 257 // the Content-Type header field. |
261 if (response_headers) { | 258 if (response_headers) { |
262 std::string charset; | 259 std::string charset; |
263 if (response_headers->GetCharset(&charset)) { | 260 if (response_headers->GetCharset(&charset)) { |
264 string16 data_16; | 261 string16 data_16; |
265 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. | 262 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
266 if (base::CodepageToUTF16(data, charset.c_str(), | 263 if (base::CodepageToUTF16(json_data, charset.c_str(), |
267 base::OnStringConversionError::FAIL, | 264 base::OnStringConversionError::FAIL, |
268 &data_16)) | 265 &data_16)) |
269 json_data = UTF16ToUTF8(data_16); | 266 json_data = UTF16ToUTF8(data_16); |
270 } | 267 } |
271 } | 268 } |
272 | 269 |
273 bool is_keyword_results = (source == keyword_fetcher_.get()); | 270 bool is_keyword_results = (source == keyword_fetcher_.get()); |
274 SuggestResults* suggest_results = is_keyword_results ? | 271 SuggestResults* suggest_results = is_keyword_results ? |
275 &keyword_suggest_results_ : &default_suggest_results_; | 272 &keyword_suggest_results_ : &default_suggest_results_; |
276 | 273 |
277 if (status.is_success() && response_code == 200) { | 274 if (source->status().is_success() && source->response_code() == 200) { |
278 JSONStringValueSerializer deserializer(json_data); | 275 JSONStringValueSerializer deserializer(json_data); |
279 deserializer.set_allow_trailing_comma(true); | 276 deserializer.set_allow_trailing_comma(true); |
280 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL)); | 277 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL)); |
281 const string16& input_text = | 278 const string16& input_text = |
282 is_keyword_results ? keyword_input_text_ : input_.text(); | 279 is_keyword_results ? keyword_input_text_ : input_.text(); |
283 have_suggest_results_ = | 280 have_suggest_results_ = |
284 root_val.get() && | 281 root_val.get() && |
285 ParseSuggestResults(root_val.get(), is_keyword_results, input_text, | 282 ParseSuggestResults(root_val.get(), is_keyword_results, input_text, |
286 suggest_results); | 283 suggest_results); |
287 } | 284 } |
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 | 939 |
943 return match; | 940 return match; |
944 } | 941 } |
945 | 942 |
946 void SearchProvider::UpdateDone() { | 943 void SearchProvider::UpdateDone() { |
947 // We're done when there are no more suggest queries pending (this is set to 1 | 944 // We're done when there are no more suggest queries pending (this is set to 1 |
948 // when the timer is started) and we're not waiting on instant. | 945 // when the timer is started) and we're not waiting on instant. |
949 done_ = ((suggest_results_pending_ == 0) && | 946 done_ = ((suggest_results_pending_ == 0) && |
950 (instant_finalized_ || !InstantController::IsEnabled(profile_))); | 947 (instant_finalized_ || !InstantController::IsEnabled(profile_))); |
951 } | 948 } |
OLD | NEW |