Roll src/third_party/skia 1b6b626:d5d27d9
[chromium-blink-merge.git] / components / omnibox / url_prefix.cc
blobbe5a5ef40e19123a14d3a0a41329af2062d4dbcf
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/url_prefix.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
11 namespace {
13 // Like URLPrefix::BestURLPrefix() except also handles the prefix of
14 // "www.".
15 const URLPrefix* BestURLPrefixWithWWWCase(
16 const base::string16& text,
17 const base::string16& prefix_suffix) {
18 CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix,
19 (base::ASCIIToUTF16("www."), 1));
20 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix);
21 if ((best_prefix == NULL) ||
22 (best_prefix->num_components < www_prefix.num_components)) {
23 if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix))
24 best_prefix = &www_prefix;
26 return best_prefix;
29 } // namespace
31 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components)
32 : prefix(prefix),
33 num_components(num_components) {
36 // static
37 const URLPrefixes& URLPrefix::GetURLPrefixes() {
38 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
39 if (prefixes.empty()) {
40 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2));
41 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2));
42 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
43 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
46 prefixes.push_back(URLPrefix(base::string16(), 0));
48 return prefixes;
51 // static
52 bool URLPrefix::IsURLPrefix(const base::string16& prefix) {
53 const URLPrefixes& list = GetURLPrefixes();
54 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
55 if (i->prefix == prefix)
56 return true;
57 return false;
60 // static
61 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
62 const base::string16& prefix_suffix) {
63 const URLPrefixes& list = GetURLPrefixes();
64 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
65 if (PrefixMatch(*i, text, prefix_suffix))
66 return &(*i);
67 return NULL;
70 // static
71 bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
72 const base::string16& text,
73 const base::string16& prefix_suffix) {
74 return StartsWith(text, prefix.prefix + prefix_suffix, false);
77 // static
78 size_t URLPrefix::GetInlineAutocompleteOffset(
79 const base::string16& input,
80 const base::string16& fixed_up_input,
81 const bool allow_www_prefix_without_scheme,
82 const base::string16& text) {
83 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ?
84 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input);
85 const base::string16* matching_string = &input;
86 // If we failed to find a best_prefix initially, try again using a fixed-up
87 // version of the user input. This is especially useful to get about: URLs
88 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
89 // chrome:// scheme.)
90 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) {
91 best_prefix = allow_www_prefix_without_scheme ?
92 BestURLPrefixWithWWWCase(text, fixed_up_input) :
93 BestURLPrefix(text, fixed_up_input);
94 matching_string = &fixed_up_input;
96 return (best_prefix != NULL) ?
97 (best_prefix->prefix.length() + matching_string->length()) :
98 base::string16::npos;