OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/url_prefix.h" | 5 #include "chrome/browser/autocomplete/url_prefix.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 11 |
| 12 namespace { |
| 13 |
| 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; |
| 26 } |
| 27 return best_prefix; |
| 28 } |
| 29 |
| 30 } // namespace |
10 | 31 |
11 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components) | 32 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components) |
12 : prefix(prefix), | 33 : prefix(prefix), |
13 num_components(num_components) { | 34 num_components(num_components) { |
14 } | 35 } |
15 | 36 |
16 // static | 37 // static |
17 const URLPrefixes& URLPrefix::GetURLPrefixes() { | 38 const URLPrefixes& URLPrefix::GetURLPrefixes() { |
18 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ()); | 39 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ()); |
19 if (prefixes.empty()) { | 40 if (prefixes.empty()) { |
(...skipping 27 matching lines...) Expand all Loading... |
47 return &(*i); | 68 return &(*i); |
48 return NULL; | 69 return NULL; |
49 } | 70 } |
50 | 71 |
51 // static | 72 // static |
52 bool URLPrefix::PrefixMatch(const URLPrefix& prefix, | 73 bool URLPrefix::PrefixMatch(const URLPrefix& prefix, |
53 const base::string16& text, | 74 const base::string16& text, |
54 const base::string16& prefix_suffix) { | 75 const base::string16& prefix_suffix) { |
55 return StartsWith(text, prefix.prefix + prefix_suffix, false); | 76 return StartsWith(text, prefix.prefix + prefix_suffix, false); |
56 } | 77 } |
| 78 |
| 79 // static |
| 80 void URLPrefix::ComputeMatchStartAndInlineAutocompleteOffset( |
| 81 const AutocompleteInput& input, |
| 82 const AutocompleteInput& fixed_up_input, |
| 83 const bool allow_www_prefix_without_scheme, |
| 84 const base::string16& text, |
| 85 size_t* match_start, |
| 86 size_t* inline_autocomplete_offset) { |
| 87 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ? |
| 88 BestURLPrefixWithWWWCase(text, input.text()) : |
| 89 BestURLPrefix(text, input.text()); |
| 90 const base::string16* matching_string = &input.text(); |
| 91 // If we failed to find a best_prefix initially, try again using a fixed-up |
| 92 // version of the user input. This is especially useful to get about: URLs |
| 93 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the |
| 94 // chrome:// scheme.) |
| 95 if ((best_prefix == NULL) && !fixed_up_input.text().empty() && |
| 96 (fixed_up_input.text() != input.text())) { |
| 97 best_prefix = allow_www_prefix_without_scheme ? |
| 98 BestURLPrefixWithWWWCase(text, fixed_up_input.text()) : |
| 99 BestURLPrefix(text, fixed_up_input.text()); |
| 100 matching_string = &fixed_up_input.text(); |
| 101 } |
| 102 if (best_prefix != NULL) { |
| 103 *match_start = best_prefix->prefix.length(); |
| 104 *inline_autocomplete_offset = |
| 105 best_prefix->prefix.length() + matching_string->length(); |
| 106 } else { |
| 107 *match_start = base::string16::npos; |
| 108 *inline_autocomplete_offset = base::string16::npos; |
| 109 } |
| 110 } |
OLD | NEW |