File: paint_preview_base_service.cc

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 (104 lines) | stat: -rw-r--r-- 4,243 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 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/paint_preview/browser/paint_preview_base_service.h"

#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "components/paint_preview/browser/compositor_utils.h"
#include "components/paint_preview/browser/paint_preview_client.h"
#include "components/paint_preview/common/mojom/paint_preview_recorder.mojom.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/geometry/rect.h"

namespace paint_preview {

PaintPreviewBaseService::PaintPreviewBaseService(
    std::unique_ptr<PaintPreviewFileMixin> file_mixin,
    std::unique_ptr<PaintPreviewPolicy> policy,
    bool is_off_the_record)
    : file_mixin_(std::move(file_mixin)),
      policy_(std::move(policy)),
      is_off_the_record_(is_off_the_record) {}

PaintPreviewBaseService::~PaintPreviewBaseService() = default;

void PaintPreviewBaseService::CapturePaintPreview(CaptureParams capture_params,
                                                  OnCapturedCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  content::WebContents* web_contents = capture_params.web_contents;
  content::RenderFrameHost* render_frame_host =
      capture_params.render_frame_host ? capture_params.render_frame_host.get()
                                       : web_contents->GetPrimaryMainFrame();
  if (policy_ && !policy_->SupportedForContents(web_contents)) {
    std::move(callback).Run(CaptureStatus::kContentUnsupported, {});
    return;
  }

  PaintPreviewClient::CreateForWebContents(web_contents);  // Is a singleton.
  auto* client = PaintPreviewClient::FromWebContents(web_contents);
  if (!client) {
    std::move(callback).Run(CaptureStatus::kClientCreationFailed, {});
    return;
  }

  PaintPreviewClient::PaintPreviewParams params(capture_params.persistence);
  if (capture_params.root_dir) {
    params.root_dir = *capture_params.root_dir;
  }
  params.inner.clip_rect = capture_params.clip_rect;
  params.inner.is_main_frame = render_frame_host->IsInPrimaryMainFrame();
  params.inner.capture_links = capture_params.capture_links;
  params.inner.max_capture_size = capture_params.max_per_capture_size;
  params.inner.max_decoded_image_size_bytes =
      capture_params.max_decoded_image_size_bytes;
  params.inner.skip_accelerated_content =
      capture_params.skip_accelerated_content;

  // TODO(crbug.com/40123632): Consider moving to client so that this always
  // happens. Although, it is harder to get this right in the client due to its
  // lifecycle.
  auto capture_handle = web_contents->IncrementCapturerCount(
      gfx::Size(), /*stay_hidden=*/true,
      /*stay_awake=*/true, /*is_activity=*/true);

  auto start_time = base::TimeTicks::Now();
  client->CapturePaintPreview(
      params, render_frame_host,
      base::BindOnce(&PaintPreviewBaseService::OnCaptured,
                     weak_ptr_factory_.GetWeakPtr(), std::move(capture_handle),
                     start_time, std::move(callback)));
}

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

  if (!(status == mojom::PaintPreviewStatus::kOk ||
        status == mojom::PaintPreviewStatus::kPartialSuccess) ||
      !result->capture_success) {
    DVLOG(1) << "ERROR: Paint Preview failed to capture for document "
             << guid.ToString() << " with error " << status;
    std::move(callback).Run(CaptureStatus::kCaptureFailed, {});
    return;
  }
  base::UmaHistogramTimes("Browser.PaintPreview.Capture.TotalCaptureDuration",
                          base::TimeTicks::Now() - start_time);
  std::move(callback).Run(CaptureStatus::kOk, std::move(result));
}

}  // namespace paint_preview