8f4ea70f7bed5f4a69d685deb205e92a9977e02e
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / dom_distiller_service.h
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
17 class GURL;
18
19 namespace syncer {
20 class SyncableService;
21 }
22
23 namespace dom_distiller {
24
25 class DistilledArticleProto;
26 class DistillerFactory;
27 class DomDistillerObserver;
28 class DomDistillerStoreInterface;
29 class TaskTracker;
30 class ViewerHandle;
31 class ViewRequestDelegate;
32
33 // Service for interacting with the Dom Distiller.
34 // Construction, destruction, and usage of this service must happen on the same
35 // thread. Callbacks will be called on that same thread.
36 class DomDistillerServiceInterface {
37  public:
38   typedef base::Callback<void(bool)> ArticleAvailableCallback;
39   virtual ~DomDistillerServiceInterface() {}
40
41   virtual syncer::SyncableService* GetSyncableService() const = 0;
42
43   // Distill the article at |url| and add the resulting entry to the DOM
44   // distiller list. |article_cb| is always invoked, and the bool argument to it
45   // represents whether the article is available offline.
46   virtual const std::string AddToList(
47       const GURL& url,
48       const ArticleAvailableCallback& article_cb) = 0;
49
50   // Gets the full list of entries.
51   virtual std::vector<ArticleEntry> GetEntries() const = 0;
52
53   // Removes the specified entry from the dom distiller store.
54   virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string& entry_id) = 0;
55
56   // Request to view an article by entry id. Returns a null pointer if no entry
57   // with |entry_id| exists. The ViewerHandle should be destroyed before the
58   // ViewRequestDelegate. The request will be cancelled when the handle is
59   // destroyed (or when this service is destroyed), which also ensures that
60   // the |delegate| is not called after that.
61   virtual scoped_ptr<ViewerHandle> ViewEntry(ViewRequestDelegate* delegate,
62                                              const std::string& entry_id) = 0;
63
64   // Request to view an article by url.
65   virtual scoped_ptr<ViewerHandle> ViewUrl(ViewRequestDelegate* delegate,
66                                            const GURL& url) = 0;
67
68   virtual void AddObserver(DomDistillerObserver* observer) = 0;
69   virtual void RemoveObserver(DomDistillerObserver* observer) = 0;
70
71  protected:
72   DomDistillerServiceInterface() {}
73
74  private:
75   DISALLOW_COPY_AND_ASSIGN(DomDistillerServiceInterface);
76 };
77
78 // Provide a view of the article list and ways of interacting with it.
79 class DomDistillerService : public DomDistillerServiceInterface {
80  public:
81   DomDistillerService(scoped_ptr<DomDistillerStoreInterface> store,
82                       scoped_ptr<DistillerFactory> distiller_factory);
83   virtual ~DomDistillerService();
84
85   // DomDistillerServiceInterface implementation.
86   virtual syncer::SyncableService* GetSyncableService() const OVERRIDE;
87   virtual const std::string AddToList(
88       const GURL& url,
89       const ArticleAvailableCallback& article_cb) OVERRIDE;
90   virtual std::vector<ArticleEntry> GetEntries() const OVERRIDE;
91   virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string& entry_id)
92       OVERRIDE;
93   virtual scoped_ptr<ViewerHandle> ViewEntry(ViewRequestDelegate* delegate,
94                                              const std::string& entry_id)
95       OVERRIDE;
96   virtual scoped_ptr<ViewerHandle> ViewUrl(ViewRequestDelegate* delegate,
97                                            const GURL& url) OVERRIDE;
98   virtual void AddObserver(DomDistillerObserver* observer) OVERRIDE;
99   virtual void RemoveObserver(DomDistillerObserver* observer) OVERRIDE;
100
101  private:
102   void CancelTask(TaskTracker* task);
103   void AddDistilledPageToList(const ArticleEntry& entry,
104                               const DistilledArticleProto* article_proto,
105                               bool distillation_succeeded);
106
107   TaskTracker* CreateTaskTracker(const ArticleEntry& entry);
108
109   TaskTracker* GetTaskTrackerForEntry(const ArticleEntry& entry) const;
110
111   // Gets the task tracker for the given |url| or |entry|. If no appropriate
112   // tracker exists, this will create one, initialize it, and add it to
113   // |tasks_|.
114   TaskTracker* GetOrCreateTaskTrackerForUrl(const GURL& url);
115   TaskTracker* GetOrCreateTaskTrackerForEntry(const ArticleEntry& entry);
116
117   scoped_ptr<DomDistillerStoreInterface> store_;
118   scoped_ptr<DistillerFactory> distiller_factory_;
119
120   typedef ScopedVector<TaskTracker> TaskList;
121   TaskList tasks_;
122
123   DISALLOW_COPY_AND_ASSIGN(DomDistillerService);
124 };
125
126 }  // namespace dom_distiller
127
128 #endif  // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_