| 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 "components/omnibox/browser/history_url_provider.h" | 5 #include "components/omnibox/browser/history_url_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 if ((score_buckets.relevance_cap() != -1) && | 120 if ((score_buckets.relevance_cap() != -1) && |
| 121 (undecayed_relevance >= score_buckets.relevance_cap())) | 121 (undecayed_relevance >= score_buckets.relevance_cap())) |
| 122 return undecayed_relevance; | 122 return undecayed_relevance; |
| 123 | 123 |
| 124 // Time based decay using half-life time. | 124 // Time based decay using half-life time. |
| 125 double decayed_count = undecayed_count; | 125 double decayed_count = undecayed_count; |
| 126 double decay_factor = score_buckets.HalfLifeTimeDecay(time_since_last_visit); | 126 double decay_factor = score_buckets.HalfLifeTimeDecay(time_since_last_visit); |
| 127 if (decayed_count > 0) | 127 if (decayed_count > 0) |
| 128 decayed_count *= decay_factor; | 128 decayed_count *= decay_factor; |
| 129 | 129 |
| 130 const HUPScoringParams::ScoreBuckets::CountMaxRelevance* score_bucket = NULL; | 130 const HUPScoringParams::ScoreBuckets::CountMaxRelevance* score_bucket = |
| 131 nullptr; |
| 131 const double factor = (score_buckets.use_decay_factor() ? | 132 const double factor = (score_buckets.use_decay_factor() ? |
| 132 decay_factor : decayed_count); | 133 decay_factor : decayed_count); |
| 133 for (size_t i = 0; i < score_buckets.buckets().size(); ++i) { | 134 for (size_t i = 0; i < score_buckets.buckets().size(); ++i) { |
| 134 score_bucket = &score_buckets.buckets()[i]; | 135 score_bucket = &score_buckets.buckets()[i]; |
| 135 if (factor >= score_bucket->first) | 136 if (factor >= score_bucket->first) |
| 136 break; | 137 break; |
| 137 } | 138 } |
| 138 | 139 |
| 139 return (score_bucket && (undecayed_relevance > score_bucket->second)) ? | 140 return (score_bucket && (undecayed_relevance > score_bucket->second)) ? |
| 140 score_bucket->second : undecayed_relevance; | 141 score_bucket->second : undecayed_relevance; |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 const SearchTermsData& search_terms_data) | 428 const SearchTermsData& search_terms_data) |
| 428 : message_loop(base::MessageLoop::current()), | 429 : message_loop(base::MessageLoop::current()), |
| 429 input(input), | 430 input(input), |
| 430 prevent_inline_autocomplete(input.prevent_inline_autocomplete()), | 431 prevent_inline_autocomplete(input.prevent_inline_autocomplete()), |
| 431 trim_http(trim_http), | 432 trim_http(trim_http), |
| 432 what_you_typed_match(what_you_typed_match), | 433 what_you_typed_match(what_you_typed_match), |
| 433 failed(false), | 434 failed(false), |
| 434 exact_suggestion_is_in_history(false), | 435 exact_suggestion_is_in_history(false), |
| 435 promote_type(NEITHER), | 436 promote_type(NEITHER), |
| 436 default_search_provider(default_search_provider ? | 437 default_search_provider(default_search_provider ? |
| 437 new TemplateURL(default_search_provider->data()) : NULL), | 438 new TemplateURL(default_search_provider->data()) : nullptr), |
| 438 search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) { | 439 search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) { |
| 439 } | 440 } |
| 440 | 441 |
| 441 HistoryURLProviderParams::~HistoryURLProviderParams() { | 442 HistoryURLProviderParams::~HistoryURLProviderParams() { |
| 442 } | 443 } |
| 443 | 444 |
| 444 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderClient* client, | 445 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderClient* client, |
| 445 AutocompleteProviderListener* listener) | 446 AutocompleteProviderListener* listener) |
| 446 : HistoryProvider(AutocompleteProvider::TYPE_HISTORY_URL, client), | 447 : HistoryProvider(AutocompleteProvider::TYPE_HISTORY_URL, client), |
| 447 listener_(listener), | 448 listener_(listener), |
| 448 params_(NULL), | 449 params_(nullptr), |
| 449 search_url_database_(OmniboxFieldTrial::HUPSearchDatabase()) { | 450 search_url_database_(OmniboxFieldTrial::HUPSearchDatabase()) { |
| 450 // Initialize the default HUP scoring params. | 451 // Initialize the default HUP scoring params. |
| 451 OmniboxFieldTrial::GetDefaultHUPScoringParams(&scoring_params_); | 452 OmniboxFieldTrial::GetDefaultHUPScoringParams(&scoring_params_); |
| 452 // Initialize HUP scoring params based on the current experiment. | 453 // Initialize HUP scoring params based on the current experiment. |
| 453 OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_); | 454 OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_); |
| 454 } | 455 } |
| 455 | 456 |
| 456 void HistoryURLProvider::Start(const AutocompleteInput& input, | 457 void HistoryURLProvider::Start(const AutocompleteInput& input, |
| 457 bool minimal_changes) { | 458 bool minimal_changes) { |
| 458 TRACE_EVENT0("omnibox", "HistoryURLProvider::Start"); | 459 TRACE_EVENT0("omnibox", "HistoryURLProvider::Start"); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 matches_.push_back(what_you_typed_match); | 500 matches_.push_back(what_you_typed_match); |
| 500 | 501 |
| 501 // We'll need the history service to run both passes, so try to obtain it. | 502 // We'll need the history service to run both passes, so try to obtain it. |
| 502 history::HistoryService* const history_service = | 503 history::HistoryService* const history_service = |
| 503 client()->GetHistoryService(); | 504 client()->GetHistoryService(); |
| 504 if (!history_service) | 505 if (!history_service) |
| 505 return; | 506 return; |
| 506 | 507 |
| 507 // Get the default search provider and search terms data now since we have to | 508 // Get the default search provider and search terms data now since we have to |
| 508 // retrieve these on the UI thread, and the second pass runs on the history | 509 // retrieve these on the UI thread, and the second pass runs on the history |
| 509 // thread. |template_url_service| can be NULL when testing. | 510 // thread. |template_url_service| can be null when testing. |
| 510 TemplateURLService* template_url_service = client()->GetTemplateURLService(); | 511 TemplateURLService* template_url_service = client()->GetTemplateURLService(); |
| 511 TemplateURL* default_search_provider = template_url_service ? | 512 TemplateURL* default_search_provider = template_url_service ? |
| 512 template_url_service->GetDefaultSearchProvider() : NULL; | 513 template_url_service->GetDefaultSearchProvider() : nullptr; |
| 513 | 514 |
| 514 // Create the data structure for the autocomplete passes. We'll save this off | 515 // Create the data structure for the autocomplete passes. We'll save this off |
| 515 // onto the |params_| member for later deletion below if we need to run pass | 516 // onto the |params_| member for later deletion below if we need to run pass |
| 516 // 2. | 517 // 2. |
| 517 std::unique_ptr<HistoryURLProviderParams> params(new HistoryURLProviderParams( | 518 std::unique_ptr<HistoryURLProviderParams> params(new HistoryURLProviderParams( |
| 518 fixed_up_input, trim_http, what_you_typed_match, default_search_provider, | 519 fixed_up_input, trim_http, what_you_typed_match, default_search_provider, |
| 519 client()->GetSearchTermsData())); | 520 client()->GetSearchTermsData())); |
| 520 // Note that we use the non-fixed-up input here, since fixup may strip | 521 // Note that we use the non-fixed-up input here, since fixup may strip |
| 521 // trailing whitespace. | 522 // trailing whitespace. |
| 522 params->prevent_inline_autocomplete = PreventInlineAutocomplete(input); | 523 params->prevent_inline_autocomplete = PreventInlineAutocomplete(input); |
| 523 | 524 |
| 524 // Pass 1: Get the in-memory URL database, and use it to find and promote | 525 // Pass 1: Get the in-memory URL database, and use it to find and promote |
| 525 // the inline autocomplete match, if any. | 526 // the inline autocomplete match, if any. |
| 526 history::URLDatabase* url_db = history_service->InMemoryDatabase(); | 527 history::URLDatabase* url_db = history_service->InMemoryDatabase(); |
| 527 // url_db can be NULL if it hasn't finished initializing (or failed to | 528 // url_db can be null if it hasn't finished initializing (or failed to |
| 528 // initialize). In this case all we can do is fall back on the second | 529 // initialize). In this case all we can do is fall back on the second pass. |
| 529 // pass. | |
| 530 // | 530 // |
| 531 // TODO(pkasting): We should just block here until this loads. Any time | 531 // TODO(pkasting): We should just block here until this loads. Any time |
| 532 // someone unloads the history backend, we'll get inconsistent inline | 532 // someone unloads the history backend, we'll get inconsistent inline |
| 533 // autocomplete behavior here. | 533 // autocomplete behavior here. |
| 534 if (url_db) { | 534 if (url_db) { |
| 535 DoAutocomplete(NULL, url_db, params.get()); | 535 DoAutocomplete(nullptr, url_db, params.get()); |
| 536 matches_.clear(); | 536 matches_.clear(); |
| 537 PromoteMatchesIfNecessary(*params); | 537 PromoteMatchesIfNecessary(*params); |
| 538 // NOTE: We don't reset |params| here since at least the |promote_type| | 538 // NOTE: We don't reset |params| here since at least the |promote_type| |
| 539 // field on it will be read by the second pass -- see comments in | 539 // field on it will be read by the second pass -- see comments in |
| 540 // DoAutocomplete(). | 540 // DoAutocomplete(). |
| 541 } | 541 } |
| 542 | 542 |
| 543 // Pass 2: Ask the history service to call us back on the history thread, | 543 // Pass 2: Ask the history service to call us back on the history thread, |
| 544 // where we can read the full on-disk DB. | 544 // where we can read the full on-disk DB. |
| 545 if (search_url_database_ && input.want_asynchronous_matches()) { | 545 if (search_url_database_ && input.want_asynchronous_matches()) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 // it's surprising and annoying. | 595 // it's surprising and annoying. |
| 596 | 596 |
| 597 // Try to highlight "innermost" match location. If we fix up "w" into | 597 // Try to highlight "innermost" match location. If we fix up "w" into |
| 598 // "www.w.com", we want to highlight the fifth character, not the first. | 598 // "www.w.com", we want to highlight the fifth character, not the first. |
| 599 // This relies on match.destination_url being the non-prefix-trimmed version | 599 // This relies on match.destination_url being the non-prefix-trimmed version |
| 600 // of match.contents. | 600 // of match.contents. |
| 601 match.contents = display_string; | 601 match.contents = display_string; |
| 602 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix( | 602 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix( |
| 603 base::UTF8ToUTF16(destination_url.spec()), input.text()); | 603 base::UTF8ToUTF16(destination_url.spec()), input.text()); |
| 604 // It's possible for match.destination_url to not contain the user's input | 604 // It's possible for match.destination_url to not contain the user's input |
| 605 // at all (so |best_prefix| is NULL), for example if the input is | 605 // at all (so |best_prefix| is null), for example if the input is |
| 606 // "view-source:x" and |destination_url| has an inserted "http://" in the | 606 // "view-source:x" and |destination_url| has an inserted "http://" in the |
| 607 // middle. | 607 // middle. |
| 608 if (best_prefix == NULL) { | 608 if (!best_prefix) { |
| 609 AutocompleteMatch::ClassifyMatchInString(input.text(), | 609 AutocompleteMatch::ClassifyMatchInString(input.text(), |
| 610 match.contents, | 610 match.contents, |
| 611 ACMatchClassification::URL, | 611 ACMatchClassification::URL, |
| 612 &match.contents_class); | 612 &match.contents_class); |
| 613 } else { | 613 } else { |
| 614 AutocompleteMatch::ClassifyLocationInString( | 614 AutocompleteMatch::ClassifyLocationInString( |
| 615 best_prefix->prefix.length() - offset, input.text().length(), | 615 best_prefix->prefix.length() - offset, input.text().length(), |
| 616 match.contents.length(), ACMatchClassification::URL, | 616 match.contents.length(), ACMatchClassification::URL, |
| 617 &match.contents_class); | 617 &match.contents_class); |
| 618 } | 618 } |
| 619 } | 619 } |
| 620 | 620 |
| 621 return match; | 621 return match; |
| 622 } | 622 } |
| 623 | 623 |
| 624 void HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams* params, | 624 void HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams* params, |
| 625 history::HistoryBackend* backend, | 625 history::HistoryBackend* backend, |
| 626 history::URLDatabase* db) { | 626 history::URLDatabase* db) { |
| 627 // We may get called with a NULL database if it couldn't be properly | 627 // We may get called with a null database if it couldn't be properly |
| 628 // initialized. | 628 // initialized. |
| 629 if (!db) { | 629 if (!db) { |
| 630 params->failed = true; | 630 params->failed = true; |
| 631 } else if (!params->cancel_flag.IsSet()) { | 631 } else if (!params->cancel_flag.IsSet()) { |
| 632 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 632 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 633 | 633 |
| 634 DoAutocomplete(backend, db, params); | 634 DoAutocomplete(backend, db, params); |
| 635 | 635 |
| 636 UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime", | 636 UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime", |
| 637 base::TimeTicks::Now() - beginning_time); | 637 base::TimeTicks::Now() - beginning_time); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 | 840 |
| 841 void HistoryURLProvider::QueryComplete( | 841 void HistoryURLProvider::QueryComplete( |
| 842 HistoryURLProviderParams* params_gets_deleted) { | 842 HistoryURLProviderParams* params_gets_deleted) { |
| 843 TRACE_EVENT0("omnibox", "HistoryURLProvider::QueryComplete"); | 843 TRACE_EVENT0("omnibox", "HistoryURLProvider::QueryComplete"); |
| 844 // Ensure |params_gets_deleted| gets deleted on exit. | 844 // Ensure |params_gets_deleted| gets deleted on exit. |
| 845 std::unique_ptr<HistoryURLProviderParams> params(params_gets_deleted); | 845 std::unique_ptr<HistoryURLProviderParams> params(params_gets_deleted); |
| 846 | 846 |
| 847 // If the user hasn't already started another query, clear our member pointer | 847 // If the user hasn't already started another query, clear our member pointer |
| 848 // so we can't write into deleted memory. | 848 // so we can't write into deleted memory. |
| 849 if (params_ == params_gets_deleted) | 849 if (params_ == params_gets_deleted) |
| 850 params_ = NULL; | 850 params_ = nullptr; |
| 851 | 851 |
| 852 // Don't send responses for queries that have been canceled. | 852 // Don't send responses for queries that have been canceled. |
| 853 if (params->cancel_flag.IsSet()) | 853 if (params->cancel_flag.IsSet()) |
| 854 return; // Already set done_ when we canceled, no need to set it again. | 854 return; // Already set done_ when we canceled, no need to set it again. |
| 855 | 855 |
| 856 // Don't modify |matches_| if the query failed, since it might have a default | 856 // Don't modify |matches_| if the query failed, since it might have a default |
| 857 // match in it, whereas |params->matches| will be empty. | 857 // match in it, whereas |params->matches| will be empty. |
| 858 if (!params->failed) { | 858 if (!params->failed) { |
| 859 matches_.clear(); | 859 matches_.clear(); |
| 860 PromoteMatchesIfNecessary(*params); | 860 PromoteMatchesIfNecessary(*params); |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1192 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | 1192 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| 1193 match.contents.length(), ACMatchClassification::URL, | 1193 match.contents.length(), ACMatchClassification::URL, |
| 1194 &match.contents_class); | 1194 &match.contents_class); |
| 1195 } | 1195 } |
| 1196 match.description = info.title(); | 1196 match.description = info.title(); |
| 1197 match.description_class = | 1197 match.description_class = |
| 1198 ClassifyDescription(params.input.text(), match.description); | 1198 ClassifyDescription(params.input.text(), match.description); |
| 1199 RecordAdditionalInfoFromUrlRow(info, &match); | 1199 RecordAdditionalInfoFromUrlRow(info, &match); |
| 1200 return match; | 1200 return match; |
| 1201 } | 1201 } |
| OLD | NEW |