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 HistoryProvider::HistoryProvider(AutocompleteProviderListener
* listener
,
23 AutocompleteProvider::Type type
)
24 : AutocompleteProvider(listener
, profile
, type
) {
27 void HistoryProvider::DeleteMatch(const AutocompleteMatch
& match
) {
30 DCHECK(match
.deletable
);
32 HistoryService
* const history_service
=
33 HistoryServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
);
35 // Delete the match from the history DB.
36 DCHECK(history_service
);
37 DCHECK(match
.destination_url
.is_valid());
38 history_service
->DeleteURL(match
.destination_url
);
39 DeleteMatchFromMatches(match
);
42 HistoryProvider::~HistoryProvider() {}
44 void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch
& match
) {
46 for (ACMatches::iterator
i(matches_
.begin()); i
!= matches_
.end(); ++i
) {
47 if (i
->destination_url
== match
.destination_url
&& i
->type
== match
.type
) {
49 if (i
->is_history_what_you_typed_match
|| i
->starred
) {
50 // We can't get rid of What-You-Typed or Bookmarked matches,
51 // but we can make them look like they have no backing data.
53 i
->description
.clear();
54 i
->description_class
.clear();
61 DCHECK(found
) << "Asked to delete a URL that isn't in our set of matches";
62 listener_
->OnProviderUpdate(true);
66 bool HistoryProvider::PreventInlineAutocomplete(
67 const AutocompleteInput
& input
) {
68 return input
.prevent_inline_autocomplete() ||
69 (!input
.text().empty() &&
70 IsWhitespace(input
.text()[input
.text().length() - 1]));
74 ACMatchClassifications
HistoryProvider::SpansFromTermMatch(
75 const history::TermMatches
& matches
,
78 ACMatchClassification::Style url_style
=
79 is_url
? ACMatchClassification::URL
: ACMatchClassification::NONE
;
80 ACMatchClassifications spans
;
81 if (matches
.empty()) {
83 spans
.push_back(ACMatchClassification(0, url_style
));
86 if (matches
[0].offset
)
87 spans
.push_back(ACMatchClassification(0, url_style
));
88 size_t match_count
= matches
.size();
89 for (size_t i
= 0; i
< match_count
;) {
90 size_t offset
= matches
[i
].offset
;
91 spans
.push_back(ACMatchClassification(offset
,
92 ACMatchClassification::MATCH
| url_style
));
93 // Skip all adjacent matches.
95 offset
+= matches
[i
].length
;
97 } while ((i
< match_count
) && (offset
== matches
[i
].offset
));
98 if (offset
< text_length
)
99 spans
.push_back(ACMatchClassification(offset
, url_style
));