Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_backend.cc
blob312a7653eb0fcebacaf4ea4f7f6b27f5ad2c4e0c
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/autocomplete_input.h"
17 #include "chrome/browser/autocomplete/autocomplete_match.h"
18 #include "chrome/browser/autocomplete/autocomplete_result.h"
19 #include "chrome/browser/autocomplete/base_search_provider.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/history/history_notifications.h"
22 #include "chrome/browser/history/history_service.h"
23 #include "chrome/browser/history/shortcuts_database.h"
24 #include "chrome/browser/omnibox/omnibox_log.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/autocomplete_match_type.h"
27 #include "chrome/common/chrome_constants.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/notification_details.h"
30 #include "content/public/browser/notification_source.h"
31 #include "extensions/common/extension.h"
33 using content::BrowserThread;
35 namespace {
37 // Takes Match classification vector and removes all matched positions,
38 // compacting repetitions if necessary.
39 std::string StripMatchMarkers(const ACMatchClassifications& matches) {
40 ACMatchClassifications unmatched;
41 for (ACMatchClassifications::const_iterator i(matches.begin());
42 i != matches.end(); ++i) {
43 AutocompleteMatch::AddLastClassificationIfNecessary(
44 &unmatched, i->offset, i->style & ~ACMatchClassification::MATCH);
46 return AutocompleteMatch::ClassificationsToString(unmatched);
49 // Normally shortcuts have the same match type as the original match they were
50 // created from, but for certain match types, we should modify the shortcut's
51 // type slightly to reflect that the origin of the shortcut is historical.
52 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) {
53 switch (type) {
54 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
55 case AutocompleteMatchType::NAVSUGGEST:
56 return AutocompleteMatchType::HISTORY_URL;
58 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
59 return type;
61 default:
62 return AutocompleteMatch::IsSearchType(type) ?
63 AutocompleteMatchType::SEARCH_HISTORY : type;
67 } // namespace
70 // ShortcutsBackend -----------------------------------------------------------
72 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db)
73 : profile_(profile),
74 current_state_(NOT_INITIALIZED),
75 no_db_access_(suppress_db) {
76 if (!suppress_db) {
77 db_ = new history::ShortcutsDatabase(
78 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
80 // |profile| can be NULL in tests.
81 if (profile) {
82 notification_registrar_.Add(
83 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
84 content::Source<Profile>(profile));
85 notification_registrar_.Add(
86 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
87 content::Source<Profile>(profile));
91 bool ShortcutsBackend::Init() {
92 if (current_state_ != NOT_INITIALIZED)
93 return false;
95 if (no_db_access_) {
96 current_state_ = INITIALIZED;
97 return true;
100 current_state_ = INITIALIZING;
101 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
102 base::Bind(&ShortcutsBackend::InitInternal, this));
105 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& shortcut_url) {
106 return initialized() && DeleteShortcutsWithURL(shortcut_url, true);
109 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
110 observer_list_.AddObserver(obs);
113 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
114 observer_list_.RemoveObserver(obs);
117 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text,
118 const AutocompleteMatch& match) {
119 const base::string16 text_lowercase(base::i18n::ToLower(text));
120 const base::Time now(base::Time::Now());
121 for (ShortcutMap::const_iterator it(
122 shortcuts_map_.lower_bound(text_lowercase));
123 it != shortcuts_map_.end() &&
124 StartsWith(it->first, text_lowercase, true); ++it) {
125 if (match.destination_url == it->second.match_core.destination_url) {
126 UpdateShortcut(history::ShortcutsDatabase::Shortcut(
127 it->second.id, text, MatchToMatchCore(match, profile_), now,
128 it->second.number_of_hits + 1));
129 return;
132 AddShortcut(history::ShortcutsDatabase::Shortcut(
133 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
136 ShortcutsBackend::~ShortcutsBackend() {
139 // static
140 history::ShortcutsDatabase::Shortcut::MatchCore
141 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match,
142 Profile* profile) {
143 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
144 const AutocompleteMatch& normalized_match =
145 AutocompleteMatch::IsSpecializedSearchType(match.type) ?
146 BaseSearchProvider::CreateSearchSuggestion(
147 match.search_terms_args->search_terms, match_type,
148 (match.transition == content::PAGE_TRANSITION_KEYWORD),
149 match.GetTemplateURL(profile, false)) :
150 match;
151 return history::ShortcutsDatabase::Shortcut::MatchCore(
152 normalized_match.fill_into_edit, normalized_match.destination_url,
153 normalized_match.contents,
154 StripMatchMarkers(normalized_match.contents_class),
155 normalized_match.description,
156 StripMatchMarkers(normalized_match.description_class),
157 normalized_match.transition, match_type, normalized_match.keyword);
160 void ShortcutsBackend::ShutdownOnUIThread() {
161 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
162 BrowserThread::CurrentlyOn(BrowserThread::UI));
163 notification_registrar_.RemoveAll();
166 void ShortcutsBackend::Observe(int type,
167 const content::NotificationSource& source,
168 const content::NotificationDetails& details) {
169 if (!initialized())
170 return;
172 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
173 // When an extension is unloaded, we want to remove any Shortcuts associated
174 // with it.
175 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>(
176 details)->extension->url(), false);
177 return;
180 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type);
181 const history::URLsDeletedDetails* deleted_details =
182 content::Details<const history::URLsDeletedDetails>(details).ptr();
183 if (deleted_details->all_history) {
184 DeleteAllShortcuts();
185 return;
188 const history::URLRows& rows(deleted_details->rows);
189 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
190 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end();
191 ++it) {
192 if (std::find_if(
193 rows.begin(), rows.end(), history::URLRow::URLRowHasURL(
194 it->second->second.match_core.destination_url)) != rows.end())
195 shortcut_ids.push_back(it->first);
197 DeleteShortcutsWithIDs(shortcut_ids);
200 void ShortcutsBackend::InitInternal() {
201 DCHECK(current_state_ == INITIALIZING);
202 db_->Init();
203 history::ShortcutsDatabase::GuidToShortcutMap shortcuts;
204 db_->LoadShortcuts(&shortcuts);
205 temp_shortcuts_map_.reset(new ShortcutMap);
206 temp_guid_map_.reset(new GuidMap);
207 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
208 shortcuts.begin()); it != shortcuts.end(); ++it) {
209 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
210 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
212 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
213 base::Bind(&ShortcutsBackend::InitCompleted, this));
216 void ShortcutsBackend::InitCompleted() {
217 temp_guid_map_->swap(guid_map_);
218 temp_shortcuts_map_->swap(shortcuts_map_);
219 temp_shortcuts_map_.reset(NULL);
220 temp_guid_map_.reset(NULL);
221 current_state_ = INITIALIZED;
222 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
223 OnShortcutsLoaded());
226 bool ShortcutsBackend::AddShortcut(
227 const history::ShortcutsDatabase::Shortcut& shortcut) {
228 if (!initialized())
229 return false;
230 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
231 guid_map_[shortcut.id] = shortcuts_map_.insert(
232 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
233 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
234 OnShortcutsChanged());
235 return no_db_access_ ||
236 BrowserThread::PostTask(
237 BrowserThread::DB, FROM_HERE,
238 base::Bind(base::IgnoreResult(
239 &history::ShortcutsDatabase::AddShortcut),
240 db_.get(), shortcut));
243 bool ShortcutsBackend::UpdateShortcut(
244 const history::ShortcutsDatabase::Shortcut& shortcut) {
245 if (!initialized())
246 return false;
247 GuidMap::iterator it(guid_map_.find(shortcut.id));
248 if (it != guid_map_.end())
249 shortcuts_map_.erase(it->second);
250 guid_map_[shortcut.id] = shortcuts_map_.insert(
251 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
252 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
253 OnShortcutsChanged());
254 return no_db_access_ ||
255 BrowserThread::PostTask(
256 BrowserThread::DB, FROM_HERE,
257 base::Bind(base::IgnoreResult(
258 &history::ShortcutsDatabase::UpdateShortcut),
259 db_.get(), shortcut));
262 bool ShortcutsBackend::DeleteShortcutsWithIDs(
263 const history::ShortcutsDatabase::ShortcutIDs& shortcut_ids) {
264 if (!initialized())
265 return false;
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);
270 guid_map_.erase(it);
273 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
274 OnShortcutsChanged());
275 return no_db_access_ ||
276 BrowserThread::PostTask(
277 BrowserThread::DB, FROM_HERE,
278 base::Bind(base::IgnoreResult(
279 &history::ShortcutsDatabase::DeleteShortcutsWithIDs),
280 db_.get(), shortcut_ids));
283 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& url,
284 bool exact_match) {
285 const std::string& url_spec = url.spec();
286 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
287 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
288 if (exact_match ?
289 (it->second->second.match_core.destination_url == url) :
290 StartsWithASCII(it->second->second.match_core.destination_url.spec(),
291 url_spec, true)) {
292 shortcut_ids.push_back(it->first);
293 shortcuts_map_.erase(it->second);
294 guid_map_.erase(it++);
295 } else {
296 ++it;
299 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
300 OnShortcutsChanged());
301 return no_db_access_ ||
302 BrowserThread::PostTask(
303 BrowserThread::DB, FROM_HERE,
304 base::Bind(base::IgnoreResult(
305 &history::ShortcutsDatabase::DeleteShortcutsWithURL),
306 db_.get(), url_spec));
309 bool ShortcutsBackend::DeleteAllShortcuts() {
310 if (!initialized())
311 return false;
312 shortcuts_map_.clear();
313 guid_map_.clear();
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::DeleteAllShortcuts),
321 db_.get()));