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