Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/test/base/testing_profile.cc

Issue 861273002: Make TopSites a RefcountedKeyedService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix win compilation Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/test/base/testing_profile.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/test/base/testing_profile.h" 5 #include "chrome/test/base/testing_profile.h"
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/prefs/testing_pref_store.h" 12 #include "base/prefs/testing_pref_store.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "chrome/browser/autocomplete/autocomplete_classifier.h" 15 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" 17 #include "chrome/browser/bookmarks/chrome_bookmark_client.h"
18 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" 18 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
19 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chrome_notification_types.h" 20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/favicon/chrome_favicon_client_factory.h" 21 #include "chrome/browser/favicon/chrome_favicon_client_factory.h"
22 #include "chrome/browser/favicon/favicon_service.h" 22 #include "chrome/browser/favicon/favicon_service.h"
23 #include "chrome/browser/favicon/favicon_service_factory.h" 23 #include "chrome/browser/favicon/favicon_service_factory.h"
24 #include "chrome/browser/history/chrome_history_client.h" 24 #include "chrome/browser/history/chrome_history_client.h"
25 #include "chrome/browser/history/chrome_history_client_factory.h" 25 #include "chrome/browser/history/chrome_history_client_factory.h"
26 #include "chrome/browser/history/history_backend.h" 26 #include "chrome/browser/history/history_backend.h"
27 #include "chrome/browser/history/history_service.h" 27 #include "chrome/browser/history/history_service.h"
28 #include "chrome/browser/history/history_service_factory.h" 28 #include "chrome/browser/history/history_service_factory.h"
29 #include "chrome/browser/history/top_sites.h" 29 #include "chrome/browser/history/top_sites.h"
30 #include "chrome/browser/history/top_sites_factory.h"
31 #include "chrome/browser/history/top_sites_impl.h"
30 #include "chrome/browser/history/web_history_service_factory.h" 32 #include "chrome/browser/history/web_history_service_factory.h"
31 #include "chrome/browser/net/pref_proxy_config_tracker.h" 33 #include "chrome/browser/net/pref_proxy_config_tracker.h"
32 #include "chrome/browser/net/proxy_service_factory.h" 34 #include "chrome/browser/net/proxy_service_factory.h"
33 #include "chrome/browser/notifications/desktop_notification_service.h" 35 #include "chrome/browser/notifications/desktop_notification_service.h"
34 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 36 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
35 #include "chrome/browser/policy/profile_policy_connector.h" 37 #include "chrome/browser/policy/profile_policy_connector.h"
36 #include "chrome/browser/policy/profile_policy_connector_factory.h" 38 #include "chrome/browser/policy/profile_policy_connector_factory.h"
37 #include "chrome/browser/prefs/browser_prefs.h" 39 #include "chrome/browser/prefs/browser_prefs.h"
38 #include "chrome/browser/prefs/pref_service_syncable.h" 40 #include "chrome/browser/prefs/pref_service_syncable.h"
39 #include "chrome/browser/prerender/prerender_manager.h" 41 #include "chrome/browser/prerender/prerender_manager.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 109
108 using base::Time; 110 using base::Time;
109 using bookmarks::BookmarkModel; 111 using bookmarks::BookmarkModel;
110 using content::BrowserThread; 112 using content::BrowserThread;
111 using content::DownloadManagerDelegate; 113 using content::DownloadManagerDelegate;
112 using testing::NiceMock; 114 using testing::NiceMock;
113 using testing::Return; 115 using testing::Return;
114 116
115 namespace { 117 namespace {
116 118
119 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that
120 // need to be run to properly shutdown. Run all pending tasks now. This is
121 // normally handled by browser_process shutdown.
122 void CleanupAfterTopSitesDestroyed() {
123 if (base::MessageLoop::current())
124 base::MessageLoop::current()->RunUntilIdle();
125 }
126
127 // Returns true if a TopSites service has been registered for |profile|.
128 bool HasTopSites(Profile* profile) {
129 return !!TopSitesFactory::GetInstance()->GetForProfileIfExists(profile);
130 }
131
117 // Used to make sure TopSites has finished loading 132 // Used to make sure TopSites has finished loading
118 class WaitTopSitesLoadedObserver : public history::TopSitesObserver { 133 class WaitTopSitesLoadedObserver : public history::TopSitesObserver {
119 public: 134 public:
120 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner) 135 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner)
121 : runner_(runner) {} 136 : runner_(runner) {}
122 void TopSitesLoaded(history::TopSites* top_sites) override { 137 void TopSitesLoaded(history::TopSites* top_sites) override {
123 runner_->Quit(); 138 runner_->Quit();
124 } 139 }
125 void TopSitesChanged(history::TopSites* top_sites) override {} 140 void TopSitesChanged(history::TopSites* top_sites) override {}
126 141
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 KeyedService* BuildWebDataService(content::BrowserContext* context) { 252 KeyedService* BuildWebDataService(content::BrowserContext* context) {
238 const base::FilePath& context_path = context->GetPath(); 253 const base::FilePath& context_path = context->GetPath();
239 return new WebDataServiceWrapper( 254 return new WebDataServiceWrapper(
240 context_path, g_browser_process->GetApplicationLocale(), 255 context_path, g_browser_process->GetApplicationLocale(),
241 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), 256 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
242 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), 257 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
243 sync_start_util::GetFlareForSyncableService(context_path), 258 sync_start_util::GetFlareForSyncableService(context_path),
244 &TestProfileErrorCallback); 259 &TestProfileErrorCallback);
245 } 260 }
246 261
262 scoped_refptr<RefcountedKeyedService> BuildTopSites(
263 content::BrowserContext* profile) {
264 history::TopSitesImpl* top_sites =
265 new history::TopSitesImpl(static_cast<Profile*>(profile));
266 top_sites->Init(profile->GetPath().Append(chrome::kTopSitesFilename));
267 return make_scoped_refptr(top_sites);
268 }
269
247 } // namespace 270 } // namespace
248 271
249 // static 272 // static
250 #if defined(OS_CHROMEOS) 273 #if defined(OS_CHROMEOS)
251 // Must be kept in sync with 274 // Must be kept in sync with
252 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. 275 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
253 const char TestingProfile::kTestUserProfileDir[] = "test-user"; 276 const char TestingProfile::kTestUserProfileDir[] = "test-user";
254 #else 277 #else
255 const char TestingProfile::kTestUserProfileDir[] = "Default"; 278 const char TestingProfile::kTestUserProfileDir[] = "Default";
256 #endif 279 #endif
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 force_incognito_ = false; 512 force_incognito_ = false;
490 513
491 // If this profile owns an incognito profile, tear it down first. 514 // If this profile owns an incognito profile, tear it down first.
492 incognito_profile_.reset(); 515 incognito_profile_.reset();
493 516
494 // Any objects holding live URLFetchers should be deleted before teardown. 517 // Any objects holding live URLFetchers should be deleted before teardown.
495 TemplateURLFetcherFactory::ShutdownForProfile(this); 518 TemplateURLFetcherFactory::ShutdownForProfile(this);
496 519
497 MaybeSendDestroyedNotification(); 520 MaybeSendDestroyedNotification();
498 521
522 // Remember whether a TopSites has beed created for the current profile,
523 // so that we can run cleanup after destroying all services.
524 bool had_top_sites = HasTopSites(this);
525
499 browser_context_dependency_manager_->DestroyBrowserContextServices(this); 526 browser_context_dependency_manager_->DestroyBrowserContextServices(this);
500 527
501 if (host_content_settings_map_.get()) 528 if (host_content_settings_map_.get())
502 host_content_settings_map_->ShutdownOnUIThread(); 529 host_content_settings_map_->ShutdownOnUIThread();
503 530
504 DestroyTopSites(); 531 // Wait until TopSites shutdown tasks have completed if a TopSites had
532 // been created for the current profile.
533 if (had_top_sites)
534 CleanupAfterTopSitesDestroyed();
505 535
506 if (pref_proxy_config_tracker_.get()) 536 if (pref_proxy_config_tracker_.get())
507 pref_proxy_config_tracker_->DetachFromPrefService(); 537 pref_proxy_config_tracker_->DetachFromPrefService();
508 // Failing a post == leaks == heapcheck failure. Make that an immediate test 538 // Failing a post == leaks == heapcheck failure. Make that an immediate test
509 // failure. 539 // failure.
510 if (resource_context_) { 540 if (resource_context_) {
511 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, 541 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
512 resource_context_)); 542 resource_context_));
513 resource_context_ = NULL; 543 resource_context_ = NULL;
514 content::RunAllPendingInMessageLoop(BrowserThread::IO); 544 content::RunAllPendingInMessageLoop(BrowserThread::IO);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 598
569 // Make sure we don't have any event pending that could disrupt the next 599 // Make sure we don't have any event pending that could disrupt the next
570 // test. 600 // test.
571 base::MessageLoop::current()->PostTask(FROM_HERE, 601 base::MessageLoop::current()->PostTask(FROM_HERE,
572 base::MessageLoop::QuitClosure()); 602 base::MessageLoop::QuitClosure());
573 base::MessageLoop::current()->Run(); 603 base::MessageLoop::current()->Run();
574 } 604 }
575 605
576 void TestingProfile::CreateTopSites() { 606 void TestingProfile::CreateTopSites() {
577 DestroyTopSites(); 607 DestroyTopSites();
578 top_sites_ = history::TopSites::Create( 608 TopSitesFactory::GetInstance()->SetTestingFactoryAndUse(this, BuildTopSites);
579 this, GetPath().Append(chrome::kTopSitesFilename));
580 }
581
582 void TestingProfile::SetTopSites(history::TopSites* top_sites) {
583 DestroyTopSites();
584 top_sites_ = top_sites;
585 } 609 }
586 610
587 void TestingProfile::DestroyTopSites() { 611 void TestingProfile::DestroyTopSites() {
588 if (top_sites_.get()) { 612 TopSitesFactory* top_sites_factory = TopSitesFactory::GetInstance();
589 top_sites_->Shutdown(); 613 if (top_sites_factory->GetForProfileIfExists(this)) {
590 top_sites_ = NULL; 614 // BrowserContextKeyedServiceFactory will destroy the previous service when
591 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that 615 // registering a new testing factory so use this to ensure that destroy the
592 // need to be run to properly shutdown. Run all pending tasks now. This is 616 // old service.
593 // normally handled by browser_process shutdown. 617 top_sites_factory->SetTestingFactory(this, nullptr);
594 if (base::MessageLoop::current()) 618 CleanupAfterTopSitesDestroyed();
595 base::MessageLoop::current()->RunUntilIdle();
596 } 619 }
597 } 620 }
598 621
599 void TestingProfile::CreateBookmarkModel(bool delete_file) { 622 void TestingProfile::CreateBookmarkModel(bool delete_file) {
600 if (delete_file) { 623 if (delete_file) {
601 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName); 624 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName);
602 base::DeleteFile(path, false); 625 base::DeleteFile(path, false);
603 } 626 }
604 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory( 627 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory(
605 this, BuildChromeHistoryClient); 628 this, BuildChromeHistoryClient);
(...skipping 25 matching lines...) Expand all
631 run_loop.Run(); 654 run_loop.Run();
632 index->set_restore_cache_observer(NULL); 655 index->set_restore_cache_observer(NULL);
633 DCHECK(index->restored()); 656 DCHECK(index->restored());
634 } 657 }
635 658
636 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? 659 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
637 void TestingProfile::BlockUntilTopSitesLoaded() { 660 void TestingProfile::BlockUntilTopSitesLoaded() {
638 scoped_refptr<content::MessageLoopRunner> runner = 661 scoped_refptr<content::MessageLoopRunner> runner =
639 new content::MessageLoopRunner; 662 new content::MessageLoopRunner;
640 WaitTopSitesLoadedObserver observer(runner.get()); 663 WaitTopSitesLoadedObserver observer(runner.get());
641 top_sites_->AddObserver(&observer); 664 scoped_refptr<history::TopSites> top_sites =
665 TopSitesFactory::GetForProfile(this);
666 top_sites->AddObserver(&observer);
642 runner->Run(); 667 runner->Run();
643 top_sites_->RemoveObserver(&observer); 668 top_sites->RemoveObserver(&observer);
644 } 669 }
645 670
646 void TestingProfile::SetGuestSession(bool guest) { 671 void TestingProfile::SetGuestSession(bool guest) {
647 guest_session_ = guest; 672 guest_session_ = guest;
648 } 673 }
649 674
650 base::FilePath TestingProfile::GetPath() const { 675 base::FilePath TestingProfile::GetPath() const {
651 return profile_path_; 676 return profile_path_;
652 } 677 }
653 678
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 this, profile_policy_connector_.get()); 816 this, profile_policy_connector_.get());
792 CHECK_EQ(profile_policy_connector_.get(), 817 CHECK_EQ(profile_policy_connector_.get(),
793 policy::ProfilePolicyConnectorFactory::GetForProfile(this)); 818 policy::ProfilePolicyConnectorFactory::GetForProfile(this));
794 } 819 }
795 820
796 PrefService* TestingProfile::GetPrefs() { 821 PrefService* TestingProfile::GetPrefs() {
797 DCHECK(prefs_); 822 DCHECK(prefs_);
798 return prefs_.get(); 823 return prefs_.get();
799 } 824 }
800 825
801 history::TopSites* TestingProfile::GetTopSites() {
802 return top_sites_.get();
803 }
804
805 history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
806 return top_sites_.get();
807 }
808
809 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { 826 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
810 return NULL; 827 return NULL;
811 } 828 }
812 829
813 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { 830 net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
814 return GetDefaultStoragePartition(this)->GetURLRequestContext(); 831 return GetDefaultStoragePartition(this)->GetURLRequestContext();
815 } 832 }
816 833
817 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( 834 net::URLRequestContextGetter* TestingProfile::CreateRequestContext(
818 content::ProtocolHandlerMap* protocol_handlers, 835 content::ProtocolHandlerMap* protocol_handlers,
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
1068 #if defined(ENABLE_EXTENSIONS) 1085 #if defined(ENABLE_EXTENSIONS)
1069 extension_policy_, 1086 extension_policy_,
1070 #endif 1087 #endif
1071 pref_service_.Pass(), 1088 pref_service_.Pass(),
1072 original_profile, 1089 original_profile,
1073 guest_session_, 1090 guest_session_,
1074 supervised_user_id_, 1091 supervised_user_id_,
1075 policy_service_.Pass(), 1092 policy_service_.Pass(),
1076 testing_factories_); 1093 testing_factories_);
1077 } 1094 }
OLDNEW
« no previous file with comments | « chrome/test/base/testing_profile.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698