File: 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 (340 lines) | stat: -rw-r--r-- 12,204 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2012 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/print_backend.h"

#include <optional>
#include <string>
#include <utility>

#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"

namespace {

// PrintBackend override for testing.
printing::PrintBackend* g_print_backend_for_test = nullptr;

}  // namespace

namespace printing {

PrinterBasicInfo::PrinterBasicInfo() = default;

PrinterBasicInfo::PrinterBasicInfo(const std::string& printer_name,
                                   const std::string& display_name,
                                   const std::string& printer_description,
                                   const PrinterBasicInfoOptions& options)
    : printer_name(printer_name),
      display_name(display_name),
      printer_description(printer_description),
      options(options) {}

PrinterBasicInfo::PrinterBasicInfo(const PrinterBasicInfo& other) = default;

PrinterBasicInfo::~PrinterBasicInfo() = default;

bool PrinterBasicInfo::operator==(const PrinterBasicInfo& other) const {
  return printer_name == other.printer_name &&
         display_name == other.display_name &&
         printer_description == other.printer_description &&
         options == other.options;
}

#if BUILDFLAG(IS_CHROMEOS)

AdvancedCapabilityValue::AdvancedCapabilityValue() = default;

AdvancedCapabilityValue::AdvancedCapabilityValue(
    const std::string& name,
    const std::string& display_name)
    : name(name), display_name(display_name) {}

AdvancedCapabilityValue::AdvancedCapabilityValue(
    const AdvancedCapabilityValue& other) = default;

AdvancedCapabilityValue::~AdvancedCapabilityValue() = default;

bool AdvancedCapabilityValue::operator==(
    const AdvancedCapabilityValue& other) const {
  return name == other.name && display_name == other.display_name;
}

AdvancedCapability::AdvancedCapability() = default;

AdvancedCapability::AdvancedCapability(const std::string& name,
                                       AdvancedCapability::Type type)
    : name(name), type(type) {}

AdvancedCapability::AdvancedCapability(
    const std::string& name,
    const std::string& display_name,
    AdvancedCapability::Type type,
    const std::string& default_value,
    const std::vector<AdvancedCapabilityValue>& values)
    : name(name),
      display_name(display_name),
      type(type),
      default_value(default_value),
      values(values) {}

AdvancedCapability::AdvancedCapability(const AdvancedCapability& other) =
    default;

AdvancedCapability::~AdvancedCapability() = default;

bool AdvancedCapability::operator==(const AdvancedCapability& other) const {
  return name == other.name && display_name == other.display_name &&
         type == other.type && default_value == other.default_value &&
         values == other.values;
}

PaperMargins::PaperMargins()
    : top_margin_um(0),
      right_margin_um(0),
      bottom_margin_um(0),
      left_margin_um(0) {}

PaperMargins::PaperMargins(int32_t top_margin_um,
                           int32_t right_margin_um,
                           int32_t bottom_margin_um,
                           int32_t left_margin_um)
    : top_margin_um(top_margin_um),
      right_margin_um(right_margin_um),
      bottom_margin_um(bottom_margin_um),
      left_margin_um(left_margin_um) {}

PaperMargins::~PaperMargins() = default;

PaperMargins::PaperMargins(const PaperMargins& other) = default;

PaperMargins& PaperMargins::operator=(const PaperMargins& other) = default;

bool PaperMargins::operator==(const PaperMargins& other) const {
  return top_margin_um == other.top_margin_um &&
         right_margin_um == other.right_margin_um &&
         bottom_margin_um == other.bottom_margin_um &&
         left_margin_um == other.left_margin_um;
}

#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_WIN)

PageOutputQualityAttribute::PageOutputQualityAttribute() = default;

PageOutputQualityAttribute::PageOutputQualityAttribute(
    const std::string& display_name,
    const std::string& name)
    : display_name(display_name), name(name) {}

PageOutputQualityAttribute::~PageOutputQualityAttribute() = default;

bool PageOutputQualityAttribute::operator==(
    const PageOutputQualityAttribute& other) const {
  return display_name == other.display_name && name == other.name;
}

bool PageOutputQualityAttribute::operator<(
    const ::printing::PageOutputQualityAttribute& other) const {
  return std::tie(name, display_name) <
         std::tie(other.name, other.display_name);
}

PageOutputQuality::PageOutputQuality() = default;

PageOutputQuality::PageOutputQuality(PageOutputQualityAttributes qualities,
                                     std::optional<std::string> default_quality)
    : qualities(std::move(qualities)),
      default_quality(std::move(default_quality)) {}

PageOutputQuality::PageOutputQuality(const PageOutputQuality& other) = default;

PageOutputQuality::~PageOutputQuality() = default;

// This function is only supposed to be used in tests. The declaration in the
// header file is guarded by "#if defined(UNIT_TEST)" so that they can be used
// by tests but not non-test code. However, this .cc file is compiled as part of
// "backend" where "UNIT_TEST" is not defined. So we need to specify
// "COMPONENT_EXPORT(PRINT_BACKEND)" here again so that they are visible to
// tests.
COMPONENT_EXPORT(PRINT_BACKEND)
bool operator==(const PageOutputQuality& quality1,
                const PageOutputQuality& quality2) {
  return quality1.qualities == quality2.qualities &&
         quality1.default_quality == quality2.default_quality;
}

XpsCapabilities::XpsCapabilities() = default;

XpsCapabilities::XpsCapabilities(XpsCapabilities&& other) noexcept = default;

XpsCapabilities& XpsCapabilities::operator=(XpsCapabilities&& other) noexcept =
    default;

XpsCapabilities::~XpsCapabilities() = default;

#endif  // BUILDFLAG(IS_WIN)

PrinterSemanticCapsAndDefaults::Paper::Paper() = default;

PrinterSemanticCapsAndDefaults::Paper::Paper(const std::string& display_name,
                                             const std::string& vendor_id,
                                             const gfx::Size& size_um)
    : Paper(display_name, vendor_id, size_um, gfx::Rect(size_um)) {}

PrinterSemanticCapsAndDefaults::Paper::Paper(const std::string& display_name,
                                             const std::string& vendor_id,
                                             const gfx::Size& size_um,
                                             const gfx::Rect& printable_area_um)
    : Paper(display_name,
            vendor_id,
            size_um,
            printable_area_um,
            /*max_height_um=*/0,
            /*has_borderless_variant=*/false) {}

PrinterSemanticCapsAndDefaults::Paper::Paper(const std::string& display_name,
                                             const std::string& vendor_id,
                                             const gfx::Size& size_um,
                                             const gfx::Rect& printable_area_um,
                                             int max_height_um)
    : Paper(display_name,
            vendor_id,
            size_um,
            printable_area_um,
            max_height_um,
            /*has_borderless_variant=*/false) {}

PrinterSemanticCapsAndDefaults::Paper::Paper(
    const std::string& display_name,
    const std::string& vendor_id,
    const gfx::Size& size_um,
    const gfx::Rect& printable_area_um,
    int max_height_um,
    bool has_borderless_variant
#if BUILDFLAG(IS_CHROMEOS)
    ,
    std::optional<PaperMargins> supported_margins_um
#endif  // BUILDFLAG(IS_CHROMEOS)
    )
    : display_name_(display_name),
      vendor_id_(vendor_id),
      size_um_(size_um),
      printable_area_um_(printable_area_um),
      max_height_um_(max_height_um),
      has_borderless_variant_(has_borderless_variant)
#if BUILDFLAG(IS_CHROMEOS)
      ,
      supported_margins_um_(supported_margins_um)
#endif  // BUILDFLAG(IS_CHROMEOS)
    {}

PrinterSemanticCapsAndDefaults::Paper::~Paper() = default;

PrinterSemanticCapsAndDefaults::Paper::Paper(const Paper& other) = default;

PrinterSemanticCapsAndDefaults::Paper&
PrinterSemanticCapsAndDefaults::Paper::operator=(const Paper& other) = default;

bool PrinterSemanticCapsAndDefaults::Paper::operator==(
    const PrinterSemanticCapsAndDefaults::Paper& other) const {
  return display_name_ == other.display_name_ &&
         vendor_id_ == other.vendor_id_ && size_um_ == other.size_um_ &&
         printable_area_um_ == other.printable_area_um_ &&
         max_height_um_ == other.max_height_um_ &&
         has_borderless_variant_ == other.has_borderless_variant_
#if BUILDFLAG(IS_CHROMEOS)
         && supported_margins_um_.value_or(PaperMargins()) ==
                other.supported_margins_um_.value_or(PaperMargins())
#endif  // BUILDFLAG(IS_CHROMEOS)
      ;
}

bool PrinterSemanticCapsAndDefaults::Paper::SupportsCustomSize() const {
  return max_height_um_ > 0;
}

bool PrinterSemanticCapsAndDefaults::Paper::IsSizeWithinBounds(
    const gfx::Size& other_um) const {
  if (other_um == size_um_) {
    return true;
  }

  if (!SupportsCustomSize()) {
    return false;
  }

  return size_um_.width() == other_um.width() &&
         size_um_.height() <= other_um.height() &&
         other_um.height() <= max_height_um_;
}

bool PrinterSemanticCapsAndDefaults::MediaType::operator==(
    const PrinterSemanticCapsAndDefaults::MediaType& other) const {
  return display_name == other.display_name && vendor_id == other.vendor_id;
}

PrinterSemanticCapsAndDefaults::PrinterSemanticCapsAndDefaults() = default;

PrinterSemanticCapsAndDefaults::PrinterSemanticCapsAndDefaults(
    const PrinterSemanticCapsAndDefaults& other) = default;

PrinterSemanticCapsAndDefaults::~PrinterSemanticCapsAndDefaults() = default;

// This function is only supposed to be used in tests. The declaration in the
// header file is guarded by "#if defined(UNIT_TEST)" so that they can be used
// by tests but not non-test code. However, this .cc file is compiled as part of
// "backend" where "UNIT_TEST" is not defined. So we need to specify
// "COMPONENT_EXPORT(PRINT_BACKEND)" here again so that they are visible to
// tests.
COMPONENT_EXPORT(PRINT_BACKEND)
bool operator==(const PrinterSemanticCapsAndDefaults& caps1,
                const PrinterSemanticCapsAndDefaults& caps2) {
  return caps1.collate_capable == caps2.collate_capable &&
         caps1.collate_default == caps2.collate_default &&
         caps1.copies_max == caps2.copies_max &&
         caps1.duplex_modes == caps2.duplex_modes &&
         caps1.duplex_default == caps2.duplex_default &&
         caps1.color_changeable == caps2.color_changeable &&
         caps1.color_default == caps2.color_default &&
         caps1.color_model == caps2.color_model &&
         caps1.bw_model == caps2.bw_model && caps1.papers == caps2.papers &&
         caps1.user_defined_papers == caps2.user_defined_papers &&
         caps1.default_paper == caps2.default_paper &&
         caps1.dpis == caps2.dpis && caps1.default_dpi == caps2.default_dpi
#if BUILDFLAG(IS_CHROMEOS)
         && caps1.pin_supported == caps2.pin_supported &&
         caps1.advanced_capabilities == caps2.advanced_capabilities
#endif  // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_WIN)
         && caps1.page_output_quality == caps2.page_output_quality
#endif  // BUILDFLAG(IS_WIN)
      ;
}

PrinterCapsAndDefaults::PrinterCapsAndDefaults() = default;

PrinterCapsAndDefaults::PrinterCapsAndDefaults(
    const PrinterCapsAndDefaults& other) = default;

PrinterCapsAndDefaults::~PrinterCapsAndDefaults() = default;

PrintBackend::PrintBackend() = default;

PrintBackend::~PrintBackend() = default;

// static
scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
    const std::string& locale) {
  return g_print_backend_for_test ? g_print_backend_for_test
                                  : PrintBackend::CreateInstanceImpl(locale);
}

// static
void PrintBackend::SetPrintBackendForTesting(PrintBackend* backend) {
  g_print_backend_for_test = backend;
}

}  // namespace printing