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
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "printing/backend/cups_ipp_helper.h"
#include <cups/cups.h>
#include <algorithm>
#include <string>
#include <vector>
#include "base/logging.h"
#include "base/optional.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "printing/backend/cups_connection.h"
#include "printing/backend/cups_ipp_constants.h"
#include "printing/backend/cups_printer.h"
#include "printing/backend/print_backend_consts.h"
#include "printing/units.h"
#if defined(OS_CHROMEOS)
#include "base/callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "printing/backend/ipp_handler_map.h"
#include "printing/printing_features.h"
#endif // defined(OS_CHROMEOS)
namespace printing {
#if defined(OS_CHROMEOS)
constexpr int kPinMinimumLength = 4;
#endif // defined(OS_CHROMEOS)
namespace {
constexpr int kMicronsPerMM = 1000;
constexpr double kMMPerInch = 25.4;
constexpr double kMicronsPerInch = kMMPerInch * kMicronsPerMM;
constexpr double kCmPerInch = kMMPerInch * 0.1;
// Defines two prefixes of a special breed of media sizes not meant for
// users' eyes. CUPS incidentally returns these IPP values to us, but
// we have no use for them.
constexpr base::StringPiece kMediaCustomMinPrefix = "custom_min";
constexpr base::StringPiece kMediaCustomMaxPrefix = "custom_max";
enum Unit {
INCHES,
MILLIMETERS,
};
struct ColorMap {
const char* color;
ColorModel model;
};
struct DuplexMap {
const char* name;
DuplexMode mode;
};
const ColorMap kColorList[]{
{CUPS_PRINT_COLOR_MODE_COLOR, COLORMODE_COLOR},
{CUPS_PRINT_COLOR_MODE_MONOCHROME, COLORMODE_MONOCHROME},
};
const DuplexMap kDuplexList[]{
{CUPS_SIDES_ONE_SIDED, SIMPLEX},
{CUPS_SIDES_TWO_SIDED_PORTRAIT, LONG_EDGE},
{CUPS_SIDES_TWO_SIDED_LANDSCAPE, SHORT_EDGE},
};
ColorModel ColorModelFromIppColor(base::StringPiece ippColor) {
for (const ColorMap& color : kColorList) {
if (ippColor.compare(color.color) == 0) {
return color.model;
}
}
return UNKNOWN_COLOR_MODEL;
}
DuplexMode DuplexModeFromIpp(base::StringPiece ipp_duplex) {
for (const DuplexMap& entry : kDuplexList) {
if (base::EqualsCaseInsensitiveASCII(ipp_duplex, entry.name))
return entry.mode;
}
return UNKNOWN_DUPLEX_MODE;
}
gfx::Size DimensionsToMicrons(base::StringPiece value) {
Unit unit;
base::StringPiece dims;
size_t unit_position;
if ((unit_position = value.find("mm")) != base::StringPiece::npos) {
unit = MILLIMETERS;
dims = value.substr(0, unit_position);
} else if ((unit_position = value.find("in")) != base::StringPiece::npos) {
unit = INCHES;
dims = value.substr(0, unit_position);
} else {
LOG(WARNING) << "Could not parse paper dimensions";
return {0, 0};
}
double width;
double height;
std::vector<std::string> pieces = base::SplitString(
dims, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (pieces.size() != 2 || !base::StringToDouble(pieces[0], &width) ||
!base::StringToDouble(pieces[1], &height)) {
return {0, 0};
}
int width_microns;
int height_microns;
switch (unit) {
case MILLIMETERS:
width_microns = width * kMicronsPerMM;
height_microns = height * kMicronsPerMM;
break;
case INCHES:
width_microns = width * kMicronsPerInch;
height_microns = height * kMicronsPerInch;
break;
default:
NOTREACHED();
break;
}
return gfx::Size{width_microns, height_microns};
}
// We read the media name expressed by |value| and return a Paper
// with the vendor_id and size_um members populated.
// We don't handle l10n here. We do populate the display_name member
// with the prettified vendor ID, but fully expect the caller to clobber
// this if a better localization exists.
PrinterSemanticCapsAndDefaults::Paper ParsePaper(base::StringPiece value) {
// <name>_<width>x<height>{in,mm}
// e.g. na_letter_8.5x11in, iso_a4_210x297mm
std::vector<base::StringPiece> pieces = base::SplitStringPiece(
value, "_", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
// We expect at least a display string and a dimension string.
// Additionally, we drop the "custom_min*" and "custom_max*" special
// "sizes" (not for users' eyes).
if (pieces.size() < 2 || value.starts_with(kMediaCustomMinPrefix) ||
value.starts_with(kMediaCustomMaxPrefix))
return PrinterSemanticCapsAndDefaults::Paper();
base::StringPiece dimensions = pieces.back();
PrinterSemanticCapsAndDefaults::Paper paper;
paper.vendor_id = value.as_string();
paper.size_um = DimensionsToMicrons(dimensions);
// Omits the final token describing the media dimensions.
pieces.pop_back();
paper.display_name = base::JoinString(pieces, " ");
return paper;
}
ColorModel DefaultColorModel(const CupsOptionProvider& printer) {
// default color
ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppColor);
if (!attr)
return UNKNOWN_COLOR_MODEL;
return ColorModelFromIppColor(ippGetString(attr, 0, nullptr));
}
std::vector<ColorModel> SupportedColorModels(
const CupsOptionProvider& printer) {
std::vector<ColorModel> colors;
std::vector<base::StringPiece> color_modes =
printer.GetSupportedOptionValueStrings(kIppColor);
for (base::StringPiece color : color_modes) {
ColorModel color_model = ColorModelFromIppColor(color);
if (color_model != UNKNOWN_COLOR_MODEL) {
colors.push_back(color_model);
}
}
return colors;
}
void ExtractColor(const CupsOptionProvider& printer,
PrinterSemanticCapsAndDefaults* printer_info) {
printer_info->bw_model = UNKNOWN_COLOR_MODEL;
printer_info->color_model = UNKNOWN_COLOR_MODEL;
// color and b&w
std::vector<ColorModel> color_models = SupportedColorModels(printer);
for (ColorModel color : color_models) {
switch (color) {
case COLORMODE_COLOR:
printer_info->color_model = COLORMODE_COLOR;
break;
case COLORMODE_MONOCHROME:
printer_info->bw_model = COLORMODE_MONOCHROME;
break;
default:
// value not needed
break;
}
}
// changeable
printer_info->color_changeable =
(printer_info->color_model != UNKNOWN_COLOR_MODEL &&
printer_info->bw_model != UNKNOWN_COLOR_MODEL);
// default color
printer_info->color_default = DefaultColorModel(printer) == COLORMODE_COLOR;
}
void ExtractDuplexModes(const CupsOptionProvider& printer,
PrinterSemanticCapsAndDefaults* printer_info) {
std::vector<base::StringPiece> duplex_modes =
printer.GetSupportedOptionValueStrings(kIppDuplex);
for (base::StringPiece duplex : duplex_modes) {
DuplexMode duplex_mode = DuplexModeFromIpp(duplex);
if (duplex_mode != UNKNOWN_DUPLEX_MODE)
printer_info->duplex_modes.push_back(duplex_mode);
}
ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppDuplex);
printer_info->duplex_default =
attr ? DuplexModeFromIpp(ippGetString(attr, 0, nullptr))
: UNKNOWN_DUPLEX_MODE;
}
void CopiesRange(const CupsOptionProvider& printer,
int* lower_bound,
int* upper_bound) {
ipp_attribute_t* attr = printer.GetSupportedOptionValues(kIppCopies);
if (!attr) {
*lower_bound = -1;
*upper_bound = -1;
}
*lower_bound = ippGetRange(attr, 0, upper_bound);
}
void ExtractCopies(const CupsOptionProvider& printer,
PrinterSemanticCapsAndDefaults* printer_info) {
// copies
int lower_bound;
int upper_bound;
CopiesRange(printer, &lower_bound, &upper_bound);
printer_info->copies_max =
(lower_bound != -1 && upper_bound >= 2) ? upper_bound : 1;
}
// Reads resolution from |attr| and puts into |size| in dots per inch.
base::Optional<gfx::Size> GetResolution(ipp_attribute_t* attr, int i) {
ipp_res_t units;
int yres;
int xres = ippGetResolution(attr, i, &yres, &units);
if (!xres)
return {};
switch (units) {
case IPP_RES_PER_INCH:
return gfx::Size(xres, yres);
case IPP_RES_PER_CM:
return gfx::Size(xres * kCmPerInch, yres * kCmPerInch);
}
return {};
}
// Initializes |printer_info.dpis| with available resolutions and
// |printer_info.default_dpi| with default resolution provided by |printer|.
void ExtractResolutions(const CupsOptionProvider& printer,
PrinterSemanticCapsAndDefaults* printer_info) {
ipp_attribute_t* attr = printer.GetSupportedOptionValues(kIppResolution);
if (!attr)
return;
int num_options = ippGetCount(attr);
for (int i = 0; i < num_options; i++) {
base::Optional<gfx::Size> size = GetResolution(attr, i);
if (size)
printer_info->dpis.push_back(size.value());
}
ipp_attribute_t* def_attr = printer.GetDefaultOptionValue(kIppResolution);
base::Optional<gfx::Size> size = GetResolution(def_attr, 0);
if (size)
printer_info->default_dpi = size.value();
}
PrinterSemanticCapsAndDefaults::Papers SupportedPapers(
const CupsOptionProvider& printer) {
std::vector<base::StringPiece> papers =
printer.GetSupportedOptionValueStrings(kIppMedia);
PrinterSemanticCapsAndDefaults::Papers parsed_papers;
parsed_papers.reserve(papers.size());
for (base::StringPiece paper : papers) {
PrinterSemanticCapsAndDefaults::Paper parsed = ParsePaper(paper);
// If a paper fails to parse reasonably, we should avoid propagating
// it - e.g. CUPS is known to give out empty vendor IDs at times:
// https://crbug.com/920295#c23
if (!parsed.display_name.empty()) {
parsed_papers.push_back(parsed);
}
}
return parsed_papers;
}
bool CollateCapable(const CupsOptionProvider& printer) {
std::vector<base::StringPiece> values =
printer.GetSupportedOptionValueStrings(kIppCollate);
return base::Contains(values, kCollated) &&
base::Contains(values, kUncollated);
}
bool CollateDefault(const CupsOptionProvider& printer) {
ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppCollate);
if (!attr)
return false;
base::StringPiece name = ippGetString(attr, 0, nullptr);
return name.compare(kCollated) == 0;
}
#if defined(OS_CHROMEOS)
bool PinSupported(const CupsOptionProvider& printer) {
ipp_attribute_t* attr = printer.GetSupportedOptionValues(kIppPin);
if (!attr)
return false;
int password_maximum_length_supported = ippGetInteger(attr, 0);
if (password_maximum_length_supported < kPinMinimumLength)
return false;
std::vector<base::StringPiece> values =
printer.GetSupportedOptionValueStrings(kIppPinEncryption);
return base::Contains(values, kPinEncryptionNone);
}
// Returns the number of IPP attributes added to |caps| (not necessarily in
// 1-to-1 correspondence).
size_t AddAttributes(const CupsOptionProvider& printer,
const char* attr_group_name,
AdvancedCapabilities* caps) {
ipp_attribute_t* attr = printer.GetSupportedOptionValues(attr_group_name);
if (!attr)
return 0;
int num_options = ippGetCount(attr);
static const base::NoDestructor<HandlerMap> handlers(GenerateHandlers());
std::vector<std::string> unknown_options;
size_t attr_count = 0;
for (int i = 0; i < num_options; i++) {
const char* option_name = ippGetString(attr, i, nullptr);
auto it = handlers->find(option_name);
if (it == handlers->end()) {
unknown_options.emplace_back(option_name);
continue;
}
size_t previous_size = caps->size();
// Run the handler that adds items to |caps| based on option type.
it->second.Run(printer, option_name, caps);
if (caps->size() > previous_size)
attr_count++;
}
if (!unknown_options.empty()) {
LOG(WARNING) << "Unknown IPP options: "
<< base::JoinString(unknown_options, ", ");
}
return attr_count;
}
void ExtractAdvancedCapabilities(const CupsOptionProvider& printer,
PrinterSemanticCapsAndDefaults* printer_info) {
AdvancedCapabilities* options = &printer_info->advanced_capabilities;
size_t attr_count = AddAttributes(printer, kIppJobAttributes, options);
attr_count += AddAttributes(printer, kIppDocumentAttributes, options);
base::UmaHistogramCounts1000("Printing.CUPS.IppAttributesCount", attr_count);
}
#endif // defined(OS_CHROMEOS)
} // namespace
PrinterSemanticCapsAndDefaults::Paper DefaultPaper(
const CupsOptionProvider& printer) {
ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppMedia);
if (!attr)
return PrinterSemanticCapsAndDefaults::Paper();
return ParsePaper(ippGetString(attr, 0, nullptr));
}
void CapsAndDefaultsFromPrinter(const CupsOptionProvider& printer,
PrinterSemanticCapsAndDefaults* printer_info) {
// collate
printer_info->collate_default = CollateDefault(printer);
printer_info->collate_capable = CollateCapable(printer);
// paper
printer_info->default_paper = DefaultPaper(printer);
printer_info->papers = SupportedPapers(printer);
#if defined(OS_CHROMEOS)
printer_info->pin_supported = PinSupported(printer);
if (base::FeatureList::IsEnabled(printing::features::kAdvancedPpdAttributes))
ExtractAdvancedCapabilities(printer, printer_info);
#endif // defined(OS_CHROMEOS)
ExtractCopies(printer, printer_info);
ExtractColor(printer, printer_info);
ExtractDuplexModes(printer, printer_info);
ExtractResolutions(printer, printer_info);
}
ScopedIppPtr WrapIpp(ipp_t* ipp) {
return ScopedIppPtr(ipp, &ippDelete);
}
} // namespace printing
|