File: paint_preview_base_service.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 7,043 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
// 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_BASE_SERVICE_H_
#define COMPONENTS_PAINT_PREVIEW_BROWSER_PAINT_PREVIEW_BASE_SERVICE_H_

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "components/paint_preview/browser/paint_preview_file_mixin.h"
#include "components/paint_preview/browser/paint_preview_policy.h"
#include "components/paint_preview/common/capture_result.h"
#include "components/paint_preview/common/file_utils.h"
#include "components/paint_preview/common/mojom/paint_preview_recorder.mojom.h"
#include "components/paint_preview/common/proto/paint_preview.pb.h"
#include "components/paint_preview/common/serialized_recording.h"
#include "content/public/browser/web_contents.h"

namespace paint_preview {

// A base class that serves as the Public API for Paint Previews.
// Features that want to use Paint Previews should extend this class.
// This service supports both in-memery and in-file captures.
//
// The KeyedService provides a 1:1 mapping between the service and a key or
// profile allowing each feature built on Paint Previews to reliably store
// necessary data to the right directory on disk.
//
// [NOTE] for file system captures:
// - PaintPreviewFileMixin object needs to be supplied.
// - Implementations of the service should be created by implementing a factory
//   that extends one of:
//   - BrowserContextKeyedServiceFactory
//   OR preferably the
//   - SimpleKeyedServiceFactory
class PaintPreviewBaseService : public KeyedService {
 public:
  enum class CaptureStatus : int {
    kOk = 0,
    kContentUnsupported,
    kClientCreationFailed,
    kCaptureFailed,
  };

  struct CaptureParams {
    raw_ptr<content::WebContents> web_contents = nullptr;

    // In case of specifying, an individual |render_frame_host| and its
    // descendents will be captured. In case of nullptr, full page contents will
    // be captured.
    //
    // Generally, leaving this as nullptr is what you should be doing for most
    // features. Specifying a |render_frame_host| is intended for capturing
    // individual subframes and should be used for only a few use cases.
    raw_ptr<content::RenderFrameHost> render_frame_host = nullptr;

    // Store artifacts in the file system or in memory buffers.
    RecordingPersistence persistence;

    // |root_dir| should be created using
    // GetFileManager()->CreateOrGetDirectoryFor(). However, to provide
    // flexibility in managing the lifetime of created objects and ease cleanup
    // if a capture fails the service implementation is responsible for
    // implementing this management and tracking the directories in existence.
    // Data in a directory will contain:
    // - a number of SKPs listed as <guid>.skp (one per frame)
    //
    // Will be ignored if persistence = kMemoryBuffer
    raw_ptr<const base::FilePath> root_dir = nullptr;

    // The captured area is clipped to |clip_rect| if it is non-zero.
    gfx::Rect clip_rect;

    // Whether to record links.
    bool capture_links;

    // Cap the perframe SkPicture size to |max_per_capture_size| if non-zero.
    size_t max_per_capture_size{0};

    // Limit on the maximum size of a decoded image that can be serialized.
    // Any images with a decoded size exceeding this value will be discarded.
    // This can be used to reduce the chance of an OOM during serialization and
    // later during playback.
    uint64_t max_decoded_image_size_bytes{std::numeric_limits<uint64_t>::max()};

    // 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).
    //
    // At present this flag:
    // - Shows a poster or blank space instead of live video frames.
    bool skip_accelerated_content{false};
  };

  using OnCapturedCallback =
      base::OnceCallback<void(CaptureStatus, std::unique_ptr<CaptureResult>)>;

  // Creates a service instance for a feature. Artifacts produced will live in
  // |profile_dir|/paint_preview/|ascii_feature_name|. Implementers of the
  // factory can also elect their factory to not construct services in the event
  // a profile |is_off_the_record|. The |policy| object is responsible for
  // determining whether or not a given WebContents is amenable to paint
  // preview. If nullptr is passed as |policy| all content is deemed amenable.
  //
  // NOTE: Pass nullptr as |file_mixin| if you're planning to use the service
  // for only in-memory captures.
  PaintPreviewBaseService(std::unique_ptr<PaintPreviewFileMixin> file_mixin,
                          std::unique_ptr<PaintPreviewPolicy> policy,
                          bool is_off_the_record);
  ~PaintPreviewBaseService() override;

  PaintPreviewFileMixin* GetFileMixin() { return file_mixin_.get(); }

  // Returns whether the created service is off the record.
  bool IsOffTheRecord() const { return is_off_the_record_; }

  // Captures need to run on the Browser UI thread! Captures may involve child
  // frames so the PaintPreviewClient (WebContentsObserver) must be stored as
  // WebContentsUserData which is not thread safe and must only be accessible
  // from a specific sequence i.e. the UI thread.
  //
  // Captures the main frame of |capture_params.web_contents| (an observer for
  // capturing Paint Previews is created for web contents if it does not exist).
  // The capture is attributed to the URL of the main frame. On completion the
  // status of the capture is provided via |callback|.
  //
  // See |PaintPreviewBaseService::CaptureParams| for more info about the
  // capture parameters.
  void CapturePaintPreview(CaptureParams capture_params,
                           OnCapturedCallback callback);

 private:
  void OnCaptured(base::ScopedClosureRunner capture_handle,
                  base::TimeTicks start_time,
                  OnCapturedCallback callback,
                  base::UnguessableToken guid,
                  mojom::PaintPreviewStatus status,
                  std::unique_ptr<CaptureResult> result);

  std::unique_ptr<PaintPreviewFileMixin> file_mixin_;
  std::unique_ptr<PaintPreviewPolicy> policy_;
  bool is_off_the_record_;

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

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

}  // namespace paint_preview

#endif  // COMPONENTS_PAINT_PREVIEW_BROWSER_PAINT_PREVIEW_BASE_SERVICE_H_