Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: components/omnibox/browser/url_prefix.cc

Issue 1897403002: Optimize URLPrefix::BestURLPrefix (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes after review, round 2 Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "components/omnibox/browser/url_prefix.h" 5 #include "components/omnibox/browser/url_prefix.h"
6 6
7 #include "base/i18n/case_conversion.h" 7 #include "base/i18n/case_conversion.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 10
11 namespace { 11 namespace {
12 12
13 // Like URLPrefix::BestURLPrefix() except also handles the prefix of 13 // Implements URLPrefix::BestURLPrefix(). Expects parameters to
14 // "www.". 14 // already be lowercased.
15 const URLPrefix* BestURLPrefixInternal(
16 const base::string16& lower_text,
17 const base::string16& lower_prefix_suffix) {
18 const URLPrefixes& list = URLPrefix::GetURLPrefixes();
19 for (const URLPrefix& prefix : list) {
20 if (base::StartsWith(lower_text, prefix.prefix + lower_prefix_suffix,
21 base::CompareCase::SENSITIVE))
22 return &prefix;
23 }
24 return nullptr;
25 }
26
27 // Like BestURLPrefixInternal() except also handles the prefix of "www.".
15 const URLPrefix* BestURLPrefixWithWWWCase( 28 const URLPrefix* BestURLPrefixWithWWWCase(
16 const base::string16& text, 29 const base::string16& lower_text,
17 const base::string16& prefix_suffix) { 30 const base::string16& lower_prefix_suffix) {
18 CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix, 31 CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix,
19 (base::ASCIIToUTF16("www."), 1)); 32 (base::ASCIIToUTF16("www."), 1));
20 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix); 33 const URLPrefix* best_prefix =
21 if ((best_prefix == NULL) || 34 BestURLPrefixInternal(lower_text, lower_prefix_suffix);
22 (best_prefix->num_components < www_prefix.num_components)) { 35 if ((best_prefix == nullptr ||
23 if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix)) 36 best_prefix->num_components < www_prefix.num_components) &&
24 best_prefix = &www_prefix; 37 base::StartsWith(lower_text, www_prefix.prefix + lower_prefix_suffix,
25 } 38 base::CompareCase::SENSITIVE))
39 best_prefix = &www_prefix;
26 return best_prefix; 40 return best_prefix;
27 } 41 }
28 42
29 } // namespace 43 } // namespace
30 44
31 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components) 45 URLPrefix::URLPrefix(const base::string16& lower_prefix, size_t num_components)
32 : prefix(prefix), 46 : prefix(lower_prefix), num_components(num_components) {
33 num_components(num_components) { 47 // Input prefix must be in lowercase.
48 DCHECK_EQ(lower_prefix, base::i18n::ToLower(lower_prefix));
34 } 49 }
35 50
36 // static 51 // static
37 const URLPrefixes& URLPrefix::GetURLPrefixes() { 52 const URLPrefixes& URLPrefix::GetURLPrefixes() {
38 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ()); 53 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
39 if (prefixes.empty()) { 54 if (prefixes.empty()) {
40 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2)); 55 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2));
41 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2)); 56 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2));
42 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2)); 57 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
43 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1)); 58 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1)); 59 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1)); 60 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
46 prefixes.push_back(URLPrefix(base::string16(), 0)); 61 prefixes.push_back(URLPrefix(base::string16(), 0));
47 } 62 }
48 return prefixes; 63 return prefixes;
49 } 64 }
50 65
51 // static 66 // 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;
58 }
59
60 // static
61 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text, 67 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
62 const base::string16& prefix_suffix) { 68 const base::string16& prefix_suffix) {
63 const URLPrefixes& list = GetURLPrefixes(); 69 return BestURLPrefixInternal(base::i18n::ToLower(text),
64 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i) 70 base::i18n::ToLower(prefix_suffix));
65 if (PrefixMatch(*i, text, prefix_suffix))
66 return &(*i);
67 return NULL;
68 } 71 }
69 72
70 // static 73 // static
71 bool URLPrefix::PrefixMatch(const URLPrefix& prefix,
72 const base::string16& text,
73 const base::string16& prefix_suffix) {
74 return base::StartsWith(
75 base::i18n::ToLower(text),
76 base::i18n::ToLower(prefix.prefix + prefix_suffix),
77 base::CompareCase::SENSITIVE);
78 }
79
80 // static
81 size_t URLPrefix::GetInlineAutocompleteOffset( 74 size_t URLPrefix::GetInlineAutocompleteOffset(
82 const base::string16& input, 75 const base::string16& input,
83 const base::string16& fixed_up_input, 76 const base::string16& fixed_up_input,
84 const bool allow_www_prefix_without_scheme, 77 const bool allow_www_prefix_without_scheme,
85 const base::string16& text) { 78 const base::string16& text) {
86 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ? 79 const base::string16 lower_text(base::i18n::ToLower(text));
87 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input); 80 const base::string16 lower_input(base::i18n::ToLower(input));
81 const URLPrefix* best_prefix =
82 allow_www_prefix_without_scheme
83 ? BestURLPrefixWithWWWCase(lower_text, lower_input)
84 : BestURLPrefixInternal(lower_text, lower_input);
88 const base::string16* matching_string = &input; 85 const base::string16* matching_string = &input;
89 // If we failed to find a best_prefix initially, try again using a fixed-up 86 // 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 87 // 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 88 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
92 // chrome:// scheme.) 89 // chrome:// scheme.)
93 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) { 90 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) {
94 best_prefix = allow_www_prefix_without_scheme ? 91 const base::string16 lower_fixed_up_input(
95 BestURLPrefixWithWWWCase(text, fixed_up_input) : 92 base::i18n::ToLower(fixed_up_input));
96 BestURLPrefix(text, fixed_up_input); 93 best_prefix =
94 allow_www_prefix_without_scheme
95 ? BestURLPrefixWithWWWCase(lower_text, lower_fixed_up_input)
96 : BestURLPrefixInternal(lower_text, lower_fixed_up_input);
97 matching_string = &fixed_up_input; 97 matching_string = &fixed_up_input;
98 } 98 }
99 return (best_prefix != NULL) ? 99 return (best_prefix != NULL) ?
100 (best_prefix->prefix.length() + matching_string->length()) : 100 (best_prefix->prefix.length() + matching_string->length()) :
101 base::string16::npos; 101 base::string16::npos;
102 } 102 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/url_prefix.h ('k') | components/omnibox/browser/url_prefix_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698