diff options
author | sdefresne <sdefresne@chromium.org> | 2015-06-22 07:19:52 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-22 14:20:28 +0000 |
commit | 680ee2cb41b65df7cfb2b30d882181ca97d35833 (patch) | |
tree | 04f2ac39c8cb728a91536c40241d398e53e013b0 /ios/chrome/browser/history | |
parent | 64a9f6ad68912fe63c0edec7789346a8b7b76446 (diff) | |
download | chromium_src-680ee2cb41b65df7cfb2b30d882181ca97d35833.zip chromium_src-680ee2cb41b65df7cfb2b30d882181ca97d35833.tar.gz chromium_src-680ee2cb41b65df7cfb2b30d882181ca97d35833.tar.bz2 |
[iOS] Upstream TopSitesFactory
Provides an implementation of TopSitesFactory on iOS.
BUG=429756
Review URL: https://codereview.chromium.org/1194893003
Cr-Commit-Position: refs/heads/master@{#335499}
Diffstat (limited to 'ios/chrome/browser/history')
-rw-r--r-- | ios/chrome/browser/history/top_sites_factory.cc | 94 | ||||
-rw-r--r-- | ios/chrome/browser/history/top_sites_factory.h | 49 |
2 files changed, 143 insertions, 0 deletions
diff --git a/ios/chrome/browser/history/top_sites_factory.cc b/ios/chrome/browser/history/top_sites_factory.cc new file mode 100644 index 0000000..538cc3f --- /dev/null +++ b/ios/chrome/browser/history/top_sites_factory.cc @@ -0,0 +1,94 @@ +// 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 "ios/chrome/browser/history/top_sites_factory.h" + +#include "base/memory/singleton.h" +#include "components/dom_distiller/core/url_constants.h" +#include "components/history/core/browser/history_constants.h" +#include "components/history/core/browser/top_sites_impl.h" +#include "components/keyed_service/core/refcounted_keyed_service.h" +#include "components/keyed_service/core/service_access_type.h" +#include "components/keyed_service/ios/browser_state_dependency_manager.h" +#include "components/pref_registry/pref_registry_syncable.h" +#include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h" +#include "ios/public/provider/chrome/browser/chrome_browser_provider.h" +#include "ios/public/provider/chrome/browser/keyed_service_provider.h" +#include "ios/web/public/web_thread.h" +#include "url/gurl.h" +#include "url/url_constants.h" + +namespace ios { + +namespace { +// Returns true if this looks like the type of URL that should be added to the +// history. This filters out URLs such a JavaScript. +bool CanAddURLToHistory(const GURL& url) { + if (!url.is_valid()) + return false; + + // TODO: We should allow ChromeUIScheme URLs if they have been explicitly + // typed. Right now, however, these are marked as typed even when triggered + // by a shortcut or menu action. + if (url.SchemeIs(url::kJavaScriptScheme) || + url.SchemeIs(dom_distiller::kDomDistillerScheme) || + url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme())) + return false; + + // Allow all about: and chrome: URLs except about:blank, since the user may + // like to see "chrome://memory/", etc. in their history and autocomplete. + if (url == GURL(url::kAboutBlankURL)) + return false; + + return true; +} +} // namespace + +// static +scoped_refptr<history::TopSites> TopSitesFactory::GetForBrowserState( + ios::ChromeBrowserState* browser_state) { + return make_scoped_refptr(static_cast<history::TopSites*>( + GetInstance()->GetServiceForBrowserState(browser_state, true).get())); +} + +// static +TopSitesFactory* TopSitesFactory::GetInstance() { + return Singleton<TopSitesFactory>::get(); +} + +TopSitesFactory::TopSitesFactory() + : RefcountedBrowserStateKeyedServiceFactory( + "TopSites", + BrowserStateDependencyManager::GetInstance()) { + DependsOn(ios::GetKeyedServiceProvider()->GetHistoryServiceFactory()); +} + +TopSitesFactory::~TopSitesFactory() { +} + +scoped_refptr<RefcountedKeyedService> TopSitesFactory::BuildServiceInstanceFor( + web::BrowserState* context) const { + ios::ChromeBrowserState* browser_state = + ios::ChromeBrowserState::FromBrowserState(context); + scoped_refptr<history::TopSitesImpl> top_sites(new history::TopSitesImpl( + browser_state->GetPrefs(), + ios::GetKeyedServiceProvider()->GetHistoryServiceForBrowserState( + browser_state, ServiceAccessType::EXPLICIT_ACCESS), + history::PrepopulatedPageList(), base::Bind(CanAddURLToHistory))); + top_sites->Init( + browser_state->GetStatePath().Append(history::kTopSitesFilename), + web::WebThread::GetTaskRunnerForThread(web::WebThread::DB)); + return top_sites; +} + +void TopSitesFactory::RegisterBrowserStatePrefs( + user_prefs::PrefRegistrySyncable* registry) { + history::TopSitesImpl::RegisterPrefs(registry); +} + +bool TopSitesFactory::ServiceIsNULLWhileTesting() const { + return true; +} + +} // namespace ios diff --git a/ios/chrome/browser/history/top_sites_factory.h b/ios/chrome/browser/history/top_sites_factory.h new file mode 100644 index 0000000..8f1cbd5 --- /dev/null +++ b/ios/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 IOS_CHROME_BROWSER_HISTORY_TOP_SITES_FACTORY_H_ +#define IOS_CHROME_BROWSER_HISTORY_TOP_SITES_FACTORY_H_ + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "components/keyed_service/ios/refcounted_browser_state_keyed_service_factory.h" + +template <typename T> +struct DefaultSingletonTraits; + +namespace history { +class TopSites; +} + +namespace ios { + +class ChromeBrowserState; + +// TopSitesFactory is a singleton that associates history::TopSites instance to +// ChromeBrowserState. +class TopSitesFactory : public RefcountedBrowserStateKeyedServiceFactory { + public: + static scoped_refptr<history::TopSites> GetForBrowserState( + ios::ChromeBrowserState* browser_state); + static TopSitesFactory* GetInstance(); + + private: + friend struct DefaultSingletonTraits<TopSitesFactory>; + + TopSitesFactory(); + ~TopSitesFactory() override; + + // RefcountedBrowserStateKeyedServiceFactory implementation. + scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( + web::BrowserState* context) const override; + void RegisterBrowserStatePrefs( + user_prefs::PrefRegistrySyncable* registry) override; + bool ServiceIsNULLWhileTesting() const override; + + DISALLOW_COPY_AND_ASSIGN(TopSitesFactory); +}; + +} // namespace ios + +#endif // IOS_CHROME_BROWSER_HISTORY_TOP_SITES_FACTORY_H_ |