File: printer_capabilities_mac.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (145 lines) | stat: -rw-r--r-- 4,868 bytes parent folder | download | duplicates (9)
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
// Copyright 2018 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/common/printing/printer_capabilities_mac.h"

#import <AppKit/AppKit.h>

#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/no_destructor.h"
#include "base/strings/sys_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "printing/units.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace printing {

namespace {

// On macOS, the custom paper size UI limits the value to 99999.
constexpr int kMacPaperDimensionLimit = 99999 * kPointsPerInch;

PrinterSemanticCapsAndDefaults::Papers& GetTestPapers() {
  static base::NoDestructor<PrinterSemanticCapsAndDefaults::Papers> test_papers;
  return *test_papers;
}

bool IsValidMargin(int margin) {
  return 0 <= margin && margin <= kMacPaperDimensionLimit;
}

}  // namespace

PrinterSemanticCapsAndDefaults::Papers GetMacCustomPaperSizes() {
  if (!GetTestPapers().empty()) {
    return GetTestPapers();
  }

  base::FilePath local_library;
  bool success =
      base::apple::GetUserDirectory(NSLibraryDirectory, &local_library);
  DCHECK(success);

  base::FilePath plist = local_library.Append("Preferences")
                             .Append("com.apple.print.custompapers.plist");
  return internal::GetMacCustomPaperSizesFromFile(plist);
}

void SetMacCustomPaperSizesForTesting(
    const PrinterSemanticCapsAndDefaults::Papers& papers) {
  for (const PrinterSemanticCapsAndDefaults::Paper& paper : papers)
    DCHECK_EQ("", paper.vendor_id());

  GetTestPapers() = papers;
}

namespace internal {

PrinterSemanticCapsAndDefaults::Papers GetMacCustomPaperSizesFromFile(
    const base::FilePath& path) {
  PrinterSemanticCapsAndDefaults::Papers custom_paper_sizes;

  NSDictionary* custom_papers_dict;
  {
    base::ScopedBlockingCall scoped_block(FROM_HERE,
                                          base::BlockingType::MAY_BLOCK);
    custom_papers_dict = [[NSDictionary alloc]
        initWithContentsOfURL:base::apple::FilePathToNSURL(path)
                        error:nil];
    if (!custom_papers_dict) {
      return custom_paper_sizes;
    }
  }

  for (id key in custom_papers_dict) {
    NSDictionary* paper = base::apple::ObjCCast<NSDictionary>(
        [custom_papers_dict objectForKey:key]);
    if (!paper) {
      continue;
    }

    int size_width = [paper[@"width"] intValue];
    int size_height = [paper[@"height"] intValue];
    if (size_width <= 0 || size_height <= 0 ||
        size_width > kMacPaperDimensionLimit ||
        size_height > kMacPaperDimensionLimit) {
      continue;
    }

    NSString* name = paper[@"name"];
    if (![name isKindOfClass:[NSString class]] || name.length == 0) {
      continue;
    }

    gfx::Size size_microns(
        ConvertUnit(size_width, kPointsPerInch, kMicronsPerInch),
        ConvertUnit(size_height, kPointsPerInch, kMicronsPerInch));

    int margin_left = [paper[@"left"] intValue];
    int margin_bottom = [paper[@"bottom"] intValue];
    int margin_right = [paper[@"right"] intValue];
    int margin_top = [paper[@"top"] intValue];
    if (!IsValidMargin(margin_left) || !IsValidMargin(margin_bottom) ||
        !IsValidMargin(margin_right) || !IsValidMargin(margin_top)) {
      continue;
    }

    // Since each margin must be less than `kMacPaperDimensionLimit`, there
    // won't be any integer overflow here.
    int margin_width = margin_left + margin_right;
    int margin_height = margin_bottom + margin_top;
    if (margin_width >= size_width || margin_height >= size_height) {
      continue;
    }

    // The printable area should now always be non-empty and always in-bounds of
    // the paper size.
    int printable_area_width = size_width - margin_width;
    int printable_area_height = size_height - margin_height;
    gfx::Rect printable_area_microns(
        ConvertUnit(margin_left, kPointsPerInch, kMicronsPerInch),
        ConvertUnit(margin_bottom, kPointsPerInch, kMicronsPerInch),
        ConvertUnit(printable_area_width, kPointsPerInch, kMicronsPerInch),
        ConvertUnit(printable_area_height, kPointsPerInch, kMicronsPerInch));

    custom_paper_sizes.emplace_back(base::SysNSStringToUTF8(name),
                                    /*vendor_id=*/"", size_microns,
                                    printable_area_microns);
  }
  std::sort(custom_paper_sizes.begin(), custom_paper_sizes.end(),
            [](const PrinterSemanticCapsAndDefaults::Paper& a,
               const PrinterSemanticCapsAndDefaults::Paper& b) {
              return a.display_name() < b.display_name();
            });

  return custom_paper_sizes;
}

}  // namespace internal

}  // namespace printing