File: cups_helper.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 (990 lines) | stat: -rw-r--r-- 36,551 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "printing/backend/cups_helper.h"

#include <cups/ppd.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>

#include <optional>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>

#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "printing/backend/cups_deleters.h"
#include "printing/backend/cups_weak_functions.h"
#include "printing/backend/print_backend.h"
#include "printing/backend/print_backend_consts.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_job_constants_cups.h"
#include "printing/printing_utils.h"
#include "printing/units.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"

using base::EqualsCaseInsensitiveASCII;

namespace printing {

// This section contains helper code for PPD parsing for semantic capabilities.
namespace {

// Timeout for establishing a CUPS connection.  It is expected that cupsd is
// able to start and respond on all systems within this duration.
constexpr base::TimeDelta kCupsTimeout = base::Seconds(5);

// CUPS default max copies value (parsed from kCupsMaxCopies PPD attribute).
constexpr int32_t kDefaultMaxCopies = 9999;
constexpr char kCupsMaxCopies[] = "cupsMaxCopies";

constexpr char kColorDevice[] = "ColorDevice";

constexpr char kDuplex[] = "Duplex";
constexpr char kDuplexNone[] = "None";
constexpr char kDuplexNoTumble[] = "DuplexNoTumble";
constexpr char kDuplexTumble[] = "DuplexTumble";
constexpr char kPageSize[] = "PageSize";

// Brother printer specific options.
constexpr char kBrotherDuplex[] = "BRDuplex";

int32_t GetCopiesMax(ppd_file_t* ppd) {
  ppd_attr_t* attr = ppdFindAttr(ppd, kCupsMaxCopies, nullptr);
  if (!attr || !attr->value) {
    return kDefaultMaxCopies;
  }

  int32_t ret;
  return base::StringToInt(attr->value, &ret) ? ret : kDefaultMaxCopies;
}

std::pair<std::vector<mojom::DuplexMode>, mojom::DuplexMode> GetDuplexSettings(
    ppd_file_t* ppd) {
  std::vector<mojom::DuplexMode> duplex_modes;
  mojom::DuplexMode duplex_default = mojom::DuplexMode::kUnknownDuplexMode;

  ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex);
  ppd_option_t* option = ppdFindOption(ppd, kDuplex);
  if (!option)
    option = ppdFindOption(ppd, kBrotherDuplex);

  if (!option)
    return std::make_pair(std::move(duplex_modes), duplex_default);

  if (!duplex_choice)
    duplex_choice = ppdFindChoice(option, option->defchoice);

  if (ppdFindChoice(option, kDuplexNone))
    duplex_modes.push_back(mojom::DuplexMode::kSimplex);

  if (ppdFindChoice(option, kDuplexNoTumble))
    duplex_modes.push_back(mojom::DuplexMode::kLongEdge);

  if (ppdFindChoice(option, kDuplexTumble))
    duplex_modes.push_back(mojom::DuplexMode::kShortEdge);

  if (!duplex_choice)
    return std::make_pair(std::move(duplex_modes), duplex_default);

