Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_backend.cc
blobf5f6148d3d320d62300ac9cd631ff7b69416a814
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/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"
33 #include "extensions/browser/notification_types.h"
34 #include "extensions/common/extension.h"
36 using content::BrowserThread;
38 namespace {
40 // Takes Match classification vector and removes all matched positions,
41 // compacting repetitions if necessary.
42 std::string StripMatchMarkers(const ACMatchClassifications& matches) {
43 ACMatchClassifications unmatched;
44 for (ACMatchClassifications::const_iterator i(matches.begin());
45 i != matches.end(); ++i) {
46 AutocompleteMatch::AddLastClassificationIfNecessary(
47 &unmatched, i->offset, i->style & ~ACMatchClassification::MATCH);
49 return AutocompleteMatch::ClassificationsToString(unmatched);
52 // Normally shortcuts have the same match type as the original match they were
53 // created from, but for certain match types, we should modify the shortcut's
54 // type slightly to reflect that the origin of the shortcut is historical.
55 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) {
56 switch (type) {
57 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
58 case AutocompleteMatchType::NAVSUGGEST:
59 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
60 return AutocompleteMatchType::HISTORY_URL;
62 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
63 return type;
65 default:
66 return AutocompleteMatch::IsSearchType(type) ?
67 AutocompleteMatchType::SEARCH_HISTORY : type;
71 } // namespace
74 // ShortcutsBackend -----------------------------------------------------------
76 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db)
77 : profile_(profile),
78 current_state_(NOT_INITIALIZED),
79 no_db_access_(suppress_db) {
80 if (!suppress_db) {
81 db_ = new history::ShortcutsDatabase(
82 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
84 // |profile| can be NULL in tests.
85 if (profile) {
86 notification_registrar_.Add(
87 this,
88 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
89 content::Source<Profile>(profile));
90 notification_registrar_.Add(
91 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
92 content::Source<Profile>(profile));
96 bool ShortcutsBackend::Init() {
97 if (current_state_ != NOT_INITIALIZED)
98 return false;
100 if (no_db_access_) {
101 current_state_ = INITIALIZED;
102 return true;
105 current_state_ = INITIALIZING;
106 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
107 base::Bind(&ShortcutsBackend::InitInternal, this));
110 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& shortcut_url) {
111 return initialized() && DeleteShortcutsWithURL(shortcut_url, true);
114 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
115 observer_list_.AddObserver(obs);
118 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
119 observer_list_.RemoveObserver(obs);
122 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text,
123 const AutocompleteMatch& match) {
124 const base::string16 text_lowercase(base::i18n::ToLower(text));
125 const base::Time now(base::Time::Now());
126 for (ShortcutMap::const_iterator it(
127 shortcuts_map_.lower_bound(text_lowercase));
128 it != shortcuts_map_.end() &&
129 StartsWith(it->first, text_lowercase, true); ++it) {
130 if (match.destination_url == it->second.match_core.destination_url) {
131 UpdateShortcut(history::ShortcutsDatabase::Shortcut(
132 it->second.id, text, MatchToMatchCore(match, profile_), now,
133 it->second.number_of_hits + 1));
134 return;
137 AddShortcut(history::ShortcutsDatabase::Shortcut(
138 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
141 ShortcutsBackend::~ShortcutsBackend() {
144 // static
145 history::ShortcutsDatabase::Shortcut::MatchCore
146 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match,
147 Profile* profile) {
148 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
149 TemplateURLService* service =
150 TemplateURLServiceFactory::GetForProfile(profile);
151 const AutocompleteMatch& normalized_match =
152 AutocompleteMatch::IsSpecializedSearchType(match.type) ?
153 BaseSearchProvider::CreateSearchSuggestion(
154 match.search_terms_args->search_terms, match_type,
155 (match.transition == content::PAGE_TRANSITION_KEYWORD),
156 match.GetTemplateURL(service, false),
157 UIThreadSearchTermsData(profile)) :
158 match;
159 return history::ShortcutsDatabase::Shortcut::MatchCore(
160 normalized_match.fill_into_edit, normalized_match.destination_url,
161 normalized_match.contents,
162 StripMatchMarkers(normalized_match.contents_class),
163 normalized_match.description,
164 StripMatchMarkers(normalized_match.description_class),
165 normalized_match.transition, match_type, normalized_match.keyword);
168 void ShortcutsBackend::ShutdownOnUIThread() {
169 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
170 BrowserThread::CurrentlyOn(BrowserThread::UI));
171 notification_registrar_.RemoveAll();
174 void ShortcutsBackend::Observe(int type,
175 const content::NotificationSource& source,
176 const content::NotificationDetails& details) {
177 if (!initialized())
178 return;
180 if (type == extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
181 // When an extension is unloaded, we want to remove any Shortcuts associated
182 // with it.
183 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>(
184 details)->extension->url(), false);
185 return;
188 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type);
189 const history::URLsDeletedDetails* deleted_details =
190 content::Details<const history::URLsDeletedDetails>(details).ptr();
191 if (deleted_details->all_history) {
192 DeleteAllShortcuts();
193 return;
196 const history::URLRows& rows(deleted_details->rows);
197 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
198 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end();
199 ++it) {
200 if (std::find_if(
201 rows.begin(), rows.end(), history::URLRow::URLRowHasURL(
202 it->second->second.match_core.destination_url)) != rows.end())
203 shortcut_ids.push_back(it->first);
205 DeleteShortcutsWithIDs(shortcut_ids);
208 void ShortcutsBackend::InitInternal() {
209 DCHECK(current_state_ == INITIALIZING);
210 db_->Init();
211 history::ShortcutsDatabase::GuidToShortcutMap shortcuts;
212 db_->LoadShortcuts(&shortcuts);
213 temp_shortcuts_map_.reset(new ShortcutMap);
214 temp_guid_map_.reset(new GuidMap);
215 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
216 shortcuts.begin()); it != shortcuts.end(); ++it) {
217 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
218 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
221 base::Bind(&ShortcutsBackend::InitCompleted, this));
224 void ShortcutsBackend::InitCompleted() {
225 temp_guid_map_->swap(guid_map_);
226 temp_shortcuts_map_->swap(shortcuts_map_);
227 temp_shortcuts_map_.reset(NULL);
228 temp_guid_map_.reset(NULL);
229 current_state_ = INITIALIZED;
230 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
231 OnShortcutsLoaded());
234 bool ShortcutsBackend::AddShortcut(
235 const history::ShortcutsDatabase::Shortcut& shortcut) {
236 if (!initialized())
237 return false;
238 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
239 guid_map_[shortcut.id] = shortcuts_map_.insert(
240 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
241 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
242 OnShortcutsChanged());
243 return no_db_access_ ||
244 BrowserThread::PostTask(
245 BrowserThread::DB, FROM_HERE,
246 base::Bind(base::IgnoreResult(
247 &history::ShortcutsDatabase::AddShortcut),
248 db_.get(), shortcut));
251 bool ShortcutsBackend::UpdateShortcut(
252 const history::ShortcutsDatabase::Shortcut& shortcut) {
253 if (!initialized())
254 return false;
255 GuidMap::iterator it(guid_map_.find(shortcut.id));
256 if (it != guid_map_.end())
257 shortcuts_map_.erase(it->second);
258 guid_map_[shortcut.id] = shortcuts_map_.insert(
259 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
260 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
261 OnShortcutsChanged());
262 return no_db_access_ ||
263 BrowserThread::PostTask(
264 BrowserThread::DB, FROM_HERE,
265 base::Bind(base::IgnoreResult(
266 &history::ShortcutsDatabase::UpdateShortcut),
267 db_.get(), shortcut));
270 bool ShortcutsBackend::DeleteShortcutsWithIDs(
271 const history::ShortcutsDatabase::ShortcutIDs& shortcut_ids) {
272 if (!initialized())
273 return false;
274 for (size_t i = 0; i < shortcut_ids.size(); ++i) {
275 GuidMap::iterator it(guid_map_.find(shortcut_ids[i]));
276 if (it != guid_map_.end()) {
277 shortcuts_map_.erase(it->second);
278 guid_map_.erase(it);
281 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
282 OnShortcutsChanged());
283 return no_db_access_ ||
284 BrowserThread::PostTask(
285 BrowserThread::DB, FROM_HERE,
286 base::Bind(base::IgnoreResult(
287 &history::ShortcutsDatabase::DeleteShortcutsWithIDs),
288 db_.get(), shortcut_ids));
291 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& url,
292 bool exact_match) {
293 const std::string& url_spec = url.spec();
294 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
295 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
296 if (exact_match ?
297 (it->second->second.match_core.destination_url == url) :
298 StartsWithASCII(it->second->second.match_core.destination_url.spec(),
299 url_spec, true)) {
300 shortcut_ids.push_back(it->first);
301 shortcuts_map_.erase(it->second);
302 guid_map_.erase(it++);
303 } else {
304 ++it;
307 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
308 OnShortcutsChanged());
309 return no_db_access_ ||
310 BrowserThread::PostTask(
311 BrowserThread::DB, FROM_HERE,
312 base::Bind(base::IgnoreResult(
313 &history::ShortcutsDatabase::DeleteShortcutsWithURL),
314 db_.get(), url_spec));
317 bool ShortcutsBackend::DeleteAllShortcuts() {
318 if (!initialized())
319 return false;
320 shortcuts_map_.clear();
321 guid_map_.clear();
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(base::IgnoreResult(
328 &history::ShortcutsDatabase::DeleteAllShortcuts),
329 db_.get()));