OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/autocomplete/autocomplete_provider.h" | 5 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
11 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 11 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
12 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/net/url_fixer_upper.h" |
15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
16 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
17 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 // static | 21 // static |
21 const size_t AutocompleteProvider::kMaxMatches = 3; | 22 const size_t AutocompleteProvider::kMaxMatches = 3; |
22 | 23 |
23 AutocompleteProvider::AutocompleteProvider( | 24 AutocompleteProvider::AutocompleteProvider( |
24 AutocompleteProviderListener* listener, | 25 AutocompleteProviderListener* listener, |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 if (!profile_) | 129 if (!profile_) |
129 return; | 130 return; |
130 | 131 |
131 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); | 132 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); |
132 if (!bookmark_model || !bookmark_model->loaded()) | 133 if (!bookmark_model || !bookmark_model->loaded()) |
133 return; | 134 return; |
134 | 135 |
135 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 136 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
136 i->starred = bookmark_model->IsBookmarked(i->destination_url); | 137 i->starred = bookmark_model->IsBookmarked(i->destination_url); |
137 } | 138 } |
| 139 |
| 140 // static |
| 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(UTF16ToUTF8(input_text), |
| 145 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. |
| 149 return false; |
| 150 } |
| 151 |
| 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 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, |
| 168 original_hostname); |
| 169 } |
| 170 base::string16 output = 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); |
| 175 |
| 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(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(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); |
| 204 |
| 205 url_parse::Parsed parts; |
| 206 URLFixerUpper::SegmentURL(output, &parts); |
| 207 input->UpdateText(output, base::string16::npos, parts); |
| 208 return !output.empty(); |
| 209 } |
| 210 |
| 211 // static |
| 212 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { |
| 213 // Find any "http:". |
| 214 if (!AutocompleteInput::HasHTTPScheme(*url)) |
| 215 return 0; |
| 216 size_t scheme_pos = |
| 217 url->find(ASCIIToUTF16(content::kHttpScheme) + char16(':')); |
| 218 DCHECK_NE(base::string16::npos, scheme_pos); |
| 219 |
| 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] == '/')) |
| 224 ++prefix_end; |
| 225 url->erase(scheme_pos, prefix_end - scheme_pos); |
| 226 return (scheme_pos == 0) ? prefix_end : 0; |
| 227 } |
| 228 |
OLD | NEW |