[email protected] | ce6f591 | 2012-05-16 23:07:30 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/backend/cups_helper.h" |
| 6 | |
[email protected] | eb08bbe5 | 2012-09-07 09:32:01 | [diff] [blame] | 7 | #include <cups/ppd.h> |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | eb08bbe5 | 2012-09-07 09:32:01 | [diff] [blame] | 9 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 13 | #include "base/base_paths.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 896d161f | 2013-06-11 22:52:24 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 18 | #include "base/strings/string_split.h" |
[email protected] | 79fe227 | 2013-07-13 20:01:40 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 20 | #include "base/values.h" |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 21 | #include "printing/backend/print_backend.h" |
| 22 | #include "printing/backend/print_backend_consts.h" |
[email protected] | 117eb82d | 2014-06-13 22:00:57 | [diff] [blame] | 23 | #include "printing/units.h" |
[email protected] | 79fe227 | 2013-07-13 20:01:40 | [diff] [blame] | 24 | #include "url/gurl.h" |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 25 | |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 26 | using base::EqualsCaseInsensitiveASCII; |
| 27 | |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 28 | namespace printing { |
| 29 | |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 30 | // This section contains helper code for PPD parsing for semantic capabilities. |
| 31 | namespace { |
| 32 | |
| 33 | const char kColorDevice[] = "ColorDevice"; |
| 34 | const char kColorModel[] = "ColorModel"; |
| 35 | const char kColorMode[] = "ColorMode"; |
| 36 | const char kProcessColorModel[] = "ProcessColorModel"; |
| 37 | const char kPrintoutMode[] = "PrintoutMode"; |
| 38 | const char kDraftGray[] = "Draft.Gray"; |
| 39 | const char kHighGray[] = "High.Gray"; |
| 40 | |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 41 | constexpr char kDuplex[] = "Duplex"; |
| 42 | constexpr char kDuplexNone[] = "None"; |
| 43 | constexpr char kDuplexTumble[] = "DuplexTumble"; |
| 44 | constexpr char kPageSize[] = "PageSize"; |
| 45 | |
| 46 | // Brother printer specific options. |
| 47 | constexpr char kBrotherDuplex[] = "BRDuplex"; |
| 48 | constexpr char kBrotherMonoColor[] = "BRMonoColor"; |
| 49 | constexpr char kBrotherPrintQuality[] = "BRPrintQuality"; |
[email protected] | 117eb82d | 2014-06-13 22:00:57 | [diff] [blame] | 50 | |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 51 | // Samsung printer specific options. |
| 52 | constexpr char kSamsungColorTrue[] = "True"; |
| 53 | constexpr char kSamsungColorFalse[] = "False"; |
| 54 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 55 | void ParseLpOptions(const base::FilePath& filepath, |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 56 | base::StringPiece printer_name, |
| 57 | int* num_options, |
| 58 | cups_option_t** options) { |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 59 | std::string content; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 60 | if (!base::ReadFileToString(filepath, &content)) |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 61 | return; |
| 62 | |
| 63 | const char kDest[] = "dest"; |
| 64 | const char kDefault[] = "default"; |
| 65 | const size_t kDestLen = sizeof(kDest) - 1; |
| 66 | const size_t kDefaultLen = sizeof(kDefault) - 1; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 67 | |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 68 | for (base::StringPiece line : |
| 69 | base::SplitStringPiece(content, "\n", base::KEEP_WHITESPACE, |
| 70 | base::SPLIT_WANT_NONEMPTY)) { |
| 71 | if (base::StartsWith(line, base::StringPiece(kDefault, kDefaultLen), |
| 72 | base::CompareCase::INSENSITIVE_ASCII) && |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 73 | isspace(line[kDefaultLen])) { |
| 74 | line = line.substr(kDefaultLen); |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 75 | } else if (base::StartsWith(line, base::StringPiece(kDest, kDestLen), |
| 76 | base::CompareCase::INSENSITIVE_ASCII) && |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 77 | isspace(line[kDestLen])) { |
| 78 | line = line.substr(kDestLen); |
| 79 | } else { |
| 80 | continue; |
| 81 | } |
| 82 | |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 83 | line = base::TrimWhitespaceASCII(line, base::TRIM_ALL); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 84 | if (line.empty()) |
| 85 | continue; |
| 86 | |
| 87 | size_t space_found = line.find(' '); |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 88 | if (space_found == base::StringPiece::npos) |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 89 | continue; |
| 90 | |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 91 | base::StringPiece name = line.substr(0, space_found); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 92 | if (name.empty()) |
| 93 | continue; |
| 94 | |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 95 | if (!EqualsCaseInsensitiveASCII(printer_name, name)) |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 96 | continue; // This is not the required printer. |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 97 | |
| 98 | line = line.substr(space_found + 1); |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 99 | // Remove extra spaces. |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 100 | line = base::TrimWhitespaceASCII(line, base::TRIM_ALL); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 101 | if (line.empty()) |
| 102 | continue; |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 103 | |
brettw | 8a80090 | 2015-07-10 18:28:33 | [diff] [blame] | 104 | // Parse the selected printer custom options. Need to pass a |
| 105 | // null-terminated string. |
| 106 | *num_options = cupsParseOptions(line.as_string().c_str(), 0, options); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 110 | void MarkLpOptions(base::StringPiece printer_name, ppd_file_t** ppd) { |
| 111 | cups_option_t* options = nullptr; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 112 | int num_options = 0; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 113 | |
| 114 | const char kSystemLpOptionPath[] = "/etc/cups/lpoptions"; |
| 115 | const char kUserLpOptionPath[] = ".cups/lpoptions"; |
| 116 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 117 | std::vector<base::FilePath> file_locations; |
| 118 | file_locations.push_back(base::FilePath(kSystemLpOptionPath)); |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 119 | base::FilePath homedir; |
Avi Drissman | ea15ea0 | 2018-05-07 18:55:12 | [diff] [blame] | 120 | base::PathService::Get(base::DIR_HOME, &homedir); |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 121 | file_locations.push_back(base::FilePath(homedir.Append(kUserLpOptionPath))); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 122 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 123 | for (const base::FilePath& location : file_locations) { |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 124 | num_options = 0; |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 125 | options = nullptr; |
| 126 | ParseLpOptions(location, printer_name, &num_options, &options); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 127 | if (num_options > 0 && options) { |
| 128 | cupsMarkOptions(*ppd, num_options, options); |
| 129 | cupsFreeOptions(num_options, options); |
| 130 | } |
| 131 | } |
| 132 | } |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 133 | |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 134 | void GetDuplexSettings(ppd_file_t* ppd, |
| 135 | bool* duplex_capable, |
| 136 | DuplexMode* duplex_default) { |
| 137 | ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); |
| 138 | if (!duplex_choice) { |
| 139 | ppd_option_t* option = ppdFindOption(ppd, kDuplex); |
| 140 | if (!option) |
| 141 | option = ppdFindOption(ppd, kBrotherDuplex); |
| 142 | if (!option) |
| 143 | return; |
| 144 | |
| 145 | duplex_choice = ppdFindChoice(option, option->defchoice); |
| 146 | } |
| 147 | |
| 148 | if (!duplex_choice) |
| 149 | return; |
| 150 | |
| 151 | *duplex_capable = true; |
| 152 | const char* choice = duplex_choice->choice; |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 153 | if (EqualsCaseInsensitiveASCII(choice, kDuplexNone)) { |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 154 | *duplex_default = SIMPLEX; |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 155 | } else if (EqualsCaseInsensitiveASCII(choice, kDuplexTumble)) { |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 156 | *duplex_default = SHORT_EDGE; |
| 157 | } else { |
| 158 | *duplex_default = LONG_EDGE; |
| 159 | } |
| 160 | } |
| 161 | |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 162 | bool GetBasicColorModelSettings(ppd_file_t* ppd, |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 163 | ColorModel* color_model_for_black, |
| 164 | ColorModel* color_model_for_color, |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 165 | bool* color_is_default) { |
| 166 | ppd_option_t* color_model = ppdFindOption(ppd, kColorModel); |
| 167 | if (!color_model) |
| 168 | return false; |
| 169 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 170 | if (ppdFindChoice(color_model, kBlack)) |
| 171 | *color_model_for_black = BLACK; |
| 172 | else if (ppdFindChoice(color_model, kGray)) |
| 173 | *color_model_for_black = GRAY; |
| 174 | else if (ppdFindChoice(color_model, kGrayscale)) |
| 175 | *color_model_for_black = GRAYSCALE; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 176 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 177 | if (ppdFindChoice(color_model, kColor)) |
| 178 | *color_model_for_color = COLOR; |
| 179 | else if (ppdFindChoice(color_model, kCMYK)) |
| 180 | *color_model_for_color = CMYK; |
| 181 | else if (ppdFindChoice(color_model, kRGB)) |
| 182 | *color_model_for_color = RGB; |
| 183 | else if (ppdFindChoice(color_model, kRGBA)) |
| 184 | *color_model_for_color = RGBA; |
| 185 | else if (ppdFindChoice(color_model, kRGB16)) |
| 186 | *color_model_for_color = RGB16; |
| 187 | else if (ppdFindChoice(color_model, kCMY)) |
| 188 | *color_model_for_color = CMY; |
| 189 | else if (ppdFindChoice(color_model, kKCMY)) |
| 190 | *color_model_for_color = KCMY; |
| 191 | else if (ppdFindChoice(color_model, kCMY_K)) |
| 192 | *color_model_for_color = CMY_K; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 193 | |
| 194 | ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorModel); |
| 195 | if (!marked_choice) |
| 196 | marked_choice = ppdFindChoice(color_model, color_model->defchoice); |
| 197 | |
| 198 | if (marked_choice) { |
| 199 | *color_is_default = |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 200 | !EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && |
| 201 | !EqualsCaseInsensitiveASCII(marked_choice->choice, kGray) && |
| 202 | !EqualsCaseInsensitiveASCII(marked_choice->choice, kGrayscale); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool GetPrintOutModeColorSettings(ppd_file_t* ppd, |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 208 | ColorModel* color_model_for_black, |
| 209 | ColorModel* color_model_for_color, |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 210 | bool* color_is_default) { |
| 211 | ppd_option_t* printout_mode = ppdFindOption(ppd, kPrintoutMode); |
| 212 | if (!printout_mode) |
| 213 | return false; |
| 214 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 215 | *color_model_for_color = PRINTOUTMODE_NORMAL; |
| 216 | *color_model_for_black = PRINTOUTMODE_NORMAL; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 217 | |
| 218 | // Check to see if NORMAL_GRAY value is supported by PrintoutMode. |
| 219 | // If NORMAL_GRAY is not supported, NORMAL value is used to |
| 220 | // represent grayscale. If NORMAL_GRAY is supported, NORMAL is used to |
| 221 | // represent color. |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 222 | if (ppdFindChoice(printout_mode, kNormalGray)) |
| 223 | *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 224 | |
| 225 | // Get the default marked choice to identify the default color setting |
| 226 | // value. |
| 227 | ppd_choice_t* printout_mode_choice = ppdFindMarkedChoice(ppd, kPrintoutMode); |
| 228 | if (!printout_mode_choice) { |
| 229 | printout_mode_choice = ppdFindChoice(printout_mode, |
| 230 | printout_mode->defchoice); |
| 231 | } |
| 232 | if (printout_mode_choice) { |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 233 | if (EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kNormalGray) || |
| 234 | EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kHighGray) || |
| 235 | EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kDraftGray)) { |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 236 | *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 237 | *color_is_default = false; |
| 238 | } |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | bool GetColorModeSettings(ppd_file_t* ppd, |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 244 | ColorModel* color_model_for_black, |
| 245 | ColorModel* color_model_for_color, |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 246 | bool* color_is_default) { |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 247 | // Samsung printers use "ColorMode" attribute in their PPDs. |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 248 | ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); |
| 249 | if (!color_mode_option) |
| 250 | return false; |
| 251 | |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 252 | if (ppdFindChoice(color_mode_option, kColor) || |
| 253 | ppdFindChoice(color_mode_option, kSamsungColorTrue)) { |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 254 | *color_model_for_color = COLORMODE_COLOR; |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 255 | } |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 256 | |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 257 | if (ppdFindChoice(color_mode_option, kMonochrome) || |
| 258 | ppdFindChoice(color_mode_option, kSamsungColorFalse)) { |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 259 | *color_model_for_black = COLORMODE_MONOCHROME; |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 260 | } |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 261 | |
| 262 | ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 263 | if (!mode_choice) { |
| 264 | mode_choice = ppdFindChoice(color_mode_option, |
| 265 | color_mode_option->defchoice); |
| 266 | } |
| 267 | |
| 268 | if (mode_choice) { |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 269 | *color_is_default = |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 270 | EqualsCaseInsensitiveASCII(mode_choice->choice, kColor) || |
| 271 | EqualsCaseInsensitiveASCII(mode_choice->choice, kSamsungColorTrue); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 276 | bool GetBrotherColorSettings(ppd_file_t* ppd, |
| 277 | ColorModel* color_model_for_black, |
| 278 | ColorModel* color_model_for_color, |
| 279 | bool* color_is_default) { |
| 280 | // Some Brother printers use "BRMonoColor" attribute in their PPDs. |
| 281 | // Some Brother printers use "BRPrintQuality" attribute in their PPDs. |
| 282 | ppd_option_t* color_mode_option = ppdFindOption(ppd, kBrotherMonoColor); |
| 283 | if (!color_mode_option) |
| 284 | color_mode_option = ppdFindOption(ppd, kBrotherPrintQuality); |
| 285 | if (!color_mode_option) |
| 286 | return false; |
| 287 | |
thestig | ca4af0b | 2017-03-15 18:28:48 | [diff] [blame] | 288 | if (ppdFindChoice(color_mode_option, kFullColor)) |
| 289 | *color_model_for_color = BROTHER_CUPS_COLOR; |
| 290 | else if (ppdFindChoice(color_mode_option, kColor)) |
| 291 | *color_model_for_color = BROTHER_BRSCRIPT3_COLOR; |
| 292 | |
| 293 | if (ppdFindChoice(color_mode_option, kMono)) |
| 294 | *color_model_for_black = BROTHER_CUPS_MONO; |
| 295 | else if (ppdFindChoice(color_mode_option, kBlack)) |
| 296 | *color_model_for_black = BROTHER_BRSCRIPT3_BLACK; |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 297 | |
| 298 | ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 299 | if (!marked_choice) { |
| 300 | marked_choice = |
| 301 | ppdFindChoice(color_mode_option, color_mode_option->defchoice); |
| 302 | } |
| 303 | if (marked_choice) { |
| 304 | *color_is_default = |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 305 | !EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && |
| 306 | !EqualsCaseInsensitiveASCII(marked_choice->choice, kMono); |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 307 | } |
| 308 | return true; |
| 309 | } |
| 310 | |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 311 | bool GetHPColorSettings(ppd_file_t* ppd, |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 312 | ColorModel* color_model_for_black, |
| 313 | ColorModel* color_model_for_color, |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 314 | bool* color_is_default) { |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 315 | // HP printers use "Color/Color Model" attribute in their PPDs. |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 316 | ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 317 | if (!color_mode_option) |
| 318 | return false; |
| 319 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 320 | if (ppdFindChoice(color_mode_option, kColor)) |
| 321 | *color_model_for_color = HP_COLOR_COLOR; |
| 322 | if (ppdFindChoice(color_mode_option, kBlack)) |
| 323 | *color_model_for_black = HP_COLOR_BLACK; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 324 | |
| 325 | ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 326 | if (!mode_choice) { |
| 327 | mode_choice = ppdFindChoice(color_mode_option, |
| 328 | color_mode_option->defchoice); |
| 329 | } |
| 330 | if (mode_choice) { |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 331 | *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | bool GetProcessColorModelSettings(ppd_file_t* ppd, |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 337 | ColorModel* color_model_for_black, |
| 338 | ColorModel* color_model_for_color, |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 339 | bool* color_is_default) { |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 340 | // Canon printers use "ProcessColorModel" attribute in their PPDs. |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 341 | ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); |
| 342 | if (!color_mode_option) |
| 343 | return false; |
| 344 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 345 | if (ppdFindChoice(color_mode_option, kRGB)) |
| 346 | *color_model_for_color = PROCESSCOLORMODEL_RGB; |
| 347 | else if (ppdFindChoice(color_mode_option, kCMYK)) |
| 348 | *color_model_for_color = PROCESSCOLORMODEL_CMYK; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 349 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 350 | if (ppdFindChoice(color_mode_option, kGreyscale)) |
| 351 | *color_model_for_black = PROCESSCOLORMODEL_GREYSCALE; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 352 | |
| 353 | ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kProcessColorModel); |
| 354 | if (!mode_choice) { |
| 355 | mode_choice = ppdFindChoice(color_mode_option, |
| 356 | color_mode_option->defchoice); |
| 357 | } |
| 358 | |
| 359 | if (mode_choice) { |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 360 | *color_is_default = |
thestig | 91d04b6 | 2017-03-23 19:39:13 | [diff] [blame] | 361 | !EqualsCaseInsensitiveASCII(mode_choice->choice, kGreyscale); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | bool GetColorModelSettings(ppd_file_t* ppd, |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 367 | ColorModel* cm_black, |
| 368 | ColorModel* cm_color, |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 369 | bool* is_color) { |
| 370 | bool is_color_device = false; |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 371 | ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 372 | if (attr && attr->value) |
| 373 | is_color_device = ppd->color_device; |
| 374 | |
| 375 | *is_color = is_color_device; |
| 376 | return (is_color_device && |
| 377 | GetBasicColorModelSettings(ppd, cm_black, cm_color, is_color)) || |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 378 | GetPrintOutModeColorSettings(ppd, cm_black, cm_color, is_color) || |
| 379 | GetColorModeSettings(ppd, cm_black, cm_color, is_color) || |
| 380 | GetHPColorSettings(ppd, cm_black, cm_color, is_color) || |
| 381 | GetBrotherColorSettings(ppd, cm_black, cm_color, is_color) || |
| 382 | GetProcessColorModelSettings(ppd, cm_black, cm_color, is_color); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 385 | // Default port for IPP print servers. |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 386 | const int kDefaultIPPServerPort = 631; |
| 387 | |
| 388 | } // namespace |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 389 | |
| 390 | // Helper wrapper around http_t structure, with connection and cleanup |
| 391 | // functionality. |
[email protected] | ce6f591 | 2012-05-16 23:07:30 | [diff] [blame] | 392 | HttpConnectionCUPS::HttpConnectionCUPS(const GURL& print_server_url, |
| 393 | http_encryption_t encryption) |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 394 | : http_(nullptr) { |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 395 | // If we have an empty url, use default print server. |
| 396 | if (print_server_url.is_empty()) |
| 397 | return; |
| 398 | |
| 399 | int port = print_server_url.IntPort(); |
[email protected] | 04307e0 | 2014-05-01 18:01:49 | [diff] [blame] | 400 | if (port == url::PORT_UNSPECIFIED) |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 401 | port = kDefaultIPPServerPort; |
| 402 | |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 403 | http_ = httpConnectEncrypt(print_server_url.host().c_str(), port, encryption); |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 404 | if (!http_) { |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 405 | LOG(ERROR) << "CP_CUPS: Failed connecting to print server: " |
| 406 | << print_server_url; |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | HttpConnectionCUPS::~HttpConnectionCUPS() { |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 411 | if (http_) |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 412 | httpClose(http_); |
| 413 | } |
| 414 | |
[email protected] | 5a49cdca0 | 2010-12-06 18:17:39 | [diff] [blame] | 415 | void HttpConnectionCUPS::SetBlocking(bool blocking) { |
| 416 | httpBlocking(http_, blocking ? 1 : 0); |
| 417 | } |
| 418 | |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 419 | http_t* HttpConnectionCUPS::http() { |
| 420 | return http_; |
| 421 | } |
| 422 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 423 | bool ParsePpdCapabilities(base::StringPiece printer_name, |
| 424 | base::StringPiece printer_capabilities, |
| 425 | PrinterSemanticCapsAndDefaults* printer_info) { |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 426 | base::FilePath ppd_file_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 427 | if (!base::CreateTemporaryFile(&ppd_file_path)) |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 428 | return false; |
| 429 | |
| 430 | int data_size = printer_capabilities.length(); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 431 | if (data_size != base::WriteFile( |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 432 | ppd_file_path, |
| 433 | printer_capabilities.data(), |
| 434 | data_size)) { |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 435 | base::DeleteFile(ppd_file_path, false); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
| 439 | ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); |
[email protected] | 117eb82d | 2014-06-13 22:00:57 | [diff] [blame] | 440 | if (!ppd) { |
| 441 | int line = 0; |
| 442 | ppd_status_t ppd_status = ppdLastError(&line); |
| 443 | LOG(ERROR) << "Failed to open PDD file: error " << ppd_status << " at line " |
| 444 | << line << ", " << ppdErrorString(ppd_status); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 445 | return false; |
[email protected] | 117eb82d | 2014-06-13 22:00:57 | [diff] [blame] | 446 | } |
alekseys | 87e0220 | 2014-09-17 21:24:56 | [diff] [blame] | 447 | ppdMarkDefaults(ppd); |
| 448 | MarkLpOptions(printer_name, &ppd); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 449 | |
thestig | d24bc53 | 2016-05-17 21:07:06 | [diff] [blame] | 450 | PrinterSemanticCapsAndDefaults caps; |
[email protected] | dfb4a9d | 2014-06-05 21:44:45 | [diff] [blame] | 451 | caps.collate_capable = true; |
| 452 | caps.collate_default = true; |
| 453 | caps.copies_capable = true; |
| 454 | |
thestig | 1847e48 | 2017-03-11 02:58:31 | [diff] [blame] | 455 | GetDuplexSettings(ppd, &caps.duplex_capable, &caps.duplex_default); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 456 | |
| 457 | bool is_color = false; |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 458 | ColorModel cm_color = UNKNOWN_COLOR_MODEL, cm_black = UNKNOWN_COLOR_MODEL; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 459 | if (!GetColorModelSettings(ppd, &cm_black, &cm_color, &is_color)) { |
| 460 | VLOG(1) << "Unknown printer color model"; |
| 461 | } |
| 462 | |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 463 | caps.color_changeable = ((cm_color != UNKNOWN_COLOR_MODEL) && |
| 464 | (cm_black != UNKNOWN_COLOR_MODEL) && |
| 465 | (cm_color != cm_black)); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 466 | caps.color_default = is_color; |
[email protected] | 4e7ee5a | 2013-10-18 03:13:35 | [diff] [blame] | 467 | caps.color_model = cm_color; |
| 468 | caps.bw_model = cm_black; |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 469 | |
[email protected] | 117eb82d | 2014-06-13 22:00:57 | [diff] [blame] | 470 | if (ppd->num_sizes > 0 && ppd->sizes) { |
| 471 | VLOG(1) << "Paper list size - " << ppd->num_sizes; |
| 472 | ppd_option_t* paper_option = ppdFindOption(ppd, kPageSize); |
| 473 | for (int i = 0; i < ppd->num_sizes; ++i) { |
| 474 | gfx::Size paper_size_microns( |
Lei Zhang | d11ab16 | 2018-06-26 04:28:50 | [diff] [blame^] | 475 | ConvertUnit(ppd->sizes[i].width, kPointsPerInch, kMicronsPerInch), |
| 476 | ConvertUnit(ppd->sizes[i].length, kPointsPerInch, kMicronsPerInch)); |
[email protected] | 117eb82d | 2014-06-13 22:00:57 | [diff] [blame] | 477 | if (paper_size_microns.width() > 0 && paper_size_microns.height() > 0) { |
| 478 | PrinterSemanticCapsAndDefaults::Paper paper; |
| 479 | paper.size_um = paper_size_microns; |
| 480 | paper.vendor_id = ppd->sizes[i].name; |
| 481 | if (paper_option) { |
| 482 | ppd_choice_t* paper_choice = |
| 483 | ppdFindChoice(paper_option, ppd->sizes[i].name); |
| 484 | // Human readable paper name should be UTF-8 encoded, but some PPDs |
| 485 | // do not follow this standard. |
| 486 | if (paper_choice && base::IsStringUTF8(paper_choice->text)) { |
| 487 | paper.display_name = paper_choice->text; |
| 488 | } |
| 489 | } |
| 490 | caps.papers.push_back(paper); |
| 491 | if (i == 0 || ppd->sizes[i].marked) { |
| 492 | caps.default_paper = paper; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 498 | ppdClose(ppd); |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 499 | base::DeleteFile(ppd_file_path, false); |
[email protected] | 8b6098e | 2012-09-07 04:21:21 | [diff] [blame] | 500 | |
| 501 | *printer_info = caps; |
| 502 | return true; |
| 503 | } |
| 504 | |
[email protected] | 8e553f49 | 2010-10-25 20:05:44 | [diff] [blame] | 505 | } // namespace printing |