| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "printing/backend/cups_helper.h" | 5 #include "printing/backend/cups_helper.h" |
| 6 | 6 |
| 7 #include <cups/ppd.h> | 7 #include <cups/ppd.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 const char kColorDevice[] = "ColorDevice"; | 31 const char kColorDevice[] = "ColorDevice"; |
| 32 const char kColorModel[] = "ColorModel"; | 32 const char kColorModel[] = "ColorModel"; |
| 33 const char kColorMode[] = "ColorMode"; | 33 const char kColorMode[] = "ColorMode"; |
| 34 const char kProcessColorModel[] = "ProcessColorModel"; | 34 const char kProcessColorModel[] = "ProcessColorModel"; |
| 35 const char kPrintoutMode[] = "PrintoutMode"; | 35 const char kPrintoutMode[] = "PrintoutMode"; |
| 36 const char kDraftGray[] = "Draft.Gray"; | 36 const char kDraftGray[] = "Draft.Gray"; |
| 37 const char kHighGray[] = "High.Gray"; | 37 const char kHighGray[] = "High.Gray"; |
| 38 | 38 |
| 39 const char kDuplex[] = "Duplex"; | 39 constexpr char kDuplex[] = "Duplex"; |
| 40 const char kDuplexNone[] = "None"; | 40 constexpr char kDuplexNone[] = "None"; |
| 41 const char kPageSize[] = "PageSize"; | 41 constexpr char kDuplexTumble[] = "DuplexTumble"; |
| 42 constexpr char kPageSize[] = "PageSize"; |
| 43 |
| 44 // Brother printer specific options. |
| 45 constexpr char kBrotherDuplex[] = "BRDuplex"; |
| 46 constexpr char kBrotherMonoColor[] = "BRMonoColor"; |
| 47 constexpr char kBrotherPrintQuality[] = "BRPrintQuality"; |
| 48 constexpr char kFullColor[] = "FullColor"; |
| 49 constexpr char kMono[] = "Mono"; |
| 42 | 50 |
| 43 const double kMicronsPerPoint = 10.0f * kHundrethsMMPerInch / kPointsPerInch; | 51 const double kMicronsPerPoint = 10.0f * kHundrethsMMPerInch / kPointsPerInch; |
| 44 | 52 |
| 45 void ParseLpOptions(const base::FilePath& filepath, | 53 void ParseLpOptions(const base::FilePath& filepath, |
| 46 base::StringPiece printer_name, | 54 base::StringPiece printer_name, |
| 47 int* num_options, | 55 int* num_options, |
| 48 cups_option_t** options) { | 56 cups_option_t** options) { |
| 49 std::string content; | 57 std::string content; |
| 50 if (!base::ReadFileToString(filepath, &content)) | 58 if (!base::ReadFileToString(filepath, &content)) |
| 51 return; | 59 return; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 num_options = 0; | 122 num_options = 0; |
| 115 options = nullptr; | 123 options = nullptr; |
| 116 ParseLpOptions(location, printer_name, &num_options, &options); | 124 ParseLpOptions(location, printer_name, &num_options, &options); |
| 117 if (num_options > 0 && options) { | 125 if (num_options > 0 && options) { |
| 118 cupsMarkOptions(*ppd, num_options, options); | 126 cupsMarkOptions(*ppd, num_options, options); |
| 119 cupsFreeOptions(num_options, options); | 127 cupsFreeOptions(num_options, options); |
| 120 } | 128 } |
| 121 } | 129 } |
| 122 } | 130 } |
| 123 | 131 |
| 132 void GetDuplexSettings(ppd_file_t* ppd, |
| 133 bool* duplex_capable, |
| 134 DuplexMode* duplex_default) { |
| 135 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); |
| 136 if (!duplex_choice) { |
| 137 ppd_option_t* option = ppdFindOption(ppd, kDuplex); |
| 138 if (!option) |
| 139 option = ppdFindOption(ppd, kBrotherDuplex); |
| 140 if (!option) |
| 141 return; |
| 142 |
| 143 duplex_choice = ppdFindChoice(option, option->defchoice); |
| 144 } |
| 145 |
| 146 if (!duplex_choice) |
| 147 return; |
| 148 |
| 149 *duplex_capable = true; |
| 150 const char* choice = duplex_choice->choice; |
| 151 if (base::EqualsCaseInsensitiveASCII(choice, kDuplexNone)) { |
| 152 *duplex_default = SIMPLEX; |
| 153 } else if (base::EqualsCaseInsensitiveASCII(choice, kDuplexTumble)) { |
| 154 *duplex_default = SHORT_EDGE; |
| 155 } else { |
| 156 *duplex_default = LONG_EDGE; |
| 157 } |
| 158 } |
| 159 |
| 124 bool GetBasicColorModelSettings(ppd_file_t* ppd, | 160 bool GetBasicColorModelSettings(ppd_file_t* ppd, |
| 125 ColorModel* color_model_for_black, | 161 ColorModel* color_model_for_black, |
| 126 ColorModel* color_model_for_color, | 162 ColorModel* color_model_for_color, |
| 127 bool* color_is_default) { | 163 bool* color_is_default) { |
| 128 ppd_option_t* color_model = ppdFindOption(ppd, kColorModel); | 164 ppd_option_t* color_model = ppdFindOption(ppd, kColorModel); |
| 129 if (!color_model) | 165 if (!color_model) |
| 130 return false; | 166 return false; |
| 131 | 167 |
| 132 if (ppdFindChoice(color_model, kBlack)) | 168 if (ppdFindChoice(color_model, kBlack)) |
| 133 *color_model_for_black = BLACK; | 169 *color_model_for_black = BLACK; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 *color_is_default = false; | 238 *color_is_default = false; |
| 203 } | 239 } |
| 204 } | 240 } |
| 205 return true; | 241 return true; |
| 206 } | 242 } |
| 207 | 243 |
| 208 bool GetColorModeSettings(ppd_file_t* ppd, | 244 bool GetColorModeSettings(ppd_file_t* ppd, |
| 209 ColorModel* color_model_for_black, | 245 ColorModel* color_model_for_black, |
| 210 ColorModel* color_model_for_color, | 246 ColorModel* color_model_for_color, |
| 211 bool* color_is_default) { | 247 bool* color_is_default) { |
| 212 // Samsung printers use "ColorMode" attribute in their ppds. | 248 // Samsung printers use "ColorMode" attribute in their PPDs. |
| 213 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); | 249 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); |
| 214 if (!color_mode_option) | 250 if (!color_mode_option) |
| 215 return false; | 251 return false; |
| 216 | 252 |
| 217 if (ppdFindChoice(color_mode_option, kColor)) | 253 if (ppdFindChoice(color_mode_option, kColor)) |
| 218 *color_model_for_color = COLORMODE_COLOR; | 254 *color_model_for_color = COLORMODE_COLOR; |
| 219 | 255 |
| 220 if (ppdFindChoice(color_mode_option, kMonochrome)) | 256 if (ppdFindChoice(color_mode_option, kMonochrome)) |
| 221 *color_model_for_black = COLORMODE_MONOCHROME; | 257 *color_model_for_black = COLORMODE_MONOCHROME; |
| 222 | 258 |
| 223 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); | 259 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 224 if (!mode_choice) { | 260 if (!mode_choice) { |
| 225 mode_choice = ppdFindChoice(color_mode_option, | 261 mode_choice = ppdFindChoice(color_mode_option, |
| 226 color_mode_option->defchoice); | 262 color_mode_option->defchoice); |
| 227 } | 263 } |
| 228 | 264 |
| 229 if (mode_choice) { | 265 if (mode_choice) { |
| 230 *color_is_default = | 266 *color_is_default = |
| 231 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); | 267 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); |
| 232 } | 268 } |
| 233 return true; | 269 return true; |
| 234 } | 270 } |
| 235 | 271 |
| 272 bool GetBrotherColorSettings(ppd_file_t* ppd, |
| 273 ColorModel* color_model_for_black, |
| 274 ColorModel* color_model_for_color, |
| 275 bool* color_is_default) { |
| 276 // Some Brother printers use "BRMonoColor" attribute in their PPDs. |
| 277 // Some Brother printers use "BRPrintQuality" attribute in their PPDs. |
| 278 ppd_option_t* color_mode_option = ppdFindOption(ppd, kBrotherMonoColor); |
| 279 if (!color_mode_option) |
| 280 color_mode_option = ppdFindOption(ppd, kBrotherPrintQuality); |
| 281 if (!color_mode_option) |
| 282 return false; |
| 283 |
| 284 if (ppdFindChoice(color_mode_option, kFullColor) || |
| 285 ppdFindChoice(color_mode_option, kColor)) { |
| 286 *color_model_for_color = BROTHER_COLOR_COLOR; |
| 287 } |
| 288 if (ppdFindChoice(color_mode_option, kBlack) || |
| 289 ppdFindChoice(color_mode_option, kMono)) { |
| 290 *color_model_for_black = BROTHER_COLOR_BLACK; |
| 291 } |
| 292 |
| 293 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 294 if (!marked_choice) { |
| 295 marked_choice = |
| 296 ppdFindChoice(color_mode_option, color_mode_option->defchoice); |
| 297 } |
| 298 if (marked_choice) { |
| 299 *color_is_default = |
| 300 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && |
| 301 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kMono); |
| 302 } |
| 303 return true; |
| 304 } |
| 305 |
| 236 bool GetHPColorSettings(ppd_file_t* ppd, | 306 bool GetHPColorSettings(ppd_file_t* ppd, |
| 237 ColorModel* color_model_for_black, | 307 ColorModel* color_model_for_black, |
| 238 ColorModel* color_model_for_color, | 308 ColorModel* color_model_for_color, |
| 239 bool* color_is_default) { | 309 bool* color_is_default) { |
| 240 // HP printers use "Color/Color Model" attribute in their ppds. | 310 // HP printers use "Color/Color Model" attribute in their PPDs. |
| 241 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor); | 311 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor); |
| 242 if (!color_mode_option) | 312 if (!color_mode_option) |
| 243 return false; | 313 return false; |
| 244 | 314 |
| 245 if (ppdFindChoice(color_mode_option, kColor)) | 315 if (ppdFindChoice(color_mode_option, kColor)) |
| 246 *color_model_for_color = HP_COLOR_COLOR; | 316 *color_model_for_color = HP_COLOR_COLOR; |
| 247 if (ppdFindChoice(color_mode_option, kBlack)) | 317 if (ppdFindChoice(color_mode_option, kBlack)) |
| 248 *color_model_for_black = HP_COLOR_BLACK; | 318 *color_model_for_black = HP_COLOR_BLACK; |
| 249 | 319 |
| 250 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); | 320 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 251 if (!mode_choice) { | 321 if (!mode_choice) { |
| 252 mode_choice = ppdFindChoice(color_mode_option, | 322 mode_choice = ppdFindChoice(color_mode_option, |
| 253 color_mode_option->defchoice); | 323 color_mode_option->defchoice); |
| 254 } | 324 } |
| 255 if (mode_choice) { | 325 if (mode_choice) { |
| 256 *color_is_default = | 326 *color_is_default = |
| 257 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); | 327 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); |
| 258 } | 328 } |
| 259 return true; | 329 return true; |
| 260 } | 330 } |
| 261 | 331 |
| 262 bool GetProcessColorModelSettings(ppd_file_t* ppd, | 332 bool GetProcessColorModelSettings(ppd_file_t* ppd, |
| 263 ColorModel* color_model_for_black, | 333 ColorModel* color_model_for_black, |
| 264 ColorModel* color_model_for_color, | 334 ColorModel* color_model_for_color, |
| 265 bool* color_is_default) { | 335 bool* color_is_default) { |
| 266 // Canon printers use "ProcessColorModel" attribute in their ppds. | 336 // Canon printers use "ProcessColorModel" attribute in their PPDs. |
| 267 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); | 337 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); |
| 268 if (!color_mode_option) | 338 if (!color_mode_option) |
| 269 return false; | 339 return false; |
| 270 | 340 |
| 271 if (ppdFindChoice(color_mode_option, kRGB)) | 341 if (ppdFindChoice(color_mode_option, kRGB)) |
| 272 *color_model_for_color = PROCESSCOLORMODEL_RGB; | 342 *color_model_for_color = PROCESSCOLORMODEL_RGB; |
| 273 else if (ppdFindChoice(color_mode_option, kCMYK)) | 343 else if (ppdFindChoice(color_mode_option, kCMYK)) |
| 274 *color_model_for_color = PROCESSCOLORMODEL_CMYK; | 344 *color_model_for_color = PROCESSCOLORMODEL_CMYK; |
| 275 | 345 |
| 276 if (ppdFindChoice(color_mode_option, kGreyscale)) | 346 if (ppdFindChoice(color_mode_option, kGreyscale)) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 294 ColorModel* cm_color, | 364 ColorModel* cm_color, |
| 295 bool* is_color) { | 365 bool* is_color) { |
| 296 bool is_color_device = false; | 366 bool is_color_device = false; |
| 297 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr); | 367 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr); |
| 298 if (attr && attr->value) | 368 if (attr && attr->value) |
| 299 is_color_device = ppd->color_device; | 369 is_color_device = ppd->color_device; |
| 300 | 370 |
| 301 *is_color = is_color_device; | 371 *is_color = is_color_device; |
| 302 return (is_color_device && | 372 return (is_color_device && |
| 303 GetBasicColorModelSettings(ppd, cm_black, cm_color, is_color)) || | 373 GetBasicColorModelSettings(ppd, cm_black, cm_color, is_color)) || |
| 304 GetPrintOutModeColorSettings(ppd, cm_black, cm_color, is_color) || | 374 GetPrintOutModeColorSettings(ppd, cm_black, cm_color, is_color) || |
| 305 GetColorModeSettings(ppd, cm_black, cm_color, is_color) || | 375 GetColorModeSettings(ppd, cm_black, cm_color, is_color) || |
| 306 GetHPColorSettings(ppd, cm_black, cm_color, is_color) || | 376 GetHPColorSettings(ppd, cm_black, cm_color, is_color) || |
| 307 GetProcessColorModelSettings(ppd, cm_black, cm_color, is_color); | 377 GetBrotherColorSettings(ppd, cm_black, cm_color, is_color) || |
| 378 GetProcessColorModelSettings(ppd, cm_black, cm_color, is_color); |
| 308 } | 379 } |
| 309 | 380 |
| 310 // Default port for IPP print servers. | 381 // Default port for IPP print servers. |
| 311 const int kDefaultIPPServerPort = 631; | 382 const int kDefaultIPPServerPort = 631; |
| 312 | 383 |
| 313 } // namespace | 384 } // namespace |
| 314 | 385 |
| 315 // Helper wrapper around http_t structure, with connection and cleanup | 386 // Helper wrapper around http_t structure, with connection and cleanup |
| 316 // functionality. | 387 // functionality. |
| 317 HttpConnectionCUPS::HttpConnectionCUPS(const GURL& print_server_url, | 388 HttpConnectionCUPS::HttpConnectionCUPS(const GURL& print_server_url, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 return false; | 441 return false; |
| 371 } | 442 } |
| 372 ppdMarkDefaults(ppd); | 443 ppdMarkDefaults(ppd); |
| 373 MarkLpOptions(printer_name, &ppd); | 444 MarkLpOptions(printer_name, &ppd); |
| 374 | 445 |
| 375 PrinterSemanticCapsAndDefaults caps; | 446 PrinterSemanticCapsAndDefaults caps; |
| 376 caps.collate_capable = true; | 447 caps.collate_capable = true; |
| 377 caps.collate_default = true; | 448 caps.collate_default = true; |
| 378 caps.copies_capable = true; | 449 caps.copies_capable = true; |
| 379 | 450 |
| 380 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); | 451 GetDuplexSettings(ppd, &caps.duplex_capable, &caps.duplex_default); |
| 381 if (!duplex_choice) { | |
| 382 ppd_option_t* option = ppdFindOption(ppd, kDuplex); | |
| 383 if (option) | |
| 384 duplex_choice = ppdFindChoice(option, option->defchoice); | |
| 385 } | |
| 386 | |
| 387 if (duplex_choice) { | |
| 388 caps.duplex_capable = true; | |
| 389 if (!base::EqualsCaseInsensitiveASCII(duplex_choice->choice, kDuplexNone)) | |
| 390 caps.duplex_default = LONG_EDGE; | |
| 391 else | |
| 392 caps.duplex_default = SIMPLEX; | |
| 393 } | |
| 394 | 452 |
| 395 bool is_color = false; | 453 bool is_color = false; |
| 396 ColorModel cm_color = UNKNOWN_COLOR_MODEL, cm_black = UNKNOWN_COLOR_MODEL; | 454 ColorModel cm_color = UNKNOWN_COLOR_MODEL, cm_black = UNKNOWN_COLOR_MODEL; |
| 397 if (!GetColorModelSettings(ppd, &cm_black, &cm_color, &is_color)) { | 455 if (!GetColorModelSettings(ppd, &cm_black, &cm_color, &is_color)) { |
| 398 VLOG(1) << "Unknown printer color model"; | 456 VLOG(1) << "Unknown printer color model"; |
| 399 } | 457 } |
| 400 | 458 |
| 401 caps.color_changeable = ((cm_color != UNKNOWN_COLOR_MODEL) && | 459 caps.color_changeable = ((cm_color != UNKNOWN_COLOR_MODEL) && |
| 402 (cm_black != UNKNOWN_COLOR_MODEL) && | 460 (cm_black != UNKNOWN_COLOR_MODEL) && |
| 403 (cm_color != cm_black)); | 461 (cm_color != cm_black)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 434 } | 492 } |
| 435 | 493 |
| 436 ppdClose(ppd); | 494 ppdClose(ppd); |
| 437 base::DeleteFile(ppd_file_path, false); | 495 base::DeleteFile(ppd_file_path, false); |
| 438 | 496 |
| 439 *printer_info = caps; | 497 *printer_info = caps; |
| 440 return true; | 498 return true; |
| 441 } | 499 } |
| 442 | 500 |
| 443 } // namespace printing | 501 } // namespace printing |
| OLD | NEW |