Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / dom_distiller / dom_distiller_service_factory.cc
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
20 namespace dom_distiller {
21
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()) {
31 }
32
33 // static
34 DomDistillerServiceFactory* DomDistillerServiceFactory::GetInstance() {
35   return Singleton<DomDistillerServiceFactory>::get();
36 }
37
38 // static
39 DomDistillerContextKeyedService*
40 DomDistillerServiceFactory::GetForBrowserContext(
41     content::BrowserContext* context) {
42   return static_cast<DomDistillerContextKeyedService*>(
43       GetInstance()->GetServiceForBrowserContext(context, true));
44 }
45
46 DomDistillerServiceFactory::DomDistillerServiceFactory()
47     : BrowserContextKeyedServiceFactory(
48           "DomDistillerService",
49           BrowserContextDependencyManager::GetInstance()) {}
50
51 DomDistillerServiceFactory::~DomDistillerServiceFactory() {}
52
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());
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(
67       new DomDistillerStore(db.Pass(), 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(dom_distiller_store.Pass(),
86                                           distiller_factory.Pass(),
87                                           distiller_page_factory.Pass(),
88                                           distilled_page_prefs.Pass());
89
90   return service;
91 }
92
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);
97 }
98
99 }  // namespace dom_distiller