1// Copyright 2014 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/lazy_dom_distiller_service.h"
6
7#include "chrome/browser/chrome_notification_types.h"
8#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
9#include "chrome/browser/profiles/profile.h"
10#include "components/dom_distiller/core/distilled_page_prefs.h"
11#include "components/dom_distiller/core/distiller_page.h"
12#include "components/dom_distiller/core/dom_distiller_service.h"
13#include "content/public/browser/notification_source.h"
14
15namespace dom_distiller {
16
17LazyDomDistillerService::LazyDomDistillerService(
18    Profile* profile,
19    const DomDistillerServiceFactory* service_factory)
20    : profile_(profile), service_factory_(service_factory) {
21  registrar_.Add(this,
22                 chrome::NOTIFICATION_PROFILE_DESTROYED,
23                 content::Source<Profile>(profile));
24}
25
26LazyDomDistillerService::~LazyDomDistillerService() {
27}
28
29// This will create an object and schedule work the first time it's called
30// and just return an existing object after that.
31DomDistillerServiceInterface* LazyDomDistillerService::instance() const {
32  return service_factory_->GetForBrowserContext(profile_);
33}
34
35void LazyDomDistillerService::Observe(
36    int type,
37    const content::NotificationSource& source,
38    const content::NotificationDetails& details) {
39  DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
40  DCHECK_EQ(profile_, content::Source<Profile>(source).ptr());
41  delete this;
42}
43
44syncer::SyncableService* LazyDomDistillerService::GetSyncableService() const {
45  return instance()->GetSyncableService();
46}
47
48bool LazyDomDistillerService::HasEntry(const std::string& entry_id) {
49  return instance()->HasEntry(entry_id);
50}
51
52std::string LazyDomDistillerService::GetUrlForEntry(
53    const std::string& entry_id) {
54  return instance()->GetUrlForEntry(entry_id);
55}
56
57const std::string LazyDomDistillerService::AddToList(
58    const GURL& url,
59    scoped_ptr<DistillerPage> distiller_page,
60    const ArticleAvailableCallback& article_cb) {
61  return instance()->AddToList(url, distiller_page.Pass(), article_cb);
62}
63
64std::vector<ArticleEntry> LazyDomDistillerService::GetEntries() const {
65  return instance()->GetEntries();
66}
67
68scoped_ptr<ArticleEntry> LazyDomDistillerService::RemoveEntry(
69    const std::string& entry_id) {
70  return instance()->RemoveEntry(entry_id);
71}
72
73scoped_ptr<ViewerHandle> LazyDomDistillerService::ViewEntry(
74    ViewRequestDelegate* delegate,
75    scoped_ptr<DistillerPage> distiller_page,
76    const std::string& entry_id) {
77  return instance()->ViewEntry(delegate, distiller_page.Pass(), entry_id);
78}
79
80scoped_ptr<ViewerHandle> LazyDomDistillerService::ViewUrl(
81    ViewRequestDelegate* delegate,
82    scoped_ptr<DistillerPage> distiller_page,
83    const GURL& url) {
84  return instance()->ViewUrl(delegate, distiller_page.Pass(), url);
85}
86
87scoped_ptr<DistillerPage>
88LazyDomDistillerService::CreateDefaultDistillerPage(
89    const gfx::Size& render_view_size) {
90  return instance()->CreateDefaultDistillerPage(render_view_size);
91}
92
93scoped_ptr<DistillerPage>
94LazyDomDistillerService::CreateDefaultDistillerPageWithHandle(
95    scoped_ptr<SourcePageHandle> handle) {
96  return instance()->CreateDefaultDistillerPageWithHandle(handle.Pass());
97}
98
99void LazyDomDistillerService::AddObserver(DomDistillerObserver* observer) {
100  instance()->AddObserver(observer);
101}
102
103void LazyDomDistillerService::RemoveObserver(DomDistillerObserver* observer) {
104  instance()->RemoveObserver(observer);
105}
106
107DistilledPagePrefs* LazyDomDistillerService::GetDistilledPagePrefs() {
108  return instance()->GetDistilledPagePrefs();
109}
110
111}  // namespace dom_distiller
112