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.
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_
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"
20 class SyncableService;
23 namespace dom_distiller {
25 class DistilledArticleProto;
26 class DistillerFactory;
27 class DomDistillerObserver;
28 class DomDistillerStoreInterface;
31 class ViewRequestDelegate;
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 {
38 typedef base::Callback<void(bool)> ArticleAvailableCallback;
39 virtual ~DomDistillerServiceInterface() {}
41 virtual syncer::SyncableService* GetSyncableService() const = 0;
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(
48 const ArticleAvailableCallback& article_cb) = 0;
50 // Gets the full list of entries.
51 virtual std::vector<ArticleEntry> GetEntries() const = 0;
53 // Removes the specified entry from the dom distiller store.
54 virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string& entry_id) = 0;
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;
64 // Request to view an article by url.
65 virtual scoped_ptr<ViewerHandle> ViewUrl(ViewRequestDelegate* delegate,
68 virtual void AddObserver(DomDistillerObserver* observer) = 0;
69 virtual void RemoveObserver(DomDistillerObserver* observer) = 0;
72 DomDistillerServiceInterface() {}
75 DISALLOW_COPY_AND_ASSIGN(DomDistillerServiceInterface);
78 // Provide a view of the article list and ways of interacting with it.
79 class DomDistillerService : public DomDistillerServiceInterface {
81 DomDistillerService(scoped_ptr<DomDistillerStoreInterface> store,
82 scoped_ptr<DistillerFactory> distiller_factory);
83 virtual ~DomDistillerService();
85 // DomDistillerServiceInterface implementation.
86 virtual syncer::SyncableService* GetSyncableService() const OVERRIDE;
87 virtual const std::string AddToList(
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)
93 virtual scoped_ptr<ViewerHandle> ViewEntry(ViewRequestDelegate* delegate,
94 const std::string& entry_id)
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;
102 void CancelTask(TaskTracker* task);
103 void AddDistilledPageToList(const ArticleEntry& entry,
104 const DistilledArticleProto* article_proto,
105 bool distillation_succeeded);
107 TaskTracker* CreateTaskTracker(const ArticleEntry& entry);
109 TaskTracker* GetTaskTrackerForEntry(const ArticleEntry& entry) const;
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
114 TaskTracker* GetOrCreateTaskTrackerForUrl(const GURL& url);
115 TaskTracker* GetOrCreateTaskTrackerForEntry(const ArticleEntry& entry);
117 scoped_ptr<DomDistillerStoreInterface> store_;
118 scoped_ptr<DistillerFactory> distiller_factory_;
120 typedef ScopedVector<TaskTracker> TaskList;
123 DISALLOW_COPY_AND_ASSIGN(DomDistillerService);
126 } // namespace dom_distiller
128 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_SERVICE_H_