[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / autocomplete / url_prefix.cc
blob8a49f2c95836b2bbd741e3adde99861e5d4bca98
1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/url_prefix.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/autocomplete_input.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://ftp."), 2));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
46 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
47 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
48 prefixes.push_back(URLPrefix(base::string16(), 0));
50 return prefixes;
53 // static
54 bool URLPrefix::IsURLPrefix(const base::string16& prefix) {
55 const URLPrefixes& list = GetURLPrefixes();
56 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
57 if (i->prefix == prefix)
58 return true;
59 return false;
62 // static
63 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
64 const base::string16& prefix_suffix) {
65 const URLPrefixes& list = GetURLPrefixes();
66 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i)
67 if (PrefixMatch(*i, text, prefix_suffix))
68 return &(*i);
69 return NULL;
72 // static
73 bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
74 const base::string16& text,
75 const base::string16& prefix_suffix) {
76 return StartsWith(text, prefix.prefix + prefix_suffix, false);
79 // static
80 size_t URLPrefix::GetInlineAutocompleteOffset(
81 const AutocompleteInput& input,
82 const AutocompleteInput& fixed_up_input,
83 const bool allow_www_prefix_without_scheme,
84 const base::string16& text) {
85 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ?
86 BestURLPrefixWithWWWCase(text, input.text()) :
87 BestURLPrefix(text, input.text());
88 const base::string16* matching_string = &input.text();
89 // If we failed to find a best_prefix initially, try again using a fixed-up
90 // version of the user input. This is especially useful to get about: URLs
91 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
92 // chrome:// scheme.)
93 if ((best_prefix == NULL) && !fixed_up_input.text().empty() &&
94 (fixed_up_input.text() != input.text())) {
95 best_prefix = allow_www_prefix_without_scheme ?
96 BestURLPrefixWithWWWCase(text, fixed_up_input.text()) :
97 BestURLPrefix(text, fixed_up_input.text());
98 matching_string = &fixed_up_input.text();
100 return (best_prefix != NULL) ?
101 (best_prefix->prefix.length() + matching_string->length()) :
102 base::string16::npos;