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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 else | 79 else |
80 matches->push_back(match); | 80 matches->push_back(match); |
81 | 81 |
82 return true; | 82 return true; |
83 } | 83 } |
84 | 84 |
85 // Given the user's |input| and a |match| created from it, reduce the match's | 85 // Given the user's |input| and a |match| created from it, reduce the match's |
86 // URL to just a host. If this host still matches the user input, return it. | 86 // URL to just a host. If this host still matches the user input, return it. |
87 // Returns the empty string on failure. | 87 // Returns the empty string on failure. |
88 GURL ConvertToHostOnly(const history::HistoryMatch& match, | 88 GURL ConvertToHostOnly(const history::HistoryMatch& match, |
89 const string16& input) { | 89 const base::string16& input) { |
90 // See if we should try to do host-only suggestions for this URL. Nonstandard | 90 // See if we should try to do host-only suggestions for this URL. Nonstandard |
91 // schemes means there's no authority section, so suggesting the host name | 91 // schemes means there's no authority section, so suggesting the host name |
92 // is useless. File URLs are standard, but host suggestion is not useful for | 92 // is useless. File URLs are standard, but host suggestion is not useful for |
93 // them either. | 93 // them either. |
94 const GURL& url = match.url_info.url(); | 94 const GURL& url = match.url_info.url(); |
95 if (!url.is_valid() || !url.IsStandard() || url.SchemeIsFile()) | 95 if (!url.is_valid() || !url.IsStandard() || url.SchemeIsFile()) |
96 return GURL(); | 96 return GURL(); |
97 | 97 |
98 // Transform to a host-only match. Bail if the host no longer matches the | 98 // Transform to a host-only match. Bail if the host no longer matches the |
99 // user input (e.g. because the user typed more than just a host). | 99 // user input (e.g. because the user typed more than just a host). |
100 GURL host = url.GetWithEmptyPath(); | 100 GURL host = url.GetWithEmptyPath(); |
101 if ((host.spec().length() < (match.input_location + input.length()))) | 101 if ((host.spec().length() < (match.input_location + input.length()))) |
102 return GURL(); // User typing is longer than this host suggestion. | 102 return GURL(); // User typing is longer than this host suggestion. |
103 | 103 |
104 const string16 spec = UTF8ToUTF16(host.spec()); | 104 const base::string16 spec = UTF8ToUTF16(host.spec()); |
105 if (spec.compare(match.input_location, input.length(), input)) | 105 if (spec.compare(match.input_location, input.length(), input)) |
106 return GURL(); // User typing is no longer a prefix. | 106 return GURL(); // User typing is no longer a prefix. |
107 | 107 |
108 return host; | 108 return host; |
109 } | 109 } |
110 | 110 |
111 // Acts like the > operator for URLInfo classes. | 111 // Acts like the > operator for URLInfo classes. |
112 bool CompareHistoryMatch(const history::HistoryMatch& a, | 112 bool CompareHistoryMatch(const history::HistoryMatch& a, |
113 const history::HistoryMatch& b) { | 113 const history::HistoryMatch& b) { |
114 // A promoted match is better than non-promoted. | 114 // A promoted match is better than non-promoted. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // member strings, then returning those strings when its own getters are called. | 197 // member strings, then returning those strings when its own getters are called. |
198 // This will typically be constructed on the UI thread from | 198 // This will typically be constructed on the UI thread from |
199 // UIThreadSearchTermsData but is subsequently safe to use on any thread. | 199 // UIThreadSearchTermsData but is subsequently safe to use on any thread. |
200 class SearchTermsDataSnapshot : public SearchTermsData { | 200 class SearchTermsDataSnapshot : public SearchTermsData { |
201 public: | 201 public: |
202 explicit SearchTermsDataSnapshot(const SearchTermsData& search_terms_data); | 202 explicit SearchTermsDataSnapshot(const SearchTermsData& search_terms_data); |
203 virtual ~SearchTermsDataSnapshot(); | 203 virtual ~SearchTermsDataSnapshot(); |
204 | 204 |
205 virtual std::string GoogleBaseURLValue() const OVERRIDE; | 205 virtual std::string GoogleBaseURLValue() const OVERRIDE; |
206 virtual std::string GetApplicationLocale() const OVERRIDE; | 206 virtual std::string GetApplicationLocale() const OVERRIDE; |
207 virtual string16 GetRlzParameterValue() const OVERRIDE; | 207 virtual base::string16 GetRlzParameterValue() const OVERRIDE; |
208 virtual std::string GetSearchClient() const OVERRIDE; | 208 virtual std::string GetSearchClient() const OVERRIDE; |
209 virtual std::string ForceInstantResultsParam( | 209 virtual std::string ForceInstantResultsParam( |
210 bool for_prerender) const OVERRIDE; | 210 bool for_prerender) const OVERRIDE; |
211 virtual std::string InstantExtendedEnabledParam() const OVERRIDE; | 211 virtual std::string InstantExtendedEnabledParam() const OVERRIDE; |
212 virtual std::string NTPIsThemedParam() const OVERRIDE; | 212 virtual std::string NTPIsThemedParam() const OVERRIDE; |
213 | 213 |
214 private: | 214 private: |
215 std::string google_base_url_value_; | 215 std::string google_base_url_value_; |
216 std::string application_locale_; | 216 std::string application_locale_; |
217 string16 rlz_parameter_value_; | 217 base::string16 rlz_parameter_value_; |
218 std::string search_client_; | 218 std::string search_client_; |
219 std::string force_instant_results_param_; | 219 std::string force_instant_results_param_; |
220 std::string instant_extended_enabled_param_; | 220 std::string instant_extended_enabled_param_; |
221 std::string ntp_is_themed_param_; | 221 std::string ntp_is_themed_param_; |
222 | 222 |
223 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot); | 223 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot); |
224 }; | 224 }; |
225 | 225 |
226 SearchTermsDataSnapshot::SearchTermsDataSnapshot( | 226 SearchTermsDataSnapshot::SearchTermsDataSnapshot( |
227 const SearchTermsData& search_terms_data) | 227 const SearchTermsData& search_terms_data) |
(...skipping 11 matching lines...) Expand all Loading... |
239 } | 239 } |
240 | 240 |
241 std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const { | 241 std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const { |
242 return google_base_url_value_; | 242 return google_base_url_value_; |
243 } | 243 } |
244 | 244 |
245 std::string SearchTermsDataSnapshot::GetApplicationLocale() const { | 245 std::string SearchTermsDataSnapshot::GetApplicationLocale() const { |
246 return application_locale_; | 246 return application_locale_; |
247 } | 247 } |
248 | 248 |
249 string16 SearchTermsDataSnapshot::GetRlzParameterValue() const { | 249 base::string16 SearchTermsDataSnapshot::GetRlzParameterValue() const { |
250 return rlz_parameter_value_; | 250 return rlz_parameter_value_; |
251 } | 251 } |
252 | 252 |
253 std::string SearchTermsDataSnapshot::GetSearchClient() const { | 253 std::string SearchTermsDataSnapshot::GetSearchClient() const { |
254 return search_client_; | 254 return search_client_; |
255 } | 255 } |
256 | 256 |
257 std::string SearchTermsDataSnapshot::ForceInstantResultsParam( | 257 std::string SearchTermsDataSnapshot::ForceInstantResultsParam( |
258 bool for_prerender) const { | 258 bool for_prerender) const { |
259 return force_instant_results_param_; | 259 return force_instant_results_param_; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 392 } |
393 | 393 |
394 void HistoryURLProvider::Stop(bool clear_cached_results) { | 394 void HistoryURLProvider::Stop(bool clear_cached_results) { |
395 done_ = true; | 395 done_ = true; |
396 | 396 |
397 if (params_) | 397 if (params_) |
398 params_->cancel_flag.Set(); | 398 params_->cancel_flag.Set(); |
399 } | 399 } |
400 | 400 |
401 AutocompleteMatch HistoryURLProvider::SuggestExactInput( | 401 AutocompleteMatch HistoryURLProvider::SuggestExactInput( |
402 const string16& text, | 402 const base::string16& text, |
403 const GURL& destination_url, | 403 const GURL& destination_url, |
404 bool trim_http) { | 404 bool trim_http) { |
405 AutocompleteMatch match(this, 0, false, | 405 AutocompleteMatch match(this, 0, false, |
406 AutocompleteMatchType::URL_WHAT_YOU_TYPED); | 406 AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
407 | 407 |
408 if (destination_url.is_valid()) { | 408 if (destination_url.is_valid()) { |
409 match.destination_url = destination_url; | 409 match.destination_url = destination_url; |
410 | 410 |
411 // Trim off "http://" if the user didn't type it. | 411 // Trim off "http://" if the user didn't type it. |
412 // NOTE: We use TrimHttpPrefix() here rather than StringForURLDisplay() to | 412 // NOTE: We use TrimHttpPrefix() here rather than StringForURLDisplay() to |
413 // strip the scheme as we need to know the offset so we can adjust the | 413 // strip the scheme as we need to know the offset so we can adjust the |
414 // |match_location| below. StringForURLDisplay() and TrimHttpPrefix() have | 414 // |match_location| below. StringForURLDisplay() and TrimHttpPrefix() have |
415 // slightly different behavior as well (the latter will strip even without | 415 // slightly different behavior as well (the latter will strip even without |
416 // two slashes after the scheme). | 416 // two slashes after the scheme). |
417 DCHECK(!trim_http || !AutocompleteInput::HasHTTPScheme(text)); | 417 DCHECK(!trim_http || !AutocompleteInput::HasHTTPScheme(text)); |
418 string16 display_string(StringForURLDisplay(destination_url, false, false)); | 418 base::string16 display_string( |
| 419 StringForURLDisplay(destination_url, false, false)); |
419 const size_t offset = trim_http ? TrimHttpPrefix(&display_string) : 0; | 420 const size_t offset = trim_http ? TrimHttpPrefix(&display_string) : 0; |
420 match.fill_into_edit = | 421 match.fill_into_edit = |
421 AutocompleteInput::FormattedStringWithEquivalentMeaning(destination_url, | 422 AutocompleteInput::FormattedStringWithEquivalentMeaning(destination_url, |
422 display_string); | 423 display_string); |
423 match.allowed_to_be_default_match = true; | 424 match.allowed_to_be_default_match = true; |
424 // NOTE: Don't set match.inline_autocompletion to something non-empty here; | 425 // NOTE: Don't set match.inline_autocompletion to something non-empty here; |
425 // it's surprising and annoying. | 426 // it's surprising and annoying. |
426 | 427 |
427 // Try to highlight "innermost" match location. If we fix up "w" into | 428 // Try to highlight "innermost" match location. If we fix up "w" into |
428 // "www.w.com", we want to highlight the fifth character, not the first. | 429 // "www.w.com", we want to highlight the fifth character, not the first. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 // to work with. CullRedirects() will then reduce the list to | 521 // to work with. CullRedirects() will then reduce the list to |
521 // the best kMaxMatches results. | 522 // the best kMaxMatches results. |
522 db->AutocompleteForPrefix( | 523 db->AutocompleteForPrefix( |
523 UTF16ToUTF8(i->prefix + params->input.text()), | 524 UTF16ToUTF8(i->prefix + params->input.text()), |
524 kMaxMatches * 2, | 525 kMaxMatches * 2, |
525 (backend == NULL), | 526 (backend == NULL), |
526 &url_matches); | 527 &url_matches); |
527 for (history::URLRows::const_iterator j(url_matches.begin()); | 528 for (history::URLRows::const_iterator j(url_matches.begin()); |
528 j != url_matches.end(); ++j) { | 529 j != url_matches.end(); ++j) { |
529 const URLPrefix* best_prefix = | 530 const URLPrefix* best_prefix = |
530 URLPrefix::BestURLPrefix(UTF8ToUTF16(j->url().spec()), string16()); | 531 URLPrefix::BestURLPrefix(UTF8ToUTF16(j->url().spec()), |
| 532 base::string16()); |
531 DCHECK(best_prefix != NULL); | 533 DCHECK(best_prefix != NULL); |
532 history_matches.push_back(history::HistoryMatch(*j, i->prefix.length(), | 534 history_matches.push_back(history::HistoryMatch(*j, i->prefix.length(), |
533 i->num_components == 0, | 535 i->num_components == 0, |
534 i->num_components >= best_prefix->num_components)); | 536 i->num_components >= best_prefix->num_components)); |
535 } | 537 } |
536 } | 538 } |
537 } | 539 } |
538 | 540 |
539 // Create sorted list of suggestions. | 541 // Create sorted list of suggestions. |
540 CullPoorMatches(*params, &history_matches); | 542 CullPoorMatches(*params, &history_matches); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 | 820 |
819 // If there are any other matches, then don't promote this match here, in | 821 // 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. | 822 // hopes the caller will be able to inline autocomplete a better suggestion. |
821 // DoAutocomplete() will fall back on this match if inline autocompletion | 823 // 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- | 824 // fails. This matches how we react to never-visited URL inputs in the non- |
823 // intranet case. | 825 // intranet case. |
824 if (type == UNVISITED_INTRANET && !matches->empty()) | 826 if (type == UNVISITED_INTRANET && !matches->empty()) |
825 return false; | 827 return false; |
826 | 828 |
827 // Put it on the front of the HistoryMatches for redirect culling. | 829 // Put it on the front of the HistoryMatches for redirect culling. |
828 CreateOrPromoteMatch(classifier.url_row(), string16::npos, false, matches, | 830 CreateOrPromoteMatch(classifier.url_row(), base::string16::npos, false, |
829 true, true); | 831 matches, true, true); |
830 return true; | 832 return true; |
831 } | 833 } |
832 | 834 |
833 bool HistoryURLProvider::CanFindIntranetURL( | 835 bool HistoryURLProvider::CanFindIntranetURL( |
834 history::URLDatabase* db, | 836 history::URLDatabase* db, |
835 const AutocompleteInput& input) const { | 837 const AutocompleteInput& input) const { |
836 // Normally passing the first two conditions below ought to guarantee the | 838 // Normally passing the first two conditions below ought to guarantee the |
837 // third condition, but because FixupUserInput() can run and modify the | 839 // 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 | 840 // input's text and parts between Parse() and here, it seems better to be |
839 // paranoid and check. | 841 // paranoid and check. |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1053 std::string() : params.languages; | 1055 std::string() : params.languages; |
1054 const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & | 1056 const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & |
1055 ~((params.trim_http && !history_match.match_in_scheme) ? | 1057 ~((params.trim_http && !history_match.match_in_scheme) ? |
1056 0 : net::kFormatUrlOmitHTTP); | 1058 0 : net::kFormatUrlOmitHTTP); |
1057 match.fill_into_edit = | 1059 match.fill_into_edit = |
1058 AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), | 1060 AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), |
1059 net::FormatUrl(info.url(), languages, format_types, | 1061 net::FormatUrl(info.url(), languages, format_types, |
1060 net::UnescapeRule::SPACES, NULL, NULL, | 1062 net::UnescapeRule::SPACES, NULL, NULL, |
1061 &inline_autocomplete_offset)); | 1063 &inline_autocomplete_offset)); |
1062 if (!params.prevent_inline_autocomplete && | 1064 if (!params.prevent_inline_autocomplete && |
1063 (inline_autocomplete_offset != string16::npos)) { | 1065 (inline_autocomplete_offset != base::string16::npos)) { |
1064 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); | 1066 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); |
1065 match.inline_autocompletion = | 1067 match.inline_autocompletion = |
1066 match.fill_into_edit.substr(inline_autocomplete_offset); | 1068 match.fill_into_edit.substr(inline_autocomplete_offset); |
1067 } | 1069 } |
1068 // The latter part of the test effectively asks "is the inline completion | 1070 // 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?). | 1071 // empty?" (i.e., is this match effectively the what-you-typed match?). |
1070 match.allowed_to_be_default_match = !params.prevent_inline_autocomplete || | 1072 match.allowed_to_be_default_match = !params.prevent_inline_autocomplete || |
1071 ((inline_autocomplete_offset != string16::npos) && | 1073 ((inline_autocomplete_offset != base::string16::npos) && |
1072 (inline_autocomplete_offset >= match.fill_into_edit.length())); | 1074 (inline_autocomplete_offset >= match.fill_into_edit.length())); |
1073 | 1075 |
1074 size_t match_start = history_match.input_location; | 1076 size_t match_start = history_match.input_location; |
1075 match.contents = net::FormatUrl(info.url(), languages, | 1077 match.contents = net::FormatUrl(info.url(), languages, |
1076 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); | 1078 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); |
1077 if ((match_start != string16::npos) && | 1079 if ((match_start != base::string16::npos) && |
1078 (inline_autocomplete_offset != string16::npos) && | 1080 (inline_autocomplete_offset != base::string16::npos) && |
1079 (inline_autocomplete_offset != match_start)) { | 1081 (inline_autocomplete_offset != match_start)) { |
1080 DCHECK(inline_autocomplete_offset > match_start); | 1082 DCHECK(inline_autocomplete_offset > match_start); |
1081 AutocompleteMatch::ClassifyLocationInString(match_start, | 1083 AutocompleteMatch::ClassifyLocationInString(match_start, |
1082 inline_autocomplete_offset - match_start, match.contents.length(), | 1084 inline_autocomplete_offset - match_start, match.contents.length(), |
1083 ACMatchClassification::URL, &match.contents_class); | 1085 ACMatchClassification::URL, &match.contents_class); |
1084 } else { | 1086 } else { |
1085 AutocompleteMatch::ClassifyLocationInString(string16::npos, 0, | 1087 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
1086 match.contents.length(), ACMatchClassification::URL, | 1088 match.contents.length(), ACMatchClassification::URL, |
1087 &match.contents_class); | 1089 &match.contents_class); |
1088 } | 1090 } |
1089 match.description = info.title(); | 1091 match.description = info.title(); |
1090 match.description_class = | 1092 match.description_class = |
1091 ClassifyDescription(params.input.text(), match.description); | 1093 ClassifyDescription(params.input.text(), match.description); |
1092 RecordAdditionalInfoFromUrlRow(info, &match); | 1094 RecordAdditionalInfoFromUrlRow(info, &match); |
1093 return match; | 1095 return match; |
1094 } | 1096 } |
1095 | 1097 |
1096 // static | 1098 // static |
1097 ACMatchClassifications HistoryURLProvider::ClassifyDescription( | 1099 ACMatchClassifications HistoryURLProvider::ClassifyDescription( |
1098 const string16& input_text, | 1100 const base::string16& input_text, |
1099 const string16& description) { | 1101 const base::string16& description) { |
1100 string16 clean_description = history::CleanUpTitleForMatching(description); | 1102 base::string16 clean_description = history::CleanUpTitleForMatching(descriptio
n); |
1101 history::TermMatches description_matches(SortAndDeoverlapMatches( | 1103 history::TermMatches description_matches(SortAndDeoverlapMatches( |
1102 history::MatchTermInString(input_text, clean_description, 0))); | 1104 history::MatchTermInString(input_text, clean_description, 0))); |
1103 history::WordStarts description_word_starts; | 1105 history::WordStarts description_word_starts; |
1104 history::String16VectorFromString16( | 1106 history::String16VectorFromString16( |
1105 clean_description, false, &description_word_starts); | 1107 clean_description, false, &description_word_starts); |
1106 description_matches = | 1108 description_matches = |
1107 history::ScoredHistoryMatch::FilterTermMatchesByWordStarts( | 1109 history::ScoredHistoryMatch::FilterTermMatchesByWordStarts( |
1108 description_matches, description_word_starts, 0); | 1110 description_matches, description_word_starts, 0); |
1109 return SpansFromTermMatch( | 1111 return SpansFromTermMatch( |
1110 description_matches, clean_description.length(), false); | 1112 description_matches, clean_description.length(), false); |
1111 } | 1113 } |
OLD | NEW |