  const char* choice = duplex_choice->choice;
  if (EqualsCaseInsensitiveASCII(choice, kDuplexNone)) {
    duplex_default = mojom::DuplexMode::kSimplex;
  } else if (EqualsCaseInsensitiveASCII(choice, kDuplexTumble)) {
    duplex_default = mojom::DuplexMode::kShortEdge;
  } else {
    duplex_default = mojom::DuplexMode::kLongEdge;
  }
  return std::make_pair(std::move(duplex_modes), duplex_default);
}

std::optional<gfx::Size> ParseResolutionString(const char* input) {
  int len = strlen(input);
  if (len == 0) {
    VLOG(1) << "Bad PPD resolution choice: null string";
    return std::nullopt;
  }

  int n = 0;  // number of chars successfully parsed by sscanf()
  int dpi_x;
  int dpi_y;
  sscanf(input, "%ddpi%n", &dpi_x, &n);
  if (n == len) {
    dpi_y = dpi_x;
  } else {
    sscanf(input, "%dx%ddpi%n", &dpi_x, &dpi_y, &n);
    if (n != len) {
      VLOG(1) << "Bad PPD resolution choice: " << input;
      return std::nullopt;
    }
  }
  if (dpi_x <= 0 || dpi_y <= 0) {
    VLOG(1) << "Invalid PPD resolution dimensions: " << dpi_x << " " << dpi_y;
    return std::nullopt;
  }

  return gfx::Size(dpi_x, dpi_y);
}

std::pair<std::vector<gfx::Size>, gfx::Size> GetResolutionSettings(
    ppd_file_t* ppd) {
  static constexpr const char* kResolutions[] = {
      "Resolution",     "JCLResolution", "SetResolution", "CNRes_PGP",
      "HPPrintQuality", "LXResolution",  "BRResolution"};
  ppd_option_t* res;
  for (const char* res_name : kResolutions) {
    res = ppdFindOption(ppd, res_name);
    if (res)
      break;
  }

  // Some printers, such as Generic-CUPS-BRF-Printer, do not specify a
  // resolution in their ppd file. Provide a default DPI if no valid DPI is
  // found.
#if BUILDFLAG(IS_MAC)
  constexpr gfx::Size kDefaultMissingDpi(kDefaultMacDpi, kDefaultMacDpi);
#else
  constexpr gfx::Size kDefaultMissingDpi(kDefaultPdfDpi, kDefaultPdfDpi);
#endif

  std::vector<gfx::Size> dpis;
  gfx::Size default_dpi;
  if (res) {
    // SAFETY: Required from CUPS.
    auto choices = UNSAFE_BUFFERS(base::span<const ppd_choice_t>(
        res->choices, static_cast<size_t>(res->num_choices)));
    for (const auto& choice : choices) {
      const char* choice_str = choice.choice;
      CHECK(choice_str);
      std::optional<gfx::Size> parsed_size = ParseResolutionString(choice_str);
      if (!parsed_size.has_value()) {
        continue;
      }

      dpis.push_back(parsed_size.value());
      if (!strcmp(choice_str, res->defchoice)) {
        default_dpi = dpis.back();
      }
    }
  } else {
    // If there is no resolution option, then check for a standalone
    // DefaultResolution.
    ppd_attr_t* attr = ppdFindAttr(ppd, "DefaultResolution", nullptr);
    if (attr) {
      CHECK(attr->value);
      std::optional<gfx::Size> parsed_size = ParseResolutionString(attr->value);
      if (parsed_size.has_value()) {
        dpis.push_back(parsed_size.value());
        default_dpi = parsed_size.value();
      }
    }
  }

  if (dpis.empty()) {
    dpis.push_back(kDefaultMissingDpi);
    default_dpi = kDefaultMissingDpi;
  }
  return std::make_pair(std::move(dpis), default_dpi);
}

bool GetBasicColorModelSettings(ppd_file_t* ppd,
                                mojom::ColorModel* color_model_for_black,
                                mojom::ColorModel* color_model_for_color,
                                bool* color_is_default) {
  ppd_option_t* color_model = ppdFindOption(ppd, kCUPSColorModel);
  if (!color_model)
    return false;

  if (ppdFindChoice(color_model, kBlack))
    *color_model_for_black = mojom::ColorModel::kBlack;
  else if (ppdFindChoice(color_model, kGray))
    *color_model_for_black = mojom::ColorModel::kGray;
  else if (ppdFindChoice(color_model, kGrayscale))
    *color_model_for_black = mojom::ColorModel::kGrayscale;

  if (ppdFindChoice(color_model, kColor))
    *color_model_for_color = mojom::ColorModel::kColor;
  else if (ppdFindChoice(color_model, kCMYK))
    *color_model_for_color = mojom::ColorModel::kCMYK;
  else if (ppdFindChoice(color_model, kRGB))
    *color_model_for_color = mojom::ColorModel::kRGB;
  else if (ppdFindChoice(color_model, kRGBA))
    *color_model_for_color = mojom::ColorModel::kRGBA;
  else if (ppdFindChoice(color_model, kRGB16))
    *color_model_for_color = mojom::ColorModel::kRGB16;
  else if (ppdFindChoice(color_model, kCMY))
    *color_model_for_color = mojom::ColorModel::kCMY;
  else if (ppdFindChoice(color_model, kKCMY))
    *color_model_for_color = mojom::ColorModel::kKCMY;
  else if (ppdFindChoice(color_model, kCMY_K))
    *color_model_for_color = mojom::ColorModel::kCMYPlusK;

  ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kCUPSColorModel);
  if (!marked_choice)
    marked_choice = ppdFindChoice(color_model, color_model->defchoice);

