| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 std::wstring HistoryProvider::FixupUserInput(const AutocompleteInput& input) { |
| 64 const string16& input_text = input.text(); | 64 const std::wstring& input_text = input.text(); |
| 65 // Fixup and canonicalize user input. | 65 // Fixup and canonicalize user input. |
| 66 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), | 66 const GURL canonical_gurl(URLFixerUpper::FixupURL(WideToUTF8(input_text), |
| 67 std::string())); | 67 std::string())); |
| 68 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | 68 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); |
| 69 if (canonical_gurl_str.empty()) { | 69 if (canonical_gurl_str.empty()) { |
| 70 // This probably won't happen, but there are no guarantees. | 70 // This probably won't happen, but there are no guarantees. |
| 71 return input_text; | 71 return input_text; |
| 72 } | 72 } |
| 73 | 73 |
| 74 // If the user types a number, GURL will convert it to a dotted quad. | 74 // 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 | 75 // 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 | 76 // 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 | 77 // 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 | 78 // 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. | 79 // "17173.com"), swap the original hostname in for the fixed-up one. |
| 80 if ((input.type() != AutocompleteInput::URL) && | 80 if ((input.type() != AutocompleteInput::URL) && |
| 81 canonical_gurl.HostIsIPAddress()) { | 81 canonical_gurl.HostIsIPAddress()) { |
| 82 std::string original_hostname = | 82 std::string original_hostname = |
| 83 UTF16ToUTF8(input_text.substr(input.parts().host.begin, | 83 WideToUTF8(input_text.substr(input.parts().host.begin, |
| 84 input.parts().host.len)); | 84 input.parts().host.len)); |
| 85 const url_parse::Parsed& parts = | 85 const url_parse::Parsed& parts = |
| 86 canonical_gurl.parsed_for_possibly_invalid_spec(); | 86 canonical_gurl.parsed_for_possibly_invalid_spec(); |
| 87 // parts.host must not be empty when HostIsIPAddress() is true. | 87 // parts.host must not be empty when HostIsIPAddress() is true. |
| 88 DCHECK(parts.host.is_nonempty()); | 88 DCHECK(parts.host.is_nonempty()); |
| 89 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | 89 canonical_gurl_str.replace(parts.host.begin, parts.host.len, |
| 90 original_hostname); | 90 original_hostname); |
| 91 } | 91 } |
| 92 string16 output = UTF8ToUTF16(canonical_gurl_str); | 92 std::wstring output = UTF8ToWide(canonical_gurl_str); |
| 93 // Don't prepend a scheme when the user didn't have one. Since the fixer | 93 // 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. | 94 // upper only prepends the "http" scheme, that's all we need to check for. |
| 95 if (canonical_gurl.SchemeIs(chrome::kHttpScheme) && | 95 if (canonical_gurl.SchemeIs(chrome::kHttpScheme) && |
| 96 !url_util::FindAndCompareScheme(UTF16ToUTF8(input_text), | 96 !url_util::FindAndCompareScheme(WideToUTF8(input_text), |
| 97 chrome::kHttpScheme, NULL)) | 97 chrome::kHttpScheme, NULL)) |
| 98 TrimHttpPrefix(&output); | 98 TrimHttpPrefix(&output); |
| 99 | 99 |
| 100 // Make the number of trailing slashes on the output exactly match the input. | 100 // Make the number of trailing slashes on the output exactly match the input. |
| 101 // Examples of why not doing this would matter: | 101 // Examples of why not doing this would matter: |
| 102 // * The user types "a" and has this fixed up to "a/". Now no other sites | 102 // * The user types "a" and has this fixed up to "a/". Now no other sites |
| 103 // beginning with "a" will match. | 103 // beginning with "a" will match. |
| 104 // * The user types "file:" and has this fixed up to "file://". Now inline | 104 // * The user types "file:" and has this fixed up to "file://". Now inline |
| 105 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." | 105 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." |
| 106 // instead of "file:///b..." | 106 // instead of "file:///b..." |
| 107 // * The user types "http:/" and has this fixed up to "http:". Now inline | 107 // * The user types "http:/" and has this fixed up to "http:". Now inline |
| 108 // autocomplete will append too many slashes, resulting in e.g. | 108 // autocomplete will append too many slashes, resulting in e.g. |
| 109 // "http:///c..." instead of "http://c...". | 109 // "http:///c..." instead of "http://c...". |
| 110 // NOTE: We do this after calling TrimHttpPrefix() since that can strip | 110 // NOTE: We do this after calling TrimHttpPrefix() since that can strip |
| 111 // trailing slashes (if the scheme is the only thing in the input). It's not | 111 // trailing slashes (if the scheme is the only thing in the input). It's not |
| 112 // clear that the result of fixup really matters in this case, but there's no | 112 // clear that the result of fixup really matters in this case, but there's no |
| 113 // harm in making sure. | 113 // harm in making sure. |
| 114 const size_t last_input_nonslash = | 114 const size_t last_input_nonslash = input_text.find_last_not_of(L"/\\"); |
| 115 input_text.find_last_not_of(ASCIIToUTF16("/\\")); | 115 const size_t num_input_slashes = (last_input_nonslash == std::wstring::npos) ? |
| 116 const size_t num_input_slashes = (last_input_nonslash == string16::npos) ? | |
| 117 input_text.length() : (input_text.length() - 1 - last_input_nonslash); | 116 input_text.length() : (input_text.length() - 1 - last_input_nonslash); |
| 118 const size_t last_output_nonslash = | 117 const size_t last_output_nonslash = output.find_last_not_of(L"/\\"); |
| 119 output.find_last_not_of(ASCIIToUTF16("/\\")); | |
| 120 const size_t num_output_slashes = | 118 const size_t num_output_slashes = |
| 121 (last_output_nonslash == string16::npos) ? | 119 (last_output_nonslash == std::wstring::npos) ? |
| 122 output.length() : (output.length() - 1 - last_output_nonslash); | 120 output.length() : (output.length() - 1 - last_output_nonslash); |
| 123 if (num_output_slashes < num_input_slashes) | 121 if (num_output_slashes < num_input_slashes) |
| 124 output.append(num_input_slashes - num_output_slashes, '/'); | 122 output.append(num_input_slashes - num_output_slashes, '/'); |
| 125 else if (num_output_slashes > num_input_slashes) | 123 else if (num_output_slashes > num_input_slashes) |
| 126 output.erase(output.length() - num_output_slashes + num_input_slashes); | 124 output.erase(output.length() - num_output_slashes + num_input_slashes); |
| 127 | 125 |
| 128 return output; | 126 return output; |
| 129 } | 127 } |
| 130 | 128 |
| 131 // static | 129 // static |
| 132 size_t HistoryProvider::TrimHttpPrefix(string16* url) { | 130 size_t HistoryProvider::TrimHttpPrefix(std::wstring* url) { |
| 133 // Find any "http:". | 131 // Find any "http:". |
| 134 if (!HasHTTPScheme(*url)) | 132 if (!HasHTTPScheme(*url)) |
| 135 return 0; | 133 return 0; |
| 136 size_t scheme_pos = | 134 size_t scheme_pos = url->find(ASCIIToWide(chrome::kHttpScheme) + L":"); |
| 137 url->find(ASCIIToUTF16(chrome::kHttpScheme) + char16(':')); | 135 DCHECK(scheme_pos != std::wstring::npos); |
| 138 DCHECK(scheme_pos != string16::npos); | |
| 139 | 136 |
| 140 // Erase scheme plus up to two slashes. | 137 // Erase scheme plus up to two slashes. |
| 141 size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; | 138 size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; |
| 142 const size_t after_slashes = std::min(url->length(), prefix_end + 2); | 139 const size_t after_slashes = std::min(url->length(), prefix_end + 2); |
| 143 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | 140 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == L'/')) |
| 144 ++prefix_end; | 141 ++prefix_end; |
| 145 url->erase(scheme_pos, prefix_end - scheme_pos); | 142 url->erase(scheme_pos, prefix_end - scheme_pos); |
| 146 return (scheme_pos == 0) ? prefix_end : 0; | 143 return (scheme_pos == 0) ? prefix_end : 0; |
| 147 } | 144 } |
| OLD | NEW |