OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/shortcuts_backend.h" | 5 #include "chrome/browser/autocomplete/shortcuts_backend.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/guid.h" | 13 #include "base/guid.h" |
14 #include "base/i18n/case_conversion.h" | 14 #include "base/i18n/case_conversion.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "chrome/browser/chrome_notification_types.h" | |
17 #include "chrome/browser/history/history_notifications.h" | |
18 #include "chrome/browser/history/history_service.h" | 16 #include "chrome/browser/history/history_service.h" |
| 17 #include "chrome/browser/history/history_service_factory.h" |
19 #include "chrome/browser/history/shortcuts_database.h" | 18 #include "chrome/browser/history/shortcuts_database.h" |
20 #include "chrome/browser/omnibox/omnibox_log.h" | 19 #include "chrome/browser/omnibox/omnibox_log.h" |
21 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/search_engines/template_url_service_factory.h" | 21 #include "chrome/browser/search_engines/template_url_service_factory.h" |
23 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 22 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
24 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
25 #include "components/omnibox/autocomplete_input.h" | 24 #include "components/omnibox/autocomplete_input.h" |
26 #include "components/omnibox/autocomplete_match.h" | 25 #include "components/omnibox/autocomplete_match.h" |
27 #include "components/omnibox/autocomplete_match_type.h" | 26 #include "components/omnibox/autocomplete_match_type.h" |
28 #include "components/omnibox/autocomplete_result.h" | 27 #include "components/omnibox/autocomplete_result.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 71 } |
73 | 72 |
74 } // namespace | 73 } // namespace |
75 | 74 |
76 | 75 |
77 // ShortcutsBackend ----------------------------------------------------------- | 76 // ShortcutsBackend ----------------------------------------------------------- |
78 | 77 |
79 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) | 78 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) |
80 : profile_(profile), | 79 : profile_(profile), |
81 current_state_(NOT_INITIALIZED), | 80 current_state_(NOT_INITIALIZED), |
| 81 history_service_observer_(this), |
82 no_db_access_(suppress_db) { | 82 no_db_access_(suppress_db) { |
83 if (!suppress_db) { | 83 if (!suppress_db) { |
84 db_ = new history::ShortcutsDatabase( | 84 db_ = new history::ShortcutsDatabase( |
85 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); | 85 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); |
86 } | 86 } |
87 // |profile| can be NULL in tests. | 87 // |profile| can be NULL in tests. |
88 if (profile) { | 88 if (profile) { |
89 #if defined(ENABLE_EXTENSIONS) | 89 #if defined(ENABLE_EXTENSIONS) |
90 notification_registrar_.Add( | 90 notification_registrar_.Add( |
91 this, | 91 this, |
92 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 92 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
93 content::Source<Profile>(profile)); | 93 content::Source<Profile>(profile)); |
94 #endif | 94 #endif |
95 notification_registrar_.Add( | 95 HistoryService* hs = |
96 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 96 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
97 content::Source<Profile>(profile)); | 97 if (hs) |
| 98 history_service_observer_.Add(hs); |
98 } | 99 } |
99 } | 100 } |
100 | 101 |
101 bool ShortcutsBackend::Init() { | 102 bool ShortcutsBackend::Init() { |
102 if (current_state_ != NOT_INITIALIZED) | 103 if (current_state_ != NOT_INITIALIZED) |
103 return false; | 104 return false; |
104 | 105 |
105 if (no_db_access_) { | 106 if (no_db_access_) { |
106 current_state_ = INITIALIZED; | 107 current_state_ = INITIALIZED; |
107 return true; | 108 return true; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 StripMatchMarkers(normalized_match.contents_class), | 168 StripMatchMarkers(normalized_match.contents_class), |
168 normalized_match.description, | 169 normalized_match.description, |
169 StripMatchMarkers(normalized_match.description_class), | 170 StripMatchMarkers(normalized_match.description_class), |
170 normalized_match.transition, match_type, normalized_match.keyword); | 171 normalized_match.transition, match_type, normalized_match.keyword); |
171 } | 172 } |
172 | 173 |
173 void ShortcutsBackend::ShutdownOnUIThread() { | 174 void ShortcutsBackend::ShutdownOnUIThread() { |
174 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 175 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
175 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
176 notification_registrar_.RemoveAll(); | 177 notification_registrar_.RemoveAll(); |
| 178 history_service_observer_.RemoveAll(); |
177 } | 179 } |
178 | 180 |
179 void ShortcutsBackend::Observe(int type, | 181 void ShortcutsBackend::Observe(int type, |
180 const content::NotificationSource& source, | 182 const content::NotificationSource& source, |
181 const content::NotificationDetails& details) { | 183 const content::NotificationDetails& details) { |
| 184 #if defined(ENABLE_EXTENSIONS) |
| 185 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, type); |
182 if (!initialized()) | 186 if (!initialized()) |
183 return; | 187 return; |
184 | 188 |
185 #if defined(ENABLE_EXTENSIONS) | 189 // When an extension is unloaded, we want to remove any Shortcuts associated |
186 if (type == extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | 190 // with it. |
187 // When an extension is unloaded, we want to remove any Shortcuts associated | 191 DeleteShortcutsWithURL( |
188 // with it. | 192 content::Details<extensions::UnloadedExtensionInfo>( |
189 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( | 193 details)->extension->url(), |
190 details)->extension->url(), false); | 194 false); |
| 195 #endif |
| 196 } |
| 197 |
| 198 void ShortcutsBackend::OnURLsDeleted(HistoryService* history_service, |
| 199 bool all_history, |
| 200 bool expired, |
| 201 const history::URLRows& deleted_rows, |
| 202 const std::set<GURL>& favicon_urls) { |
| 203 if (!initialized()) |
191 return; | 204 return; |
192 } | |
193 #endif | |
194 | 205 |
195 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type); | 206 if (all_history) { |
196 const history::URLsDeletedDetails* deleted_details = | |
197 content::Details<const history::URLsDeletedDetails>(details).ptr(); | |
198 if (deleted_details->all_history) { | |
199 DeleteAllShortcuts(); | 207 DeleteAllShortcuts(); |
200 return; | 208 return; |
201 } | 209 } |
202 | 210 |
203 const history::URLRows& rows(deleted_details->rows); | |
204 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; | 211 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; |
205 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); | 212 for (const auto& guid_pair : guid_map_) { |
206 ++it) { | |
207 if (std::find_if( | 213 if (std::find_if( |
208 rows.begin(), rows.end(), history::URLRow::URLRowHasURL( | 214 deleted_rows.begin(), deleted_rows.end(), |
209 it->second->second.match_core.destination_url)) != rows.end()) | 215 history::URLRow::URLRowHasURL( |
210 shortcut_ids.push_back(it->first); | 216 guid_pair.second->second.match_core.destination_url)) != |
| 217 deleted_rows.end()) { |
| 218 shortcut_ids.push_back(guid_pair.first); |
| 219 } |
211 } | 220 } |
212 DeleteShortcutsWithIDs(shortcut_ids); | 221 DeleteShortcutsWithIDs(shortcut_ids); |
213 } | 222 } |
214 | 223 |
215 void ShortcutsBackend::InitInternal() { | 224 void ShortcutsBackend::InitInternal() { |
216 DCHECK(current_state_ == INITIALIZING); | 225 DCHECK(current_state_ == INITIALIZING); |
217 db_->Init(); | 226 db_->Init(); |
218 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; | 227 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; |
219 db_->LoadShortcuts(&shortcuts); | 228 db_->LoadShortcuts(&shortcuts); |
220 temp_shortcuts_map_.reset(new ShortcutMap); | 229 temp_shortcuts_map_.reset(new ShortcutMap); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 guid_map_.clear(); | 337 guid_map_.clear(); |
329 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 338 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
330 OnShortcutsChanged()); | 339 OnShortcutsChanged()); |
331 return no_db_access_ || | 340 return no_db_access_ || |
332 BrowserThread::PostTask( | 341 BrowserThread::PostTask( |
333 BrowserThread::DB, FROM_HERE, | 342 BrowserThread::DB, FROM_HERE, |
334 base::Bind(base::IgnoreResult( | 343 base::Bind(base::IgnoreResult( |
335 &history::ShortcutsDatabase::DeleteAllShortcuts), | 344 &history::ShortcutsDatabase::DeleteAllShortcuts), |
336 db_.get())); | 345 db_.get())); |
337 } | 346 } |
OLD | NEW |