GoogleURLTrackerInfoBarDelegate: Initialize uninitialized member in constructor.
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_backend.cc
blob15e8bba32175e4fa43566fad634019562c269c9a
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 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
57 return AutocompleteMatchType::HISTORY_URL;
59 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
60 return type;
62 default:
63 return AutocompleteMatch::IsSearchType(type) ?
64 AutocompleteMatchType::SEARCH_HISTORY : type;
68 } // namespace
71 // ShortcutsBackend -----------------------------------------------------------
73 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db)
74 : profile_(profile),
75 current_state_(NOT_INITIALIZED),
76 no_db_access_(suppress_db) {
77 if (!suppress_db) {
78 db_ = new history::ShortcutsDatabase(
79 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
81 // |profile| can be NULL in tests.
82 if (profile) {
83 notification_registrar_.Add(
84 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
85 content::Source<Profile>(profile));
86 notification_registrar_.Add(
87 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
88 content::Source<Profile>(profile));
92 bool ShortcutsBackend::Init() {
93 if (current_state_ != NOT_INITIALIZED)
94 return false;
96 if (no_db_access_) {
97 current_state_ = INITIALIZED;
98 return true;
101 current_state_ = INITIALIZING;
102 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
103 base::Bind(&ShortcutsBackend::InitInternal, this));
106 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& shortcut_url) {
107 return initialized() && DeleteShortcutsWithURL(shortcut_url, true);
110 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
111 observer_list_.AddObserver(obs);
114 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
115 observer_list_.RemoveObserver(obs);
118 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text,
119 const AutocompleteMatch& match) {
120 const base::string16 text_lowercase(base::i18n::ToLower(text));
121 const base::Time now(base::Time::Now());
122 for (ShortcutMap::const_iterator it(
123 shortcuts_map_.lower_bound(text_lowercase));
124 it != shortcuts_map_.end() &&
125 StartsWith(it->first, text_lowercase, true); ++it) {
126 if (match.destination_url == it->second.match_core.destination_url) {
127 UpdateShortcut(history::ShortcutsDatabase::Shortcut(
128 it->second.id, text, MatchToMatchCore(match, profile_), now,
129 it->second.number_of_hits + 1));
130 return;
133 AddShortcut(history::ShortcutsDatabase::Shortcut(
134 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
137 ShortcutsBackend::~ShortcutsBackend() {
140 // static
141 history::ShortcutsDatabase::Shortcut::MatchCore
142 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match,
143 Profile* profile) {
144 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
145 const AutocompleteMatch& normalized_match =
146 AutocompleteMatch::IsSpecializedSearchType(match.type) ?
147 BaseSearchProvider::CreateSearchSuggestion(
148 match.search_terms_args->search_terms, match_type,
149 (match.transition == content::PAGE_TRANSITION_KEYWORD),
150 match.GetTemplateURL(profile, false)) :
151 match;
152 return history::ShortcutsDatabase::Shortcut::MatchCore(
153 normalized_match.fill_into_edit, normalized_match.destination_url,
154 normalized_match.contents,
155 StripMatchMarkers(normalized_match.contents_class),
156 normalized_match.description,
157 StripMatchMarkers(normalized_match.description_class),
158 normalized_match.transition, match_type, normalized_match.keyword);
161 void ShortcutsBackend::ShutdownOnUIThread() {
162 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
163 BrowserThread::CurrentlyOn(BrowserThread::UI));
164 notification_registrar_.RemoveAll();
167 void ShortcutsBackend::Observe(int type,
168 const content::NotificationSource& source,
169 const content::NotificationDetails& details) {
170 if (!initialized())
171 return;
173 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
174 // When an extension is unloaded, we want to remove any Shortcuts associated
175 // with it.
176 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>(
177 details)->extension->url(), false);
178 return;
181 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type);
182 const history::URLsDeletedDetails* deleted_details =
183 content::Details<const history::URLsDeletedDetails>(details).ptr();
184 if (deleted_details->all_history) {
185 DeleteAllShortcuts();
186 return;
189 const history::URLRows& rows(deleted_details->rows);
190 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
191 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end();
192 ++it) {
193 if (std::find_if(
194 rows.begin(), rows.end(), history::URLRow::URLRowHasURL(
195 it->second->second.match_core.destination_url)) != rows.end())
196 shortcut_ids.push_back(it->first);
198 DeleteShortcutsWithIDs(shortcut_ids);
201 void ShortcutsBackend::InitInternal() {
202 DCHECK(current_state_ == INITIALIZING);
203 db_->Init();
204 history::ShortcutsDatabase::GuidToShortcutMap shortcuts;
205 db_->LoadShortcuts(&shortcuts);
206 temp_shortcuts_map_.reset(new ShortcutMap);
207 temp_guid_map_.reset(new GuidMap);
208 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
209 shortcuts.begin()); it != shortcuts.end(); ++it) {
210 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
211 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
213 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
214 base::Bind(&ShortcutsBackend::InitCompleted, this));
217 void ShortcutsBackend::InitCompleted() {
218 temp_guid_map_->swap(guid_map_);
219 temp_shortcuts_map_->swap(shortcuts_map_);
220 temp_shortcuts_map_.reset(NULL);
221 temp_guid_map_.reset(NULL);
222 current_state_ = INITIALIZED;
223 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
224 OnShortcutsLoaded());
227 bool ShortcutsBackend::AddShortcut(
228 const history::ShortcutsDatabase::Shortcut& shortcut) {
229 if (!initialized())
230 return false;
231 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
232 guid_map_[shortcut.id] = shortcuts_map_.insert(
233 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
234 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
235 OnShortcutsChanged());
236 return no_db_access_ ||
237 BrowserThread::PostTask(
238 BrowserThread::DB, FROM_HERE,
239 base::Bind(base::IgnoreResult(
240 &history::ShortcutsDatabase::AddShortcut),
241 db_.get(), shortcut));
244 bool ShortcutsBackend::UpdateShortcut(
245 const history::ShortcutsDatabase::Shortcut& shortcut) {
246 if (!initialized())
247 return false;
248 GuidMap::iterator it(guid_map_.find(shortcut.id));
249 if (it != guid_map_.end())
250 shortcuts_map_.erase(it->second);
251 guid_map_[shortcut.id] = shortcuts_map_.insert(
252 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
253 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
254 OnShortcutsChanged());
255 return no_db_access_ ||
256 BrowserThread::PostTask(
257 BrowserThread::DB, FROM_HERE,
258 base::Bind(base::IgnoreResult(
259 &history::ShortcutsDatabase::UpdateShortcut),
260 db_.get(), shortcut));
263 bool ShortcutsBackend::DeleteShortcutsWithIDs(
264 const history::ShortcutsDatabase::ShortcutIDs& shortcut_ids) {
265 if (!initialized())
266 return false;
267 for (size_t i = 0; i < shortcut_ids.size(); ++i) {
268 GuidMap::iterator it(guid_map_.find(shortcut_ids[i]));
269 if (it != guid_map_.end()) {
270 shortcuts_map_.erase(it->second);
271 guid_map_.erase(it);
274 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
275 OnShortcutsChanged());
276 return no_db_access_ ||
277 BrowserThread::PostTask(
278 BrowserThread::DB, FROM_HERE,
279 base::Bind(base::IgnoreResult(
280 &history::ShortcutsDatabase::DeleteShortcutsWithIDs),
281 db_.get(), shortcut_ids));
284 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& url,
285 bool exact_match) {
286 const std::string& url_spec = url.spec();
287 history::ShortcutsDatabase::ShortcutIDs shortcut_ids;
288 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
289 if (exact_match ?
290 (it->second->second.match_core.destination_url == url) :
291 StartsWithASCII(it->second->second.match_core.destination_url.spec(),
292 url_spec, true)) {
293 shortcut_ids.push_back(it->first);
294 shortcuts_map_.erase(it->second);
295 guid_map_.erase(it++);
296 } else {
297 ++it;
300 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
301 OnShortcutsChanged());
302 return no_db_access_ ||
303 BrowserThread::PostTask(
304 BrowserThread::DB, FROM_HERE,
305 base::Bind(base::IgnoreResult(
306 &history::ShortcutsDatabase::DeleteShortcutsWithURL),
307 db_.get(), url_spec));
310 bool ShortcutsBackend::DeleteAllShortcuts() {
311 if (!initialized())
312 return false;
313 shortcuts_map_.clear();
314 guid_map_.clear();
315 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
316 OnShortcutsChanged());
317 return no_db_access_ ||
318 BrowserThread::PostTask(
319 BrowserThread::DB, FROM_HERE,
320 base::Bind(base::IgnoreResult(
321 &history::ShortcutsDatabase::DeleteAllShortcuts),
322 db_.get()));