| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "printing/backend/print_backend.h" | 21 #include "printing/backend/print_backend.h" |
| 22 #include "printing/backend/print_backend_consts.h" | 22 #include "printing/backend/print_backend_consts.h" |
| 23 #include "printing/units.h" | 23 #include "printing/units.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 | 25 |
| 26 using base::EqualsCaseInsensitiveASCII; |
| 27 |
| 26 namespace printing { | 28 namespace printing { |
| 27 | 29 |
| 28 // This section contains helper code for PPD parsing for semantic capabilities. | 30 // This section contains helper code for PPD parsing for semantic capabilities. |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 const char kColorDevice[] = "ColorDevice"; | 33 const char kColorDevice[] = "ColorDevice"; |
| 32 const char kColorModel[] = "ColorModel"; | 34 const char kColorModel[] = "ColorModel"; |
| 33 const char kColorMode[] = "ColorMode"; | 35 const char kColorMode[] = "ColorMode"; |
| 34 const char kProcessColorModel[] = "ProcessColorModel"; | 36 const char kProcessColorModel[] = "ProcessColorModel"; |
| 35 const char kPrintoutMode[] = "PrintoutMode"; | 37 const char kPrintoutMode[] = "PrintoutMode"; |
| 36 const char kDraftGray[] = "Draft.Gray"; | 38 const char kDraftGray[] = "Draft.Gray"; |
| 37 const char kHighGray[] = "High.Gray"; | 39 const char kHighGray[] = "High.Gray"; |
| 38 | 40 |
| 39 constexpr char kDuplex[] = "Duplex"; | 41 constexpr char kDuplex[] = "Duplex"; |
| 40 constexpr char kDuplexNone[] = "None"; | 42 constexpr char kDuplexNone[] = "None"; |
| 41 constexpr char kDuplexTumble[] = "DuplexTumble"; | 43 constexpr char kDuplexTumble[] = "DuplexTumble"; |
| 42 constexpr char kPageSize[] = "PageSize"; | 44 constexpr char kPageSize[] = "PageSize"; |
| 43 | 45 |
| 44 // Brother printer specific options. | 46 // Brother printer specific options. |
| 45 constexpr char kBrotherDuplex[] = "BRDuplex"; | 47 constexpr char kBrotherDuplex[] = "BRDuplex"; |
| 46 constexpr char kBrotherMonoColor[] = "BRMonoColor"; | 48 constexpr char kBrotherMonoColor[] = "BRMonoColor"; |
| 47 constexpr char kBrotherPrintQuality[] = "BRPrintQuality"; | 49 constexpr char kBrotherPrintQuality[] = "BRPrintQuality"; |
| 48 | 50 |
| 51 // Samsung printer specific options. |
| 52 constexpr char kSamsungColorTrue[] = "True"; |
| 53 constexpr char kSamsungColorFalse[] = "False"; |
| 54 |
| 49 const double kMicronsPerPoint = 10.0f * kHundrethsMMPerInch / kPointsPerInch; | 55 const double kMicronsPerPoint = 10.0f * kHundrethsMMPerInch / kPointsPerInch; |
| 50 | 56 |
| 51 void ParseLpOptions(const base::FilePath& filepath, | 57 void ParseLpOptions(const base::FilePath& filepath, |
| 52 base::StringPiece printer_name, | 58 base::StringPiece printer_name, |
| 53 int* num_options, | 59 int* num_options, |
| 54 cups_option_t** options) { | 60 cups_option_t** options) { |
| 55 std::string content; | 61 std::string content; |
| 56 if (!base::ReadFileToString(filepath, &content)) | 62 if (!base::ReadFileToString(filepath, &content)) |
| 57 return; | 63 return; |
| 58 | 64 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 81 continue; | 87 continue; |
| 82 | 88 |
| 83 size_t space_found = line.find(' '); | 89 size_t space_found = line.find(' '); |
| 84 if (space_found == base::StringPiece::npos) | 90 if (space_found == base::StringPiece::npos) |
| 85 continue; | 91 continue; |
| 86 | 92 |
| 87 base::StringPiece name = line.substr(0, space_found); | 93 base::StringPiece name = line.substr(0, space_found); |
| 88 if (name.empty()) | 94 if (name.empty()) |
| 89 continue; | 95 continue; |
| 90 | 96 |
| 91 if (!base::EqualsCaseInsensitiveASCII(printer_name, name)) | 97 if (!EqualsCaseInsensitiveASCII(printer_name, name)) |
| 92 continue; // This is not the required printer. | 98 continue; // This is not the required printer. |
| 93 | 99 |
| 94 line = line.substr(space_found + 1); | 100 line = line.substr(space_found + 1); |
| 95 // Remove extra spaces. | 101 // Remove extra spaces. |
| 96 line = base::TrimWhitespaceASCII(line, base::TRIM_ALL); | 102 line = base::TrimWhitespaceASCII(line, base::TRIM_ALL); |
| 97 if (line.empty()) | 103 if (line.empty()) |
| 98 continue; | 104 continue; |
| 99 | 105 |
| 100 // Parse the selected printer custom options. Need to pass a | 106 // Parse the selected printer custom options. Need to pass a |
| 101 // null-terminated string. | 107 // null-terminated string. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 return; | 145 return; |
| 140 | 146 |
| 141 duplex_choice = ppdFindChoice(option, option->defchoice); | 147 duplex_choice = ppdFindChoice(option, option->defchoice); |
| 142 } | 148 } |
| 143 | 149 |
| 144 if (!duplex_choice) | 150 if (!duplex_choice) |
| 145 return; | 151 return; |
| 146 | 152 |
| 147 *duplex_capable = true; | 153 *duplex_capable = true; |
| 148 const char* choice = duplex_choice->choice; | 154 const char* choice = duplex_choice->choice; |
| 149 if (base::EqualsCaseInsensitiveASCII(choice, kDuplexNone)) { | 155 if (EqualsCaseInsensitiveASCII(choice, kDuplexNone)) { |
| 150 *duplex_default = SIMPLEX; | 156 *duplex_default = SIMPLEX; |
| 151 } else if (base::EqualsCaseInsensitiveASCII(choice, kDuplexTumble)) { | 157 } else if (EqualsCaseInsensitiveASCII(choice, kDuplexTumble)) { |
| 152 *duplex_default = SHORT_EDGE; | 158 *duplex_default = SHORT_EDGE; |
| 153 } else { | 159 } else { |
| 154 *duplex_default = LONG_EDGE; | 160 *duplex_default = LONG_EDGE; |
| 155 } | 161 } |
| 156 } | 162 } |
| 157 | 163 |
| 158 bool GetBasicColorModelSettings(ppd_file_t* ppd, | 164 bool GetBasicColorModelSettings(ppd_file_t* ppd, |
| 159 ColorModel* color_model_for_black, | 165 ColorModel* color_model_for_black, |
| 160 ColorModel* color_model_for_color, | 166 ColorModel* color_model_for_color, |
| 161 bool* color_is_default) { | 167 bool* color_is_default) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 186 *color_model_for_color = KCMY; | 192 *color_model_for_color = KCMY; |
| 187 else if (ppdFindChoice(color_model, kCMY_K)) | 193 else if (ppdFindChoice(color_model, kCMY_K)) |
| 188 *color_model_for_color = CMY_K; | 194 *color_model_for_color = CMY_K; |
| 189 | 195 |
| 190 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorModel); | 196 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorModel); |
| 191 if (!marked_choice) | 197 if (!marked_choice) |
| 192 marked_choice = ppdFindChoice(color_model, color_model->defchoice); | 198 marked_choice = ppdFindChoice(color_model, color_model->defchoice); |
| 193 | 199 |
| 194 if (marked_choice) { | 200 if (marked_choice) { |
| 195 *color_is_default = | 201 *color_is_default = |
| 196 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && | 202 !EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && |
| 197 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kGray) && | 203 !EqualsCaseInsensitiveASCII(marked_choice->choice, kGray) && |
| 198 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kGrayscale); | 204 !EqualsCaseInsensitiveASCII(marked_choice->choice, kGrayscale); |
| 199 } | 205 } |
| 200 return true; | 206 return true; |
| 201 } | 207 } |
| 202 | 208 |
| 203 bool GetPrintOutModeColorSettings(ppd_file_t* ppd, | 209 bool GetPrintOutModeColorSettings(ppd_file_t* ppd, |
| 204 ColorModel* color_model_for_black, | 210 ColorModel* color_model_for_black, |
| 205 ColorModel* color_model_for_color, | 211 ColorModel* color_model_for_color, |
| 206 bool* color_is_default) { | 212 bool* color_is_default) { |
| 207 ppd_option_t* printout_mode = ppdFindOption(ppd, kPrintoutMode); | 213 ppd_option_t* printout_mode = ppdFindOption(ppd, kPrintoutMode); |
| 208 if (!printout_mode) | 214 if (!printout_mode) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 219 *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; | 225 *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; |
| 220 | 226 |
| 221 // Get the default marked choice to identify the default color setting | 227 // Get the default marked choice to identify the default color setting |
| 222 // value. | 228 // value. |
| 223 ppd_choice_t* printout_mode_choice = ppdFindMarkedChoice(ppd, kPrintoutMode); | 229 ppd_choice_t* printout_mode_choice = ppdFindMarkedChoice(ppd, kPrintoutMode); |
| 224 if (!printout_mode_choice) { | 230 if (!printout_mode_choice) { |
| 225 printout_mode_choice = ppdFindChoice(printout_mode, | 231 printout_mode_choice = ppdFindChoice(printout_mode, |
| 226 printout_mode->defchoice); | 232 printout_mode->defchoice); |
| 227 } | 233 } |
| 228 if (printout_mode_choice) { | 234 if (printout_mode_choice) { |
| 229 if (base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, | 235 if (EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kNormalGray) || |
| 230 kNormalGray) || | 236 EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kHighGray) || |
| 231 base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, | 237 EqualsCaseInsensitiveASCII(printout_mode_choice->choice, kDraftGray)) { |
| 232 kHighGray) || | |
| 233 base::EqualsCaseInsensitiveASCII(printout_mode_choice->choice, | |
| 234 kDraftGray)) { | |
| 235 *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; | 238 *color_model_for_black = PRINTOUTMODE_NORMAL_GRAY; |
| 236 *color_is_default = false; | 239 *color_is_default = false; |
| 237 } | 240 } |
| 238 } | 241 } |
| 239 return true; | 242 return true; |
| 240 } | 243 } |
| 241 | 244 |
| 242 bool GetColorModeSettings(ppd_file_t* ppd, | 245 bool GetColorModeSettings(ppd_file_t* ppd, |
| 243 ColorModel* color_model_for_black, | 246 ColorModel* color_model_for_black, |
| 244 ColorModel* color_model_for_color, | 247 ColorModel* color_model_for_color, |
| 245 bool* color_is_default) { | 248 bool* color_is_default) { |
| 246 // Samsung printers use "ColorMode" attribute in their PPDs. | 249 // Samsung printers use "ColorMode" attribute in their PPDs. |
| 247 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); | 250 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColorMode); |
| 248 if (!color_mode_option) | 251 if (!color_mode_option) |
| 249 return false; | 252 return false; |
| 250 | 253 |
| 251 if (ppdFindChoice(color_mode_option, kColor)) | 254 if (ppdFindChoice(color_mode_option, kColor) || |
| 255 ppdFindChoice(color_mode_option, kSamsungColorTrue)) { |
| 252 *color_model_for_color = COLORMODE_COLOR; | 256 *color_model_for_color = COLORMODE_COLOR; |
| 257 } |
| 253 | 258 |
| 254 if (ppdFindChoice(color_mode_option, kMonochrome)) | 259 if (ppdFindChoice(color_mode_option, kMonochrome) || |
| 260 ppdFindChoice(color_mode_option, kSamsungColorFalse)) { |
| 255 *color_model_for_black = COLORMODE_MONOCHROME; | 261 *color_model_for_black = COLORMODE_MONOCHROME; |
| 262 } |
| 256 | 263 |
| 257 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); | 264 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 258 if (!mode_choice) { | 265 if (!mode_choice) { |
| 259 mode_choice = ppdFindChoice(color_mode_option, | 266 mode_choice = ppdFindChoice(color_mode_option, |
| 260 color_mode_option->defchoice); | 267 color_mode_option->defchoice); |
| 261 } | 268 } |
| 262 | 269 |
| 263 if (mode_choice) { | 270 if (mode_choice) { |
| 264 *color_is_default = | 271 *color_is_default = |
| 265 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); | 272 EqualsCaseInsensitiveASCII(mode_choice->choice, kColor) || |
| 273 EqualsCaseInsensitiveASCII(mode_choice->choice, kSamsungColorTrue); |
| 266 } | 274 } |
| 267 return true; | 275 return true; |
| 268 } | 276 } |
| 269 | 277 |
| 270 bool GetBrotherColorSettings(ppd_file_t* ppd, | 278 bool GetBrotherColorSettings(ppd_file_t* ppd, |
| 271 ColorModel* color_model_for_black, | 279 ColorModel* color_model_for_black, |
| 272 ColorModel* color_model_for_color, | 280 ColorModel* color_model_for_color, |
| 273 bool* color_is_default) { | 281 bool* color_is_default) { |
| 274 // Some Brother printers use "BRMonoColor" attribute in their PPDs. | 282 // Some Brother printers use "BRMonoColor" attribute in their PPDs. |
| 275 // Some Brother printers use "BRPrintQuality" attribute in their PPDs. | 283 // Some Brother printers use "BRPrintQuality" attribute in their PPDs. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 289 else if (ppdFindChoice(color_mode_option, kBlack)) | 297 else if (ppdFindChoice(color_mode_option, kBlack)) |
| 290 *color_model_for_black = BROTHER_BRSCRIPT3_BLACK; | 298 *color_model_for_black = BROTHER_BRSCRIPT3_BLACK; |
| 291 | 299 |
| 292 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorMode); | 300 ppd_choice_t* marked_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 293 if (!marked_choice) { | 301 if (!marked_choice) { |
| 294 marked_choice = | 302 marked_choice = |
| 295 ppdFindChoice(color_mode_option, color_mode_option->defchoice); | 303 ppdFindChoice(color_mode_option, color_mode_option->defchoice); |
| 296 } | 304 } |
| 297 if (marked_choice) { | 305 if (marked_choice) { |
| 298 *color_is_default = | 306 *color_is_default = |
| 299 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && | 307 !EqualsCaseInsensitiveASCII(marked_choice->choice, kBlack) && |
| 300 !base::EqualsCaseInsensitiveASCII(marked_choice->choice, kMono); | 308 !EqualsCaseInsensitiveASCII(marked_choice->choice, kMono); |
| 301 } | 309 } |
| 302 return true; | 310 return true; |
| 303 } | 311 } |
| 304 | 312 |
| 305 bool GetHPColorSettings(ppd_file_t* ppd, | 313 bool GetHPColorSettings(ppd_file_t* ppd, |
| 306 ColorModel* color_model_for_black, | 314 ColorModel* color_model_for_black, |
| 307 ColorModel* color_model_for_color, | 315 ColorModel* color_model_for_color, |
| 308 bool* color_is_default) { | 316 bool* color_is_default) { |
| 309 // HP printers use "Color/Color Model" attribute in their PPDs. | 317 // HP printers use "Color/Color Model" attribute in their PPDs. |
| 310 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor); | 318 ppd_option_t* color_mode_option = ppdFindOption(ppd, kColor); |
| 311 if (!color_mode_option) | 319 if (!color_mode_option) |
| 312 return false; | 320 return false; |
| 313 | 321 |
| 314 if (ppdFindChoice(color_mode_option, kColor)) | 322 if (ppdFindChoice(color_mode_option, kColor)) |
| 315 *color_model_for_color = HP_COLOR_COLOR; | 323 *color_model_for_color = HP_COLOR_COLOR; |
| 316 if (ppdFindChoice(color_mode_option, kBlack)) | 324 if (ppdFindChoice(color_mode_option, kBlack)) |
| 317 *color_model_for_black = HP_COLOR_BLACK; | 325 *color_model_for_black = HP_COLOR_BLACK; |
| 318 | 326 |
| 319 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); | 327 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kColorMode); |
| 320 if (!mode_choice) { | 328 if (!mode_choice) { |
| 321 mode_choice = ppdFindChoice(color_mode_option, | 329 mode_choice = ppdFindChoice(color_mode_option, |
| 322 color_mode_option->defchoice); | 330 color_mode_option->defchoice); |
| 323 } | 331 } |
| 324 if (mode_choice) { | 332 if (mode_choice) { |
| 325 *color_is_default = | 333 *color_is_default = EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); |
| 326 base::EqualsCaseInsensitiveASCII(mode_choice->choice, kColor); | |
| 327 } | 334 } |
| 328 return true; | 335 return true; |
| 329 } | 336 } |
| 330 | 337 |
| 331 bool GetProcessColorModelSettings(ppd_file_t* ppd, | 338 bool GetProcessColorModelSettings(ppd_file_t* ppd, |
| 332 ColorModel* color_model_for_black, | 339 ColorModel* color_model_for_black, |
| 333 ColorModel* color_model_for_color, | 340 ColorModel* color_model_for_color, |
| 334 bool* color_is_default) { | 341 bool* color_is_default) { |
| 335 // Canon printers use "ProcessColorModel" attribute in their PPDs. | 342 // Canon printers use "ProcessColorModel" attribute in their PPDs. |
| 336 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); | 343 ppd_option_t* color_mode_option = ppdFindOption(ppd, kProcessColorModel); |
| 337 if (!color_mode_option) | 344 if (!color_mode_option) |
| 338 return false; | 345 return false; |
| 339 | 346 |
| 340 if (ppdFindChoice(color_mode_option, kRGB)) | 347 if (ppdFindChoice(color_mode_option, kRGB)) |
| 341 *color_model_for_color = PROCESSCOLORMODEL_RGB; | 348 *color_model_for_color = PROCESSCOLORMODEL_RGB; |
| 342 else if (ppdFindChoice(color_mode_option, kCMYK)) | 349 else if (ppdFindChoice(color_mode_option, kCMYK)) |
| 343 *color_model_for_color = PROCESSCOLORMODEL_CMYK; | 350 *color_model_for_color = PROCESSCOLORMODEL_CMYK; |
| 344 | 351 |
| 345 if (ppdFindChoice(color_mode_option, kGreyscale)) | 352 if (ppdFindChoice(color_mode_option, kGreyscale)) |
| 346 *color_model_for_black = PROCESSCOLORMODEL_GREYSCALE; | 353 *color_model_for_black = PROCESSCOLORMODEL_GREYSCALE; |
| 347 | 354 |
| 348 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kProcessColorModel); | 355 ppd_choice_t* mode_choice = ppdFindMarkedChoice(ppd, kProcessColorModel); |
| 349 if (!mode_choice) { | 356 if (!mode_choice) { |
| 350 mode_choice = ppdFindChoice(color_mode_option, | 357 mode_choice = ppdFindChoice(color_mode_option, |
| 351 color_mode_option->defchoice); | 358 color_mode_option->defchoice); |
| 352 } | 359 } |
| 353 | 360 |
| 354 if (mode_choice) { | 361 if (mode_choice) { |
| 355 *color_is_default = | 362 *color_is_default = |
| 356 !base::EqualsCaseInsensitiveASCII(mode_choice->choice, kGreyscale); | 363 !EqualsCaseInsensitiveASCII(mode_choice->choice, kGreyscale); |
| 357 } | 364 } |
| 358 return true; | 365 return true; |
| 359 } | 366 } |
| 360 | 367 |
| 361 bool GetColorModelSettings(ppd_file_t* ppd, | 368 bool GetColorModelSettings(ppd_file_t* ppd, |
| 362 ColorModel* cm_black, | 369 ColorModel* cm_black, |
| 363 ColorModel* cm_color, | 370 ColorModel* cm_color, |
| 364 bool* is_color) { | 371 bool* is_color) { |
| 365 bool is_color_device = false; | 372 bool is_color_device = false; |
| 366 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr); | 373 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, nullptr); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 } | 498 } |
| 492 | 499 |
| 493 ppdClose(ppd); | 500 ppdClose(ppd); |
| 494 base::DeleteFile(ppd_file_path, false); | 501 base::DeleteFile(ppd_file_path, false); |
| 495 | 502 |
| 496 *printer_info = caps; | 503 *printer_info = caps; |
| 497 return true; | 504 return true; |
| 498 } | 505 } |
| 499 | 506 |
| 500 } // namespace printing | 507 } // namespace printing |
| OLD | NEW |