#ifndef CHROME_BROWSER_UI_THUMBNAILS_THUMBNAIL_CAPTURE_DRIVER_H_
#define CHROME_BROWSER_UI_THUMBNAILS_THUMBNAIL_CAPTURE_DRIVER_H_
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "chrome/browser/ui/thumbnails/thumbnail_readiness_tracker.h"
#include "chrome/browser/ui/thumbnails/thumbnail_scheduler.h"
class ThumbnailCaptureDriver : public ThumbnailScheduler::TabCapturer {
public:
class Client {
public:
virtual void RequestCapture() = 0;
virtual void StartCapture() = 0;
virtual void StopCapture() = 0;
protected:
~Client() = default;
};
using PageReadiness = ThumbnailReadinessTracker::Readiness;
explicit ThumbnailCaptureDriver(Client* client,
ThumbnailScheduler* scheduler);
~ThumbnailCaptureDriver() override;
void UpdatePageReadiness(PageReadiness page_readiness);
void UpdatePageVisibility(bool page_visible);
void UpdateThumbnailVisibility(bool thumbnail_visible);
void SetCanCapture(bool can_capture);
void GotFrame();
void SetCapturePermittedByScheduler(bool scheduled) override;
static constexpr base::TimeDelta kCooldownDelay = base::Milliseconds(500);
static constexpr size_t kMaxCooldownRetries = 3;
private:
enum class CaptureState : int {
kNoCapture = 0,
kCaptureRequested,
kCapturing,
kCooldown,
kHaveFinalCapture,
};
void UpdateSchedulingPriority();
void UpdateCaptureState();
void StartCooldown();
void OnCooldownEnded();
const raw_ptr<Client> client_;
const raw_ptr<ThumbnailScheduler> scheduler_;
PageReadiness page_readiness_ = PageReadiness::kNotReady;
bool page_visible_ = false;
bool thumbnail_visible_ = false;
bool can_capture_ = false;
bool scheduled_ = false;
CaptureState capture_state_ = CaptureState::kNoCapture;
bool captured_cooldown_frame_ = false;
size_t cooldown_retry_count_ = 0U;
base::RetainingOneShotTimer cooldown_timer_;
base::WeakPtrFactory<ThumbnailCaptureDriver> weak_ptr_factory_{this};
};
#endif