Disable PPAPINaClNewlibTest.URLLoader2 test again.
[chromium-blink-merge.git] / components / omnibox / shortcuts_backend.cc
blob945639bf06155567d96838a0e7aca17701f16520
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/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/autocomplete_input.h"
19 #include "components/omnibox/autocomplete_match.h"
20 #include "components/omnibox/autocomplete_match_type.h"
21 #include "components/omnibox/autocomplete_result.h"
22 #include "components/omnibox/base_search_provider.h"
23 #include "components/omnibox/omnibox_log.h"
24 #include "components/omnibox/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, true);
123 ++it) {
124 if (match.destination_url == it->second.match_core.destination_url) {
125 UpdateShortcut(ShortcutsDatabase::Shortcut(
126 it->second.id, text, MatchToMatchCore(match, template_url_service_,
127 search_terms_data_.get()),
128 now, it->second.number_of_hits + 1));
129 return;
132 AddShortcut(ShortcutsDatabase::Shortcut(
133 base::GenerateGUID(), text,
134 MatchToMatchCore(match, template_url_service_, search_terms_data_.get()),
135 now, 1));
138 ShortcutsBackend::~ShortcutsBackend() {
139 if (db_) {
140 auto* db = db_.get();
141 db->AddRef();
142 db_ = nullptr;
143 if (!db_runner_->ReleaseSoon(FROM_HERE, db))
144 db->Release();
148 // static
149 ShortcutsDatabase::Shortcut::MatchCore ShortcutsBackend::MatchToMatchCore(
150 const AutocompleteMatch& match,
151 TemplateURLService* template_url_service,
152 SearchTermsData* search_terms_data) {
153 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
154 const AutocompleteMatch& normalized_match =
155 AutocompleteMatch::IsSpecializedSearchType(match.type)
156 ? BaseSearchProvider::CreateSearchSuggestion(
157 match.search_terms_args->search_terms, match_type,
158 (match.transition == ui::PAGE_TRANSITION_KEYWORD),
159 match.GetTemplateURL(template_url_service, false),
160 *search_terms_data)
161 : match;
162 return ShortcutsDatabase::Shortcut::MatchCore(
163 normalized_match.fill_into_edit, normalized_match.destination_url,
164 normalized_match.contents,
165 StripMatchMarkers(normalized_match.contents_class),
166 normalized_match.description,
167 StripMatchMarkers(normalized_match.description_class),
168 normalized_match.transition, match_type, normalized_match.keyword);
171 void ShortcutsBackend::ShutdownOnUIThread() {
172 history_service_observer_.RemoveAll();
175 void ShortcutsBackend::OnURLsDeleted(history::HistoryService* history_service,
176 bool all_history,
177 bool expired,
178 const history::URLRows& deleted_rows,
179 const std::set<GURL>& favicon_urls) {
180 if (!initialized())
181 return;
183 if (all_history) {
184 DeleteAllShortcuts();
185 return;
188 ShortcutsDatabase::ShortcutIDs shortcut_ids;
189 for (const auto& guid_pair : guid_map_) {
190 if (std::find_if(
191 deleted_rows.begin(), deleted_rows.end(),
192 history::URLRow::URLRowHasURL(
193 guid_pair.second->second.match_core.destination_url)) !=
194 deleted_rows.end()) {
195 shortcut_ids.push_back(guid_pair.first);
198 DeleteShortcutsWithIDs(shortcut_ids);
201 void ShortcutsBackend::InitInternal() {
202 DCHECK(current_state_ == INITIALIZING);
203 db_->Init();
204 ShortcutsDatabase::GuidToShortcutMap shortcuts;
205 db_->LoadShortcuts(&shortcuts);
206 temp_shortcuts_map_.reset(new ShortcutMap);
207 temp_guid_map_.reset(new GuidMap);
208 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
209 shortcuts.begin());
210 it != shortcuts.end(); ++it) {
211 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
212 std::make_pair(base::i18n::ToLower(it->second.text), it->second));
214 main_runner_->PostTask(FROM_HERE,
215 base::Bind(&ShortcutsBackend::InitCompleted, this));
218 void ShortcutsBackend::InitCompleted() {
219 temp_guid_map_->swap(guid_map_);
220 temp_shortcuts_map_->swap(shortcuts_map_);
221 temp_shortcuts_map_.reset(NULL);
222 temp_guid_map_.reset(NULL);
223 current_state_ = INITIALIZED;
224 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
225 OnShortcutsLoaded());
228 bool ShortcutsBackend::AddShortcut(
229 const ShortcutsDatabase::Shortcut& shortcut) {
230 if (!initialized())
231 return false;
232 DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
233 guid_map_[shortcut.id] = shortcuts_map_.insert(
234 std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
235 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
236 OnShortcutsChanged());
237 return no_db_access_ ||
238 db_runner_->PostTask(
239 FROM_HERE,
240 base::Bind(base::IgnoreResult(&ShortcutsDatabase::AddShortcut),
241 db_.get(), shortcut));
244 bool ShortcutsBackend::UpdateShortcut(
245 const 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 db_runner_->PostTask(
257 FROM_HERE,
258 base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut),
259 db_.get(), shortcut));
262 bool ShortcutsBackend::DeleteShortcutsWithIDs(
263 const 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 db_runner_->PostTask(
277 FROM_HERE,
278 base::Bind(
279 base::IgnoreResult(&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 ShortcutsDatabase::ShortcutIDs shortcut_ids;
287 for (GuidMap::iterator it(guid_map_.begin()); it != guid_map_.end(); ) {
288 if (exact_match ? (it->second->second.match_core.destination_url == url)
289 : base::StartsWithASCII(
290 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 db_runner_->PostTask(
303 FROM_HERE,
304 base::Bind(
305 base::IgnoreResult(&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 db_runner_->PostTask(
318 FROM_HERE, base::Bind(base::IgnoreResult(
319 &ShortcutsDatabase::DeleteAllShortcuts),
320 db_.get()));