history_url_provider.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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 3// found in the LICENSE file. 4 5#include "chrome/browser/autocomplete/history_url_provider.h" 6 7#include <algorithm> 8 9#include "base/basictypes.h" 10#include "base/bind.h" 11#include "base/command_line.h" 12#include "base/message_loop/message_loop.h" 13#include "base/metrics/histogram.h" 14#include "base/prefs/pref_service.h" 15#include "base/strings/string_util.h" 16#include "base/strings/utf_string_conversions.h" 17#include "chrome/browser/autocomplete/autocomplete_match.h" 18#include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 19#include "chrome/browser/history/history_backend.h" 20#include "chrome/browser/history/history_database.h" 21#include "chrome/browser/history/history_service.h" 22#include "chrome/browser/history/history_service_factory.h" 23#include "chrome/browser/history/history_types.h" 24#include "chrome/browser/omnibox/omnibox_field_trial.h" 25#include "chrome/browser/profiles/profile.h" 26#include "chrome/browser/search_engines/template_url_service.h" 27#include "chrome/browser/search_engines/template_url_service_factory.h" 28#include "chrome/common/chrome_switches.h" 29#include "chrome/common/net/url_fixer_upper.h" 30#include "chrome/common/pref_names.h" 31#include "chrome/common/url_constants.h" 32#include "net/base/net_util.h" 33#include "net/base/registry_controlled_domains/registry_controlled_domain.h" 34#include "url/gurl.h" 35#include "url/url_parse.h" 36#include "url/url_util.h" 37 38namespace { 39 40// If |create_if_necessary| is true, ensures that |matches| contains an 41// entry for |info|, creating a new such entry if necessary (using 42// |input_location| and |match_in_scheme|). 43// 44// If |promote| is true, this also ensures the entry is the first element in 45// |matches|, moving or adding it to the front as appropriate. When |promote| 46// is false, existing matches are left in place, and newly added matches are 47// placed at the back. 48// 49// It's OK to call this function with both |create_if_necessary| and 50// |promote| false, in which case we'll do nothing. 51// 52// Returns whether the match exists regardless if it was promoted/created. 53bool CreateOrPromoteMatch(const history::URLRow& info, 54 size_t input_location, 55 bool match_in_scheme, 56 history::HistoryMatches* matches, 57 bool create_if_necessary, 58 bool promote) { 59 // |matches| may already have an entry for this. 60 for (history::HistoryMatches::iterator i(matches->begin()); 61 i != matches->end(); ++i) { 62 if (i->url_info.url() == info.url()) { 63 // Rotate it to the front if the caller wishes. 64 if (promote) 65 std::rotate(matches->begin(), i, i + 1); 66 return true; 67 } 68 } 69 70 if (!create_if_necessary) 71 return false; 72 73 // No entry, so create one. 74 history::HistoryMatch match(info, input_location, match_in_scheme, true); 75 if (promote) 76 matches->push_front(match); 77 else 78 matches->push_back(match); 79 80 return true; 81} 82 83// Given the user's |input| and a |match| created from it, reduce the match's 84// URL to just a host. If this host still matches the user input, return it. 85// Returns the empty string on failure. 86GURL ConvertToHostOnly(const history::HistoryMatch& match, 87 const string16& input) { 88 // See if we should try to do host-only suggestions for this URL. Nonstandard 89 // schemes means there's no authority section, so suggesting the host name 90 // is useless. File URLs are standard, but host suggestion is not useful for 91 // them either. 92 const GURL& url = match.url_info.url(); 93 if (!url.is_valid() || !url.IsStandard() || url.SchemeIsFile()) 94 return GURL(); 95 96 // Transform to a host-only match. Bail if the host no longer matches the 97 // user input (e.g. because the user typed more than just a host). 98 GURL host = url.GetWithEmptyPath(); 99 if ((host.spec().length() < (match.input_location + input.length()))) 100 return GURL(); // User typing is longer than this host suggestion. 101 102 const string16 spec = UTF8ToUTF16(host.spec()); 103 if (spec.compare(match.input_location, input.length(), input)) 104 return GURL(); // User typing is no longer a prefix. 105 106 return host; 107} 108 109// Acts like the > operator for URLInfo classes. 110bool CompareHistoryMatch(const history::HistoryMatch& a, 111 const history::HistoryMatch& b) { 112 // A promoted match is better than non-promoted. 113 if (a.promoted != b.promoted) 114 return a.promoted; 115 116 // A URL that has been typed at all is better than one that has never been 117 // typed. (Note "!"s on each side) 118 if (!a.url_info.typed_count() != !b.url_info.typed_count()) 119 return a.url_info.typed_count() > b.url_info.typed_count(); 120 121 // Innermost matches (matches after any scheme or "www.") are better than 122 // non-innermost matches. 123 if (a.innermost_match != b.innermost_match) 124 return a.innermost_match; 125 126 // URLs that have been typed more often are better. 127 if (a.url_info.typed_count() != b.url_info.typed_count()) 128 return a.url_info.typed_count() > b.url_info.typed_count(); 129 130 // For URLs that have each been typed once, a host (alone) is better than a 131 // page inside. 132 if ((a.url_info.typed_count() == 1) && (a.IsHostOnly() != b.IsHostOnly())) 133 return a.IsHostOnly(); 134 135 // URLs that have been visited more often are better. 136 if (a.url_info.visit_count() != b.url_info.visit_count()) 137 return a.url_info.visit_count() > b.url_info.visit_count(); 138 139 // URLs that have been visited more recently are better. 140 return a.url_info.last_visit() > b.url_info.last_visit(); 141} 142 143// Sorts and dedups the given list of matches. 144void SortAndDedupMatches(history::HistoryMatches* matches) { 145 // Sort by quality, best first. 146 std::sort(matches->begin(), matches->end(), &CompareHistoryMatch); 147 148 // Remove duplicate matches (caused by the search string appearing in one of 149 // the prefixes as well as after it). Consider the following scenario: 150 // 151 // User has visited "http://http.com" once and "http://htaccess.com" twice. 152 // User types "http". The autocomplete search with prefix "http://" returns 153 // the first host, while the search with prefix "" returns both hosts. Now 154 // we sort them into rank order: 155 // http://http.com (innermost_match) 156 // http://htaccess.com (!innermost_match, url_info.visit_count == 2) 157 // http://http.com (!innermost_match, url_info.visit_count == 1) 158 // 159 // The above scenario tells us we can't use std::unique(), since our 160 // duplicates are not always sequential. It also tells us we should remove 161 // the lower-quality duplicate(s), since otherwise the returned results won't 162 // be ordered correctly. This is easy to do: we just always remove the later 163 // element of a duplicate pair. 164 // Be careful! Because the vector contents may change as we remove elements, 165 // we use an index instead of an iterator in the outer loop, and don't 166 // precalculate the ending position. 167 for (size_t i = 0; i < matches->size(); ++i) { 168 for (history::HistoryMatches::iterator j(matches->begin() + i + 1); 169 j != matches->end(); ) { 170 if ((*matches)[i].url_info.url() == j->url_info.url()) 171 j = matches->erase(j); 172 else 173 ++j; 174 } 175 } 176} 177 178// Extracts typed_count, visit_count, and last_visited time from the 179// URLRow and puts them in the additional info field of the |match| 180// for display in about:omnibox. 181void RecordAdditionalInfoFromUrlRow(const history::URLRow& info, 182 AutocompleteMatch* match) { 183 match->RecordAdditionalInfo("typed count", info.typed_count()); 184 match->RecordAdditionalInfo("visit count", info.visit_count()); 185 match->RecordAdditionalInfo("last visit", info.last_visit()); 186} 187 188} // namespace 189 190// ----------------------------------------------------------------- 191// SearchTermsDataSnapshot 192 193// Implementation of SearchTermsData that takes a snapshot of another 194// SearchTermsData by copying all the responses to the different getters into 195// member strings, then returning those strings when its own getters are called. 196// This will typically be constructed on the UI thread from 197// UIThreadSearchTermsData but is subsequently safe to use on any thread. 198class SearchTermsDataSnapshot : public SearchTermsData { 199 public: 200 explicit SearchTermsDataSnapshot(const SearchTermsData& search_terms_data); 201 virtual ~SearchTermsDataSnapshot(); 202 203 virtual std::string GoogleBaseURLValue() const OVERRIDE; 204 virtual std::string GetApplicationLocale() const OVERRIDE; 205 virtual string16 GetRlzParameterValue() const OVERRIDE; 206 virtual std::string GetSearchClient() const OVERRIDE; 207 virtual std::string InstantEnabledParam() const OVERRIDE; 208 virtual std::string InstantExtendedEnabledParam() const OVERRIDE; 209 virtual std::string NTPIsThemedParam() const OVERRIDE; 210 211 private: 212 std::string google_base_url_value_; 213 std::string application_locale_; 214 string16 rlz_parameter_value_; 215 std::string search_client_; 216 std::string instant_enabled_param_; 217 std::string instant_extended_enabled_param_; 218 std::string ntp_is_themed_param_; 219 220 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot); 221}; 222 223SearchTermsDataSnapshot::SearchTermsDataSnapshot( 224 const SearchTermsData& search_terms_data) 225 : google_base_url_value_(search_terms_data.GoogleBaseURLValue()), 226 application_locale_(search_terms_data.GetApplicationLocale()), 227 rlz_parameter_value_(search_terms_data.GetRlzParameterValue()), 228 search_client_(search_terms_data.GetSearchClient()), 229 instant_enabled_param_(search_terms_data.InstantEnabledParam()), 230 instant_extended_enabled_param_( 231 search_terms_data.InstantExtendedEnabledParam()), 232 ntp_is_themed_param_(search_terms_data.NTPIsThemedParam()) {} 233 234SearchTermsDataSnapshot::~SearchTermsDataSnapshot() { 235} 236 237std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const { 238 return google_base_url_value_; 239} 240 241std::string SearchTermsDataSnapshot::GetApplicationLocale() const { 242 return application_locale_; 243} 244 245string16 SearchTermsDataSnapshot::GetRlzParameterValue() const { 246 return rlz_parameter_value_; 247} 248 249std::string SearchTermsDataSnapshot::GetSearchClient() const { 250 return search_client_; 251} 252 253std::string SearchTermsDataSnapshot::InstantEnabledParam() const { 254 return instant_enabled_param_; 255} 256 257std::string SearchTermsDataSnapshot::InstantExtendedEnabledParam() const { 258 return instant_extended_enabled_param_; 259} 260 261std::string SearchTermsDataSnapshot::NTPIsThemedParam() const { 262 return ntp_is_themed_param_; 263} 264 265// ----------------------------------------------------------------- 266// HistoryURLProvider 267 268// These ugly magic numbers will go away once we switch all scoring 269// behavior (including URL-what-you-typed) to HistoryQuick provider. 270const int HistoryURLProvider::kScoreForBestInlineableResult = 1413; 271const int HistoryURLProvider::kScoreForUnvisitedIntranetResult = 1403; 272const int HistoryURLProvider::kScoreForWhatYouTypedResult = 1203; 273const int HistoryURLProvider::kBaseScoreForNonInlineableResult = 900; 274 275// VisitClassifier is used to classify the type of visit to a particular url. 276class HistoryURLProvider::VisitClassifier { 277 public: 278 enum Type { 279 INVALID, // Navigations to the URL are not allowed. 280 UNVISITED_INTRANET, // A navigable URL for which we have no visit data but 281 // which is known to refer to a visited intranet host. 282 VISITED, // The site has been previously visited. 283 }; 284 285 VisitClassifier(HistoryURLProvider* provider, 286 const AutocompleteInput& input, 287 history::URLDatabase* db); 288 289 // Returns the type of visit for the specified input. 290 Type type() const { return type_; } 291 292 // Returns the URLRow for the visit. 293 const history::URLRow& url_row() const { return url_row_; } 294 295 private: 296 HistoryURLProvider* provider_; 297 history::URLDatabase* db_; 298 Type type_; 299 history::URLRow url_row_; 300 301 DISALLOW_COPY_AND_ASSIGN(VisitClassifier); 302}; 303 304HistoryURLProvider::VisitClassifier::VisitClassifier( 305 HistoryURLProvider* provider, 306 const AutocompleteInput& input, 307 history::URLDatabase* db) 308 : provider_(provider), 309 db_(db), 310 type_(INVALID) { 311 const GURL& url = input.canonicalized_url(); 312 // Detect email addresses. These cases will look like "http://user@site/", 313 // and because the history backend strips auth creds, we'll get a bogus exact 314 // match below if the user has visited "site". 315 if (!url.is_valid() || 316 ((input.type() == AutocompleteInput::UNKNOWN) && 317 input.parts().username.is_nonempty() && 318 !input.parts().password.is_nonempty() && 319 !input.parts().path.is_nonempty())) 320 return; 321 322 if (db_->GetRowForURL(url, &url_row_)) { 323 type_ = VISITED; 324 return; 325 } 326 327 if (provider_->CanFindIntranetURL(db_, input)) { 328 // The user typed an intranet hostname that they've visited (albeit with a 329 // different port and/or path) before. 330 url_row_ = history::URLRow(url); 331 type_ = UNVISITED_INTRANET; 332 } 333} 334 335HistoryURLProviderParams::HistoryURLProviderParams( 336 const AutocompleteInput& input, 337 bool trim_http, 338 const std::string& languages, 339 TemplateURL* default_search_provider, 340 const SearchTermsData& search_terms_data) 341 : message_loop(base::MessageLoop::current()), 342 input(input), 343 prevent_inline_autocomplete(input.prevent_inline_autocomplete()), 344 trim_http(trim_http), 345 failed(false), 346 languages(languages), 347 dont_suggest_exact_input(false), 348 default_search_provider(default_search_provider ? 349 new TemplateURL(default_search_provider->profile(), 350 default_search_provider->data()) : NULL), 351 search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) { 352} 353 354HistoryURLProviderParams::~HistoryURLProviderParams() { 355} 356 357HistoryURLProvider::HistoryURLProvider(AutocompleteProviderListener* listener, 358 Profile* profile) 359 : HistoryProvider(listener, profile, 360 AutocompleteProvider::TYPE_HISTORY_URL), 361 params_(NULL), 362 cull_redirects_( 363 !OmniboxFieldTrial::InHUPCullRedirectsFieldTrial() || 364 !OmniboxFieldTrial::InHUPCullRedirectsFieldTrialExperimentGroup()), 365 create_shorter_match_( 366 !OmniboxFieldTrial::InHUPCreateShorterMatchFieldTrial() || 367 !OmniboxFieldTrial:: 368 InHUPCreateShorterMatchFieldTrialExperimentGroup()), 369 search_url_database_(true) { 370} 371 372// static 373AutocompleteMatch HistoryURLProvider::SuggestExactInput( 374 AutocompleteProvider* provider, 375 const AutocompleteInput& input, 376 bool trim_http) { 377 AutocompleteMatch match(provider, 0, false, 378 AutocompleteMatchType::URL_WHAT_YOU_TYPED); 379 380 const GURL& url = input.canonicalized_url(); 381 if (url.is_valid()) { 382 match.destination_url = url; 383 384 // Trim off "http://" if the user didn't type it. 385 // NOTE: We use TrimHttpPrefix() here rather than StringForURLDisplay() to 386 // strip the scheme as we need to know the offset so we can adjust the 387 // |match_location| below. StringForURLDisplay() and TrimHttpPrefix() have 388 // slightly different behavior as well (the latter will strip even without 389 // two slashes after the scheme). 390 DCHECK(!trim_http || !HasHTTPScheme(input.text())); 391 string16 display_string(provider->StringForURLDisplay(url, false, false)); 392 const size_t offset = trim_http ? TrimHttpPrefix(&display_string) : 0; 393 match.fill_into_edit = 394 AutocompleteInput::FormattedStringWithEquivalentMeaning(url, 395 display_string); 396 match.allowed_to_be_default_match = true; 397 // NOTE: Don't set match.inline_autocompletion to something non-empty here; 398 // it's surprising and annoying. 399 400 // Try to highlight "innermost" match location. If we fix up "w" into 401 // "www.w.com", we want to highlight the fifth character, not the first. 402 // This relies on match.destination_url being the non-prefix-trimmed version 403 // of match.contents. 404 match.contents = display_string; 405 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix( 406 UTF8ToUTF16(match.destination_url.spec()), input.text()); 407 // It's possible for match.destination_url to not contain the user's input 408 // at all (so |best_prefix| is NULL), for example if the input is 409 // "view-source:x" and |destination_url| has an inserted "http://" in the 410 // middle. 411 if (best_prefix == NULL) { 412 AutocompleteMatch::ClassifyMatchInString(input.text(), match.contents, 413 ACMatchClassification::URL, 414 &match.contents_class); 415 } else { 416 AutocompleteMatch::ClassifyLocationInString( 417 best_prefix->prefix.length() - offset, input.text().length(), 418 match.contents.length(), ACMatchClassification::URL, 419 &match.contents_class); 420 } 421 422 match.is_history_what_you_typed_match = true; 423 } 424 425 return match; 426} 427 428void HistoryURLProvider::Start(const AutocompleteInput& input, 429 bool minimal_changes) { 430 // NOTE: We could try hard to do less work in the |minimal_changes| case 431 // here; some clever caching would let us reuse the raw matches from the 432 // history DB without re-querying. However, we'd still have to go back to 433 // the history thread to mark these up properly, and if pass 2 is currently 434 // running, we'd need to wait for it to return to the main thread before 435 // doing this (we can't just write new data for it to read due to thread 436 // safety issues). At that point it's just as fast, and easier, to simply 437 // re-run the query from scratch and ignore |minimal_changes|. 438 439 // Cancel any in-progress query. 440 Stop(false); 441 442 RunAutocompletePasses(input, true); 443} 444 445void HistoryURLProvider::Stop(bool clear_cached_results) { 446 done_ = true; 447 448 if (params_) 449 params_->cancel_flag.Set(); 450} 451 452// Called on the history thread. 453void HistoryURLProvider::ExecuteWithDB(history::HistoryBackend* backend, 454 history::URLDatabase* db, 455 HistoryURLProviderParams* params) { 456 // We may get called with a NULL database if it couldn't be properly 457 // initialized. 458 if (!db) { 459 params->failed = true; 460 } else if (!params->cancel_flag.IsSet()) { 461 base::TimeTicks beginning_time = base::TimeTicks::Now(); 462 463 DoAutocomplete(backend, db, params); 464 465 UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime", 466 base::TimeTicks::Now() - beginning_time); 467 } 468 469 // Return the results (if any) to the main thread. 470 params->message_loop->PostTask(FROM_HERE, base::Bind( 471 &HistoryURLProvider::QueryComplete, this, params)); 472} 473 474// Used by both autocomplete passes, and therefore called on multiple different 475// threads (though not simultaneously). 476void HistoryURLProvider::DoAutocomplete(history::HistoryBackend* backend, 477 history::URLDatabase* db, 478 HistoryURLProviderParams* params) { 479 VisitClassifier classifier(this, params->input, db); 480 // Create a What You Typed match, which we'll need below. 481 // 482 // We display this to the user when there's a reasonable chance they actually 483 // care: 484 // * Their input can be opened as a URL, and 485 // * We parsed the input as a URL, or it starts with an explicit "http:" or 486 // "https:". 487 // that is when their input can be opened as a URL. 488 // Otherwise, this is just low-quality noise. In the cases where we've parsed 489 // as UNKNOWN, we'll still show an accidental search infobar if need be. 490 bool have_what_you_typed_match = 491 params->input.canonicalized_url().is_valid() && 492 (params->input.type() != AutocompleteInput::QUERY) && 493 ((params->input.type() != AutocompleteInput::UNKNOWN) || 494 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || 495 !params->trim_http || 496 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0)); 497 AutocompleteMatch what_you_typed_match( 498 SuggestExactInput(this, params->input, params->trim_http)); 499 what_you_typed_match.relevance = CalculateRelevance(WHAT_YOU_TYPED, 0); 500 501 // Get the matching URLs from the DB 502 history::URLRows url_matches; 503 history::HistoryMatches history_matches; 504 505 if (search_url_database_) { 506 const URLPrefixes& prefixes = URLPrefix::GetURLPrefixes(); 507 for (URLPrefixes::const_iterator i(prefixes.begin()); i != prefixes.end(); 508 ++i) { 509 if (params->cancel_flag.IsSet()) 510 return; // Canceled in the middle of a query, give up. 511 // We only need kMaxMatches results in the end, but before we 512 // get there we need to promote lower-quality matches that are 513 // prefixes of higher-quality matches, and remove lower-quality 514 // redirects. So we ask for more results than we need, of every 515 // prefix type, in hopes this will give us far more than enough 516 // to work with. CullRedirects() will then reduce the list to 517 // the best kMaxMatches results. 518 db->AutocompleteForPrefix( 519 UTF16ToUTF8(i->prefix + params->input.text()), 520 kMaxMatches * 2, 521 (backend == NULL), 522 &url_matches); 523 for (history::URLRows::const_iterator j(url_matches.begin()); 524 j != url_matches.end(); ++j) { 525 const URLPrefix* best_prefix = 526 URLPrefix::BestURLPrefix(UTF8ToUTF16(j->url().spec()), string16()); 527 DCHECK(best_prefix != NULL); 528 history_matches.push_back(history::HistoryMatch(*j, i->prefix.length(), 529 i->num_components == 0, 530 i->num_components >= best_prefix->num_components)); 531 } 532 } 533 } 534 535 // Create sorted list of suggestions. 536 CullPoorMatches(*params, &history_matches); 537 SortAndDedupMatches(&history_matches); 538 PromoteOrCreateShorterSuggestion(db, *params, have_what_you_typed_match, 539 what_you_typed_match, &history_matches); 540 541 // Try to promote a match as an exact/inline autocomplete match. This also 542 // moves it to the front of |history_matches|, so skip over it when 543 // converting the rest of the matches. 544 size_t first_match = 1; 545 size_t exact_suggestion = 0; 546 // Checking |is_history_what_you_typed_match| tells us whether 547 // SuggestExactInput() succeeded in constructing a valid match. 548 if (what_you_typed_match.is_history_what_you_typed_match && 549 (!backend || !params->dont_suggest_exact_input) && 550 FixupExactSuggestion(db, params->input, classifier, &what_you_typed_match, 551 &history_matches)) { 552 // Got an exact match for the user's input. Treat it as the best match 553 // regardless of the input type. 554 exact_suggestion = 1; 555 params->matches.push_back(what_you_typed_match); 556 } else if (params->prevent_inline_autocomplete || 557 history_matches.empty() || 558 !PromoteMatchForInlineAutocomplete(history_matches.front(), params)) { 559 // Failed to promote any URLs for inline autocompletion. Use the What You 560 // Typed match, if we have it. 561 first_match = 0; 562 if (have_what_you_typed_match) 563 params->matches.push_back(what_you_typed_match); 564 } 565 566 // This is the end of the synchronous pass. 567 if (!backend) 568 return; 569 // If search_url_database_ is false, we shouldn't have scheduled a second 570 // pass. 571 DCHECK(search_url_database_); 572 573 // Determine relevancy of highest scoring match, if any. 574 int relevance = -1; 575 for (ACMatches::const_iterator it = params->matches.begin(); 576 it != params->matches.end(); ++it) { 577 relevance = std::max(relevance, it->relevance); 578 } 579 580 if (cull_redirects_) { 581 // Remove redirects and trim list to size. We want to provide up to 582 // kMaxMatches results plus the What You Typed result, if it was added to 583 // |history_matches| above. 584 CullRedirects(backend, &history_matches, kMaxMatches + exact_suggestion); 585 } else { 586 // Simply trim the list to size. 587 if (history_matches.size() > kMaxMatches + exact_suggestion) 588 history_matches.resize(kMaxMatches + exact_suggestion); 589 } 590 591 // Convert the history matches to autocomplete matches. 592 for (size_t i = first_match; i < history_matches.size(); ++i) { 593 const history::HistoryMatch& match = history_matches[i]; 594 DCHECK(!have_what_you_typed_match || 595 (match.url_info.url() != 596 GURL(params->matches.front().destination_url))); 597 // If we've assigned a score already, all later matches score one 598 // less than the previous match. 599 relevance = (relevance > 0) ? (relevance - 1) : 600 CalculateRelevance(NORMAL, history_matches.size() - 1 - i); 601 AutocompleteMatch ac_match = HistoryMatchToACMatch(*params, match, 602 NORMAL, relevance); 603 params->matches.push_back(ac_match); 604 } 605} 606 607// Called on the main thread when the query is complete. 608void HistoryURLProvider::QueryComplete( 609 HistoryURLProviderParams* params_gets_deleted) { 610 // Ensure |params_gets_deleted| gets deleted on exit. 611 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); 612 613 // If the user hasn't already started another query, clear our member pointer 614 // so we can't write into deleted memory. 615 if (params_ == params_gets_deleted) 616 params_ = NULL; 617 618 // Don't send responses for queries that have been canceled. 619 if (params->cancel_flag.IsSet()) 620 return; // Already set done_ when we canceled, no need to set it again. 621 622 // Don't modify |matches_| if the query failed, since it might have a default 623 // match in it, whereas |params->matches| will be empty. 624 if (!params->failed) { 625 matches_.swap(params->matches); 626 UpdateStarredStateOfMatches(); 627 } 628 629 done_ = true; 630 listener_->OnProviderUpdate(true); 631} 632 633HistoryURLProvider::~HistoryURLProvider() { 634 // Note: This object can get leaked on shutdown if there are pending 635 // requests on the database (which hold a reference to us). Normally, these 636 // messages get flushed for each thread. We do a round trip from main, to 637 // history, back to main while holding a reference. If the main thread 638 // completes before the history thread, the message to delegate back to the 639 // main thread will not run and the reference will leak. Therefore, don't do 640 // anything on destruction. 641} 642 643int HistoryURLProvider::CalculateRelevance(MatchType match_type, 644 size_t match_number) const { 645 switch (match_type) { 646 case INLINE_AUTOCOMPLETE: 647 return kScoreForBestInlineableResult; 648 649 case UNVISITED_INTRANET: 650 return kScoreForUnvisitedIntranetResult; 651 652 case WHAT_YOU_TYPED: 653 return kScoreForWhatYouTypedResult; 654 655 default: // NORMAL 656 return kBaseScoreForNonInlineableResult + 657 static_cast<int>(match_number); 658 } 659} 660 661void HistoryURLProvider::RunAutocompletePasses( 662 const AutocompleteInput& input, 663 bool fixup_input_and_run_pass_1) { 664 matches_.clear(); 665 666 if ((input.type() == AutocompleteInput::INVALID) || 667 (input.type() == AutocompleteInput::FORCED_QUERY)) 668 return; 669 670 // Create a match for exactly what the user typed. This will only be used as 671 // a fallback in case we can't get the history service or URL DB; otherwise, 672 // we'll run this again in DoAutocomplete() and use that result instead. 673 const bool trim_http = !HasHTTPScheme(input.text()); 674 // Don't do this for queries -- while we can sometimes mark up a match for 675 // this, it's not what the user wants, and just adds noise. 676 if ((input.type() != AutocompleteInput::QUERY) && 677 input.canonicalized_url().is_valid()) { 678 AutocompleteMatch what_you_typed(SuggestExactInput(this, input, trim_http)); 679 what_you_typed.relevance = CalculateRelevance(WHAT_YOU_TYPED, 0); 680 matches_.push_back(what_you_typed); 681 } 682 683 // We'll need the history service to run both passes, so try to obtain it. 684 if (!profile_) 685 return; 686 HistoryService* const history_service = 687 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 688 if (!history_service) 689 return; 690 691 // Get the default search provider and search terms data now since we have to 692 // retrieve these on the UI thread, and the second pass runs on the history 693 // thread. |template_url_service| can be NULL when testing. 694 TemplateURLService* template_url_service = 695 TemplateURLServiceFactory::GetForProfile(profile_); 696 TemplateURL* default_search_provider = template_url_service ? 697 template_url_service->GetDefaultSearchProvider() : NULL; 698 UIThreadSearchTermsData data(profile_); 699 700 // Create the data structure for the autocomplete passes. We'll save this off 701 // onto the |params_| member for later deletion below if we need to run pass 702 // 2. 703 std::string languages(languages_); 704 if (languages.empty()) { 705 languages = 706 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); 707 } 708 scoped_ptr<HistoryURLProviderParams> params( 709 new HistoryURLProviderParams(input, trim_http, languages, 710 default_search_provider, data)); 711 712 params->prevent_inline_autocomplete = 713 PreventInlineAutocomplete(input); 714 715 if (fixup_input_and_run_pass_1) { 716 // Do some fixup on the user input before matching against it, so we provide 717 // good results for local file paths, input with spaces, etc. 718 if (!FixupUserInput(¶ms->input)) 719 return; 720 721 // Pass 1: Get the in-memory URL database, and use it to find and promote 722 // the inline autocomplete match, if any. 723 history::URLDatabase* url_db = history_service->InMemoryDatabase(); 724 // url_db can be NULL if it hasn't finished initializing (or failed to 725 // initialize). In this case all we can do is fall back on the second 726 // pass. 727 // 728 // TODO(pkasting): We should just block here until this loads. Any time 729 // someone unloads the history backend, we'll get inconsistent inline 730 // autocomplete behavior here. 731 if (url_db) { 732 DoAutocomplete(NULL, url_db, params.get()); 733 // params->matches now has the matches we should expose to the provider. 734 // Pass 2 expects a "clean slate" set of matches. 735 matches_.clear(); 736 matches_.swap(params->matches); 737 UpdateStarredStateOfMatches(); 738 } 739 } 740 741 // Pass 2: Ask the history service to call us back on the history thread, 742 // where we can read the full on-disk DB. 743 if (search_url_database_ && 744 (input.matches_requested() == AutocompleteInput::ALL_MATCHES)) { 745 done_ = false; 746 params_ = params.release(); // This object will be destroyed in 747 // QueryComplete() once we're done with it. 748 history_service->ScheduleAutocomplete(this, params_); 749 } 750} 751 752bool HistoryURLProvider::FixupExactSuggestion( 753 history::URLDatabase* db, 754 const AutocompleteInput& input, 755 const VisitClassifier& classifier, 756 AutocompleteMatch* match, 757 history::HistoryMatches* matches) const { 758 DCHECK(match != NULL); 759 DCHECK(matches != NULL); 760 761 MatchType type = INLINE_AUTOCOMPLETE; 762 switch (classifier.type()) { 763 case VisitClassifier::INVALID: 764 return false; 765 case VisitClassifier::UNVISITED_INTRANET: 766 type = UNVISITED_INTRANET; 767 break; 768 default: 769 DCHECK_EQ(VisitClassifier::VISITED, classifier.type()); 770 // We have data for this match, use it. 771 match->deletable = true; 772 match->description = classifier.url_row().title(); 773 RecordAdditionalInfoFromUrlRow(classifier.url_row(), match); 774 AutocompleteMatch::ClassifyMatchInString( 775 input.text(), 776 classifier.url_row().title(), 777 ACMatchClassification::NONE, &match->description_class); 778 if (!classifier.url_row().typed_count()) { 779 // If we reach here, we must be in the second pass, and we must not have 780 // this row's data available during the first pass. That means we 781 // either scored it as WHAT_YOU_TYPED or UNVISITED_INTRANET, and to 782 // maintain the ordering between passes consistent, we need to score it 783 // the same way here. 784 type = CanFindIntranetURL(db, input) ? 785 UNVISITED_INTRANET : WHAT_YOU_TYPED; 786 } 787 break; 788 } 789 790 const GURL& url = match->destination_url; 791 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); 792 // If the what-you-typed result looks like a single word (which can be 793 // interpreted as an intranet address) followed by a pound sign ("#"), 794 // leave the score for the url-what-you-typed result as is. It will be 795 // outscored by a search query from the SearchProvider. This test fixes 796 // cases such as "c#" and "c# foo" where the user has visited an intranet 797 // site "c". We want the search-what-you-typed score to beat the 798 // URL-what-you-typed score in this case. Most of the below test tries to 799 // make sure that this code does not trigger if the user did anything to 800 // indicate the desired match is a URL. For instance, "c/# foo" will not 801 // pass the test because that will be classified as input type URL. The 802 // parsed.CountCharactersBefore() in the test looks for the presence of a 803 // reference fragment in the URL by checking whether the position differs 804 // included the delimiter (pound sign) versus not including the delimiter. 805 // (One cannot simply check url.ref() because it will not distinguish 806 // between the input "c" and the input "c#", both of which will have empty 807 // reference fragments.) 808 if ((type == UNVISITED_INTRANET) && 809 (input.type() != AutocompleteInput::URL) && 810 url.username().empty() && url.password().empty() && url.port().empty() && 811 (url.path() == "/") && url.query().empty() && 812 (parsed.CountCharactersBefore(url_parse::Parsed::REF, true) != 813 parsed.CountCharactersBefore(url_parse::Parsed::REF, false))) { 814 return false; 815 } 816 817 match->relevance = CalculateRelevance(type, 0); 818 819 // If there are any other matches, then don't promote this match here, in 820 // hopes the caller will be able to inline autocomplete a better suggestion. 821 // DoAutocomplete() will fall back on this match if inline autocompletion 822 // fails. This matches how we react to never-visited URL inputs in the non- 823 // intranet case. 824 if (type == UNVISITED_INTRANET && !matches->empty()) 825 return false; 826 827 // Put it on the front of the HistoryMatches for redirect culling. 828 CreateOrPromoteMatch(classifier.url_row(), string16::npos, false, matches, 829 true, true); 830 return true; 831} 832 833bool HistoryURLProvider::CanFindIntranetURL( 834 history::URLDatabase* db, 835 const AutocompleteInput& input) const { 836 // Normally passing the first two conditions below ought to guarantee the 837 // third condition, but because FixupUserInput() can run and modify the 838 // input's text and parts between Parse() and here, it seems better to be 839 // paranoid and check. 840 if ((input.type() != AutocompleteInput::UNKNOWN) || 841 !LowerCaseEqualsASCII(input.scheme(), content::kHttpScheme) || 842 !input.parts().host.is_nonempty()) 843 return false; 844 const std::string host(UTF16ToUTF8( 845 input.text().substr(input.parts().host.begin, input.parts().host.len))); 846 const size_t registry_length = 847 net::registry_controlled_domains::GetRegistryLength( 848 host, 849 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 850 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 851 return registry_length == 0 && db->IsTypedHost(host); 852} 853 854bool HistoryURLProvider::PromoteMatchForInlineAutocomplete( 855 const history::HistoryMatch& match, 856 HistoryURLProviderParams* params) { 857 // Promote the first match if it's been marked for promotion or typed at least 858 // n times, where n == 1 for "simple" (host-only) URLs and n == 2 for others. 859 // We set a higher bar for these long URLs because it's less likely that users 860 // will want to visit them again. Even though we don't increment the 861 // typed_count for pasted-in URLs, if the user manually edits the URL or types 862 // some long thing in by hand, we wouldn't want to immediately start 863 // autocompleting it. 864 if (!match.promoted && 865 (!match.url_info.typed_count() || 866 ((match.url_info.typed_count() == 1) && 867 !match.IsHostOnly()))) 868 return false; 869 870 // In the case where the user has typed "foo.com" and visited (but not typed) 871 // "foo/", and the input is "foo", we can reach here for "foo.com" during the 872 // first pass but have the second pass suggest the exact input as a better 873 // URL. Since we need both passes to agree, and since during the first pass 874 // there's no way to know about "foo/", make reaching this point prevent any 875 // future pass from suggesting the exact input as a better match. 876 if (params) { 877 params->dont_suggest_exact_input = true; 878 params->matches.push_back(HistoryMatchToACMatch(*params, match, 879 INLINE_AUTOCOMPLETE, CalculateRelevance(INLINE_AUTOCOMPLETE, 0))); 880 } 881 return true; 882} 883 884// See if a shorter version of the best match should be created, and if so place 885// it at the front of |matches|. This can suggest history URLs that are 886// prefixes of the best match (if they've been visited enough, compared to the 887// best match), or create host-only suggestions even when they haven't been 888// visited before: if the user visited http://example.com/asdf once, we'll 889// suggest http://example.com/ even if they've never been to it. 890void HistoryURLProvider::PromoteOrCreateShorterSuggestion( 891 history::URLDatabase* db, 892 const HistoryURLProviderParams& params, 893 bool have_what_you_typed_match, 894 const AutocompleteMatch& what_you_typed_match, 895 history::HistoryMatches* matches) { 896 if (matches->empty()) 897 return; // No matches, nothing to do. 898 899 // Determine the base URL from which to search, and whether that URL could 900 // itself be added as a match. We can add the base iff it's not "effectively 901 // the same" as any "what you typed" match. 902 const history::HistoryMatch& match = matches->front(); 903 GURL search_base = ConvertToHostOnly(match, params.input.text()); 904 bool can_add_search_base_to_matches = !have_what_you_typed_match; 905 if (search_base.is_empty()) { 906 // Search from what the user typed when we couldn't reduce the best match 907 // to a host. Careful: use a substring of |match| here, rather than the 908 // first match in |params|, because they might have different prefixes. If 909 // the user typed "google.com", |what_you_typed_match| will hold 910 // "http://google.com/", but |match| might begin with 911 // "http://www.google.com/". 912 // TODO: this should be cleaned up, and is probably incorrect for IDN. 913 std::string new_match = match.url_info.url().possibly_invalid_spec(). 914 substr(0, match.input_location + params.input.text().length()); 915 search_base = GURL(new_match); 916 // TODO(mrossetti): There is a degenerate case where the following may 917 // cause a failure: http://www/~someword/fubar.html. Diagnose. 918 // See: http://crbug.com/50101 919 if (search_base.is_empty()) 920 return; // Can't construct a valid URL from which to start a search. 921 } else if (!can_add_search_base_to_matches) { 922 can_add_search_base_to_matches = 923 (search_base != what_you_typed_match.destination_url); 924 } 925 if (search_base == match.url_info.url()) 926 return; // Couldn't shorten |match|, so no range of URLs to search over. 927 928 // Search the DB for short URLs between our base and |match|. 929 history::URLRow info(search_base); 930 bool promote = true; 931 // A short URL is only worth suggesting if it's been visited at least a third 932 // as often as the longer URL. 933 const int min_visit_count = ((match.url_info.visit_count() - 1) / 3) + 1; 934 // For stability between the in-memory and on-disk autocomplete passes, when 935 // the long URL has been typed before, only suggest shorter URLs that have 936 // also been typed. Otherwise, the on-disk pass could suggest a shorter URL 937 // (which hasn't been typed) that the in-memory pass doesn't know about, 938 // thereby making the top match, and thus the behavior of inline 939 // autocomplete, unstable. 940 const int min_typed_count = match.url_info.typed_count() ? 1 : 0; 941 if (!db->FindShortestURLFromBase(search_base.possibly_invalid_spec(), 942 match.url_info.url().possibly_invalid_spec(), min_visit_count, 943 min_typed_count, can_add_search_base_to_matches, &info)) { 944 if (!can_add_search_base_to_matches) 945 return; // Couldn't find anything and can't add the search base, bail. 946 947 // Try to get info on the search base itself. Promote it to the top if the 948 // original best match isn't good enough to autocomplete. 949 db->GetRowForURL(search_base, &info); 950 promote = match.url_info.typed_count() <= 1; 951 } 952 953 // Promote or add the desired URL to the list of matches. 954 bool ensure_can_inline = 955 promote && PromoteMatchForInlineAutocomplete(match, NULL); 956 ensure_can_inline &= CreateOrPromoteMatch(info, match.input_location, 957 match.match_in_scheme, matches, create_shorter_match_, promote); 958 if (ensure_can_inline) 959 matches->front().promoted = true; 960} 961 962void HistoryURLProvider::CullPoorMatches( 963 const HistoryURLProviderParams& params, 964 history::HistoryMatches* matches) const { 965 const base::Time& threshold(history::AutocompleteAgeThreshold()); 966 for (history::HistoryMatches::iterator i(matches->begin()); 967 i != matches->end(); ) { 968 if (RowQualifiesAsSignificant(i->url_info, threshold) && 969 !(params.default_search_provider && 970 params.default_search_provider->IsSearchURLUsingTermsData( 971 i->url_info.url(), *params.search_terms_data.get()))) { 972 ++i; 973 } else { 974 i = matches->erase(i); 975 } 976 } 977} 978 979void HistoryURLProvider::CullRedirects(history::HistoryBackend* backend, 980 history::HistoryMatches* matches, 981 size_t max_results) const { 982 for (size_t source = 0; 983 (source < matches->size()) && (source < max_results); ) { 984 const GURL& url = (*matches)[source].url_info.url(); 985 // TODO(brettw) this should go away when everything uses GURL. 986 history::RedirectList redirects; 987 backend->GetMostRecentRedirectsFrom(url, &redirects); 988 if (!redirects.empty()) { 989 // Remove all but the first occurrence of any of these redirects in the 990 // search results. We also must add the URL we queried for, since it may 991 // not be the first match and we'd want to remove it. 992 // 993 // For example, when A redirects to B and our matches are [A, X, B], 994 // we'll get B as the redirects from, and we want to remove the second 995 // item of that pair, removing B. If A redirects to B and our matches are 996 // [B, X, A], we'll want to remove A instead. 997 redirects.push_back(url); 998 source = RemoveSubsequentMatchesOf(matches, source, redirects); 999 } else { 1000 // Advance to next item. 1001 source++; 1002 } 1003 } 1004 1005 if (matches->size() > max_results) 1006 matches->resize(max_results); 1007} 1008 1009size_t HistoryURLProvider::RemoveSubsequentMatchesOf( 1010 history::HistoryMatches* matches, 1011 size_t source_index, 1012 const std::vector<GURL>& remove) const { 1013 size_t next_index = source_index + 1; // return value = item after source 1014 1015 // Find the first occurrence of any URL in the redirect chain. We want to 1016 // keep this one since it is rated the highest. 1017 history::HistoryMatches::iterator first(std::find_first_of( 1018 matches->begin(), matches->end(), remove.begin(), remove.end(), 1019 history::HistoryMatch::EqualsGURL)); 1020 DCHECK(first != matches->end()) << "We should have always found at least the " 1021 "original URL."; 1022 1023 // Find any following occurrences of any URL in the redirect chain, these 1024 // should be deleted. 1025 for (history::HistoryMatches::iterator next(std::find_first_of(first + 1, 1026 matches->end(), remove.begin(), remove.end(), 1027 history::HistoryMatch::EqualsGURL)); 1028 next != matches->end(); next = std::find_first_of(next, matches->end(), 1029 remove.begin(), remove.end(), history::HistoryMatch::EqualsGURL)) { 1030 // Remove this item. When we remove an item before the source index, we 1031 // need to shift it to the right and remember that so we can return it. 1032 next = matches->erase(next); 1033 if (static_cast<size_t>(next - matches->begin()) < next_index) 1034 --next_index; 1035 } 1036 return next_index; 1037} 1038 1039AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch( 1040 const HistoryURLProviderParams& params, 1041 const history::HistoryMatch& history_match, 1042 MatchType match_type, 1043 int relevance) { 1044 const history::URLRow& info = history_match.url_info; 1045 AutocompleteMatch match(this, relevance, 1046 !!info.visit_count(), AutocompleteMatchType::HISTORY_URL); 1047 match.typed_count = info.typed_count(); 1048 match.destination_url = info.url(); 1049 DCHECK(match.destination_url.is_valid()); 1050 size_t inline_autocomplete_offset = 1051 history_match.input_location + params.input.text().length(); 1052 std::string languages = (match_type == WHAT_YOU_TYPED) ? 1053 std::string() : params.languages; 1054 const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & 1055 ~((params.trim_http && !history_match.match_in_scheme) ? 1056 0 : net::kFormatUrlOmitHTTP); 1057 match.fill_into_edit = 1058 AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), 1059 net::FormatUrl(info.url(), languages, format_types, 1060 net::UnescapeRule::SPACES, NULL, NULL, 1061 &inline_autocomplete_offset)); 1062 if (!params.prevent_inline_autocomplete && 1063 (inline_autocomplete_offset != string16::npos)) { 1064 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); 1065 match.inline_autocompletion = 1066 match.fill_into_edit.substr(inline_autocomplete_offset); 1067 } 1068 // The latter part of the test effectively asks "is the inline completion 1069 // empty?" (i.e., is this match effectively the what-you-typed match?). 1070 match.allowed_to_be_default_match = !params.prevent_inline_autocomplete || 1071 ((inline_autocomplete_offset != string16::npos) && 1072 (inline_autocomplete_offset >= match.fill_into_edit.length())); 1073 1074 size_t match_start = history_match.input_location; 1075 match.contents = net::FormatUrl(info.url(), languages, 1076 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); 1077 if ((match_start != string16::npos) && 1078 (inline_autocomplete_offset != string16::npos) && 1079 (inline_autocomplete_offset != match_start)) { 1080 DCHECK(inline_autocomplete_offset > match_start); 1081 AutocompleteMatch::ClassifyLocationInString(match_start, 1082 inline_autocomplete_offset - match_start, match.contents.length(), 1083 ACMatchClassification::URL, &match.contents_class); 1084 } else { 1085 AutocompleteMatch::ClassifyLocationInString(string16::npos, 0, 1086 match.contents.length(), ACMatchClassification::URL, 1087 &match.contents_class); 1088 } 1089 match.description = info.title(); 1090 AutocompleteMatch::ClassifyMatchInString(params.input.text(), 1091 info.title(), 1092 ACMatchClassification::NONE, 1093 &match.description_class); 1094 RecordAdditionalInfoFromUrlRow(info, &match); 1095 return match; 1096} 1097