cc: Convert LTHCommon tests to LayerImpl
[chromium-blink-merge.git] / components / omnibox / browser / history_provider.cc
blob004d542b22a562689d60a8db0e036b93000fe77b
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 "components/omnibox/browser/history_provider.h"
7 #include <string>
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/bookmarks/browser/bookmark_model.h"
12 #include "components/history/core/browser/history_service.h"
13 #include "components/omnibox/browser/autocomplete_input.h"
14 #include "components/omnibox/browser/autocomplete_match.h"
15 #include "components/omnibox/browser/in_memory_url_index_types.h"
16 #include "url/url_util.h"
18 using bookmarks::BookmarkModel;
20 void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) {
21 DCHECK(done_);
22 DCHECK(client_);
23 DCHECK(match.deletable);
25 history::HistoryService* const history_service = client_->GetHistoryService();
27 // Delete the underlying URL along with all its visits from the history DB.
28 // The resulting HISTORY_URLS_DELETED notification will also cause all caches
29 // and indices to drop any data they might have stored pertaining to the URL.
30 DCHECK(history_service);
31 DCHECK(match.destination_url.is_valid());
32 history_service->DeleteURL(match.destination_url);
34 DeleteMatchFromMatches(match);
37 // static
38 bool HistoryProvider::PreventInlineAutocomplete(
39 const AutocompleteInput& input) {
40 return input.prevent_inline_autocomplete() ||
41 (!input.text().empty() &&
42 base::IsUnicodeWhitespace(input.text()[input.text().length() - 1]));
45 HistoryProvider::HistoryProvider(AutocompleteProvider::Type type,
46 AutocompleteProviderClient* client)
47 : AutocompleteProvider(type), client_(client) {
50 HistoryProvider::~HistoryProvider() {}
52 void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) {
53 bool found = false;
54 BookmarkModel* bookmark_model = client_->GetBookmarkModel();
55 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
56 if (i->destination_url == match.destination_url && i->type == match.type) {
57 found = true;
58 if ((i->type == AutocompleteMatchType::URL_WHAT_YOU_TYPED) ||
59 (bookmark_model &&
60 bookmark_model->IsBookmarked(i->destination_url))) {
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);
69 break;
72 DCHECK(found) << "Asked to delete a URL that isn't in our set of matches";
75 // static
76 ACMatchClassifications HistoryProvider::SpansFromTermMatch(
77 const 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;
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));
104 return spans;