| 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" |
| 11 #include "chrome/browser/autocomplete/autocomplete_input.h" | 11 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 14 #include "chrome/browser/history/history_service.h" | 14 #include "chrome/browser/history/history_service.h" |
| 15 #include "chrome/browser/history/history_service_factory.h" | 15 #include "chrome/browser/history/history_service_factory.h" |
| 16 #include "chrome/browser/history/in_memory_url_index_types.h" | 16 #include "chrome/browser/history/in_memory_url_index_types.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/net/url_fixer_upper.h" | |
| 19 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 20 #include "url/url_util.h" | 19 #include "url/url_util.h" |
| 21 | 20 |
| 22 HistoryProvider::HistoryProvider(AutocompleteProviderListener* listener, | 21 HistoryProvider::HistoryProvider(AutocompleteProviderListener* listener, |
| 23 Profile* profile, | 22 Profile* profile, |
| 24 AutocompleteProvider::Type type) | 23 AutocompleteProvider::Type type) |
| 25 : AutocompleteProvider(listener, profile, type) { | 24 : AutocompleteProvider(listener, profile, type) { |
| 26 } | 25 } |
| 27 | 26 |
| 28 void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) { | 27 void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 57 matches_.erase(i); | 56 matches_.erase(i); |
| 58 } | 57 } |
| 59 break; | 58 break; |
| 60 } | 59 } |
| 61 } | 60 } |
| 62 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; | 61 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; |
| 63 listener_->OnProviderUpdate(true); | 62 listener_->OnProviderUpdate(true); |
| 64 } | 63 } |
| 65 | 64 |
| 66 // static | 65 // static |
| 67 bool HistoryProvider::FixupUserInput(AutocompleteInput* input) { | |
| 68 const base::string16& input_text = input->text(); | |
| 69 // Fixup and canonicalize user input. | |
| 70 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), | |
| 71 std::string())); | |
| 72 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | |
| 73 if (canonical_gurl_str.empty()) { | |
| 74 // This probably won't happen, but there are no guarantees. | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 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 | |
| 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 | |
| 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. | |
| 84 if ((input->type() != AutocompleteInput::URL) && | |
| 85 canonical_gurl.HostIsIPAddress()) { | |
| 86 std::string original_hostname = | |
| 87 UTF16ToUTF8(input_text.substr(input->parts().host.begin, | |
| 88 input->parts().host.len)); | |
| 89 const url_parse::Parsed& parts = | |
| 90 canonical_gurl.parsed_for_possibly_invalid_spec(); | |
| 91 // parts.host must not be empty when HostIsIPAddress() is true. | |
| 92 DCHECK(parts.host.is_nonempty()); | |
| 93 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | |
| 94 original_hostname); | |
| 95 } | |
| 96 base::string16 output = UTF8ToUTF16(canonical_gurl_str); | |
| 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. | |
| 99 if (!AutocompleteInput::HasHTTPScheme(input_text)) | |
| 100 TrimHttpPrefix(&output); | |
| 101 | |
| 102 // Make the number of trailing slashes on the output exactly match the input. | |
| 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 | |
| 105 // beginning with "a" will match. | |
| 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..." | |
| 108 // instead of "file:///b..." | |
| 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. | |
| 111 // "http:///c..." instead of "http://c...". | |
| 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 | |
| 114 // clear that the result of fixup really matters in this case, but there's no | |
| 115 // harm in making sure. | |
| 116 const size_t last_input_nonslash = | |
| 117 input_text.find_last_not_of(ASCIIToUTF16("/\\")); | |
| 118 const size_t num_input_slashes = | |
| 119 (last_input_nonslash == base::string16::npos) ? | |
| 120 input_text.length() : (input_text.length() - 1 - last_input_nonslash); | |
| 121 const size_t last_output_nonslash = | |
| 122 output.find_last_not_of(ASCIIToUTF16("/\\")); | |
| 123 const size_t num_output_slashes = | |
| 124 (last_output_nonslash == base::string16::npos) ? | |
| 125 output.length() : (output.length() - 1 - last_output_nonslash); | |
| 126 if (num_output_slashes < num_input_slashes) | |
| 127 output.append(num_input_slashes - num_output_slashes, '/'); | |
| 128 else if (num_output_slashes > num_input_slashes) | |
| 129 output.erase(output.length() - num_output_slashes + num_input_slashes); | |
| 130 | |
| 131 url_parse::Parsed parts; | |
| 132 URLFixerUpper::SegmentURL(output, &parts); | |
| 133 input->UpdateText(output, base::string16::npos, parts); | |
| 134 return !output.empty(); | |
| 135 } | |
| 136 | |
| 137 // static | |
| 138 size_t HistoryProvider::TrimHttpPrefix(base::string16* url) { | |
| 139 // Find any "http:". | |
| 140 if (!AutocompleteInput::HasHTTPScheme(*url)) | |
| 141 return 0; | |
| 142 size_t scheme_pos = | |
| 143 url->find(ASCIIToUTF16(content::kHttpScheme) + char16(':')); | |
| 144 DCHECK_NE(base::string16::npos, scheme_pos); | |
| 145 | |
| 146 // Erase scheme plus up to two slashes. | |
| 147 size_t prefix_end = scheme_pos + strlen(content::kHttpScheme) + 1; | |
| 148 const size_t after_slashes = std::min(url->length(), prefix_end + 2); | |
| 149 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | |
| 150 ++prefix_end; | |
| 151 url->erase(scheme_pos, prefix_end - scheme_pos); | |
| 152 return (scheme_pos == 0) ? prefix_end : 0; | |
| 153 } | |
| 154 | |
| 155 // static | |
| 156 bool HistoryProvider::PreventInlineAutocomplete( | 66 bool HistoryProvider::PreventInlineAutocomplete( |
| 157 const AutocompleteInput& input) { | 67 const AutocompleteInput& input) { |
| 158 return input.prevent_inline_autocomplete() || | 68 return input.prevent_inline_autocomplete() || |
| 159 (!input.text().empty() && | 69 (!input.text().empty() && |
| 160 IsWhitespace(input.text()[input.text().length() - 1])); | 70 IsWhitespace(input.text()[input.text().length() - 1])); |
| 161 } | 71 } |
| 162 | 72 |
| 163 // static | 73 // static |
| 164 ACMatchClassifications HistoryProvider::SpansFromTermMatch( | 74 ACMatchClassifications HistoryProvider::SpansFromTermMatch( |
| 165 const history::TermMatches& matches, | 75 const history::TermMatches& matches, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 184 do { | 94 do { |
| 185 offset += matches[i].length; | 95 offset += matches[i].length; |
| 186 ++i; | 96 ++i; |
| 187 } while ((i < match_count) && (offset == matches[i].offset)); | 97 } while ((i < match_count) && (offset == matches[i].offset)); |
| 188 if (offset < text_length) | 98 if (offset < text_length) |
| 189 spans.push_back(ACMatchClassification(offset, url_style)); | 99 spans.push_back(ACMatchClassification(offset, url_style)); |
| 190 } | 100 } |
| 191 | 101 |
| 192 return spans; | 102 return spans; |
| 193 } | 103 } |
| 194 | |
| OLD | NEW |