Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / shortcuts_backend.cc
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.
4
5 #include "chrome/browser/autocomplete/shortcuts_backend.h"
6
7 #include <map>
8 #include <string>
9 #include <vector>
10
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/base_search_provider.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/history/history_notifications.h"
19 #include "chrome/browser/history/history_service.h"
20 #include "chrome/browser/history/shortcuts_database.h"
21 #include "chrome/browser/omnibox/omnibox_log.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search_engines/template_url_service_factory.h"
24 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "components/omnibox/autocomplete_input.h"
27 #include "components/omnibox/autocomplete_match.h"
28 #include "components/omnibox/autocomplete_match_type.h"
29 #include "components/omnibox/autocomplete_result.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"
35
36 using content::BrowserThread;
37
38 namespace {
39
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);
48   }
49   return AutocompleteMatch::ClassificationsToString(unmatched);
50 }
51
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;
61
62     case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
63       return type;
64
65     default:
66       return AutocompleteMatch::IsSearchType(type) ?
67           AutocompleteMatchType::SEARCH_HISTORY : type;
68   }
69 }
70
71 }  // namespace
72
73
74 // ShortcutsBackend -----------------------------------------------------------
75
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));
83   }
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));
93   }
94 }
95
96 bool ShortcutsBackend::Init() {
97   if (current_state_ != NOT_INITIALIZED)
98     return false;
99
100   if (no_db_access_) {
101     current_state_ = INITIALIZED;
102     return true;
103   }
104
105   current_state_ = INITIALIZING;
106   return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
107       base::Bind(&ShortcutsBackend::InitInternal, this));
108 }
109
110 bool ShortcutsBackend::DeleteShortcutsWithURL(const GURL& shortcut_url) {
111   return initialized() && DeleteShortcutsWithURL(shortcut_url, true);
112 }
113
114 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
115   observer_list_.AddObserver(obs);
116 }
117
118 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
119   observer_list_.RemoveObserver(obs);
120 }
121
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;
135     }
136   }
137   AddShortcut(history::ShortcutsDatabase::Shortcut(
138       base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
139 }
140
141 ShortcutsBackend::~ShortcutsBackend() {
142 }
143
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);
166 }
167
168 void ShortcutsBackend::ShutdownOnUIThread() {
169   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
170          BrowserThread::CurrentlyOn(BrowserThread::UI));
171   notification_registrar_.RemoveAll();
172 }
173
174 void ShortcutsBackend::Observe(int type,
175                                const content::NotificationSource& source,
176                                const content::NotificationDetails& details) {
177   if (!initialized())
178     return;
179
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;
186   }
187
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;
194   }
195
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);
204   }
205   DeleteShortcutsWithIDs(shortcut_ids);
206 }
207
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));
219   }
220   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
221       base::Bind(&ShortcutsBackend::InitCompleted, this));
222 }
223
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());
232 }
233
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));
249 }
250
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));
268 }
269
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);
279     }
280   }
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));
289 }
290
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;
305     }
306   }
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));
315 }
316
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()));
330 }