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

Side by Side Diff: chrome/browser/autocomplete/search_provider.cc

Issue 8375039: Create a content::UrlFetcher interface that lives in content/public/common and convert users to i... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: review comments Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 // providers. 237 // providers.
238 DCHECK_GT(suggest_results_pending_, 0); 238 DCHECK_GT(suggest_results_pending_, 0);
239 } 239 }
240 240
241 void SearchProvider::Stop() { 241 void SearchProvider::Stop() {
242 StopSuggest(); 242 StopSuggest();
243 done_ = true; 243 done_ = true;
244 default_provider_suggest_text_.clear(); 244 default_provider_suggest_text_.clear();
245 } 245 }
246 246
247 void SearchProvider::OnURLFetchComplete(const URLFetcher* source) { 247 void SearchProvider::OnURLFetchComplete(const content::URLFetcher* source) {
248 DCHECK(!done_); 248 DCHECK(!done_);
249 suggest_results_pending_--; 249 suggest_results_pending_--;
250 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. 250 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
251 const net::HttpResponseHeaders* const response_headers = 251 const net::HttpResponseHeaders* const response_headers =
252 source->response_headers(); 252 source->GetResponseHeaders();
253 std::string json_data; 253 std::string json_data;
254 source->GetResponseAsString(&json_data); 254 source->GetResponseAsString(&json_data);
255 // 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
256 // 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
257 // the Content-Type header field. 257 // the Content-Type header field.
258 if (response_headers) { 258 if (response_headers) {
259 std::string charset; 259 std::string charset;
260 if (response_headers->GetCharset(&charset)) { 260 if (response_headers->GetCharset(&charset)) {
261 string16 data_16; 261 string16 data_16;
262 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. 262 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
263 if (base::CodepageToUTF16(json_data, charset.c_str(), 263 if (base::CodepageToUTF16(json_data, charset.c_str(),
264 base::OnStringConversionError::FAIL, 264 base::OnStringConversionError::FAIL,
265 &data_16)) 265 &data_16))
266 json_data = UTF16ToUTF8(data_16); 266 json_data = UTF16ToUTF8(data_16);
267 } 267 }
268 } 268 }
269 269
270 bool is_keyword_results = (source == keyword_fetcher_.get()); 270 bool is_keyword_results = (source == keyword_fetcher_.get());
271 SuggestResults* suggest_results = is_keyword_results ? 271 SuggestResults* suggest_results = is_keyword_results ?
272 &keyword_suggest_results_ : &default_suggest_results_; 272 &keyword_suggest_results_ : &default_suggest_results_;
273 273
274 if (source->status().is_success() && source->response_code() == 200) { 274 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
275 JSONStringValueSerializer deserializer(json_data); 275 JSONStringValueSerializer deserializer(json_data);
276 deserializer.set_allow_trailing_comma(true); 276 deserializer.set_allow_trailing_comma(true);
277 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL)); 277 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL));
278 const string16& input_text = 278 const string16& input_text =
279 is_keyword_results ? keyword_input_text_ : input_.text(); 279 is_keyword_results ? keyword_input_text_ : input_.text();
280 have_suggest_results_ = 280 have_suggest_results_ =
281 root_val.get() && 281 root_val.get() &&
282 ParseSuggestResults(root_val.get(), is_keyword_results, input_text, 282 ParseSuggestResults(root_val.get(), is_keyword_results, input_text,
283 suggest_results); 283 suggest_results);
284 } 284 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 // Stop any in-progress URL fetches. 425 // Stop any in-progress URL fetches.
426 keyword_fetcher_.reset(); 426 keyword_fetcher_.reset();
427 default_fetcher_.reset(); 427 default_fetcher_.reset();
428 keyword_suggest_results_.clear(); 428 keyword_suggest_results_.clear();
429 default_suggest_results_.clear(); 429 default_suggest_results_.clear();
430 keyword_navigation_results_.clear(); 430 keyword_navigation_results_.clear();
431 default_navigation_results_.clear(); 431 default_navigation_results_.clear();
432 have_suggest_results_ = false; 432 have_suggest_results_ = false;
433 } 433 }
434 434
435 URLFetcher* SearchProvider::CreateSuggestFetcher(int id, 435 content::URLFetcher* SearchProvider::CreateSuggestFetcher(
436 const TemplateURL& provider, 436 int id,
437 const string16& text) { 437 const TemplateURL& provider,
438 const string16& text) {
438 const TemplateURLRef* const suggestions_url = provider.suggestions_url(); 439 const TemplateURLRef* const suggestions_url = provider.suggestions_url();
439 DCHECK(suggestions_url->SupportsReplacement()); 440 DCHECK(suggestions_url->SupportsReplacement());
440 URLFetcher* fetcher = URLFetcher::Create(id, 441 URLFetcher* fetcher = URLFetcher::Create(id,
441 GURL(suggestions_url->ReplaceSearchTermsUsingProfile(profile_, provider, 442 GURL(suggestions_url->ReplaceSearchTermsUsingProfile(profile_, provider,
442 text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), 443 text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())),
443 URLFetcher::GET, this); 444 URLFetcher::GET, this);
444 fetcher->set_request_context(profile_->GetRequestContext()); 445 fetcher->SetRequestContext(profile_->GetRequestContext());
445 fetcher->Start(); 446 fetcher->Start();
446 return fetcher; 447 return fetcher;
447 } 448 }
448 449
449 bool SearchProvider::ParseSuggestResults(Value* root_val, 450 bool SearchProvider::ParseSuggestResults(Value* root_val,
450 bool is_keyword, 451 bool is_keyword,
451 const string16& input_text, 452 const string16& input_text,
452 SuggestResults* suggest_results) { 453 SuggestResults* suggest_results) {
453 if (!root_val->IsType(Value::TYPE_LIST)) 454 if (!root_val->IsType(Value::TYPE_LIST))
454 return false; 455 return false;
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after
939 940
940 return match; 941 return match;
941 } 942 }
942 943
943 void SearchProvider::UpdateDone() { 944 void SearchProvider::UpdateDone() {
944 // We're done when there are no more suggest queries pending (this is set to 1 945 // We're done when there are no more suggest queries pending (this is set to 1
945 // when the timer is started) and we're not waiting on instant. 946 // when the timer is started) and we're not waiting on instant.
946 done_ = ((suggest_results_pending_ == 0) && 947 done_ = ((suggest_results_pending_ == 0) &&
947 (instant_finalized_ || !InstantController::IsEnabled(profile_))); 948 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
948 } 949 }
OLDNEW
« 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