| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/history_url_provider.h" | 5 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 matches_.erase(i); | 53 matches_.erase(i); |
| 54 } | 54 } |
| 55 break; | 55 break; |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; | 58 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; |
| 59 listener_->OnProviderUpdate(true); | 59 listener_->OnProviderUpdate(true); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // static | 62 // static |
| 63 string16 HistoryProvider::FixupUserInput(const AutocompleteInput& input) { | 63 bool HistoryProvider::FixupUserInput(AutocompleteInput* input) { |
| 64 const string16& input_text = input.text(); | 64 const string16& input_text = input->text(); |
| 65 // Fixup and canonicalize user input. | 65 // Fixup and canonicalize user input. |
| 66 // NOTE: This purposefully doesn't take input.desired_tld() into account; if |
| 67 // it did, then holding "ctrl" would change all the results from the provider, |
| 68 // not just the What You Typed Result. |
| 66 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), | 69 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), |
| 67 std::string())); | 70 std::string())); |
| 68 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | 71 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); |
| 69 if (canonical_gurl_str.empty()) { | 72 if (canonical_gurl_str.empty()) { |
| 70 // This probably won't happen, but there are no guarantees. | 73 // This probably won't happen, but there are no guarantees. |
| 71 return input_text; | 74 return false; |
| 72 } | 75 } |
| 73 | 76 |
| 74 // If the user types a number, GURL will convert it to a dotted quad. | 77 // If the user types a number, GURL will convert it to a dotted quad. |
| 75 // However, if the parser did not mark this as a URL, then the user probably | 78 // However, if the parser did not mark this as a URL, then the user probably |
| 76 // didn't intend this interpretation. Since this can break history matching | 79 // didn't intend this interpretation. Since this can break history matching |
| 77 // for hostname beginning with numbers (e.g. input of "17173" will be matched | 80 // for hostname beginning with numbers (e.g. input of "17173" will be matched |
| 78 // against "0.0.67.21" instead of the original "17173", failing to find | 81 // against "0.0.67.21" instead of the original "17173", failing to find |
| 79 // "17173.com"), swap the original hostname in for the fixed-up one. | 82 // "17173.com"), swap the original hostname in for the fixed-up one. |
| 80 if ((input.type() != AutocompleteInput::URL) && | 83 if ((input->type() != AutocompleteInput::URL) && |
| 81 canonical_gurl.HostIsIPAddress()) { | 84 canonical_gurl.HostIsIPAddress()) { |
| 82 std::string original_hostname = | 85 std::string original_hostname = |
| 83 UTF16ToUTF8(input_text.substr(input.parts().host.begin, | 86 UTF16ToUTF8(input_text.substr(input->parts().host.begin, |
| 84 input.parts().host.len)); | 87 input->parts().host.len)); |
| 85 const url_parse::Parsed& parts = | 88 const url_parse::Parsed& parts = |
| 86 canonical_gurl.parsed_for_possibly_invalid_spec(); | 89 canonical_gurl.parsed_for_possibly_invalid_spec(); |
| 87 // parts.host must not be empty when HostIsIPAddress() is true. | 90 // parts.host must not be empty when HostIsIPAddress() is true. |
| 88 DCHECK(parts.host.is_nonempty()); | 91 DCHECK(parts.host.is_nonempty()); |
| 89 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | 92 canonical_gurl_str.replace(parts.host.begin, parts.host.len, |
| 90 original_hostname); | 93 original_hostname); |
| 91 } | 94 } |
| 92 string16 output = UTF8ToUTF16(canonical_gurl_str); | 95 string16 output = UTF8ToUTF16(canonical_gurl_str); |
| 93 // Don't prepend a scheme when the user didn't have one. Since the fixer | 96 // Don't prepend a scheme when the user didn't have one. Since the fixer |
| 94 // upper only prepends the "http" scheme, that's all we need to check for. | 97 // upper only prepends the "http" scheme, that's all we need to check for. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 118 const size_t last_output_nonslash = | 121 const size_t last_output_nonslash = |
| 119 output.find_last_not_of(ASCIIToUTF16("/\\")); | 122 output.find_last_not_of(ASCIIToUTF16("/\\")); |
| 120 const size_t num_output_slashes = | 123 const size_t num_output_slashes = |
| 121 (last_output_nonslash == string16::npos) ? | 124 (last_output_nonslash == string16::npos) ? |
| 122 output.length() : (output.length() - 1 - last_output_nonslash); | 125 output.length() : (output.length() - 1 - last_output_nonslash); |
| 123 if (num_output_slashes < num_input_slashes) | 126 if (num_output_slashes < num_input_slashes) |
| 124 output.append(num_input_slashes - num_output_slashes, '/'); | 127 output.append(num_input_slashes - num_output_slashes, '/'); |
| 125 else if (num_output_slashes > num_input_slashes) | 128 else if (num_output_slashes > num_input_slashes) |
| 126 output.erase(output.length() - num_output_slashes + num_input_slashes); | 129 output.erase(output.length() - num_output_slashes + num_input_slashes); |
| 127 | 130 |
| 128 return output; | 131 url_parse::Parsed parts; |
| 132 URLFixerUpper::SegmentURL(output, &parts); |
| 133 input->UpdateText(output, parts); |
| 134 return !output.empty(); |
| 129 } | 135 } |
| 130 | 136 |
| 131 // static | 137 // static |
| 132 size_t HistoryProvider::TrimHttpPrefix(string16* url) { | 138 size_t HistoryProvider::TrimHttpPrefix(string16* url) { |
| 133 // Find any "http:". | 139 // Find any "http:". |
| 134 if (!HasHTTPScheme(*url)) | 140 if (!HasHTTPScheme(*url)) |
| 135 return 0; | 141 return 0; |
| 136 size_t scheme_pos = | 142 size_t scheme_pos = |
| 137 url->find(ASCIIToUTF16(chrome::kHttpScheme) + char16(':')); | 143 url->find(ASCIIToUTF16(chrome::kHttpScheme) + char16(':')); |
| 138 DCHECK_NE(string16::npos, scheme_pos); | 144 DCHECK_NE(string16::npos, scheme_pos); |
| 139 | 145 |
| 140 // Erase scheme plus up to two slashes. | 146 // Erase scheme plus up to two slashes. |
| 141 size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; | 147 size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; |
| 142 const size_t after_slashes = std::min(url->length(), prefix_end + 2); | 148 const size_t after_slashes = std::min(url->length(), prefix_end + 2); |
| 143 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | 149 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) |
| 144 ++prefix_end; | 150 ++prefix_end; |
| 145 url->erase(scheme_pos, prefix_end - scheme_pos); | 151 url->erase(scheme_pos, prefix_end - scheme_pos); |
| 146 return (scheme_pos == 0) ? prefix_end : 0; | 152 return (scheme_pos == 0) ? prefix_end : 0; |
| 147 } | 153 } |
| 148 | 154 |
| 149 // static | 155 // static |
| 150 bool HistoryProvider::PreventInlineAutocomplete( | 156 bool HistoryProvider::PreventInlineAutocomplete( |
| 151 const AutocompleteInput& input) { | 157 const AutocompleteInput& input) { |
| 152 return input.prevent_inline_autocomplete() || | 158 return input.prevent_inline_autocomplete() || |
| 153 (!input.text().empty() && | 159 (!input.text().empty() && |
| 154 IsWhitespace(input.text()[input.text().length() - 1])); | 160 IsWhitespace(input.text()[input.text().length() - 1])); |
| 155 } | 161 } |
| OLD | NEW |