File: paint_preview_client.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (241 lines) | stat: -rw-r--r-- 9,833 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAINT_PREVIEW_BROWSER_PAINT_PREVIEW_CLIENT_H_
#define COMPONENTS_PAINT_PREVIEW_BROWSER_PAINT_PREVIEW_CLIENT_H_

#include <stdint.h>

#include <memory>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "components/paint_preview/common/capture_result.h"
#include "components/paint_preview/common/mojom/paint_preview_recorder.mojom-shared.h"
#include "components/paint_preview/common/mojom/paint_preview_recorder.mojom.h"
#include "components/paint_preview/common/proto/paint_preview.pb.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "ui/gfx/geometry/rect.h"

namespace paint_preview {

// Client responsible for making requests to the mojom::PaintPreviewRecorder. A
// client coordinates between multiple frames and handles capture and
// aggreagation of data from both the main frame and subframes.
//
// Should be created and accessed from the UI thread as WebContentsUserData
// requires this behavior.
class PaintPreviewClient
    : public content::WebContentsUserData<PaintPreviewClient>,
      public content::WebContentsObserver {
 public:
  using PaintPreviewCallback =
      base::OnceCallback<void(base::UnguessableToken,
                              mojom::PaintPreviewStatus,
                              std::unique_ptr<CaptureResult>)>;

  // Augmented version of mojom::PaintPreviewServiceParams.
  struct PaintPreviewParams {
    explicit PaintPreviewParams(RecordingPersistence persistence);
    ~PaintPreviewParams();

    // Indicates where the PaintPreviewRecorder should store its intermediate
    // artifacts.
    RecordingPersistence persistence;

    // The root directory in which to store paint_previews. This should be
    // a subdirectory inside the active user profile's directory.
    base::FilePath root_dir;

    RecordingParams inner;
  };

  ~PaintPreviewClient() override;

  // IMPORTANT: The Capture* methods must be called on the UI thread!

  // Captures a paint preview corresponding to the content of
  // |render_frame_host|. This will work for capturing entire documents if
  // passed the main frame or for just a specific subframe depending on
  // |render_frame_host|. |callback| is invoked on completion.
  void CapturePaintPreview(const PaintPreviewParams& params,
                           content::RenderFrameHost* render_frame_host,
                           PaintPreviewCallback callback);

  // Captures a paint preview of the subframe corresponding to
  // |render_subframe_host|.
  void CaptureSubframePaintPreview(
      const base::UnguessableToken& guid,
      const gfx::Rect& rect,
      content::RenderFrameHost* render_subframe_host);

  // WebContentsObserver implementation ---------------------------------------

  void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;

 private:
  explicit PaintPreviewClient(content::WebContents* web_contents);
  friend class content::WebContentsUserData<PaintPreviewClient>;

  // Internal Storage Classes -------------------------------------------------

  // Ephemeral state for a document being captured. This will be accumulated to
  // as the capture progresses and results in a |CaptureResult|.
  struct InProgressDocumentCaptureState {
   public:
    InProgressDocumentCaptureState();
    ~InProgressDocumentCaptureState();

    RecordingPersistence persistence;

    // If |RecordingPersistence::kFileSystem|, the root directory to store
    // artifacts to. Ignored if |RecordingPersistence::kMemoryBuffer|.
    base::FilePath root_dir;

    // This corresponds to RenderFrameHost::EmbeddingToken.
    base::UnguessableToken root_frame_token;

    // From the first recording params. Whether to capture links and the
    // size limit per capture respectively.
    bool capture_links = true;
    size_t max_per_capture_size = 0;
    uint64_t max_decoded_image_size_bytes{std::numeric_limits<uint64_t>::max()};

    // UKM Source ID of the WebContent.
    ukm::SourceId source_id;

    // Main frame capture time.
    base::TimeDelta main_frame_blink_recording_time;

    // Callback that is invoked on completion of data.
    PaintPreviewCallback callback;

    // All the render frames that are still required.
    base::flat_set<base::UnguessableToken> awaiting_subframes;

    // All the render frames that have finished.
    base::flat_set<base::UnguessableToken> finished_subframes;

    // All the render frames that are allowed to be captured.
    base::flat_set<base::UnguessableToken> accepted_tokens;

    // If |RecordingPersistence::kMemoryBuffer|, this will contain the
    // successful recordings. Empty if |RecordingPersistence::FileSystem|
    base::flat_map<base::UnguessableToken, mojo_base::BigBuffer>
        serialized_skps;

    PaintPreviewProto proto;

    // Indicates that at least one subframe finished unsuccessfully.
    bool had_error = false;

    // Indicates that at least one subframe finished successfully.
    bool had_success = false;

    // Indicates if we should clean up files associated with awaiting frames on
    // destruction
    bool should_clean_up_files = false;

    // This flag will skip GPU accelerated content where applicable when
    // capturing. This reduces hangs, capture time and may also reduce OOM
    // crashes, but results in a lower fideltiy capture (i.e. the contents
    // captured may not accurately reflect the content visible to the user at
    // time of capture). See PaintPreviewBaseService::CaptureParams for a
    // description of the effects of this flag.
    bool skip_accelerated_content = false;

    // Generates a file path based off |root_dir| and |frame_guid|. Will be in
    // the form "{hexadecimal}.skp".
    base::FilePath FilePathForFrame(const base::UnguessableToken& frame_guid);

    // Record a successful recording into this capture state.
    void RecordSuccessfulFrame(const base::UnguessableToken& frame_guid,
                               bool is_main_frame,
                               mojom::PaintPreviewCaptureResponsePtr response);

    // Convert this capture state into a form that can be returned to the
    // original paint preview capture request.
    std::unique_ptr<CaptureResult> IntoCaptureResult() &&;

    InProgressDocumentCaptureState& operator=(
        InProgressDocumentCaptureState&& other) noexcept;
    InProgressDocumentCaptureState(
        InProgressDocumentCaptureState&& other) noexcept;

   private:
    InProgressDocumentCaptureState(const InProgressDocumentCaptureState&) =
        delete;
    InProgressDocumentCaptureState& operator=(
        const InProgressDocumentCaptureState&) = delete;
  };

  // Sets up for a capture of a frame on |render_frame_host| according to
  // |params|.
  void CapturePaintPreviewInternal(const RecordingParams& params,
                                   content::RenderFrameHost* render_frame_host);

  // Initiates capture via the PaintPreviewRecorder associated with
  // |render_frame_host| using |params| to configure the request. |frame_guid|
  // is the GUID associated with the frame. |path| is file path associated with
  // the File stored in |result| (base::File isn't aware of its file path).
  void RequestCaptureOnUIThread(
      const base::UnguessableToken& frame_guid,
      const RecordingParams& params,
      const content::GlobalRenderFrameHostId& render_frame_id,
      mojom::PaintPreviewStatus status,
      mojom::PaintPreviewCaptureParamsPtr capture_params);

  // Handles recording the frame and updating client state when capture is
  // complete.
  void OnPaintPreviewCapturedCallback(
      const base::UnguessableToken& frame_guid,
      const RecordingParams& params,
      const content::GlobalRenderFrameHostId& render_frame_id,
      mojom::PaintPreviewStatus status,
      mojom::PaintPreviewCaptureResponsePtr response);

  // Marks a frame as having been processed, this should occur regardless of
  // whether the processed frame is valid as there is no retry.
  void MarkFrameAsProcessed(base::UnguessableToken guid,
                            const base::UnguessableToken& frame_guid);

  // Handles finishing the capture once all frames are received.
  void OnFinished(base::UnguessableToken guid,
                  InProgressDocumentCaptureState* document_data);

  // Storage ------------------------------------------------------------------

  // Maps a RenderFrameHost and document to a remote interface.
  base::flat_map<base::UnguessableToken,
                 mojo::AssociatedRemote<mojom::PaintPreviewRecorder>>
      interface_ptrs_;

  // Maps render frame's GUID and document cookies that requested the frame.
  base::flat_map<base::UnguessableToken, base::flat_set<base::UnguessableToken>>
      pending_previews_on_subframe_;

  // Maps a document GUID to its capture state while it is in-progress. Entries
  // in this map should be cleaned up when a capture completes (either
  // successfully or not).
  base::flat_map<base::UnguessableToken, InProgressDocumentCaptureState>
      all_document_data_;

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

  WEB_CONTENTS_USER_DATA_KEY_DECL();

  PaintPreviewClient(const PaintPreviewClient&) = delete;
  PaintPreviewClient& operator=(const PaintPreviewClient&) = delete;
};

}  // namespace paint_preview

#endif  // COMPONENTS_PAINT_PREVIEW_BROWSER_PAINT_PREVIEW_CLIENT_H_