cc: Convert LTHCommon tests to LayerImpl
[chromium-blink-merge.git] / components / omnibox / browser / shortcuts_backend.cc
blob4fbd7dc8e5f83492eea6376677670f53ac66dd80
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 "components/omnibox/browser/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 "base/thread_task_runner_handle.h"
17 #include "components/history/core/browser/history_service.h"
18 #include "components/omnibox/browser/autocomplete_input.h"
19 #include "components/omnibox/browser/autocomplete_match.h"
20 #include "components/omnibox/browser/autocomplete_match_type.h"
21 #include "components/omnibox/browser/autocomplete_result.h"
22 #include "components/omnibox/browser/base_search_provider.h"
23 #include "components/omnibox/browser/omnibox_log.h"
24 #include "components/omnibox/browser/shortcuts_database.h"
26 namespace {
28 // Takes Match classification vector and removes all matched positions,
29 // compacting repetitions if necessary.
30 std::string StripMatchMarkers(const ACMatchClassifications& matches) {
31 ACMatchClassifications unmatched;
32 for (ACMatchClassifications::const_iterator i(matches.begin());
33 i != matches.end(); ++i) {
34 AutocompleteMatch::AddLastClassificationIfNecessary(
35 &unmatched, i->offset, i->style & ~ACMatchClassification::MATCH);
37 return AutocompleteMatch::ClassificationsToString(unmatched);
40 // Normally shortcuts have the same match type as the original match they were
41 // created from, but for certain match types, we should modify the shortcut's
42 // type slightly to reflect that the origin of the shortcut is historical.
43 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) {
44 switch (type) {
45 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
46 case AutocompleteMatchType::NAVSUGGEST:
47 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
48 return AutocompleteMatchType::HISTORY_URL;
50 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
51 return type;
53 default:
54 return AutocompleteMatch::IsSearchType(type) ?
55 AutocompleteMatchType::SEARCH_HISTORY : type;
59 } // namespace
62 // ShortcutsBackend -----------------------------------------------------------
64 ShortcutsBackend::ShortcutsBackend(
65 TemplateURLService* template_url_service,
66 scoped_ptr<SearchTermsData> search_terms_data,
67 history::HistoryService* history_service,
68 scoped_refptr<base::SequencedTaskRunner> db_runner,
69 base::FilePath database_path,
70 bool suppress_db)
71 : template_url_service_(template_url_service),
72 search_terms_data_(search_terms_data.Pass()),
73 current_state_(NOT_INITIALIZED),
74 history_service_observer_(this),
75 main_runner_(base::ThreadTaskRunnerHandle::Get()),
76 db_runner_(db_runner),
77 no_db_access_(suppress_db) {
78 if (!suppress_db)
79 db_ = new ShortcutsDatabase(database_path);
80 if (history_service)
81 history_service_observer_.Add(history_service);
84 bool ShortcutsBackend::Init() {
85 if (current_state_ != NOT_INITIALIZED)
86 return false;
88 if (no_db_access_) {
89 current_state_ = INITIALIZED;
90 return true;
93 current_state_ = INITIALIZING;
94 return db_runner_->PostTask(
95 FROM_HERE, base::Bind(&ShortcutsBackend::InitInternal, this));
98 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& shortcut_url) {
99 return initialized() && DeleteShortcutsWithURL(shortcut_url, true);
102 bool ShortcutsBackend::DeleteShortcutsBeginningWithURL(
103 const GURL& shortcut_url) {
104 return initialized() && DeleteShortcutsWithURL(shortcut_url, false);
107 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
108 observer_list_.AddObserver(obs);
111 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
112 observer_list_.RemoveObserver(obs);
115 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text,
116 const AutocompleteMatch& match) {
117 const base::string16 text_lowercase(base::i18n::ToLower(text));
118 const base::Time now(base::Time::Now());
119 for (ShortcutMap::const_iterator it(
120 shortcuts_map_.lower_bound(text_lowercase));
121 it != shortcuts_map_.end() &&
122 base::StartsWith(it->first, text_lowercase,
123 base::CompareCase::SENSITIVE);
124 ++it) {
125 if (match.destination_url == it->second.match_core.destination_url) {
126 UpdateShortcut(ShortcutsDatabase::Shortcut(
127 it->second.id, text, MatchToMatchCore(match, template_url_service_,
128 search_terms_data_.get()),
129 now, it->second.number_of_hits + 1));
130 return;
133 AddShortcut(ShortcutsDatabase::Shortcut(
134 base::GenerateGUID(), text,
135 MatchToMatchCore(match, template_url_service_, search_terms_data_.get()),
136 now, 1));
139 ShortcutsBackend::~ShortcutsBackend() {
140 if (db_) {
141 auto* db = db_.get();
142 db->AddRef();
143 db_ = nullptr;
144 if (!db_runner_->ReleaseSoon(FROM_HERE, db))
145 db->Release();
149 // static
150 ShortcutsDatabase::Shortcut::MatchCore ShortcutsBackend::MatchToMatchCore(
151 const AutocompleteMatch& match,
152 TemplateURLService* template_url_service,
153 SearchTermsData* search_terms_data) {
154 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
155 const AutocompleteMatch& normalized_match =
156 AutocompleteMatch::IsSpecializedSearchType(match.type)
157 ? BaseSearchProvider::CreateSearchSuggestion(
158 match.search_terms_args->search_terms, match_type,
159 (match.transition == ui::PAGE_TRANSITION_KEYWORD),
160 match.GetTemplateURL(template_url_service, false),
161 *search_terms_data)
162 : match;
163 return ShortcutsDatabase::Shortcut::MatchCore(
164 normalized_match.fill_into_edit, normalized_match.destination_url,
165 normalized_match.contents,
166 StripMatchMarkers(normalized_match.contents_class),
167 normalized_match.description,
168 StripMatchMarkers(normalized_match.description_class),
169 normalized_match.transition, match_type, normalized_match.keyword);
172 void ShortcutsBackend::ShutdownOnUIThread() {
173 history_service_observer_.RemoveAll();
176 void ShortcutsBackend::OnURLsDeleted(history::HistoryService* history_service,
177 bool all_history,
178 bool expired,
179 const history::URLRows& deleted_rows,
180 const std::set<GURL>& favicon_urls) {
181 if (!initialized())
182 return;
184 if (all_history) {
185 DeleteAllShortcuts();
186 return;
189 ShortcutsDatabase::ShortcutIDs shortcut_ids;
190 for (const auto& guid_pair : guid_map_) {
191 if (std::find_if(
192 deleted_rows.begin(), deleted_rows.end(),
193 history::URLRow::URLRowHasURL(
194 guid_pair.second->second.match_core.destination_url)) !=
195 deleted_rows.end()) {
196 shortcut_ids.push_back(guid_pair.first);
199 DeleteShortcutsWithIDs(shortcut_ids);
202 void ShortcutsBackend::InitInternal() {
203 DCHECK(current_state_ == INITIALIZING);
204 db_->Init();
205 ShortcutsDatabase::GuidToShortcutMap shortcuts;
206 db_->LoadShortcuts(&shortcuts);
207 temp_shortcuts_map_.reset(new ShortcutMap);
208 temp_guid_map_.reset(new GuidMap);
209 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
210 shortcuts.begin());
211 it != shortcuts.end(); ++it) {
212 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
213 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
215 main_runner_->PostTask(FROM_HERE,
216 base::Bind(&ShortcutsBackend::InitCompleted, this));
219 void ShortcutsBackend::InitCompleted() {
220 temp_guid_map_->swap(guid_map_);
221 temp_shortcuts_map_->swap(shortcuts_map_);
222 temp_shortcuts_map_.reset(NULL);
223 temp_guid_map_.reset(NULL);
224 current_state_ = INITIALIZED;
225 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
226 OnShortcutsLoaded());
229 bool ShortcutsBackend::AddShortcut(
230 const ShortcutsDatabase::Shortcut& shortcut) {
231 if (!initialized())
232 return false;
233 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
234 guid_map_[shortcut.id] = shortcuts_map_.insert(
235 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
236 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
237 OnShortcutsChanged());
238 return no_db_access_ ||
239 db_runner_->PostTask(
240 FROM_HERE,
241 base::Bind(base::IgnoreResult(&ShortcutsDatabase::AddShortcut),
242 db_.get(), shortcut));
245 bool ShortcutsBackend::UpdateShortcut(
246 const ShortcutsDatabase::Shortcut& shortcut) {
247 if (!initialized())
248 return false;
249 GuidMap::iterator it(guid_map_.find(shortcut.id));
250 if (it != guid_map_.end())
251 shortcuts_map_.erase(it->second);
252 guid_map_[shortcut.id] = shortcuts_map_.insert(
253 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
254 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
255 OnShortcutsChanged());
256 return no_db_access_ ||
257 db_runner_->PostTask(
258 FROM_HERE,
259 base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut),
260 db_.get(), shortcut));
263 bool ShortcutsBackend::DeleteShortcutsWithIDs(
264 const 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 db_runner_->PostTask(
278 FROM_HERE,
279 base::Bind(
280 base::IgnoreResult(&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 ShortcutsDatabase::ShortcutIDs shortcut_ids;
288 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
289 if (exact_match ? (it->second->second.match_core.destination_url == url)
290 : base::StartsWith(
291 it->second->second.match_core.destination_url.spec(),
292 url_spec, base::CompareCase::SENSITIVE)) {
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 db_runner_->PostTask(
304 FROM_HERE,
305 base::Bind(
306 base::IgnoreResult(&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 db_runner_->PostTask(
319 FROM_HERE, base::Bind(base::IgnoreResult(
320 &ShortcutsDatabase::DeleteAllShortcuts),
321 db_.get()));