  if (marked_choice) {
    *color_is_default =
        !EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) &&
        !EqualsCaseInsensitiveASCII(marked_choice->choice, kGray) &&
        !EqualsCaseInsensitiveASCII(marked_choice->choice, kGrayscale);
  }
  return true;
}

bool GetPrintOutModeColorSettings(ppd_file_t* ppd,
                                  mojom::ColorModel* color_model_for_black,
                                  mojom::ColorModel* color_model_for_color,
                                  bool* color_is_default) {
  ppd_option_t* printout_mode = ppdFindOption(ppd, kCUPSPrintoutMode);
  if (!printout_mode)
    return false;

  *color_model_for_color = mojom::ColorModel::kPrintoutModeNormal;
  *color_model_for_black = mojom::ColorModel::kPrintoutModeNormal;

  // Check to see if NORMAL_GRAY value is supported by PrintoutMode.
  // If NORMAL_GRAY is not supported, NORMAL value is used to
  // represent grayscale. If NORMAL_GRAY is supported, NORMAL is used to
  // represent color.
  if (ppdFindChoice(printout_mode, kNormalGray))
    *color_model_for_black = mojom::ColorModel::kPrintoutModeNormalGray;

  // Get the default marked choice to identify the default color setting
  // value.
  ppd_choice_t* printout_mode_choice =
      ppdFindMarkedChoice(ppd, kCUPSPrintoutMode);
  if (!printout_mode_choice) {
    printout_mode_choice =
        ppdFindChoice(printout_mode, printout_mode->defchoice);
  }
  if (printout_mode_choice) {
    if (EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kNormalGray) ||
        EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kHighGray) ||
        EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kDraftGray)) {
      *color_model_for_black = mojom::ColorModel::kPrintoutModeNormalGray;
      *color_is_default = false;
    }
  }
  return true;
}

bool GetColorModeSettings(ppd_file_t* ppd,
                          mojom::ColorModel* color_model_for_black,
                          mojom::ColorModel* color_model_for_color,
                          bool* color_is_default) {
  // Samsung printers use "ColorMode" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSColorMode);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kColor) ||
      ppdFindChoice(color_mode_option, kSamsungColorTrue)) {
    *color_model_for_color = mojom::ColorModel::kColorModeColor;
  }

  if (ppdFindChoice(color_mode_option, kMonochrome) ||
      ppdFindChoice(color_mode_option, kSamsungColorFalse)) {
    *color_model_for_black = mojom::ColorModel::kColorModeMonochrome;
  }

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSColorMode);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default =
        EqualsCaseInsensitiveASCII(mode_choice->choice, kColor) ||
        EqualsCaseInsensitiveASCII(mode_choice->choice, kSamsungColorTrue);
  }
  return true;
}

bool GetBrotherColorSettings(ppd_file_t* ppd,
                             mojom::ColorModel* color_model_for_black,
                             mojom::ColorModel* color_model_for_color,
                             bool* color_is_default) {
  // Some Brother printers use "BRMonoColor" attribute in their PPDs.
  // Some Brother printers use "BRPrintQuality" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSBrotherMonoColor);
  if (!color_mode_option)
    color_mode_option = ppdFindOption(ppd, kCUPSBrotherPrintQuality);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kFullColor))
    *color_model_for_color = mojom::ColorModel::kBrotherCUPSColor;
  else if (ppdFindChoice(color_mode_option, kColor))
    *color_model_for_color = mojom::ColorModel::kBrotherBRScript3Color;

  if (ppdFindChoice(color_mode_option, kMono))
    *color_model_for_black = mojom::ColorModel::kBrotherCUPSMono;
  else if (ppdFindChoice(color_mode_option, kBlack))
    *color_model_for_black = mojom::ColorModel::kBrotherBRScript3Black;

  ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kCUPSColorMode);
  if (!marked_choice) {
    marked_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }
  if (marked_choice) {
    *color_is_default =
        !EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) &&
        !EqualsCaseInsensitiveASCII(marked_choice->choice, kMono);
  }
  return true;
}

