history_provider.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 3// found in the LICENSE file. 4 5#include "chrome/browser/autocomplete/history_provider.h" 6 7#include <string> 8 9#include "base/strings/string_util.h" 10#include "base/strings/utf_string_conversions.h" 11#include "chrome/browser/autocomplete/autocomplete_input.h" 12#include "chrome/browser/autocomplete/autocomplete_match.h" 13#include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 14#include "chrome/browser/history/history_service.h" 15#include "chrome/browser/history/history_service_factory.h" 16#include "chrome/browser/net/url_fixer_upper.h" 17#include "chrome/browser/profiles/profile.h" 18#include "chrome/common/url_constants.h" 19#include "url/url_util.h" 20 21HistoryProvider::HistoryProvider(AutocompleteProviderListener* listener, 22 Profile* profile, 23 AutocompleteProvider::Type type) 24 : AutocompleteProvider(listener, profile, type), 25 always_prevent_inline_autocomplete_(false) { 26} 27 28void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) { 29 DCHECK(done_); 30 DCHECK(profile_); 31 DCHECK(match.deletable); 32 33 HistoryService* const history_service = 34 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 35 36 // Delete the match from the history DB. 37 DCHECK(history_service); 38 DCHECK(match.destination_url.is_valid()); 39 history_service->DeleteURL(match.destination_url); 40 DeleteMatchFromMatches(match); 41} 42 43HistoryProvider::~HistoryProvider() {} 44 45void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) { 46 bool found = false; 47 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { 48 if (i->destination_url == match.destination_url && i->type == match.type) { 49 found = true; 50 if (i->is_history_what_you_typed_match || i->starred) { 51 // We can't get rid of What-You-Typed or Bookmarked matches, 52 // but we can make them look like they have no backing data. 53 i->deletable = false; 54 i->description.clear(); 55 i->description_class.clear(); 56 } else { 57 matches_.erase(i); 58 } 59 break; 60 } 61 } 62 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; 63 listener_->OnProviderUpdate(true); 64} 65 66// static 67bool HistoryProvider::FixupUserInput(AutocompleteInput* input) { 68 const 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 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 (canonical_gurl.SchemeIs(chrome::kHttpScheme) && 100 !url_util::FindAndCompareScheme(UTF16ToUTF8(input_text), 101 chrome::kHttpScheme, NULL)) 102 TrimHttpPrefix(&output); 103 104 // Make the number of trailing slashes on the output exactly match the input. 105 // Examples of why not doing this would matter: 106 // * The user types "a" and has this fixed up to "a/". Now no other sites 107 // beginning with "a" will match. 108 // * The user types "file:" and has this fixed up to "file://". Now inline 109 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." 110 // instead of "file:///b..." 111 // * The user types "http:/" and has this fixed up to "http:". Now inline 112 // autocomplete will append too many slashes, resulting in e.g. 113 // "http:///c..." instead of "http://c...". 114 // NOTE: We do this after calling TrimHttpPrefix() since that can strip 115 // trailing slashes (if the scheme is the only thing in the input). It's not 116 // clear that the result of fixup really matters in this case, but there's no 117 // harm in making sure. 118 const size_t last_input_nonslash = 119 input_text.find_last_not_of(ASCIIToUTF16("/\\")); 120 const size_t num_input_slashes = (last_input_nonslash == string16::npos) ? 121 input_text.length() : (input_text.length() - 1 - last_input_nonslash); 122 const size_t last_output_nonslash = 123 output.find_last_not_of(ASCIIToUTF16("/\\")); 124 const size_t num_output_slashes = 125 (last_output_nonslash == string16::npos) ? 126 output.length() : (output.length() - 1 - last_output_nonslash); 127 if (num_output_slashes < num_input_slashes) 128 output.append(num_input_slashes - num_output_slashes, '/'); 129 else if (num_output_slashes > num_input_slashes) 130 output.erase(output.length() - num_output_slashes + num_input_slashes); 131 132 url_parse::Parsed parts; 133 URLFixerUpper::SegmentURL(output, &parts); 134 input->UpdateText(output, string16::npos, parts); 135 return !output.empty(); 136} 137 138// static 139size_t HistoryProvider::TrimHttpPrefix(string16* url) { 140 // Find any "http:". 141 if (!HasHTTPScheme(*url)) 142 return 0; 143 size_t scheme_pos = 144 url->find(ASCIIToUTF16(chrome::kHttpScheme) + char16(':')); 145 DCHECK_NE(string16::npos, scheme_pos); 146 147 // Erase scheme plus up to two slashes. 148 size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; 149 const size_t after_slashes = std::min(url->length(), prefix_end + 2); 150 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) 151 ++prefix_end; 152 url->erase(scheme_pos, prefix_end - scheme_pos); 153 return (scheme_pos == 0) ? prefix_end : 0; 154} 155 156// static 157bool HistoryProvider::PreventInlineAutocomplete( 158 const AutocompleteInput& input) { 159 return input.prevent_inline_autocomplete() || 160 always_prevent_inline_autocomplete_ || 161 (!input.text().empty() && 162 IsWhitespace(input.text()[input.text().length() - 1])); 163} 164