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/autocomplete_provider.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/autocomplete_input.h"
10 #include "components/omnibox/autocomplete_match.h"
11 #include "components/url_fixer/url_fixer.h"
12 #include "net/base/net_util.h"
16 const size_t AutocompleteProvider::kMaxMatches
= 3;
18 AutocompleteProvider::AutocompleteProvider(Type type
)
24 const char* AutocompleteProvider::TypeToString(Type type
) {
30 case TYPE_HISTORY_QUICK
:
31 return "HistoryQuick";
32 case TYPE_HISTORY_URL
:
40 case TYPE_ZERO_SUGGEST
:
43 NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type
;
48 void AutocompleteProvider::Stop(bool clear_cached_results
,
49 bool due_to_user_inactivity
) {
53 const char* AutocompleteProvider::GetName() const {
54 return TypeToString(type_
);
57 metrics::OmniboxEventProto_ProviderType
AutocompleteProvider::
58 AsOmniboxEventProviderType() const {
61 return metrics::OmniboxEventProto::BOOKMARK
;
63 return metrics::OmniboxEventProto::BUILTIN
;
64 case TYPE_HISTORY_QUICK
:
65 return metrics::OmniboxEventProto::HISTORY_QUICK
;
66 case TYPE_HISTORY_URL
:
67 return metrics::OmniboxEventProto::HISTORY_URL
;
69 return metrics::OmniboxEventProto::KEYWORD
;
71 return metrics::OmniboxEventProto::SEARCH
;
73 return metrics::OmniboxEventProto::SHORTCUTS
;
74 case TYPE_ZERO_SUGGEST
:
75 return metrics::OmniboxEventProto::ZERO_SUGGEST
;
77 NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type_
;
78 return metrics::OmniboxEventProto::UNKNOWN_PROVIDER
;
82 void AutocompleteProvider::DeleteMatch(const AutocompleteMatch
& match
) {
83 DLOG(WARNING
) << "The AutocompleteProvider '" << GetName()
84 << "' has not implemented DeleteMatch.";
87 void AutocompleteProvider::AddProviderInfo(ProvidersInfo
* provider_info
) const {
90 void AutocompleteProvider::ResetSession() {
93 AutocompleteProvider::~AutocompleteProvider() {
98 AutocompleteProvider::FixupReturn
AutocompleteProvider::FixupUserInput(
99 const AutocompleteInput
& input
) {
100 const base::string16
& input_text
= input
.text();
101 const FixupReturn
failed(false, input_text
);
103 // Fixup and canonicalize user input.
104 const GURL
canonical_gurl(
105 url_fixer::FixupURL(base::UTF16ToUTF8(input_text
), std::string()));
106 std::string
canonical_gurl_str(canonical_gurl
.possibly_invalid_spec());
107 if (canonical_gurl_str
.empty()) {
108 // This probably won't happen, but there are no guarantees.
112 // If the user types a number, GURL will convert it to a dotted quad.
113 // However, if the parser did not mark this as a URL, then the user probably
114 // didn't intend this interpretation. Since this can break history matching
115 // for hostname beginning with numbers (e.g. input of "17173" will be matched
116 // against "0.0.67.21" instead of the original "17173", failing to find
117 // "17173.com"), swap the original hostname in for the fixed-up one.
118 if ((input
.type() != metrics::OmniboxInputType::URL
) &&
119 canonical_gurl
.HostIsIPAddress()) {
120 std::string original_hostname
=
121 base::UTF16ToUTF8(input_text
.substr(input
.parts().host
.begin
,
122 input
.parts().host
.len
));
123 const url::Parsed
& parts
=
124 canonical_gurl
.parsed_for_possibly_invalid_spec();
125 // parts.host must not be empty when HostIsIPAddress() is true.
126 DCHECK(parts
.host
.is_nonempty());
127 canonical_gurl_str
.replace(parts
.host
.begin
, parts
.host
.len
,
130 base::string16
output(base::UTF8ToUTF16(canonical_gurl_str
));
131 // Don't prepend a scheme when the user didn't have one. Since the fixer
132 // upper only prepends the "http" scheme, that's all we need to check for.
133 if (!AutocompleteInput::HasHTTPScheme(input_text
))
134 TrimHttpPrefix(&output
);
136 // Make the number of trailing slashes on the output exactly match the input.
137 // Examples of why not doing this would matter:
138 // * The user types "a" and has this fixed up to "a/". Now no other sites
139 // beginning with "a" will match.
140 // * The user types "file:" and has this fixed up to "file://". Now inline
141 // autocomplete will append too few slashes, resulting in e.g. "file:/b..."
142 // instead of "file:///b..."
143 // * The user types "http:/" and has this fixed up to "http:". Now inline
144 // autocomplete will append too many slashes, resulting in e.g.
145 // "http:///c..." instead of "http://c...".
146 // NOTE: We do this after calling TrimHttpPrefix() since that can strip
147 // trailing slashes (if the scheme is the only thing in the input). It's not
148 // clear that the result of fixup really matters in this case, but there's no
149 // harm in making sure.
150 const size_t last_input_nonslash
=
151 input_text
.find_last_not_of(base::ASCIIToUTF16("/\\"));
152 const size_t num_input_slashes
=
153 (last_input_nonslash
== base::string16::npos
) ?
154 input_text
.length() : (input_text
.length() - 1 - last_input_nonslash
);
155 const size_t last_output_nonslash
=
156 output
.find_last_not_of(base::ASCIIToUTF16("/\\"));
157 const size_t num_output_slashes
=
158 (last_output_nonslash
== base::string16::npos
) ?
159 output
.length() : (output
.length() - 1 - last_output_nonslash
);
160 if (num_output_slashes
< num_input_slashes
)
161 output
.append(num_input_slashes
- num_output_slashes
, '/');
162 else if (num_output_slashes
> num_input_slashes
)
163 output
.erase(output
.length() - num_output_slashes
+ num_input_slashes
);
167 return FixupReturn(true, output
);
171 size_t AutocompleteProvider::TrimHttpPrefix(base::string16
* url
) {
173 if (!AutocompleteInput::HasHTTPScheme(*url
))
176 url
->find(base::ASCIIToUTF16(url::kHttpScheme
) + base::char16(':'));
177 DCHECK_NE(base::string16::npos
, scheme_pos
);
179 // Erase scheme plus up to two slashes.
180 size_t prefix_end
= scheme_pos
+ strlen(url::kHttpScheme
) + 1;
181 const size_t after_slashes
= std::min(url
->length(), prefix_end
+ 2);
182 while ((prefix_end
< after_slashes
) && ((*url
)[prefix_end
] == '/'))
184 url
->erase(scheme_pos
, prefix_end
- scheme_pos
);
185 return (scheme_pos
== 0) ? prefix_end
: 0;