bool GetHPColorSettings(ppd_file_t* ppd,
                        mojom::ColorModel* color_model_for_black,
                        mojom::ColorModel* color_model_for_color,
                        bool* color_is_default) {
  // Some HP printers use "Color/Color Model" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kColor))
    *color_model_for_color = mojom::ColorModel::kHPColorColor;
  if (ppdFindChoice(color_mode_option, kBlack))
    *color_model_for_black = mojom::ColorModel::kHPColorBlack;

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSColorMode);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }
  if (mode_choice) {
    *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kColor);
  }
  return true;
}

bool GetHPColorModeSettings(ppd_file_t* ppd,
                            mojom::ColorModel* color_model_for_black,
                            mojom::ColorModel* color_model_for_color,
                            bool* color_is_default) {
  // Some HP printers use "HPColorMode/Mode" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSHpColorMode);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kHpColorPrint))
    *color_model_for_color = mojom::ColorModel::kHPColorColor;
  if (ppdFindChoice(color_mode_option, kHpGrayscalePrint))
    *color_model_for_black = mojom::ColorModel::kHPColorBlack;

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSHpColorMode);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }
  if (mode_choice) {
    *color_is_default =
        EqualsCaseInsensitiveASCII(mode_choice->choice, kHpColorPrint);
  }
  return true;
}

bool GetHpPjlColorAsGrayModeSettings(ppd_file_t* ppd,
                                     mojom::ColorModel* color_model_for_black,
                                     mojom::ColorModel* color_model_for_color,
                                     bool* color_is_default) {
  // Some HP printers use "HPPJLColorAsGray" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSHpPjlColorAsGray);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kHpPjlColorAsGrayYes)) {
    *color_model_for_black = mojom::ColorModel::kHpPjlColorAsGrayYes;
  }

  if (ppdFindChoice(color_mode_option, kHpPjlColorAsGrayNo)) {
    *color_model_for_color = mojom::ColorModel::kHpPjlColorAsGrayNo;
  }

  ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kCUPSHpPjlColorAsGray);
  if (!marked_choice) {
    marked_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }
  if (marked_choice) {
    *color_is_default =
        EqualsCaseInsensitiveASCII(marked_choice->choice, kHpPjlColorAsGrayNo);
  }
  return true;
}

bool GetCanonCNColorModeSettings(ppd_file_t* ppd,
                                 mojom::ColorModel* color_model_for_black,
                                 mojom::ColorModel* color_model_for_color,
                                 bool* color_is_default) {
  // Some Canon printers use "CNColorMode" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSCanonCNColorMode);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kColor)) {
    *color_model_for_color = mojom::ColorModel::kCanonCNColorModeColor;
  }

  if (ppdFindChoice(color_mode_option, kMono)) {
    *color_model_for_black = mojom::ColorModel::kCanonCNColorModeMono;
  }

  ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kCUPSCanonCNColorMode);
  if (!marked_choice) {
    marked_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }
  if (marked_choice) {
    *color_is_default =
        EqualsCaseInsensitiveASCII(marked_choice->choice, kColor);
  }
  return true;
}

bool GetCanonCNIJGrayscaleSettings(ppd_file_t* ppd,
                                   mojom::ColorModel* color_model_for_black,
                                   mojom::ColorModel* color_model_for_color,
                                   bool* color_is_default) {
  // Some Canon printers use "CNIJGrayScale" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSCanonCNIJGrayScale);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kZero)) {
    *color_model_for_color = mojom::ColorModel::kCanonCNIJGrayScaleZero;
  }
  if (ppdFindChoice(color_mode_option, kOne)) {
    *color_model_for_black = mojom::ColorModel::kCanonCNIJGrayScaleOne;
  }

  ppd_choice_t* marked_choice =
      ppdFindMarkedChoice(ppd, kCUPSCanonCNIJGrayScale);
  if (marked_choice) {
    *color_is_default =
        !EqualsCaseInsensitiveASCII(marked_choice->choice, kOne);
  }
  return true;
}

bool GetEpsonInkSettings(ppd_file_t* ppd,
                         mojom::ColorModel* color_model_for_black,
                         mojom::ColorModel* color_model_for_color,
                         bool* color_is_default) {
  // Epson printers use "Ink" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSEpsonInk);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kEpsonColor))
    *color_model_for_color = mojom::ColorModel::kEpsonInkColor;
  if (ppdFindChoice(color_mode_option, kEpsonMono))
    *color_model_for_black = mojom::ColorModel::kEpsonInkMono;

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSEpsonInk);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kColor);
  }
  return true;
}

