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/scoped_ptr.h"
9 #include "base/memory/singleton.h"
10 #include "base/threading/sequenced_worker_pool.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_service.h"
14 #include "components/dom_distiller/core/dom_distiller_store.h"
15 #include "components/dom_distiller/ios/distiller_page_factory_ios.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
18 #include "components/leveldb_proto/proto_database.h"
19 #include "components/leveldb_proto/proto_database_impl.h"
20 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
21 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
22 #include "ios/web/public/browser_state.h"
23 #include "ios/web/public/web_thread.h"
26 // A simple wrapper for DomDistillerService to expose it as a
28 class DomDistillerKeyedService
29 : public KeyedService
,
30 public dom_distiller::DomDistillerService
{
32 DomDistillerKeyedService(
33 scoped_ptr
<dom_distiller::DomDistillerStoreInterface
> store
,
34 scoped_ptr
<dom_distiller::DistillerFactory
> distiller_factory
,
35 scoped_ptr
<dom_distiller::DistillerPageFactory
> distiller_page_factory
,
36 scoped_ptr
<dom_distiller::DistilledPagePrefs
> distilled_page_prefs
)
37 : DomDistillerService(store
.Pass(),
38 distiller_factory
.Pass(),
39 distiller_page_factory
.Pass(),
40 distilled_page_prefs
.Pass()) {}
42 ~DomDistillerKeyedService() override
{}
45 DISALLOW_COPY_AND_ASSIGN(DomDistillerKeyedService
);
49 namespace dom_distiller
{
52 DomDistillerServiceFactory
* DomDistillerServiceFactory::GetInstance() {
53 return Singleton
<DomDistillerServiceFactory
>::get();
57 DomDistillerService
* DomDistillerServiceFactory::GetForBrowserState(
58 web::BrowserState
* browser_state
) {
59 return static_cast<DomDistillerKeyedService
*>(
60 GetInstance()->GetServiceForBrowserState(browser_state
, true));
63 DomDistillerServiceFactory::DomDistillerServiceFactory()
64 : BrowserStateKeyedServiceFactory(
65 "DomDistillerService",
66 BrowserStateDependencyManager::GetInstance()) {
69 DomDistillerServiceFactory::~DomDistillerServiceFactory() {
72 KeyedService
* DomDistillerServiceFactory::BuildServiceInstanceFor(
73 web::BrowserState
* browser_state
) const {
74 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
=
75 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(
76 web::WebThread::GetBlockingPool()->GetSequenceToken());
78 scoped_ptr
<leveldb_proto::ProtoDatabaseImpl
<ArticleEntry
>> db(
79 new leveldb_proto::ProtoDatabaseImpl
<ArticleEntry
>(
80 background_task_runner
));
82 base::FilePath
database_dir(
83 browser_state
->GetStatePath().Append(FILE_PATH_LITERAL("Articles")));
85 scoped_ptr
<DomDistillerStore
> dom_distiller_store(
86 new DomDistillerStore(db
.Pass(), database_dir
));
88 scoped_ptr
<DistillerPageFactory
> distiller_page_factory(
89 new DistillerPageFactoryIOS(browser_state
));
90 scoped_ptr
<DistillerURLFetcherFactory
> distiller_url_fetcher_factory(
91 new DistillerURLFetcherFactory(browser_state
->GetRequestContext()));
93 dom_distiller::proto::DomDistillerOptions options
;
94 scoped_ptr
<DistillerFactory
> distiller_factory(
95 new DistillerFactoryImpl(distiller_url_fetcher_factory
.Pass(), options
));
96 scoped_ptr
<DistilledPagePrefs
> distilled_page_prefs(new DistilledPagePrefs(
97 ios::ChromeBrowserState::FromBrowserState(browser_state
)->GetPrefs()));
99 DomDistillerKeyedService
* service
=
100 new DomDistillerKeyedService(
101 dom_distiller_store
.Pass(), distiller_factory
.Pass(),
102 distiller_page_factory
.Pass(), distilled_page_prefs
.Pass());
107 web::BrowserState
* DomDistillerServiceFactory::GetBrowserStateToUse(
108 web::BrowserState
* browser_state
) const {
109 // Makes normal profile and off-the-record profile use same service instance.
110 return GetBrowserStateRedirectedInIncognito(browser_state
);
113 } // namespace dom_distiller