Fix +x on Windows-only binary after https://chromium.googlesource.com/chromium/src...
[chromium-blink-merge.git] / components / omnibox / autocomplete_match.cc
blobf2255f67e99025f95e2896664480eaf3acb23936
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_piece.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "components/omnibox/autocomplete_provider.h"
16 #include "components/omnibox/suggestion_answer.h"
17 #include "components/search_engines/template_url.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "grit/components_scaled_resources.h"
21 namespace {
23 bool IsTrivialClassification(const ACMatchClassifications& classifications) {
24 return classifications.empty() ||
25 ((classifications.size() == 1) &&
26 (classifications.back().style == ACMatchClassification::NONE));
29 } // namespace
31 // AutocompleteMatch ----------------------------------------------------------
33 // static
34 const base::char16 AutocompleteMatch::kInvalidChars[] = {
35 '\n', '\r', '\t',
36 0x2028, // Line separator
37 0x2029, // Paragraph separator
41 AutocompleteMatch::AutocompleteMatch()
42 : provider(NULL),
43 relevance(0),
44 typed_count(-1),
45 deletable(false),
46 allowed_to_be_default_match(false),
47 transition(ui::PAGE_TRANSITION_GENERATED),
48 is_history_what_you_typed_match(false),
49 type(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED),
50 from_previous(false) {
53 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider* provider,
54 int relevance,
55 bool deletable,
56 Type type)
57 : provider(provider),
58 relevance(relevance),
59 typed_count(-1),
60 deletable(deletable),
61 allowed_to_be_default_match(false),
62 transition(ui::PAGE_TRANSITION_TYPED),
63 is_history_what_you_typed_match(false),
64 type(type),
65 from_previous(false) {
68 AutocompleteMatch::AutocompleteMatch(const AutocompleteMatch& match)
69 : provider(match.provider),
70 relevance(match.relevance),
71 typed_count(match.typed_count),
72 deletable(match.deletable),
73 fill_into_edit(match.fill_into_edit),
74 inline_autocompletion(match.inline_autocompletion),
75 allowed_to_be_default_match(match.allowed_to_be_default_match),
76 destination_url(match.destination_url),
77 stripped_destination_url(match.stripped_destination_url),
78 contents(match.contents),
79 contents_class(match.contents_class),
80 description(match.description),
81 description_class(match.description_class),
82 answer_contents(match.answer_contents),
83 answer_type(match.answer_type),
84 answer(SuggestionAnswer::copy(match.answer.get())),
85 transition(match.transition),
86 is_history_what_you_typed_match(match.is_history_what_you_typed_match),
87 type(match.type),
88 associated_keyword(match.associated_keyword.get() ?
89 new AutocompleteMatch(*match.associated_keyword) : NULL),
90 keyword(match.keyword),
91 from_previous(match.from_previous),
92 search_terms_args(match.search_terms_args.get() ?
93 new TemplateURLRef::SearchTermsArgs(*match.search_terms_args) :
94 NULL),
95 additional_info(match.additional_info),
96 duplicate_matches(match.duplicate_matches) {
99 AutocompleteMatch::~AutocompleteMatch() {
102 AutocompleteMatch& AutocompleteMatch::operator=(
103 const AutocompleteMatch& match) {
104 if (this == &match)
105 return *this;
107 provider = match.provider;
108 relevance = match.relevance;
109 typed_count = match.typed_count;
110 deletable = match.deletable;
111 fill_into_edit = match.fill_into_edit;
112 inline_autocompletion = match.inline_autocompletion;
113 allowed_to_be_default_match = match.allowed_to_be_default_match;
114 destination_url = match.destination_url;
115 stripped_destination_url = match.stripped_destination_url;
116 contents = match.contents;
117 contents_class = match.contents_class;
118 description = match.description;
119 description_class = match.description_class;
120 answer_contents = match.answer_contents;
121 answer_type = match.answer_type;
122 answer = SuggestionAnswer::copy(match.answer.get());
123 transition = match.transition;
124 is_history_what_you_typed_match = match.is_history_what_you_typed_match;
125 type = match.type;
126 associated_keyword.reset(match.associated_keyword.get() ?
127 new AutocompleteMatch(*match.associated_keyword) : NULL);
128 keyword = match.keyword;
129 from_previous = match.from_previous;
130 search_terms_args.reset(match.search_terms_args.get() ?
131 new TemplateURLRef::SearchTermsArgs(*match.search_terms_args) : NULL);
132 additional_info = match.additional_info;
133 duplicate_matches = match.duplicate_matches;
134 return *this;
137 // static
138 int AutocompleteMatch::TypeToIcon(Type type) {
139 #if !defined(OS_IOS)
140 static const int kIcons[] = {
141 IDR_OMNIBOX_HTTP, // URL_WHAT_YOU_TYPE
142 IDR_OMNIBOX_HTTP, // HISTORY_URL
143 IDR_OMNIBOX_HTTP, // HISTORY_TITLE
144 IDR_OMNIBOX_HTTP, // HISTORY_BODY
145 IDR_OMNIBOX_HTTP, // HISTORY_KEYWORD
146 IDR_OMNIBOX_HTTP, // NAVSUGGEST
147 IDR_OMNIBOX_SEARCH, // SEARCH_WHAT_YOU_TYPED
148 IDR_OMNIBOX_SEARCH, // SEARCH_HISTORY
149 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST
150 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_ENTITY
151 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_TAIL
152 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PERSONALIZED
153 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PROFILE
154 IDR_OMNIBOX_SEARCH, // SEARCH_OTHER_ENGINE
155 IDR_OMNIBOX_EXTENSION_APP, // EXTENSION_APP
156 IDR_OMNIBOX_SEARCH, // CONTACT_DEPRECATED
157 IDR_OMNIBOX_HTTP, // BOOKMARK_TITLE
158 IDR_OMNIBOX_HTTP, // NAVSUGGEST_PERSONALIZED
160 #else
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_TAIL
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
181 #endif
182 static_assert(arraysize(kIcons) == AutocompleteMatchType::NUM_TYPES,
183 "icons array must have NUM_TYPES elements");
184 return kIcons[type];
187 // static
188 bool AutocompleteMatch::MoreRelevant(const AutocompleteMatch& elem1,
189 const AutocompleteMatch& elem2) {
190 // For equal-relevance matches, we sort alphabetically, so that providers
191 // who return multiple elements at the same priority get a "stable" sort
192 // across multiple updates.
193 return (elem1.relevance == elem2.relevance) ?
194 (elem1.contents < elem2.contents) : (elem1.relevance > elem2.relevance);
197 // static
198 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1,
199 const AutocompleteMatch& elem2) {
200 if (elem1.stripped_destination_url.is_empty() &&
201 elem2.stripped_destination_url.is_empty())
202 return false;
203 return elem1.stripped_destination_url == elem2.stripped_destination_url;
206 // static
207 void AutocompleteMatch::ClassifyMatchInString(
208 const base::string16& find_text,
209 const base::string16& text,
210 int style,
211 ACMatchClassifications* classification) {
212 ClassifyLocationInString(text.find(find_text), find_text.length(),
213 text.length(), style, classification);
216 // static
217 void AutocompleteMatch::ClassifyLocationInString(
218 size_t match_location,
219 size_t match_length,
220 size_t overall_length,
221 int style,
222 ACMatchClassifications* classification) {
223 classification->clear();
225 // Don't classify anything about an empty string
226 // (AutocompleteMatch::Validate() checks this).
227 if (overall_length == 0)
228 return;
230 // Mark pre-match portion of string (if any).
231 if (match_location != 0) {
232 classification->push_back(ACMatchClassification(0, style));
235 // Mark matching portion of string.
236 if (match_location == base::string16::npos) {
237 // No match, above classification will suffice for whole string.
238 return;
240 // Classifying an empty match makes no sense and will lead to validation
241 // errors later.
242 DCHECK_GT(match_length, 0U);
243 classification->push_back(ACMatchClassification(match_location,
244 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM));
246 // Mark post-match portion of string (if any).
247 const size_t after_match(match_location + match_length);
248 if (after_match < overall_length) {
249 classification->push_back(ACMatchClassification(after_match, style));
253 // static
254 AutocompleteMatch::ACMatchClassifications
255 AutocompleteMatch::MergeClassifications(
256 const ACMatchClassifications& classifications1,
257 const ACMatchClassifications& classifications2) {
258 // We must return the empty vector only if both inputs are truly empty.
259 // The result of merging an empty vector with a single (0, NONE)
260 // classification is the latter one-entry vector.
261 if (IsTrivialClassification(classifications1))
262 return classifications2.empty() ? classifications1 : classifications2;
263 if (IsTrivialClassification(classifications2))
264 return classifications1;
266 ACMatchClassifications output;
267 for (ACMatchClassifications::const_iterator i = classifications1.begin(),
268 j = classifications2.begin(); i != classifications1.end();) {
269 AutocompleteMatch::AddLastClassificationIfNecessary(&output,
270 std::max(i->offset, j->offset), i->style | j->style);
271 const size_t next_i_offset = (i + 1) == classifications1.end() ?
272 static_cast<size_t>(-1) : (i + 1)->offset;
273 const size_t next_j_offset = (j + 1) == classifications2.end() ?
274 static_cast<size_t>(-1) : (j + 1)->offset;
275 if (next_i_offset >= next_j_offset)
276 ++j;
277 if (next_j_offset >= next_i_offset)
278 ++i;
281 return output;
284 // static
285 std::string AutocompleteMatch::ClassificationsToString(
286 const ACMatchClassifications& classifications) {
287 std::string serialized_classifications;
288 for (size_t i = 0; i < classifications.size(); ++i) {
289 if (i)
290 serialized_classifications += ',';
291 serialized_classifications += base::IntToString(classifications[i].offset) +
292 ',' + base::IntToString(classifications[i].style);
294 return serialized_classifications;
297 // static
298 ACMatchClassifications AutocompleteMatch::ClassificationsFromString(
299 const std::string& serialized_classifications) {
300 ACMatchClassifications classifications;
301 std::vector<std::string> tokens;
302 Tokenize(serialized_classifications, ",", &tokens);
303 DCHECK(!(tokens.size() & 1)); // The number of tokens should be even.
304 for (size_t i = 0; i < tokens.size(); i += 2) {
305 int classification_offset = 0;
306 int classification_style = ACMatchClassification::NONE;
307 if (!base::StringToInt(tokens[i], &classification_offset) ||
308 !base::StringToInt(tokens[i + 1], &classification_style)) {
309 NOTREACHED();
310 return classifications;
312 classifications.push_back(ACMatchClassification(classification_offset,
313 classification_style));
315 return classifications;
318 // static
319 void AutocompleteMatch::AddLastClassificationIfNecessary(
320 ACMatchClassifications* classifications,
321 size_t offset,
322 int style) {
323 DCHECK(classifications);
324 if (classifications->empty() || classifications->back().style != style) {
325 DCHECK(classifications->empty() ||
326 (offset > classifications->back().offset));
327 classifications->push_back(ACMatchClassification(offset, style));
331 // static
332 base::string16 AutocompleteMatch::SanitizeString(const base::string16& text) {
333 // NOTE: This logic is mirrored by |sanitizeString()| in
334 // omnibox_custom_bindings.js.
335 base::string16 result;
336 base::TrimWhitespace(text, base::TRIM_LEADING, &result);
337 base::RemoveChars(result, kInvalidChars, &result);
338 return result;
341 // static
342 bool AutocompleteMatch::IsSearchType(Type type) {
343 return type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
344 type == AutocompleteMatchType::SEARCH_HISTORY ||
345 type == AutocompleteMatchType::SEARCH_SUGGEST ||
346 type == AutocompleteMatchType::SEARCH_OTHER_ENGINE ||
347 IsSpecializedSearchType(type);
350 // static
351 bool AutocompleteMatch::IsSpecializedSearchType(Type type) {
352 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY ||
353 type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL ||
354 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED ||
355 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE;
358 // static
359 TemplateURL* AutocompleteMatch::GetTemplateURLWithKeyword(
360 TemplateURLService* template_url_service,
361 const base::string16& keyword,
362 const std::string& host) {
363 if (template_url_service == NULL)
364 return NULL;
365 TemplateURL* template_url = keyword.empty() ?
366 NULL : template_url_service->GetTemplateURLForKeyword(keyword);
367 return (template_url || host.empty()) ?
368 template_url : template_url_service->GetTemplateURLForHost(host);
371 // static
372 GURL AutocompleteMatch::GURLToStrippedGURL(
373 const GURL& url,
374 TemplateURLService* template_url_service,
375 const base::string16& keyword) {
376 if (!url.is_valid())
377 return url;
379 GURL stripped_destination_url = url;
381 // If the destination URL looks like it was generated from a TemplateURL,
382 // remove all substitutions other than the search terms. This allows us
383 // to eliminate cases like past search URLs from history that differ only
384 // by some obscure query param from each other or from the search/keyword
385 // provider matches.
386 TemplateURL* template_url = GetTemplateURLWithKeyword(
387 template_url_service, keyword, stripped_destination_url.host());
388 if (template_url != NULL &&
389 template_url->SupportsReplacement(
390 template_url_service->search_terms_data())) {
391 base::string16 search_terms;
392 if (template_url->ExtractSearchTermsFromURL(
393 stripped_destination_url,
394 template_url_service->search_terms_data(),
395 &search_terms)) {
396 stripped_destination_url =
397 GURL(template_url->url_ref().ReplaceSearchTerms(
398 TemplateURLRef::SearchTermsArgs(search_terms),
399 template_url_service->search_terms_data()));
403 // |replacements| keeps all the substitions we're going to make to
404 // from {destination_url} to {stripped_destination_url}. |need_replacement|
405 // is a helper variable that helps us keep track of whether we need
406 // to apply the replacement.
407 bool needs_replacement = false;
408 GURL::Replacements replacements;
410 // Remove the www. prefix from the host.
411 static const char prefix[] = "www.";
412 static const size_t prefix_len = arraysize(prefix) - 1;
413 std::string host = stripped_destination_url.host();
414 if (host.compare(0, prefix_len, prefix) == 0) {
415 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len));
416 needs_replacement = true;
419 // Replace https protocol with http protocol.
420 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) {
421 replacements.SetScheme(url::kHttpScheme,
422 url::Component(0, strlen(url::kHttpScheme)));
423 needs_replacement = true;
426 if (needs_replacement)
427 stripped_destination_url = stripped_destination_url.ReplaceComponents(
428 replacements);
429 return stripped_destination_url;
432 void AutocompleteMatch::ComputeStrippedDestinationURL(
433 TemplateURLService* template_url_service) {
434 stripped_destination_url =
435 GURLToStrippedGURL(destination_url, template_url_service, keyword);
438 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault(
439 const GURL& canonical_input_url,
440 TemplateURLService* template_url_service) {
441 if (!allowed_to_be_default_match) {
442 const GURL& stripped_canonical_input_url =
443 AutocompleteMatch::GURLToStrippedGURL(
444 canonical_input_url, template_url_service, base::string16());
445 ComputeStrippedDestinationURL(template_url_service);
446 allowed_to_be_default_match =
447 stripped_canonical_input_url == stripped_destination_url;
451 void AutocompleteMatch::GetKeywordUIState(
452 TemplateURLService* template_url_service,
453 base::string16* keyword,
454 bool* is_keyword_hint) const {
455 *is_keyword_hint = associated_keyword.get() != NULL;
456 keyword->assign(*is_keyword_hint ? associated_keyword->keyword :
457 GetSubstitutingExplicitlyInvokedKeyword(template_url_service));
460 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword(
461 TemplateURLService* template_url_service) const {
462 if (transition != ui::PAGE_TRANSITION_KEYWORD ||
463 template_url_service == NULL) {
464 return base::string16();
467 const TemplateURL* t_url = GetTemplateURL(template_url_service, false);
468 return (t_url &&
469 t_url->SupportsReplacement(
470 template_url_service->search_terms_data())) ?
471 keyword : base::string16();
474 TemplateURL* AutocompleteMatch::GetTemplateURL(
475 TemplateURLService* template_url_service,
476 bool allow_fallback_to_destination_host) const {
477 return GetTemplateURLWithKeyword(
478 template_url_service, keyword,
479 allow_fallback_to_destination_host ?
480 destination_url.host() : std::string());
483 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
484 const std::string& value) {
485 DCHECK(!property.empty());
486 DCHECK(!value.empty());
487 additional_info[property] = value;
490 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
491 int value) {
492 RecordAdditionalInfo(property, base::IntToString(value));
495 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
496 const base::Time& value) {
497 RecordAdditionalInfo(property,
498 base::UTF16ToUTF8(
499 base::TimeFormatShortDateAndTime(value)));
502 std::string AutocompleteMatch::GetAdditionalInfo(
503 const std::string& property) const {
504 AdditionalInfo::const_iterator i(additional_info.find(property));
505 return (i == additional_info.end()) ? std::string() : i->second;
508 bool AutocompleteMatch::IsVerbatimType() const {
509 const bool is_keyword_verbatim_match =
510 (type == AutocompleteMatchType::SEARCH_OTHER_ENGINE &&
511 provider != NULL &&
512 provider->type() == AutocompleteProvider::TYPE_SEARCH);
513 return type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
514 type == AutocompleteMatchType::URL_WHAT_YOU_TYPED ||
515 is_keyword_verbatim_match;
518 bool AutocompleteMatch::SupportsDeletion() const {
519 if (deletable)
520 return true;
522 for (ACMatches::const_iterator it(duplicate_matches.begin());
523 it != duplicate_matches.end(); ++it) {
524 if (it->deletable)
525 return true;
527 return false;
530 #ifndef NDEBUG
531 void AutocompleteMatch::Validate() const {
532 ValidateClassifications(contents, contents_class);
533 ValidateClassifications(description, description_class);
536 void AutocompleteMatch::ValidateClassifications(
537 const base::string16& text,
538 const ACMatchClassifications& classifications) const {
539 if (text.empty()) {
540 DCHECK(classifications.empty());
541 return;
544 // The classifications should always cover the whole string.
545 DCHECK(!classifications.empty()) << "No classification for \"" << text << '"';
546 DCHECK_EQ(0U, classifications[0].offset)
547 << "Classification misses beginning for \"" << text << '"';
548 if (classifications.size() == 1)
549 return;
551 // The classifications should always be sorted.
552 size_t last_offset = classifications[0].offset;
553 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1);
554 i != classifications.end(); ++i) {
555 const char* provider_name = provider ? provider->GetName() : "None";
556 DCHECK_GT(i->offset, last_offset)
557 << " Classification for \"" << text << "\" with offset of " << i->offset
558 << " is unsorted in relation to last offset of " << last_offset
559 << ". Provider: " << provider_name << ".";
560 DCHECK_LT(i->offset, text.length())
561 << " Classification of [" << i->offset << "," << text.length()
562 << "] is out of bounds for \"" << text << "\". Provider: "
563 << provider_name << ".";
564 last_offset = i->offset;
567 #endif