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

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

Issue 10033017: More misc. cleanups to minimize future refactoring diffs. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months 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) 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 found_word = true; 55 found_word = true;
56 } 56 }
57 } 57 }
58 } 58 }
59 return false; 59 return false;
60 } 60 }
61 61
62 }; 62 };
63 63
64 64
65 // SearchProvider ------------------------------------------------------------- 65 // SearchProvider::Providers --------------------------------------------------
66
67 // static
68 const int SearchProvider::kDefaultProviderURLFetcherID = 1;
69 // static
70 const int SearchProvider::kKeywordProviderURLFetcherID = 2;
71 // static
72 bool SearchProvider::query_suggest_immediately_ = false;
73 66
74 void SearchProvider::Providers::Set(const TemplateURL* default_provider, 67 void SearchProvider::Providers::Set(const TemplateURL* default_provider,
75 const TemplateURL* keyword_provider) { 68 const TemplateURL* keyword_provider) {
76 // TODO(pkasting): http://b/1162970 We shouldn't need to structure-copy 69 // TODO(pkasting): http://b/1162970 We shouldn't need to structure-copy
77 // this. Nor should we need |default_provider_| and |keyword_provider_| 70 // this. Nor should we need |default_provider_| and |keyword_provider_|
78 // just to know whether the provider changed. 71 // just to know whether the provider changed.
79 default_provider_ = default_provider; 72 default_provider_ = default_provider;
80 if (default_provider) 73 if (default_provider)
81 cached_default_provider_ = *default_provider; 74 cached_default_provider_ = *default_provider;
82 keyword_provider_ = keyword_provider; 75 keyword_provider_ = keyword_provider;
83 if (keyword_provider) 76 if (keyword_provider)
84 cached_keyword_provider_ = *keyword_provider; 77 cached_keyword_provider_ = *keyword_provider;
85 } 78 }
86 79
80
81 // SearchProvider -------------------------------------------------------------
82
83 // static
84 const int SearchProvider::kDefaultProviderURLFetcherID = 1;
85 // static
86 const int SearchProvider::kKeywordProviderURLFetcherID = 2;
87 // static
88 bool SearchProvider::query_suggest_immediately_ = false;
89
87 SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile) 90 SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
88 : AutocompleteProvider(listener, profile, "Search"), 91 : AutocompleteProvider(listener, profile, "Search"),
89 suggest_results_pending_(0), 92 suggest_results_pending_(0),
90 have_suggest_results_(false), 93 have_suggest_results_(false),
91 instant_finalized_(false) { 94 instant_finalized_(false) {
92 // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field 95 // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field
93 // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1 96 // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1
94 // (inclusive). 97 // (inclusive).
95 int suggest_field_trial_group_number = 98 int suggest_field_trial_group_number =
96 AutocompleteFieldTrial::GetSuggestNumberOfGroups(); 99 AutocompleteFieldTrial::GetSuggestNumberOfGroups();
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 bool operator()(const ScoredTerm& a, const ScoredTerm& b) { 238 bool operator()(const ScoredTerm& a, const ScoredTerm& b) {
236 // Sort in descending relevance order. 239 // Sort in descending relevance order.
237 return a.second > b.second; 240 return a.second > b.second;
238 } 241 }
239 }; 242 };
240 243
241 void SearchProvider::Run() { 244 void SearchProvider::Run() {
242 // Start a new request with the current input. 245 // Start a new request with the current input.
243 DCHECK(!done_); 246 DCHECK(!done_);
244 suggest_results_pending_ = 0; 247 suggest_results_pending_ = 0;
248 if (providers_.valid_suggest_for_default_provider()) {
249 suggest_results_pending_++;
250 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
251 providers_.default_provider().suggestions_url_ref(), input_.text()));
252 }
245 if (providers_.valid_suggest_for_keyword_provider()) { 253 if (providers_.valid_suggest_for_keyword_provider()) {
246 suggest_results_pending_++; 254 suggest_results_pending_++;
247 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, 255 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
248 providers_.keyword_provider(), keyword_input_text_)); 256 providers_.keyword_provider().suggestions_url_ref(),
249 } 257 keyword_input_text_));
250 if (providers_.valid_suggest_for_default_provider()) {
251 suggest_results_pending_++;
252 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
253 providers_.default_provider(), input_.text()));
254 } 258 }
255 // We should only get here if we have a suggest url for the keyword or default 259 // We should only get here if we have a suggest url for the keyword or default
256 // providers. 260 // providers.
257 DCHECK_GT(suggest_results_pending_, 0); 261 DCHECK_GT(suggest_results_pending_, 0);
258 } 262 }
259 263
260 void SearchProvider::Stop() { 264 void SearchProvider::Stop() {
261 StopSuggest(); 265 StopSuggest();
262 done_ = true; 266 done_ = true;
263 default_provider_suggest_text_.clear(); 267 default_provider_suggest_text_.clear();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 // Request history for both the keyword and default provider. We grab many 332 // Request history for both the keyword and default provider. We grab many
329 // more matches than we'll ultimately clamp to so that if there are several 333 // more matches than we'll ultimately clamp to so that if there are several
330 // recent multi-word matches who scores are lowered (see 334 // recent multi-word matches who scores are lowered (see
331 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring 335 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
332 // matches. Note that this doesn't fix the problem entirely, but merely 336 // matches. Note that this doesn't fix the problem entirely, but merely
333 // limits it to cases with a very large number of such multi-word matches; for 337 // limits it to cases with a very large number of such multi-word matches; for
334 // now, this seems OK compared with the complexity of a real fix, which would 338 // now, this seems OK compared with the complexity of a real fix, which would
335 // require multiple searches and tracking of "single- vs. multi-word" in the 339 // require multiple searches and tracking of "single- vs. multi-word" in the
336 // database. 340 // database.
337 int num_matches = kMaxMatches * 5; 341 int num_matches = kMaxMatches * 5;
342 if (providers_.valid_default_provider()) {
343 url_db->GetMostRecentKeywordSearchTerms(providers_.default_provider().id(),
344 input_.text(), num_matches, &default_history_results_);
345 }
338 if (providers_.valid_keyword_provider()) { 346 if (providers_.valid_keyword_provider()) {
339 url_db->GetMostRecentKeywordSearchTerms(providers_.keyword_provider().id(), 347 url_db->GetMostRecentKeywordSearchTerms(providers_.keyword_provider().id(),
340 keyword_input_text_, num_matches, &keyword_history_results_); 348 keyword_input_text_, num_matches, &keyword_history_results_);
341 } 349 }
342 if (providers_.valid_default_provider()) {
343 url_db->GetMostRecentKeywordSearchTerms(providers_.default_provider().id(),
344 input_.text(), num_matches, &default_history_results_);
345 }
346 } 350 }
347 351
348 void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { 352 void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
349 // Don't send any queries to the server until some time has elapsed after 353 // Don't send any queries to the server until some time has elapsed after
350 // the last keypress, to avoid flooding the server with requests we are 354 // the last keypress, to avoid flooding the server with requests we are
351 // likely to end up throwing away anyway. 355 // likely to end up throwing away anyway.
352 const int kQueryDelayMs = 200; 356 const int kQueryDelayMs = 200;
353 357
354 if (!IsQuerySuitableForSuggest()) { 358 if (!IsQuerySuitableForSuggest()) {
355 StopSuggest(); 359 StopSuggest();
(...skipping 23 matching lines...) Expand all
379 suggest_results_pending_ = 1; 383 suggest_results_pending_ = 1;
380 384
381 // Kick off a timer that will start the URL fetch if it completes before 385 // Kick off a timer that will start the URL fetch if it completes before
382 // the user types another character. 386 // the user types another character.
383 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs; 387 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
384 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this, 388 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this,
385 &SearchProvider::Run); 389 &SearchProvider::Run);
386 } 390 }
387 391
388 bool SearchProvider::IsQuerySuitableForSuggest() const { 392 bool SearchProvider::IsQuerySuitableForSuggest() const {
389 // Don't run Suggest in incognito mode, the engine doesn't support it, or 393 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
390 // the user has disabled it. 394 // if the user has disabled it.
391 if (profile_->IsOffTheRecord() || 395 if (profile_->IsOffTheRecord() ||
392 (!providers_.valid_suggest_for_keyword_provider() && 396 (!providers_.valid_suggest_for_default_provider() &&
393 !providers_.valid_suggest_for_default_provider()) || 397 !providers_.valid_suggest_for_keyword_provider()) ||
394 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) 398 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
395 return false; 399 return false;
396 400
397 // If the input type might be a URL, we take extra care so that private data 401 // If the input type might be a URL, we take extra care so that private data
398 // isn't sent to the server. 402 // isn't sent to the server.
399 403
400 // FORCED_QUERY means the user is explicitly asking us to search for this, so 404 // FORCED_QUERY means the user is explicitly asking us to search for this, so
401 // we assume it isn't a URL and/or there isn't private data. 405 // we assume it isn't a URL and/or there isn't private data.
402 if (input_.type() == AutocompleteInput::FORCED_QUERY) 406 if (input_.type() == AutocompleteInput::FORCED_QUERY)
403 return true; 407 return true;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 default_fetcher_.reset(); 450 default_fetcher_.reset();
447 keyword_suggest_results_.clear(); 451 keyword_suggest_results_.clear();
448 default_suggest_results_.clear(); 452 default_suggest_results_.clear();
449 keyword_navigation_results_.clear(); 453 keyword_navigation_results_.clear();
450 default_navigation_results_.clear(); 454 default_navigation_results_.clear();
451 have_suggest_results_ = false; 455 have_suggest_results_ = false;
452 } 456 }
453 457
454 content::URLFetcher* SearchProvider::CreateSuggestFetcher( 458 content::URLFetcher* SearchProvider::CreateSuggestFetcher(
455 int id, 459 int id,
456 const TemplateURL& provider, 460 const TemplateURLRef& suggestions_url,
457 const string16& text) { 461 const string16& text) {
458 const TemplateURLRef& suggestions_url = provider.suggestions_url_ref();
459 DCHECK(suggestions_url.SupportsReplacement()); 462 DCHECK(suggestions_url.SupportsReplacement());
460 content::URLFetcher* fetcher = content::URLFetcher::Create(id, 463 content::URLFetcher* fetcher = content::URLFetcher::Create(id,
461 GURL(suggestions_url.ReplaceSearchTermsUsingProfile( 464 GURL(suggestions_url.ReplaceSearchTermsUsingProfile(
462 profile_, text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, 465 profile_, text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
463 string16())), 466 string16())),
464 content::URLFetcher::GET, this); 467 content::URLFetcher::GET, this);
465 fetcher->SetRequestContext(profile_->GetRequestContext()); 468 fetcher->SetRequestContext(profile_->GetRequestContext());
466 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); 469 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
467 fetcher->Start(); 470 fetcher->Start();
468 return fetcher; 471 return fetcher;
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 void SearchProvider::AddMatchToMap(const string16& query_string, 834 void SearchProvider::AddMatchToMap(const string16& query_string,
832 const string16& input_text, 835 const string16& input_text,
833 int relevance, 836 int relevance,
834 AutocompleteMatch::Type type, 837 AutocompleteMatch::Type type,
835 int accepted_suggestion, 838 int accepted_suggestion,
836 bool is_keyword, 839 bool is_keyword,
837 bool prevent_inline_autocomplete, 840 bool prevent_inline_autocomplete,
838 MatchMap* map) { 841 MatchMap* map) {
839 AutocompleteMatch match(this, relevance, false, type); 842 AutocompleteMatch match(this, relevance, false, type);
840 std::vector<size_t> content_param_offsets; 843 std::vector<size_t> content_param_offsets;
841 const TemplateURL& provider = is_keyword ? providers_.keyword_provider() : 844 const TemplateURL& provider = is_keyword ?
842 providers_.default_provider(); 845 providers_.keyword_provider() : providers_.default_provider();
843 match.template_url = &provider; 846 match.template_url = &provider;
844 match.contents.assign(query_string); 847 match.contents.assign(query_string);
845 // We do intra-string highlighting for suggestions - the suggested segment 848 // We do intra-string highlighting for suggestions - the suggested segment
846 // will be highlighted, e.g. for input_text = "you" the suggestion may be 849 // will be highlighted, e.g. for input_text = "you" the suggestion may be
847 // "youtube", so we'll bold the "tube" section: you*tube*. 850 // "youtube", so we'll bold the "tube" section: you*tube*.
848 if (input_text != query_string) { 851 if (input_text != query_string) {
849 size_t input_position = match.contents.find(input_text); 852 size_t input_position = match.contents.find(input_text);
850 if (input_position == string16::npos) { 853 if (input_position == string16::npos) {
851 // The input text is not a substring of the query string, e.g. input 854 // The input text is not a substring of the query string, e.g. input
852 // text is "slasdot" and the query string is "slashdot", so we bold the 855 // text is "slasdot" and the query string is "slashdot", so we bold the
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 964
962 return match; 965 return match;
963 } 966 }
964 967
965 void SearchProvider::UpdateDone() { 968 void SearchProvider::UpdateDone() {
966 // We're done when there are no more suggest queries pending (this is set to 1 969 // We're done when there are no more suggest queries pending (this is set to 1
967 // when the timer is started) and we're not waiting on instant. 970 // when the timer is started) and we're not waiting on instant.
968 done_ = ((suggest_results_pending_ == 0) && 971 done_ = ((suggest_results_pending_ == 0) &&
969 (instant_finalized_ || !InstantController::IsEnabled(profile_))); 972 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
970 } 973 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698