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.
4
5#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
6
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/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"
19
20namespace dom_distiller {
21
22DomDistillerContextKeyedService::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()) {
31}
32
33// static
34DomDistillerServiceFactory* DomDistillerServiceFactory::GetInstance() {
35  return Singleton<DomDistillerServiceFactory>::get();
36}
37
38// static
39DomDistillerContextKeyedService*
40DomDistillerServiceFactory::GetForBrowserContext(
41    content::BrowserContext* context) {
42  return static_cast<DomDistillerContextKeyedService*>(
43      GetInstance()->GetServiceForBrowserContext(context, true));
44}
45
46DomDistillerServiceFactory::DomDistillerServiceFactory()
47    : BrowserContextKeyedServiceFactory(
48          "DomDistillerService",
49          BrowserContextDependencyManager::GetInstance()) {}
50
51DomDistillerServiceFactory::~DomDistillerServiceFactory() {}
52
53KeyedService* 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());
58
59  scoped_ptr<leveldb_proto::ProtoDatabaseImpl<ArticleEntry> > db(
60      new leveldb_proto::ProtoDatabaseImpl<ArticleEntry>(
61          background_task_runner));
62
63  base::FilePath database_dir(
64      profile->GetPath().Append(FILE_PATH_LITERAL("Articles")));
65
66  scoped_ptr<DomDistillerStore> dom_distiller_store(new DomDistillerStore(
67      db.PassAs<leveldb_proto::ProtoDatabase<ArticleEntry> >(), database_dir));
68
69  scoped_ptr<DistillerPageFactory> distiller_page_factory(
70      new DistillerPageWebContentsFactory(profile));
71  scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory(
72      new DistillerURLFetcherFactory(profile->GetRequestContext()));
73
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())));
78  }
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()));
83
84  DomDistillerContextKeyedService* service =
85      new DomDistillerContextKeyedService(
86          dom_distiller_store.PassAs<DomDistillerStoreInterface>(),
87          distiller_factory.Pass(),
88          distiller_page_factory.Pass(),
89          distilled_page_prefs.Pass());
90
91  return service;
92}
93
94content::BrowserContext* DomDistillerServiceFactory::GetBrowserContextToUse(
95    content::BrowserContext* context) const {
96  // Makes normal profile and off-the-record profile use same service instance.
97  return chrome::GetBrowserContextRedirectedInIncognito(context);
98}
99
100}  // namespace dom_distiller
101