File: test_print_backend.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 (295 lines) | stat: -rw-r--r-- 9,385 bytes parent folder | download | duplicates (6)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/backend/test_print_backend.h"

#include <memory>
#include <string>
#include <utility>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/location.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "printing/backend/print_backend.h"
#include "printing/mojom/print.mojom.h"

#if BUILDFLAG(IS_WIN)
#include "base/strings/string_number_conversions.h"
#include "base/types/expected.h"
#endif  // BUILDFLAG(IS_WIN)

namespace printing {

namespace {

#if BUILDFLAG(IS_WIN)
// Default XML with feature not of interest.
constexpr char kXmlDefaultCapabilities[] =
    R"(<?xml version="1.0" encoding="UTF-8"?>
    <psf:PrintCapabilities>
      <!-- Need at least one psf:Feature for
      ParseValueForXpsPrinterCapabilities() to consider it valid XML -->
      <psf:Feature name="TestFeature">
      </psf:Feature>
    </psf:PrintCapabilities>)";
#endif  // BUILDFLAG(IS_WIN)

mojom::ResultCode ReportErrorAccessDenied(const base::Location& from_here) {
  DLOG(ERROR) << from_here.ToString() << " failed, access denied";
  return mojom::ResultCode::kAccessDenied;
}

mojom::ResultCode ReportErrorNoData(const base::Location& from_here) {
  DLOG(ERROR) << from_here.ToString() << " failed, no data";
  return mojom::ResultCode::kFailed;
}

mojom::ResultCode ReportErrorNoDevice(const base::Location& from_here) {
  DLOG(ERROR) << from_here.ToString() << " failed, no such device";
  return mojom::ResultCode::kFailed;
}

#if BUILDFLAG(IS_WIN)
mojom::ResultCode ReportErrorNotImplemented(const base::Location& from_here) {
  DLOG(ERROR) << from_here.ToString() << " failed, method not implemented";
  return mojom::ResultCode::kFailed;
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace

TestPrintBackend::TestPrintBackend() = default;

TestPrintBackend::~TestPrintBackend() = default;

mojom::ResultCode TestPrintBackend::EnumeratePrinters(
    PrinterList& printer_list) {
  DCHECK(printer_list.empty());
  if (printer_map_.empty())
    return mojom::ResultCode::kSuccess;

  for (const auto& entry : printer_map_) {
    const std::unique_ptr<PrinterData>& data = entry.second;

    // Can only return basic info for printers which have registered info.
    if (data->info)
      printer_list.emplace_back(*data->info);
  }
  return mojom::ResultCode::kSuccess;
}

mojom::ResultCode TestPrintBackend::GetDefaultPrinterName(
    std::string& default_printer) {
  default_printer = default_printer_name_;
  return mojom::ResultCode::kSuccess;
}

mojom::ResultCode TestPrintBackend::GetPrinterBasicInfo(
    const std::string& printer_name,
    PrinterBasicInfo* printer_info) {
  auto found = printer_map_.find(printer_name);
  if (found == printer_map_.end()) {
    // Matching entry not found.
    return ReportErrorNoDevice(FROM_HERE);
  }

  const std::unique_ptr<PrinterData>& data = found->second;
  if (data->blocked_by_permissions)
    return ReportErrorAccessDenied(FROM_HERE);

  // Basic info might not have been provided.
  if (!data->info)
    return ReportErrorNoData(FROM_HERE);

  *printer_info = *data->info;
  return mojom::ResultCode::kSuccess;
}

mojom::ResultCode TestPrintBackend::GetPrinterSemanticCapsAndDefaults(
    const std::string& printer_name,
    PrinterSemanticCapsAndDefaults* printer_caps) {
  auto found = printer_map_.find(printer_name);
  if (found == printer_map_.end())
    return ReportErrorNoDevice(FROM_HERE);

  const std::unique_ptr<PrinterData>& data = found->second;
  if (data->blocked_by_permissions)
    return ReportErrorAccessDenied(FROM_HERE);

  // Capabilities might not have been provided.
  if (!data->caps)
    return ReportErrorNoData(FROM_HERE);

  *printer_caps = *data->caps;
#if BUILDFLAG(IS_WIN)
  // The Windows implementation does not load the printable area for all
  // paper sizes, only for the default size.  Mimic this behavior by
  // defaulting the printable area to the physical size for all other paper
  // sizes.
  for (auto& paper : printer_caps->papers) {
    if (paper != printer_caps->default_paper) {
      paper.set_printable_area_to_paper_size();
    }
  }
#endif
  return mojom::ResultCode::kSuccess;
}

#if BUILDFLAG(IS_WIN)
mojom::ResultCode TestPrintBackend::GetPrinterCapsAndDefaults(
    const std::string& printer_name,
    PrinterCapsAndDefaults* printer_caps) {
  return ReportErrorNotImplemented(FROM_HERE);
}

std::optional<gfx::Rect> TestPrintBackend::GetPaperPrintableArea(
    const std::string& printer_name,
    const std::string& paper_vendor_id,
    const gfx::Size& paper_size_um) {
  auto found = printer_map_.find(printer_name);
  if (found == printer_map_.end()) {
    return std::nullopt;
  }

  const std::unique_ptr<PrinterData>& data = found->second;
  if (data->blocked_by_permissions) {
    return std::nullopt;
  }

  // Capabilities might not have been provided.
  if (!data->caps) {
    return std::nullopt;
  }

  // Windows uses non-zero IDs to represent specific standard paper sizes.
  unsigned id;
  if (base::StringToUint(paper_vendor_id, &id) && id) {
    PrinterSemanticCapsAndDefaults::Papers& papers = data->caps->papers;
    for (auto paper = papers.begin(); paper != papers.end(); ++paper) {
      if (paper->vendor_id() == paper_vendor_id) {
        return paper->printable_area_um();
      }
    }

    // No match for the specified paper identification.
    return std::nullopt;
  }

  // Custom paper size.  For testing just treat as match to paper size.
  return gfx::Rect(paper_size_um);
}
#endif  // BUILDFLAG(IS_WIN)

std::vector<std::string> TestPrintBackend::GetPrinterDriverInfo(
    const std::string& printer_name) {
  // not implemented
  return std::vector<std::string>();
}

bool TestPrintBackend::IsValidPrinter(const std::string& printer_name) {
  return base::Contains(printer_map_, printer_name);
}

#if BUILDFLAG(IS_WIN)
base::expected<std::string, mojom::ResultCode>
TestPrintBackend::GetXmlPrinterCapabilitiesForXpsDriver(
    const std::string& printer_name) {
  auto found = printer_map_.find(printer_name);
  if (found == printer_map_.end())
    return base::unexpected(ReportErrorNoDevice(FROM_HERE));

  const PrinterData* data = found->second.get();
  if (data->blocked_by_permissions)
    return base::unexpected(ReportErrorAccessDenied(FROM_HERE));

  // XML capabilities might not have been provided.
  if (data->capabilities_xml.empty())
    return base::unexpected(ReportErrorNoData(FROM_HERE));

  return data->capabilities_xml;
}
#endif  // BUILDFLAG(IS_WIN)

void TestPrintBackend::SetDefaultPrinterName(const std::string& printer_name) {
  if (default_printer_name_ == printer_name)
    return;

  auto found = printer_map_.find(printer_name);
  if (found == printer_map_.end()) {
    DLOG(ERROR) << "Unable to set an unknown printer as the default.  Unknown "
                << "printer name: " << printer_name;
    return;
  }

  default_printer_name_ = printer_name;
}

void TestPrintBackend::AddValidPrinter(
    const std::string& printer_name,
    std::unique_ptr<PrinterSemanticCapsAndDefaults> caps,
    std::unique_ptr<PrinterBasicInfo> info) {
  AddPrinter(printer_name, std::move(caps), std::move(info),
             /*blocked_by_permissions=*/false);
#if BUILDFLAG(IS_WIN)
  SetXmlCapabilitiesForPrinter(printer_name, kXmlDefaultCapabilities);
#endif  // BUILDFLAG(IS_WIN)
}

void TestPrintBackend::AddInvalidDataPrinter(const std::string& printer_name) {
  // The blank fields in default `PrinterBasicInfo` cause Mojom data validation
  // errors.
  AddPrinter(printer_name, std::make_unique<PrinterSemanticCapsAndDefaults>(),
             std::make_unique<PrinterBasicInfo>(),
             /*blocked_by_permissions=*/false);
}

void TestPrintBackend::AddAccessDeniedPrinter(const std::string& printer_name) {
  AddPrinter(printer_name, /*caps=*/nullptr, /*info=*/nullptr,
             /*blocked_by_permissions=*/true);
}

#if BUILDFLAG(IS_WIN)
void TestPrintBackend::SetXmlCapabilitiesForPrinter(
    const std::string& printer_name,
    const std::string& capabilities_xml) {
  auto found = printer_map_.find(printer_name);
  if (found == printer_map_.end()) {
    DLOG(ERROR) << "Unable to find printer.  Unknown printer name: "
                << printer_name;
    return;
  }
  found->second->capabilities_xml = capabilities_xml;
}
#endif  // BUILDFLAG(IS_WIN)

void TestPrintBackend::AddPrinter(
    const std::string& printer_name,
    std::unique_ptr<PrinterSemanticCapsAndDefaults> caps,
    std::unique_ptr<PrinterBasicInfo> info,
    bool blocked_by_permissions) {
  DCHECK(!printer_name.empty());

  // Ensure that default settings are honored if this prior default should no
  // longer be so.
  if (default_printer_name_ == printer_name && !info) {
    default_printer_name_.clear();
  }

  printer_map_[printer_name] = std::make_unique<PrinterData>(
      std::move(caps), std::move(info), blocked_by_permissions);
}

TestPrintBackend::PrinterData::PrinterData(
    std::unique_ptr<PrinterSemanticCapsAndDefaults> caps,
    std::unique_ptr<PrinterBasicInfo> info,
    bool blocked_by_permissions)
    : caps(std::move(caps)),
      info(std::move(info)),
      blocked_by_permissions(blocked_by_permissions) {}

TestPrintBackend::PrinterData::~PrinterData() = default;

}  // namespace printing