#ifndef COMPONENTS_DOM_DISTILLER_CORE_TASK_TRACKER_H_
#define COMPONENTS_DOM_DISTILLER_CORE_TASK_TRACKER_H_
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "components/dom_distiller/core/article_distillation_update.h"
#include "components/dom_distiller/core/article_entry.h"
#include "components/dom_distiller/core/distiller.h"
#include "components/dom_distiller/core/proto/distilled_page.pb.h"
class GURL;
namespace dom_distiller {
class DistilledArticleProto;
class ViewerHandle {
public:
typedef base::Callback<void()> CancelCallback;
explicit ViewerHandle(CancelCallback callback);
~ViewerHandle();
private:
CancelCallback cancel_callback_;
DISALLOW_COPY_AND_ASSIGN(ViewerHandle);
};
class ViewRequestDelegate {
public:
virtual ~ViewRequestDelegate() {}
virtual void OnArticleReady(const DistilledArticleProto* article_proto) = 0;
virtual void OnArticleUpdated(ArticleDistillationUpdate article_update) = 0;
};
class TaskTracker {
public:
typedef base::Callback<void(TaskTracker*)> CancelCallback;
typedef base::Callback<
void(const ArticleEntry&, const DistilledArticleProto*, bool)>
SaveCallback;
TaskTracker(const ArticleEntry& entry, CancelCallback callback);
~TaskTracker();
void StartDistiller(DistillerFactory* factory);
void StartBlobFetcher();
void AddSaveCallback(const SaveCallback& callback);
void CancelSaveCallbacks();
scoped_ptr<ViewerHandle> AddViewer(ViewRequestDelegate* delegate);
const std::string& GetEntryId() const;
bool HasEntryId(const std::string& entry_id) const;
bool HasUrl(const GURL& url) const;
private:
void OnDistillerFinished(scoped_ptr<DistilledArticleProto> distilled_article);
void OnDistilledArticleReady(
scoped_ptr<DistilledArticleProto> distilled_article);
void OnArticleDistillationUpdated(
const ArticleDistillationUpdate& article_update);
void ScheduleSaveCallbacks(bool distillation_succeeded);
void DoSaveCallbacks(bool distillation_succeeded);
void RemoveViewer(ViewRequestDelegate* delegate);
void NotifyViewer(ViewRequestDelegate* delegate);
void MaybeCancel();
CancelCallback cancel_callback_;
std::vector<SaveCallback> save_callbacks_;
scoped_ptr<Distiller> distiller_;
std::vector<ViewRequestDelegate*> viewers_;
ArticleEntry entry_;
scoped_ptr<DistilledArticleProto> distilled_article_;
bool content_ready_;
bool destruction_allowed_;
base::WeakPtrFactory<TaskTracker> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(TaskTracker);
};
}
#endif