• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/dom_distiller/core/article_entry.h"
16 #include "components/dom_distiller/core/distiller_page.h"
17 
18 class GURL;
19 
20 namespace syncer {
21 class SyncableService;
22 }
23 
24 namespace dom_distiller {
25 
26 class DistilledArticleProto;
27 class DistilledContentStore;
28 class DistillerFactory;
29 class DistillerPageFactory;
30 class DomDistillerObserver;
31 class DomDistillerStoreInterface;
32 class TaskTracker;
33 class ViewerHandle;
34 class ViewRequestDelegate;
35 
36 // Service for interacting with the Dom Distiller.
37 // Construction, destruction, and usage of this service must happen on the same
38 // thread. Callbacks will be called on that same thread.
39 class DomDistillerServiceInterface {
40  public:
41   typedef base::Callback<void(bool)> ArticleAvailableCallback;
~DomDistillerServiceInterface()42   virtual ~DomDistillerServiceInterface() {}
43 
44   virtual syncer::SyncableService* GetSyncableService() const = 0;
45 
46   // Distill the article at |url| and add the resulting entry to the DOM
47   // distiller list. |article_cb| is always invoked, and the bool argument to it
48   // represents whether the article is available offline.
49   // Use CreateDefaultDistillerPage() to create a default |distiller_page|.
50   // The provided |distiller_page| is only used if there is not already a
51   // distillation task in progress for the given |url|.
52   virtual const std::string AddToList(
53       const GURL& url,
54       scoped_ptr<DistillerPage> distiller_page,
55       const ArticleAvailableCallback& article_cb) = 0;
56 
57   // Gets the full list of entries.
58   virtual std::vector<ArticleEntry> GetEntries() const = 0;
59 
60   // Removes the specified entry from the dom distiller store.
61   virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string& entry_id) = 0;
62 
63   // Request to view an article by entry id. Returns a null pointer if no entry
64   // with |entry_id| exists. The ViewerHandle should be destroyed before the
65   // ViewRequestDelegate. The request will be cancelled when the handle is
66   // destroyed (or when this service is destroyed), which also ensures that
67   // the |delegate| is not called after that.
68   // Use CreateDefaultDistillerPage() to create a default |distiller_page|.
69   // The provided |distiller_page| is only used if there is not already a
70   // distillation task in progress for the given |entry_id|.
71   virtual scoped_ptr<ViewerHandle> ViewEntry(
72       ViewRequestDelegate* delegate,
73       scoped_ptr<DistillerPage> distiller_page,
74       const std::string& entry_id) = 0;
75 
76   // Request to view an article by url.
77   // Use CreateDefaultDistillerPage() to create a default |distiller_page|.
78   // The provided |distiller_page| is only used if there is not already a
79   // distillation task in progress for the given |url|.
80   virtual scoped_ptr<ViewerHandle> ViewUrl(
81       ViewRequestDelegate* delegate,
82       scoped_ptr<DistillerPage> distiller_page,
83       const GURL& url) = 0;
84 
85   // Creates a default DistillerPage.
86   virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPage() = 0;
87   virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPageWithHandle(
88       scoped_ptr<SourcePageHandle> handle) = 0;
89 
90   virtual void AddObserver(DomDistillerObserver* observer) = 0;
91   virtual void RemoveObserver(DomDistillerObserver* observer) = 0;
92 
93  protected:
DomDistillerServiceInterface()94   DomDistillerServiceInterface() {}
95 
96  private:
97   DISALLOW_COPY_AND_ASSIGN(DomDistillerServiceInterface);
98 };
99 
100 // Provide a view of the article list and ways of interacting with it.
101 class DomDistillerService : public DomDistillerServiceInterface {
102  public:
103   DomDistillerService(scoped_ptr<DomDistillerStoreInterface> store,
104                       scoped_ptr<DistillerFactory> distiller_factory,
105                       scoped_ptr<DistillerPageFactory> distiller_page_factory);
106   virtual ~DomDistillerService();
107 
108   // DomDistillerServiceInterface implementation.
109   virtual syncer::SyncableService* GetSyncableService() const OVERRIDE;
110   virtual const std::string AddToList(
111       const GURL& url,
112       scoped_ptr<DistillerPage> distiller_page,
113       const ArticleAvailableCallback& article_cb) OVERRIDE;
114   virtual std::vector<ArticleEntry> GetEntries() const OVERRIDE;
115   virtual scoped_ptr<ArticleEntry> RemoveEntry(
116       const std::string& entry_id) OVERRIDE;
117   virtual scoped_ptr<ViewerHandle> ViewEntry(
118       ViewRequestDelegate* delegate,
119       scoped_ptr<DistillerPage> distiller_page,
120       const std::string& entry_id) OVERRIDE;
121   virtual scoped_ptr<ViewerHandle> ViewUrl(
122       ViewRequestDelegate* delegate,
123       scoped_ptr<DistillerPage> distiller_page,
124       const GURL& url) OVERRIDE;
125   virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPage() OVERRIDE;
126   virtual scoped_ptr<DistillerPage> CreateDefaultDistillerPageWithHandle(
127       scoped_ptr<SourcePageHandle> handle) OVERRIDE;
128   virtual void AddObserver(DomDistillerObserver* observer) OVERRIDE;
129   virtual void RemoveObserver(DomDistillerObserver* observer) OVERRIDE;
130 
131  private:
132   void CancelTask(TaskTracker* task);
133   void AddDistilledPageToList(const ArticleEntry& entry,
134                               const DistilledArticleProto* article_proto,
135                               bool distillation_succeeded);
136 
137   TaskTracker* CreateTaskTracker(const ArticleEntry& entry);
138 
139   TaskTracker* GetTaskTrackerForEntry(const ArticleEntry& entry) const;
140 
141   // Gets the task tracker for the given |url| or |entry|. If no appropriate
142   // tracker exists, this will create one, initialize it, and add it to
143   // |tasks_|.
144   TaskTracker* GetOrCreateTaskTrackerForUrl(const GURL& url);
145   TaskTracker* GetOrCreateTaskTrackerForEntry(const ArticleEntry& entry);
146 
147   scoped_ptr<DomDistillerStoreInterface> store_;
148   scoped_ptr<DistilledContentStore> content_store_;
149   scoped_ptr<DistillerFactory> distiller_factory_;
150   scoped_ptr<DistillerPageFactory> distiller_page_factory_;
151 
152   typedef ScopedVector<TaskTracker> TaskList;
153   TaskList tasks_;
154 
155   DISALLOW_COPY_AND_ASSIGN(DomDistillerService);
156 };
157 
158 }  // namespace dom_distiller
159 
160 #endif  // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_
161