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" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/history/history_notifications.h" | 17 #include "chrome/browser/history/history_notifications.h" |
| 18 #include "chrome/browser/history/history_service.h" | 18 #include "chrome/browser/history/history_service.h" |
| 19 #include "chrome/browser/history/history_service_factory.h" | |
| 19 #include "chrome/browser/history/shortcuts_database.h" | 20 #include "chrome/browser/history/shortcuts_database.h" |
| 20 #include "chrome/browser/omnibox/omnibox_log.h" | 21 #include "chrome/browser/omnibox/omnibox_log.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" | 23 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 23 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 24 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 24 #include "chrome/common/chrome_constants.h" | 25 #include "chrome/common/chrome_constants.h" |
| 25 #include "components/omnibox/autocomplete_input.h" | 26 #include "components/omnibox/autocomplete_input.h" |
| 26 #include "components/omnibox/autocomplete_match.h" | 27 #include "components/omnibox/autocomplete_match.h" |
| 27 #include "components/omnibox/autocomplete_match_type.h" | 28 #include "components/omnibox/autocomplete_match_type.h" |
| 28 #include "components/omnibox/autocomplete_result.h" | 29 #include "components/omnibox/autocomplete_result.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 } | 73 } |
| 73 | 74 |
| 74 } // namespace | 75 } // namespace |
| 75 | 76 |
| 76 | 77 |
| 77 // ShortcutsBackend ----------------------------------------------------------- | 78 // ShortcutsBackend ----------------------------------------------------------- |
| 78 | 79 |
| 79 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) | 80 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) |
| 80 : profile_(profile), | 81 : profile_(profile), |
| 81 current_state_(NOT_INITIALIZED), | 82 current_state_(NOT_INITIALIZED), |
| 82 no_db_access_(suppress_db) { | 83 no_db_access_(suppress_db), |
| 84 history_service_observer_(this) { | |
| 83 if (!suppress_db) { | 85 if (!suppress_db) { |
| 84 db_ = new history::ShortcutsDatabase( | 86 db_ = new history::ShortcutsDatabase( |
| 85 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); | 87 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); |
| 86 } | 88 } |
| 87 // |profile| can be NULL in tests. | 89 // |profile| can be NULL in tests. |
| 88 if (profile) { | 90 if (profile) { |
| 89 #if defined(ENABLE_EXTENSIONS) | 91 #if defined(ENABLE_EXTENSIONS) |
| 90 notification_registrar_.Add( | 92 notification_registrar_.Add( |
| 91 this, | 93 this, |
| 92 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 94 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 93 content::Source<Profile>(profile)); | 95 content::Source<Profile>(profile)); |
| 94 #endif | 96 #endif |
| 95 notification_registrar_.Add( | 97 HistoryService* hs = |
| 96 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 98 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
| 97 content::Source<Profile>(profile)); | 99 if (hs) |
| 100 history_service_observer_.Add(hs); | |
| 98 } | 101 } |
| 99 } | 102 } |
| 100 | 103 |
| 101 bool ShortcutsBackend::Init() { | 104 bool ShortcutsBackend::Init() { |
| 102 if (current_state_ != NOT_INITIALIZED) | 105 if (current_state_ != NOT_INITIALIZED) |
| 103 return false; | 106 return false; |
| 104 | 107 |
| 105 if (no_db_access_) { | 108 if (no_db_access_) { |
| 106 current_state_ = INITIALIZED; | 109 current_state_ = INITIALIZED; |
| 107 return true; | 110 return true; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 StripMatchMarkers(normalized_match.contents_class), | 170 StripMatchMarkers(normalized_match.contents_class), |
| 168 normalized_match.description, | 171 normalized_match.description, |
| 169 StripMatchMarkers(normalized_match.description_class), | 172 StripMatchMarkers(normalized_match.description_class), |
| 170 normalized_match.transition, match_type, normalized_match.keyword); | 173 normalized_match.transition, match_type, normalized_match.keyword); |
| 171 } | 174 } |
| 172 | 175 |
| 173 void ShortcutsBackend::ShutdownOnUIThread() { | 176 void ShortcutsBackend::ShutdownOnUIThread() { |
| 174 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 177 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
| 175 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 178 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 176 notification_registrar_.RemoveAll(); | 179 notification_registrar_.RemoveAll(); |
| 180 history_service_observer_.RemoveAll(); | |
| 177 } | 181 } |
| 178 | 182 |
| 179 void ShortcutsBackend::Observe(int type, | 183 void ShortcutsBackend::Observe(int type, |
| 180 const content::NotificationSource& source, | 184 const content::NotificationSource& source, |
| 181 const content::NotificationDetails& details) { | 185 const content::NotificationDetails& details) { |
| 182 if (!initialized()) | 186 if (!initialized()) |
| 183 return; | 187 return; |
| 184 | 188 |
| 185 #if defined(ENABLE_EXTENSIONS) | 189 #if defined(ENABLE_EXTENSIONS) |
| 186 if (type == extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | 190 if (type == extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { |
|
sdefresne
2014/12/04 17:21:12
Same here, this is the only notification listened
nshaik
2014/12/07 09:34:49
Done.
| |
| 187 // When an extension is unloaded, we want to remove any Shortcuts associated | 191 // When an extension is unloaded, we want to remove any Shortcuts associated |
| 188 // with it. | 192 // with it. |
| 189 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( | 193 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( |
| 190 details)->extension->url(), false); | 194 details)->extension->url(), false); |
| 191 return; | 195 return; |
| 192 } | 196 } |
| 193 #endif | 197 #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 } | 198 } |
| 214 | 199 |
| 215 void ShortcutsBackend::InitInternal() { | 200 void ShortcutsBackend::InitInternal() { |
| 216 DCHECK(current_state_ == INITIALIZING); | 201 DCHECK(current_state_ == INITIALIZING); |
| 217 db_->Init(); | 202 db_->Init(); |
| 218 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; | 203 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; |
| 219 db_->LoadShortcuts(&shortcuts); | 204 db_->LoadShortcuts(&shortcuts); |
| 220 temp_shortcuts_map_.reset(new ShortcutMap); | 205 temp_shortcuts_map_.reset(new ShortcutMap); |
| 221 temp_guid_map_.reset(new GuidMap); | 206 temp_guid_map_.reset(new GuidMap); |
| 222 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it( | 207 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 guid_map_.clear(); | 313 guid_map_.clear(); |
| 329 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 314 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
| 330 OnShortcutsChanged()); | 315 OnShortcutsChanged()); |
| 331 return no_db_access_ || | 316 return no_db_access_ || |
| 332 BrowserThread::PostTask( | 317 BrowserThread::PostTask( |
| 333 BrowserThread::DB, FROM_HERE, | 318 BrowserThread::DB, FROM_HERE, |
| 334 base::Bind(base::IgnoreResult( | 319 base::Bind(base::IgnoreResult( |
| 335 &history::ShortcutsDatabase::DeleteAllShortcuts), | 320 &history::ShortcutsDatabase::DeleteAllShortcuts), |
| 336 db_.get())); | 321 db_.get())); |
| 337 } | 322 } |
| 323 | |
| 324 void ShortcutsBackend::OnURLsDeleted( | |
| 325 HistoryService* history_service, | |
| 326 const history::URLsDeletedDetails& deleted_details) { | |
| 327 if (!initialized()) | |
| 328 return; | |
| 329 if (deleted_details.all_history) { | |
| 330 DeleteAllShortcuts(); | |
| 331 return; | |
| 332 } | |
| 333 | |
| 334 const history::URLRows& rows(deleted_details.rows); | |
| 335 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; | |
| 336 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); | |
|
sdefresne
2014/12/04 17:21:12
nit: c++11 for-loop and auto are now allowed, so t
nshaik
2014/12/07 09:34:49
Done.
| |
| 337 ++it) { | |
| 338 if (std::find_if(rows.begin(), rows.end(), | |
| 339 history::URLRow::URLRowHasURL( | |
| 340 it->second->second.match_core.destination_url)) != | |
| 341 rows.end()) | |
| 342 shortcut_ids.push_back(it->first); | |
| 343 } | |
| 344 DeleteShortcutsWithIDs(shortcut_ids); | |
| 345 } | |
| OLD | NEW |