1 // Copyright 2015 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 "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "components/dom_distiller/core/article_entry.h"
11 #include "components/dom_distiller/core/distiller.h"
12 #include "components/dom_distiller/core/dom_distiller_service.h"
13 #include "components/dom_distiller/core/dom_distiller_store.h"
14 #include "components/dom_distiller/ios/distiller_page_factory_ios.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
17 #include "components/leveldb_proto/proto_database.h"
18 #include "components/leveldb_proto/proto_database_impl.h"
19 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
20 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
21 #include "ios/web/public/browser_state.h"
22 #include "ios/web/public/web_thread.h"
25 // A simple wrapper for DomDistillerService to expose it as a
27 class DomDistillerKeyedService
28 : public KeyedService
,
29 public dom_distiller::DomDistillerService
{
31 DomDistillerKeyedService(
32 scoped_ptr
<dom_distiller::DomDistillerStoreInterface
> store
,
33 scoped_ptr
<dom_distiller::DistillerFactory
> distiller_factory
,
34 scoped_ptr
<dom_distiller::DistillerPageFactory
> distiller_page_factory
,
35 scoped_ptr
<dom_distiller::DistilledPagePrefs
> distilled_page_prefs
)
36 : DomDistillerService(store
.Pass(),
37 distiller_factory
.Pass(),
38 distiller_page_factory
.Pass(),
39 distilled_page_prefs
.Pass()) {}
41 ~DomDistillerKeyedService() override
{}
44 DISALLOW_COPY_AND_ASSIGN(DomDistillerKeyedService
);
48 namespace dom_distiller
{
51 DomDistillerServiceFactory
* DomDistillerServiceFactory::GetInstance() {
52 return base::Singleton
<DomDistillerServiceFactory
>::get();
56 DomDistillerService
* DomDistillerServiceFactory::GetForBrowserState(
57 ios::ChromeBrowserState
* browser_state
) {
58 return static_cast<DomDistillerKeyedService
*>(
59 GetInstance()->GetServiceForBrowserState(browser_state
, true));
62 DomDistillerServiceFactory::DomDistillerServiceFactory()
63 : BrowserStateKeyedServiceFactory(
64 "DomDistillerService",
65 BrowserStateDependencyManager::GetInstance()) {
68 DomDistillerServiceFactory::~DomDistillerServiceFactory() {
71 scoped_ptr
<KeyedService
> DomDistillerServiceFactory::BuildServiceInstanceFor(
72 web::BrowserState
* context
) const {
73 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
=
74 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(
75 web::WebThread::GetBlockingPool()->GetSequenceToken());
77 scoped_ptr
<leveldb_proto::ProtoDatabaseImpl
<ArticleEntry
>> db(
78 new leveldb_proto::ProtoDatabaseImpl
<ArticleEntry
>(
79 background_task_runner
));
81 base::FilePath
database_dir(
82 context
->GetStatePath().Append(FILE_PATH_LITERAL("Articles")));
84 scoped_ptr
<DomDistillerStore
> dom_distiller_store(
85 new DomDistillerStore(db
.Pass(), database_dir
));
87 scoped_ptr
<DistillerPageFactory
> distiller_page_factory(
88 new DistillerPageFactoryIOS(context
));
89 scoped_ptr
<DistillerURLFetcherFactory
> distiller_url_fetcher_factory(
90 new DistillerURLFetcherFactory(context
->GetRequestContext()));
92 dom_distiller::proto::DomDistillerOptions options
;
93 scoped_ptr
<DistillerFactory
> distiller_factory(
94 new DistillerFactoryImpl(distiller_url_fetcher_factory
.Pass(), options
));
95 scoped_ptr
<DistilledPagePrefs
> distilled_page_prefs(new DistilledPagePrefs(
96 ios::ChromeBrowserState::FromBrowserState(context
)->GetPrefs()));
98 return make_scoped_ptr(new DomDistillerKeyedService(
99 dom_distiller_store
.Pass(), distiller_factory
.Pass(),
100 distiller_page_factory
.Pass(), distilled_page_prefs
.Pass()));
103 web::BrowserState
* DomDistillerServiceFactory::GetBrowserStateToUse(
104 web::BrowserState
* context
) const {
105 // Makes normal profile and off-the-record profile use same service instance.
106 return GetBrowserStateRedirectedInIncognito(context
);
109 } // namespace dom_distiller