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