history_provider.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/string_util.h" 10#include "base/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.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 "googleurl/src/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 // NOTE: This purposefully doesn't take input.desired_tld() into account; if 71 // it did, then holding "ctrl" would change all the results from the provider, 72 // not just the What You Typed Result. 73 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), 74 std::string())); 75 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); 76 if (canonical_gurl_str.empty()) { 77 // This probably won't happen, but there are no guarantees. 78 return false; 79 } 80 81 // If the user types a number, GURL will convert it to a dotted quad. 82 // However, if the parser did not mark this as a URL, then the user probably 83 // didn't intend this interpretation. Since this can break history matching 84 // for hostname beginning with numbers (e.g. input of "17173" will be matched 85 // against "0.0.67.21" instead of the original "17173", failing to find 86 // "17173.com"), swap the original hostname in for the fixed-up one. 87 if ((input->type() != AutocompleteInput::URL) && 88 canonical_gurl.HostIsIPAddress()) { 89 std::string original_hostname = 90 UTF16ToUTF8(input_text.substr(input->parts().host.begin, 91 input->parts().host.len)); 92 const url_parse::Parsed& parts = 93 canonical_gurl.parsed_for_possibly_invalid_spec(); 94 // parts.host must not be empty when HostIsIPAddress() is true. 95 DCHECK(parts.host.is_nonempty()); 96 canonical_gurl_str.replace(parts.host.begin, parts.host.len, 97 original_hostname); 98 } 99 string16 output = UTF8ToUTF16(canonical_gurl_str); 100 // Don't prepend a scheme when the user didn't have one. Since the fixer 101 // upper only prepends the "http" scheme, that's all we need to check for. 102 if (canonical_gurl.SchemeIs(chrome::kHttpScheme) && 103 !url_util::FindAndCompareScheme(UTF16ToUTF8(input_text), 104 chrome::kHttpScheme, NULL)) 105 TrimHttpPrefix(&output); 106 107 // Make the number of trailing slashes on the output exactly match the input. 108 // Examples of why not doing this would matter: 109 // * The user types "a" and has this fixed up to "a/". Now no other sites 110 // beginning with "a" will match. 111 // * The user types "file:" and has this fixed up to "file://". Now inline 112 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." 113 // instead of "file:///b..." 114 // * The user types "http:/" and has this fixed up to "http:". Now inline 115 // autocomplete will append too many slashes, resulting in e.g. 116 // "http:///c..." instead of "http://c...". 117 // NOTE: We do this after calling TrimHttpPrefix() since that can strip 118 // trailing slashes (if the scheme is the only thing in the input). It's not 119 // clear that the result of fixup really matters in this case, but there's no 120 // harm in making sure. 121 const size_t last_input_nonslash = 122 input_text.find_last_not_of(ASCIIToUTF16("/\\")); 123 const size_t num_input_slashes = (last_input_nonslash == string16::npos) ? 124 input_text.length() : (input_text.length() - 1 - last_input_nonslash); 125 const size_t last_output_nonslash = 126 output.find_last_not_of(ASCIIToUTF16("/\\")); 127 const size_t num_output_slashes = 128 (last_output_nonslash == string16::npos) ? 129 output.length() : (output.length() - 1 - last_output_nonslash); 130 if (num_output_slashes < num_input_slashes) 131 output.append(num_input_slashes - num_output_slashes, '/'); 132 else if (num_output_slashes > num_input_slashes) 133 output.erase(output.length() - num_output_slashes + num_input_slashes); 134 135 url_parse::Parsed parts; 136 URLFixerUpper::SegmentURL(output, &parts); 137 input->UpdateText(output, parts); 138 return !output.empty(); 139} 140 141// static 142size_t HistoryProvider::TrimHttpPrefix(string16* url) { 143 // Find any "http:". 144 if (!HasHTTPScheme(*url)) 145 return 0; 146 size_t scheme_pos = 147 url->find(ASCIIToUTF16(chrome::kHttpScheme) + char16(':')); 148 DCHECK_NE(string16::npos, scheme_pos); 149 150 // Erase scheme plus up to two slashes. 151 size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; 152 const size_t after_slashes = std::min(url->length(), prefix_end + 2); 153 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) 154 ++prefix_end; 155 url->erase(scheme_pos, prefix_end - scheme_pos); 156 return (scheme_pos == 0) ? prefix_end : 0; 157} 158 159// static 160bool HistoryProvider::PreventInlineAutocomplete( 161 const AutocompleteInput& input) { 162 return input.prevent_inline_autocomplete() || 163 always_prevent_inline_autocomplete_ || 164 (!input.text().empty() && 165 IsWhitespace(input.text()[input.text().length() - 1])); 166} 167