| 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/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 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // Sort in descending relevance order. | 305 // Sort in descending relevance order. |
| 306 return a.relevance() > b.relevance(); | 306 return a.relevance() > b.relevance(); |
| 307 } | 307 } |
| 308 }; | 308 }; |
| 309 | 309 |
| 310 void SearchProvider::Run() { | 310 void SearchProvider::Run() { |
| 311 // Start a new request with the current input. | 311 // Start a new request with the current input. |
| 312 DCHECK(!done_); | 312 DCHECK(!done_); |
| 313 suggest_results_pending_ = 0; | 313 suggest_results_pending_ = 0; |
| 314 time_suggest_request_sent_ = base::TimeTicks::Now(); | 314 time_suggest_request_sent_ = base::TimeTicks::Now(); |
| 315 const TemplateURL* default_url = providers_.GetDefaultProviderURL(); | 315 |
| 316 if (default_url && !default_url->suggestions_url().empty()) { | 316 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
| 317 suggest_results_pending_++; | 317 providers_.GetDefaultProviderURL(), input_.text())); |
| 318 LogOmniboxSuggestRequest(REQUEST_SENT); | 318 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
| 319 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, | 319 providers_.GetKeywordProviderURL(), keyword_input_text_)); |
| 320 default_url->suggestions_url_ref(), input_.text())); | |
| 321 } | |
| 322 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | |
| 323 if (keyword_url && !keyword_url->suggestions_url().empty()) { | |
| 324 suggest_results_pending_++; | |
| 325 LogOmniboxSuggestRequest(REQUEST_SENT); | |
| 326 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, | |
| 327 keyword_url->suggestions_url_ref(), keyword_input_text_)); | |
| 328 } | |
| 329 | 320 |
| 330 // Both the above can fail if the providers have been modified or deleted | 321 // Both the above can fail if the providers have been modified or deleted |
| 331 // since the query began. | 322 // since the query began. |
| 332 if (suggest_results_pending_ == 0) { | 323 if (suggest_results_pending_ == 0) { |
| 333 UpdateDone(); | 324 UpdateDone(); |
| 334 // We only need to update the listener if we're actually done. | 325 // We only need to update the listener if we're actually done. |
| 335 if (done_) | 326 if (done_) |
| 336 listener_->OnProviderUpdate(false); | 327 listener_->OnProviderUpdate(false); |
| 337 } | 328 } |
| 338 } | 329 } |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 void SearchProvider::ApplyCalculatedNavigationRelevance(NavigationResults* list, | 625 void SearchProvider::ApplyCalculatedNavigationRelevance(NavigationResults* list, |
| 635 bool is_keyword) { | 626 bool is_keyword) { |
| 636 for (size_t i = 0; i < list->size(); ++i) { | 627 for (size_t i = 0; i < list->size(); ++i) { |
| 637 (*list)[i].set_relevance(CalculateRelevanceForNavigation(is_keyword) + | 628 (*list)[i].set_relevance(CalculateRelevanceForNavigation(is_keyword) + |
| 638 (list->size() - i - 1)); | 629 (list->size() - i - 1)); |
| 639 } | 630 } |
| 640 } | 631 } |
| 641 | 632 |
| 642 net::URLFetcher* SearchProvider::CreateSuggestFetcher( | 633 net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
| 643 int id, | 634 int id, |
| 644 const TemplateURLRef& suggestions_url, | 635 const TemplateURL* template_url, |
| 645 const string16& text) { | 636 const string16& text) { |
| 646 DCHECK(suggestions_url.SupportsReplacement()); | 637 if (!template_url || template_url->suggestions_url().empty()) |
| 647 net::URLFetcher* fetcher = net::URLFetcher::Create(id, | 638 return NULL; |
| 648 GURL(suggestions_url.ReplaceSearchTerms( | 639 |
| 649 TemplateURLRef::SearchTermsArgs(text))), | 640 // Bail if the suggestion URL is invalid with the given replacements. |
| 650 net::URLFetcher::GET, this); | 641 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( |
| 642 TemplateURLRef::SearchTermsArgs(text))); |
| 643 if (!suggest_url.is_valid()) |
| 644 return NULL; |
| 645 |
| 646 suggest_results_pending_++; |
| 647 LogOmniboxSuggestRequest(REQUEST_SENT); |
| 648 |
| 649 net::URLFetcher* fetcher = |
| 650 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); |
| 651 fetcher->SetRequestContext(profile_->GetRequestContext()); | 651 fetcher->SetRequestContext(profile_->GetRequestContext()); |
| 652 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 652 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 653 fetcher->Start(); | 653 fetcher->Start(); |
| 654 return fetcher; | 654 return fetcher; |
| 655 } | 655 } |
| 656 | 656 |
| 657 bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) { | 657 bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) { |
| 658 // TODO(pkasting): Fix |have_suggest_results_|; see http://crbug.com/130631 | 658 // TODO(pkasting): Fix |have_suggest_results_|; see http://crbug.com/130631 |
| 659 have_suggest_results_ = false; | 659 have_suggest_results_ = false; |
| 660 | 660 |
| (...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1232 return match; | 1232 return match; |
| 1233 } | 1233 } |
| 1234 | 1234 |
| 1235 void SearchProvider::UpdateDone() { | 1235 void SearchProvider::UpdateDone() { |
| 1236 // We're done when the timer isn't running, there are no suggest queries | 1236 // We're done when the timer isn't running, there are no suggest queries |
| 1237 // pending, and we're not waiting on instant. | 1237 // pending, and we're not waiting on instant. |
| 1238 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && | 1238 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && |
| 1239 (instant_finalized_ || | 1239 (instant_finalized_ || |
| 1240 !InstantController::IsSuggestEnabled(profile_))); | 1240 !InstantController::IsSuggestEnabled(profile_))); |
| 1241 } | 1241 } |
| OLD | NEW |