bool GetKonicaMinoltaSelectColorSettings(
    ppd_file_t* ppd,
    mojom::ColorModel* color_model_for_black,
    mojom::ColorModel* color_model_for_color,
    bool* color_is_default) {
  ppd_option_t* color_mode_option =
      ppdFindOption(ppd, kCUPSKonicaMinoltaSelectColor);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kColor)) {
    *color_model_for_color = mojom::ColorModel::kColor;
  }
  if (ppdFindChoice(color_mode_option, kGrayscale)) {
    *color_model_for_black = mojom::ColorModel::kGrayscale;
  }

  ppd_choice_t* mode_choice =
      ppdFindMarkedChoice(ppd, kCUPSKonicaMinoltaSelectColor);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kColor);
  }
  return true;
}

bool GetLexmarkBLWSettings(ppd_file_t* ppd,
                           mojom::ColorModel* color_model_for_black,
                           mojom::ColorModel* color_model_for_color,
                           bool* color_is_default) {
  // Lexmark printers use "BLW" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSLexmarkBLW);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kLexmarkBLWFalse)) {
    *color_model_for_color = mojom::ColorModel::kColor;
  }
  if (ppdFindChoice(color_mode_option, kLexmarkBLWTrue)) {
    *color_model_for_black = mojom::ColorModel::kGray;
  }

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSLexmarkBLW);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kColor);
  }
  return true;
}

bool GetOkiSettings(ppd_file_t* ppd,
                    mojom::ColorModel* color_model_for_black,
                    mojom::ColorModel* color_model_for_color,
                    bool* color_is_default) {
  // Oki printers use "OKControl" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSOkiControl);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kAuto)) {
    *color_model_for_color = mojom::ColorModel::kOkiOKControlColor;
  }
  if (ppdFindChoice(color_mode_option, kGray)) {
    *color_model_for_black = mojom::ColorModel::kOkiOKControlGray;
  }

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSOkiControl);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kAuto);
  }
  return true;
}

bool GetSharpARCModeSettings(ppd_file_t* ppd,
                             mojom::ColorModel* color_model_for_black,
                             mojom::ColorModel* color_model_for_color,
                             bool* color_is_default) {
  // Sharp printers use "ARCMode" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSSharpARCMode);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kSharpCMColor))
    *color_model_for_color = mojom::ColorModel::kSharpARCModeCMColor;
  if (ppdFindChoice(color_mode_option, kSharpCMBW))
    *color_model_for_black = mojom::ColorModel::kSharpARCModeCMBW;

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSSharpARCMode);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    // Many Sharp printers use "CMAuto" as the default color mode.
    *color_is_default =
        !EqualsCaseInsensitiveASCII(mode_choice->choice, kSharpCMBW);
  }
  return true;
}

bool GetXeroxColorSettings(ppd_file_t* ppd,
                           mojom::ColorModel* color_model_for_black,
                           mojom::ColorModel* color_model_for_color,
                           bool* color_is_default) {
  // Some Xerox printers use "XRXColor" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSXeroxXRXColor);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kXeroxAutomatic))
    *color_model_for_color = mojom::ColorModel::kXeroxXRXColorAutomatic;
  if (ppdFindChoice(color_mode_option, kXeroxBW))
    *color_model_for_black = mojom::ColorModel::kXeroxXRXColorBW;

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSXeroxXRXColor);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    // Many Xerox printers use "Automatic" as the default color mode.
    *color_is_default =
        !EqualsCaseInsensitiveASCII(mode_choice->choice, kXeroxBW);
  }
  return true;
}

bool GetXeroxOutputColorSettings(ppd_file_t* ppd,
                                 mojom::ColorModel* color_model_for_black,
                                 mojom::ColorModel* color_model_for_color,
                                 bool* color_is_default) {
  // Some Xerox printers use "XROutputColor" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSXeroxXROutputColor);
  if (!color_mode_option) {
    return false;
  }

  if (ppdFindChoice(color_mode_option, kPrintAsColor)) {
    *color_model_for_color = mojom::ColorModel::kXeroxXROutputColorPrintAsColor;
  }
  if (ppdFindChoice(color_mode_option, kPrintAsGrayscale)) {
    *color_model_for_black =
        mojom::ColorModel::kXeroxXROutputColorPrintAsGrayscale;
  }

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSXeroxXROutputColor);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default =
        EqualsCaseInsensitiveASCII(mode_choice->choice, kPrintAsColor);
  }
  return true;
}

