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

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: 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 // This is an implementation method for BestURLPrefix function.
14 // It accepts input text and prefix suffix already converted to lower case.
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)
Peter Kasting 2016/04/23 00:31:01 Nit: {}
Alexander Yashkin 2016/04/23 08:40:20 Done.
20 if (base::StartsWith(lower_text, prefix.prefix + lower_prefix_suffix,
21 base::CompareCase::SENSITIVE))
22 return &prefix;
23 return nullptr;
24 }
25
26 // Like URLPrefix::BestURLPrefixInternal() except also handles the prefix of
Peter Kasting 2016/04/23 00:31:01 Nit: URLPrefix:: qualifier is incorrect
Alexander Yashkin 2016/04/23 08:40:19 Done.
14 // "www.". 27 // "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) || (best_prefix->num_components == 0)) {
Peter Kasting 2016/04/23 00:31:01 Nit: I think "(best_prefix->num_components < www_p
Alexander Yashkin 2016/04/23 08:40:19 Done.
23 if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix)) 36 if (base::StartsWith(lower_text, www_prefix.prefix + lower_prefix_suffix,
37 base::CompareCase::SENSITIVE))
Peter Kasting 2016/04/23 00:31:01 Nit: Combine nested ifs
Alexander Yashkin 2016/04/23 08:40:19 Done.
24 best_prefix = &www_prefix; 38 best_prefix = &www_prefix;
25 } 39 }
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),
33 num_components(num_components) { 47 num_components(num_components) {
48 // Input prefix must be in lower case.
Peter Kasting 2016/04/23 00:31:01 Nit: lowercase
Alexander Yashkin 2016/04/23 08:40:19 Done.
49 DCHECK_EQ(lower_prefix, base::i18n::ToLower(lower_prefix));
34 } 50 }
35 51
36 // static 52 // static
37 const URLPrefixes& URLPrefix::GetURLPrefixes() { 53 const URLPrefixes& URLPrefix::GetURLPrefixes() {
38 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ()); 54 CR_DEFINE_STATIC_LOCAL(URLPrefixes, prefixes, ());
39 if (prefixes.empty()) { 55 if (prefixes.empty()) {
40 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2)); 56 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://www."), 2));
41 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2)); 57 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://www."), 2));
42 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2)); 58 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://www."), 2));
43 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1)); 59 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("https://"), 1));
44 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1)); 60 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("http://"), 1));
45 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1)); 61 prefixes.push_back(URLPrefix(base::ASCIIToUTF16("ftp://"), 1));
46 prefixes.push_back(URLPrefix(base::string16(), 0)); 62 prefixes.push_back(URLPrefix(base::string16(), 0));
47 } 63 }
48 return prefixes; 64 return prefixes;
49 } 65 }
50 66
51 // static 67 // 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, 68 const URLPrefix* URLPrefix::BestURLPrefix(const base::string16& text,
62 const base::string16& prefix_suffix) { 69 const base::string16& prefix_suffix) {
63 const URLPrefixes& list = GetURLPrefixes(); 70 return BestURLPrefixInternal(base::i18n::ToLower(text),
64 for (URLPrefixes::const_iterator i = list.begin(); i != list.end(); ++i) 71 base::i18n::ToLower(prefix_suffix));
65 if (PrefixMatch(*i, text, prefix_suffix))
66 return &(*i);
67 return NULL;
68 } 72 }
69 73
70 // static 74 // 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( 75 size_t URLPrefix::GetInlineAutocompleteOffset(
82 const base::string16& input, 76 const base::string16& input,
83 const base::string16& fixed_up_input, 77 const base::string16& fixed_up_input,
84 const bool allow_www_prefix_without_scheme, 78 const bool allow_www_prefix_without_scheme,
85 const base::string16& text) { 79 const base::string16& text) {
86 const URLPrefix* best_prefix = allow_www_prefix_without_scheme ? 80 const base::string16 lower_text(base::i18n::ToLower(text));
87 BestURLPrefixWithWWWCase(text, input) : BestURLPrefix(text, input); 81 const base::string16 lower_input(base::i18n::ToLower(input));
82 const URLPrefix* best_prefix =
83 allow_www_prefix_without_scheme
84 ? BestURLPrefixWithWWWCase(lower_text, lower_input)
85 : BestURLPrefixInternal(lower_text, lower_input);
88 const base::string16* matching_string = &input; 86 const base::string16* matching_string = &input;
89 // If we failed to find a best_prefix initially, try again using a fixed-up 87 // 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 88 // 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 89 // to inline against chrome:// shortcuts. (about: URLs are fixed up to the
92 // chrome:// scheme.) 90 // chrome:// scheme.)
93 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) { 91 if (!best_prefix && !fixed_up_input.empty() && (fixed_up_input != input)) {
94 best_prefix = allow_www_prefix_without_scheme ? 92 const base::string16 lower_fixed_up_input(
95 BestURLPrefixWithWWWCase(text, fixed_up_input) : 93 base::i18n::ToLower(fixed_up_input));
96 BestURLPrefix(text, fixed_up_input); 94 best_prefix =
95 allow_www_prefix_without_scheme
96 ? BestURLPrefixWithWWWCase(lower_text, lower_fixed_up_input)
97 : BestURLPrefixInternal(lower_text, lower_fixed_up_input);
97 matching_string = &fixed_up_input; 98 matching_string = &fixed_up_input;
98 } 99 }
99 return (best_prefix != NULL) ? 100 return (best_prefix != NULL) ?
100 (best_prefix->prefix.length() + matching_string->length()) : 101 (best_prefix->prefix.length() + matching_string->length()) :
101 base::string16::npos; 102 base::string16::npos;
102 } 103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698