| 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" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 127 |
| 128 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); | 128 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); |
| 129 if (!bookmark_model || !bookmark_model->loaded()) | 129 if (!bookmark_model || !bookmark_model->loaded()) |
| 130 return; | 130 return; |
| 131 | 131 |
| 132 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 132 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
| 133 i->starred = bookmark_model->IsBookmarked(i->destination_url); | 133 i->starred = bookmark_model->IsBookmarked(i->destination_url); |
| 134 } | 134 } |
| 135 | 135 |
| 136 // static | 136 // static |
| 137 bool AutocompleteProvider::FixupUserInput(AutocompleteInput* input) { | 137 base::string16 AutocompleteProvider::FixupUserInput( |
| 138 const base::string16& input_text = input->text(); | 138 const AutocompleteInput& input, |
| 139 bool return_input_on_failure) { |
| 140 const base::string16& input_text = input.text(); |
| 139 // Fixup and canonicalize user input. | 141 // Fixup and canonicalize user input. |
| 140 const GURL canonical_gurl(URLFixerUpper::FixupURL( | 142 const GURL canonical_gurl(URLFixerUpper::FixupURL( |
| 141 base::UTF16ToUTF8(input_text), std::string())); | 143 base::UTF16ToUTF8(input_text), std::string())); |
| 142 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | 144 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); |
| 143 if (canonical_gurl_str.empty()) { | 145 if (canonical_gurl_str.empty()) { |
| 144 // This probably won't happen, but there are no guarantees. | 146 // This probably won't happen, but there are no guarantees. |
| 145 return false; | 147 return return_input_on_failure ? input_text : base::string16(); |
| 146 } | 148 } |
| 147 | 149 |
| 148 // If the user types a number, GURL will convert it to a dotted quad. | 150 // If the user types a number, GURL will convert it to a dotted quad. |
| 149 // However, if the parser did not mark this as a URL, then the user probably | 151 // However, if the parser did not mark this as a URL, then the user probably |
| 150 // didn't intend this interpretation. Since this can break history matching | 152 // didn't intend this interpretation. Since this can break history matching |
| 151 // for hostname beginning with numbers (e.g. input of "17173" will be matched | 153 // for hostname beginning with numbers (e.g. input of "17173" will be matched |
| 152 // against "0.0.67.21" instead of the original "17173", failing to find | 154 // against "0.0.67.21" instead of the original "17173", failing to find |
| 153 // "17173.com"), swap the original hostname in for the fixed-up one. | 155 // "17173.com"), swap the original hostname in for the fixed-up one. |
| 154 if ((input->type() != AutocompleteInput::URL) && | 156 if ((input.type() != AutocompleteInput::URL) && |
| 155 canonical_gurl.HostIsIPAddress()) { | 157 canonical_gurl.HostIsIPAddress()) { |
| 156 std::string original_hostname = | 158 std::string original_hostname = |
| 157 base::UTF16ToUTF8(input_text.substr(input->parts().host.begin, | 159 base::UTF16ToUTF8(input_text.substr(input.parts().host.begin, |
| 158 input->parts().host.len)); | 160 input.parts().host.len)); |
| 159 const url::Parsed& parts = | 161 const url::Parsed& parts = |
| 160 canonical_gurl.parsed_for_possibly_invalid_spec(); | 162 canonical_gurl.parsed_for_possibly_invalid_spec(); |
| 161 // parts.host must not be empty when HostIsIPAddress() is true. | 163 // parts.host must not be empty when HostIsIPAddress() is true. |
| 162 DCHECK(parts.host.is_nonempty()); | 164 DCHECK(parts.host.is_nonempty()); |
| 163 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | 165 canonical_gurl_str.replace(parts.host.begin, parts.host.len, |
| 164 original_hostname); | 166 original_hostname); |
| 165 } | 167 } |
| 166 base::string16 output = base::UTF8ToUTF16(canonical_gurl_str); | 168 base::string16 output(base::UTF8ToUTF16(canonical_gurl_str)); |
| 167 // Don't prepend a scheme when the user didn't have one. Since the fixer | 169 // Don't prepend a scheme when the user didn't have one. Since the fixer |
| 168 // upper only prepends the "http" scheme, that's all we need to check for. | 170 // upper only prepends the "http" scheme, that's all we need to check for. |
| 169 if (!AutocompleteInput::HasHTTPScheme(input_text)) | 171 if (!AutocompleteInput::HasHTTPScheme(input_text)) |
| 170 TrimHttpPrefix(&output); | 172 TrimHttpPrefix(&output); |
| 171 | 173 |
| 172 // Make the number of trailing slashes on the output exactly match the input. | 174 // Make the number of trailing slashes on the output exactly match the input. |
| 173 // Examples of why not doing this would matter: | 175 // Examples of why not doing this would matter: |
| 174 // * The user types "a" and has this fixed up to "a/". Now no other sites | 176 // * The user types "a" and has this fixed up to "a/". Now no other sites |
| 175 // beginning with "a" will match. | 177 // beginning with "a" will match. |
| 176 // * The user types "file:" and has this fixed up to "file://". Now inline | 178 // * The user types "file:" and has this fixed up to "file://". Now inline |
| (...skipping 13 matching lines...) Expand all Loading... |
| 190 input_text.length() : (input_text.length() - 1 - last_input_nonslash); | 192 input_text.length() : (input_text.length() - 1 - last_input_nonslash); |
| 191 const size_t last_output_nonslash = | 193 const size_t last_output_nonslash = |
| 192 output.find_last_not_of(base::ASCIIToUTF16("/\\")); | 194 output.find_last_not_of(base::ASCIIToUTF16("/\\")); |
| 193 const size_t num_output_slashes = | 195 const size_t num_output_slashes = |
| 194 (last_output_nonslash == base::string16::npos) ? | 196 (last_output_nonslash == base::string16::npos) ? |
| 195 output.length() : (output.length() - 1 - last_output_nonslash); | 197 output.length() : (output.length() - 1 - last_output_nonslash); |
| 196 if (num_output_slashes < num_input_slashes) | 198 if (num_output_slashes < num_input_slashes) |
| 197 output.append(num_input_slashes - num_output_slashes, '/'); | 199 output.append(num_input_slashes - num_output_slashes, '/'); |
| 198 else if (num_output_slashes > num_input_slashes) | 200 else if (num_output_slashes > num_input_slashes) |
| 199 output.erase(output.length() - num_output_slashes + num_input_slashes); | 201 output.erase(output.length() - num_output_slashes + num_input_slashes); |
| 202 if (output.empty()) |
| 203 return return_input_on_failure ? input_text : base::string16(); |
| 200 | 204 |
| 201 url::Parsed parts; | 205 return output; |
| 202 URLFixerUpper::SegmentURL(output, &parts); | |
| 203 input->UpdateText(output, base::string16::npos, parts); | |
| 204 return !output.empty(); | |
| 205 } | 206 } |
| 206 | 207 |
| 207 // static | 208 // static |
| 208 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { | 209 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { |
| 209 // Find any "http:". | 210 // Find any "http:". |
| 210 if (!AutocompleteInput::HasHTTPScheme(*url)) | 211 if (!AutocompleteInput::HasHTTPScheme(*url)) |
| 211 return 0; | 212 return 0; |
| 212 size_t scheme_pos = | 213 size_t scheme_pos = |
| 213 url->find(base::ASCIIToUTF16(url::kHttpScheme) + base::char16(':')); | 214 url->find(base::ASCIIToUTF16(url::kHttpScheme) + base::char16(':')); |
| 214 DCHECK_NE(base::string16::npos, scheme_pos); | 215 DCHECK_NE(base::string16::npos, scheme_pos); |
| 215 | 216 |
| 216 // Erase scheme plus up to two slashes. | 217 // Erase scheme plus up to two slashes. |
| 217 size_t prefix_end = scheme_pos + strlen(url::kHttpScheme) + 1; | 218 size_t prefix_end = scheme_pos + strlen(url::kHttpScheme) + 1; |
| 218 const size_t after_slashes = std::min(url->length(), prefix_end + 2); | 219 const size_t after_slashes = std::min(url->length(), prefix_end + 2); |
| 219 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | 220 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) |
| 220 ++prefix_end; | 221 ++prefix_end; |
| 221 url->erase(scheme_pos, prefix_end - scheme_pos); | 222 url->erase(scheme_pos, prefix_end - scheme_pos); |
| 222 return (scheme_pos == 0) ? prefix_end : 0; | 223 return (scheme_pos == 0) ? prefix_end : 0; |
| 223 } | 224 } |
| 224 | 225 |
| OLD | NEW |