Chromium Code Reviews| 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/history_url_provider.h" | 5 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 516 // url_db can be NULL if it hasn't finished initializing (or failed to | 516 // url_db can be NULL if it hasn't finished initializing (or failed to |
| 517 // initialize). In this case all we can do is fall back on the second | 517 // initialize). In this case all we can do is fall back on the second |
| 518 // pass. | 518 // pass. |
| 519 // | 519 // |
| 520 // TODO(pkasting): We should just block here until this loads. Any time | 520 // TODO(pkasting): We should just block here until this loads. Any time |
| 521 // someone unloads the history backend, we'll get inconsistent inline | 521 // someone unloads the history backend, we'll get inconsistent inline |
| 522 // autocomplete behavior here. | 522 // autocomplete behavior here. |
| 523 if (url_db) { | 523 if (url_db) { |
| 524 DoAutocomplete(NULL, url_db, params.get()); | 524 DoAutocomplete(NULL, url_db, params.get()); |
| 525 matches_.clear(); | 525 matches_.clear(); |
| 526 PromoteMatchIfNecessary(*params); | 526 PromoteMatchesIfNecessary(*params); |
| 527 UpdateStarredStateOfMatches(); | 527 UpdateStarredStateOfMatches(); |
| 528 // NOTE: We don't reset |params| here since at least the |promote_type| | 528 // NOTE: We don't reset |params| here since at least the |promote_type| |
| 529 // field on it will be read by the second pass -- see comments in | 529 // field on it will be read by the second pass -- see comments in |
| 530 // DoAutocomplete(). | 530 // DoAutocomplete(). |
| 531 } | 531 } |
| 532 | 532 |
| 533 // Pass 2: Ask the history service to call us back on the history thread, | 533 // Pass 2: Ask the history service to call us back on the history thread, |
| 534 // where we can read the full on-disk DB. | 534 // where we can read the full on-disk DB. |
| 535 if (input.want_asynchronous_matches()) { | 535 if (input.want_asynchronous_matches()) { |
| 536 done_ = false; | 536 done_ = false; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 708 *j, i->prefix.length(), !i->num_components, | 708 *j, i->prefix.length(), !i->num_components, |
| 709 i->num_components >= best_prefix->num_components)); | 709 i->num_components >= best_prefix->num_components)); |
| 710 } | 710 } |
| 711 } | 711 } |
| 712 | 712 |
| 713 // Create sorted list of suggestions. | 713 // Create sorted list of suggestions. |
| 714 CullPoorMatches(params); | 714 CullPoorMatches(params); |
| 715 SortAndDedupMatches(¶ms->matches); | 715 SortAndDedupMatches(¶ms->matches); |
| 716 | 716 |
| 717 // Try to create a shorter suggestion from the best match. | 717 // Try to create a shorter suggestion from the best match. |
| 718 // We allow the what you typed match to be displayed when there's a reasonable | 718 // We consider the what you typed match to be eligible to be displayed when |
|
Peter Kasting
2014/06/20 21:12:42
Nit: to be eligible to be displayed -> eligible fo
Mark P
2014/06/23 22:23:12
Done.
| |
| 719 // chance the user actually cares: | 719 // there's a reasonable chance the user actually cares: |
| 720 // * Their input can be opened as a URL, and | 720 // * Their input can be opened as a URL, and |
| 721 // * We parsed the input as a URL, or it starts with an explicit "http:" or | 721 // * We parsed the input as a URL, or it starts with an explicit "http:" or |
| 722 // "https:". | 722 // "https:". |
| 723 // Otherwise, this is just low-quality noise. In the cases where we've parsed | 723 // Otherwise, this is just low-quality noise. In the cases where we've parsed |
| 724 // as UNKNOWN, we'll still show an accidental search infobar if need be. | 724 // as UNKNOWN, we'll still show an accidental search infobar if need be. |
| 725 VisitClassifier classifier(this, params->input, db); | 725 VisitClassifier classifier(this, params->input, db); |
| 726 bool have_what_you_typed_match = | 726 params->have_what_you_typed_match = |
| 727 (params->input.type() != metrics::OmniboxInputType::QUERY) && | 727 (params->input.type() != metrics::OmniboxInputType::QUERY) && |
| 728 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) || | 728 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) || |
| 729 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || | 729 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || |
| 730 !params->trim_http || | 730 !params->trim_http || |
| 731 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0)); | 731 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0)); |
| 732 const bool have_shorter_suggestion_suitable_for_inline_autocomplete = | 732 const bool have_shorter_suggestion_suitable_for_inline_autocomplete = |
| 733 PromoteOrCreateShorterSuggestion(db, have_what_you_typed_match, params); | 733 PromoteOrCreateShorterSuggestion( |
| 734 db, params->have_what_you_typed_match, params); | |
| 734 | 735 |
| 735 // Check whether what the user typed appears in history. | 736 // Check whether what the user typed appears in history. |
| 736 const bool can_check_history_for_exact_match = | 737 const bool can_check_history_for_exact_match = |
| 737 // Checking what_you_typed_match.is_history_what_you_typed_match tells us | 738 // Checking what_you_typed_match.is_history_what_you_typed_match tells us |
| 738 // whether SuggestExactInput() succeeded in constructing a valid match. | 739 // whether SuggestExactInput() succeeded in constructing a valid match. |
| 739 params->what_you_typed_match.is_history_what_you_typed_match && | 740 params->what_you_typed_match.is_history_what_you_typed_match && |
| 740 // Additionally, in the case where the user has typed "foo.com" and | 741 // Additionally, in the case where the user has typed "foo.com" and |
| 741 // visited (but not typed) "foo/", and the input is "foo", the first pass | 742 // visited (but not typed) "foo/", and the input is "foo", the first pass |
| 742 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the | 743 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the |
| 743 // second pass can suggest the exact input as a better URL. Since we need | 744 // second pass can suggest the exact input as a better URL. Since we need |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 757 // then we check whether there's an inline autocompletion we can create from | 758 // then we check whether there's an inline autocompletion we can create from |
| 758 // this input, so we can promote that as the best match. | 759 // this input, so we can promote that as the best match. |
| 759 if (params->exact_suggestion_is_in_history) { | 760 if (params->exact_suggestion_is_in_history) { |
| 760 params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH; | 761 params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH; |
| 761 } else if (!params->prevent_inline_autocomplete && !params->matches.empty() && | 762 } else if (!params->prevent_inline_autocomplete && !params->matches.empty() && |
| 762 (have_shorter_suggestion_suitable_for_inline_autocomplete || | 763 (have_shorter_suggestion_suitable_for_inline_autocomplete || |
| 763 CanPromoteMatchForInlineAutocomplete(params->matches[0]))) { | 764 CanPromoteMatchForInlineAutocomplete(params->matches[0]))) { |
| 764 params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH; | 765 params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH; |
| 765 } else { | 766 } else { |
| 766 // Failed to promote any URLs. Use the What You Typed match, if we have it. | 767 // Failed to promote any URLs. Use the What You Typed match, if we have it. |
| 767 params->promote_type = have_what_you_typed_match ? | 768 params->promote_type = params->have_what_you_typed_match ? |
| 768 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH : | 769 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH : |
| 769 HistoryURLProviderParams::NEITHER; | 770 HistoryURLProviderParams::NEITHER; |
| 770 } | 771 } |
| 771 | 772 |
| 772 const size_t max_results = | 773 const size_t max_results = |
| 773 kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0); | 774 kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0); |
| 774 if (backend && cull_redirects_) { | 775 if (backend && cull_redirects_) { |
| 775 // Remove redirects and trim list to size. We want to provide up to | 776 // Remove redirects and trim list to size. We want to provide up to |
| 776 // kMaxMatches results plus the What You Typed result, if it was added to | 777 // kMaxMatches results plus the What You Typed result, if it was added to |
| 777 // params->matches above. | 778 // params->matches above. |
| 778 CullRedirects(backend, ¶ms->matches, max_results); | 779 CullRedirects(backend, ¶ms->matches, max_results); |
| 779 } else if (params->matches.size() > max_results) { | 780 } else if (params->matches.size() > max_results) { |
| 780 // Simply trim the list to size. | 781 // Simply trim the list to size. |
| 781 params->matches.resize(max_results); | 782 params->matches.resize(max_results); |
| 782 } | 783 } |
| 783 } | 784 } |
| 784 | 785 |
| 785 void HistoryURLProvider::PromoteMatchIfNecessary( | 786 void HistoryURLProvider::PromoteMatchesIfNecessary( |
| 786 const HistoryURLProviderParams& params) { | 787 const HistoryURLProviderParams& params) { |
| 787 if (params.promote_type == HistoryURLProviderParams::NEITHER) | 788 if (params.promote_type == HistoryURLProviderParams::FRONT_HISTORY_MATCH) { |
| 788 return; | 789 matches_.push_back(HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE, |
| 789 matches_.push_back( | 790 CalculateRelevance(INLINE_AUTOCOMPLETE, 0))); |
| 790 (params.promote_type == HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) ? | 791 if (OmniboxFieldTrial::AddUWYTMatchEvenIfPromotedURLs() && |
| 791 params.what_you_typed_match : | 792 params.have_what_you_typed_match) { |
| 792 HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE, | 793 matches_.push_back(params.what_you_typed_match); |
| 793 CalculateRelevance(INLINE_AUTOCOMPLETE, 0))); | 794 } |
| 795 } else if (params.promote_type == | |
| 796 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) { | |
| 797 matches_.push_back(params.what_you_typed_match); | |
| 798 } | |
| 794 } | 799 } |
| 795 | 800 |
| 796 void HistoryURLProvider::QueryComplete( | 801 void HistoryURLProvider::QueryComplete( |
| 797 HistoryURLProviderParams* params_gets_deleted) { | 802 HistoryURLProviderParams* params_gets_deleted) { |
| 798 // Ensure |params_gets_deleted| gets deleted on exit. | 803 // Ensure |params_gets_deleted| gets deleted on exit. |
| 799 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); | 804 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); |
| 800 | 805 |
| 801 // If the user hasn't already started another query, clear our member pointer | 806 // If the user hasn't already started another query, clear our member pointer |
| 802 // so we can't write into deleted memory. | 807 // so we can't write into deleted memory. |
| 803 if (params_ == params_gets_deleted) | 808 if (params_ == params_gets_deleted) |
| 804 params_ = NULL; | 809 params_ = NULL; |
| 805 | 810 |
| 806 // Don't send responses for queries that have been canceled. | 811 // Don't send responses for queries that have been canceled. |
| 807 if (params->cancel_flag.IsSet()) | 812 if (params->cancel_flag.IsSet()) |
| 808 return; // Already set done_ when we canceled, no need to set it again. | 813 return; // Already set done_ when we canceled, no need to set it again. |
| 809 | 814 |
| 810 // Don't modify |matches_| if the query failed, since it might have a default | 815 // Don't modify |matches_| if the query failed, since it might have a default |
| 811 // match in it, whereas |params->matches| will be empty. | 816 // match in it, whereas |params->matches| will be empty. |
| 812 if (!params->failed) { | 817 if (!params->failed) { |
| 813 matches_.clear(); | 818 matches_.clear(); |
| 814 PromoteMatchIfNecessary(*params); | 819 PromoteMatchesIfNecessary(*params); |
| 815 | 820 |
| 816 // Determine relevance of highest scoring match, if any. | 821 // Determine relevance of highest scoring match, if any. |
| 817 int relevance = matches_.empty() ? | 822 int relevance = matches_.empty() ? |
| 818 CalculateRelevance(NORMAL, | 823 CalculateRelevance(NORMAL, |
| 819 static_cast<int>(params->matches.size() - 1)) : | 824 static_cast<int>(params->matches.size() - 1)) : |
| 820 matches_[0].relevance; | 825 matches_[0].relevance; |
| 821 | 826 |
| 822 // Convert the history matches to autocomplete matches. If we promoted the | 827 // Convert the history matches to autocomplete matches. If we promoted the |
| 823 // first match, skip over it. | 828 // first match, skip over it. |
| 824 const size_t first_match = | 829 const size_t first_match = |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1137 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | 1142 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| 1138 match.contents.length(), ACMatchClassification::URL, | 1143 match.contents.length(), ACMatchClassification::URL, |
| 1139 &match.contents_class); | 1144 &match.contents_class); |
| 1140 } | 1145 } |
| 1141 match.description = info.title(); | 1146 match.description = info.title(); |
| 1142 match.description_class = | 1147 match.description_class = |
| 1143 ClassifyDescription(params.input.text(), match.description); | 1148 ClassifyDescription(params.input.text(), match.description); |
| 1144 RecordAdditionalInfoFromUrlRow(info, &match); | 1149 RecordAdditionalInfoFromUrlRow(info, &match); |
| 1145 return match; | 1150 return match; |
| 1146 } | 1151 } |
| OLD | NEW |