OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/memory/singleton.h" | |
13 #include "base/threading/sequenced_worker_pool.h" | |
14 #include "components/dom_distiller/core/article_entry.h" | |
15 #include "components/dom_distiller/core/distiller.h" | |
16 #include "components/dom_distiller/core/dom_distiller_service.h" | |
17 #include "components/dom_distiller/core/dom_distiller_store.h" | |
18 #include "components/dom_distiller/ios/distiller_page_factory_ios.h" | |
19 #include "components/keyed_service/core/keyed_service.h" | |
20 #include "components/keyed_service/ios/browser_state_dependency_manager.h" | |
21 #include "components/leveldb_proto/proto_database.h" | |
22 #include "components/leveldb_proto/proto_database_impl.h" | |
23 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h" | |
24 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | |
25 #include "ios/web/public/browser_state.h" | |
26 #include "ios/web/public/web_thread.h" | |
27 | |
28 namespace { | |
29 // A simple wrapper for DomDistillerService to expose it as a | |
30 // KeyedService. | |
31 class DomDistillerKeyedService | |
32 : public KeyedService, | |
33 public dom_distiller::DomDistillerService { | |
34 public: | |
35 DomDistillerKeyedService( | |
36 std::unique_ptr<dom_distiller::DomDistillerStoreInterface> store, | |
37 std::unique_ptr<dom_distiller::DistillerFactory> distiller_factory, | |
38 std::unique_ptr<dom_distiller::DistillerPageFactory> | |
39 distiller_page_factory, | |
40 std::unique_ptr<dom_distiller::DistilledPagePrefs> distilled_page_prefs) | |
41 : DomDistillerService(std::move(store), | |
42 std::move(distiller_factory), | |
43 std::move(distiller_page_factory), | |
44 std::move(distilled_page_prefs)) {} | |
45 | |
46 ~DomDistillerKeyedService() override {} | |
47 | |
48 private: | |
49 DISALLOW_COPY_AND_ASSIGN(DomDistillerKeyedService); | |
50 }; | |
51 } // namespace | |
52 | |
53 namespace dom_distiller { | |
54 | |
55 // static | |
56 DomDistillerServiceFactory* DomDistillerServiceFactory::GetInstance() { | |
57 return base::Singleton<DomDistillerServiceFactory>::get(); | |
58 } | |
59 | |
60 // static | |
61 DomDistillerService* DomDistillerServiceFactory::GetForBrowserState( | |
62 ios::ChromeBrowserState* browser_state) { | |
63 return static_cast<DomDistillerKeyedService*>( | |
64 GetInstance()->GetServiceForBrowserState(browser_state, true)); | |
65 } | |
66 | |
67 DomDistillerServiceFactory::DomDistillerServiceFactory() | |
68 : BrowserStateKeyedServiceFactory( | |
69 "DomDistillerService", | |
70 BrowserStateDependencyManager::GetInstance()) { | |
71 } | |
72 | |
73 DomDistillerServiceFactory::~DomDistillerServiceFactory() { | |
74 } | |
75 | |
76 std::unique_ptr<KeyedService> | |
77 DomDistillerServiceFactory::BuildServiceInstanceFor( | |
78 web::BrowserState* context) const { | |
79 scoped_refptr<base::SequencedTaskRunner> background_task_runner = | |
80 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner( | |
81 web::WebThread::GetBlockingPool()->GetSequenceToken()); | |
82 | |
83 std::unique_ptr<leveldb_proto::ProtoDatabaseImpl<ArticleEntry>> db( | |
84 new leveldb_proto::ProtoDatabaseImpl<ArticleEntry>( | |
85 background_task_runner)); | |
86 | |
87 base::FilePath database_dir( | |
88 context->GetStatePath().Append(FILE_PATH_LITERAL("Articles"))); | |
89 | |
90 std::unique_ptr<DomDistillerStore> dom_distiller_store( | |
91 new DomDistillerStore(std::move(db), database_dir)); | |
92 | |
93 std::unique_ptr<DistillerPageFactory> distiller_page_factory( | |
94 new DistillerPageFactoryIOS(context)); | |
95 std::unique_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory( | |
96 new DistillerURLFetcherFactory(context->GetRequestContext())); | |
97 | |
98 dom_distiller::proto::DomDistillerOptions options; | |
99 std::unique_ptr<DistillerFactory> distiller_factory(new DistillerFactoryImpl( | |
100 std::move(distiller_url_fetcher_factory), options)); | |
101 std::unique_ptr<DistilledPagePrefs> distilled_page_prefs( | |
102 new DistilledPagePrefs( | |
103 ios::ChromeBrowserState::FromBrowserState(context)->GetPrefs())); | |
104 | |
105 return base::MakeUnique<DomDistillerKeyedService>( | |
106 std::move(dom_distiller_store), std::move(distiller_factory), | |
107 std::move(distiller_page_factory), std::move(distilled_page_prefs)); | |
108 } | |
109 | |
110 web::BrowserState* DomDistillerServiceFactory::GetBrowserStateToUse( | |
111 web::BrowserState* context) const { | |
112 // Makes normal profile and off-the-record profile use same service instance. | |
113 return GetBrowserStateRedirectedInIncognito(context); | |
114 } | |
115 | |
116 } // namespace dom_distiller | |
OLD | NEW |