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. 4 5 #include "components/autocomplete/url_prefix.h" 6 7 #include "base/basictypes.h" 8 #include "base/strings/string_util.h" 9 #include "base/strings/utf_string_conversions.h" 10 11 namespace { 12 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; 25 } 26 return best_prefix; 27 } 28 29 } // namespace 30 31 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components) 32 : prefix(prefix), 33 num_components(num_components) { 34 } 35 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://ftp."), 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)); 48 } 49 return prefixes; 50 } 51 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; 59 } 60 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; 69 } 70 71 // static 72 bool URLPrefix::PrefixMatch(const URLPrefix& prefix, 73 const base::string16& text, 74 const base::string16& prefix_suffix) { 75 return StartsWith(text, prefix.prefix + prefix_suffix, false); 76 } 77 78 // static 79 size_t URLPrefix::GetInlineAutocompleteOffset( 80 const base::string16& input, 81 const base::string16& fixed_up_input, 82 const bool allow_www_prefix_without_scheme, 83 const base::string16& text) { 84 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ? 85 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input); 86 const base::string16* matching_string = &input; 87 // If we failed to find a best_prefix initially, try again using a fixed-up 88 // version of the user input. This is especially useful to get about: URLs 89 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the 90 // chrome:// scheme.) 91 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) { 92 best_prefix = allow_www_prefix_without_scheme ? 93 BestURLPrefixWithWWWCase(text, fixed_up_input) : 94 BestURLPrefix(text, fixed_up_input); 95 matching_string = &fixed_up_input; 96 } 97 return (best_prefix != NULL) ? 98 (best_prefix->prefix.length() + matching_string->length()) : 99 base::string16::npos; 100 } 101