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/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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 | 107 |
| 106 using base::Time; | 108 using base::Time; |
| 107 using bookmarks::BookmarkModel; | 109 using bookmarks::BookmarkModel; |
| 108 using content::BrowserThread; | 110 using content::BrowserThread; |
| 109 using content::DownloadManagerDelegate; | 111 using content::DownloadManagerDelegate; |
| 110 using testing::NiceMock; | 112 using testing::NiceMock; |
| 111 using testing::Return; | 113 using testing::Return; |
| 112 | 114 |
| 113 namespace { | 115 namespace { |
| 114 | 116 |
| 117 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that | |
| 118 // need to be run to properly shutdown. Run all pending tasks now. This is | |
| 119 // normally handled by browser_process shutdown. | |
| 120 | |
| 121 void CleanupAfterTopSitesDestroyed() { | |
| 122 if (base::MessageLoop::current()) | |
| 123 base::MessageLoop::current()->RunUntilIdle(); | |
| 124 } | |
| 125 | |
| 126 // Returns true if a TopSites service has been registered for |profile|. | |
| 127 bool HasTopSites(Profile* profile) { | |
| 128 TopSitesFactory* top_sites_factory = TopSitesFactory::GetInstance(); | |
|
sdefresne
2015/01/22 19:05:05
This does not compile on Windows, and I don't real
Bernhard Bauer
2015/01/22 21:23:27
In any case, the .get() and the comparison to null
| |
| 129 return top_sites_factory->GetForProfileIfExists(profile).get() != nullptr; | |
| 130 } | |
| 131 | |
| 115 // Used to make sure TopSites has finished loading | 132 // Used to make sure TopSites has finished loading |
| 116 class WaitTopSitesLoadedObserver : public history::TopSitesObserver { | 133 class WaitTopSitesLoadedObserver : public history::TopSitesObserver { |
| 117 public: | 134 public: |
| 118 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner) | 135 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner) |
| 119 : runner_(runner) {} | 136 : runner_(runner) {} |
| 120 void TopSitesLoaded(history::TopSites* top_sites) override { | 137 void TopSitesLoaded(history::TopSites* top_sites) override { |
| 121 runner_->Quit(); | 138 runner_->Quit(); |
| 122 } | 139 } |
| 123 void TopSitesChanged(history::TopSites* top_sites) override {} | 140 void TopSitesChanged(history::TopSites* top_sites) override {} |
| 124 | 141 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 KeyedService* BuildWebDataService(content::BrowserContext* context) { | 252 KeyedService* BuildWebDataService(content::BrowserContext* context) { |
| 236 const base::FilePath& context_path = context->GetPath(); | 253 const base::FilePath& context_path = context->GetPath(); |
| 237 return new WebDataServiceWrapper( | 254 return new WebDataServiceWrapper( |
| 238 context_path, g_browser_process->GetApplicationLocale(), | 255 context_path, g_browser_process->GetApplicationLocale(), |
| 239 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | 256 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
| 240 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), | 257 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB), |
| 241 sync_start_util::GetFlareForSyncableService(context_path), | 258 sync_start_util::GetFlareForSyncableService(context_path), |
| 242 &TestProfileErrorCallback); | 259 &TestProfileErrorCallback); |
| 243 } | 260 } |
| 244 | 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 | |
| 245 } // namespace | 270 } // namespace |
| 246 | 271 |
| 247 // static | 272 // static |
| 248 #if defined(OS_CHROMEOS) | 273 #if defined(OS_CHROMEOS) |
| 249 // Must be kept in sync with | 274 // Must be kept in sync with |
| 250 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. | 275 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. |
| 251 const char TestingProfile::kTestUserProfileDir[] = "test-user"; | 276 const char TestingProfile::kTestUserProfileDir[] = "test-user"; |
| 252 #else | 277 #else |
| 253 const char TestingProfile::kTestUserProfileDir[] = "Default"; | 278 const char TestingProfile::kTestUserProfileDir[] = "Default"; |
| 254 #endif | 279 #endif |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 force_incognito_ = false; | 510 force_incognito_ = false; |
| 486 | 511 |
| 487 // If this profile owns an incognito profile, tear it down first. | 512 // If this profile owns an incognito profile, tear it down first. |
| 488 incognito_profile_.reset(); | 513 incognito_profile_.reset(); |
| 489 | 514 |
| 490 // Any objects holding live URLFetchers should be deleted before teardown. | 515 // Any objects holding live URLFetchers should be deleted before teardown. |
| 491 TemplateURLFetcherFactory::ShutdownForProfile(this); | 516 TemplateURLFetcherFactory::ShutdownForProfile(this); |
| 492 | 517 |
| 493 MaybeSendDestroyedNotification(); | 518 MaybeSendDestroyedNotification(); |
| 494 | 519 |
| 520 // Remember whether a TopSites has beed created for the current profile, | |
|
Bernhard Bauer
2015/01/22 21:23:27
Nit: "has been", or "had been" (below you use the
Jitu( very slow this week)
2015/01/23 11:36:53
Done.
| |
| 521 // so that we can run cleanup after destroying all services. | |
| 522 bool had_top_sites = HasTopSites(this); | |
| 523 | |
| 495 browser_context_dependency_manager_->DestroyBrowserContextServices(this); | 524 browser_context_dependency_manager_->DestroyBrowserContextServices(this); |
| 496 | 525 |
| 497 if (host_content_settings_map_.get()) | 526 if (host_content_settings_map_.get()) |
| 498 host_content_settings_map_->ShutdownOnUIThread(); | 527 host_content_settings_map_->ShutdownOnUIThread(); |
| 499 | 528 |
| 500 DestroyTopSites(); | 529 // Wait until TopSites shutdown tasks have completed if a TopSites had |
| 530 // been created for the current profile. | |
| 531 if (had_top_sites) | |
| 532 CleanupAfterTopSitesDestroyed(); | |
| 501 | 533 |
| 502 if (pref_proxy_config_tracker_.get()) | 534 if (pref_proxy_config_tracker_.get()) |
| 503 pref_proxy_config_tracker_->DetachFromPrefService(); | 535 pref_proxy_config_tracker_->DetachFromPrefService(); |
| 504 // Failing a post == leaks == heapcheck failure. Make that an immediate test | 536 // Failing a post == leaks == heapcheck failure. Make that an immediate test |
| 505 // failure. | 537 // failure. |
| 506 if (resource_context_) { | 538 if (resource_context_) { |
| 507 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 539 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 508 resource_context_)); | 540 resource_context_)); |
| 509 resource_context_ = NULL; | 541 resource_context_ = NULL; |
| 510 content::RunAllPendingInMessageLoop(BrowserThread::IO); | 542 content::RunAllPendingInMessageLoop(BrowserThread::IO); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 | 595 |
| 564 // Make sure we don't have any event pending that could disrupt the next | 596 // Make sure we don't have any event pending that could disrupt the next |
| 565 // test. | 597 // test. |
| 566 base::MessageLoop::current()->PostTask(FROM_HERE, | 598 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 567 base::MessageLoop::QuitClosure()); | 599 base::MessageLoop::QuitClosure()); |
| 568 base::MessageLoop::current()->Run(); | 600 base::MessageLoop::current()->Run(); |
| 569 } | 601 } |
| 570 | 602 |
| 571 void TestingProfile::CreateTopSites() { | 603 void TestingProfile::CreateTopSites() { |
| 572 DestroyTopSites(); | 604 DestroyTopSites(); |
| 573 top_sites_ = history::TopSites::Create( | 605 TopSitesFactory::GetInstance()->SetTestingFactoryAndUse(this, BuildTopSites); |
| 574 this, GetPath().Append(chrome::kTopSitesFilename)); | |
| 575 } | |
| 576 | |
| 577 void TestingProfile::SetTopSites(history::TopSites* top_sites) { | |
| 578 DestroyTopSites(); | |
| 579 top_sites_ = top_sites; | |
| 580 } | 606 } |
| 581 | 607 |
| 582 void TestingProfile::DestroyTopSites() { | 608 void TestingProfile::DestroyTopSites() { |
| 583 if (top_sites_.get()) { | 609 TopSitesFactory* top_sites_factory = TopSitesFactory::GetInstance(); |
| 584 top_sites_->Shutdown(); | 610 if (top_sites_factory->GetForProfileIfExists(this)) { |
| 585 top_sites_ = NULL; | 611 // BrowserContextKeyedServiceFactory will destroy the previous service when |
| 586 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that | 612 // registering a new testing factory so use this to ensure that destroy the |
| 587 // need to be run to properly shutdown. Run all pending tasks now. This is | 613 // old service. |
| 588 // normally handled by browser_process shutdown. | 614 top_sites_factory->SetTestingFactory(this, nullptr); |
| 589 if (base::MessageLoop::current()) | 615 CleanupAfterTopSitesDestroyed(); |
| 590 base::MessageLoop::current()->RunUntilIdle(); | |
| 591 } | 616 } |
| 592 } | 617 } |
| 593 | 618 |
| 594 void TestingProfile::CreateBookmarkModel(bool delete_file) { | 619 void TestingProfile::CreateBookmarkModel(bool delete_file) { |
| 595 if (delete_file) { | 620 if (delete_file) { |
| 596 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName); | 621 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName); |
| 597 base::DeleteFile(path, false); | 622 base::DeleteFile(path, false); |
| 598 } | 623 } |
| 599 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory( | 624 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory( |
| 600 this, BuildChromeHistoryClient); | 625 this, BuildChromeHistoryClient); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 626 run_loop.Run(); | 651 run_loop.Run(); |
| 627 index->set_restore_cache_observer(NULL); | 652 index->set_restore_cache_observer(NULL); |
| 628 DCHECK(index->restored()); | 653 DCHECK(index->restored()); |
| 629 } | 654 } |
| 630 | 655 |
| 631 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? | 656 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? |
| 632 void TestingProfile::BlockUntilTopSitesLoaded() { | 657 void TestingProfile::BlockUntilTopSitesLoaded() { |
| 633 scoped_refptr<content::MessageLoopRunner> runner = | 658 scoped_refptr<content::MessageLoopRunner> runner = |
| 634 new content::MessageLoopRunner; | 659 new content::MessageLoopRunner; |
| 635 WaitTopSitesLoadedObserver observer(runner.get()); | 660 WaitTopSitesLoadedObserver observer(runner.get()); |
| 636 top_sites_->AddObserver(&observer); | 661 scoped_refptr<history::TopSites> top_sites = |
| 662 TopSitesFactory::GetForProfile(this); | |
| 663 top_sites->AddObserver(&observer); | |
| 637 runner->Run(); | 664 runner->Run(); |
| 638 top_sites_->RemoveObserver(&observer); | 665 top_sites->RemoveObserver(&observer); |
| 639 } | 666 } |
| 640 | 667 |
| 641 void TestingProfile::SetGuestSession(bool guest) { | 668 void TestingProfile::SetGuestSession(bool guest) { |
| 642 guest_session_ = guest; | 669 guest_session_ = guest; |
| 643 } | 670 } |
| 644 | 671 |
| 645 base::FilePath TestingProfile::GetPath() const { | 672 base::FilePath TestingProfile::GetPath() const { |
| 646 return profile_path_; | 673 return profile_path_; |
| 647 } | 674 } |
| 648 | 675 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 786 this, profile_policy_connector_.get()); | 813 this, profile_policy_connector_.get()); |
| 787 CHECK_EQ(profile_policy_connector_.get(), | 814 CHECK_EQ(profile_policy_connector_.get(), |
| 788 policy::ProfilePolicyConnectorFactory::GetForProfile(this)); | 815 policy::ProfilePolicyConnectorFactory::GetForProfile(this)); |
| 789 } | 816 } |
| 790 | 817 |
| 791 PrefService* TestingProfile::GetPrefs() { | 818 PrefService* TestingProfile::GetPrefs() { |
| 792 DCHECK(prefs_); | 819 DCHECK(prefs_); |
| 793 return prefs_.get(); | 820 return prefs_.get(); |
| 794 } | 821 } |
| 795 | 822 |
| 796 history::TopSites* TestingProfile::GetTopSites() { | |
| 797 return top_sites_.get(); | |
| 798 } | |
| 799 | |
| 800 history::TopSites* TestingProfile::GetTopSitesWithoutCreating() { | |
| 801 return top_sites_.get(); | |
| 802 } | |
| 803 | |
| 804 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { | 823 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { |
| 805 return NULL; | 824 return NULL; |
| 806 } | 825 } |
| 807 | 826 |
| 808 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { | 827 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { |
| 809 return GetDefaultStoragePartition(this)->GetURLRequestContext(); | 828 return GetDefaultStoragePartition(this)->GetURLRequestContext(); |
| 810 } | 829 } |
| 811 | 830 |
| 812 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( | 831 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( |
| 813 content::ProtocolHandlerMap* protocol_handlers, | 832 content::ProtocolHandlerMap* protocol_handlers, |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1063 #if defined(ENABLE_EXTENSIONS) | 1082 #if defined(ENABLE_EXTENSIONS) |
| 1064 extension_policy_, | 1083 extension_policy_, |
| 1065 #endif | 1084 #endif |
| 1066 pref_service_.Pass(), | 1085 pref_service_.Pass(), |
| 1067 original_profile, | 1086 original_profile, |
| 1068 guest_session_, | 1087 guest_session_, |
| 1069 supervised_user_id_, | 1088 supervised_user_id_, |
| 1070 policy_service_.Pass(), | 1089 policy_service_.Pass(), |
| 1071 testing_factories_); | 1090 testing_factories_); |
| 1072 } | 1091 } |
| OLD | NEW |