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/autocomplete/shortcuts_database.h" | 16 #include "chrome/browser/autocomplete/shortcuts_database.h" |
17 #include "chrome/browser/chrome_notification_types.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" | 18 #include "chrome/browser/history/history_service.h" |
| 19 #include "chrome/browser/history/history_service_factory.h" |
20 #include "chrome/browser/omnibox/omnibox_log.h" | 20 #include "chrome/browser/omnibox/omnibox_log.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/search_engines/template_url_service_factory.h" | 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
23 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 23 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
24 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
25 #include "components/omnibox/autocomplete_input.h" | 25 #include "components/omnibox/autocomplete_input.h" |
26 #include "components/omnibox/autocomplete_match.h" | 26 #include "components/omnibox/autocomplete_match.h" |
27 #include "components/omnibox/autocomplete_match_type.h" | 27 #include "components/omnibox/autocomplete_match_type.h" |
28 #include "components/omnibox/autocomplete_result.h" | 28 #include "components/omnibox/autocomplete_result.h" |
29 #include "components/omnibox/base_search_provider.h" | 29 #include "components/omnibox/base_search_provider.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 | 76 |
77 // ShortcutsBackend ----------------------------------------------------------- | 77 // ShortcutsBackend ----------------------------------------------------------- |
78 | 78 |
79 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) | 79 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) |
80 : profile_(profile), | 80 : profile_(profile), |
81 current_state_(NOT_INITIALIZED), | 81 current_state_(NOT_INITIALIZED), |
| 82 history_service_observer_(this), |
82 no_db_access_(suppress_db) { | 83 no_db_access_(suppress_db) { |
83 if (!suppress_db) { | 84 if (!suppress_db) { |
84 db_ = new history::ShortcutsDatabase( | 85 db_ = new history::ShortcutsDatabase( |
85 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); | 86 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); |
86 } | 87 } |
87 // |profile| can be NULL in tests. | 88 // |profile| can be NULL in tests. |
88 if (profile) { | 89 if (profile) { |
89 #if defined(ENABLE_EXTENSIONS) | 90 #if defined(ENABLE_EXTENSIONS) |
90 notification_registrar_.Add( | 91 notification_registrar_.Add( |
91 this, | 92 this, |
92 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 93 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
93 content::Source<Profile>(profile)); | 94 content::Source<Profile>(profile)); |
94 #endif | 95 #endif |
95 notification_registrar_.Add( | 96 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
96 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 97 profile, ServiceAccessType::EXPLICIT_ACCESS); |
97 content::Source<Profile>(profile)); | 98 if (hs) |
| 99 history_service_observer_.Add(hs); |
98 } | 100 } |
99 } | 101 } |
100 | 102 |
101 bool ShortcutsBackend::Init() { | 103 bool ShortcutsBackend::Init() { |
102 if (current_state_ != NOT_INITIALIZED) | 104 if (current_state_ != NOT_INITIALIZED) |
103 return false; | 105 return false; |
104 | 106 |
105 if (no_db_access_) { | 107 if (no_db_access_) { |
106 current_state_ = INITIALIZED; | 108 current_state_ = INITIALIZED; |
107 return true; | 109 return true; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 StripMatchMarkers(normalized_match.contents_class), | 169 StripMatchMarkers(normalized_match.contents_class), |
168 normalized_match.description, | 170 normalized_match.description, |
169 StripMatchMarkers(normalized_match.description_class), | 171 StripMatchMarkers(normalized_match.description_class), |
170 normalized_match.transition, match_type, normalized_match.keyword); | 172 normalized_match.transition, match_type, normalized_match.keyword); |
171 } | 173 } |
172 | 174 |
173 void ShortcutsBackend::ShutdownOnUIThread() { | 175 void ShortcutsBackend::ShutdownOnUIThread() { |
174 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 176 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
175 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
176 notification_registrar_.RemoveAll(); | 178 notification_registrar_.RemoveAll(); |
| 179 history_service_observer_.RemoveAll(); |
177 } | 180 } |
178 | 181 |
179 void ShortcutsBackend::Observe(int type, | 182 void ShortcutsBackend::Observe(int type, |
180 const content::NotificationSource& source, | 183 const content::NotificationSource& source, |
181 const content::NotificationDetails& details) { | 184 const content::NotificationDetails& details) { |
| 185 #if defined(ENABLE_EXTENSIONS) |
| 186 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, type); |
182 if (!initialized()) | 187 if (!initialized()) |
183 return; | 188 return; |
184 | 189 |
185 #if defined(ENABLE_EXTENSIONS) | 190 // When an extension is unloaded, we want to remove any Shortcuts associated |
186 if (type == extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | 191 // with it. |
187 // When an extension is unloaded, we want to remove any Shortcuts associated | 192 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( |
188 // with it. | 193 details)->extension->url(), |
189 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( | 194 false); |
190 details)->extension->url(), 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 |