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/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 Loading... |
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 |
| 123 void CleanupAfterTopSitesDestroyed() { |
| 124 if (base::MessageLoop::current()) |
| 125 base::MessageLoop::current()->RunUntilIdle(); |
| 126 } |
| 127 |
| 128 // Returns true if a TopSites service has been registered for |profile|. |
| 129 bool HasTopSites(Profile* profile) { |
| 130 return !!TopSitesFactory::GetInstance()->GetForProfileIfExists(profile); |
| 131 } |
| 132 |
117 // Used to make sure TopSites has finished loading | 133 // Used to make sure TopSites has finished loading |
118 class WaitTopSitesLoadedObserver : public history::TopSitesObserver { | 134 class WaitTopSitesLoadedObserver : public history::TopSitesObserver { |
119 public: | 135 public: |
120 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner) | 136 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner) |
121 : runner_(runner) {} | 137 : runner_(runner) {} |
122 void TopSitesLoaded(history::TopSites* top_sites) override { | 138 void TopSitesLoaded(history::TopSites* top_sites) override { |
123 runner_->Quit(); | 139 runner_->Quit(); |
124 } | 140 } |
125 void TopSitesChanged(history::TopSites* top_sites) override {} | 141 void TopSitesChanged(history::TopSites* top_sites) override {} |
126 | 142 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 KeyedService* BuildWebDataService(content::BrowserContext* context) { | 253 KeyedService* BuildWebDataService(content::BrowserContext* context) { |
238 const base::FilePath& context_path = context->GetPath(); | 254 const base::FilePath& context_path = context->GetPath(); |
239 return new WebDataServiceWrapper( | 255 return new WebDataServiceWrapper( |
240 context_path, g_browser_process->GetApplicationLocale(), | 256 context_path, g_browser_process->GetApplicationLocale(), |
241 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | 257 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
242 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), | 258 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), |
243 sync_start_util::GetFlareForSyncableService(context_path), | 259 sync_start_util::GetFlareForSyncableService(context_path), |
244 &TestProfileErrorCallback); | 260 &TestProfileErrorCallback); |
245 } | 261 } |
246 | 262 |
| 263 scoped_refptr<RefcountedKeyedService> BuildTopSites( |
| 264 content::BrowserContext* profile) { |
| 265 history::TopSitesImpl* top_sites = |
| 266 new history::TopSitesImpl(static_cast<Profile*>(profile)); |
| 267 top_sites->Init(profile->GetPath().Append(chrome::kTopSitesFilename)); |
| 268 return make_scoped_refptr(top_sites); |
| 269 } |
| 270 |
247 } // namespace | 271 } // namespace |
248 | 272 |
249 // static | 273 // static |
250 #if defined(OS_CHROMEOS) | 274 #if defined(OS_CHROMEOS) |
251 // Must be kept in sync with | 275 // Must be kept in sync with |
252 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. | 276 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. |
253 const char TestingProfile::kTestUserProfileDir[] = "test-user"; | 277 const char TestingProfile::kTestUserProfileDir[] = "test-user"; |
254 #else | 278 #else |
255 const char TestingProfile::kTestUserProfileDir[] = "Default"; | 279 const char TestingProfile::kTestUserProfileDir[] = "Default"; |
256 #endif | 280 #endif |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 force_incognito_ = false; | 513 force_incognito_ = false; |
490 | 514 |
491 // If this profile owns an incognito profile, tear it down first. | 515 // If this profile owns an incognito profile, tear it down first. |
492 incognito_profile_.reset(); | 516 incognito_profile_.reset(); |
493 | 517 |
494 // Any objects holding live URLFetchers should be deleted before teardown. | 518 // Any objects holding live URLFetchers should be deleted before teardown. |
495 TemplateURLFetcherFactory::ShutdownForProfile(this); | 519 TemplateURLFetcherFactory::ShutdownForProfile(this); |
496 | 520 |
497 MaybeSendDestroyedNotification(); | 521 MaybeSendDestroyedNotification(); |
498 | 522 |
| 523 // Remember whether a TopSites has been created for the current profile, |
| 524 // so that we can run cleanup after destroying all services. |
| 525 bool had_top_sites = HasTopSites(this); |
| 526 |
499 browser_context_dependency_manager_->DestroyBrowserContextServices(this); | 527 browser_context_dependency_manager_->DestroyBrowserContextServices(this); |
500 | 528 |
501 if (host_content_settings_map_.get()) | 529 if (host_content_settings_map_.get()) |
502 host_content_settings_map_->ShutdownOnUIThread(); | 530 host_content_settings_map_->ShutdownOnUIThread(); |
503 | 531 |
504 DestroyTopSites(); | 532 // Wait until TopSites shutdown tasks have completed if a TopSites has |
| 533 // been created for the current profile. |
| 534 if (had_top_sites) |
| 535 CleanupAfterTopSitesDestroyed(); |
505 | 536 |
506 if (pref_proxy_config_tracker_.get()) | 537 if (pref_proxy_config_tracker_.get()) |
507 pref_proxy_config_tracker_->DetachFromPrefService(); | 538 pref_proxy_config_tracker_->DetachFromPrefService(); |
508 // Failing a post == leaks == heapcheck failure. Make that an immediate test | 539 // Failing a post == leaks == heapcheck failure. Make that an immediate test |
509 // failure. | 540 // failure. |
510 if (resource_context_) { | 541 if (resource_context_) { |
511 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 542 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
512 resource_context_)); | 543 resource_context_)); |
513 resource_context_ = NULL; | 544 resource_context_ = NULL; |
514 content::RunAllPendingInMessageLoop(BrowserThread::IO); | 545 content::RunAllPendingInMessageLoop(BrowserThread::IO); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 | 599 |
569 // Make sure we don't have any event pending that could disrupt the next | 600 // Make sure we don't have any event pending that could disrupt the next |
570 // test. | 601 // test. |
571 base::MessageLoop::current()->PostTask(FROM_HERE, | 602 base::MessageLoop::current()->PostTask(FROM_HERE, |
572 base::MessageLoop::QuitClosure()); | 603 base::MessageLoop::QuitClosure()); |
573 base::MessageLoop::current()->Run(); | 604 base::MessageLoop::current()->Run(); |
574 } | 605 } |
575 | 606 |
576 void TestingProfile::CreateTopSites() { | 607 void TestingProfile::CreateTopSites() { |
577 DestroyTopSites(); | 608 DestroyTopSites(); |
578 top_sites_ = history::TopSites::Create( | 609 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 } | 610 } |
586 | 611 |
587 void TestingProfile::DestroyTopSites() { | 612 void TestingProfile::DestroyTopSites() { |
588 if (top_sites_.get()) { | 613 TopSitesFactory* top_sites_factory = TopSitesFactory::GetInstance(); |
589 top_sites_->Shutdown(); | 614 if (top_sites_factory->GetForProfileIfExists(this)) { |
590 top_sites_ = NULL; | 615 // BrowserContextKeyedServiceFactory will destroy the previous service when |
591 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that | 616 // 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 | 617 // old service. |
593 // normally handled by browser_process shutdown. | 618 top_sites_factory->SetTestingFactory(this, nullptr); |
594 if (base::MessageLoop::current()) | 619 CleanupAfterTopSitesDestroyed(); |
595 base::MessageLoop::current()->RunUntilIdle(); | |
596 } | 620 } |
597 } | 621 } |
598 | 622 |
599 void TestingProfile::CreateBookmarkModel(bool delete_file) { | 623 void TestingProfile::CreateBookmarkModel(bool delete_file) { |
600 if (delete_file) { | 624 if (delete_file) { |
601 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName); | 625 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName); |
602 base::DeleteFile(path, false); | 626 base::DeleteFile(path, false); |
603 } | 627 } |
604 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory( | 628 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory( |
605 this, BuildChromeHistoryClient); | 629 this, BuildChromeHistoryClient); |
(...skipping 25 matching lines...) Expand all Loading... |
631 run_loop.Run(); | 655 run_loop.Run(); |
632 index->set_restore_cache_observer(NULL); | 656 index->set_restore_cache_observer(NULL); |
633 DCHECK(index->restored()); | 657 DCHECK(index->restored()); |
634 } | 658 } |
635 | 659 |
636 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? | 660 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? |
637 void TestingProfile::BlockUntilTopSitesLoaded() { | 661 void TestingProfile::BlockUntilTopSitesLoaded() { |
638 scoped_refptr<content::MessageLoopRunner> runner = | 662 scoped_refptr<content::MessageLoopRunner> runner = |
639 new content::MessageLoopRunner; | 663 new content::MessageLoopRunner; |
640 WaitTopSitesLoadedObserver observer(runner.get()); | 664 WaitTopSitesLoadedObserver observer(runner.get()); |
641 top_sites_->AddObserver(&observer); | 665 scoped_refptr<history::TopSites> top_sites = |
| 666 TopSitesFactory::GetForProfile(this); |
| 667 top_sites->AddObserver(&observer); |
642 runner->Run(); | 668 runner->Run(); |
643 top_sites_->RemoveObserver(&observer); | 669 top_sites->RemoveObserver(&observer); |
644 } | 670 } |
645 | 671 |
646 void TestingProfile::SetGuestSession(bool guest) { | 672 void TestingProfile::SetGuestSession(bool guest) { |
647 guest_session_ = guest; | 673 guest_session_ = guest; |
648 } | 674 } |
649 | 675 |
650 base::FilePath TestingProfile::GetPath() const { | 676 base::FilePath TestingProfile::GetPath() const { |
651 return profile_path_; | 677 return profile_path_; |
652 } | 678 } |
653 | 679 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 this, profile_policy_connector_.get()); | 817 this, profile_policy_connector_.get()); |
792 CHECK_EQ(profile_policy_connector_.get(), | 818 CHECK_EQ(profile_policy_connector_.get(), |
793 policy::ProfilePolicyConnectorFactory::GetForProfile(this)); | 819 policy::ProfilePolicyConnectorFactory::GetForProfile(this)); |
794 } | 820 } |
795 | 821 |
796 PrefService* TestingProfile::GetPrefs() { | 822 PrefService* TestingProfile::GetPrefs() { |
797 DCHECK(prefs_); | 823 DCHECK(prefs_); |
798 return prefs_.get(); | 824 return prefs_.get(); |
799 } | 825 } |
800 | 826 |
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() { | 827 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { |
810 return NULL; | 828 return NULL; |
811 } | 829 } |
812 | 830 |
813 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { | 831 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { |
814 return GetDefaultStoragePartition(this)->GetURLRequestContext(); | 832 return GetDefaultStoragePartition(this)->GetURLRequestContext(); |
815 } | 833 } |
816 | 834 |
817 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( | 835 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( |
818 content::ProtocolHandlerMap* protocol_handlers, | 836 content::ProtocolHandlerMap* protocol_handlers, |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1068 #if defined(ENABLE_EXTENSIONS) | 1086 #if defined(ENABLE_EXTENSIONS) |
1069 extension_policy_, | 1087 extension_policy_, |
1070 #endif | 1088 #endif |
1071 pref_service_.Pass(), | 1089 pref_service_.Pass(), |
1072 original_profile, | 1090 original_profile, |
1073 guest_session_, | 1091 guest_session_, |
1074 supervised_user_id_, | 1092 supervised_user_id_, |
1075 policy_service_.Pass(), | 1093 policy_service_.Pass(), |
1076 testing_factories_); | 1094 testing_factories_); |
1077 } | 1095 } |
OLD | NEW |