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/history_provider.h" | 5 #include "chrome/browser/autocomplete/history_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 break; | 59 break; |
60 } | 60 } |
61 } | 61 } |
62 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; | 62 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; |
63 listener_->OnProviderUpdate(true); | 63 listener_->OnProviderUpdate(true); |
64 } | 64 } |
65 | 65 |
66 // static | 66 // static |
67 bool HistoryProvider::FixupUserInput(AutocompleteInput* input) { | 67 bool HistoryProvider::FixupUserInput(AutocompleteInput* input) { |
68 const string16& input_text = input->text(); | 68 const base::string16& input_text = input->text(); |
69 // Fixup and canonicalize user input. | 69 // Fixup and canonicalize user input. |
70 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), | 70 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), |
71 std::string())); | 71 std::string())); |
72 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | 72 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); |
73 if (canonical_gurl_str.empty()) { | 73 if (canonical_gurl_str.empty()) { |
74 // This probably won't happen, but there are no guarantees. | 74 // This probably won't happen, but there are no guarantees. |
75 return false; | 75 return false; |
76 } | 76 } |
77 | 77 |
78 // If the user types a number, GURL will convert it to a dotted quad. | 78 // If the user types a number, GURL will convert it to a dotted quad. |
79 // However, if the parser did not mark this as a URL, then the user probably | 79 // However, if the parser did not mark this as a URL, then the user probably |
80 // didn't intend this interpretation. Since this can break history matching | 80 // didn't intend this interpretation. Since this can break history matching |
81 // for hostname beginning with numbers (e.g. input of "17173" will be matched | 81 // for hostname beginning with numbers (e.g. input of "17173" will be matched |
82 // against "0.0.67.21" instead of the original "17173", failing to find | 82 // against "0.0.67.21" instead of the original "17173", failing to find |
83 // "17173.com"), swap the original hostname in for the fixed-up one. | 83 // "17173.com"), swap the original hostname in for the fixed-up one. |
84 if ((input->type() != AutocompleteInput::URL) && | 84 if ((input->type() != AutocompleteInput::URL) && |
85 canonical_gurl.HostIsIPAddress()) { | 85 canonical_gurl.HostIsIPAddress()) { |
86 std::string original_hostname = | 86 std::string original_hostname = |
87 UTF16ToUTF8(input_text.substr(input->parts().host.begin, | 87 UTF16ToUTF8(input_text.substr(input->parts().host.begin, |
88 input->parts().host.len)); | 88 input->parts().host.len)); |
89 const url_parse::Parsed& parts = | 89 const url_parse::Parsed& parts = |
90 canonical_gurl.parsed_for_possibly_invalid_spec(); | 90 canonical_gurl.parsed_for_possibly_invalid_spec(); |
91 // parts.host must not be empty when HostIsIPAddress() is true. | 91 // parts.host must not be empty when HostIsIPAddress() is true. |
92 DCHECK(parts.host.is_nonempty()); | 92 DCHECK(parts.host.is_nonempty()); |
93 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | 93 canonical_gurl_str.replace(parts.host.begin, parts.host.len, |
94 original_hostname); | 94 original_hostname); |
95 } | 95 } |
96 string16 output = UTF8ToUTF16(canonical_gurl_str); | 96 base::string16 output = UTF8ToUTF16(canonical_gurl_str); |
97 // Don't prepend a scheme when the user didn't have one. Since the fixer | 97 // Don't prepend a scheme when the user didn't have one. Since the fixer |
98 // upper only prepends the "http" scheme, that's all we need to check for. | 98 // upper only prepends the "http" scheme, that's all we need to check for. |
99 if (!AutocompleteInput::HasHTTPScheme(input_text)) | 99 if (!AutocompleteInput::HasHTTPScheme(input_text)) |
100 TrimHttpPrefix(&output); | 100 TrimHttpPrefix(&output); |
101 | 101 |
102 // Make the number of trailing slashes on the output exactly match the input. | 102 // Make the number of trailing slashes on the output exactly match the input. |
103 // Examples of why not doing this would matter: | 103 // Examples of why not doing this would matter: |
104 // * The user types "a" and has this fixed up to "a/". Now no other sites | 104 // * The user types "a" and has this fixed up to "a/". Now no other sites |
105 // beginning with "a" will match. | 105 // beginning with "a" will match. |
106 // * The user types "file:" and has this fixed up to "file://". Now inline | 106 // * The user types "file:" and has this fixed up to "file://". Now inline |
107 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." | 107 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." |
108 // instead of "file:///b..." | 108 // instead of "file:///b..." |
109 // * The user types "http:/" and has this fixed up to "http:". Now inline | 109 // * The user types "http:/" and has this fixed up to "http:". Now inline |
110 // autocomplete will append too many slashes, resulting in e.g. | 110 // autocomplete will append too many slashes, resulting in e.g. |
111 // "http:///c..." instead of "http://c...". | 111 // "http:///c..." instead of "http://c...". |
112 // NOTE: We do this after calling TrimHttpPrefix() since that can strip | 112 // NOTE: We do this after calling TrimHttpPrefix() since that can strip |
113 // trailing slashes (if the scheme is the only thing in the input). It's not | 113 // trailing slashes (if the scheme is the only thing in the input). It's not |
114 // clear that the result of fixup really matters in this case, but there's no | 114 // clear that the result of fixup really matters in this case, but there's no |
115 // harm in making sure. | 115 // harm in making sure. |
116 const size_t last_input_nonslash = | 116 const size_t last_input_nonslash = |
117 input_text.find_last_not_of(ASCIIToUTF16("/\\")); | 117 input_text.find_last_not_of(ASCIIToUTF16("/\\")); |
118 const size_t num_input_slashes = (last_input_nonslash == string16::npos) ? | 118 const size_t num_input_slashes = |
| 119 (last_input_nonslash == base::string16::npos) ? |
119 input_text.length() : (input_text.length() - 1 - last_input_nonslash); | 120 input_text.length() : (input_text.length() - 1 - last_input_nonslash); |
120 const size_t last_output_nonslash = | 121 const size_t last_output_nonslash = |
121 output.find_last_not_of(ASCIIToUTF16("/\\")); | 122 output.find_last_not_of(ASCIIToUTF16("/\\")); |
122 const size_t num_output_slashes = | 123 const size_t num_output_slashes = |
123 (last_output_nonslash == string16::npos) ? | 124 (last_output_nonslash == base::string16::npos) ? |
124 output.length() : (output.length() - 1 - last_output_nonslash); | 125 output.length() : (output.length() - 1 - last_output_nonslash); |
125 if (num_output_slashes < num_input_slashes) | 126 if (num_output_slashes < num_input_slashes) |
126 output.append(num_input_slashes - num_output_slashes, '/'); | 127 output.append(num_input_slashes - num_output_slashes, '/'); |
127 else if (num_output_slashes > num_input_slashes) | 128 else if (num_output_slashes > num_input_slashes) |
128 output.erase(output.length() - num_output_slashes + num_input_slashes); | 129 output.erase(output.length() - num_output_slashes + num_input_slashes); |
129 | 130 |
130 url_parse::Parsed parts; | 131 url_parse::Parsed parts; |
131 URLFixerUpper::SegmentURL(output, &parts); | 132 URLFixerUpper::SegmentURL(output, &parts); |
132 input->UpdateText(output, string16::npos, parts); | 133 input->UpdateText(output, base::string16::npos, parts); |
133 return !output.empty(); | 134 return !output.empty(); |
134 } | 135 } |
135 | 136 |
136 // static | 137 // static |
137 size_t HistoryProvider::TrimHttpPrefix(string16* url) { | 138 size_t HistoryProvider::TrimHttpPrefix(base::string16* url) { |
138 // Find any "http:". | 139 // Find any "http:". |
139 if (!AutocompleteInput::HasHTTPScheme(*url)) | 140 if (!AutocompleteInput::HasHTTPScheme(*url)) |
140 return 0; | 141 return 0; |
141 size_t scheme_pos = | 142 size_t scheme_pos = |
142 url->find(ASCIIToUTF16(content::kHttpScheme) + char16(':')); | 143 url->find(ASCIIToUTF16(content::kHttpScheme) + char16(':')); |
143 DCHECK_NE(string16::npos, scheme_pos); | 144 DCHECK_NE(base::string16::npos, scheme_pos); |
144 | 145 |
145 // Erase scheme plus up to two slashes. | 146 // Erase scheme plus up to two slashes. |
146 size_t prefix_end = scheme_pos + strlen(content::kHttpScheme) + 1; | 147 size_t prefix_end = scheme_pos + strlen(content::kHttpScheme) + 1; |
147 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); |
148 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | 149 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) |
149 ++prefix_end; | 150 ++prefix_end; |
150 url->erase(scheme_pos, prefix_end - scheme_pos); | 151 url->erase(scheme_pos, prefix_end - scheme_pos); |
151 return (scheme_pos == 0) ? prefix_end : 0; | 152 return (scheme_pos == 0) ? prefix_end : 0; |
152 } | 153 } |
153 | 154 |
(...skipping 30 matching lines...) Expand all Loading... |
184 offset += matches[i].length; | 185 offset += matches[i].length; |
185 ++i; | 186 ++i; |
186 } while ((i < match_count) && (offset == matches[i].offset)); | 187 } while ((i < match_count) && (offset == matches[i].offset)); |
187 if (offset < text_length) | 188 if (offset < text_length) |
188 spans.push_back(ACMatchClassification(offset, url_style)); | 189 spans.push_back(ACMatchClassification(offset, url_style)); |
189 } | 190 } |
190 | 191 |
191 return spans; | 192 return spans; |
192 } | 193 } |
193 | 194 |
OLD | NEW |