Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / autocomplete / history_provider.cc
blobd0763de298d24a0e91109e9d8560e313488f5e36
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.
5 #include "chrome/browser/autocomplete/history_provider.h"
7 #include <string>
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"
21 HistoryProvider::HistoryProvider(AutocompleteProviderListener* listener,
22 Profile* profile,
23 AutocompleteProvider::Type type)
24 : AutocompleteProvider(listener, profile, type) {
27 void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) {
28 DCHECK(done_);
29 DCHECK(profile_);
30 DCHECK(match.deletable);
32 HistoryService* const history_service =
33 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
35 // Delete the match from the history DB.
36 DCHECK(history_service);
37 DCHECK(match.destination_url.is_valid());
38 history_service->DeleteURL(match.destination_url);
39 DeleteMatchFromMatches(match);
42 HistoryProvider::~HistoryProvider() {}
44 void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) {
45 bool found = false;
46 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
47 if (i->destination_url == match.destination_url && i->type == match.type) {
48 found = true;
49 if (i->is_history_what_you_typed_match || i->starred) {
50 // We can't get rid of What-You-Typed or Bookmarked matches,
51 // but we can make them look like they have no backing data.
52 i->deletable = false;
53 i->description.clear();
54 i->description_class.clear();
55 } else {
56 matches_.erase(i);
58 break;
61 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches";
62 listener_->OnProviderUpdate(true);
65 // static
66 bool HistoryProvider::PreventInlineAutocomplete(
67 const AutocompleteInput& input) {
68 return input.prevent_inline_autocomplete() ||
69 (!input.text().empty() &&
70 IsWhitespace(input.text()[input.text().length() - 1]));
73 // static
74 ACMatchClassifications HistoryProvider::SpansFromTermMatch(
75 const history::TermMatches& matches,
76 size_t text_length,
77 bool is_url) {
78 ACMatchClassification::Style url_style =
79 is_url ? ACMatchClassification::URL : ACMatchClassification::NONE;
80 ACMatchClassifications spans;
81 if (matches.empty()) {
82 if (text_length)
83 spans.push_back(ACMatchClassification(0, url_style));
84 return spans;
86 if (matches[0].offset)
87 spans.push_back(ACMatchClassification(0, url_style));
88 size_t match_count = matches.size();
89 for (size_t i = 0; i < match_count;) {
90 size_t offset = matches[i].offset;
91 spans.push_back(ACMatchClassification(offset,
92 ACMatchClassification::MATCH | url_style));
93 // Skip all adjacent matches.
94 do {
95 offset += matches[i].length;
96 ++i;
97 } while ((i < match_count) && (offset == matches[i].offset));
98 if (offset < text_length)
99 spans.push_back(ACMatchClassification(offset, url_style));
102 return spans;