File: printing_api_utils.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 (446 lines) | stat: -rw-r--r-- 17,008 bytes parent folder | download | duplicates (4)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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 "chrome/browser/extensions/api/printing/printing_api_utils.h"

#include <algorithm>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/json/json_reader.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/values.h"
#include "chromeos/crosapi/mojom/local_printer.mojom.h"
#include "chromeos/printing/printer_configuration.h"
#include "components/cloud_devices/common/cloud_device_description.h"
#include "components/cloud_devices/common/printer_description.h"
#include "printing/backend/print_backend.h"
#include "printing/mojom/print.mojom.h"
#include "printing/page_setup.h"
#include "printing/print_settings.h"
#include "printing/printing_features.h"
#include "printing/units.h"
#include "third_party/re2/src/re2/re2.h"

namespace extensions {

namespace idl = api::printing;

namespace {

constexpr char kLocal[] = "local";
constexpr char kKind[] = "kind";
constexpr char kIdPattern[] = "idPattern";
constexpr char kNamePattern[] = "namePattern";

bool DoesPrinterMatchDefaultPrinterRules(
    const crosapi::mojom::LocalDestinationInfo& printer,
    const std::optional<DefaultPrinterRules>& rules) {
  if (!rules.has_value())
    return false;
  return (rules->kind.empty() || rules->kind == kLocal) &&
         (rules->id_pattern.empty() ||
          RE2::FullMatch(printer.id, rules->id_pattern)) &&
         (rules->name_pattern.empty() ||
          RE2::FullMatch(printer.name, rules->name_pattern));
}

// Validate a vendor ticket item from a print job ticket.  Items are validated
// against an allow-list of values in addition to the advanced capabilities of
// the printer.  Return true if the given item is allowed, false if not.
bool ValidateVendorItem(const std::string& name,
                        const std::string& value,
                        const printing::AdvancedCapabilities& capabilities) {
  // A map containing the allowed vendor items.  The key is an IPP attribute,
  // and the value is a set of allowable values for that attribute.
  static const base::NoDestructor<
      base::flat_map<std::string_view, base::flat_set<std::string_view>>>
      kVendorItemAllowList({
          {"finishings", {"none", "trim"}},
      });

  // Check the explicit allow list.  If the value does not match, this IPP
  // attribute is then checked against the list of printer capabilities.
  const auto& item = kVendorItemAllowList->find(name);
  if (item != kVendorItemAllowList->end() && item->second.contains(value)) {
    return true;
  }

  // Check other allowed attributes against the printer capabilities.
  for (const printing::AdvancedCapability& capability : capabilities) {
    if (capability.name != name) {
      continue;
    }

    return base::Contains(capability.values, value,
                          &printing::AdvancedCapabilityValue::name);
  }

  return false;
}

}  // namespace

std::optional<DefaultPrinterRules> GetDefaultPrinterRules(
    const std::string& default_destination_selection_rules) {
  if (default_destination_selection_rules.empty())
    return std::nullopt;

  std::optional<base::Value> default_destination_selection_rules_value =
      base::JSONReader::Read(default_destination_selection_rules);
  base::Value::Dict* default_destination_selection_rules_dict =
      default_destination_selection_rules_value.has_value()
          ? default_destination_selection_rules_value->GetIfDict()
          : nullptr;
  if (!default_destination_selection_rules_dict) {
    return std::nullopt;
  }

  DefaultPrinterRules default_printer_rules;
  if (const std::string* kind =
          default_destination_selection_rules_dict->FindString(kKind)) {
    default_printer_rules.kind = *kind;
  }
  if (const std::string* id_pattern =
          default_destination_selection_rules_dict->FindString(kIdPattern)) {
    default_printer_rules.id_pattern = *id_pattern;
  }
  if (const std::string* name_pattern =
          default_destination_selection_rules_dict->FindString(kNamePattern)) {
    default_printer_rules.name_pattern = *name_pattern;
  }

  return default_printer_rules;
}

idl::Printer PrinterToIdl(
    const crosapi::mojom::LocalDestinationInfo& printer,
    const std::optional<DefaultPrinterRules>& default_printer_rules,
    const base::flat_map<std::string, int>& recently_used_ranks) {
  idl::Printer idl_printer;
  idl_printer.id = printer.id;
  idl_printer.name = printer.name;
  idl_printer.description = printer.description;
  if (printer.uri)
    idl_printer.uri = *printer.uri;
  idl_printer.source = printer.configured_via_policy
                           ? idl::PrinterSource::kPolicy
                           : idl::PrinterSource::kUser;
  idl_printer.is_default =
      DoesPrinterMatchDefaultPrinterRules(printer, default_printer_rules);
  auto it = recently_used_ranks.find(printer.id);
  if (it != recently_used_ranks.end())
    idl_printer.recently_used_rank = it->second;
  return idl_printer;
}

idl::PrinterStatus PrinterStatusToIdl(chromeos::PrinterErrorCode status) {
  switch (status) {
    case chromeos::PrinterErrorCode::NO_ERROR:
      return idl::PrinterStatus::kAvailable;
    case chromeos::PrinterErrorCode::PAPER_JAM:
      return idl::PrinterStatus::kPaperJam;
    case chromeos::PrinterErrorCode::OUT_OF_PAPER:
      return idl::PrinterStatus::kOutOfPaper;
    case chromeos::PrinterErrorCode::OUT_OF_INK:
      return idl::PrinterStatus::kOutOfInk;
    case chromeos::PrinterErrorCode::DOOR_OPEN:
      return idl::PrinterStatus::kDoorOpen;
    case chromeos::PrinterErrorCode::PRINTER_UNREACHABLE:
      return idl::PrinterStatus::kUnreachable;
    case chromeos::PrinterErrorCode::TRAY_MISSING:
      return idl::PrinterStatus::kTrayMissing;
    case chromeos::PrinterErrorCode::OUTPUT_FULL:
      return idl::PrinterStatus::kOutputFull;
    case chromeos::PrinterErrorCode::STOPPED:
      return idl::PrinterStatus::kStopped;
    case chromeos::PrinterErrorCode::EXPIRED_CERTIFICATE:
      return idl::PrinterStatus::kExpiredCertificate;
    default:
      break;
  }
  return idl::PrinterStatus::kGenericIssue;
}

std::unique_ptr<printing::PrintSettings> ParsePrintTicket(
    base::Value::Dict ticket) {
  cloud_devices::CloudDeviceDescription description;
  if (!description.InitFromValue(std::move(ticket))) {
    LOG(ERROR) << "Unable to initialize CDD from print ticket.";
    return nullptr;
  }

  auto settings = std::make_unique<printing::PrintSettings>();

  cloud_devices::printer::ColorTicketItem color;
  if (!color.LoadFrom(description)) {
    LOG(ERROR) << "Unable to load color from print ticket.";
    return nullptr;
  }
  switch (color.value().type) {
    case cloud_devices::printer::ColorType::STANDARD_MONOCHROME:
    case cloud_devices::printer::ColorType::CUSTOM_MONOCHROME:
      settings->set_color(printing::mojom::ColorModel::kGray);
      break;

    case cloud_devices::printer::ColorType::STANDARD_COLOR:
    case cloud_devices::printer::ColorType::CUSTOM_COLOR:
    case cloud_devices::printer::ColorType::AUTO_COLOR:
      settings->set_color(printing::mojom::ColorModel::kColor);
      break;

    default:
      NOTREACHED();
  }

  cloud_devices::printer::DuplexTicketItem duplex;
  if (!duplex.LoadFrom(description)) {
    LOG(ERROR) << "Unable to load duplex from print ticket.";
    return nullptr;
  }
  switch (duplex.value()) {
    case cloud_devices::printer::DuplexType::NO_DUPLEX:
      settings->set_duplex_mode(printing::mojom::DuplexMode::kSimplex);
      break;
    case cloud_devices::printer::DuplexType::LONG_EDGE:
      settings->set_duplex_mode(printing::mojom::DuplexMode::kLongEdge);
      break;
    case cloud_devices::printer::DuplexType::SHORT_EDGE:
      settings->set_duplex_mode(printing::mojom::DuplexMode::kShortEdge);
      break;
    default:
      NOTREACHED();
  }

  cloud_devices::printer::OrientationTicketItem orientation;
  if (!orientation.LoadFrom(description)) {
    LOG(ERROR) << "Unable to load orientation from print ticket.";
    return nullptr;
  }
  switch (orientation.value()) {
    case cloud_devices::printer::OrientationType::LANDSCAPE:
      settings->SetOrientation(/*landscape=*/true);
      break;
    case cloud_devices::printer::OrientationType::PORTRAIT:
      settings->SetOrientation(/*landscape=*/false);
      break;
    default:
      NOTREACHED();
  }

  cloud_devices::printer::CopiesTicketItem copies;
  if (!copies.LoadFrom(description) || copies.value() < 1) {
    LOG(ERROR) << "Unable to load copies from print ticket.";
    return nullptr;
  }
  settings->set_copies(copies.value());

  cloud_devices::printer::DpiTicketItem dpi;
  if (!dpi.LoadFrom(description)) {
    LOG(ERROR) << "Unable to load DPI from print ticket.";
    return nullptr;
  }
  settings->set_dpi_xy(dpi.value().horizontal, dpi.value().vertical);

  cloud_devices::printer::MediaTicketItem media;
  if (!media.LoadFrom(description)) {
    LOG(ERROR) << "Unable to load media from print ticket.";
    return nullptr;
  }
  cloud_devices::printer::Media media_value = media.value();
  printing::PrintSettings::RequestedMedia requested_media;
  if (media_value.size_um.width() <= 0 || media_value.size_um.height() <= 0) {
    LOG(ERROR) << "Loaded invalid media from print ticket.";
    return nullptr;
  }
  requested_media.size_microns = media_value.size_um;
  requested_media.vendor_id = media_value.vendor_id;
  settings->set_requested_media(requested_media);

  cloud_devices::printer::CollateTicketItem collate;
  if (!collate.LoadFrom(description)) {
    LOG(ERROR) << "Unable to load collate from print ticket.";
    return nullptr;
  }
  settings->set_collate(collate.value());

  // These items are optional - don't fail if they don't exist.
  cloud_devices::printer::VendorTicketItems vendor_items;
  if (vendor_items.LoadFrom(description)) {
    for (const auto& item : vendor_items) {
      settings->advanced_settings().emplace(item.id, item.value);
    }
  }

  if (base::FeatureList::IsEnabled(
          printing::features::kApiPrintingMarginsAndScale)) {
    // This item is optional - don't fail if it doesn't exist.
    cloud_devices::printer::FitToPageTicketItem fit_to_page_ticket;
    if (fit_to_page_ticket.LoadFrom(description)) {
      switch (fit_to_page_ticket.value()) {
        case cloud_devices::printer::FitToPageType::AUTO:
          settings->set_print_scaling(printing::mojom::PrintScalingType::kAuto);
          break;
        case cloud_devices::printer::FitToPageType::AUTO_FIT:
          settings->set_print_scaling(
              printing::mojom::PrintScalingType::kAutoFit);
          break;
        case cloud_devices::printer::FitToPageType::FILL:
          settings->set_print_scaling(printing::mojom::PrintScalingType::kFill);
          break;
        case cloud_devices::printer::FitToPageType::FIT:
          settings->set_print_scaling(printing::mojom::PrintScalingType::kFit);
          break;
        case cloud_devices::printer::FitToPageType::NONE:
          settings->set_print_scaling(printing::mojom::PrintScalingType::kNone);
          break;
        default:
          NOTREACHED();
      }
    }

    // This item is optional - don't fail if it doesn't exist.
    cloud_devices::printer::MarginsTicketItem margin_ticket;
    if (!margin_ticket.LoadFrom(description)) {
      settings->set_margin_type(printing::mojom::MarginType::kDefaultMargins);
    } else if (margin_ticket.value().left_um < 0 ||
               margin_ticket.value().right_um < 0 ||
               margin_ticket.value().top_um < 0 ||
               margin_ticket.value().bottom_um < 0) {
      LOG(ERROR) << "Loaded invalid margins from print ticket.";
      return nullptr;
    } else {
      settings->SetCustomMargins(
          {/*header=*/0, /*footer=*/0, margin_ticket.value().left_um,
           margin_ticket.value().right_um, margin_ticket.value().top_um,
           margin_ticket.value().bottom_um});
      if (margin_ticket.value().left_um == 0 &&
          margin_ticket.value().right_um == 0 &&
          margin_ticket.value().top_um == 0 &&
          margin_ticket.value().bottom_um == 0) {
        settings->set_margin_type(printing::mojom::MarginType::kNoMargins);
        settings->set_borderless(true);
      }
    }
  }

  return settings;
}

bool CheckSettingsAndCapabilitiesCompatibility(
    const printing::PrintSettings& settings,
    const printing::PrinterSemanticCapsAndDefaults& capabilities) {
  if (settings.collate() && !capabilities.collate_capable)
    return false;

  if (settings.copies() > capabilities.copies_max)
    return false;

  if (!base::Contains(capabilities.duplex_modes, settings.duplex_mode()))
    return false;

  std::optional<bool> is_color =
      ::printing::IsColorModelSelected(settings.color());
  bool color_mode_selected = is_color.has_value() && is_color.value();
  if (!color_mode_selected &&
      capabilities.bw_model ==
          printing::mojom::ColorModel::kUnknownColorModel) {
    return false;
  }
  if (color_mode_selected &&
      capabilities.color_model ==
          printing::mojom::ColorModel::kUnknownColorModel) {
    return false;
  }

  if (!base::Contains(capabilities.dpis, settings.dpi_size()))
    return false;

  for (const auto& [name, value] : settings.advanced_settings()) {
    if (!value.is_string()) {
      LOG(ERROR) << "Advanced setting '" << name
                 << "' expects a string value, got: "
                 << base::Value::GetTypeName(value.type());
      return false;
    }
    if (!ValidateVendorItem(name, value.GetString(),
                            capabilities.advanced_capabilities)) {
      LOG(ERROR) << "Advanced setting '" << name << ":" << value.GetString()
                 << "' is not compatible with printer capabilities";
      return false;
    }
  }

  if (base::FeatureList::IsEnabled(
          printing::features::kApiPrintingMarginsAndScale)) {
    // Default value is `kUnknownPrintScalingType`, so we only need to check if
    // the value is not the default.
    if (settings.print_scaling() !=
        printing::mojom::PrintScalingType::kUnknownPrintScalingType) {
      const bool uses_supported_print_scaling = base::Contains(
          capabilities.print_scaling_types, settings.print_scaling());
      base::UmaHistogramBoolean("Extensions.Printing.UsesSupportedPrintScaling",
                                uses_supported_print_scaling);
      if (!uses_supported_print_scaling) {
        LOG(ERROR) << "Print scaling '" << settings.print_scaling()
                   << "' is not compatible with printer capabilities";
        return false;
      }
    }

    if (settings.margin_type() !=
        printing::mojom::MarginType::kDefaultMargins) {
      const auto& requested_margins_um =
          settings.requested_custom_margins_in_microns();
      bool margins_value_supported = std::ranges::any_of(
          capabilities.papers,
          [requested_margins_um,
           needs_borderless_variant = settings.borderless()](
              const printing::PrinterSemanticCapsAndDefaults::Paper& paper) {
            // Borderless variant doesn't have margins stored separately. Thus,
            // check if there is a paper with borderless variant.
            if (needs_borderless_variant) {
              return paper.has_borderless_variant() &&
                     requested_margins_um.IsEmpty();
            }
            if (!paper.supported_margins_um().has_value()) {
              return false;
            }
            const auto& supported_margins =
                paper.supported_margins_um().value();
            return requested_margins_um ==
                   printing::PageMargins(/*header=*/0, /*footer=*/0,
                                         supported_margins.left_margin_um,
                                         supported_margins.right_margin_um,
                                         supported_margins.top_margin_um,
                                         supported_margins.bottom_margin_um);
          });
      base::UmaHistogramBoolean("Extensions.Printing.UsesSupportedMargins",
                                margins_value_supported);
      if (!margins_value_supported) {
        LOG(ERROR) << "Margin values " << requested_margins_um.ToString()
                   << " are not supported by the printer";
        return false;
      }
    }
  }

  const printing::PrintSettings::RequestedMedia& requested_media =
      settings.requested_media();
  return std::ranges::any_of(
      capabilities.papers,
      [&requested_media](
          const printing::PrinterSemanticCapsAndDefaults::Paper& paper) {
        return paper.IsSizeWithinBounds(requested_media.size_microns);
      });
}

}  // namespace extensions