Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / url_prefix.cc
blobbc0eab4908a4bdc4210376e35e712b1108da8855
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/url_prefix.h"
7 #include "base/basictypes.h"
8 #include "base/i18n/case_conversion.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
12 namespace {
14 // Like URLPrefix::BestURLPrefix() except also handles the prefix of
15 // "www.".
16 const URLPrefix* BestURLPrefixWithWWWCase(
17 const base::string16& text,
18 const base::string16& prefix_suffix) {
19 CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix,
20 (base::ASCIIToUTF16("www."), 1));
21 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix);
22 if ((best_prefix == NULL) ||
23 (best_prefix->num_components < www_prefix.num_components)) {
24 if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix))
25 best_prefix = &www_prefix;
27 return best_prefix;
30 } // namespace
32 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components)
33 : prefix(prefix),
34 num_components(num_components) {
37 // static
38 const URLPrefixes& URLPrefix::GetURLPrefixes() {
39 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
40 if (prefixes.empty()) {
41 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2));
42 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2));
43 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
46 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
47 prefixes.push_back(URLPrefix(base::string16(), 0));
49 return prefixes;
52 // static
53 bool URLPrefix::IsURLPrefix(const base::string16& prefix) {
54 const URLPrefixes& list = GetURLPrefixes();
55 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
56 if (i->prefix == prefix)
57 return true;
58 return false;
61 // static
62 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
63 const base::string16& prefix_suffix) {
64 const URLPrefixes& list = GetURLPrefixes();
65 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
66 if (PrefixMatch(*i, text, prefix_suffix))
67 return &(*i);
68 return NULL;
71 // static
72 bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
73 const base::string16& text,
74 const base::string16& prefix_suffix) {
75 return base::StartsWith(
76 base::i18n::ToLower(text),
77 base::i18n::ToLower(prefix.prefix + prefix_suffix),
78 base::CompareCase::SENSITIVE);
81 // static
82 size_t URLPrefix::GetInlineAutocompleteOffset(
83 const base::string16& input,
84 const base::string16& fixed_up_input,
85 const bool allow_www_prefix_without_scheme,
86 const base::string16& text) {
87 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ?
88 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input);
89 const base::string16* matching_string = &input;
90 // If we failed to find a best_prefix initially, try again using a fixed-up
91 // version of the user input. This is especially useful to get about: URLs
92 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
93 // chrome:// scheme.)
94 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) {
95 best_prefix = allow_www_prefix_without_scheme ?
96 BestURLPrefixWithWWWCase(text, fixed_up_input) :
97 BestURLPrefix(text, fixed_up_input);
98 matching_string = &fixed_up_input;
100 return (best_prefix != NULL) ?
101 (best_prefix->prefix.length() + matching_string->length()) :
102 base::string16::npos;