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_TASK_TRACKER_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_TASK_TRACKER_H_
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/dom_distiller/core/article_entry.h"
15 #include "components/dom_distiller/core/distiller.h"
16 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
20 namespace dom_distiller {
22 class DistilledArticleProto;
24 // A handle to a request to view a DOM distiller entry or URL. The request will
25 // be cancelled when the handle is destroyed.
27 typedef base::Callback<void()> CancelCallback;
30 explicit ViewerHandle(CancelCallback callback);
34 CancelCallback cancel_callback_;
35 DISALLOW_COPY_AND_ASSIGN(ViewerHandle);
38 // Interface for a DOM distiller entry viewer. Implement this to make a view
39 // request and receive the data for an entry when it becomes available.
40 class ViewRequestDelegate {
42 virtual ~ViewRequestDelegate() {}
43 // Called when the distilled article contents are available. The
44 // DistilledArticleProto is owned by a TaskTracker instance and is invalidated
45 // when the corresponding ViewerHandle is destroyed (or when the
46 // DomDistillerService is destroyed).
47 virtual void OnArticleReady(const DistilledArticleProto* article_proto) = 0;
50 // A TaskTracker manages the various tasks related to viewing, saving,
51 // distilling, and fetching an article's distilled content.
53 // There are two sources of distilled content, a Distiller and the BlobFetcher.
54 // At any time, at most one of each of these will be in-progress (if one
55 // finishes, the other will be cancelled).
57 // There are also two consumers of that content, a view request and a save
58 // request. There is at most one save request (i.e. we are either adding this to
59 // the reading list or we aren't), and may be multiple view requests. When
60 // the distilled content is ready, each of these requests will be notified.
62 // A view request is cancelled by deleting the corresponding ViewerHandle. Once
63 // all view requests are cancelled (and the save callback has been called if
64 // appropriate) the cancel callback will be called.
66 // After creating a TaskTracker, a consumer of distilled content should be added
67 // and at least one of the sources should be started.
70 typedef base::Callback<void(TaskTracker*)> CancelCallback;
71 typedef base::Callback<
72 void(const ArticleEntry&, const DistilledArticleProto*, bool)>
75 TaskTracker(const ArticleEntry& entry, CancelCallback callback);
78 // |factory| will not be stored after this call.
79 void StartDistiller(DistillerFactory* factory);
80 void StartBlobFetcher();
82 void AddSaveCallback(const SaveCallback& callback);
84 void CancelSaveCallbacks();
86 // The ViewerHandle should be destroyed before the ViewRequestDelegate.
87 scoped_ptr<ViewerHandle> AddViewer(ViewRequestDelegate* delegate);
89 const std::string& GetEntryId() const;
90 bool HasEntryId(const std::string& entry_id) const;
91 bool HasUrl(const GURL& url) const;
94 void OnDistilledDataReady(
95 scoped_ptr<DistilledArticleProto> distilled_article);
96 // Posts a task to run DoSaveCallbacks with |distillation_succeeded|.
97 void ScheduleSaveCallbacks(bool distillation_succeeded);
99 // Runs all callbacks passing |distillation_succeeded| and clears them. Should
100 // be called through ScheduleSaveCallbacks.
101 void DoSaveCallbacks(bool distillation_succeeded);
103 void RemoveViewer(ViewRequestDelegate* delegate);
104 void NotifyViewer(ViewRequestDelegate* delegate);
108 CancelCallback cancel_callback_;
109 std::vector<SaveCallback> save_callbacks_;
111 scoped_ptr<Distiller> distiller_;
113 // A ViewRequestDelegate will be added to this list when a view request is
114 // made and removed when the corresponding ViewerHandle is destroyed.
115 std::vector<ViewRequestDelegate*> viewers_;
118 scoped_ptr<DistilledArticleProto> distilled_article_;
119 bool distillation_complete_;
121 // Note: This should remain the last member so it'll be destroyed and
122 // invalidate its weak pointers before any other members are destroyed.
123 base::WeakPtrFactory<TaskTracker> weak_ptr_factory_;
125 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
128 } // namespace dom_distiller
130 #endif // COMPONENTS_DOM_DISTILLER_CORE_TASK_TRACKER_H_