bool GetProcessColorModelSettings(ppd_file_t* ppd,
                                  mojom::ColorModel* color_model_for_black,
                                  mojom::ColorModel* color_model_for_color,
                                  bool* color_is_default) {
  // Canon printers use "ProcessColorModel" attribute in their PPDs.
  ppd_option_t* color_mode_option = ppdFindOption(ppd, kCUPSProcessColorModel);
  if (!color_mode_option)
    return false;

  if (ppdFindChoice(color_mode_option, kRGB))
    *color_model_for_color = mojom::ColorModel::kProcessColorModelRGB;
  else if (ppdFindChoice(color_mode_option, kCMYK))
    *color_model_for_color = mojom::ColorModel::kProcessColorModelCMYK;

  if (ppdFindChoice(color_mode_option, kGreyscale))
    *color_model_for_black = mojom::ColorModel::kProcessColorModelGreyscale;

  ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kCUPSProcessColorModel);
  if (!mode_choice) {
    mode_choice =
        ppdFindChoice(color_mode_option, color_mode_option->defchoice);
  }

  if (mode_choice) {
    *color_is_default =
        !EqualsCaseInsensitiveASCII(mode_choice->choice, kGreyscale);
  }
  return true;
}

bool GetColorModelSettings(ppd_file_t* ppd,
                           mojom::ColorModel* cm_black,
                           mojom::ColorModel* cm_color,
                           bool* is_color) {
  bool is_color_device = false;
  ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr);
  if (attr && attr->value)
    is_color_device = ppd->color_device;

  *is_color = is_color_device;
  return (is_color_device &&
          GetBasicColorModelSettings(ppd, cm_black, cm_color, is_color)) ||
         GetPrintOutModeColorSettings(ppd, cm_black, cm_color, is_color) ||
         GetColorModeSettings(ppd, cm_black, cm_color, is_color) ||
         GetHPColorSettings(ppd, cm_black, cm_color, is_color) ||
         GetHPColorModeSettings(ppd, cm_black, cm_color, is_color) ||
         GetHpPjlColorAsGrayModeSettings(ppd, cm_black, cm_color, is_color) ||
         GetBrotherColorSettings(ppd, cm_black, cm_color, is_color) ||
         GetCanonCNColorModeSettings(ppd, cm_black, cm_color, is_color) ||
         GetCanonCNIJGrayscaleSettings(ppd, cm_black, cm_color, is_color) ||
         GetEpsonInkSettings(ppd, cm_black, cm_color, is_color) ||
         GetKonicaMinoltaSelectColorSettings(ppd, cm_black, cm_color,
                                             is_color) ||
         GetLexmarkBLWSettings(ppd, cm_black, cm_color, is_color) ||
         GetOkiSettings(ppd, cm_black, cm_color, is_color) ||
         GetSharpARCModeSettings(ppd, cm_black, cm_color, is_color) ||
         GetXeroxColorSettings(ppd, cm_black, cm_color, is_color) ||
         GetXeroxOutputColorSettings(ppd, cm_black, cm_color, is_color) ||
         GetProcessColorModelSettings(ppd, cm_black, cm_color, is_color);
}

// Default port for IPP print servers.
const int kDefaultIPPServerPort = 631;

}  // namespace

// Helper wrapper around http_t structure, with connection and cleanup
// functionality.
HttpConnectionCUPS::HttpConnectionCUPS(const GURL& print_server_url,
                                       http_encryption_t encryption,
                                       bool blocking) {
  // If we have an empty url, use default print server.
  if (print_server_url.is_empty())
    return;

  int port = print_server_url.IntPort();
  if (port == url::PORT_UNSPECIFIED)
    port = kDefaultIPPServerPort;

  http_ = HttpConnect2(print_server_url.host().c_str(), port,
                       /*addrlist=*/nullptr, AF_UNSPEC, encryption,
                       blocking ? 1 : 0, kCupsTimeout.InMilliseconds(),
                       /*cancel=*/nullptr);
}

