Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_backend.cc
blob4da4f902022bc6628f85e69fe59a42c82048d8ce
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"
7 #include <map>
8 #include <string>
9 #include <vector>
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"
37 #endif
39 using content::BrowserThread;
41 namespace {
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) {
59 switch (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:
66 return type;
68 default:
69 return AutocompleteMatch::IsSearchType(type) ?
70 AutocompleteMatchType::SEARCH_HISTORY : type;
74 } // namespace
77 // ShortcutsBackend -----------------------------------------------------------
79 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db)
80 : profile_(profile),
81 current_state_(NOT_INITIALIZED),
82 history_service_observer_(this),
83 no_db_access_(suppress_db) {
84 if (!suppress_db) {
85 db_ = new ShortcutsDatabase(
86 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
88 // |profile| can be NULL in tests.
89 if (profile) {
90 #if defined(ENABLE_EXTENSIONS)
91 notification_registrar_.Add(
92 this,
93 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
94 content::Source<Profile>(profile));
95 #endif
96 history::HistoryService* hs = HistoryServiceFactory::GetForProfile(
97 profile, ServiceAccessType::EXPLICIT_ACCESS);
98 if (hs)
99 history_service_observer_.Add(hs);
103 bool ShortcutsBackend::Init() {
104 if (current_state_ != NOT_INITIALIZED)
105 return false;
107 if (no_db_access_) {
108 current_state_ = INITIALIZED;
109 return true;
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 StartsWith(it->first, text_lowercase, true); ++it) {
137 if (match.destination_url == it->second.match_core.destination_url) {
138 UpdateShortcut(ShortcutsDatabase::Shortcut(
139 it->second.id, text, MatchToMatchCore(match, profile_), now,
140 it->second.number_of_hits + 1));
141 return;
144 AddShortcut(ShortcutsDatabase::Shortcut(
145 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
148 ShortcutsBackend::~ShortcutsBackend() {
151 // static
152 ShortcutsDatabase::Shortcut::MatchCore ShortcutsBackend::MatchToMatchCore(
153 const AutocompleteMatch& match,
154 Profile* profile) {
155 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
156 TemplateURLService* service =
157 TemplateURLServiceFactory::GetForProfile(profile);
158 const AutocompleteMatch& normalized_match =
159 AutocompleteMatch::IsSpecializedSearchType(match.type) ?
160 BaseSearchProvider::CreateSearchSuggestion(
161 match.search_terms_args->search_terms, match_type,
162 (match.transition == ui::PAGE_TRANSITION_KEYWORD),
163 match.GetTemplateURL(service, false),
164 UIThreadSearchTermsData(profile)) :
165 match;
166 return ShortcutsDatabase::Shortcut::MatchCore(
167 normalized_match.fill_into_edit, normalized_match.destination_url,
168 normalized_match.contents,
169 StripMatchMarkers(normalized_match.contents_class),
170 normalized_match.description,
171 StripMatchMarkers(normalized_match.description_class),
172 normalized_match.transition, match_type, normalized_match.keyword);
175 void ShortcutsBackend::ShutdownOnUIThread() {
176 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
177 BrowserThread::CurrentlyOn(BrowserThread::UI));
178 notification_registrar_.RemoveAll();
179 history_service_observer_.RemoveAll();
182 void ShortcutsBackend::Observe(int type,
183 const content::NotificationSource& source,
184 const content::NotificationDetails& details) {
185 #if defined(ENABLE_EXTENSIONS)
186 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, type);
187 if (!initialized())
188 return;
190 // When an extension is unloaded, we want to remove any Shortcuts associated
191 // with it.
192 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>(
193 details)->extension->url(),
194 false);
195 #endif
198 void ShortcutsBackend::OnURLsDeleted(history::HistoryService* history_service,
199 bool all_history,
200 bool expired,
201 const history::URLRows& deleted_rows,
202 const std::set<GURL>& favicon_urls) {
203 if (!initialized())
204 return;
206 if (all_history) {
207 DeleteAllShortcuts();
208 return;
211 ShortcutsDatabase::ShortcutIDs shortcut_ids;
212 for (const auto& guid_pair : guid_map_) {
213 if (std::find_if(
214 deleted_rows.begin(), deleted_rows.end(),
215 history::URLRow::URLRowHasURL(
216 guid_pair.second->second.match_core.destination_url)) !=
217 deleted_rows.end()) {
218 shortcut_ids.push_back(guid_pair.first);
221 DeleteShortcutsWithIDs(shortcut_ids);
224 void ShortcutsBackend::InitInternal() {
225 DCHECK(current_state_ == INITIALIZING);
226 db_->Init();
227 ShortcutsDatabase::GuidToShortcutMap shortcuts;
228 db_->LoadShortcuts(&shortcuts);
229 temp_shortcuts_map_.reset(new ShortcutMap);
230 temp_guid_map_.reset(new GuidMap);
231 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
232 shortcuts.begin());
233 it != shortcuts.end(); ++it) {
234 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
235 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
237 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
238 base::Bind(&ShortcutsBackend::InitCompleted, this));
241 void ShortcutsBackend::InitCompleted() {
242 temp_guid_map_->swap(guid_map_);
243 temp_shortcuts_map_->swap(shortcuts_map_);
244 temp_shortcuts_map_.reset(NULL);
245 temp_guid_map_.reset(NULL);
246 current_state_ = INITIALIZED;
247 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
248 OnShortcutsLoaded());
251 bool ShortcutsBackend::AddShortcut(
252 const ShortcutsDatabase::Shortcut& shortcut) {
253 if (!initialized())
254 return false;
255 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
256 guid_map_[shortcut.id] = shortcuts_map_.insert(
257 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
258 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
259 OnShortcutsChanged());
260 return no_db_access_ ||
261 BrowserThread::PostTask(
262 BrowserThread::DB, FROM_HERE,
263 base::Bind(base::IgnoreResult(&ShortcutsDatabase::AddShortcut),
264 db_.get(), shortcut));
267 bool ShortcutsBackend::UpdateShortcut(
268 const ShortcutsDatabase::Shortcut& shortcut) {
269 if (!initialized())
270 return false;
271 GuidMap::iterator it(guid_map_.find(shortcut.id));
272 if (it != guid_map_.end())
273 shortcuts_map_.erase(it->second);
274 guid_map_[shortcut.id] = shortcuts_map_.insert(
275 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
276 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
277 OnShortcutsChanged());
278 return no_db_access_ ||
279 BrowserThread::PostTask(
280 BrowserThread::DB, FROM_HERE,
281 base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut),
282 db_.get(), shortcut));
285 bool ShortcutsBackend::DeleteShortcutsWithIDs(
286 const ShortcutsDatabase::ShortcutIDs& shortcut_ids) {
287 if (!initialized())
288 return false;
289 for (size_t i = 0; i < shortcut_ids.size(); ++i) {
290 GuidMap::iterator it(guid_map_.find(shortcut_ids[i]));
291 if (it != guid_map_.end()) {
292 shortcuts_map_.erase(it->second);
293 guid_map_.erase(it);
296 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
297 OnShortcutsChanged());
298 return no_db_access_ ||
299 BrowserThread::PostTask(
300 BrowserThread::DB, FROM_HERE,
301 base::Bind(
302 base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithIDs),
303 db_.get(), shortcut_ids));
306 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& url,
307 bool exact_match) {
308 const std::string& url_spec = url.spec();
309 ShortcutsDatabase::ShortcutIDs shortcut_ids;
310 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
311 if (exact_match ?
312 (it->second->second.match_core.destination_url == url) :
313 StartsWithASCII(it->second->second.match_core.destination_url.spec(),
314 url_spec, true)) {
315 shortcut_ids.push_back(it->first);
316 shortcuts_map_.erase(it->second);
317 guid_map_.erase(it++);
318 } else {
319 ++it;
322 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
323 OnShortcutsChanged());
324 return no_db_access_ ||
325 BrowserThread::PostTask(
326 BrowserThread::DB, FROM_HERE,
327 base::Bind(
328 base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithURL),
329 db_.get(), url_spec));
332 bool ShortcutsBackend::DeleteAllShortcuts() {
333 if (!initialized())
334 return false;
335 shortcuts_map_.clear();
336 guid_map_.clear();
337 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
338 OnShortcutsChanged());
339 return no_db_access_ ||
340 BrowserThread::PostTask(
341 BrowserThread::DB, FROM_HERE,
342 base::Bind(
343 base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts),
344 db_.get()));