File: thumbnail_capture_driver.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 3,640 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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:
    // Asks the client to prepare for capture. The client should reply
    // by calling SetCanCapture(true) when ready.
    virtual void RequestCapture() = 0;

    // Begin capturing and updating the thumbnail immediately. This will
    // not be called unless SetCanCapture(true) was called. The client
    // should call GotFrame() whenever it gets a new frame from capture.
    virtual void StartCapture() = 0;

    // Stop capturing and cancel previous RequestCapture() call.
    virtual void StopCapture() = 0;

   protected:
    // Not deleted through interface pointer.
    ~Client() = default;
  };

  using PageReadiness = ThumbnailReadinessTracker::Readiness;

  explicit ThumbnailCaptureDriver(Client* client,
                                  ThumbnailScheduler* scheduler);
  ~ThumbnailCaptureDriver() override;

  // Update the capture state machine with new data.
  void UpdatePageReadiness(PageReadiness page_readiness);
  void UpdatePageVisibility(bool page_visible);
  void UpdateThumbnailVisibility(bool thumbnail_visible);

  // Can be called whenever. Will not issue a Client::StartCapture()
  // call if this is false. If set to false during capture, assumes the
  // capture stopped but the request is still outstanding. On the next
  // call with true, may immediately issue a Client::StartCapture()
  // call.
  void SetCanCapture(bool can_capture);

  // Notify scheduler a frame was received during capture.
  void GotFrame();

  // ThumbnailScheduler:
  void SetCapturePermittedByScheduler(bool scheduled) override;

  // Determines how long to wait for final capture, and how many times
  // to retry if one is not received. Exposed for testing.
  static constexpr base::TimeDelta kCooldownDelay = base::Milliseconds(500);
  static constexpr size_t kMaxCooldownRetries = 3;

 private:
  // How far along we are in the capture lifecycle for a given page.
  enum class CaptureState : int {
    // We have not started capturing the current page.
    kNoCapture = 0,
    // We have asked to capture but haven't started yet.
    kCaptureRequested,
    // We are currently capturing.
    kCapturing,
    // We are attempting to capture our last frame.
    kCooldown,
    // We have a good capture of the final page.
    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;

  // Has a frame been captured during cooldown?
  bool captured_cooldown_frame_ = false;
  size_t cooldown_retry_count_ = 0U;

  base::RetainingOneShotTimer cooldown_timer_;

  base::WeakPtrFactory<ThumbnailCaptureDriver> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_UI_THUMBNAILS_THUMBNAIL_CAPTURE_DRIVER_H_