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