File: test_print_view_manager.cc

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 (168 lines) | stat: -rw-r--r-- 5,340 bytes parent folder | download | duplicates (9)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/printing/test_print_view_manager.h"

#include <functional>
#include <memory>
#include <utility>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/printing/print_job.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/print_view_manager_common.h"
#include "chrome/browser/printing/printer_query.h"
#include "components/printing/browser/print_manager_utils.h"
#include "components/printing/common/print.mojom.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_settings.h"

#if BUILDFLAG(IS_WIN)
#include "printing/metafile.h"
#include "printing/print_job_constants.h"
#endif

namespace printing {

namespace {

void OnDidUpdatePrintSettings(
    mojom::PrintPagesParamsPtr& snooped_params,
    mojom::PrintManagerHost::UpdatePrintSettingsCallback callback,
    mojom::PrintPagesParamsPtr settings) {
  snooped_params = mojom::PrintPagesParams::New();
  auto params = mojom::PrintParams::New();

  // Copy over any relevant fields that we want to snoop.
  if (settings) {
    params->dpi = settings->params->dpi;
    params->page_size = settings->params->page_size;
    params->content_size = settings->params->content_size;
    params->printable_area = settings->params->printable_area;
  }
  snooped_params->params = std::move(params);

  std::move(callback).Run(std::move(settings));
}

class TestPrintJob : public PrintJob {
 public:
  explicit TestPrintJob(PrintJobManager* print_job_manager)
      : PrintJob(print_job_manager) {}

#if BUILDFLAG(IS_WIN)
  void set_simulate_pdf_conversion_error_on_page_index(uint32_t page_index) {
    simulate_pdf_conversion_error_on_page_index_ = page_index;
  }
#endif

 private:
  ~TestPrintJob() override = default;

#if BUILDFLAG(IS_WIN)
  // `PrintJob` overrides:
  void OnPdfPageConverted(uint32_t page_index,
                          float scale_factor,
                          std::unique_ptr<MetafilePlayer> metafile) override {
    if (simulate_pdf_conversion_error_on_page_index_.has_value() &&
        page_index == *simulate_pdf_conversion_error_on_page_index_) {
      // Override the page index to simulate an error.
      page_index = kInvalidPageIndex;
    }
    PrintJob::OnPdfPageConverted(page_index, scale_factor, std::move(metafile));
  }

  std::optional<uint32_t> simulate_pdf_conversion_error_on_page_index_;
#endif  // BUILDFLAG(IS_WIN)
};

}  // namespace

TestPrintViewManager::TestPrintViewManager(content::WebContents* web_contents)
    : PrintViewManager(web_contents) {}

TestPrintViewManager::TestPrintViewManager(content::WebContents* web_contents,
                                           OnDidCreatePrintJobCallback callback)
    : PrintViewManager(web_contents),
      on_did_create_print_job_(std::move(callback)) {}

TestPrintViewManager::~TestPrintViewManager() = default;

bool TestPrintViewManager::StartPrinting(content::WebContents* contents) {
  auto* print_view_manager = TestPrintViewManager::FromWebContents(contents);
  if (!print_view_manager) {
    return false;
  }

  content::RenderFrameHost* rfh_to_use = GetFrameToPrint(contents);
  if (!rfh_to_use) {
    return false;
  }

  return print_view_manager->PrintNow(rfh_to_use);
}

void TestPrintViewManager::WaitUntilPreviewIsShownOrCancelled() {
  base::RunLoop run_loop;
  quit_closure_ = run_loop.QuitClosure();
  run_loop.Run();
}

// static
TestPrintViewManager* TestPrintViewManager::CreateForWebContents(
    content::WebContents* web_contents) {
  auto manager = std::make_unique<TestPrintViewManager>(web_contents);
  auto* manager_ptr = manager.get();
  web_contents->SetUserData(PrintViewManager::UserDataKey(),
                            std::move(manager));
  return manager_ptr;
}

bool TestPrintViewManager::PrintNow(content::RenderFrameHost* rfh) {
  print_now_result_ = PrintViewManager::PrintNow(rfh);
  return *print_now_result_;
}

scoped_refptr<PrintJob> TestPrintViewManager::CreatePrintJob(
    PrintJobManager* print_job_manager) {
  auto print_job = base::MakeRefCounted<TestPrintJob>(print_job_manager);
#if BUILDFLAG(IS_WIN)
  if (simulate_pdf_conversion_error_on_page_index_.has_value()) {
    print_job->set_simulate_pdf_conversion_error_on_page_index(
        *simulate_pdf_conversion_error_on_page_index_);
  }
#endif
  if (on_did_create_print_job_) {
    on_did_create_print_job_.Run(print_job.get());
  }
  return print_job;
}

void TestPrintViewManager::PrintPreviewRejectedForTesting() {
  if (quit_closure_) {
    std::move(quit_closure_).Run();
  }
}

void TestPrintViewManager::PrintPreviewAllowedForTesting() {
  if (quit_closure_) {
    std::move(quit_closure_).Run();
  }
}

void TestPrintViewManager::UpdatePrintSettings(
    base::Value::Dict job_settings,
    UpdatePrintSettingsCallback callback) {
  PrintViewManagerBase::UpdatePrintSettings(
      std::move(job_settings),
      base::BindOnce(&OnDidUpdatePrintSettings, std::ref(snooped_params_),
                     std::move(callback)));
}

}  // namespace printing