| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "chrome/browser/history/history_notifications.h" | 21 #include "chrome/browser/history/history_notifications.h" |
| 22 #include "chrome/browser/history/history_service.h" | 22 #include "chrome/browser/history/history_service.h" |
| 23 #include "chrome/browser/history/shortcuts_database.h" | 23 #include "chrome/browser/history/shortcuts_database.h" |
| 24 #include "chrome/browser/omnibox/omnibox_log.h" | 24 #include "chrome/browser/omnibox/omnibox_log.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/common/autocomplete_match_type.h" | 26 #include "chrome/common/autocomplete_match_type.h" |
| 27 #include "chrome/common/chrome_constants.h" | 27 #include "chrome/common/chrome_constants.h" |
| 28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
| 30 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
| 31 #include "extensions/browser/extension_registry.h" |
| 31 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
| 32 | 33 |
| 33 using content::BrowserThread; | 34 using content::BrowserThread; |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 // Takes Match classification vector and removes all matched positions, | 38 // Takes Match classification vector and removes all matched positions, |
| 38 // compacting repetitions if necessary. | 39 // compacting repetitions if necessary. |
| 39 std::string StripMatchMarkers(const ACMatchClassifications& matches) { | 40 std::string StripMatchMarkers(const ACMatchClassifications& matches) { |
| 40 ACMatchClassifications unmatched; | 41 ACMatchClassifications unmatched; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 66 } | 67 } |
| 67 | 68 |
| 68 } // namespace | 69 } // namespace |
| 69 | 70 |
| 70 | 71 |
| 71 // ShortcutsBackend ----------------------------------------------------------- | 72 // ShortcutsBackend ----------------------------------------------------------- |
| 72 | 73 |
| 73 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) | 74 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) |
| 74 : profile_(profile), | 75 : profile_(profile), |
| 75 current_state_(NOT_INITIALIZED), | 76 current_state_(NOT_INITIALIZED), |
| 77 extension_registry_observer_(this), |
| 76 no_db_access_(suppress_db) { | 78 no_db_access_(suppress_db) { |
| 77 if (!suppress_db) { | 79 if (!suppress_db) { |
| 78 db_ = new history::ShortcutsDatabase( | 80 db_ = new history::ShortcutsDatabase( |
| 79 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); | 81 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); |
| 80 } | 82 } |
| 81 // |profile| can be NULL in tests. | 83 // |profile| can be NULL in tests. |
| 82 if (profile) { | 84 if (profile) { |
| 83 notification_registrar_.Add( | 85 notification_registrar_.Add( |
| 84 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
| 85 content::Source<Profile>(profile)); | |
| 86 notification_registrar_.Add( | |
| 87 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 86 this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 88 content::Source<Profile>(profile)); | 87 content::Source<Profile>(profile)); |
| 88 extension_registry_observer_.Add( |
| 89 extensions::ExtensionRegistry::Get(profile)); |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 92 bool ShortcutsBackend::Init() { | 93 bool ShortcutsBackend::Init() { |
| 93 if (current_state_ != NOT_INITIALIZED) | 94 if (current_state_ != NOT_INITIALIZED) |
| 94 return false; | 95 return false; |
| 95 | 96 |
| 96 if (no_db_access_) { | 97 if (no_db_access_) { |
| 97 current_state_ = INITIALIZED; | 98 current_state_ = INITIALIZED; |
| 98 return true; | 99 return true; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 161 |
| 161 void ShortcutsBackend::ShutdownOnUIThread() { | 162 void ShortcutsBackend::ShutdownOnUIThread() { |
| 162 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 163 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
| 163 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 164 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 notification_registrar_.RemoveAll(); | 165 notification_registrar_.RemoveAll(); |
| 165 } | 166 } |
| 166 | 167 |
| 167 void ShortcutsBackend::Observe(int type, | 168 void ShortcutsBackend::Observe(int type, |
| 168 const content::NotificationSource& source, | 169 const content::NotificationSource& source, |
| 169 const content::NotificationDetails& details) { | 170 const content::NotificationDetails& details) { |
| 170 if (!initialized()) | |
| 171 return; | |
| 172 | |
| 173 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | |
| 174 // When an extension is unloaded, we want to remove any Shortcuts associated | |
| 175 // with it. | |
| 176 DeleteShortcutsWithURL(content::Details<extensions::UnloadedExtensionInfo>( | |
| 177 details)->extension->url(), false); | |
| 178 return; | |
| 179 } | |
| 180 | |
| 181 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type); | 171 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type); |
| 182 const history::URLsDeletedDetails* deleted_details = | 172 const history::URLsDeletedDetails* deleted_details = |
| 183 content::Details<const history::URLsDeletedDetails>(details).ptr(); | 173 content::Details<const history::URLsDeletedDetails>(details).ptr(); |
| 184 if (deleted_details->all_history) { | 174 if (deleted_details->all_history) { |
| 185 DeleteAllShortcuts(); | 175 DeleteAllShortcuts(); |
| 186 return; | 176 return; |
| 187 } | 177 } |
| 188 | 178 |
| 189 const history::URLRows& rows(deleted_details->rows); | 179 const history::URLRows& rows(deleted_details->rows); |
| 190 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; | 180 history::ShortcutsDatabase::ShortcutIDs shortcut_ids; |
| 191 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); | 181 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); |
| 192 ++it) { | 182 ++it) { |
| 193 if (std::find_if( | 183 if (std::find_if( |
| 194 rows.begin(), rows.end(), history::URLRow::URLRowHasURL( | 184 rows.begin(), rows.end(), history::URLRow::URLRowHasURL( |
| 195 it->second->second.match_core.destination_url)) != rows.end()) | 185 it->second->second.match_core.destination_url)) != rows.end()) |
| 196 shortcut_ids.push_back(it->first); | 186 shortcut_ids.push_back(it->first); |
| 197 } | 187 } |
| 198 DeleteShortcutsWithIDs(shortcut_ids); | 188 DeleteShortcutsWithIDs(shortcut_ids); |
| 199 } | 189 } |
| 200 | 190 |
| 191 void ShortcutsBackend::OnExtensionUnloaded( |
| 192 content::BrowserContext* browser_context, |
| 193 const extensions::Extension* extension, |
| 194 extensions::UnloadedExtensionInfo::Reason reason) { |
| 195 if (!initialized()) |
| 196 return; |
| 197 |
| 198 // When an extension is unloaded, we want to remove any Shortcuts associated |
| 199 // with it. |
| 200 DeleteShortcutsWithURL(extension->url(), false); |
| 201 } |
| 202 |
| 201 void ShortcutsBackend::InitInternal() { | 203 void ShortcutsBackend::InitInternal() { |
| 202 DCHECK(current_state_ == INITIALIZING); | 204 DCHECK(current_state_ == INITIALIZING); |
| 203 db_->Init(); | 205 db_->Init(); |
| 204 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; | 206 history::ShortcutsDatabase::GuidToShortcutMap shortcuts; |
| 205 db_->LoadShortcuts(&shortcuts); | 207 db_->LoadShortcuts(&shortcuts); |
| 206 temp_shortcuts_map_.reset(new ShortcutMap); | 208 temp_shortcuts_map_.reset(new ShortcutMap); |
| 207 temp_guid_map_.reset(new GuidMap); | 209 temp_guid_map_.reset(new GuidMap); |
| 208 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it( | 210 for (history::ShortcutsDatabase::GuidToShortcutMap::const_iterator it( |
| 209 shortcuts.begin()); it != shortcuts.end(); ++it) { | 211 shortcuts.begin()); it != shortcuts.end(); ++it) { |
| 210 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert( | 212 (*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert( |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 guid_map_.clear(); | 316 guid_map_.clear(); |
| 315 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 317 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
| 316 OnShortcutsChanged()); | 318 OnShortcutsChanged()); |
| 317 return no_db_access_ || | 319 return no_db_access_ || |
| 318 BrowserThread::PostTask( | 320 BrowserThread::PostTask( |
| 319 BrowserThread::DB, FROM_HERE, | 321 BrowserThread::DB, FROM_HERE, |
| 320 base::Bind(base::IgnoreResult( | 322 base::Bind(base::IgnoreResult( |
| 321 &history::ShortcutsDatabase::DeleteAllShortcuts), | 323 &history::ShortcutsDatabase::DeleteAllShortcuts), |
| 322 db_.get())); | 324 db_.get())); |
| 323 } | 325 } |
| OLD | NEW |