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"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/autocomplete/in_memory_url_index_types.h"
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
13 #include "chrome/browser/history/history_service_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/url_constants.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/history/core/browser/history_service.h"
18 #include "components/omnibox/autocomplete_input.h"
19 #include "components/omnibox/autocomplete_match.h"
20 #include "url/url_util.h"
22 using bookmarks::BookmarkModel
;
24 void HistoryProvider::DeleteMatch(const AutocompleteMatch
& match
) {
27 DCHECK(match
.deletable
);
29 history::HistoryService
* const history_service
=
30 HistoryServiceFactory::GetForProfile(profile_
,
31 ServiceAccessType::EXPLICIT_ACCESS
);
33 // Delete the underlying URL along with all its visits from the history DB.
34 // The resulting HISTORY_URLS_DELETED notification will also cause all caches
35 // and indices to drop any data they might have stored pertaining to the URL.
36 DCHECK(history_service
);
37 DCHECK(match
.destination_url
.is_valid());
38 history_service
->DeleteURL(match
.destination_url
);
40 DeleteMatchFromMatches(match
);
44 bool HistoryProvider::PreventInlineAutocomplete(
45 const AutocompleteInput
& input
) {
46 return input
.prevent_inline_autocomplete() ||
47 (!input
.text().empty() &&
48 IsWhitespace(input
.text()[input
.text().length() - 1]));
51 HistoryProvider::HistoryProvider(Profile
* profile
,
52 AutocompleteProvider::Type type
)
53 : AutocompleteProvider(type
),
57 HistoryProvider::~HistoryProvider() {}
59 void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch
& match
) {
61 BookmarkModel
* bookmark_model
= BookmarkModelFactory::GetForProfile(profile_
);
62 for (ACMatches::iterator
i(matches_
.begin()); i
!= matches_
.end(); ++i
) {
63 if (i
->destination_url
== match
.destination_url
&& i
->type
== match
.type
) {
65 if (i
->is_history_what_you_typed_match
||
67 bookmark_model
->IsBookmarked(i
->destination_url
))) {
68 // We can't get rid of What-You-Typed or Bookmarked matches,
69 // but we can make them look like they have no backing data.
71 i
->description
.clear();
72 i
->description_class
.clear();
79 DCHECK(found
) << "Asked to delete a URL that isn't in our set of matches";
83 ACMatchClassifications
HistoryProvider::SpansFromTermMatch(
84 const TermMatches
& matches
,
87 ACMatchClassification::Style url_style
=
88 is_url
? ACMatchClassification::URL
: ACMatchClassification::NONE
;
89 ACMatchClassifications spans
;
90 if (matches
.empty()) {
92 spans
.push_back(ACMatchClassification(0, url_style
));
95 if (matches
[0].offset
)
96 spans
.push_back(ACMatchClassification(0, url_style
));
97 size_t match_count
= matches
.size();
98 for (size_t i
= 0; i
< match_count
;) {
99 size_t offset
= matches
[i
].offset
;
100 spans
.push_back(ACMatchClassification(offset
,
101 ACMatchClassification::MATCH
| url_style
));
102 // Skip all adjacent matches.
104 offset
+= matches
[i
].length
;
106 } while ((i
< match_count
) && (offset
== matches
[i
].offset
));
107 if (offset
< text_length
)
108 spans
.push_back(ACMatchClassification(offset
, url_style
));