Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / task_tracker.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_TASK_TRACKER_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_TASK_TRACKER_H_
7
8 #include <string>
9 #include <vector>
10
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"
17
18 class GURL;
19
20 namespace dom_distiller {
21
22 class DistilledArticleProto;
23
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.
26 class ViewerHandle {
27   typedef base::Callback<void()> CancelCallback;
28
29  public:
30   explicit ViewerHandle(CancelCallback callback);
31   ~ViewerHandle();
32
33  private:
34   CancelCallback cancel_callback_;
35   DISALLOW_COPY_AND_ASSIGN(ViewerHandle);
36 };
37
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 {
41  public:
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;
48 };
49
50 // A TaskTracker manages the various tasks related to viewing, saving,
51 // distilling, and fetching an article's distilled content.
52 //
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).
56 //
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.
61 //
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.
65 //
66 // After creating a TaskTracker, a consumer of distilled content should be added
67 // and at least one of the sources should be started.
68 class TaskTracker {
69  public:
70   typedef base::Callback<void(TaskTracker*)> CancelCallback;
71   typedef base::Callback<
72       void(const ArticleEntry&, const DistilledArticleProto*, bool)>
73       SaveCallback;
74
75   TaskTracker(const ArticleEntry& entry, CancelCallback callback);
76   ~TaskTracker();
77
78   // |factory| will not be stored after this call.
79   void StartDistiller(DistillerFactory* factory);
80   void StartBlobFetcher();
81
82   void AddSaveCallback(const SaveCallback& callback);
83
84   void CancelSaveCallbacks();
85
86   // The ViewerHandle should be destroyed before the ViewRequestDelegate.
87   scoped_ptr<ViewerHandle> AddViewer(ViewRequestDelegate* delegate);
88
89   const std::string& GetEntryId() const;
90   bool HasEntryId(const std::string& entry_id) const;
91   bool HasUrl(const GURL& url) const;
92
93  private:
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);
98
99   // Runs all callbacks passing |distillation_succeeded| and clears them. Should
100   // be called through ScheduleSaveCallbacks.
101   void DoSaveCallbacks(bool distillation_succeeded);
102
103   void RemoveViewer(ViewRequestDelegate* delegate);
104   void NotifyViewer(ViewRequestDelegate* delegate);
105
106   void MaybeCancel();
107
108   CancelCallback cancel_callback_;
109   std::vector<SaveCallback> save_callbacks_;
110
111   scoped_ptr<Distiller> distiller_;
112
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_;
116
117   ArticleEntry entry_;
118   scoped_ptr<DistilledArticleProto> distilled_article_;
119   bool distillation_complete_;
120
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_;
124
125   DISALLOW_COPY_AND_ASSIGN(TaskTracker);
126 };
127
128 }  // namespace dom_distiller
129
130 #endif  // COMPONENTS_DOM_DISTILLER_CORE_TASK_TRACKER_H_