diff options
author | jitendra.ks <jitendra.ks@samsung.com> | 2015-01-28 01:47:18 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-28 09:48:04 +0000 |
commit | 30f03390995328247e3642aa5cd2cc9aae781b45 (patch) | |
tree | ea168f4f4e06752a55912a4d332350be8365b446 /chrome/browser | |
parent | e03f042d1c76262248e42334b1d0326b0e4319e7 (diff) | |
download | chromium_src-30f03390995328247e3642aa5cd2cc9aae781b45.zip chromium_src-30f03390995328247e3642aa5cd2cc9aae781b45.tar.gz chromium_src-30f03390995328247e3642aa5cd2cc9aae781b45.tar.bz2 |
Topsites become keyedService based.
TopSites was created and owned by profile.
This patch creates a TopSitesServicefactory which is keyedService.
So now instead of calling GetTopSites() from profile, can get the TopSites from
TopSitesFactory.
BUG=435501
Review URL: https://codereview.chromium.org/815983002
Cr-Commit-Position: refs/heads/master@{#313468}
Diffstat (limited to 'chrome/browser')
40 files changed, 264 insertions, 150 deletions
diff --git a/chrome/browser/android/dev_tools_manager_delegate_android.cc b/chrome/browser/android/dev_tools_manager_delegate_android.cc index a5da5f7..6a17140 100644 --- a/chrome/browser/android/dev_tools_manager_delegate_android.cc +++ b/chrome/browser/android/dev_tools_manager_delegate_android.cc @@ -11,6 +11,7 @@ #include "chrome/browser/android/tab_android.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/ui/android/tab_model/tab_model.h" #include "chrome/browser/ui/android/tab_model/tab_model_list.h" @@ -337,7 +338,8 @@ void DevToolsManagerDelegateAndroid::EnumerateTargets(TargetCallback callback) { std::string DevToolsManagerDelegateAndroid::GetPageThumbnailData( const GURL& url) { Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); - history::TopSites* top_sites = profile->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile); if (top_sites) { scoped_refptr<base::RefCountedMemory> data; if (top_sites->GetPageThumbnail(url, false, &data)) diff --git a/chrome/browser/android/dev_tools_server.cc b/chrome/browser/android/dev_tools_server.cc index 57dc0ea..bc3e454 100644 --- a/chrome/browser/android/dev_tools_server.cc +++ b/chrome/browser/android/dev_tools_server.cc @@ -21,6 +21,7 @@ #include "chrome/browser/android/tab_android.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/ui/android/tab_model/tab_model.h" #include "chrome/browser/ui/android/tab_model/tab_model_list.h" @@ -108,7 +109,8 @@ class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { static void PopulatePageThumbnails() { Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); - history::TopSites* top_sites = profile->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile); if (top_sites) top_sites->SyncWithHistory(); } diff --git a/chrome/browser/android/most_visited_sites.cc b/chrome/browser/android/most_visited_sites.cc index 5b57d4b..886c822 100644 --- a/chrome/browser/android/most_visited_sites.cc +++ b/chrome/browser/android/most_visited_sites.cc @@ -19,6 +19,7 @@ #include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_android.h" #include "chrome/browser/search/suggestions/suggestions_service_factory.h" @@ -230,7 +231,8 @@ void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, QueryMostVisitedURLs(); - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (top_sites) { // TopSites updates itself after a delay. To ensure up-to-date results, // force an update now. @@ -238,7 +240,7 @@ void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, // Register as TopSitesObserver so that we can update ourselves when the // TopSites changes. - scoped_observer_.Add(top_sites); + scoped_observer_.Add(top_sites.get()); } } @@ -253,7 +255,7 @@ void MostVisitedSites::GetURLThumbnail(JNIEnv* env, j_callback->Reset(env, j_callback_obj); std::string url_string = ConvertJavaStringToUTF8(env, url); - scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); + scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); // If the Suggestions service is enabled and in use, create a callback to // fetch a server thumbnail from it, in case the local thumbnail is not found. @@ -286,7 +288,8 @@ void MostVisitedSites::BlacklistUrl(JNIEnv* env, switch (mv_source_) { case TOP_SITES: { - TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); DCHECK(top_sites); top_sites->AddBlacklistedURL(GURL(url)); break; @@ -363,7 +366,7 @@ void MostVisitedSites::QueryMostVisitedURLs() { } void MostVisitedSites::InitiateTopSitesQuery() { - TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); if (!top_sites) return; diff --git a/chrome/browser/android/provider/chrome_browser_provider.cc b/chrome/browser/android/provider/chrome_browser_provider.cc index b0a1015..3cd405c 100644 --- a/chrome/browser/android/provider/chrome_browser_provider.cc +++ b/chrome/browser/android/provider/chrome_browser_provider.cc @@ -29,6 +29,7 @@ #include "chrome/browser/history/history_service.h" #include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/search_engines/template_url_service_factory.h" @@ -1165,7 +1166,7 @@ ChromeBrowserProvider::ChromeBrowserProvider(JNIEnv* env, jobject obj) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); profile_ = g_browser_process->profile_manager()->GetLastUsedProfile(); bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); - top_sites_ = profile_->GetTopSites(); + top_sites_ = TopSitesFactory::GetForProfile(profile_); favicon_service_ = FaviconServiceFactory::GetForProfile( profile_, ServiceAccessType::EXPLICIT_ACCESS), service_.reset(new AndroidHistoryProviderService(profile_)); diff --git a/chrome/browser/android/provider/chrome_browser_provider.h b/chrome/browser/android/provider/chrome_browser_provider.h index 6fc7661..a2ff280 100644 --- a/chrome/browser/android/provider/chrome_browser_provider.h +++ b/chrome/browser/android/provider/chrome_browser_provider.h @@ -211,7 +211,7 @@ class ChromeBrowserProvider : public bookmarks::BaseBookmarkModelObserver, // alive. Profile* profile_; bookmarks::BookmarkModel* bookmark_model_; - history::TopSites* top_sites_; + scoped_refptr<history::TopSites> top_sites_; FaviconService* favicon_service_; scoped_ptr<AndroidHistoryProviderService> service_; diff --git a/chrome/browser/autocomplete/zero_suggest_provider.cc b/chrome/browser/autocomplete/zero_suggest_provider.cc index 3feaae3..3ce8f5c 100644 --- a/chrome/browser/autocomplete/zero_suggest_provider.cc +++ b/chrome/browser/autocomplete/zero_suggest_provider.cc @@ -20,6 +20,7 @@ #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" #include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/common/pref_names.h" @@ -318,7 +319,8 @@ AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( void ZeroSuggestProvider::Run(const GURL& suggest_url) { if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { most_visited_urls_.clear(); - history::TopSites* ts = profile_->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(profile_); if (ts) { waiting_for_most_visited_urls_request_ = true; ts->GetMostVisitedURLs( diff --git a/chrome/browser/autocomplete/zero_suggest_provider_unittest.cc b/chrome/browser/autocomplete/zero_suggest_provider_unittest.cc index e79c849..1c4085d 100644 --- a/chrome/browser/autocomplete/zero_suggest_provider_unittest.cc +++ b/chrome/browser/autocomplete/zero_suggest_provider_unittest.cc @@ -11,6 +11,7 @@ #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/common/pref_names.h" #include "chrome/test/base/testing_profile.h" @@ -64,7 +65,6 @@ class FakeEmptyTopSites : public history::TopSites { return false; } void ClearBlacklistedURLs() override {} - void Shutdown() override {} base::CancelableTaskTracker::TaskId StartQueryForMostVisited() override { return 0; } @@ -90,6 +90,10 @@ class FakeEmptyTopSites : public history::TopSites { bool AddForcedURL(const GURL& url, const base::Time& time) override { return false; } + + // RefcountedKeyedService: + void ShutdownOnUIThread() override {} + // content::NotificationObserver: void Observe(int type, const content::NotificationSource& source, @@ -109,6 +113,12 @@ void FakeEmptyTopSites::GetMostVisitedURLs( mv_callback = callback; } +scoped_refptr<RefcountedKeyedService> BuildFakeEmptyTopSites( + content::BrowserContext* profile) { + scoped_refptr<history::TopSites> top_sites = new FakeEmptyTopSites(); + return top_sites; +} + } // namespace @@ -175,8 +185,9 @@ void ZeroSuggestProviderTest::SetUp() { turl_model->Add(default_t_url_); turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); - profile_.SetTopSites(new FakeEmptyTopSites()); - + profile_.DestroyTopSites(); + TopSitesFactory::GetInstance()->SetTestingFactory(&profile_, + BuildFakeEmptyTopSites); provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); } @@ -264,8 +275,9 @@ TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) { provider_->Start(input, false, true); EXPECT_TRUE(provider_->matches().empty()); - static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( - urls); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(&profile_); + static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); // Should have verbatim match + most visited url match. EXPECT_EQ(2U, provider_->matches().size()); provider_->Stop(false); @@ -275,8 +287,7 @@ TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) { EXPECT_TRUE(provider_->matches().empty()); // Most visited results arriving after Stop() has been called, ensure they // are not displayed. - static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( - urls); + static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); EXPECT_TRUE(provider_->matches().empty()); } @@ -316,8 +327,9 @@ TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { provider_->Start(srp_input, false, true); EXPECT_TRUE(provider_->matches().empty()); // Most visited results arriving after a new request has been started. - static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( - urls); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(&profile_); + static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); EXPECT_TRUE(provider_->matches().empty()); } diff --git a/chrome/browser/devtools/browser_list_tabcontents_provider.cc b/chrome/browser/devtools/browser_list_tabcontents_provider.cc index b69e4c32..47001b5 100644 --- a/chrome/browser/devtools/browser_list_tabcontents_provider.cc +++ b/chrome/browser/devtools/browser_list_tabcontents_provider.cc @@ -6,6 +6,7 @@ #include "base/path_service.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_iterator.h" @@ -28,7 +29,7 @@ std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { for (std::set<Profile*>::iterator it = profiles.begin(); it != profiles.end(); ++it) { - history::TopSites* ts = (*it)->GetTopSites(); + scoped_refptr<history::TopSites> ts = TopSitesFactory::GetForProfile(*it); if (ts) { // TopSites updates itself after a delay. Ask TopSites to update itself // when we're about to show the remote debugging landing page. diff --git a/chrome/browser/devtools/chrome_devtools_manager_delegate.cc b/chrome/browser/devtools/chrome_devtools_manager_delegate.cc index 1bd1a82..e933922 100644 --- a/chrome/browser/devtools/chrome_devtools_manager_delegate.cc +++ b/chrome/browser/devtools/chrome_devtools_manager_delegate.cc @@ -8,6 +8,7 @@ #include "chrome/browser/devtools/devtools_target_impl.h" #include "chrome/browser/devtools/devtools_window.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/ui/browser.h" @@ -50,7 +51,8 @@ std::string ChromeDevToolsManagerDelegate::GetPageThumbnailData( const GURL& url) { for (chrome::BrowserIterator it; !it.done(); it.Next()) { Profile* profile = (*it)->profile(); - history::TopSites* top_sites = profile->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile); if (!top_sites) continue; scoped_refptr<base::RefCountedMemory> data; diff --git a/chrome/browser/extensions/api/top_sites/top_sites_api.cc b/chrome/browser/extensions/api/top_sites/top_sites_api.cc index 940667a..ead56e62 100644 --- a/chrome/browser/extensions/api/top_sites/top_sites_api.cc +++ b/chrome/browser/extensions/api/top_sites/top_sites_api.cc @@ -7,6 +7,7 @@ #include "base/bind.h" #include "base/values.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" @@ -18,7 +19,8 @@ TopSitesGetFunction::TopSitesGetFunction() TopSitesGetFunction::~TopSitesGetFunction() {} bool TopSitesGetFunction::RunAsync() { - history::TopSites* ts = GetProfile()->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(GetProfile()); if (!ts) return false; diff --git a/chrome/browser/extensions/api/top_sites/top_sites_apitest.cc b/chrome/browser/extensions/api/top_sites/top_sites_apitest.cc index 0cb6427..d1e36b8 100644 --- a/chrome/browser/extensions/api/top_sites/top_sites_apitest.cc +++ b/chrome/browser/extensions/api/top_sites/top_sites_apitest.cc @@ -7,6 +7,7 @@ #include "chrome/browser/extensions/api/top_sites/top_sites_api.h" #include "chrome/browser/extensions/extension_function_test_utils.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/test/base/in_process_browser_test.h" @@ -23,7 +24,8 @@ class TopSitesExtensionTest : public InProcessBrowserTest { } void SetUpOnMainThread() override { - history::TopSites* top_sites = browser()->profile()->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(browser()->profile()); // This may return async or sync. If sync, top_sites_inited_ will be true // before we get to the conditional below. Otherwise, we'll run a nested diff --git a/chrome/browser/history/expire_history_backend_unittest.cc b/chrome/browser/history/expire_history_backend_unittest.cc index b8c21db..8810ade 100644 --- a/chrome/browser/history/expire_history_backend_unittest.cc +++ b/chrome/browser/history/expire_history_backend_unittest.cc @@ -15,6 +15,7 @@ #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/test/base/testing_profile.h" #include "chrome/tools/profiles/thumbnail-inl.h" #include "components/history/core/browser/expire_history_backend.h" @@ -104,7 +105,6 @@ class ExpireHistoryTest : public testing::Test, scoped_ptr<HistoryDatabase> main_db_; scoped_ptr<ThumbnailDatabase> thumb_db_; TestingProfile profile_; - scoped_refptr<TopSites> top_sites_; // Time at the beginning of the test, so everybody agrees what "now" is. const Time now_; @@ -132,11 +132,9 @@ class ExpireHistoryTest : public testing::Test, expirer_.SetDatabases(main_db_.get(), thumb_db_.get()); profile_.CreateTopSites(); profile_.BlockUntilTopSitesLoaded(); - top_sites_ = profile_.GetTopSites(); } void TearDown() override { - top_sites_ = NULL; ClearLastNotifications(); @@ -220,9 +218,11 @@ void ExpireHistoryTest::AddExampleData(URLID url_ids[3], Time visit_times[4]) { Time time; GURL gurl; - top_sites_->SetPageThumbnail(url_row1.url(), thumbnail, score); - top_sites_->SetPageThumbnail(url_row2.url(), thumbnail, score); - top_sites_->SetPageThumbnail(url_row3.url(), thumbnail, score); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(&profile_); + top_sites->SetPageThumbnail(url_row1.url(), thumbnail, score); + top_sites->SetPageThumbnail(url_row2.url(), thumbnail, score); + top_sites->SetPageThumbnail(url_row3.url(), thumbnail, score); // Four visits. VisitRow visit_row1; @@ -302,7 +302,9 @@ bool ExpireHistoryTest::HasThumbnail(URLID url_id) { return false; GURL url = info.url(); scoped_refptr<base::RefCountedMemory> data; - return top_sites_->GetPageThumbnail(url, false, &data); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(&profile_); + return top_sites->GetPageThumbnail(url, false, &data); } void ExpireHistoryTest::EnsureURLInfoGone(const URLRow& row, bool expired) { diff --git a/chrome/browser/history/history_backend.cc b/chrome/browser/history/history_backend.cc index c34f9b4..f8dea15 100644 --- a/chrome/browser/history/history_backend.cc +++ b/chrome/browser/history/history_backend.cc @@ -26,7 +26,6 @@ #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/history/in_memory_history_backend.h" #include "chrome/browser/history/in_memory_history_backend.h" -#include "chrome/browser/history/top_sites.h" #include "chrome/browser/history/typed_url_syncable_service.h" #include "chrome/browser/history/typed_url_syncable_service.h" #include "chrome/common/chrome_constants.h" diff --git a/chrome/browser/history/history_service.cc b/chrome/browser/history/history_service.cc index 5a36bbc..59b1f86 100644 --- a/chrome/browser/history/history_service.cc +++ b/chrome/browser/history/history_service.cc @@ -33,7 +33,6 @@ #include "chrome/browser/history/history_backend.h" #include "chrome/browser/history/in_memory_history_backend.h" #include "chrome/browser/history/in_memory_url_index.h" -#include "chrome/browser/history/top_sites.h" #include "chrome/browser/history/web_history_service.h" #include "chrome/browser/history/web_history_service_factory.h" #include "chrome/browser/profiles/profile.h" diff --git a/chrome/browser/history/top_sites.cc b/chrome/browser/history/top_sites.cc index a83077b..cf1d9df 100644 --- a/chrome/browser/history/top_sites.cc +++ b/chrome/browser/history/top_sites.cc @@ -5,7 +5,6 @@ #include "chrome/browser/history/top_sites.h" #include "base/strings/string_util.h" -#include "chrome/browser/history/top_sites_impl.h" #include "chrome/grit/chromium_strings.h" #include "chrome/grit/generated_resources.h" #include "chrome/grit/locale_settings.h" @@ -16,22 +15,19 @@ namespace history { const TopSites::PrepopulatedPage kPrepopulatedPages[] = { #if !defined(OS_ANDROID) - { IDS_CHROME_WELCOME_URL, IDS_NEW_TAB_CHROME_WELCOME_PAGE_TITLE, - IDR_PRODUCT_LOGO_16, IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL, - SkColorSetRGB(0, 147, 60) }, - { IDS_WEBSTORE_URL, IDS_EXTENSION_WEB_STORE_TITLE, - IDR_WEBSTORE_ICON_16, IDR_NEWTAB_WEBSTORE_THUMBNAIL, - SkColorSetRGB(63, 132, 197) } + {IDS_CHROME_WELCOME_URL, + IDS_NEW_TAB_CHROME_WELCOME_PAGE_TITLE, + IDR_PRODUCT_LOGO_16, + IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL, + SkColorSetRGB(0, 147, 60)}, + {IDS_WEBSTORE_URL, + IDS_EXTENSION_WEB_STORE_TITLE, + IDR_WEBSTORE_ICON_16, + IDR_NEWTAB_WEBSTORE_THUMBNAIL, + SkColorSetRGB(63, 132, 197)} #endif }; -// static -TopSites* TopSites::Create(Profile* profile, const base::FilePath& db_name) { - TopSitesImpl* top_sites_impl = new TopSitesImpl(profile); - top_sites_impl->Init(db_name); - return top_sites_impl; -} - TopSites::TopSites() { } diff --git a/chrome/browser/history/top_sites.h b/chrome/browser/history/top_sites.h index 0dd87a5..32a01da 100644 --- a/chrome/browser/history/top_sites.h +++ b/chrome/browser/history/top_sites.h @@ -8,11 +8,11 @@ #include "base/basictypes.h" #include "base/callback.h" #include "base/gtest_prod_util.h" -#include "base/memory/ref_counted.h" #include "base/observer_list.h" #include "base/task/cancelable_task_tracker.h" #include "components/history/core/browser/history_types.h" #include "components/history/core/common/thumbnail_score.h" +#include "components/keyed_service/core/refcounted_keyed_service.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" #include "third_party/skia/include/core/SkColor.h" @@ -38,15 +38,11 @@ class TopSitesObserver; // // Some methods should only be called from the UI thread (see method // descriptions below). All others are assumed to be threadsafe. -class TopSites - : public base::RefCountedThreadSafe<TopSites>, - public content::NotificationObserver { +class TopSites : public RefcountedKeyedService, + public content::NotificationObserver { public: TopSites(); - // Initializes TopSites. - static TopSites* Create(Profile* profile, const base::FilePath& db_name); - // Sets the given thumbnail for the given URL. Returns true if the thumbnail // was updated. False means either the URL wasn't known to us, or we felt // that our current thumbnail was superior to the given one. Should be called @@ -120,9 +116,6 @@ class TopSites // Clear the blacklist. Should be called from the UI thread. virtual void ClearBlacklistedURLs() = 0; - // Shuts down top sites. - virtual void Shutdown() = 0; - // Query history service for the list of available thumbnails. Returns the // task id for the request, or |base::CancelableTaskTracker::kBadTaskId| if a // request could not be made. Public only for testing purposes. diff --git a/chrome/browser/history/top_sites_factory.cc b/chrome/browser/history/top_sites_factory.cc new file mode 100644 index 0000000..b0e77b55 --- /dev/null +++ b/chrome/browser/history/top_sites_factory.cc @@ -0,0 +1,51 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/history/top_sites_factory.h" + +#include "base/memory/singleton.h" +#include "chrome/browser/history/top_sites_impl.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/common/chrome_constants.h" +#include "components/keyed_service/content/browser_context_dependency_manager.h" + +// static +scoped_refptr<history::TopSites> TopSitesFactory::GetForProfile( + Profile* profile) { + return static_cast<history::TopSites*>( + GetInstance()->GetServiceForBrowserContext(profile, true).get()); +} + +// static +scoped_refptr<history::TopSites> TopSitesFactory::GetForProfileIfExists( + Profile* profile) { + return static_cast<history::TopSites*>( + GetInstance()->GetServiceForBrowserContext(profile, false).get()); +} + +// static +TopSitesFactory* TopSitesFactory::GetInstance() { + return Singleton<TopSitesFactory>::get(); +} + +TopSitesFactory::TopSitesFactory() + : RefcountedBrowserContextKeyedServiceFactory( + "TopSites", + BrowserContextDependencyManager::GetInstance()) { +} + +TopSitesFactory::~TopSitesFactory() { +} + +scoped_refptr<RefcountedKeyedService> TopSitesFactory::BuildServiceInstanceFor( + content::BrowserContext* context) const { + history::TopSitesImpl* top_sites = + new history::TopSitesImpl(static_cast<Profile*>(context)); + top_sites->Init(context->GetPath().Append(chrome::kTopSitesFilename)); + return make_scoped_refptr(top_sites); +} + +bool TopSitesFactory::ServiceIsNULLWhileTesting() const { + return true; +} diff --git a/chrome/browser/history/top_sites_factory.h b/chrome/browser/history/top_sites_factory.h new file mode 100644 index 0000000..19c80cc --- /dev/null +++ b/chrome/browser/history/top_sites_factory.h @@ -0,0 +1,49 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_FACTORY_H_ +#define CHROME_BROWSER_HISTORY_TOP_SITES_FACTORY_H_ + +#include "base/memory/ref_counted.h" +#include "components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h" + +class Profile; + +namespace history { +class TopSites; +} + +template <typename T> +struct DefaultSingletonTraits; + +// Used for creating and fetching a per-profile instance of the +// TopSites. +class TopSitesFactory : public RefcountedBrowserContextKeyedServiceFactory { + public: + // Get the TopSites service for |profile|, creating one if needed. + static scoped_refptr<history::TopSites> GetForProfile(Profile* profile); + + // Get the TopSites service for |profile|, but do not create it if it doesn't + // exist. + static scoped_refptr<history::TopSites> GetForProfileIfExists( + Profile* profile); + + // Get the singleton instance of the factory. + static TopSitesFactory* GetInstance(); + + private: + friend struct DefaultSingletonTraits<TopSitesFactory>; + + TopSitesFactory(); + ~TopSitesFactory() override; + + // Overridden from BrowserContextKeyedServiceFactory. + scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( + content::BrowserContext* context) const override; + bool ServiceIsNULLWhileTesting() const override; + + DISALLOW_COPY_AND_ASSIGN(TopSitesFactory); +}; + +#endif // CHROME_BROWSER_HISTORY_TOP_SITES_FACTORY_H_ diff --git a/chrome/browser/history/top_sites_impl.cc b/chrome/browser/history/top_sites_impl.cc index e6c27b1..3110170 100644 --- a/chrome/browser/history/top_sites_impl.cc +++ b/chrome/browser/history/top_sites_impl.cc @@ -371,13 +371,15 @@ void TopSitesImpl::ClearBlacklistedURLs() { NotifyTopSitesChanged(); } -void TopSitesImpl::Shutdown() { +void TopSitesImpl::ShutdownOnUIThread() { profile_ = NULL; // Cancel all requests so that the service doesn't callback to us after we've // invoked Shutdown (this could happen if we have a pending request and // Shutdown is invoked). cancelable_task_tracker_.TryCancelAll(); - backend_->Shutdown(); + registrar_.RemoveAll(); + if (backend_) + backend_->Shutdown(); } // static diff --git a/chrome/browser/history/top_sites_impl.h b/chrome/browser/history/top_sites_impl.h index 130eb58..9f48b67 100644 --- a/chrome/browser/history/top_sites_impl.h +++ b/chrome/browser/history/top_sites_impl.h @@ -75,7 +75,6 @@ class TopSitesImpl : public TopSites, public HistoryServiceObserver { void RemoveBlacklistedURL(const GURL& url) override; bool IsBlacklisted(const GURL& url) override; void ClearBlacklistedURLs() override; - void Shutdown() override; base::CancelableTaskTracker::TaskId StartQueryForMostVisited() override; bool IsKnownURL(const GURL& url) override; const std::string& GetCanonicalURLString(const GURL& url) const override; @@ -85,6 +84,9 @@ class TopSitesImpl : public TopSites, public HistoryServiceObserver { bool loaded() const override; bool AddForcedURL(const GURL& url, const base::Time& time) override; + // RefcountedKeyedService: + void ShutdownOnUIThread() override; + protected: ~TopSitesImpl() override; diff --git a/chrome/browser/history/top_sites_impl_unittest.cc b/chrome/browser/history/top_sites_impl_unittest.cc index b1aa4d5..01a01f2 100644 --- a/chrome/browser/history/top_sites_impl_unittest.cc +++ b/chrome/browser/history/top_sites_impl_unittest.cc @@ -9,6 +9,7 @@ #include "base/task/cancelable_task_tracker.h" #include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/history/top_sites_impl.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" @@ -195,7 +196,8 @@ class TopSitesImplTest : public HistoryUnitTestBase { } TopSitesImpl* top_sites() { - return static_cast<TopSitesImpl*>(profile_->GetTopSites()); + return static_cast<TopSitesImpl*>( + TopSitesFactory::GetForProfile(profile_.get()).get()); } TestingProfile* profile() {return profile_.get();} HistoryService* history_service() { diff --git a/chrome/browser/jumplist_win.cc b/chrome/browser/jumplist_win.cc index 0c866b5..c47a114 100644 --- a/chrome/browser/jumplist_win.cc +++ b/chrome/browser/jumplist_win.cc @@ -20,6 +20,7 @@ #include "chrome/browser/favicon/favicon_service_factory.h" #include "chrome/browser/history/history_service.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/metrics/jumplist_metrics_win.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_info_cache.h" @@ -249,7 +250,8 @@ JumpList::JumpList(Profile* profile) icon_dir_ = profile_->GetPath().Append(chrome::kJumpListIconDirname); use_profiles_category_ = HasProfilesJumplistExperiment(); - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (top_sites) { // TopSites updates itself after a delay. This is especially noticable when // your profile is empty. Ask TopSites to update itself when jumplist is @@ -311,7 +313,8 @@ void JumpList::Terminate() { TabRestoreServiceFactory::GetForProfile(profile_); if (tab_restore_service) tab_restore_service->RemoveObserver(this); - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (top_sites) top_sites->RemoveObserver(this); registrar_.reset(); diff --git a/chrome/browser/profiles/off_the_record_profile_impl.cc b/chrome/browser/profiles/off_the_record_profile_impl.cc index d792f8f..b2202e7 100644 --- a/chrome/browser/profiles/off_the_record_profile_impl.cc +++ b/chrome/browser/profiles/off_the_record_profile_impl.cc @@ -428,14 +428,6 @@ Time OffTheRecordProfileImpl::GetStartTime() const { return start_time_; } -history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() { - return NULL; -} - -history::TopSites* OffTheRecordProfileImpl::GetTopSites() { - return NULL; -} - void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) { } diff --git a/chrome/browser/profiles/off_the_record_profile_impl.h b/chrome/browser/profiles/off_the_record_profile_impl.h index 96608dc..a38a135 100644 --- a/chrome/browser/profiles/off_the_record_profile_impl.h +++ b/chrome/browser/profiles/off_the_record_profile_impl.h @@ -62,8 +62,6 @@ class OffTheRecordProfileImpl : public Profile { HostContentSettingsMap* GetHostContentSettingsMap() override; bool IsSameProfile(Profile* profile) override; Time GetStartTime() const override; - history::TopSites* GetTopSitesWithoutCreating() override; - history::TopSites* GetTopSites() override; base::FilePath last_selected_directory() override; void set_last_selected_directory(const base::FilePath& path) override; bool WasCreatedByVersionOrLater(const std::string& version) override; diff --git a/chrome/browser/profiles/profile.h b/chrome/browser/profiles/profile.h index d7a60ce..09d6a58 100644 --- a/chrome/browser/profiles/profile.h +++ b/chrome/browser/profiles/profile.h @@ -57,10 +57,6 @@ namespace storage { class FileSystemContext; } -namespace history { -class TopSites; -} - namespace net { class SSLConfigService; } @@ -189,13 +185,6 @@ class Profile : public content::BrowserContext { // Returns whether the profile is a legacy supervised user profile. virtual bool IsLegacySupervised() = 0; - // Returns a pointer to the TopSites (thumbnail manager) instance - // for this profile. - virtual history::TopSites* GetTopSites() = 0; - - // Variant of GetTopSites that doesn't force creation. - virtual history::TopSites* GetTopSitesWithoutCreating() = 0; - // Accessor. The instance is created upon first access. virtual ExtensionSpecialStoragePolicy* GetExtensionSpecialStoragePolicy() = 0; diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc index a99e29a..8b4ce42 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -38,7 +38,6 @@ #include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/download_service.h" #include "chrome/browser/download/download_service_factory.h" -#include "chrome/browser/history/top_sites.h" #include "chrome/browser/net/net_pref_observer.h" #include "chrome/browser/net/predictor.h" #include "chrome/browser/net/pref_proxy_config_tracker.h" @@ -744,9 +743,6 @@ ProfileImpl::~ProfileImpl() { BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices( this); - if (top_sites_.get()) - top_sites_->Shutdown(); - if (pref_proxy_config_tracker_) pref_proxy_config_tracker_->DetachFromPrefService(); @@ -1089,18 +1085,6 @@ Time ProfileImpl::GetStartTime() const { return start_time_; } -history::TopSites* ProfileImpl::GetTopSites() { - if (!top_sites_.get()) { - top_sites_ = history::TopSites::Create( - this, GetPath().Append(chrome::kTopSitesFilename)); - } - return top_sites_.get(); -} - -history::TopSites* ProfileImpl::GetTopSitesWithoutCreating() { - return top_sites_.get(); -} - #if defined(ENABLE_SESSION_SERVICE) void ProfileImpl::StopCreateSessionServiceTimer() { create_session_service_timer_.Stop(); diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h index 286be73..03078af 100644 --- a/chrome/browser/profiles/profile_impl.h +++ b/chrome/browser/profiles/profile_impl.h @@ -103,8 +103,6 @@ class ProfileImpl : public Profile { bool IsSupervised() override; bool IsChild() override; bool IsLegacySupervised() override; - history::TopSites* GetTopSites() override; - history::TopSites* GetTopSitesWithoutCreating() override; ExtensionSpecialStoragePolicy* GetExtensionSpecialStoragePolicy() override; PrefService* GetPrefs() override; chrome::ChromeZoomLevelPrefs* GetZoomLevelPrefs() override; @@ -249,8 +247,6 @@ class ProfileImpl : public Profile { // See GetStartTime for details. base::Time start_time_; - scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. - #if defined(OS_CHROMEOS) scoped_ptr<chromeos::Preferences> chromeos_preferences_; diff --git a/chrome/browser/search/instant_service.cc b/chrome/browser/search/instant_service.cc index 31b68d6f..08fb328 100644 --- a/chrome/browser/search/instant_service.cc +++ b/chrome/browser/search/instant_service.cc @@ -6,6 +6,7 @@ #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/instant_io_context.h" #include "chrome/browser/search/instant_service_observer.h" @@ -92,7 +93,8 @@ InstantService::InstantService(Profile* profile) content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, content::NotificationService::AllSources()); - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (top_sites) top_sites->AddObserver(this); @@ -158,7 +160,8 @@ void InstantService::RemoveObserver(InstantServiceObserver* observer) { } void InstantService::DeleteMostVisitedItem(const GURL& url) { - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (!top_sites) return; @@ -166,7 +169,8 @@ void InstantService::DeleteMostVisitedItem(const GURL& url) { } void InstantService::UndoMostVisitedDeletion(const GURL& url) { - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (!top_sites) return; @@ -174,7 +178,8 @@ void InstantService::UndoMostVisitedDeletion(const GURL& url) { } void InstantService::UndoAllMostVisitedDeletions() { - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (!top_sites) return; @@ -204,7 +209,8 @@ void InstantService::Shutdown() { instant_io_context_)); } - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (top_sites) top_sites->RemoveObserver(this); diff --git a/chrome/browser/search/instant_service_factory.cc b/chrome/browser/search/instant_service_factory.cc index 0865807..e20d92e 100644 --- a/chrome/browser/search/instant_service_factory.cc +++ b/chrome/browser/search/instant_service_factory.cc @@ -4,6 +4,7 @@ #include "chrome/browser/search/instant_service_factory.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/instant_service.h" @@ -34,6 +35,7 @@ InstantServiceFactory::InstantServiceFactory() #if defined(ENABLE_THEMES) DependsOn(ThemeServiceFactory::GetInstance()); #endif + DependsOn(TopSitesFactory::GetInstance()); } InstantServiceFactory::~InstantServiceFactory() { diff --git a/chrome/browser/thumbnails/thumbnail_list_source.cc b/chrome/browser/thumbnails/thumbnail_list_source.cc index bd447c2..9e0bf6e 100644 --- a/chrome/browser/thumbnails/thumbnail_list_source.cc +++ b/chrome/browser/thumbnails/thumbnail_list_source.cc @@ -12,6 +12,7 @@ #include "base/memory/ref_counted_memory.h" #include "base/strings/string_util.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/instant_io_context.h" #include "chrome/browser/thumbnails/thumbnail_service.h" @@ -94,15 +95,17 @@ void ThumbnailListSource::StartDataRequest( int render_process_id, int render_frame_id, const content::URLDataSource::GotDataCallback& callback) { - if (!profile_->GetTopSites()) { + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); + if (!top_sites) { callback.Run(NULL); return; } - profile_->GetTopSites()->GetMostVisitedURLs( + top_sites->GetMostVisitedURLs( base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable, - weak_ptr_factory_.GetWeakPtr(), - callback), true); + weak_ptr_factory_.GetWeakPtr(), callback), + true); } std::string ThumbnailListSource::GetMimeType(const std::string& path) const { diff --git a/chrome/browser/thumbnails/thumbnail_service_factory.cc b/chrome/browser/thumbnails/thumbnail_service_factory.cc index 70a055d..1daa9bc 100644 --- a/chrome/browser/thumbnails/thumbnail_service_factory.cc +++ b/chrome/browser/thumbnails/thumbnail_service_factory.cc @@ -5,6 +5,7 @@ #include "chrome/browser/thumbnails/thumbnail_service_factory.h" #include "base/logging.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/thumbnails/thumbnail_service.h" #include "chrome/browser/thumbnails/thumbnail_service_impl.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" @@ -16,6 +17,7 @@ ThumbnailServiceFactory::ThumbnailServiceFactory() : RefcountedBrowserContextKeyedServiceFactory( "ThumbnailService", BrowserContextDependencyManager::GetInstance()) { + DependsOn(TopSitesFactory::GetInstance()); } ThumbnailServiceFactory::~ThumbnailServiceFactory() { diff --git a/chrome/browser/thumbnails/thumbnail_service_impl.cc b/chrome/browser/thumbnails/thumbnail_service_impl.cc index 9a8f2be..ccd59c1 100644 --- a/chrome/browser/thumbnails/thumbnail_service_impl.cc +++ b/chrome/browser/thumbnails/thumbnail_service_impl.cc @@ -8,6 +8,7 @@ #include "base/memory/ref_counted_memory.h" #include "base/time/time.h" #include "chrome/browser/history/history_service.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/thumbnails/content_based_thumbnailing_algorithm.h" #include "chrome/browser/thumbnails/simple_thumbnail_crop.h" #include "chrome/browser/thumbnails/thumbnailing_context.h" @@ -37,7 +38,7 @@ void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, const GURL& url) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (top_sites.get() != NULL) + if (top_sites) top_sites->AddForcedURL(url, base::Time::Now()); } @@ -46,7 +47,7 @@ void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, namespace thumbnails { ThumbnailServiceImpl::ThumbnailServiceImpl(Profile* profile) - : top_sites_(profile->GetTopSites()), + : top_sites_(TopSitesFactory::GetForProfile(profile)), use_thumbnail_retargeting_(IsThumbnailRetargetingEnabled()) { } @@ -56,7 +57,7 @@ ThumbnailServiceImpl::~ThumbnailServiceImpl() { bool ThumbnailServiceImpl::SetPageThumbnail(const ThumbnailingContext& context, const gfx::Image& thumbnail) { scoped_refptr<history::TopSites> local_ptr(top_sites_); - if (local_ptr.get() == NULL) + if (!local_ptr) return false; return local_ptr->SetPageThumbnail(context.url, thumbnail, context.score); @@ -67,7 +68,7 @@ bool ThumbnailServiceImpl::GetPageThumbnail( bool prefix_match, scoped_refptr<base::RefCountedMemory>* bytes) { scoped_refptr<history::TopSites> local_ptr(top_sites_); - if (local_ptr.get() == NULL) + if (!local_ptr) return false; return local_ptr->GetPageThumbnail(url, prefix_match, bytes); @@ -75,7 +76,7 @@ bool ThumbnailServiceImpl::GetPageThumbnail( void ThumbnailServiceImpl::AddForcedURL(const GURL& url) { scoped_refptr<history::TopSites> local_ptr(top_sites_); - if (local_ptr.get() == NULL) + if (!local_ptr) return; // Adding @@ -94,7 +95,7 @@ ThumbnailingAlgorithm* ThumbnailServiceImpl::GetThumbnailingAlgorithm() bool ThumbnailServiceImpl::ShouldAcquirePageThumbnail(const GURL& url) { scoped_refptr<history::TopSites> local_ptr(top_sites_); - if (local_ptr.get() == NULL) + if (!local_ptr) return false; // Skip if the given URL is not appropriate for history. diff --git a/chrome/browser/thumbnails/thumbnail_service_unittest.cc b/chrome/browser/thumbnails/thumbnail_service_unittest.cc index 32f564c..7b870f8f 100644 --- a/chrome/browser/thumbnails/thumbnail_service_unittest.cc +++ b/chrome/browser/thumbnails/thumbnail_service_unittest.cc @@ -5,20 +5,21 @@ #include "chrome/browser/thumbnails/thumbnail_service_impl.h" #include "base/memory/ref_counted.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/history/top_sites_impl.h" #include "chrome/test/base/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" typedef testing::Test ThumbnailServiceTest; +namespace { + // A mock version of TopSitesImpl, used for testing // ShouldAcquirePageThumbnail(). class MockTopSites : public history::TopSitesImpl { public: explicit MockTopSites(Profile* profile) - : history::TopSitesImpl(profile), - capacity_(1) { - } + : history::TopSitesImpl(profile), capacity_(1) {} // history::TopSitesImpl overrides. bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; } @@ -51,24 +52,33 @@ class MockTopSites : public history::TopSitesImpl { DISALLOW_COPY_AND_ASSIGN(MockTopSites); }; +// Testing factory that build a |MockTopSites| instance. +scoped_refptr<RefcountedKeyedService> BuildMockTopSites( + content::BrowserContext* profile) { + return scoped_refptr<RefcountedKeyedService>( + new MockTopSites(static_cast<Profile*>(profile))); +} + // A mock version of TestingProfile holds MockTopSites. class MockProfile : public TestingProfile { public: - MockProfile() : mock_top_sites_(new MockTopSites(this)) { + MockProfile() { + TopSitesFactory::GetInstance()->SetTestingFactory(this, BuildMockTopSites); } - history::TopSites* GetTopSites() override { return mock_top_sites_.get(); } - void AddKnownURL(const GURL& url, const ThumbnailScore& score) { - mock_top_sites_->AddKnownURL(url, score); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(this); + static_cast<MockTopSites*>(top_sites.get())->AddKnownURL(url, score); } private: - scoped_refptr<MockTopSites> mock_top_sites_; DISALLOW_COPY_AND_ASSIGN(MockProfile); }; +} // namespace + TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) { const GURL kGoodURL("http://www.google.com/"); const GURL kBadURL("chrome://newtab"); @@ -94,7 +104,9 @@ TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) { ThumbnailScore bad_score; bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp. profile.AddKnownURL(kGoodURL, bad_score); - ASSERT_TRUE(profile.GetTopSites()->IsNonForcedFull()); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(&profile); + ASSERT_TRUE(top_sites->IsNonForcedFull()); // Should be false, as the top sites data is full, and the new URL is // not known. diff --git a/chrome/browser/ui/app_list/test/fake_profile.cc b/chrome/browser/ui/app_list/test/fake_profile.cc index ff8aac1..f51f17e 100644 --- a/chrome/browser/ui/app_list/test/fake_profile.cc +++ b/chrome/browser/ui/app_list/test/fake_profile.cc @@ -112,14 +112,6 @@ bool FakeProfile::IsLegacySupervised() { return false; } -history::TopSites* FakeProfile::GetTopSites() { - return NULL; -} - -history::TopSites* FakeProfile::GetTopSitesWithoutCreating() { - return NULL; -} - ExtensionSpecialStoragePolicy* FakeProfile::GetExtensionSpecialStoragePolicy() { return NULL; } diff --git a/chrome/browser/ui/app_list/test/fake_profile.h b/chrome/browser/ui/app_list/test/fake_profile.h index 16ede3b..30a1533d 100644 --- a/chrome/browser/ui/app_list/test/fake_profile.h +++ b/chrome/browser/ui/app_list/test/fake_profile.h @@ -59,8 +59,6 @@ class FakeProfile : public Profile { bool IsSupervised() override; bool IsChild() override; bool IsLegacySupervised() override; - history::TopSites* GetTopSites() override; - history::TopSites* GetTopSitesWithoutCreating() override; ExtensionSpecialStoragePolicy* GetExtensionSpecialStoragePolicy() override; PrefService* GetPrefs() override; PrefService* GetOffTheRecordPrefs() override; diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index 5ae4a23..3c96c62 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -58,6 +58,7 @@ #include "chrome/browser/file_select_helper.h" #include "chrome/browser/first_run/first_run.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/lifetime/application_lifetime.h" #include "chrome/browser/notifications/notification_ui_manager.h" @@ -1882,7 +1883,8 @@ OmniboxView* Browser::GetOmniboxView() { } std::set<std::string> Browser::GetOpenUrls() { - history::TopSites* top_sites = profile_->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile_); if (!top_sites) // NULL for Incognito profiles. return std::set<std::string>(); diff --git a/chrome/browser/ui/views/frame/global_menu_bar_x11.cc b/chrome/browser/ui/views/frame/global_menu_bar_x11.cc index 69606c6..d239a79 100644 --- a/chrome/browser/ui/views/frame/global_menu_bar_x11.cc +++ b/chrome/browser/ui/views/frame/global_menu_bar_x11.cc @@ -17,6 +17,7 @@ #include "chrome/app/chrome_command_ids.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/profiles/profile_manager.h" @@ -425,13 +426,13 @@ void GlobalMenuBarX11::InitServer(unsigned long xid) { base::Unretained(this))); OnBookmarkBarVisibilityChanged(); - top_sites_ = profile_->GetTopSites(); + top_sites_ = TopSitesFactory::GetForProfile(profile_); if (top_sites_) { GetTopSitesData(); // Register as TopSitesObserver so that we can update ourselves when the // TopSites changes. - scoped_observer_.Add(top_sites_); + scoped_observer_.Add(top_sites_.get()); } ProfileManager* profile_manager = g_browser_process->profile_manager(); diff --git a/chrome/browser/ui/views/frame/global_menu_bar_x11.h b/chrome/browser/ui/views/frame/global_menu_bar_x11.h index d82ed7d..9dbe90c 100644 --- a/chrome/browser/ui/views/frame/global_menu_bar_x11.h +++ b/chrome/browser/ui/views/frame/global_menu_bar_x11.h @@ -172,7 +172,7 @@ class GlobalMenuBarX11 : public AvatarMenuObserver, // Tracks value of the kShowBookmarkBar preference. PrefChangeRegistrar pref_change_registrar_; - history::TopSites* top_sites_; + scoped_refptr<history::TopSites> top_sites_; TabRestoreService* tab_restore_service_; // weak diff --git a/chrome/browser/ui/webui/ntp/most_visited_handler.cc b/chrome/browser/ui/webui/ntp/most_visited_handler.cc index b8e6ebb..c6d4bad 100644 --- a/chrome/browser/ui/webui/ntp/most_visited_handler.cc +++ b/chrome/browser/ui/webui/ntp/most_visited_handler.cc @@ -21,6 +21,7 @@ #include "base/threading/thread.h" #include "base/values.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/thumbnails/thumbnail_list_source.h" #include "chrome/browser/ui/browser.h" @@ -82,7 +83,8 @@ void MostVisitedHandler::RegisterMessages() { content::URLDataSource::Add( profile, new FaviconSource(profile, FaviconSource::FAVICON)); - history::TopSites* top_sites = profile->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile); if (top_sites) { // TopSites updates itself after a delay. This is especially noticable when // your profile is empty. Ask TopSites to update itself when we're about to @@ -91,7 +93,7 @@ void MostVisitedHandler::RegisterMessages() { // Register as TopSitesObserver so that we can update ourselves when the // TopSites changes. - scoped_observer_.Add(top_sites); + scoped_observer_.Add(top_sites.get()); } // We pre-emptively make a fetch for the most visited pages so we have the @@ -136,7 +138,8 @@ void MostVisitedHandler::SendPagesValue() { const base::DictionaryValue* url_blacklist = profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); bool has_blacklisted_urls = !url_blacklist->empty(); - history::TopSites* ts = profile->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(profile); if (ts) has_blacklisted_urls = ts->HasBlacklistedItems(); @@ -149,7 +152,8 @@ void MostVisitedHandler::SendPagesValue() { } void MostVisitedHandler::StartQueryForMostVisited() { - history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); if (ts) { ts->GetMostVisitedURLs( base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, @@ -175,7 +179,8 @@ void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( return; } content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); - history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); if (ts) ts->RemoveBlacklistedURL(GURL(url)); } @@ -184,7 +189,8 @@ void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); - history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); if (ts) ts->ClearBlacklistedURLs(); } @@ -250,7 +256,8 @@ void MostVisitedHandler::TopSitesChanged(history::TopSites* top_sites) { } void MostVisitedHandler::BlacklistUrl(const GURL& url) { - history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); + scoped_refptr<history::TopSites> ts = + TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); if (ts) ts->AddBlacklistedURL(url); content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); diff --git a/chrome/browser/ui/webui/ntp/suggestions_page_handler.cc b/chrome/browser/ui/webui/ntp/suggestions_page_handler.cc index 2689c20..5bfdb2d 100644 --- a/chrome/browser/ui/webui/ntp/suggestions_page_handler.cc +++ b/chrome/browser/ui/webui/ntp/suggestions_page_handler.cc @@ -16,6 +16,7 @@ #include "base/threading/thread.h" #include "base/values.h" #include "chrome/browser/history/top_sites.h" +#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/webui/favicon_source.h" #include "chrome/browser/ui/webui/ntp/ntp_stats.h" @@ -68,7 +69,8 @@ void SuggestionsHandler::RegisterMessages() { // TODO(georgey) change the source of the web-sites to provide our data. // Initial commit uses top sites as a data source. - history::TopSites* top_sites = profile->GetTopSites(); + scoped_refptr<history::TopSites> top_sites = + TopSitesFactory::GetForProfile(profile); if (top_sites) { // TopSites updates itself after a delay. This is especially noticable when // your profile is empty. Ask TopSites to update itself when we're about to @@ -77,7 +79,7 @@ void SuggestionsHandler::RegisterMessages() { // Register as TopSitesObserver so that we can update ourselves when the // TopSites changes. - scoped_observer_.Add(top_sites); + scoped_observer_.Add(top_sites.get()); } // Setup the suggestions sources. |