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/shortcuts_backend.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/guid.h"
14 #include "base/i18n/case_conversion.h"
15 #include "base/strings/string_util.h"
16 #include "base/thread_task_runner_handle.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 "components/omnibox/autocomplete_match_type.h"
21 #include "components/omnibox/autocomplete_result.h"
22 #include "components/omnibox/base_search_provider.h"
23 #include "components/omnibox/omnibox_log.h"
24 #include "components/omnibox/shortcuts_database.h"
28 // Takes Match classification vector and removes all matched positions,
29 // compacting repetitions if necessary.
30 std::string
StripMatchMarkers(const ACMatchClassifications
& matches
) {
31 ACMatchClassifications unmatched
;
32 for (ACMatchClassifications::const_iterator
i(matches
.begin());
33 i
!= matches
.end(); ++i
) {
34 AutocompleteMatch::AddLastClassificationIfNecessary(
35 &unmatched
, i
->offset
, i
->style
& ~ACMatchClassification::MATCH
);
37 return AutocompleteMatch::ClassificationsToString(unmatched
);
40 // Normally shortcuts have the same match type as the original match they were
41 // created from, but for certain match types, we should modify the shortcut's
42 // type slightly to reflect that the origin of the shortcut is historical.
43 AutocompleteMatch::Type
GetTypeForShortcut(AutocompleteMatch::Type type
) {
45 case AutocompleteMatchType::URL_WHAT_YOU_TYPED
:
46 case AutocompleteMatchType::NAVSUGGEST
:
47 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED
:
48 return AutocompleteMatchType::HISTORY_URL
;
50 case AutocompleteMatchType::SEARCH_OTHER_ENGINE
:
54 return AutocompleteMatch::IsSearchType(type
) ?
55 AutocompleteMatchType::SEARCH_HISTORY
: type
;
62 // ShortcutsBackend -----------------------------------------------------------
64 ShortcutsBackend::ShortcutsBackend(
65 TemplateURLService
* template_url_service
,
66 scoped_ptr
<SearchTermsData
> search_terms_data
,
67 history::HistoryService
* history_service
,
68 scoped_refptr
<base::SequencedTaskRunner
> db_runner
,
69 base::FilePath database_path
,
71 : template_url_service_(template_url_service
),
72 search_terms_data_(search_terms_data
.Pass()),
73 current_state_(NOT_INITIALIZED
),
74 history_service_observer_(this),
75 main_runner_(base::ThreadTaskRunnerHandle::Get()),
76 db_runner_(db_runner
),
77 no_db_access_(suppress_db
) {
79 db_
= new ShortcutsDatabase(database_path
);
81 history_service_observer_
.Add(history_service
);
84 bool ShortcutsBackend::Init() {
85 if (current_state_
!= NOT_INITIALIZED
)
89 current_state_
= INITIALIZED
;
93 current_state_
= INITIALIZING
;
94 return db_runner_
->PostTask(
95 FROM_HERE
, base::Bind(&ShortcutsBackend::InitInternal
, this));
98 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL
& shortcut_url
) {
99 return initialized() && DeleteShortcutsWithURL(shortcut_url
, true);
102 bool ShortcutsBackend::DeleteShortcutsBeginningWithURL(
103 const GURL
& shortcut_url
) {
104 return initialized() && DeleteShortcutsWithURL(shortcut_url
, false);
107 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver
* obs
) {
108 observer_list_
.AddObserver(obs
);
111 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver
* obs
) {
112 observer_list_
.RemoveObserver(obs
);
115 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16
& text
,
116 const AutocompleteMatch
& match
) {
117 const base::string16
text_lowercase(base::i18n::ToLower(text
));
118 const base::Time
now(base::Time::Now());
119 for (ShortcutMap::const_iterator
it(
120 shortcuts_map_
.lower_bound(text_lowercase
));
121 it
!= shortcuts_map_
.end() &&
122 base::StartsWith(it
->first
, text_lowercase
, true);
124 if (match
.destination_url
== it
->second
.match_core
.destination_url
) {
125 UpdateShortcut(ShortcutsDatabase::Shortcut(
126 it
->second
.id
, text
, MatchToMatchCore(match
, template_url_service_
,
127 search_terms_data_
.get()),
128 now
, it
->second
.number_of_hits
+ 1));
132 AddShortcut(ShortcutsDatabase::Shortcut(
133 base::GenerateGUID(), text
,
134 MatchToMatchCore(match
, template_url_service_
, search_terms_data_
.get()),
138 ShortcutsBackend::~ShortcutsBackend() {
140 auto* db
= db_
.get();
143 if (!db_runner_
->ReleaseSoon(FROM_HERE
, db
))
149 ShortcutsDatabase::Shortcut::MatchCore
ShortcutsBackend::MatchToMatchCore(
150 const AutocompleteMatch
& match
,
151 TemplateURLService
* template_url_service
,
152 SearchTermsData
* search_terms_data
) {
153 const AutocompleteMatch::Type match_type
= GetTypeForShortcut(match
.type
);
154 const AutocompleteMatch
& normalized_match
=
155 AutocompleteMatch::IsSpecializedSearchType(match
.type
)
156 ? BaseSearchProvider::CreateSearchSuggestion(
157 match
.search_terms_args
->search_terms
, match_type
,
158 (match
.transition
== ui::PAGE_TRANSITION_KEYWORD
),
159 match
.GetTemplateURL(template_url_service
, false),
162 return ShortcutsDatabase::Shortcut::MatchCore(
163 normalized_match
.fill_into_edit
, normalized_match
.destination_url
,
164 normalized_match
.contents
,
165 StripMatchMarkers(normalized_match
.contents_class
),
166 normalized_match
.description
,
167 StripMatchMarkers(normalized_match
.description_class
),
168 normalized_match
.transition
, match_type
, normalized_match
.keyword
);
171 void ShortcutsBackend::ShutdownOnUIThread() {
172 history_service_observer_
.RemoveAll();
175 void ShortcutsBackend::OnURLsDeleted(history::HistoryService
* history_service
,
178 const history::URLRows
& deleted_rows
,
179 const std::set
<GURL
>& favicon_urls
) {
184 DeleteAllShortcuts();
188 ShortcutsDatabase::ShortcutIDs shortcut_ids
;
189 for (const auto& guid_pair
: guid_map_
) {
191 deleted_rows
.begin(), deleted_rows
.end(),
192 history::URLRow::URLRowHasURL(
193 guid_pair
.second
->second
.match_core
.destination_url
)) !=
194 deleted_rows
.end()) {
195 shortcut_ids
.push_back(guid_pair
.first
);
198 DeleteShortcutsWithIDs(shortcut_ids
);
201 void ShortcutsBackend::InitInternal() {
202 DCHECK(current_state_
== INITIALIZING
);
204 ShortcutsDatabase::GuidToShortcutMap shortcuts
;
205 db_
->LoadShortcuts(&shortcuts
);
206 temp_shortcuts_map_
.reset(new ShortcutMap
);
207 temp_guid_map_
.reset(new GuidMap
);
208 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator
it(
210 it
!= shortcuts
.end(); ++it
) {
211 (*temp_guid_map_
)[it
->first
] = temp_shortcuts_map_
->insert(
212 std::make_pair(base::i18n::ToLower(it
->second
.text
), it
->second
));
214 main_runner_
->PostTask(FROM_HERE
,
215 base::Bind(&ShortcutsBackend::InitCompleted
, this));
218 void ShortcutsBackend::InitCompleted() {
219 temp_guid_map_
->swap(guid_map_
);
220 temp_shortcuts_map_
->swap(shortcuts_map_
);
221 temp_shortcuts_map_
.reset(NULL
);
222 temp_guid_map_
.reset(NULL
);
223 current_state_
= INITIALIZED
;
224 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
225 OnShortcutsLoaded());
228 bool ShortcutsBackend::AddShortcut(
229 const ShortcutsDatabase::Shortcut
& shortcut
) {
232 DCHECK(guid_map_
.find(shortcut
.id
) == guid_map_
.end());
233 guid_map_
[shortcut
.id
] = shortcuts_map_
.insert(
234 std::make_pair(base::i18n::ToLower(shortcut
.text
), shortcut
));
235 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
236 OnShortcutsChanged());
237 return no_db_access_
||
238 db_runner_
->PostTask(
240 base::Bind(base::IgnoreResult(&ShortcutsDatabase::AddShortcut
),
241 db_
.get(), shortcut
));
244 bool ShortcutsBackend::UpdateShortcut(
245 const ShortcutsDatabase::Shortcut
& shortcut
) {
248 GuidMap::iterator
it(guid_map_
.find(shortcut
.id
));
249 if (it
!= guid_map_
.end())
250 shortcuts_map_
.erase(it
->second
);
251 guid_map_
[shortcut
.id
] = shortcuts_map_
.insert(
252 std::make_pair(base::i18n::ToLower(shortcut
.text
), shortcut
));
253 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
254 OnShortcutsChanged());
255 return no_db_access_
||
256 db_runner_
->PostTask(
258 base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut
),
259 db_
.get(), shortcut
));
262 bool ShortcutsBackend::DeleteShortcutsWithIDs(
263 const ShortcutsDatabase::ShortcutIDs
& shortcut_ids
) {
266 for (size_t i
= 0; i
< shortcut_ids
.size(); ++i
) {
267 GuidMap::iterator
it(guid_map_
.find(shortcut_ids
[i
]));
268 if (it
!= guid_map_
.end()) {
269 shortcuts_map_
.erase(it
->second
);
273 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
274 OnShortcutsChanged());
275 return no_db_access_
||
276 db_runner_
->PostTask(
279 base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithIDs
),
280 db_
.get(), shortcut_ids
));
283 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL
& url
,
285 const std::string
& url_spec
= url
.spec();
286 ShortcutsDatabase::ShortcutIDs shortcut_ids
;
287 for (GuidMap::iterator
it(guid_map_
.begin()); it
!= guid_map_
.end(); ) {
288 if (exact_match
? (it
->second
->second
.match_core
.destination_url
== url
)
289 : base::StartsWithASCII(
290 it
->second
->second
.match_core
.destination_url
.spec(),
292 shortcut_ids
.push_back(it
->first
);
293 shortcuts_map_
.erase(it
->second
);
294 guid_map_
.erase(it
++);
299 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
300 OnShortcutsChanged());
301 return no_db_access_
||
302 db_runner_
->PostTask(
305 base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithURL
),
306 db_
.get(), url_spec
));
309 bool ShortcutsBackend::DeleteAllShortcuts() {
312 shortcuts_map_
.clear();
314 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
315 OnShortcutsChanged());
316 return no_db_access_
||
317 db_runner_
->PostTask(
318 FROM_HERE
, base::Bind(base::IgnoreResult(
319 &ShortcutsDatabase::DeleteAllShortcuts
),