Update mojo sdk to rev 8563c3d4162bd74e96783e823e076e99869d7385
[chromium-blink-merge.git] / components / omnibox / autocomplete_match.cc
blobd0b4c71e0f4f4ab2ac7737a3dd0f829dbe7f36d2
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
159 IDR_OMNIBOX_CALCULATOR, // CALCULATOR
161 #else
162 static const int kIcons[] = {
163 IDR_OMNIBOX_HTTP, // URL_WHAT_YOU_TYPE
164 IDR_OMNIBOX_HISTORY, // HISTORY_URL
165 IDR_OMNIBOX_HISTORY, // HISTORY_TITLE
166 IDR_OMNIBOX_HISTORY, // HISTORY_BODY
167 IDR_OMNIBOX_HISTORY, // HISTORY_KEYWORD
168 IDR_OMNIBOX_HTTP, // NAVSUGGEST
169 IDR_OMNIBOX_SEARCH, // SEARCH_WHAT_YOU_TYPED
170 IDR_OMNIBOX_HISTORY, // SEARCH_HISTORY
171 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST
172 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_ENTITY
173 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_TAIL
174 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PERSONALIZED
175 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PROFILE
176 IDR_OMNIBOX_SEARCH, // SEARCH_OTHER_ENGINE
177 IDR_OMNIBOX_EXTENSION_APP, // EXTENSION_APP
178 IDR_OMNIBOX_SEARCH, // CONTACT_DEPRECATED
179 IDR_OMNIBOX_HTTP, // BOOKMARK_TITLE
180 IDR_OMNIBOX_HTTP, // NAVSUGGEST_PERSONALIZED
181 IDR_OMNIBOX_CALCULATOR, // CALCULATOR
183 #endif
184 static_assert(arraysize(kIcons) == AutocompleteMatchType::NUM_TYPES,
185 "icons array must have NUM_TYPES elements");
186 return kIcons[type];
189 // static
190 bool AutocompleteMatch::MoreRelevant(const AutocompleteMatch& elem1,
191 const AutocompleteMatch& elem2) {
192 // For equal-relevance matches, we sort alphabetically, so that providers
193 // who return multiple elements at the same priority get a "stable" sort
194 // across multiple updates.
195 return (elem1.relevance == elem2.relevance) ?
196 (elem1.contents < elem2.contents) : (elem1.relevance > elem2.relevance);
199 // static
200 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1,
201 const AutocompleteMatch& elem2) {
202 if (elem1.stripped_destination_url.is_empty() &&
203 elem2.stripped_destination_url.is_empty())
204 return false;
205 return elem1.stripped_destination_url == elem2.stripped_destination_url;
208 // static
209 void AutocompleteMatch::ClassifyMatchInString(
210 const base::string16& find_text,
211 const base::string16& text,
212 int style,
213 ACMatchClassifications* classification) {
214 ClassifyLocationInString(text.find(find_text), find_text.length(),
215 text.length(), style, classification);
218 // static
219 void AutocompleteMatch::ClassifyLocationInString(
220 size_t match_location,
221 size_t match_length,
222 size_t overall_length,
223 int style,
224 ACMatchClassifications* classification) {
225 classification->clear();
227 // Don't classify anything about an empty string
228 // (AutocompleteMatch::Validate() checks this).
229 if (overall_length == 0)
230 return;
232 // Mark pre-match portion of string (if any).
233 if (match_location != 0) {
234 classification->push_back(ACMatchClassification(0, style));
237 // Mark matching portion of string.
238 if (match_location == base::string16::npos) {
239 // No match, above classification will suffice for whole string.
240 return;
242 // Classifying an empty match makes no sense and will lead to validation
243 // errors later.
244 DCHECK_GT(match_length, 0U);
245 classification->push_back(ACMatchClassification(match_location,
246 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM));
248 // Mark post-match portion of string (if any).
249 const size_t after_match(match_location + match_length);
250 if (after_match < overall_length) {
251 classification->push_back(ACMatchClassification(after_match, style));
255 // static
256 AutocompleteMatch::ACMatchClassifications
257 AutocompleteMatch::MergeClassifications(
258 const ACMatchClassifications& classifications1,
259 const ACMatchClassifications& classifications2) {
260 // We must return the empty vector only if both inputs are truly empty.
261 // The result of merging an empty vector with a single (0, NONE)
262 // classification is the latter one-entry vector.
263 if (IsTrivialClassification(classifications1))
264 return classifications2.empty() ? classifications1 : classifications2;
265 if (IsTrivialClassification(classifications2))
266 return classifications1;
268 ACMatchClassifications output;
269 for (ACMatchClassifications::const_iterator i = classifications1.begin(),
270 j = classifications2.begin(); i != classifications1.end();) {
271 AutocompleteMatch::AddLastClassificationIfNecessary(&output,
272 std::max(i->offset, j->offset), i->style | j->style);
273 const size_t next_i_offset = (i + 1) == classifications1.end() ?
274 static_cast<size_t>(-1) : (i + 1)->offset;
275 const size_t next_j_offset = (j + 1) == classifications2.end() ?
276 static_cast<size_t>(-1) : (j + 1)->offset;
277 if (next_i_offset >= next_j_offset)
278 ++j;
279 if (next_j_offset >= next_i_offset)
280 ++i;
283 return output;
286 // static
287 std::string AutocompleteMatch::ClassificationsToString(
288 const ACMatchClassifications& classifications) {
289 std::string serialized_classifications;
290 for (size_t i = 0; i < classifications.size(); ++i) {
291 if (i)
292 serialized_classifications += ',';
293 serialized_classifications += base::IntToString(classifications[i].offset) +
294 ',' + base::IntToString(classifications[i].style);
296 return serialized_classifications;
299 // static
300 ACMatchClassifications AutocompleteMatch::ClassificationsFromString(
301 const std::string& serialized_classifications) {
302 ACMatchClassifications classifications;
303 std::vector<std::string> tokens;
304 Tokenize(serialized_classifications, ",", &tokens);
305 DCHECK(!(tokens.size() & 1)); // The number of tokens should be even.
306 for (size_t i = 0; i < tokens.size(); i += 2) {
307 int classification_offset = 0;
308 int classification_style = ACMatchClassification::NONE;
309 if (!base::StringToInt(tokens[i], &classification_offset) ||
310 !base::StringToInt(tokens[i + 1], &classification_style)) {
311 NOTREACHED();
312 return classifications;
314 classifications.push_back(ACMatchClassification(classification_offset,
315 classification_style));
317 return classifications;
320 // static
321 void AutocompleteMatch::AddLastClassificationIfNecessary(
322 ACMatchClassifications* classifications,
323 size_t offset,
324 int style) {
325 DCHECK(classifications);
326 if (classifications->empty() || classifications->back().style != style) {
327 DCHECK(classifications->empty() ||
328 (offset > classifications->back().offset));
329 classifications->push_back(ACMatchClassification(offset, style));
333 // static
334 base::string16 AutocompleteMatch::SanitizeString(const base::string16& text) {
335 // NOTE: This logic is mirrored by |sanitizeString()| in
336 // omnibox_custom_bindings.js.
337 base::string16 result;
338 base::TrimWhitespace(text, base::TRIM_LEADING, &result);
339 base::RemoveChars(result, kInvalidChars, &result);
340 return result;
343 // static
344 bool AutocompleteMatch::IsSearchType(Type type) {
345 return type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
346 type == AutocompleteMatchType::SEARCH_HISTORY ||
347 type == AutocompleteMatchType::SEARCH_SUGGEST ||
348 type == AutocompleteMatchType::SEARCH_OTHER_ENGINE ||
349 type == AutocompleteMatchType::CALCULATOR ||
350 IsSpecializedSearchType(type);
353 // static
354 bool AutocompleteMatch::IsSpecializedSearchType(Type type) {
355 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY ||
356 type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL ||
357 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED ||
358 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE;
361 // static
362 TemplateURL* AutocompleteMatch::GetTemplateURLWithKeyword(
363 TemplateURLService* template_url_service,
364 const base::string16& keyword,
365 const std::string& host) {
366 if (template_url_service == NULL)
367 return NULL;
368 TemplateURL* template_url = keyword.empty() ?
369 NULL : template_url_service->GetTemplateURLForKeyword(keyword);
370 return (template_url || host.empty()) ?
371 template_url : template_url_service->GetTemplateURLForHost(host);
374 // static
375 GURL AutocompleteMatch::GURLToStrippedGURL(
376 const GURL& url,
377 TemplateURLService* template_url_service,
378 const base::string16& keyword) {
379 if (!url.is_valid())
380 return url;
382 GURL stripped_destination_url = url;
384 // If the destination URL looks like it was generated from a TemplateURL,
385 // remove all substitutions other than the search terms. This allows us
386 // to eliminate cases like past search URLs from history that differ only
387 // by some obscure query param from each other or from the search/keyword
388 // provider matches.
389 TemplateURL* template_url = GetTemplateURLWithKeyword(
390 template_url_service, keyword, stripped_destination_url.host());
391 if (template_url != NULL &&
392 template_url->SupportsReplacement(
393 template_url_service->search_terms_data())) {
394 base::string16 search_terms;
395 if (template_url->ExtractSearchTermsFromURL(
396 stripped_destination_url,
397 template_url_service->search_terms_data(),
398 &search_terms)) {
399 stripped_destination_url =
400 GURL(template_url->url_ref().ReplaceSearchTerms(
401 TemplateURLRef::SearchTermsArgs(search_terms),
402 template_url_service->search_terms_data()));
406 // |replacements| keeps all the substitions we're going to make to
407 // from {destination_url} to {stripped_destination_url}. |need_replacement|
408 // is a helper variable that helps us keep track of whether we need
409 // to apply the replacement.
410 bool needs_replacement = false;
411 GURL::Replacements replacements;
413 // Remove the www. prefix from the host.
414 static const char prefix[] = "www.";
415 static const size_t prefix_len = arraysize(prefix) - 1;
416 std::string host = stripped_destination_url.host();
417 if (host.compare(0, prefix_len, prefix) == 0) {
418 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len));
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(
431 replacements);
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);
471 return (t_url &&
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,
494 int value) {
495 RecordAdditionalInfo(property, base::IntToString(value));
498 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
499 const base::Time& value) {
500 RecordAdditionalInfo(property,
501 base::UTF16ToUTF8(
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 &&
514 provider != NULL &&
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 {
522 if (deletable)
523 return true;
525 for (ACMatches::const_iterator it(duplicate_matches.begin());
526 it != duplicate_matches.end(); ++it) {
527 if (it->deletable)
528 return true;
530 return false;
533 #ifndef NDEBUG
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 {
542 if (text.empty()) {
543 DCHECK(classifications.empty());
544 return;
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)
552 return;
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;
570 #endif