HttpConnectionCUPS::~HttpConnectionCUPS() = default;

http_t* HttpConnectionCUPS::http() {
  return http_.get();
}

bool ParsePpdCapabilities(cups_dest_t* dest,
                          std::string_view locale,
                          std::string_view printer_capabilities,
                          PrinterSemanticCapsAndDefaults* printer_info) {
  // A file created while in a sandbox will be automatically deleted once all
  // handles to it have been closed.  This precludes the use of multiple
  // operations against a file path.
  //
  // Underlying CUPS libraries process the PPD using standard I/O file
  // descriptors, so `FILE` stream APIs that don't support that are not an
  // option (e.g., can't use fmemopen()).
  //
  // Previous attempts to just read & write with a single disk `FILE` stream
  // demonstrated occasional data corruption in the wild, so resort to working
  // directly with lower-level file descriptors.
  base::FilePath temp_dir;
  if (!base::GetTempDir(&temp_dir))
    return false;

  base::FilePath ppd_file_path;
  base::ScopedFD ppd_fd =
      base::CreateAndOpenFdForTemporaryFileInDir(temp_dir, &ppd_file_path);
  if (!ppd_fd.is_valid())
    return false;

  // Unlike Windows, POSIX platforms do not have the ability to mark files as
  // "delete on close". So just delete `ppd_file_path` here. The file is still
  // accessible via `ppd_fd`.
  if (!base::DeleteFile(ppd_file_path))
    return false;

  if (!base::WriteFileDescriptor(ppd_fd.get(), printer_capabilities) ||
      lseek(ppd_fd.get(), 0, SEEK_SET) == -1) {
    return false;
  }

  // We release ownership of `ppd_fd` here because ppdOpenFd() assumes ownership
  // of it in all but one case (see below).
  int unowned_ppd_fd = ppd_fd.release();
  ppd_file_t* ppd = ppdOpenFd(unowned_ppd_fd);
  if (!ppd) {
    int line = 0;
    ppd_status_t ppd_status = ppdLastError(&line);
    LOG(ERROR) << "Failed to open PDD file: error " << ppd_status << " at line "
               << line << ", " << ppdErrorString(ppd_status);
    if (ppd_status == PPD_FILE_OPEN_ERROR) {
      // Normally ppdOpenFd assumes ownership of the file descriptor we give it,
      // regardless of success or failure. The one exception is when it fails
      // with PPD_FILE_OPEN_ERROR. In that case ownership is retained by the
      // caller, so we must explicitly close it.
      close(unowned_ppd_fd);
    }
    return false;
  }

  ppdMarkDefaults(ppd);
  if (dest)
    cupsMarkOptions(ppd, dest->num_options, dest->options);

  PrinterSemanticCapsAndDefaults caps;
  caps.collate_capable = true;
  caps.collate_default = true;
  caps.copies_max = GetCopiesMax(ppd);

  std::tie(caps.duplex_modes, caps.duplex_default) = GetDuplexSettings(ppd);
  std::tie(caps.dpis, caps.default_dpi) = GetResolutionSettings(ppd);

  mojom::ColorModel cm_black = mojom::ColorModel::kUnknownColorModel;
  mojom::ColorModel cm_color = mojom::ColorModel::kUnknownColorModel;
  bool is_color = false;
  if (!GetColorModelSettings(ppd, &cm_black, &cm_color, &is_color)) {
    VLOG(1) << "Unknown printer color model";
  }

  caps.color_changeable =
      ((cm_color != mojom::ColorModel::kUnknownColorModel) &&
       (cm_black != mojom::ColorModel::kUnknownColorModel) &&
       (cm_color != cm_black));
  caps.color_default = is_color;
  caps.color_model = cm_color;
  caps.bw_model = cm_black;

  if (ppd->num_sizes > 0 && ppd->sizes) {
    VLOG(1) << "Paper list size - " << ppd->num_sizes;
    ppd_option_t* paper_option = ppdFindOption(ppd, kPageSize);
    bool is_default_found = false;
    // SAFETY: Required from CUPS.
    auto sizes = UNSAFE_BUFFERS(base::span<const ppd_size_t>(
        ppd->sizes, static_cast<size_t>(ppd->num_sizes)));
    for (const auto& size : sizes) {
      const gfx::Size paper_size_um(
          ConvertUnit(size.width, kPointsPerInch, kMicronsPerInch),
          ConvertUnit(size.length, kPointsPerInch, kMicronsPerInch));
      if (!paper_size_um.IsEmpty()) {
        std::string display_name;
        if (paper_option) {
          ppd_choice_t* paper_choice = ppdFindChoice(paper_option, size.name);
          // Human readable paper name should be UTF-8 encoded, but some PPDs
          // do not follow this standard.
          if (paper_choice && base::IsStringUTF8(paper_choice->text)) {
            display_name = paper_choice->text;
          }
        }
        int printable_area_left_um =
            ConvertUnit(size.left, kPointsPerInch, kMicronsPerInch);
        int printable_area_bottom_um =
            ConvertUnit(size.bottom, kPointsPerInch, kMicronsPerInch);
        // `size.right` is the horizontal distance from the left of the paper to
        // the right of the printable area.
        int printable_area_right_um =
            ConvertUnit(size.right, kPointsPerInch, kMicronsPerInch);
        // `size.top` is the vertical distance from the bottom of the paper to
        // the top of the printable area.
        int printable_area_top_um =
            ConvertUnit(size.top, kPointsPerInch, kMicronsPerInch);

        gfx::Rect printable_area_um(
            printable_area_left_um, printable_area_bottom_um,
            /*width=*/printable_area_right_um - printable_area_left_um,
            /*height=*/printable_area_top_um - printable_area_bottom_um);

        // Default to the paper size if printable area is empty.
        // We've seen some drivers have a printable area that goes out of
        // bounds of the paper size. In those cases, set the printable area to
        // be the size. (See crbug.com/1412305.)
        const gfx::Rect size_um_rect = gfx::Rect(paper_size_um);
        if (printable_area_um.IsEmpty() ||
            !size_um_rect.Contains(printable_area_um)) {
          printable_area_um = size_um_rect;
        }

        PrinterSemanticCapsAndDefaults::Paper paper(
            display_name,
            /*vendor_id=*/size.name, paper_size_um, printable_area_um);

        caps.papers.push_back(paper);
        if (size.marked) {
          caps.default_paper = paper;
          is_default_found = true;
        }
      }
    }
    if (!is_default_found) {
      gfx::Size locale_paper_um = GetDefaultPaperSizeFromLocaleMicrons(locale);
      for (const PrinterSemanticCapsAndDefaults::Paper& paper : caps.papers) {
        // Set epsilon to 500 microns to allow tolerance of rounded paper sizes.
        // While the above utility function returns paper sizes in microns, they
        // are still rounded to the nearest millimeter (1000 microns).
        constexpr int kSizeEpsilon = 500;
        if (SizesEqualWithinEpsilon(paper.size_um(), locale_paper_um,
                                    kSizeEpsilon)) {
          caps.default_paper = paper;
          is_default_found = true;
          break;
        }
      }

      // If no default was set in the PPD or if the locale default is not within
      // the printer's capabilities, select the first on the list.
      if (!is_default_found)
        caps.default_paper = caps.papers[0];
    }
  }

  ppdClose(ppd);

  *printer_info = caps;
  return true;
}

ScopedHttpPtr HttpConnect2(const char* host,
                           int port,
                           http_addrlist_t* addrlist,
                           int family,
                           http_encryption_t encryption,
                           int blocking,
                           int msec,
                           int* cancel) {
  ScopedHttpPtr http;
  if (httpConnect2) {
    http.reset(httpConnect2(host, port,
                            /*addrlist=*/nullptr, AF_UNSPEC, encryption,
                            blocking ? 1 : 0, kCupsTimeout.InMilliseconds(),
                            /*cancel=*/nullptr));
  } else {
    // Continue to use deprecated CUPS calls because because older Linux
    // distribution such as RHEL/CentOS 7 are shipped with CUPS 1.6.
    http.reset(httpConnectEncrypt(host, port, encryption));
  }

  if (!http) {
    LOG(ERROR) << "CP_CUPS: Failed connecting to print server: " << host;
    return nullptr;
  }

  if (!httpConnect2) {
    httpBlocking(http.get(), blocking ? 1 : 0);
  }

  return http;
}

}  // namespace printing