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/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
) {
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
);
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
,
46 AutocompleteProvider::Type type
)
47 : AutocompleteProvider(listener
, profile
, type
) {
50 HistoryProvider::~HistoryProvider() {}
52 void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch
& match
) {
54 for (ACMatches::iterator
i(matches_
.begin()); i
!= matches_
.end(); ++i
) {
55 if (i
->destination_url
== match
.destination_url
&& i
->type
== match
.type
) {
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.
61 i
->description
.clear();
62 i
->description_class
.clear();
69 DCHECK(found
) << "Asked to delete a URL that isn't in our set of matches";
73 ACMatchClassifications
HistoryProvider::SpansFromTermMatch(
74 const history::TermMatches
& matches
,
77 ACMatchClassification::Style url_style
=
78 is_url
? ACMatchClassification::URL
: ACMatchClassification::NONE
;
79 ACMatchClassifications spans
;
80 if (matches
.empty()) {
82 spans
.push_back(ACMatchClassification(0, url_style
));
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.
94 offset
+= matches
[i
].length
;
96 } while ((i
< match_count
) && (offset
== matches
[i
].offset
));
97 if (offset
< text_length
)
98 spans
.push_back(ACMatchClassification(offset
, url_style
));