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/history/in_memory_url_index_types.h" 17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/common/url_constants.h" 19 #include "url/url_util.h" 20 21 void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) { 22 DCHECK(done_); 23 DCHECK(profile_); 24 DCHECK(match.deletable); 25 26 HistoryService* const history_service = 27 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 28 29 // Delete the underlying URL along with all its visits from the history DB. 30 // The resulting HISTORY_URLS_DELETED notification will also cause all caches 31 // and indices to drop any data they might have stored pertaining to the URL. 32 DCHECK(history_service); 33 DCHECK(match.destination_url.is_valid()); 34 history_service->DeleteURL(match.destination_url); 35 36 DeleteMatchFromMatches(match); 37 } 38 39 // static 40 bool HistoryProvider::PreventInlineAutocomplete( 41 const AutocompleteInput& input) { 42 return input.prevent_inline_autocomplete() || 43 (!input.text().empty() && 44 IsWhitespace(input.text()[input.text().length() - 1])); 45 } 46 47 HistoryProvider::HistoryProvider(AutocompleteProviderListener* listener, 48 Profile* profile, 49 AutocompleteProvider::Type type) 50 : AutocompleteProvider(listener, profile, type) { 51 } 52 53 HistoryProvider::~HistoryProvider() {} 54 55 void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) { 56 bool found = false; 57 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { 58 if (i->destination_url == match.destination_url && i->type == match.type) { 59 found = true; 60 if (i->is_history_what_you_typed_match || i->starred) { 61 // We can't get rid of What-You-Typed or Bookmarked matches, 62 // but we can make them look like they have no backing data. 63 i->deletable = false; 64 i->description.clear(); 65 i->description_class.clear(); 66 } else { 67 matches_.erase(i); 68 } 69 break; 70 } 71 } 72 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches"; 73 } 74 75 // static 76 ACMatchClassifications HistoryProvider::SpansFromTermMatch( 77 const history::TermMatches& matches, 78 size_t text_length, 79 bool is_url) { 80 ACMatchClassification::Style url_style = 81 is_url ? ACMatchClassification::URL : ACMatchClassification::NONE; 82 ACMatchClassifications spans; 83 if (matches.empty()) { 84 if (text_length) 85 spans.push_back(ACMatchClassification(0, url_style)); 86 return spans; 87 } 88 if (matches[0].offset) 89 spans.push_back(ACMatchClassification(0, url_style)); 90 size_t match_count = matches.size(); 91 for (size_t i = 0; i < match_count;) { 92 size_t offset = matches[i].offset; 93 spans.push_back(ACMatchClassification(offset, 94 ACMatchClassification::MATCH | url_style)); 95 // Skip all adjacent matches. 96 do { 97 offset += matches[i].length; 98 ++i; 99 } while ((i < match_count) && (offset == matches[i].offset)); 100 if (offset < text_length) 101 spans.push_back(ACMatchClassification(offset, url_style)); 102 } 103 104 return spans; 105 } 106