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.
5 #include "components/omnibox/url_prefix.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
13 // Like URLPrefix::BestURLPrefix() except also handles the prefix of
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
;
31 URLPrefix::URLPrefix(const base::string16
& prefix
, size_t num_components
)
33 num_components(num_components
) {
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://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));
46 prefixes
.push_back(URLPrefix(base::string16(), 0));
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
)
61 const URLPrefix
* URLPrefix::BestURLPrefix(const base::string16
& text
,
62 const base::string16
& prefix_suffix
) {
63 const URLPrefixes
& list
= GetURLPrefixes();
64 for (URLPrefixes::const_iterator i
= list
.begin(); i
!= list
.end(); ++i
)
65 if (PrefixMatch(*i
, text
, prefix_suffix
))
71 bool URLPrefix::PrefixMatch(const URLPrefix
& prefix
,
72 const base::string16
& text
,
73 const base::string16
& prefix_suffix
) {
74 return StartsWith(text
, prefix
.prefix
+ prefix_suffix
, false);
78 size_t URLPrefix::GetInlineAutocompleteOffset(
79 const base::string16
& input
,
80 const base::string16
& fixed_up_input
,
81 const bool allow_www_prefix_without_scheme
,
82 const base::string16
& text
) {
83 const URLPrefix
* best_prefix
= allow_www_prefix_without_scheme
?
84 BestURLPrefixWithWWWCase(text
, input
) : BestURLPrefix(text
, input
);
85 const base::string16
* matching_string
= &input
;
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
90 if (!best_prefix
&& !fixed_up_input
.empty() && (fixed_up_input
!= input
)) {
91 best_prefix
= allow_www_prefix_without_scheme
?
92 BestURLPrefixWithWWWCase(text
, fixed_up_input
) :
93 BestURLPrefix(text
, fixed_up_input
);
94 matching_string
= &fixed_up_input
;
96 return (best_prefix
!= NULL
) ?
97 (best_prefix
->prefix
.length() + matching_string
->length()) :