Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / autocomplete_match.cc
blob03dc21907bdc4a79fca527614ba227083fffcf2e
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/browser/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_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "components/omnibox/browser/autocomplete_provider.h"
17 #include "components/omnibox/browser/suggestion_answer.h"
18 #include "components/search_engines/template_url.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "components/url_formatter/url_formatter.h"
21 #include "grit/components_scaled_resources.h"
23 namespace {
25 bool IsTrivialClassification(const ACMatchClassifications& classifications) {
26 return classifications.empty() ||
27 ((classifications.size() == 1) &&
28 (classifications.back().style == ACMatchClassification::NONE));
31 // Returns true if one of the |terms_prefixed_by_http_or_https| matches the
32 // beginning of the URL (sans scheme). (Recall that
33 // |terms_prefixed_by_http_or_https|, for the input "http://a b" will be
34 // ["a"].) This suggests that the user wants a particular URL with a scheme
35 // in mind, hence the caller should not consider another URL like this one
36 // but with a different scheme to be a duplicate. |languages| is used to
37 // format punycoded URLs to decide if they match.
38 bool WordMatchesURLContent(
39 const std::vector<base::string16>& terms_prefixed_by_http_or_https,
40 const std::string& languages,
41 const GURL& url) {
42 size_t prefix_length =
43 url.scheme().length() + strlen(url::kStandardSchemeSeparator);
44 DCHECK_GE(url.spec().length(), prefix_length);
45 const base::string16& formatted_url = url_formatter::FormatUrl(
46 url, languages, url_formatter::kFormatUrlOmitNothing,
47 net::UnescapeRule::NORMAL, nullptr, nullptr, &prefix_length);
48 if (prefix_length == base::string16::npos)
49 return false;
50 const base::string16& formatted_url_without_scheme =
51 formatted_url.substr(prefix_length);
52 for (const auto& term : terms_prefixed_by_http_or_https) {
53 if (base::StartsWith(formatted_url_without_scheme, term,
54 base::CompareCase::SENSITIVE))
55 return true;
57 return false;
60 } // namespace
62 // AutocompleteMatch ----------------------------------------------------------
64 // static
65 const base::char16 AutocompleteMatch::kInvalidChars[] = {
66 '\n', '\r', '\t',
67 0x2028, // Line separator
68 0x2029, // Paragraph separator
72 AutocompleteMatch::AutocompleteMatch()
73 : provider(NULL),
74 relevance(0),
75 typed_count(-1),
76 deletable(false),
77 allowed_to_be_default_match(false),
78 swap_contents_and_description(false),
79 transition(ui::PAGE_TRANSITION_GENERATED),
80 type(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED),
81 from_previous(false) {
84 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider* provider,
85 int relevance,
86 bool deletable,
87 Type type)
88 : provider(provider),
89 relevance(relevance),
90 typed_count(-1),
91 deletable(deletable),
92 allowed_to_be_default_match(false),
93 swap_contents_and_description(false),
94 transition(ui::PAGE_TRANSITION_TYPED),
95 type(type),
96 from_previous(false) {
99 AutocompleteMatch::AutocompleteMatch(const AutocompleteMatch& match)
100 : provider(match.provider),
101 relevance(match.relevance),
102 typed_count(match.typed_count),
103 deletable(match.deletable),
104 fill_into_edit(match.fill_into_edit),
105 inline_autocompletion(match.inline_autocompletion),
106 allowed_to_be_default_match(match.allowed_to_be_default_match),
107 destination_url(match.destination_url),
108 stripped_destination_url(match.stripped_destination_url),
109 contents(match.contents),
110 contents_class(match.contents_class),
111 description(match.description),
112 description_class(match.description_class),
113 swap_contents_and_description(match.swap_contents_and_description),
114 answer_contents(match.answer_contents),
115 answer_type(match.answer_type),
116 answer(SuggestionAnswer::copy(match.answer.get())),
117 transition(match.transition),
118 type(match.type),
119 associated_keyword(match.associated_keyword.get() ?
120 new AutocompleteMatch(*match.associated_keyword) : NULL),
121 keyword(match.keyword),
122 from_previous(match.from_previous),
123 search_terms_args(match.search_terms_args.get() ?
124 new TemplateURLRef::SearchTermsArgs(*match.search_terms_args) :
125 NULL),
126 additional_info(match.additional_info),
127 duplicate_matches(match.duplicate_matches) {
130 AutocompleteMatch::~AutocompleteMatch() {
133 AutocompleteMatch& AutocompleteMatch::operator=(
134 const AutocompleteMatch& match) {
135 if (this == &match)
136 return *this;
138 provider = match.provider;
139 relevance = match.relevance;
140 typed_count = match.typed_count;
141 deletable = match.deletable;
142 fill_into_edit = match.fill_into_edit;
143 inline_autocompletion = match.inline_autocompletion;
144 allowed_to_be_default_match = match.allowed_to_be_default_match;
145 destination_url = match.destination_url;
146 stripped_destination_url = match.stripped_destination_url;
147 contents = match.contents;
148 contents_class = match.contents_class;
149 description = match.description;
150 description_class = match.description_class;
151 swap_contents_and_description = match.swap_contents_and_description;
152 answer_contents = match.answer_contents;
153 answer_type = match.answer_type;
154 answer = SuggestionAnswer::copy(match.answer.get());
155 transition = match.transition;
156 type = match.type;
157 associated_keyword.reset(match.associated_keyword.get() ?
158 new AutocompleteMatch(*match.associated_keyword) : NULL);
159 keyword = match.keyword;
160 from_previous = match.from_previous;
161 search_terms_args.reset(match.search_terms_args.get() ?
162 new TemplateURLRef::SearchTermsArgs(*match.search_terms_args) : NULL);
163 additional_info = match.additional_info;
164 duplicate_matches = match.duplicate_matches;
165 return *this;
168 // static
169 int AutocompleteMatch::TypeToIcon(Type type) {
170 #if !defined(OS_IOS)
171 static const int kIcons[] = {
172 IDR_OMNIBOX_HTTP, // URL_WHAT_YOU_TYPE
173 IDR_OMNIBOX_HTTP, // HISTORY_URL
174 IDR_OMNIBOX_HTTP, // HISTORY_TITLE
175 IDR_OMNIBOX_HTTP, // HISTORY_BODY
176 IDR_OMNIBOX_HTTP, // HISTORY_KEYWORD
177 IDR_OMNIBOX_HTTP, // NAVSUGGEST
178 IDR_OMNIBOX_SEARCH, // SEARCH_WHAT_YOU_TYPED
179 IDR_OMNIBOX_SEARCH, // SEARCH_HISTORY
180 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST
181 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_ENTITY
182 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_TAIL
183 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PERSONALIZED
184 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PROFILE
185 IDR_OMNIBOX_SEARCH, // SEARCH_OTHER_ENGINE
186 IDR_OMNIBOX_EXTENSION_APP, // EXTENSION_APP
187 IDR_OMNIBOX_SEARCH, // CONTACT_DEPRECATED
188 IDR_OMNIBOX_HTTP, // BOOKMARK_TITLE
189 IDR_OMNIBOX_HTTP, // NAVSUGGEST_PERSONALIZED
190 IDR_OMNIBOX_CALCULATOR, // CALCULATOR
192 #else
193 static const int kIcons[] = {
194 IDR_OMNIBOX_HTTP, // URL_WHAT_YOU_TYPE
195 IDR_OMNIBOX_HISTORY, // HISTORY_URL
196 IDR_OMNIBOX_HISTORY, // HISTORY_TITLE
197 IDR_OMNIBOX_HISTORY, // HISTORY_BODY
198 IDR_OMNIBOX_HISTORY, // HISTORY_KEYWORD
199 IDR_OMNIBOX_HTTP, // NAVSUGGEST
200 IDR_OMNIBOX_SEARCH, // SEARCH_WHAT_YOU_TYPED
201 IDR_OMNIBOX_HISTORY, // SEARCH_HISTORY
202 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST
203 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_ENTITY
204 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_TAIL
205 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PERSONALIZED
206 IDR_OMNIBOX_SEARCH, // SEARCH_SUGGEST_PROFILE
207 IDR_OMNIBOX_SEARCH, // SEARCH_OTHER_ENGINE
208 IDR_OMNIBOX_EXTENSION_APP, // EXTENSION_APP
209 IDR_OMNIBOX_SEARCH, // CONTACT_DEPRECATED
210 IDR_OMNIBOX_HTTP, // BOOKMARK_TITLE
211 IDR_OMNIBOX_HTTP, // NAVSUGGEST_PERSONALIZED
212 IDR_OMNIBOX_CALCULATOR, // CALCULATOR
214 #endif
215 static_assert(arraysize(kIcons) == AutocompleteMatchType::NUM_TYPES,
216 "icons array must have NUM_TYPES elements");
217 return kIcons[type];
220 // static
221 bool AutocompleteMatch::MoreRelevant(const AutocompleteMatch& elem1,
222 const AutocompleteMatch& elem2) {
223 // For equal-relevance matches, we sort alphabetically, so that providers
224 // who return multiple elements at the same priority get a "stable" sort
225 // across multiple updates.
226 return (elem1.relevance == elem2.relevance) ?
227 (elem1.contents < elem2.contents) : (elem1.relevance > elem2.relevance);
230 // static
231 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1,
232 const AutocompleteMatch& elem2) {
233 if (elem1.stripped_destination_url.is_empty() &&
234 elem2.stripped_destination_url.is_empty())
235 return false;
236 return elem1.stripped_destination_url == elem2.stripped_destination_url;
239 // static
240 void AutocompleteMatch::ClassifyMatchInString(
241 const base::string16& find_text,
242 const base::string16& text,
243 int style,
244 ACMatchClassifications* classification) {
245 ClassifyLocationInString(text.find(find_text), find_text.length(),
246 text.length(), style, classification);
249 // static
250 void AutocompleteMatch::ClassifyLocationInString(
251 size_t match_location,
252 size_t match_length,
253 size_t overall_length,
254 int style,
255 ACMatchClassifications* classification) {
256 classification->clear();
258 // Don't classify anything about an empty string
259 // (AutocompleteMatch::Validate() checks this).
260 if (overall_length == 0)
261 return;
263 // Mark pre-match portion of string (if any).
264 if (match_location != 0) {
265 classification->push_back(ACMatchClassification(0, style));
268 // Mark matching portion of string.
269 if (match_location == base::string16::npos) {
270 // No match, above classification will suffice for whole string.
271 return;
273 // Classifying an empty match makes no sense and will lead to validation
274 // errors later.
275 DCHECK_GT(match_length, 0U);
276 classification->push_back(ACMatchClassification(match_location,
277 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM));
279 // Mark post-match portion of string (if any).
280 const size_t after_match(match_location + match_length);
281 if (after_match < overall_length) {
282 classification->push_back(ACMatchClassification(after_match, style));
286 // static
287 AutocompleteMatch::ACMatchClassifications
288 AutocompleteMatch::MergeClassifications(
289 const ACMatchClassifications& classifications1,
290 const ACMatchClassifications& classifications2) {
291 // We must return the empty vector only if both inputs are truly empty.
292 // The result of merging an empty vector with a single (0, NONE)
293 // classification is the latter one-entry vector.
294 if (IsTrivialClassification(classifications1))
295 return classifications2.empty() ? classifications1 : classifications2;
296 if (IsTrivialClassification(classifications2))
297 return classifications1;
299 ACMatchClassifications output;
300 for (ACMatchClassifications::const_iterator i = classifications1.begin(),
301 j = classifications2.begin(); i != classifications1.end();) {
302 AutocompleteMatch::AddLastClassificationIfNecessary(&output,
303 std::max(i->offset, j->offset), i->style | j->style);
304 const size_t next_i_offset = (i + 1) == classifications1.end() ?
305 static_cast<size_t>(-1) : (i + 1)->offset;
306 const size_t next_j_offset = (j + 1) == classifications2.end() ?
307 static_cast<size_t>(-1) : (j + 1)->offset;
308 if (next_i_offset >= next_j_offset)
309 ++j;
310 if (next_j_offset >= next_i_offset)
311 ++i;
314 return output;
317 // static
318 std::string AutocompleteMatch::ClassificationsToString(
319 const ACMatchClassifications& classifications) {
320 std::string serialized_classifications;
321 for (size_t i = 0; i < classifications.size(); ++i) {
322 if (i)
323 serialized_classifications += ',';
324 serialized_classifications += base::IntToString(classifications[i].offset) +
325 ',' + base::IntToString(classifications[i].style);
327 return serialized_classifications;
330 // static
331 ACMatchClassifications AutocompleteMatch::ClassificationsFromString(
332 const std::string& serialized_classifications) {
333 ACMatchClassifications classifications;
334 std::vector<base::StringPiece> tokens = base::SplitStringPiece(
335 serialized_classifications, ",", base::KEEP_WHITESPACE,
336 base::SPLIT_WANT_NONEMPTY);
337 DCHECK(!(tokens.size() & 1)); // The number of tokens should be even.
338 for (size_t i = 0; i < tokens.size(); i += 2) {
339 int classification_offset = 0;
340 int classification_style = ACMatchClassification::NONE;
341 if (!base::StringToInt(tokens[i], &classification_offset) ||
342 !base::StringToInt(tokens[i + 1], &classification_style)) {
343 NOTREACHED();
344 return classifications;
346 classifications.push_back(ACMatchClassification(classification_offset,
347 classification_style));
349 return classifications;
352 // static
353 void AutocompleteMatch::AddLastClassificationIfNecessary(
354 ACMatchClassifications* classifications,
355 size_t offset,
356 int style) {
357 DCHECK(classifications);
358 if (classifications->empty() || classifications->back().style != style) {
359 DCHECK(classifications->empty() ||
360 (offset > classifications->back().offset));
361 classifications->push_back(ACMatchClassification(offset, style));
365 // static
366 bool AutocompleteMatch::HasMatchStyle(
367 const ACMatchClassifications& classifications) {
368 for (const auto& it : classifications) {
369 if (it.style & AutocompleteMatch::ACMatchClassification::MATCH)
370 return true;
372 return false;
375 // static
376 base::string16 AutocompleteMatch::SanitizeString(const base::string16& text) {
377 // NOTE: This logic is mirrored by |sanitizeString()| in
378 // omnibox_custom_bindings.js.
379 base::string16 result;
380 base::TrimWhitespace(text, base::TRIM_LEADING, &result);
381 base::RemoveChars(result, kInvalidChars, &result);
382 return result;
385 // static
386 bool AutocompleteMatch::IsSearchType(Type type) {
387 return type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
388 type == AutocompleteMatchType::SEARCH_HISTORY ||
389 type == AutocompleteMatchType::SEARCH_SUGGEST ||
390 type == AutocompleteMatchType::SEARCH_OTHER_ENGINE ||
391 type == AutocompleteMatchType::CALCULATOR ||
392 IsSpecializedSearchType(type);
395 // static
396 bool AutocompleteMatch::IsSpecializedSearchType(Type type) {
397 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY ||
398 type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL ||
399 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED ||
400 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE;
403 // static
404 TemplateURL* AutocompleteMatch::GetTemplateURLWithKeyword(
405 TemplateURLService* template_url_service,
406 const base::string16& keyword,
407 const std::string& host) {
408 if (template_url_service == NULL)
409 return NULL;
410 TemplateURL* template_url = keyword.empty() ?
411 NULL : template_url_service->GetTemplateURLForKeyword(keyword);
412 return (template_url || host.empty()) ?
413 template_url : template_url_service->GetTemplateURLForHost(host);
416 // static
417 GURL AutocompleteMatch::GURLToStrippedGURL(
418 const GURL& url,
419 const AutocompleteInput& input,
420 const std::string& languages,
421 TemplateURLService* template_url_service,
422 const base::string16& keyword) {
423 if (!url.is_valid())
424 return url;
426 GURL stripped_destination_url = url;
428 // If the destination URL looks like it was generated from a TemplateURL,
429 // remove all substitutions other than the search terms. This allows us
430 // to eliminate cases like past search URLs from history that differ only
431 // by some obscure query param from each other or from the search/keyword
432 // provider matches.
433 TemplateURL* template_url = GetTemplateURLWithKeyword(
434 template_url_service, keyword, stripped_destination_url.host());
435 if (template_url != NULL &&
436 template_url->SupportsReplacement(
437 template_url_service->search_terms_data())) {
438 base::string16 search_terms;
439 if (template_url->ExtractSearchTermsFromURL(
440 stripped_destination_url,
441 template_url_service->search_terms_data(),
442 &search_terms)) {
443 stripped_destination_url =
444 GURL(template_url->url_ref().ReplaceSearchTerms(
445 TemplateURLRef::SearchTermsArgs(search_terms),
446 template_url_service->search_terms_data()));
450 // |replacements| keeps all the substitions we're going to make to
451 // from {destination_url} to {stripped_destination_url}. |need_replacement|
452 // is a helper variable that helps us keep track of whether we need
453 // to apply the replacement.
454 bool needs_replacement = false;
455 GURL::Replacements replacements;
457 // Remove the www. prefix from the host.
458 static const char prefix[] = "www.";
459 static const size_t prefix_len = arraysize(prefix) - 1;
460 std::string host = stripped_destination_url.host();
461 if (host.compare(0, prefix_len, prefix) == 0) {
462 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len));
463 needs_replacement = true;
466 // Remove any trailing slash (if it's not a lone slash), or add a slash (to
467 // make a lone slash) if the path is empty. (We can't unconditionally
468 // remove even lone slashes because for some schemes the path must consist
469 // of at least a slash.)
470 const std::string& path = stripped_destination_url.path();
471 if ((path.length() > 1) && (path[path.length() - 1] == '/')) {
472 replacements.SetPathStr(
473 base::StringPiece(path).substr(0, path.length() - 1));
474 needs_replacement = true;
475 } else if (path.empty()) {
476 static const char slash[] = "/";
477 replacements.SetPathStr(base::StringPiece(slash));
478 needs_replacement = true;
481 // Replace https protocol with http, as long as the user didn't explicitly
482 // specify one of the two.
483 if (stripped_destination_url.SchemeIs(url::kHttpsScheme) &&
484 (input.terms_prefixed_by_http_or_https().empty() ||
485 !WordMatchesURLContent(
486 input.terms_prefixed_by_http_or_https(), languages, url))) {
487 replacements.SetScheme(url::kHttpScheme,
488 url::Component(0, strlen(url::kHttpScheme)));
489 needs_replacement = true;
492 if (needs_replacement)
493 stripped_destination_url = stripped_destination_url.ReplaceComponents(
494 replacements);
495 return stripped_destination_url;
498 void AutocompleteMatch::ComputeStrippedDestinationURL(
499 const AutocompleteInput& input,
500 const std::string& languages,
501 TemplateURLService* template_url_service) {
502 stripped_destination_url = GURLToStrippedGURL(
503 destination_url, input, languages, template_url_service, keyword);
506 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault(
507 const AutocompleteInput& input,
508 const std::string& languages,
509 TemplateURLService* template_url_service) {
510 if (!allowed_to_be_default_match) {
511 const GURL& stripped_canonical_input_url =
512 AutocompleteMatch::GURLToStrippedGURL(
513 input.canonicalized_url(), input, languages, template_url_service,
514 base::string16());
515 ComputeStrippedDestinationURL(input, languages, template_url_service);
516 allowed_to_be_default_match =
517 stripped_canonical_input_url == stripped_destination_url;
521 void AutocompleteMatch::GetKeywordUIState(
522 TemplateURLService* template_url_service,
523 base::string16* keyword,
524 bool* is_keyword_hint) const {
525 *is_keyword_hint = associated_keyword.get() != NULL;
526 keyword->assign(*is_keyword_hint ? associated_keyword->keyword :
527 GetSubstitutingExplicitlyInvokedKeyword(template_url_service));
530 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword(
531 TemplateURLService* template_url_service) const {
532 if (transition != ui::PAGE_TRANSITION_KEYWORD ||
533 template_url_service == NULL) {
534 return base::string16();
537 const TemplateURL* t_url = GetTemplateURL(template_url_service, false);
538 return (t_url &&
539 t_url->SupportsReplacement(
540 template_url_service->search_terms_data())) ?
541 keyword : base::string16();
544 TemplateURL* AutocompleteMatch::GetTemplateURL(
545 TemplateURLService* template_url_service,
546 bool allow_fallback_to_destination_host) const {
547 return GetTemplateURLWithKeyword(
548 template_url_service, keyword,
549 allow_fallback_to_destination_host ?
550 destination_url.host() : std::string());
553 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
554 const std::string& value) {
555 DCHECK(!property.empty());
556 DCHECK(!value.empty());
557 additional_info[property] = value;
560 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
561 int value) {
562 RecordAdditionalInfo(property, base::IntToString(value));
565 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property,
566 const base::Time& value) {
567 RecordAdditionalInfo(property,
568 base::UTF16ToUTF8(
569 base::TimeFormatShortDateAndTime(value)));
572 std::string AutocompleteMatch::GetAdditionalInfo(
573 const std::string& property) const {
574 AdditionalInfo::const_iterator i(additional_info.find(property));
575 return (i == additional_info.end()) ? std::string() : i->second;
578 bool AutocompleteMatch::IsVerbatimType() const {
579 const bool is_keyword_verbatim_match =
580 (type == AutocompleteMatchType::SEARCH_OTHER_ENGINE &&
581 provider != NULL &&
582 provider->type() == AutocompleteProvider::TYPE_SEARCH);
583 return type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
584 type == AutocompleteMatchType::URL_WHAT_YOU_TYPED ||
585 is_keyword_verbatim_match;
588 bool AutocompleteMatch::SupportsDeletion() const {
589 if (deletable)
590 return true;
592 for (ACMatches::const_iterator it(duplicate_matches.begin());
593 it != duplicate_matches.end(); ++it) {
594 if (it->deletable)
595 return true;
597 return false;
600 void AutocompleteMatch::PossiblySwapContentsAndDescriptionForDisplay() {
601 if (swap_contents_and_description) {
602 std::swap(contents, description);
603 std::swap(contents_class, description_class);
607 #ifndef NDEBUG
608 void AutocompleteMatch::Validate() const {
609 ValidateClassifications(contents, contents_class);
610 ValidateClassifications(description, description_class);
613 void AutocompleteMatch::ValidateClassifications(
614 const base::string16& text,
615 const ACMatchClassifications& classifications) const {
616 if (text.empty()) {
617 DCHECK(classifications.empty());
618 return;
621 // The classifications should always cover the whole string.
622 DCHECK(!classifications.empty()) << "No classification for \"" << text << '"';
623 DCHECK_EQ(0U, classifications[0].offset)
624 << "Classification misses beginning for \"" << text << '"';
625 if (classifications.size() == 1)
626 return;
628 // The classifications should always be sorted.
629 size_t last_offset = classifications[0].offset;
630 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1);
631 i != classifications.end(); ++i) {
632 const char* provider_name = provider ? provider->GetName() : "None";
633 DCHECK_GT(i->offset, last_offset)
634 << " Classification for \"" << text << "\" with offset of " << i->offset
635 << " is unsorted in relation to last offset of " << last_offset
636 << ". Provider: " << provider_name << ".";
637 DCHECK_LT(i->offset, text.length())
638 << " Classification of [" << i->offset << "," << text.length()
639 << "] is out of bounds for \"" << text << "\". Provider: "
640 << provider_name << ".";
641 last_offset = i->offset;
644 #endif