| 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 // The history system runs on a background thread so that potentially slow | 5 // The history system runs on a background thread so that potentially slow |
| 6 // database operations don't delay the browser. This backend processing is | 6 // database operations don't delay the browser. This backend processing is |
| 7 // represented by HistoryBackend. The HistoryService's job is to dispatch to | 7 // represented by HistoryBackend. The HistoryService's job is to dispatch to |
| 8 // that thread. | 8 // that thread. |
| 9 // | 9 // |
| 10 // Main thread History thread | 10 // Main thread History thread |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "base/compiler_specific.h" | 26 #include "base/compiler_specific.h" |
| 27 #include "base/location.h" | 27 #include "base/location.h" |
| 28 #include "base/memory/ref_counted.h" | 28 #include "base/memory/ref_counted.h" |
| 29 #include "base/message_loop/message_loop.h" | 29 #include "base/message_loop/message_loop.h" |
| 30 #include "base/path_service.h" | 30 #include "base/path_service.h" |
| 31 #include "base/prefs/pref_service.h" | 31 #include "base/prefs/pref_service.h" |
| 32 #include "base/thread_task_runner_handle.h" | 32 #include "base/thread_task_runner_handle.h" |
| 33 #include "base/threading/thread.h" | 33 #include "base/threading/thread.h" |
| 34 #include "base/time/time.h" | 34 #include "base/time/time.h" |
| 35 #include "chrome/browser/autocomplete/history_url_provider.h" | 35 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 36 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
| 37 #include "chrome/browser/browser_process.h" | 36 #include "chrome/browser/browser_process.h" |
| 38 #include "chrome/browser/chrome_notification_types.h" | 37 #include "chrome/browser/chrome_notification_types.h" |
| 39 #include "chrome/browser/history/download_row.h" | 38 #include "chrome/browser/history/download_row.h" |
| 40 #include "chrome/browser/history/history_backend.h" | 39 #include "chrome/browser/history/history_backend.h" |
| 41 #include "chrome/browser/history/history_notifications.h" | 40 #include "chrome/browser/history/history_notifications.h" |
| 42 #include "chrome/browser/history/history_types.h" | 41 #include "chrome/browser/history/history_types.h" |
| 43 #include "chrome/browser/history/in_memory_database.h" | 42 #include "chrome/browser/history/in_memory_database.h" |
| 44 #include "chrome/browser/history/in_memory_history_backend.h" | 43 #include "chrome/browser/history/in_memory_history_backend.h" |
| 45 #include "chrome/browser/history/in_memory_url_index.h" | 44 #include "chrome/browser/history/in_memory_url_index.h" |
| 46 #include "chrome/browser/history/top_sites.h" | 45 #include "chrome/browser/history/top_sites.h" |
| 47 #include "chrome/browser/history/visit_database.h" | 46 #include "chrome/browser/history/visit_database.h" |
| 48 #include "chrome/browser/history/visit_filter.h" | 47 #include "chrome/browser/history/visit_filter.h" |
| 49 #include "chrome/browser/history/web_history_service.h" | 48 #include "chrome/browser/history/web_history_service.h" |
| 50 #include "chrome/browser/history/web_history_service_factory.h" | 49 #include "chrome/browser/history/web_history_service_factory.h" |
| 51 #include "chrome/browser/profiles/profile.h" | 50 #include "chrome/browser/profiles/profile.h" |
| 52 #include "chrome/browser/ui/profile_error_dialog.h" | 51 #include "chrome/browser/ui/profile_error_dialog.h" |
| 53 #include "chrome/common/chrome_constants.h" | 52 #include "chrome/common/chrome_constants.h" |
| 54 #include "chrome/common/chrome_switches.h" | 53 #include "chrome/common/chrome_switches.h" |
| 55 #include "chrome/common/importer/imported_favicon_usage.h" | 54 #include "chrome/common/importer/imported_favicon_usage.h" |
| 56 #include "chrome/common/pref_names.h" | 55 #include "chrome/common/pref_names.h" |
| 57 #include "chrome/common/thumbnail_score.h" | 56 #include "chrome/common/thumbnail_score.h" |
| 58 #include "chrome/common/url_constants.h" | 57 #include "chrome/common/url_constants.h" |
| 59 #include "components/bookmarks/browser/bookmark_model.h" | |
| 60 #include "components/history/core/browser/history_client.h" | 58 #include "components/history/core/browser/history_client.h" |
| 61 #include "components/visitedlink/browser/visitedlink_master.h" | 59 #include "components/visitedlink/browser/visitedlink_master.h" |
| 62 #include "content/public/browser/browser_thread.h" | 60 #include "content/public/browser/browser_thread.h" |
| 63 #include "content/public/browser/download_item.h" | 61 #include "content/public/browser/download_item.h" |
| 64 #include "content/public/browser/notification_service.h" | 62 #include "content/public/browser/notification_service.h" |
| 65 #include "grit/chromium_strings.h" | 63 #include "grit/chromium_strings.h" |
| 66 #include "grit/generated_resources.h" | 64 #include "grit/generated_resources.h" |
| 67 #include "sync/api/sync_error_factory.h" | 65 #include "sync/api/sync_error_factory.h" |
| 68 #include "third_party/skia/include/core/SkBitmap.h" | 66 #include "third_party/skia/include/core/SkBitmap.h" |
| 69 | 67 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 187 |
| 190 // The history thread is intentionally not a BrowserThread because the | 188 // The history thread is intentionally not a BrowserThread because the |
| 191 // sync integration unit tests depend on being able to create more than one | 189 // sync integration unit tests depend on being able to create more than one |
| 192 // history thread. | 190 // history thread. |
| 193 HistoryService::HistoryService() | 191 HistoryService::HistoryService() |
| 194 : weak_ptr_factory_(this), | 192 : weak_ptr_factory_(this), |
| 195 thread_(new base::Thread(kHistoryThreadName)), | 193 thread_(new base::Thread(kHistoryThreadName)), |
| 196 history_client_(NULL), | 194 history_client_(NULL), |
| 197 profile_(NULL), | 195 profile_(NULL), |
| 198 backend_loaded_(false), | 196 backend_loaded_(false), |
| 199 bookmark_service_(NULL), | |
| 200 no_db_(false) { | 197 no_db_(false) { |
| 201 } | 198 } |
| 202 | 199 |
| 203 HistoryService::HistoryService(history::HistoryClient* client, Profile* profile) | 200 HistoryService::HistoryService(history::HistoryClient* client, Profile* profile) |
| 204 : weak_ptr_factory_(this), | 201 : weak_ptr_factory_(this), |
| 205 thread_(new base::Thread(kHistoryThreadName)), | 202 thread_(new base::Thread(kHistoryThreadName)), |
| 206 history_client_(client), | 203 history_client_(client), |
| 207 profile_(profile), | 204 profile_(profile), |
| 208 visitedlink_master_(new visitedlink::VisitedLinkMaster( | 205 visitedlink_master_(new visitedlink::VisitedLinkMaster( |
| 209 profile, this, true)), | 206 profile, this, true)), |
| 210 backend_loaded_(false), | 207 backend_loaded_(false), |
| 211 bookmark_service_(NULL), | |
| 212 no_db_(false) { | 208 no_db_(false) { |
| 213 DCHECK(profile_); | 209 DCHECK(profile_); |
| 214 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 210 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 215 content::Source<Profile>(profile_)); | 211 content::Source<Profile>(profile_)); |
| 216 registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, | 212 registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
| 217 content::Source<Profile>(profile_)); | 213 content::Source<Profile>(profile_)); |
| 218 } | 214 } |
| 219 | 215 |
| 220 HistoryService::~HistoryService() { | 216 HistoryService::~HistoryService() { |
| 221 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 return true; | 320 return true; |
| 325 } | 321 } |
| 326 | 322 |
| 327 history::TypedUrlSyncableService* HistoryService::GetTypedUrlSyncableService() | 323 history::TypedUrlSyncableService* HistoryService::GetTypedUrlSyncableService() |
| 328 const { | 324 const { |
| 329 return history_backend_->GetTypedUrlSyncableService(); | 325 return history_backend_->GetTypedUrlSyncableService(); |
| 330 } | 326 } |
| 331 | 327 |
| 332 void HistoryService::Shutdown() { | 328 void HistoryService::Shutdown() { |
| 333 DCHECK(thread_checker_.CalledOnValidThread()); | 329 DCHECK(thread_checker_.CalledOnValidThread()); |
| 334 // It's possible that bookmarks haven't loaded and history is waiting for | |
| 335 // bookmarks to complete loading. In such a situation history can't shutdown | |
| 336 // (meaning if we invoked history_service_->Cleanup now, we would | |
| 337 // deadlock). To break the deadlock we tell BookmarkModel it's about to be | |
| 338 // deleted so that it can release the signal history is waiting on, allowing | |
| 339 // history to shutdown (history_service_->Cleanup to complete). In such a | |
| 340 // scenario history sees an incorrect view of bookmarks, but it's better | |
| 341 // than a deadlock. | |
| 342 BookmarkModel* bookmark_model = static_cast<BookmarkModel*>( | |
| 343 BookmarkModelFactory::GetForProfileIfExists(profile_)); | |
| 344 if (bookmark_model) | |
| 345 bookmark_model->Shutdown(); | |
| 346 | |
| 347 Cleanup(); | 330 Cleanup(); |
| 348 } | 331 } |
| 349 | 332 |
| 350 void HistoryService::SetKeywordSearchTermsForURL(const GURL& url, | 333 void HistoryService::SetKeywordSearchTermsForURL(const GURL& url, |
| 351 TemplateURLID keyword_id, | 334 TemplateURLID keyword_id, |
| 352 const base::string16& term) { | 335 const base::string16& term) { |
| 353 DCHECK(thread_checker_.CalledOnValidThread()); | 336 DCHECK(thread_checker_.CalledOnValidThread()); |
| 354 ScheduleAndForget(PRIORITY_UI, | 337 ScheduleAndForget(PRIORITY_UI, |
| 355 &HistoryBackend::SetKeywordSearchTermsForURL, | 338 &HistoryBackend::SetKeywordSearchTermsForURL, |
| 356 url, keyword_id, term); | 339 url, keyword_id, term); |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 NOTREACHED(); | 887 NOTREACHED(); |
| 905 } | 888 } |
| 906 } | 889 } |
| 907 | 890 |
| 908 void HistoryService::RebuildTable( | 891 void HistoryService::RebuildTable( |
| 909 const scoped_refptr<URLEnumerator>& enumerator) { | 892 const scoped_refptr<URLEnumerator>& enumerator) { |
| 910 DCHECK(thread_checker_.CalledOnValidThread()); | 893 DCHECK(thread_checker_.CalledOnValidThread()); |
| 911 ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::IterateURLs, enumerator); | 894 ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::IterateURLs, enumerator); |
| 912 } | 895 } |
| 913 | 896 |
| 914 bool HistoryService::Init(const base::FilePath& history_dir, | 897 bool HistoryService::Init(const base::FilePath& history_dir, bool no_db) { |
| 915 BookmarkService* bookmark_service, | |
| 916 bool no_db) { | |
| 917 DCHECK(thread_checker_.CalledOnValidThread()); | 898 DCHECK(thread_checker_.CalledOnValidThread()); |
| 918 if (!thread_->Start()) { | 899 if (!thread_->Start()) { |
| 919 Cleanup(); | 900 Cleanup(); |
| 920 return false; | 901 return false; |
| 921 } | 902 } |
| 922 | 903 |
| 923 history_dir_ = history_dir; | 904 history_dir_ = history_dir; |
| 924 bookmark_service_ = bookmark_service; | |
| 925 no_db_ = no_db; | 905 no_db_ = no_db; |
| 926 | 906 |
| 927 if (profile_) { | 907 if (profile_) { |
| 928 std::string languages = | 908 std::string languages = |
| 929 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 909 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 930 in_memory_url_index_.reset( | 910 in_memory_url_index_.reset(new history::InMemoryURLIndex( |
| 931 new history::InMemoryURLIndex(profile_, history_dir_, languages)); | 911 profile_, history_dir_, languages, history_client_)); |
| 932 in_memory_url_index_->Init(); | 912 in_memory_url_index_->Init(); |
| 933 } | 913 } |
| 934 | 914 |
| 935 // Create the history backend. | 915 // Create the history backend. |
| 936 scoped_refptr<HistoryBackend> backend( | 916 scoped_refptr<HistoryBackend> backend( |
| 937 new HistoryBackend(history_dir_, | 917 new HistoryBackend(history_dir_, |
| 938 new BackendDelegate( | 918 new BackendDelegate( |
| 939 weak_ptr_factory_.GetWeakPtr(), | 919 weak_ptr_factory_.GetWeakPtr(), |
| 940 base::ThreadTaskRunnerHandle::Get(), | 920 base::ThreadTaskRunnerHandle::Get(), |
| 941 profile_), | 921 profile_), |
| 942 bookmark_service_)); | 922 history_client_)); |
| 943 history_backend_.swap(backend); | 923 history_backend_.swap(backend); |
| 944 | 924 |
| 945 // There may not be a profile when unit testing. | 925 // There may not be a profile when unit testing. |
| 946 std::string languages; | 926 std::string languages; |
| 947 if (profile_) { | 927 if (profile_) { |
| 948 PrefService* prefs = profile_->GetPrefs(); | 928 PrefService* prefs = profile_->GetPrefs(); |
| 949 languages = prefs->GetString(prefs::kAcceptLanguages); | 929 languages = prefs->GetString(prefs::kAcceptLanguages); |
| 950 } | 930 } |
| 951 ScheduleAndForget(PRIORITY_UI, &HistoryBackend::Init, languages, no_db_); | 931 ScheduleAndForget(PRIORITY_UI, &HistoryBackend::Init, languages, no_db_); |
| 952 | 932 |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1189 DCHECK(thread_checker_.CalledOnValidThread()); | 1169 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1190 visit_database_observers_.RemoveObserver(observer); | 1170 visit_database_observers_.RemoveObserver(observer); |
| 1191 } | 1171 } |
| 1192 | 1172 |
| 1193 void HistoryService::NotifyVisitDBObserversOnAddVisit( | 1173 void HistoryService::NotifyVisitDBObserversOnAddVisit( |
| 1194 const history::BriefVisitInfo& info) { | 1174 const history::BriefVisitInfo& info) { |
| 1195 DCHECK(thread_checker_.CalledOnValidThread()); | 1175 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1196 FOR_EACH_OBSERVER(history::VisitDatabaseObserver, visit_database_observers_, | 1176 FOR_EACH_OBSERVER(history::VisitDatabaseObserver, visit_database_observers_, |
| 1197 OnAddVisit(info)); | 1177 OnAddVisit(info)); |
| 1198 } | 1178 } |
| OLD | NEW |