File: ax_tree_fixing_screenshotter.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 (108 lines) | stat: -rw-r--r-- 4,981 bytes parent folder | download | duplicates (5)
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
105
106
107
108
// Copyright 2025 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_ACCESSIBILITY_TREE_FIXING_INTERNAL_AX_TREE_FIXING_SCREENSHOTTER_H_
#define CHROME_BROWSER_ACCESSIBILITY_TREE_FIXING_INTERNAL_AX_TREE_FIXING_SCREENSHOTTER_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/paint_preview/browser/paint_preview_base_service.h"
#include "components/paint_preview/public/paint_preview_compositor_service.h"
#include "content/public/browser/web_contents.h"

// Used to capture a full-page screenshot of a WebContents.
class AXTreeFixingScreenshotter
    : public paint_preview::PaintPreviewBaseService {
 public:
  // Delegate for clients that want to screenshot WebContents.
  class ScreenshotDelegate {
   protected:
    ScreenshotDelegate() = default;

   public:
    ScreenshotDelegate(const ScreenshotDelegate&) = delete;
    ScreenshotDelegate& operator=(const ScreenshotDelegate&) = delete;
    virtual ~ScreenshotDelegate() = default;

    // This method is used to return the captured screenshot to the delegate
    // (owner) of this instance. When calling RequestScreenshot, the client must
    // provide a request_id, and this ID is passed back to the client. The
    // request_id allows clients to make multiple requests in parallel and
    // uniquely identify each response. It is the responsibility of the client
    // to handle the logic behind a request_id, this service simply passes the
    // id through.
    virtual void OnScreenshotCaptured(const SkBitmap& bitmap,
                                      int request_id) = 0;
  };

  explicit AXTreeFixingScreenshotter(ScreenshotDelegate& delegate);
  AXTreeFixingScreenshotter(const AXTreeFixingScreenshotter&) = delete;
  AXTreeFixingScreenshotter& operator=(const AXTreeFixingScreenshotter&) =
      delete;
  ~AXTreeFixingScreenshotter() override;

  // Initiates the process of capturing a paint preview screenshot for the
  // given |web_contents|. On completion, OnPaintPreviewCaptured() will be
  // invoked. Does nothing if |web_contents| is null.
  // The client should provide a request_id, which is returned to the client
  // along with the resulting bitmap via a call to OnScreenshotCaptured.
  void RequestScreenshot(const raw_ptr<content::WebContents> web_contents,
                         int request_id);

 private:
  // Called when the connection to the Paint Preview Compositor Service is lost.
  // Resets the compositor client and service pointers to ensure they are not
  // used in a disconnected state.
  void OnCompositorServiceDisconnected();

  // Callback invoked when the capture initiated by RequestScreenshot()
  // completes. If the capture was successful, it proceeds to the compositing
  // stage by calling SendCompositeRequest(). If this is the first successful
  // capture, it will also establish the connection to the compositor service
  // via CreateCompositor.
  void OnPaintPreviewCaptured(
      int request_id,
      paint_preview::PaintPreviewBaseService::CaptureStatus status,
      std::unique_ptr<paint_preview::CaptureResult> result);

  // Sends the captured paint preview data (|result|) to the compositor service
  // to begin compositing into a bitmap.
  // OnCompositeFinished() will be called upon completion of the compositing.
  void SendCompositeRequest(
      int request_id,
      std::unique_ptr<paint_preview::CaptureResult> result);

  // Callback invoked when the compositor service finishes the request initiated
  // by SendCompositeRequest(). If compositing was successful (or partially
  // successful), it requests the final bitmap of the main frame.
  void OnCompositeFinished(
      int request_id,
      paint_preview::mojom::PaintPreviewCompositor::BeginCompositeStatus status,
      paint_preview::mojom::PaintPreviewBeginCompositeResponsePtr response);

  // Callback invoked when the compositor service provides the requested bitmap.
  // This is the final step in the screenshot pipeline. This returns the
  // resulting bitmap to the owner of this instance via the provided delegate.
  void OnBitmapReceived(
      int request_id,
      paint_preview::mojom::PaintPreviewCompositor::BitmapStatus status,
      const SkBitmap& bitmap);

  // Delegate provided by client to receive screenshot.
  // Use a raw_ref since we do not own the delegate or control its lifecycle.
  const raw_ref<ScreenshotDelegate> screenshot_delegate_;

  std::unique_ptr<paint_preview::PaintPreviewCompositorService,
                  base::OnTaskRunnerDeleter>
      compositor_service_;
  std::unique_ptr<paint_preview::PaintPreviewCompositorClient,
                  base::OnTaskRunnerDeleter>
      compositor_client_;

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

#endif  // CHROME_BROWSER_ACCESSIBILITY_TREE_FIXING_INTERNAL_AX_TREE_FIXING_SCREENSHOTTER_H_