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/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 "chrome/browser/autocomplete/shortcuts_database.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/omnibox/omnibox_log.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "components/history/core/browser/history_service.h"
25 #include "components/omnibox/autocomplete_input.h"
26 #include "components/omnibox/autocomplete_match.h"
27 #include "components/omnibox/autocomplete_match_type.h"
28 #include "components/omnibox/autocomplete_result.h"
29 #include "components/omnibox/base_search_provider.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_details.h"
32 #include "content/public/browser/notification_source.h"
34 #if defined(ENABLE_EXTENSIONS)
35 #include "extensions/browser/notification_types.h"
36 #include "extensions/common/extension.h"
39 using content::BrowserThread
;
43 // Takes Match classification vector and removes all matched positions,
44 // compacting repetitions if necessary.
45 std::string
StripMatchMarkers(const ACMatchClassifications
& matches
) {
46 ACMatchClassifications unmatched
;
47 for (ACMatchClassifications::const_iterator
i(matches
.begin());
48 i
!= matches
.end(); ++i
) {
49 AutocompleteMatch::AddLastClassificationIfNecessary(
50 &unmatched
, i
->offset
, i
->style
& ~ACMatchClassification::MATCH
);
52 return AutocompleteMatch::ClassificationsToString(unmatched
);
55 // Normally shortcuts have the same match type as the original match they were
56 // created from, but for certain match types, we should modify the shortcut's
57 // type slightly to reflect that the origin of the shortcut is historical.
58 AutocompleteMatch::Type
GetTypeForShortcut(AutocompleteMatch::Type type
) {
60 case AutocompleteMatchType::URL_WHAT_YOU_TYPED
:
61 case AutocompleteMatchType::NAVSUGGEST
:
62 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED
:
63 return AutocompleteMatchType::HISTORY_URL
;
65 case AutocompleteMatchType::SEARCH_OTHER_ENGINE
:
69 return AutocompleteMatch::IsSearchType(type
) ?
70 AutocompleteMatchType::SEARCH_HISTORY
: type
;
77 // ShortcutsBackend -----------------------------------------------------------
79 ShortcutsBackend::ShortcutsBackend(Profile
* profile
, bool suppress_db
)
81 current_state_(NOT_INITIALIZED
),
82 history_service_observer_(this),
83 no_db_access_(suppress_db
) {
85 db_
= new ShortcutsDatabase(
86 profile
->GetPath().Append(chrome::kShortcutsDatabaseName
));
88 // |profile| can be NULL in tests.
90 #if defined(ENABLE_EXTENSIONS)
91 notification_registrar_
.Add(
93 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
94 content::Source
<Profile
>(profile
));
96 history::HistoryService
* hs
= HistoryServiceFactory::GetForProfile(
97 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
99 history_service_observer_
.Add(hs
);
103 bool ShortcutsBackend::Init() {
104 if (current_state_
!= NOT_INITIALIZED
)
108 current_state_
= INITIALIZED
;
112 current_state_
= INITIALIZING
;
113 return BrowserThread::PostTask(BrowserThread::DB
, FROM_HERE
,
114 base::Bind(&ShortcutsBackend::InitInternal
, this));
117 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL
& shortcut_url
) {
118 return initialized() && DeleteShortcutsWithURL(shortcut_url
, true);
121 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver
* obs
) {
122 observer_list_
.AddObserver(obs
);
125 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver
* obs
) {
126 observer_list_
.RemoveObserver(obs
);
129 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16
& text
,
130 const AutocompleteMatch
& match
) {
131 const base::string16
text_lowercase(base::i18n::ToLower(text
));
132 const base::Time
now(base::Time::Now());
133 for (ShortcutMap::const_iterator
it(
134 shortcuts_map_
.lower_bound(text_lowercase
));
135 it
!= shortcuts_map_
.end() &&
136 base::StartsWith(it
->first
, text_lowercase
, true);
138 if (match
.destination_url
== it
->second
.match_core
.destination_url
) {
139 UpdateShortcut(ShortcutsDatabase::Shortcut(
140 it
->second
.id
, text
, MatchToMatchCore(match
, profile_
), now
,
141 it
->second
.number_of_hits
+ 1));
145 AddShortcut(ShortcutsDatabase::Shortcut(
146 base::GenerateGUID(), text
, MatchToMatchCore(match
, profile_
), now
, 1));
149 ShortcutsBackend::~ShortcutsBackend() {
151 auto* db
= db_
.get();
154 if (!BrowserThread::ReleaseSoon(BrowserThread::DB
, FROM_HERE
, db
))
160 ShortcutsDatabase::Shortcut::MatchCore
ShortcutsBackend::MatchToMatchCore(
161 const AutocompleteMatch
& match
,
163 const AutocompleteMatch::Type match_type
= GetTypeForShortcut(match
.type
);
164 TemplateURLService
* service
=
165 TemplateURLServiceFactory::GetForProfile(profile
);
166 const AutocompleteMatch
& normalized_match
=
167 AutocompleteMatch::IsSpecializedSearchType(match
.type
) ?
168 BaseSearchProvider::CreateSearchSuggestion(
169 match
.search_terms_args
->search_terms
, match_type
,
170 (match
.transition
== ui::PAGE_TRANSITION_KEYWORD
),
171 match
.GetTemplateURL(service
, false),
172 UIThreadSearchTermsData(profile
)) :
174 return ShortcutsDatabase::Shortcut::MatchCore(
175 normalized_match
.fill_into_edit
, normalized_match
.destination_url
,
176 normalized_match
.contents
,
177 StripMatchMarkers(normalized_match
.contents_class
),
178 normalized_match
.description
,
179 StripMatchMarkers(normalized_match
.description_class
),
180 normalized_match
.transition
, match_type
, normalized_match
.keyword
);
183 void ShortcutsBackend::ShutdownOnUIThread() {
184 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
185 BrowserThread::CurrentlyOn(BrowserThread::UI
));
186 notification_registrar_
.RemoveAll();
187 history_service_observer_
.RemoveAll();
190 void ShortcutsBackend::Observe(int type
,
191 const content::NotificationSource
& source
,
192 const content::NotificationDetails
& details
) {
193 #if defined(ENABLE_EXTENSIONS)
194 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
, type
);
198 // When an extension is unloaded, we want to remove any Shortcuts associated
200 DeleteShortcutsWithURL(content::Details
<extensions::UnloadedExtensionInfo
>(
201 details
)->extension
->url(),
206 void ShortcutsBackend::OnURLsDeleted(history::HistoryService
* history_service
,
209 const history::URLRows
& deleted_rows
,
210 const std::set
<GURL
>& favicon_urls
) {
215 DeleteAllShortcuts();
219 ShortcutsDatabase::ShortcutIDs shortcut_ids
;
220 for (const auto& guid_pair
: guid_map_
) {
222 deleted_rows
.begin(), deleted_rows
.end(),
223 history::URLRow::URLRowHasURL(
224 guid_pair
.second
->second
.match_core
.destination_url
)) !=
225 deleted_rows
.end()) {
226 shortcut_ids
.push_back(guid_pair
.first
);
229 DeleteShortcutsWithIDs(shortcut_ids
);
232 void ShortcutsBackend::InitInternal() {
233 DCHECK(current_state_
== INITIALIZING
);
235 ShortcutsDatabase::GuidToShortcutMap shortcuts
;
236 db_
->LoadShortcuts(&shortcuts
);
237 temp_shortcuts_map_
.reset(new ShortcutMap
);
238 temp_guid_map_
.reset(new GuidMap
);
239 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator
it(
241 it
!= shortcuts
.end(); ++it
) {
242 (*temp_guid_map_
)[it
->first
] = temp_shortcuts_map_
->insert(
243 std::make_pair(base::i18n::ToLower(it
->second
.text
), it
->second
));
245 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
246 base::Bind(&ShortcutsBackend::InitCompleted
, this));
249 void ShortcutsBackend::InitCompleted() {
250 temp_guid_map_
->swap(guid_map_
);
251 temp_shortcuts_map_
->swap(shortcuts_map_
);
252 temp_shortcuts_map_
.reset(NULL
);
253 temp_guid_map_
.reset(NULL
);
254 current_state_
= INITIALIZED
;
255 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
256 OnShortcutsLoaded());
259 bool ShortcutsBackend::AddShortcut(
260 const ShortcutsDatabase::Shortcut
& shortcut
) {
263 DCHECK(guid_map_
.find(shortcut
.id
) == guid_map_
.end());
264 guid_map_
[shortcut
.id
] = shortcuts_map_
.insert(
265 std::make_pair(base::i18n::ToLower(shortcut
.text
), shortcut
));
266 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
267 OnShortcutsChanged());
268 return no_db_access_
||
269 BrowserThread::PostTask(
270 BrowserThread::DB
, FROM_HERE
,
271 base::Bind(base::IgnoreResult(&ShortcutsDatabase::AddShortcut
),
272 db_
.get(), shortcut
));
275 bool ShortcutsBackend::UpdateShortcut(
276 const ShortcutsDatabase::Shortcut
& shortcut
) {
279 GuidMap::iterator
it(guid_map_
.find(shortcut
.id
));
280 if (it
!= guid_map_
.end())
281 shortcuts_map_
.erase(it
->second
);
282 guid_map_
[shortcut
.id
] = shortcuts_map_
.insert(
283 std::make_pair(base::i18n::ToLower(shortcut
.text
), shortcut
));
284 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
285 OnShortcutsChanged());
286 return no_db_access_
||
287 BrowserThread::PostTask(
288 BrowserThread::DB
, FROM_HERE
,
289 base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut
),
290 db_
.get(), shortcut
));
293 bool ShortcutsBackend::DeleteShortcutsWithIDs(
294 const ShortcutsDatabase::ShortcutIDs
& shortcut_ids
) {
297 for (size_t i
= 0; i
< shortcut_ids
.size(); ++i
) {
298 GuidMap::iterator
it(guid_map_
.find(shortcut_ids
[i
]));
299 if (it
!= guid_map_
.end()) {
300 shortcuts_map_
.erase(it
->second
);
304 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
305 OnShortcutsChanged());
306 return no_db_access_
||
307 BrowserThread::PostTask(
308 BrowserThread::DB
, FROM_HERE
,
310 base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithIDs
),
311 db_
.get(), shortcut_ids
));
314 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL
& url
,
316 const std::string
& url_spec
= url
.spec();
317 ShortcutsDatabase::ShortcutIDs shortcut_ids
;
318 for (GuidMap::iterator
it(guid_map_
.begin()); it
!= guid_map_
.end(); ) {
319 if (exact_match
? (it
->second
->second
.match_core
.destination_url
== url
)
320 : base::StartsWithASCII(
321 it
->second
->second
.match_core
.destination_url
.spec(),
323 shortcut_ids
.push_back(it
->first
);
324 shortcuts_map_
.erase(it
->second
);
325 guid_map_
.erase(it
++);
330 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
331 OnShortcutsChanged());
332 return no_db_access_
||
333 BrowserThread::PostTask(
334 BrowserThread::DB
, FROM_HERE
,
336 base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithURL
),
337 db_
.get(), url_spec
));
340 bool ShortcutsBackend::DeleteAllShortcuts() {
343 shortcuts_map_
.clear();
345 FOR_EACH_OBSERVER(ShortcutsBackendObserver
, observer_list_
,
346 OnShortcutsChanged());
347 return no_db_access_
||
348 BrowserThread::PostTask(
349 BrowserThread::DB
, FROM_HERE
,
351 base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts
),