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 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
561 // url_db can be NULL if it hasn't finished initializing (or failed to | 561 // url_db can be NULL if it hasn't finished initializing (or failed to |
562 // initialize). In this case all we can do is fall back on the second | 562 // initialize). In this case all we can do is fall back on the second |
563 // pass. | 563 // pass. |
564 // | 564 // |
565 // TODO(pkasting): We should just block here until this loads. Any time | 565 // TODO(pkasting): We should just block here until this loads. Any time |
566 // someone unloads the history backend, we'll get inconsistent inline | 566 // someone unloads the history backend, we'll get inconsistent inline |
567 // autocomplete behavior here. | 567 // autocomplete behavior here. |
568 if (url_db) { | 568 if (url_db) { |
569 DoAutocomplete(NULL, url_db, params.get()); | 569 DoAutocomplete(NULL, url_db, params.get()); |
570 matches_.clear(); | 570 matches_.clear(); |
571 PromoteMatchIfNecessary(*params); | 571 PromoteMatchesIfNecessary(*params); |
572 // NOTE: We don't reset |params| here since at least the |promote_type| | 572 // NOTE: We don't reset |params| here since at least the |promote_type| |
573 // field on it will be read by the second pass -- see comments in | 573 // field on it will be read by the second pass -- see comments in |
574 // DoAutocomplete(). | 574 // DoAutocomplete(). |
575 } | 575 } |
576 | 576 |
577 // Pass 2: Ask the history service to call us back on the history thread, | 577 // Pass 2: Ask the history service to call us back on the history thread, |
578 // where we can read the full on-disk DB. | 578 // where we can read the full on-disk DB. |
579 if (input.want_asynchronous_matches()) { | 579 if (input.want_asynchronous_matches()) { |
580 done_ = false; | 580 done_ = false; |
581 params_ = params.release(); // This object will be destroyed in | 581 params_ = params.release(); // This object will be destroyed in |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
759 SortAndDedupMatches(¶ms->matches); | 759 SortAndDedupMatches(¶ms->matches); |
760 | 760 |
761 // Try to create a shorter suggestion from the best match. | 761 // Try to create a shorter suggestion from the best match. |
762 // We consider the what you typed match eligible for display when it's | 762 // We consider the what you typed match eligible for display when it's |
763 // navigable and there's a reasonable chance the user intended to do | 763 // navigable and there's a reasonable chance the user intended to do |
764 // something other than search. We use a variety of heuristics to determine | 764 // something other than search. We use a variety of heuristics to determine |
765 // this, e.g. whether the user explicitly typed a scheme, or if omnibox | 765 // this, e.g. whether the user explicitly typed a scheme, or if omnibox |
766 // searching has been disabled by policy. In the cases where we've parsed as | 766 // searching has been disabled by policy. In the cases where we've parsed as |
767 // UNKNOWN, we'll still show an accidental search infobar if need be. | 767 // UNKNOWN, we'll still show an accidental search infobar if need be. |
768 VisitClassifier classifier(this, params->input, db); | 768 VisitClassifier classifier(this, params->input, db); |
769 bool have_what_you_typed_match = | 769 params->have_what_you_typed_match = |
770 (params->input.type() != metrics::OmniboxInputType::QUERY) && | 770 (params->input.type() != metrics::OmniboxInputType::QUERY) && |
771 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) || | 771 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) || |
772 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || | 772 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || |
773 !params->trim_http || | 773 !params->trim_http || |
774 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0) || | 774 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0) || |
775 !params->default_search_provider); | 775 !params->default_search_provider); |
776 const bool have_shorter_suggestion_suitable_for_inline_autocomplete = | 776 const bool have_shorter_suggestion_suitable_for_inline_autocomplete = |
777 PromoteOrCreateShorterSuggestion(db, have_what_you_typed_match, params); | 777 PromoteOrCreateShorterSuggestion(db, params); |
778 | 778 |
779 // Check whether what the user typed appears in history. | 779 // Check whether what the user typed appears in history. |
780 const bool can_check_history_for_exact_match = | 780 const bool can_check_history_for_exact_match = |
781 // Checking what_you_typed_match.is_history_what_you_typed_match tells us | 781 // Checking what_you_typed_match.is_history_what_you_typed_match tells us |
782 // whether SuggestExactInput() succeeded in constructing a valid match. | 782 // whether SuggestExactInput() succeeded in constructing a valid match. |
783 params->what_you_typed_match.is_history_what_you_typed_match && | 783 params->what_you_typed_match.is_history_what_you_typed_match && |
784 // Additionally, in the case where the user has typed "foo.com" and | 784 // Additionally, in the case where the user has typed "foo.com" and |
785 // visited (but not typed) "foo/", and the input is "foo", the first pass | 785 // visited (but not typed) "foo/", and the input is "foo", the first pass |
786 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the | 786 // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the |
787 // second pass can suggest the exact input as a better URL. Since we need | 787 // second pass can suggest the exact input as a better URL. Since we need |
788 // both passes to agree, and since during the first pass there's no way to | 788 // both passes to agree, and since during the first pass there's no way to |
789 // know about "foo/", ensure that if the promote type was set to | 789 // know about "foo/", ensure that if the promote type was set to |
790 // FRONT_HISTORY_MATCH during the first pass, the second pass will not | 790 // FRONT_HISTORY_MATCH during the first pass, the second pass will not |
791 // consider the exact suggestion to be in history and therefore will not | 791 // consider the exact suggestion to be in history and therefore will not |
792 // suggest the exact input as a better match. (Note that during the first | 792 // suggest the exact input as a better match. (Note that during the first |
793 // pass, this conditional will always succeed since |promote_type| is | 793 // pass, this conditional will always succeed since |promote_type| is |
794 // initialized to NEITHER.) | 794 // initialized to NEITHER.) |
795 (params->promote_type != HistoryURLProviderParams::FRONT_HISTORY_MATCH); | 795 (params->promote_type != HistoryURLProviderParams::FRONT_HISTORY_MATCH); |
796 params->exact_suggestion_is_in_history = can_check_history_for_exact_match && | 796 params->exact_suggestion_is_in_history = can_check_history_for_exact_match && |
797 FixupExactSuggestion(db, classifier, params); | 797 FixupExactSuggestion(db, classifier, params); |
798 | 798 |
799 // If we succeeded in fixing up the exact match based on the user's history, | 799 // If we succeeded in fixing up the exact match based on the user's history, |
800 // we should treat it as the best match regardless of input type. If not, | 800 // we should treat it as the best match regardless of input type. If not, |
801 // then we check whether there's an inline autocompletion we can create from | 801 // then we check whether there's an inline autocompletion we can create from |
802 // this input, so we can promote that as the best match. | 802 // this input, so we can promote that as the best match. |
803 if (params->exact_suggestion_is_in_history) { | 803 if (params->exact_suggestion_is_in_history) { |
804 params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH; | 804 params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH; |
805 } else if (!params->prevent_inline_autocomplete && !params->matches.empty() && | 805 } else if (!params->matches.empty() && |
Peter Kasting
2015/03/24 22:02:43
Rather than just removing the |prevent_inline_auto
Mark P
2015/03/24 22:35:46
This is not a problem. Promoting has to do with s
Peter Kasting
2015/03/24 23:05:19
That is fine as long as SearchProvider always actu
Mark P
2015/03/25 19:04:57
SearchProvider is always supposed to provide a leg
| |
806 (have_shorter_suggestion_suitable_for_inline_autocomplete || | 806 (have_shorter_suggestion_suitable_for_inline_autocomplete || |
807 CanPromoteMatchForInlineAutocomplete(params->matches[0]))) { | 807 CanPromoteMatchForInlineAutocomplete(params->matches[0]))) { |
808 params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH; | 808 params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH; |
Peter Kasting
2015/03/24 23:05:19
OK, so let's put something like this comment here:
Mark P
2015/03/25 19:04:57
Okay, that's a nice clarification to add. Added w
| |
809 } else { | 809 } else { |
810 // Failed to promote any URLs. Use the What You Typed match, if we have it. | 810 // Failed to promote any URLs. Use the What You Typed match, if we have it. |
811 params->promote_type = have_what_you_typed_match ? | 811 params->promote_type = params->have_what_you_typed_match ? |
812 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH : | 812 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH : |
813 HistoryURLProviderParams::NEITHER; | 813 HistoryURLProviderParams::NEITHER; |
814 } | 814 } |
815 | 815 |
816 const size_t max_results = | 816 const size_t max_results = |
817 kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0); | 817 kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0); |
818 if (backend) { | 818 if (backend) { |
819 // Remove redirects and trim list to size. We want to provide up to | 819 // Remove redirects and trim list to size. We want to provide up to |
820 // kMaxMatches results plus the What You Typed result, if it was added to | 820 // kMaxMatches results plus the What You Typed result, if it was added to |
821 // params->matches above. | 821 // params->matches above. |
822 CullRedirects(backend, ¶ms->matches, max_results); | 822 CullRedirects(backend, ¶ms->matches, max_results); |
823 } else if (params->matches.size() > max_results) { | 823 } else if (params->matches.size() > max_results) { |
824 // Simply trim the list to size. | 824 // Simply trim the list to size. |
825 params->matches.resize(max_results); | 825 params->matches.resize(max_results); |
826 } | 826 } |
827 } | 827 } |
828 | 828 |
829 void HistoryURLProvider::PromoteMatchIfNecessary( | 829 void HistoryURLProvider::PromoteMatchesIfNecessary( |
830 const HistoryURLProviderParams& params) { | 830 const HistoryURLProviderParams& params) { |
831 if (params.promote_type == HistoryURLProviderParams::NEITHER) | 831 if (params.promote_type == HistoryURLProviderParams::NEITHER) |
832 return; | 832 return; |
833 matches_.push_back( | 833 if (params.promote_type == HistoryURLProviderParams::FRONT_HISTORY_MATCH) { |
834 (params.promote_type == HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) ? | 834 matches_.push_back( |
835 params.what_you_typed_match : | 835 HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE, |
836 HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE, | 836 CalculateRelevance(INLINE_AUTOCOMPLETE, 0))); |
837 CalculateRelevance(INLINE_AUTOCOMPLETE, 0))); | 837 } |
838 // Add the what-you-typed match if we're explicitly told to or if we might | |
839 // need it (because inline autocompletion is disabled, i.e., no other URL | |
840 // suggestion is allowed to be the default match) and it's eligible for | |
841 // display. | |
Peter Kasting
2015/03/24 22:02:43
After thinking more, this code is correct.
Mark P
2015/03/24 22:35:46
Acknowledged.
Peter Kasting
2015/03/24 23:05:19
Let's make the comment here something like this:
Mark P
2015/03/25 19:04:57
Done with trivial edits.
| |
842 if ((params.promote_type == HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) || | |
843 (params.prevent_inline_autocomplete && | |
844 params.have_what_you_typed_match)) { | |
845 matches_.push_back(params.what_you_typed_match); | |
846 } | |
838 } | 847 } |
839 | 848 |
840 void HistoryURLProvider::QueryComplete( | 849 void HistoryURLProvider::QueryComplete( |
841 HistoryURLProviderParams* params_gets_deleted) { | 850 HistoryURLProviderParams* params_gets_deleted) { |
842 // Ensure |params_gets_deleted| gets deleted on exit. | 851 // Ensure |params_gets_deleted| gets deleted on exit. |
843 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); | 852 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); |
844 | 853 |
845 // If the user hasn't already started another query, clear our member pointer | 854 // If the user hasn't already started another query, clear our member pointer |
846 // so we can't write into deleted memory. | 855 // so we can't write into deleted memory. |
847 if (params_ == params_gets_deleted) | 856 if (params_ == params_gets_deleted) |
848 params_ = NULL; | 857 params_ = NULL; |
849 | 858 |
850 // Don't send responses for queries that have been canceled. | 859 // Don't send responses for queries that have been canceled. |
851 if (params->cancel_flag.IsSet()) | 860 if (params->cancel_flag.IsSet()) |
852 return; // Already set done_ when we canceled, no need to set it again. | 861 return; // Already set done_ when we canceled, no need to set it again. |
853 | 862 |
854 // Don't modify |matches_| if the query failed, since it might have a default | 863 // Don't modify |matches_| if the query failed, since it might have a default |
855 // match in it, whereas |params->matches| will be empty. | 864 // match in it, whereas |params->matches| will be empty. |
856 if (!params->failed) { | 865 if (!params->failed) { |
857 matches_.clear(); | 866 matches_.clear(); |
858 PromoteMatchIfNecessary(*params); | 867 PromoteMatchesIfNecessary(*params); |
859 | 868 |
860 // Determine relevance of highest scoring match, if any. | 869 // Determine relevance of highest scoring match, if any. |
861 int relevance = matches_.empty() ? | 870 int relevance = matches_.empty() ? |
862 CalculateRelevance(NORMAL, | 871 CalculateRelevance(NORMAL, |
863 static_cast<int>(params->matches.size() - 1)) : | 872 static_cast<int>(params->matches.size() - 1)) : |
864 matches_[0].relevance; | 873 matches_[0].relevance; |
865 | 874 |
866 // Convert the history matches to autocomplete matches. If we promoted the | 875 // Convert the history matches to autocomplete matches. If we promoted the |
867 // first match, skip over it. | 876 // first match, skip over it. |
868 const size_t first_match = | 877 const size_t first_match = |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
949 const size_t registry_length = | 958 const size_t registry_length = |
950 net::registry_controlled_domains::GetRegistryLength( | 959 net::registry_controlled_domains::GetRegistryLength( |
951 host, | 960 host, |
952 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | 961 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
953 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 962 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
954 return registry_length == 0 && db->IsTypedHost(host); | 963 return registry_length == 0 && db->IsTypedHost(host); |
955 } | 964 } |
956 | 965 |
957 bool HistoryURLProvider::PromoteOrCreateShorterSuggestion( | 966 bool HistoryURLProvider::PromoteOrCreateShorterSuggestion( |
958 history::URLDatabase* db, | 967 history::URLDatabase* db, |
959 bool have_what_you_typed_match, | |
960 HistoryURLProviderParams* params) { | 968 HistoryURLProviderParams* params) { |
961 if (params->matches.empty()) | 969 if (params->matches.empty()) |
962 return false; // No matches, nothing to do. | 970 return false; // No matches, nothing to do. |
963 | 971 |
964 // Determine the base URL from which to search, and whether that URL could | 972 // Determine the base URL from which to search, and whether that URL could |
965 // itself be added as a match. We can add the base iff it's not "effectively | 973 // itself be added as a match. We can add the base iff it's not "effectively |
966 // the same" as any "what you typed" match. | 974 // the same" as any "what you typed" match. |
967 const history::HistoryMatch& match = params->matches[0]; | 975 const history::HistoryMatch& match = params->matches[0]; |
968 GURL search_base = ConvertToHostOnly(match, params->input.text()); | 976 GURL search_base = ConvertToHostOnly(match, params->input.text()); |
969 bool can_add_search_base_to_matches = !have_what_you_typed_match; | 977 bool can_add_search_base_to_matches = !params->have_what_you_typed_match; |
970 if (search_base.is_empty()) { | 978 if (search_base.is_empty()) { |
971 // Search from what the user typed when we couldn't reduce the best match | 979 // Search from what the user typed when we couldn't reduce the best match |
972 // to a host. Careful: use a substring of |match| here, rather than the | 980 // to a host. Careful: use a substring of |match| here, rather than the |
973 // first match in |params|, because they might have different prefixes. If | 981 // first match in |params|, because they might have different prefixes. If |
974 // the user typed "google.com", params->what_you_typed_match will hold | 982 // the user typed "google.com", params->what_you_typed_match will hold |
975 // "http://google.com/", but |match| might begin with | 983 // "http://google.com/", but |match| might begin with |
976 // "http://www.google.com/". | 984 // "http://www.google.com/". |
977 // TODO: this should be cleaned up, and is probably incorrect for IDN. | 985 // TODO: this should be cleaned up, and is probably incorrect for IDN. |
978 std::string new_match = match.url_info.url().possibly_invalid_spec(). | 986 std::string new_match = match.url_info.url().possibly_invalid_spec(). |
979 substr(0, match.input_location + params->input.text().length()); | 987 substr(0, match.input_location + params->input.text().length()); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1153 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | 1161 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
1154 match.contents.length(), ACMatchClassification::URL, | 1162 match.contents.length(), ACMatchClassification::URL, |
1155 &match.contents_class); | 1163 &match.contents_class); |
1156 } | 1164 } |
1157 match.description = info.title(); | 1165 match.description = info.title(); |
1158 match.description_class = | 1166 match.description_class = |
1159 ClassifyDescription(params.input.text(), match.description); | 1167 ClassifyDescription(params.input.text(), match.description); |
1160 RecordAdditionalInfoFromUrlRow(info, &match); | 1168 RecordAdditionalInfoFromUrlRow(info, &match); |
1161 return match; | 1169 return match; |
1162 } | 1170 } |
OLD | NEW |