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"
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
) {
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
);
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
) {
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
) {
58 if ((i
->type
== AutocompleteMatchType::URL_WHAT_YOU_TYPED
) ||
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.
64 i
->description
.clear();
65 i
->description_class
.clear();
72 DCHECK(found
) << "Asked to delete a URL that isn't in our set of matches";
76 ACMatchClassifications
HistoryProvider::SpansFromTermMatch(
77 const TermMatches
& matches
,
80 ACMatchClassification::Style url_style
=
81 is_url
? ACMatchClassification::URL
: ACMatchClassification::NONE
;
82 ACMatchClassifications spans
;
83 if (matches
.empty()) {
85 spans
.push_back(ACMatchClassification(0, url_style
));
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.
97 offset
+= matches
[i
].length
;
99 } while ((i
< match_count
) && (offset
== matches
[i
].offset
));
100 if (offset
< text_length
)
101 spans
.push_back(ACMatchClassification(offset
, url_style
));