1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/autocomplete_provider.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/autocomplete_match.h"
11 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
12 #include "chrome/browser/bookmarks/bookmark_model.h"
13 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/net/url_fixer_upper.h"
16 #include "chrome/common/pref_names.h"
17 #include "content/public/common/url_constants.h"
18 #include "net/base/net_util.h"
22 const size_t AutocompleteProvider::kMaxMatches
= 3;
24 AutocompleteProvider::AutocompleteProvider(
25 AutocompleteProviderListener
* listener
,
35 const char* AutocompleteProvider::TypeToString(Type type
) {
43 case TYPE_EXTENSION_APP
:
44 return "ExtensionApp";
45 case TYPE_HISTORY_QUICK
:
46 return "HistoryQuick";
47 case TYPE_HISTORY_URL
:
55 case TYPE_ZERO_SUGGEST
:
58 NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type
;
63 void AutocompleteProvider::Stop(bool clear_cached_results
) {
67 const char* AutocompleteProvider::GetName() const {
68 return TypeToString(type_
);
71 metrics::OmniboxEventProto_ProviderType
AutocompleteProvider::
72 AsOmniboxEventProviderType() const {
75 return metrics::OmniboxEventProto::BOOKMARK
;
77 return metrics::OmniboxEventProto::BUILTIN
;
79 return metrics::OmniboxEventProto::CONTACT
;
80 case TYPE_EXTENSION_APP
:
81 return metrics::OmniboxEventProto::EXTENSION_APPS
;
82 case TYPE_HISTORY_QUICK
:
83 return metrics::OmniboxEventProto::HISTORY_QUICK
;
84 case TYPE_HISTORY_URL
:
85 return metrics::OmniboxEventProto::HISTORY_URL
;
87 return metrics::OmniboxEventProto::KEYWORD
;
89 return metrics::OmniboxEventProto::SEARCH
;
91 return metrics::OmniboxEventProto::SHORTCUTS
;
92 case TYPE_ZERO_SUGGEST
:
93 return metrics::OmniboxEventProto::ZERO_SUGGEST
;
95 NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type_
;
96 return metrics::OmniboxEventProto::UNKNOWN_PROVIDER
;
100 void AutocompleteProvider::DeleteMatch(const AutocompleteMatch
& match
) {
101 DLOG(WARNING
) << "The AutocompleteProvider '" << GetName()
102 << "' has not implemented DeleteMatch.";
105 void AutocompleteProvider::AddProviderInfo(ProvidersInfo
* provider_info
) const {
108 void AutocompleteProvider::ResetSession() {
111 base::string16
AutocompleteProvider::StringForURLDisplay(const GURL
& url
,
112 bool check_accept_lang
,
113 bool trim_http
) const {
114 std::string languages
= (check_accept_lang
&& profile_
) ?
115 profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
) : std::string();
116 return net::FormatUrl(url
, languages
,
117 net::kFormatUrlOmitAll
& ~(trim_http
? 0 : net::kFormatUrlOmitHTTP
),
118 net::UnescapeRule::SPACES
, NULL
, NULL
, NULL
);
121 AutocompleteProvider::~AutocompleteProvider() {
125 void AutocompleteProvider::UpdateStarredStateOfMatches() {
126 if (matches_
.empty())
132 BookmarkModel
* bookmark_model
= BookmarkModelFactory::GetForProfile(profile_
);
133 if (!bookmark_model
|| !bookmark_model
->loaded())
136 for (ACMatches::iterator
i(matches_
.begin()); i
!= matches_
.end(); ++i
)
137 i
->starred
= bookmark_model
->IsBookmarked(i
->destination_url
);
141 bool AutocompleteProvider::FixupUserInput(AutocompleteInput
* input
) {
142 const base::string16
& input_text
= input
->text();
143 // Fixup and canonicalize user input.
144 const GURL
canonical_gurl(URLFixerUpper::FixupURL(
145 base::UTF16ToUTF8(input_text
), std::string()));
146 std::string
canonical_gurl_str(canonical_gurl
.possibly_invalid_spec());
147 if (canonical_gurl_str
.empty()) {
148 // This probably won't happen, but there are no guarantees.
152 // If the user types a number, GURL will convert it to a dotted quad.
153 // However, if the parser did not mark this as a URL, then the user probably
154 // didn't intend this interpretation. Since this can break history matching
155 // for hostname beginning with numbers (e.g. input of "17173" will be matched
156 // against "0.0.67.21" instead of the original "17173", failing to find
157 // "17173.com"), swap the original hostname in for the fixed-up one.
158 if ((input
->type() != AutocompleteInput::URL
) &&
159 canonical_gurl
.HostIsIPAddress()) {
160 std::string original_hostname
=
161 base::UTF16ToUTF8(input_text
.substr(input
->parts().host
.begin
,
162 input
->parts().host
.len
));
163 const url_parse::Parsed
& parts
=
164 canonical_gurl
.parsed_for_possibly_invalid_spec();
165 // parts.host must not be empty when HostIsIPAddress() is true.
166 DCHECK(parts
.host
.is_nonempty());
167 canonical_gurl_str
.replace(parts
.host
.begin
, parts
.host
.len
,
170 base::string16 output
= base::UTF8ToUTF16(canonical_gurl_str
);
171 // Don't prepend a scheme when the user didn't have one. Since the fixer
172 // upper only prepends the "http" scheme, that's all we need to check for.
173 if (!AutocompleteInput::HasHTTPScheme(input_text
))
174 TrimHttpPrefix(&output
);
176 // Make the number of trailing slashes on the output exactly match the input.
177 // Examples of why not doing this would matter:
178 // * The user types "a" and has this fixed up to "a/". Now no other sites
179 // beginning with "a" will match.
180 // * The user types "file:" and has this fixed up to "file://". Now inline
181 // autocomplete will append too few slashes, resulting in e.g. "file:/b..."
182 // instead of "file:///b..."
183 // * The user types "http:/" and has this fixed up to "http:". Now inline
184 // autocomplete will append too many slashes, resulting in e.g.
185 // "http:///c..." instead of "http://c...".
186 // NOTE: We do this after calling TrimHttpPrefix() since that can strip
187 // trailing slashes (if the scheme is the only thing in the input). It's not
188 // clear that the result of fixup really matters in this case, but there's no
189 // harm in making sure.
190 const size_t last_input_nonslash
=
191 input_text
.find_last_not_of(base::ASCIIToUTF16("/\\"));
192 const size_t num_input_slashes
=
193 (last_input_nonslash
== base::string16::npos
) ?
194 input_text
.length() : (input_text
.length() - 1 - last_input_nonslash
);
195 const size_t last_output_nonslash
=
196 output
.find_last_not_of(base::ASCIIToUTF16("/\\"));
197 const size_t num_output_slashes
=
198 (last_output_nonslash
== base::string16::npos
) ?
199 output
.length() : (output
.length() - 1 - last_output_nonslash
);
200 if (num_output_slashes
< num_input_slashes
)
201 output
.append(num_input_slashes
- num_output_slashes
, '/');
202 else if (num_output_slashes
> num_input_slashes
)
203 output
.erase(output
.length() - num_output_slashes
+ num_input_slashes
);
205 url_parse::Parsed parts
;
206 URLFixerUpper::SegmentURL(output
, &parts
);
207 input
->UpdateText(output
, base::string16::npos
, parts
);
208 return !output
.empty();
212 size_t AutocompleteProvider::TrimHttpPrefix(base::string16
* url
) {
214 if (!AutocompleteInput::HasHTTPScheme(*url
))
217 url
->find(base::ASCIIToUTF16(content::kHttpScheme
) + base::char16(':'));
218 DCHECK_NE(base::string16::npos
, scheme_pos
);
220 // Erase scheme plus up to two slashes.
221 size_t prefix_end
= scheme_pos
+ strlen(content::kHttpScheme
) + 1;
222 const size_t after_slashes
= std::min(url
->length(), prefix_end
+ 2);
223 while ((prefix_end
< after_slashes
) && ((*url
)[prefix_end
] == '/'))
225 url
->erase(scheme_pos
, prefix_end
- scheme_pos
);
226 return (scheme_pos
== 0) ? prefix_end
: 0;