Chromium Code Reviews| 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), |
| 82 no_db_access_(suppress_db) { | 81 no_db_access_(suppress_db), |
| 82 history_service_observer_(this) { | |
| 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 = |
|
sdefresne
2014/12/08 17:41:06
Since you're adding a dependency on HistoryService
nshaik
2014/12/08 21:29:52
Done.
| |
| 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(type, extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED); | |
| 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(content::Details<extensions::UnloadedExtensionInfo>( |
| 188 // with it. | 192 details)->extension->url(), |
| 189 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( | 193 false); |
| 190 details)->extension->url(), false); | |
| 191 return; | |
| 192 } | |
| 193 #endif | 194 #endif |
| 194 | |
| 195 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type); | |
| 196 const history::URLsDeletedDetails* deleted_details = | |
| 197 content::Details<const history::URLsDeletedDetails>(details).ptr(); | |
| 198 if (deleted_details->all_history) { | |
| 199 DeleteAllShortcuts(); | |
| 200 return; | |
| 201 } | |
| 202 | |
| 203 const history::URLRows& rows(deleted_details->rows); | |
| 204 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; | |
| 205 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); | |
| 206 ++it) { | |
| 207 if (std::find_if( | |
| 208 rows.begin(), rows.end(), history::URLRow::URLRowHasURL( | |
| 209 it->second->second.match_core.destination_url)) != rows.end()) | |
| 210 shortcut_ids.push_back(it->first); | |
| 211 } | |
| 212 DeleteShortcutsWithIDs(shortcut_ids); | |
| 213 } | 195 } |
| 214 | 196 |
| 215 void ShortcutsBackend::InitInternal() { | 197 void ShortcutsBackend::InitInternal() { |
| 216 DCHECK(current_state_ == INITIALIZING); | 198 DCHECK(current_state_ == INITIALIZING); |
| 217 db_->Init(); | 199 db_->Init(); |
| 218 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; | 200 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; |
| 219 db_->LoadShortcuts(&shortcuts); | 201 db_->LoadShortcuts(&shortcuts); |
| 220 temp_shortcuts_map_.reset(new ShortcutMap); | 202 temp_shortcuts_map_.reset(new ShortcutMap); |
| 221 temp_guid_map_.reset(new GuidMap); | 203 temp_guid_map_.reset(new GuidMap); |
| 222 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it( | 204 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 guid_map_.clear(); | 310 guid_map_.clear(); |
| 329 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 311 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
| 330 OnShortcutsChanged()); | 312 OnShortcutsChanged()); |
| 331 return no_db_access_ || | 313 return no_db_access_ || |
| 332 BrowserThread::PostTask( | 314 BrowserThread::PostTask( |
| 333 BrowserThread::DB, FROM_HERE, | 315 BrowserThread::DB, FROM_HERE, |
| 334 base::Bind(base::IgnoreResult( | 316 base::Bind(base::IgnoreResult( |
| 335 &history::ShortcutsDatabase::DeleteAllShortcuts), | 317 &history::ShortcutsDatabase::DeleteAllShortcuts), |
| 336 db_.get())); | 318 db_.get())); |
| 337 } | 319 } |
| 320 | |
| 321 void ShortcutsBackend::OnURLsDeleted(HistoryService* history_service, | |
| 322 bool all_history, | |
| 323 bool expired, | |
| 324 const history::URLRows& deleted_rows, | |
| 325 const std::set<GURL>& favicon_urls) { | |
| 326 if (!initialized()) | |
| 327 return; | |
| 328 if (all_history) { | |
| 329 DeleteAllShortcuts(); | |
| 330 return; | |
| 331 } | |
| 332 | |
| 333 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; | |
| 334 for (const auto& guid_pair : guid_map_) { | |
| 335 if (std::find_if( | |
| 336 deleted_rows.begin(), deleted_rows.end(), | |
| 337 history::URLRow::URLRowHasURL( | |
| 338 guid_pair.second->second.match_core.destination_url)) != | |
| 339 deleted_rows.end()) | |
| 340 shortcut_ids.push_back(guid_pair.first); | |
| 341 } | |
| 342 DeleteShortcutsWithIDs(shortcut_ids); | |
| 343 } | |
| OLD | NEW |