[email protected] | ebeb861 | 2014-06-17 01:54:50 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b1c5ab68 | 2014-08-07 11:53:17 | [diff] [blame] | 5 | #include "components/omnibox/url_prefix.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 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 |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 30 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 31 | URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components) |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 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()) { |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 40 | prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2)); |
| 41 | prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2)); |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 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)); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 46 | prefixes.push_back(URLPrefix(base::string16(), 0)); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 47 | } |
| 48 | return prefixes; |
| 49 | } |
| 50 | |
| 51 | // static |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 52 | bool URLPrefix::IsURLPrefix(const base::string16& prefix) { |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 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; |
| 58 | } |
| 59 | |
| 60 | // static |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 61 | const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text, |
| 62 | const base::string16& prefix_suffix) { |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 63 | const URLPrefixes& list = GetURLPrefixes(); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 64 | for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i) |
[email protected] | 5b3cbb9b | 2013-12-07 23:30:33 | [diff] [blame] | 65 | if (PrefixMatch(*i, text, prefix_suffix)) |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 66 | return &(*i); |
| 67 | return NULL; |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 68 | } |
[email protected] | 5b3cbb9b | 2013-12-07 23:30:33 | [diff] [blame] | 69 | |
| 70 | // static |
| 71 | bool URLPrefix::PrefixMatch(const URLPrefix& prefix, |
[email protected] | 4db2cf27 | 2013-12-10 21:53:49 | [diff] [blame] | 72 | const base::string16& text, |
| 73 | const base::string16& prefix_suffix) { |
[email protected] | 5b3cbb9b | 2013-12-07 23:30:33 | [diff] [blame] | 74 | return StartsWith(text, prefix.prefix + prefix_suffix, false); |
| 75 | } |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 76 | |
| 77 | // static |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 78 | size_t URLPrefix::GetInlineAutocompleteOffset( |
[email protected] | 13ff4316 | 2014-06-06 22:03:12 | [diff] [blame] | 79 | const base::string16& input, |
| 80 | const base::string16& fixed_up_input, |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 81 | const bool allow_www_prefix_without_scheme, |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 82 | const base::string16& text) { |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 83 | const URLPrefix* best_prefix = allow_www_prefix_without_scheme ? |
[email protected] | 13ff4316 | 2014-06-06 22:03:12 | [diff] [blame] | 84 | BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input); |
| 85 | const base::string16* matching_string = &input; |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 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.) |
[email protected] | 13ff4316 | 2014-06-06 22:03:12 | [diff] [blame] | 90 | if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) { |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 91 | best_prefix = allow_www_prefix_without_scheme ? |
[email protected] | 13ff4316 | 2014-06-06 22:03:12 | [diff] [blame] | 92 | BestURLPrefixWithWWWCase(text, fixed_up_input) : |
| 93 | BestURLPrefix(text, fixed_up_input); |
| 94 | matching_string = &fixed_up_input; |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 95 | } |
[email protected] | b3a8489 | 2014-04-23 04:28:07 | [diff] [blame] | 96 | return (best_prefix != NULL) ? |
| 97 | (best_prefix->prefix.length() + matching_string->length()) : |
| 98 | base::string16::npos; |
[email protected] | 973a886c | 2014-04-10 21:08:06 | [diff] [blame] | 99 | } |