1 // Copyright 2014 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.
5 #include "components/omnibox/autocomplete_match.h"
7 #include "base/i18n/time_formatting.h"
8 #include "base/logging.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "components/omnibox/autocomplete_provider.h"
15 #include "components/omnibox/suggestion_answer.h"
16 #include "components/search_engines/template_url.h"
17 #include "components/search_engines/template_url_service.h"
18 #include "grit/components_scaled_resources.h"
22 bool IsTrivialClassification(const ACMatchClassifications
& classifications
) {
23 return classifications
.empty() ||
24 ((classifications
.size() == 1) &&
25 (classifications
.back().style
== ACMatchClassification::NONE
));
30 // AutocompleteMatch ----------------------------------------------------------
33 const base::char16
AutocompleteMatch::kInvalidChars
[] = {
35 0x2028, // Line separator
36 0x2029, // Paragraph separator
40 AutocompleteMatch::AutocompleteMatch()
45 allowed_to_be_default_match(false),
46 transition(ui::PAGE_TRANSITION_GENERATED
),
47 is_history_what_you_typed_match(false),
48 type(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
),
49 from_previous(false) {
52 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider
* provider
,
60 allowed_to_be_default_match(false),
61 transition(ui::PAGE_TRANSITION_TYPED
),
62 is_history_what_you_typed_match(false),
64 from_previous(false) {
67 AutocompleteMatch::AutocompleteMatch(const AutocompleteMatch
& match
)
68 : provider(match
.provider
),
69 relevance(match
.relevance
),
70 typed_count(match
.typed_count
),
71 deletable(match
.deletable
),
72 fill_into_edit(match
.fill_into_edit
),
73 inline_autocompletion(match
.inline_autocompletion
),
74 allowed_to_be_default_match(match
.allowed_to_be_default_match
),
75 destination_url(match
.destination_url
),
76 stripped_destination_url(match
.stripped_destination_url
),
77 contents(match
.contents
),
78 contents_class(match
.contents_class
),
79 description(match
.description
),
80 description_class(match
.description_class
),
81 answer_contents(match
.answer_contents
),
82 answer_type(match
.answer_type
),
83 answer(SuggestionAnswer::copy(match
.answer
.get())),
84 transition(match
.transition
),
85 is_history_what_you_typed_match(match
.is_history_what_you_typed_match
),
87 associated_keyword(match
.associated_keyword
.get() ?
88 new AutocompleteMatch(*match
.associated_keyword
) : NULL
),
89 keyword(match
.keyword
),
90 from_previous(match
.from_previous
),
91 search_terms_args(match
.search_terms_args
.get() ?
92 new TemplateURLRef::SearchTermsArgs(*match
.search_terms_args
) :
94 additional_info(match
.additional_info
),
95 duplicate_matches(match
.duplicate_matches
) {
98 AutocompleteMatch::~AutocompleteMatch() {
101 AutocompleteMatch
& AutocompleteMatch::operator=(
102 const AutocompleteMatch
& match
) {
106 provider
= match
.provider
;
107 relevance
= match
.relevance
;
108 typed_count
= match
.typed_count
;
109 deletable
= match
.deletable
;
110 fill_into_edit
= match
.fill_into_edit
;
111 inline_autocompletion
= match
.inline_autocompletion
;
112 allowed_to_be_default_match
= match
.allowed_to_be_default_match
;
113 destination_url
= match
.destination_url
;
114 stripped_destination_url
= match
.stripped_destination_url
;
115 contents
= match
.contents
;
116 contents_class
= match
.contents_class
;
117 description
= match
.description
;
118 description_class
= match
.description_class
;
119 answer_contents
= match
.answer_contents
;
120 answer_type
= match
.answer_type
;
121 answer
= SuggestionAnswer::copy(match
.answer
.get());
122 transition
= match
.transition
;
123 is_history_what_you_typed_match
= match
.is_history_what_you_typed_match
;
125 associated_keyword
.reset(match
.associated_keyword
.get() ?
126 new AutocompleteMatch(*match
.associated_keyword
) : NULL
);
127 keyword
= match
.keyword
;
128 from_previous
= match
.from_previous
;
129 search_terms_args
.reset(match
.search_terms_args
.get() ?
130 new TemplateURLRef::SearchTermsArgs(*match
.search_terms_args
) : NULL
);
131 additional_info
= match
.additional_info
;
132 duplicate_matches
= match
.duplicate_matches
;
137 int AutocompleteMatch::TypeToIcon(Type type
) {
139 static const int kIcons
[] = {
140 IDR_OMNIBOX_HTTP
, // URL_WHAT_YOU_TYPE
141 IDR_OMNIBOX_HTTP
, // HISTORY_URL
142 IDR_OMNIBOX_HTTP
, // HISTORY_TITLE
143 IDR_OMNIBOX_HTTP
, // HISTORY_BODY
144 IDR_OMNIBOX_HTTP
, // HISTORY_KEYWORD
145 IDR_OMNIBOX_HTTP
, // NAVSUGGEST
146 IDR_OMNIBOX_SEARCH
, // SEARCH_WHAT_YOU_TYPED
147 IDR_OMNIBOX_SEARCH
, // SEARCH_HISTORY
148 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST
149 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_ENTITY
150 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_INFINITE
151 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_PERSONALIZED
152 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_PROFILE
153 IDR_OMNIBOX_SEARCH
, // SEARCH_OTHER_ENGINE
154 IDR_OMNIBOX_EXTENSION_APP
, // EXTENSION_APP
155 IDR_OMNIBOX_SEARCH
, // CONTACT_DEPRECATED
156 IDR_OMNIBOX_HTTP
, // BOOKMARK_TITLE
157 IDR_OMNIBOX_HTTP
, // NAVSUGGEST_PERSONALIZED
158 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_ANSWER
161 static const int kIcons
[] = {
162 IDR_OMNIBOX_HTTP
, // URL_WHAT_YOU_TYPE
163 IDR_OMNIBOX_HISTORY
, // HISTORY_URL
164 IDR_OMNIBOX_HISTORY
, // HISTORY_TITLE
165 IDR_OMNIBOX_HISTORY
, // HISTORY_BODY
166 IDR_OMNIBOX_HISTORY
, // HISTORY_KEYWORD
167 IDR_OMNIBOX_HTTP
, // NAVSUGGEST
168 IDR_OMNIBOX_SEARCH
, // SEARCH_WHAT_YOU_TYPED
169 IDR_OMNIBOX_HISTORY
, // SEARCH_HISTORY
170 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST
171 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_ENTITY
172 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_INFINITE
173 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_PERSONALIZED
174 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_PROFILE
175 IDR_OMNIBOX_SEARCH
, // SEARCH_OTHER_ENGINE
176 IDR_OMNIBOX_EXTENSION_APP
, // EXTENSION_APP
177 IDR_OMNIBOX_SEARCH
, // CONTACT_DEPRECATED
178 IDR_OMNIBOX_HTTP
, // BOOKMARK_TITLE
179 IDR_OMNIBOX_HTTP
, // NAVSUGGEST_PERSONALIZED
180 IDR_OMNIBOX_SEARCH
, // SEARCH_SUGGEST_ANSWER
183 static_assert(arraysize(kIcons
) == AutocompleteMatchType::NUM_TYPES
,
184 "icons array must have NUM_TYPES elements");
189 bool AutocompleteMatch::MoreRelevant(const AutocompleteMatch
& elem1
,
190 const AutocompleteMatch
& elem2
) {
191 // For equal-relevance matches, we sort alphabetically, so that providers
192 // who return multiple elements at the same priority get a "stable" sort
193 // across multiple updates.
194 return (elem1
.relevance
== elem2
.relevance
) ?
195 (elem1
.contents
< elem2
.contents
) : (elem1
.relevance
> elem2
.relevance
);
199 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch
& elem1
,
200 const AutocompleteMatch
& elem2
) {
201 if (elem1
.stripped_destination_url
.is_empty() &&
202 elem2
.stripped_destination_url
.is_empty())
204 return elem1
.stripped_destination_url
== elem2
.stripped_destination_url
;
208 void AutocompleteMatch::ClassifyMatchInString(
209 const base::string16
& find_text
,
210 const base::string16
& text
,
212 ACMatchClassifications
* classification
) {
213 ClassifyLocationInString(text
.find(find_text
), find_text
.length(),
214 text
.length(), style
, classification
);
218 void AutocompleteMatch::ClassifyLocationInString(
219 size_t match_location
,
221 size_t overall_length
,
223 ACMatchClassifications
* classification
) {
224 classification
->clear();
226 // Don't classify anything about an empty string
227 // (AutocompleteMatch::Validate() checks this).
228 if (overall_length
== 0)
231 // Mark pre-match portion of string (if any).
232 if (match_location
!= 0) {
233 classification
->push_back(ACMatchClassification(0, style
));
236 // Mark matching portion of string.
237 if (match_location
== base::string16::npos
) {
238 // No match, above classification will suffice for whole string.
241 // Classifying an empty match makes no sense and will lead to validation
243 DCHECK_GT(match_length
, 0U);
244 classification
->push_back(ACMatchClassification(match_location
,
245 (style
| ACMatchClassification::MATCH
) & ~ACMatchClassification::DIM
));
247 // Mark post-match portion of string (if any).
248 const size_t after_match(match_location
+ match_length
);
249 if (after_match
< overall_length
) {
250 classification
->push_back(ACMatchClassification(after_match
, style
));
255 AutocompleteMatch::ACMatchClassifications
256 AutocompleteMatch::MergeClassifications(
257 const ACMatchClassifications
& classifications1
,
258 const ACMatchClassifications
& classifications2
) {
259 // We must return the empty vector only if both inputs are truly empty.
260 // The result of merging an empty vector with a single (0, NONE)
261 // classification is the latter one-entry vector.
262 if (IsTrivialClassification(classifications1
))
263 return classifications2
.empty() ? classifications1
: classifications2
;
264 if (IsTrivialClassification(classifications2
))
265 return classifications1
;
267 ACMatchClassifications output
;
268 for (ACMatchClassifications::const_iterator i
= classifications1
.begin(),
269 j
= classifications2
.begin(); i
!= classifications1
.end();) {
270 AutocompleteMatch::AddLastClassificationIfNecessary(&output
,
271 std::max(i
->offset
, j
->offset
), i
->style
| j
->style
);
272 const size_t next_i_offset
= (i
+ 1) == classifications1
.end() ?
273 static_cast<size_t>(-1) : (i
+ 1)->offset
;
274 const size_t next_j_offset
= (j
+ 1) == classifications2
.end() ?
275 static_cast<size_t>(-1) : (j
+ 1)->offset
;
276 if (next_i_offset
>= next_j_offset
)
278 if (next_j_offset
>= next_i_offset
)
286 std::string
AutocompleteMatch::ClassificationsToString(
287 const ACMatchClassifications
& classifications
) {
288 std::string serialized_classifications
;
289 for (size_t i
= 0; i
< classifications
.size(); ++i
) {
291 serialized_classifications
+= ',';
292 serialized_classifications
+= base::IntToString(classifications
[i
].offset
) +
293 ',' + base::IntToString(classifications
[i
].style
);
295 return serialized_classifications
;
299 ACMatchClassifications
AutocompleteMatch::ClassificationsFromString(
300 const std::string
& serialized_classifications
) {
301 ACMatchClassifications classifications
;
302 std::vector
<std::string
> tokens
;
303 Tokenize(serialized_classifications
, ",", &tokens
);
304 DCHECK(!(tokens
.size() & 1)); // The number of tokens should be even.
305 for (size_t i
= 0; i
< tokens
.size(); i
+= 2) {
306 int classification_offset
= 0;
307 int classification_style
= ACMatchClassification::NONE
;
308 if (!base::StringToInt(tokens
[i
], &classification_offset
) ||
309 !base::StringToInt(tokens
[i
+ 1], &classification_style
)) {
311 return classifications
;
313 classifications
.push_back(ACMatchClassification(classification_offset
,
314 classification_style
));
316 return classifications
;
320 void AutocompleteMatch::AddLastClassificationIfNecessary(
321 ACMatchClassifications
* classifications
,
324 DCHECK(classifications
);
325 if (classifications
->empty() || classifications
->back().style
!= style
) {
326 DCHECK(classifications
->empty() ||
327 (offset
> classifications
->back().offset
));
328 classifications
->push_back(ACMatchClassification(offset
, style
));
333 base::string16
AutocompleteMatch::SanitizeString(const base::string16
& text
) {
334 // NOTE: This logic is mirrored by |sanitizeString()| in
335 // omnibox_custom_bindings.js.
336 base::string16 result
;
337 base::TrimWhitespace(text
, base::TRIM_LEADING
, &result
);
338 base::RemoveChars(result
, kInvalidChars
, &result
);
343 bool AutocompleteMatch::IsSearchType(Type type
) {
344 return type
== AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
||
345 type
== AutocompleteMatchType::SEARCH_HISTORY
||
346 type
== AutocompleteMatchType::SEARCH_SUGGEST
||
347 type
== AutocompleteMatchType::SEARCH_OTHER_ENGINE
||
348 IsSpecializedSearchType(type
);
352 bool AutocompleteMatch::IsSpecializedSearchType(Type type
) {
353 return type
== AutocompleteMatchType::SEARCH_SUGGEST_ENTITY
||
354 type
== AutocompleteMatchType::SEARCH_SUGGEST_INFINITE
||
355 type
== AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED
||
356 type
== AutocompleteMatchType::SEARCH_SUGGEST_PROFILE
||
357 type
== AutocompleteMatchType::SEARCH_SUGGEST_ANSWER
;
361 TemplateURL
* AutocompleteMatch::GetTemplateURLWithKeyword(
362 TemplateURLService
* template_url_service
,
363 const base::string16
& keyword
,
364 const std::string
& host
) {
365 if (template_url_service
== NULL
)
367 TemplateURL
* template_url
= keyword
.empty() ?
368 NULL
: template_url_service
->GetTemplateURLForKeyword(keyword
);
369 return (template_url
|| host
.empty()) ?
370 template_url
: template_url_service
->GetTemplateURLForHost(host
);
374 GURL
AutocompleteMatch::GURLToStrippedGURL(
376 TemplateURLService
* template_url_service
,
377 const base::string16
& keyword
) {
381 GURL stripped_destination_url
= url
;
383 // If the destination URL looks like it was generated from a TemplateURL,
384 // remove all substitutions other than the search terms. This allows us
385 // to eliminate cases like past search URLs from history that differ only
386 // by some obscure query param from each other or from the search/keyword
388 TemplateURL
* template_url
= GetTemplateURLWithKeyword(
389 template_url_service
, keyword
, stripped_destination_url
.host());
390 if (template_url
!= NULL
&&
391 template_url
->SupportsReplacement(
392 template_url_service
->search_terms_data())) {
393 base::string16 search_terms
;
394 if (template_url
->ExtractSearchTermsFromURL(
395 stripped_destination_url
,
396 template_url_service
->search_terms_data(),
398 stripped_destination_url
=
399 GURL(template_url
->url_ref().ReplaceSearchTerms(
400 TemplateURLRef::SearchTermsArgs(search_terms
),
401 template_url_service
->search_terms_data()));
405 // |replacements| keeps all the substitions we're going to make to
406 // from {destination_url} to {stripped_destination_url}. |need_replacement|
407 // is a helper variable that helps us keep track of whether we need
408 // to apply the replacement.
409 bool needs_replacement
= false;
410 GURL::Replacements replacements
;
412 // Remove the www. prefix from the host.
413 static const char prefix
[] = "www.";
414 static const size_t prefix_len
= arraysize(prefix
) - 1;
415 std::string host
= stripped_destination_url
.host();
416 if (host
.compare(0, prefix_len
, prefix
) == 0) {
417 host
= host
.substr(prefix_len
);
418 replacements
.SetHostStr(host
);
419 needs_replacement
= true;
422 // Replace https protocol with http protocol.
423 if (stripped_destination_url
.SchemeIs(url::kHttpsScheme
)) {
424 replacements
.SetScheme(url::kHttpScheme
,
425 url::Component(0, strlen(url::kHttpScheme
)));
426 needs_replacement
= true;
429 if (needs_replacement
)
430 stripped_destination_url
= stripped_destination_url
.ReplaceComponents(
432 return stripped_destination_url
;
435 void AutocompleteMatch::ComputeStrippedDestinationURL(
436 TemplateURLService
* template_url_service
) {
437 stripped_destination_url
=
438 GURLToStrippedGURL(destination_url
, template_url_service
, keyword
);
441 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault(
442 const GURL
& canonical_input_url
,
443 TemplateURLService
* template_url_service
) {
444 if (!allowed_to_be_default_match
) {
445 const GURL
& stripped_canonical_input_url
=
446 AutocompleteMatch::GURLToStrippedGURL(
447 canonical_input_url
, template_url_service
, base::string16());
448 ComputeStrippedDestinationURL(template_url_service
);
449 allowed_to_be_default_match
=
450 stripped_canonical_input_url
== stripped_destination_url
;
454 void AutocompleteMatch::GetKeywordUIState(
455 TemplateURLService
* template_url_service
,
456 base::string16
* keyword
,
457 bool* is_keyword_hint
) const {
458 *is_keyword_hint
= associated_keyword
.get() != NULL
;
459 keyword
->assign(*is_keyword_hint
? associated_keyword
->keyword
:
460 GetSubstitutingExplicitlyInvokedKeyword(template_url_service
));
463 base::string16
AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword(
464 TemplateURLService
* template_url_service
) const {
465 if (transition
!= ui::PAGE_TRANSITION_KEYWORD
||
466 template_url_service
== NULL
) {
467 return base::string16();
470 const TemplateURL
* t_url
= GetTemplateURL(template_url_service
, false);
472 t_url
->SupportsReplacement(
473 template_url_service
->search_terms_data())) ?
474 keyword
: base::string16();
477 TemplateURL
* AutocompleteMatch::GetTemplateURL(
478 TemplateURLService
* template_url_service
,
479 bool allow_fallback_to_destination_host
) const {
480 return GetTemplateURLWithKeyword(
481 template_url_service
, keyword
,
482 allow_fallback_to_destination_host
?
483 destination_url
.host() : std::string());
486 void AutocompleteMatch::RecordAdditionalInfo(const std::string
& property
,
487 const std::string
& value
) {
488 DCHECK(!property
.empty());
489 DCHECK(!value
.empty());
490 additional_info
[property
] = value
;
493 void AutocompleteMatch::RecordAdditionalInfo(const std::string
& property
,
495 RecordAdditionalInfo(property
, base::IntToString(value
));
498 void AutocompleteMatch::RecordAdditionalInfo(const std::string
& property
,
499 const base::Time
& value
) {
500 RecordAdditionalInfo(property
,
502 base::TimeFormatShortDateAndTime(value
)));
505 std::string
AutocompleteMatch::GetAdditionalInfo(
506 const std::string
& property
) const {
507 AdditionalInfo::const_iterator
i(additional_info
.find(property
));
508 return (i
== additional_info
.end()) ? std::string() : i
->second
;
511 bool AutocompleteMatch::IsVerbatimType() const {
512 const bool is_keyword_verbatim_match
=
513 (type
== AutocompleteMatchType::SEARCH_OTHER_ENGINE
&&
515 provider
->type() == AutocompleteProvider::TYPE_SEARCH
);
516 return type
== AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
||
517 type
== AutocompleteMatchType::URL_WHAT_YOU_TYPED
||
518 is_keyword_verbatim_match
;
521 bool AutocompleteMatch::SupportsDeletion() const {
525 for (ACMatches::const_iterator
it(duplicate_matches
.begin());
526 it
!= duplicate_matches
.end(); ++it
) {
534 void AutocompleteMatch::Validate() const {
535 ValidateClassifications(contents
, contents_class
);
536 ValidateClassifications(description
, description_class
);
539 void AutocompleteMatch::ValidateClassifications(
540 const base::string16
& text
,
541 const ACMatchClassifications
& classifications
) const {
543 DCHECK(classifications
.empty());
547 // The classifications should always cover the whole string.
548 DCHECK(!classifications
.empty()) << "No classification for \"" << text
<< '"';
549 DCHECK_EQ(0U, classifications
[0].offset
)
550 << "Classification misses beginning for \"" << text
<< '"';
551 if (classifications
.size() == 1)
554 // The classifications should always be sorted.
555 size_t last_offset
= classifications
[0].offset
;
556 for (ACMatchClassifications::const_iterator
i(classifications
.begin() + 1);
557 i
!= classifications
.end(); ++i
) {
558 const char* provider_name
= provider
? provider
->GetName() : "None";
559 DCHECK_GT(i
->offset
, last_offset
)
560 << " Classification for \"" << text
<< "\" with offset of " << i
->offset
561 << " is unsorted in relation to last offset of " << last_offset
562 << ". Provider: " << provider_name
<< ".";
563 DCHECK_LT(i
->offset
, text
.length())
564 << " Classification of [" << i
->offset
<< "," << text
.length()
565 << "] is out of bounds for \"" << text
<< "\". Provider: "
566 << provider_name
<< ".";
567 last_offset
= i
->offset
;