Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / dom_distiller / dom_distiller_service_factory.cc
blob497d5a177cdf2ca5cfff0b93e992590af17376e8
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
7 #include "base/threading/sequenced_worker_pool.h"
8 #include "chrome/browser/profiles/incognito_helpers.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "components/dom_distiller/content/browser/distiller_page_web_contents.h"
11 #include "components/dom_distiller/core/article_entry.h"
12 #include "components/dom_distiller/core/distiller.h"
13 #include "components/dom_distiller/core/dom_distiller_store.h"
14 #include "components/keyed_service/content/browser_context_dependency_manager.h"
15 #include "components/leveldb_proto/proto_database.h"
16 #include "components/leveldb_proto/proto_database_impl.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/browser_thread.h"
20 namespace dom_distiller {
22 DomDistillerContextKeyedService::DomDistillerContextKeyedService(
23 scoped_ptr<DomDistillerStoreInterface> store,
24 scoped_ptr<DistillerFactory> distiller_factory,
25 scoped_ptr<DistillerPageFactory> distiller_page_factory,
26 scoped_ptr<DistilledPagePrefs> distilled_page_prefs)
27 : DomDistillerService(store.Pass(),
28 distiller_factory.Pass(),
29 distiller_page_factory.Pass(),
30 distilled_page_prefs.Pass()) {
33 // static
34 DomDistillerServiceFactory* DomDistillerServiceFactory::GetInstance() {
35 return base::Singleton<DomDistillerServiceFactory>::get();
38 // static
39 DomDistillerContextKeyedService*
40 DomDistillerServiceFactory::GetForBrowserContext(
41 content::BrowserContext* context) {
42 return static_cast<DomDistillerContextKeyedService*>(
43 GetInstance()->GetServiceForBrowserContext(context, true));
46 DomDistillerServiceFactory::DomDistillerServiceFactory()
47 : BrowserContextKeyedServiceFactory(
48 "DomDistillerService",
49 BrowserContextDependencyManager::GetInstance()) {}
51 DomDistillerServiceFactory::~DomDistillerServiceFactory() {}
53 KeyedService* DomDistillerServiceFactory::BuildServiceInstanceFor(
54 content::BrowserContext* profile) const {
55 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
56 content::BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
57 content::BrowserThread::GetBlockingPool()->GetSequenceToken());
59 scoped_ptr<leveldb_proto::ProtoDatabaseImpl<ArticleEntry> > db(
60 new leveldb_proto::ProtoDatabaseImpl<ArticleEntry>(
61 background_task_runner));
63 base::FilePath database_dir(
64 profile->GetPath().Append(FILE_PATH_LITERAL("Articles")));
66 scoped_ptr<DomDistillerStore> dom_distiller_store(
67 new DomDistillerStore(db.Pass(), database_dir));
69 scoped_ptr<DistillerPageFactory> distiller_page_factory(
70 new DistillerPageWebContentsFactory(profile));
71 scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory(
72 new DistillerURLFetcherFactory(profile->GetRequestContext()));
74 dom_distiller::proto::DomDistillerOptions options;
75 if (VLOG_IS_ON(1)) {
76 options.set_debug_level(logging::GetVlogLevelHelper(
77 FROM_HERE.file_name(), ::strlen(FROM_HERE.file_name())));
79 scoped_ptr<DistillerFactory> distiller_factory(
80 new DistillerFactoryImpl(distiller_url_fetcher_factory.Pass(), options));
81 scoped_ptr<DistilledPagePrefs> distilled_page_prefs(
82 new DistilledPagePrefs(Profile::FromBrowserContext(profile)->GetPrefs()));
84 DomDistillerContextKeyedService* service =
85 new DomDistillerContextKeyedService(dom_distiller_store.Pass(),
86 distiller_factory.Pass(),
87 distiller_page_factory.Pass(),
88 distilled_page_prefs.Pass());
90 return service;
93 content::BrowserContext* DomDistillerServiceFactory::GetBrowserContextToUse(
94 content::BrowserContext* context) const {
95 // Makes normal profile and off-the-record profile use same service instance.
96 return chrome::GetBrowserContextRedirectedInIncognito(context);
99 } // namespace dom_distiller