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/chrome_notification_types.h"
17 #include "chrome/browser/history/history_notifications.h"
18 #include "chrome/browser/history/history_service.h"
19 #include "chrome/browser/history/shortcuts_database.h"
20 #include "chrome/browser/omnibox/omnibox_log.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/search_engines/template_url_service_factory.h"
23 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
24 #include "chrome/common/chrome_constants.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 no_db_access_(suppress_db) {
84 db_ = new history::ShortcutsDatabase(
85 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
87 // |profile| can be NULL in tests.
89 #if defined(ENABLE_EXTENSIONS)
90 notification_registrar_.Add(
92 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
93 content::Source<Profile>(profile));
95 notification_registrar_.Add(
96 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
97 content::Source<Profile>(profile));
101 bool ShortcutsBackend::Init() {
102 if (current_state_ != NOT_INITIALIZED)
106 current_state_ = INITIALIZED;
110 current_state_ = INITIALIZING;
111 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
112 base::Bind(&ShortcutsBackend::InitInternal, this));
115 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& shortcut_url) {
116 return initialized() && DeleteShortcutsWithURL(shortcut_url, true);
119 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
120 observer_list_.AddObserver(obs);
123 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
124 observer_list_.RemoveObserver(obs);
127 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text,
128 const AutocompleteMatch& match) {
129 const base::string16 text_lowercase(base::i18n::ToLower(text));
130 const base::Time now(base::Time::Now());
131 for (ShortcutMap::const_iterator it(
132 shortcuts_map_.lower_bound(text_lowercase));
133 it != shortcuts_map_.end() &&
134 StartsWith(it->first, text_lowercase, true); ++it) {
135 if (match.destination_url == it->second.match_core.destination_url) {
136 UpdateShortcut(history::ShortcutsDatabase::Shortcut(
137 it->second.id, text, MatchToMatchCore(match, profile_), now,
138 it->second.number_of_hits + 1));
142 AddShortcut(history::ShortcutsDatabase::Shortcut(
143 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
146 ShortcutsBackend::~ShortcutsBackend() {
150 history::ShortcutsDatabase::Shortcut::MatchCore
151 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match,
153 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
154 TemplateURLService* service =
155 TemplateURLServiceFactory::GetForProfile(profile);
156 const AutocompleteMatch& normalized_match =
157 AutocompleteMatch::IsSpecializedSearchType(match.type) ?
158 BaseSearchProvider::CreateSearchSuggestion(
159 match.search_terms_args->search_terms, match_type,
160 (match.transition == ui::PAGE_TRANSITION_KEYWORD),
161 match.GetTemplateURL(service, false),
162 UIThreadSearchTermsData(profile)) :
164 return history::ShortcutsDatabase::Shortcut::MatchCore(
165 normalized_match.fill_into_edit, normalized_match.destination_url,
166 normalized_match.contents,
167 StripMatchMarkers(normalized_match.contents_class),
168 normalized_match.description,
169 StripMatchMarkers(normalized_match.description_class),
170 normalized_match.transition, match_type, normalized_match.keyword);
173 void ShortcutsBackend::ShutdownOnUIThread() {
174 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
175 BrowserThread::CurrentlyOn(BrowserThread::UI));
176 notification_registrar_.RemoveAll();
179 void ShortcutsBackend::Observe(int type,
180 const content::NotificationSource& source,
181 const content::NotificationDetails& details) {
185 #if defined(ENABLE_EXTENSIONS)
186 if (type == extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
187 // When an extension is unloaded, we want to remove any Shortcuts associated
189 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>(
190 details)->extension->url(), false);
195 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type);
196 const history::URLsDeletedDetails* deleted_details =
197 content::Details<const history::URLsDeletedDetails>(details).ptr();
198 if (deleted_details->all_history) {
199 DeleteAllShortcuts();
203 const history::URLRows& rows(deleted_details->rows);
204 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
205 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end();
208 rows.begin(), rows.end(), history::URLRow::URLRowHasURL(
209 it->second->second.match_core.destination_url)) != rows.end())
210 shortcut_ids.push_back(it->first);
212 DeleteShortcutsWithIDs(shortcut_ids);
215 void ShortcutsBackend::InitInternal() {
216 DCHECK(current_state_ == INITIALIZING);
218 history::ShortcutsDatabase::GuidToShortcutMap shortcuts;
219 db_->LoadShortcuts(&shortcuts);
220 temp_shortcuts_map_.reset(new ShortcutMap);
221 temp_guid_map_.reset(new GuidMap);
222 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
223 shortcuts.begin()); it != shortcuts.end(); ++it) {
224 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
225 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
227 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
228 base::Bind(&ShortcutsBackend::InitCompleted, this));
231 void ShortcutsBackend::InitCompleted() {
232 temp_guid_map_->swap(guid_map_);
233 temp_shortcuts_map_->swap(shortcuts_map_);
234 temp_shortcuts_map_.reset(NULL);
235 temp_guid_map_.reset(NULL);
236 current_state_ = INITIALIZED;
237 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
238 OnShortcutsLoaded());
241 bool ShortcutsBackend::AddShortcut(
242 const history::ShortcutsDatabase::Shortcut& shortcut) {
245 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
246 guid_map_[shortcut.id] = shortcuts_map_.insert(
247 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
248 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
249 OnShortcutsChanged());
250 return no_db_access_ ||
251 BrowserThread::PostTask(
252 BrowserThread::DB, FROM_HERE,
253 base::Bind(base::IgnoreResult(
254 &history::ShortcutsDatabase::AddShortcut),
255 db_.get(), shortcut));
258 bool ShortcutsBackend::UpdateShortcut(
259 const history::ShortcutsDatabase::Shortcut& shortcut) {
262 GuidMap::iterator it(guid_map_.find(shortcut.id));
263 if (it != guid_map_.end())
264 shortcuts_map_.erase(it->second);
265 guid_map_[shortcut.id] = shortcuts_map_.insert(
266 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
267 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
268 OnShortcutsChanged());
269 return no_db_access_ ||
270 BrowserThread::PostTask(
271 BrowserThread::DB, FROM_HERE,
272 base::Bind(base::IgnoreResult(
273 &history::ShortcutsDatabase::UpdateShortcut),
274 db_.get(), shortcut));
277 bool ShortcutsBackend::DeleteShortcutsWithIDs(
278 const history::ShortcutsDatabase::ShortcutIDs& shortcut_ids) {
281 for (size_t i = 0; i < shortcut_ids.size(); ++i) {
282 GuidMap::iterator it(guid_map_.find(shortcut_ids[i]));
283 if (it != guid_map_.end()) {
284 shortcuts_map_.erase(it->second);
288 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
289 OnShortcutsChanged());
290 return no_db_access_ ||
291 BrowserThread::PostTask(
292 BrowserThread::DB, FROM_HERE,
293 base::Bind(base::IgnoreResult(
294 &history::ShortcutsDatabase::DeleteShortcutsWithIDs),
295 db_.get(), shortcut_ids));
298 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& url,
300 const std::string& url_spec = url.spec();
301 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
302 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
304 (it->second->second.match_core.destination_url == url) :
305 StartsWithASCII(it->second->second.match_core.destination_url.spec(),
307 shortcut_ids.push_back(it->first);
308 shortcuts_map_.erase(it->second);
309 guid_map_.erase(it++);
314 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
315 OnShortcutsChanged());
316 return no_db_access_ ||
317 BrowserThread::PostTask(
318 BrowserThread::DB, FROM_HERE,
319 base::Bind(base::IgnoreResult(
320 &history::ShortcutsDatabase::DeleteShortcutsWithURL),
321 db_.get(), url_spec));
324 bool ShortcutsBackend::DeleteAllShortcuts() {
327 shortcuts_map_.clear();
329 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
330 OnShortcutsChanged());
331 return no_db_access_ ||
332 BrowserThread::PostTask(
333 BrowserThread::DB, FROM_HERE,
334 base::Bind(base::IgnoreResult(
335 &history::ShortcutsDatabase::DeleteAllShortcuts),