OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/printing/print_system_task_proxy.h" | 5 #include "chrome/browser/printing/print_system_task_proxy.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/string_split.h" | 14 #include "base/string_split.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/ui/webui/print_preview_handler.h" |
17 #include "printing/backend/print_backend.h" | 18 #include "printing/backend/print_backend.h" |
18 #include "printing/print_job_constants.h" | 19 #include "printing/print_job_constants.h" |
19 #include "printing/print_settings.h" | 20 #include "printing/print_settings.h" |
20 | 21 |
21 #if defined(USE_CUPS) | 22 #if defined(USE_CUPS) |
22 #include <cups/cups.h> | 23 #include <cups/cups.h> |
23 #include <cups/ppd.h> | 24 #include <cups/ppd.h> |
24 | 25 |
25 #include "base/file_util.h" | 26 #include "base/file_util.h" |
26 #endif | 27 #endif |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 | 305 |
305 PrintSystemTaskProxy::PrintSystemTaskProxy( | 306 PrintSystemTaskProxy::PrintSystemTaskProxy( |
306 const base::WeakPtr<PrintPreviewHandler>& handler, | 307 const base::WeakPtr<PrintPreviewHandler>& handler, |
307 printing::PrintBackend* print_backend, | 308 printing::PrintBackend* print_backend, |
308 bool has_logged_printers_count) | 309 bool has_logged_printers_count) |
309 : handler_(handler), | 310 : handler_(handler), |
310 print_backend_(print_backend), | 311 print_backend_(print_backend), |
311 has_logged_printers_count_(has_logged_printers_count) { | 312 has_logged_printers_count_(has_logged_printers_count) { |
312 } | 313 } |
313 | 314 |
| 315 #if defined(UNIT_TEST) && defined(USE_CUPS) |
| 316 // Only used for testing. |
| 317 PrintSystemTaskProxy::PrintSystemTaskProxy() { |
| 318 } |
| 319 #endif |
| 320 |
314 PrintSystemTaskProxy::~PrintSystemTaskProxy() { | 321 PrintSystemTaskProxy::~PrintSystemTaskProxy() { |
315 } | 322 } |
316 | 323 |
317 void PrintSystemTaskProxy::GetDefaultPrinter() { | 324 void PrintSystemTaskProxy::GetDefaultPrinter() { |
318 VLOG(1) << "Get default printer start"; | 325 VLOG(1) << "Get default printer start"; |
319 std::string* default_printer = NULL; | 326 std::string* default_printer = NULL; |
320 if (PrintPreviewHandler::last_used_printer_name_ == NULL) { | 327 if (PrintPreviewHandler::last_used_printer_name_ == NULL) { |
321 default_printer = new std::string(print_backend_->GetDefaultPrinterName()); | 328 default_printer = new std::string(print_backend_->GetDefaultPrinterName()); |
322 } else { | 329 } else { |
323 default_printer = new std::string( | 330 default_printer = new std::string( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 BrowserThread::UI, FROM_HERE, | 387 BrowserThread::UI, FROM_HERE, |
381 base::Bind(&PrintSystemTaskProxy::SetupPrinterList, this, printers)); | 388 base::Bind(&PrintSystemTaskProxy::SetupPrinterList, this, printers)); |
382 } | 389 } |
383 | 390 |
384 void PrintSystemTaskProxy::SetupPrinterList(ListValue* printers) { | 391 void PrintSystemTaskProxy::SetupPrinterList(ListValue* printers) { |
385 if (handler_) | 392 if (handler_) |
386 handler_->SetupPrinterList(*printers); | 393 handler_->SetupPrinterList(*printers); |
387 delete printers; | 394 delete printers; |
388 } | 395 } |
389 | 396 |
390 void PrintSystemTaskProxy::GetPrinterCapabilities( | |
391 const std::string& printer_name) { | |
392 VLOG(1) << "Get printer capabilities start for " << printer_name; | |
393 printing::PrinterCapsAndDefaults printer_info; | |
394 | |
395 bool set_color_as_default = false; | |
396 bool disable_color_options = true; | |
397 bool set_duplex_as_default = false; | |
398 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; | |
399 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; | |
400 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; | |
401 if (!print_backend_->GetPrinterCapsAndDefaults(printer_name, | |
402 &printer_info)) { | |
403 return; | |
404 } | |
405 | |
406 #if defined(USE_CUPS) | 397 #if defined(USE_CUPS) |
| 398 bool PrintSystemTaskProxy::GetPrinterCapabilitiesCUPS( |
| 399 const printing::PrinterCapsAndDefaults& printer_info, |
| 400 const std::string& printer_name, |
| 401 bool* set_color_as_default, |
| 402 int* printer_color_space_for_color, |
| 403 int* printer_color_space_for_black, |
| 404 bool* set_duplex_as_default, |
| 405 int* default_duplex_setting_value) { |
407 FilePath ppd_file_path; | 406 FilePath ppd_file_path; |
408 if (!file_util::CreateTemporaryFile(&ppd_file_path)) | 407 if (!file_util::CreateTemporaryFile(&ppd_file_path)) |
409 return; | 408 return false; |
410 | 409 |
411 int data_size = printer_info.printer_capabilities.length(); | 410 int data_size = printer_info.printer_capabilities.length(); |
412 if (data_size != file_util::WriteFile( | 411 if (data_size != file_util::WriteFile( |
413 ppd_file_path, | 412 ppd_file_path, |
414 printer_info.printer_capabilities.data(), | 413 printer_info.printer_capabilities.data(), |
415 data_size)) { | 414 data_size)) { |
416 file_util::Delete(ppd_file_path, false); | 415 file_util::Delete(ppd_file_path, false); |
417 return; | 416 return false; |
418 } | 417 } |
419 | 418 |
420 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); | 419 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); |
421 if (ppd) { | 420 if (ppd) { |
422 #if !defined(OS_MACOSX) | 421 #if !defined(OS_MACOSX) |
423 printing_internal::mark_lpoptions(printer_name, &ppd); | 422 printing_internal::mark_lpoptions(printer_name, &ppd); |
424 #endif | 423 #endif |
425 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); | 424 ppd_choice_t* duplex_choice = ppdFindMarkedChoice(ppd, kDuplex); |
426 if (!duplex_choice) { | 425 if (!duplex_choice) { |
427 ppd_option_t* option = ppdFindOption(ppd, kDuplex); | 426 ppd_option_t* option = ppdFindOption(ppd, kDuplex); |
428 if (option) | 427 if (option) |
429 duplex_choice = ppdFindChoice(option, option->defchoice); | 428 duplex_choice = ppdFindChoice(option, option->defchoice); |
430 } | 429 } |
431 | 430 |
432 if (duplex_choice) { | 431 if (duplex_choice) { |
433 if (base::strcasecmp(duplex_choice->choice, kDuplexNone) != 0) { | 432 if (base::strcasecmp(duplex_choice->choice, kDuplexNone) != 0) { |
434 set_duplex_as_default = true; | 433 *set_duplex_as_default = true; |
435 default_duplex_setting_value = printing::LONG_EDGE; | 434 *default_duplex_setting_value = printing::LONG_EDGE; |
436 } else { | 435 } else { |
437 default_duplex_setting_value = printing::SIMPLEX; | 436 *default_duplex_setting_value = printing::SIMPLEX; |
438 } | 437 } |
439 } | 438 } |
440 | 439 |
441 bool is_color_device = false; | 440 bool is_color_device = false; |
442 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, NULL); | 441 ppd_attr_t* attr = ppdFindAttr(ppd, kColorDevice, NULL); |
443 if (attr && attr->value) | 442 if (attr && attr->value) |
444 is_color_device = ppd->color_device; | 443 is_color_device = ppd->color_device; |
445 disable_color_options = !is_color_device; | 444 *set_color_as_default = is_color_device; |
446 set_color_as_default = is_color_device; | |
447 | 445 |
448 if (!((is_color_device && getBasicColorModelSettings( | 446 if (!((is_color_device && getBasicColorModelSettings( |
449 ppd, &printer_color_space_for_black, | 447 ppd, printer_color_space_for_black, |
450 &printer_color_space_for_color, &set_color_as_default)) || | 448 printer_color_space_for_color, set_color_as_default)) || |
451 getPrintOutModeColorSettings( | 449 getPrintOutModeColorSettings( |
452 ppd, &printer_color_space_for_black, | 450 ppd, printer_color_space_for_black, |
453 &printer_color_space_for_color, &set_color_as_default) || | 451 printer_color_space_for_color, set_color_as_default) || |
454 getColorModeSettings( | 452 getColorModeSettings( |
455 ppd, &printer_color_space_for_black, | 453 ppd, printer_color_space_for_black, |
456 &printer_color_space_for_color, &set_color_as_default) || | 454 printer_color_space_for_color, set_color_as_default) || |
457 getHPColorSettings( | 455 getHPColorSettings( |
458 ppd, &printer_color_space_for_black, | 456 ppd, printer_color_space_for_black, |
459 &printer_color_space_for_color, &set_color_as_default) || | 457 printer_color_space_for_color, set_color_as_default) || |
460 getProcessColorModelSettings( | 458 getProcessColorModelSettings( |
461 ppd, &printer_color_space_for_black, | 459 ppd, printer_color_space_for_black, |
462 &printer_color_space_for_color, &set_color_as_default))) { | 460 printer_color_space_for_color, set_color_as_default))) { |
463 VLOG(1) << "Unknown printer color model"; | 461 VLOG(1) << "Unknown printer color model"; |
464 } | 462 } |
465 ppdClose(ppd); | 463 ppdClose(ppd); |
466 } | 464 } |
467 file_util::Delete(ppd_file_path, false); | 465 file_util::Delete(ppd_file_path, false); |
468 #elif defined(OS_WIN) | 466 return true; |
| 467 } |
| 468 #endif // defined(USE_CUPS) |
| 469 |
| 470 #if defined(OS_WIN) |
| 471 void PrintSystemTaskProxy::GetPrinterCapabilitiesWin( |
| 472 const printing::PrinterCapsAndDefaults& printer_info, |
| 473 bool* set_color_as_default, |
| 474 int* printer_color_space_for_color, |
| 475 int* printer_color_space_for_black, |
| 476 bool* set_duplex_as_default, |
| 477 int* default_duplex_setting_value) { |
469 // According to XPS 1.0 spec, only color printers have psk:Color. | 478 // According to XPS 1.0 spec, only color printers have psk:Color. |
470 // Therefore we don't need to parse the whole XML file, we just need to | 479 // Therefore we don't need to parse the whole XML file, we just need to |
471 // search the string. The spec can be found at: | 480 // search the string. The spec can be found at: |
472 // http://msdn.microsoft.com/en-us/windows/hardware/gg463431. | 481 // http://msdn.microsoft.com/en-us/windows/hardware/gg463431. |
473 if (printer_info.printer_capabilities.find(kPskColor) != std::string::npos) | 482 if (printer_info.printer_capabilities.find(kPskColor) != std::string::npos) |
474 printer_color_space_for_color = printing::COLOR; | 483 *printer_color_space_for_color = printing::COLOR; |
475 | 484 |
476 if ((printer_info.printer_capabilities.find(kPskGray) != | 485 if ((printer_info.printer_capabilities.find(kPskGray) != |
477 std::string::npos) || | 486 std::string::npos) || |
478 (printer_info.printer_capabilities.find(kPskMonochrome) != | 487 (printer_info.printer_capabilities.find(kPskMonochrome) != |
479 std::string::npos)) { | 488 std::string::npos)) { |
480 printer_color_space_for_black = printing::GRAY; | 489 *printer_color_space_for_black = printing::GRAY; |
481 } | 490 } |
482 set_color_as_default = | 491 *set_color_as_default = |
483 (printer_info.printer_defaults.find(kPskColor) != std::string::npos); | 492 (printer_info.printer_defaults.find(kPskColor) != std::string::npos); |
484 | 493 |
485 set_duplex_as_default = | 494 *set_duplex_as_default = |
486 (printer_info.printer_defaults.find(kPskDuplexFeature) != | 495 (printer_info.printer_defaults.find(kPskDuplexFeature) != |
487 std::string::npos) && | 496 std::string::npos) && |
488 (printer_info.printer_defaults.find(kPskTwoSided) != | 497 (printer_info.printer_defaults.find(kPskTwoSided) != |
489 std::string::npos); | 498 std::string::npos); |
490 | 499 |
491 if (printer_info.printer_defaults.find(kPskDuplexFeature) != | 500 if (printer_info.printer_defaults.find(kPskDuplexFeature) != |
492 std::string::npos) { | 501 std::string::npos) { |
493 if (printer_info.printer_defaults.find(kPskTwoSided) != | 502 if (printer_info.printer_defaults.find(kPskTwoSided) != |
494 std::string::npos) { | 503 std::string::npos) { |
495 default_duplex_setting_value = printing::LONG_EDGE; | 504 *default_duplex_setting_value = printing::LONG_EDGE; |
496 } else { | 505 } else { |
497 default_duplex_setting_value = printing::SIMPLEX; | 506 *default_duplex_setting_value = printing::SIMPLEX; |
498 } | 507 } |
499 } | 508 } |
| 509 } |
| 510 #endif // defined(OS_WIN) |
| 511 |
| 512 void PrintSystemTaskProxy::GetPrinterCapabilities( |
| 513 const std::string& printer_name) { |
| 514 VLOG(1) << "Get printer capabilities start for " << printer_name; |
| 515 printing::PrinterCapsAndDefaults printer_info; |
| 516 if (!print_backend_->GetPrinterCapsAndDefaults(printer_name, |
| 517 &printer_info)) { |
| 518 return; |
| 519 } |
| 520 |
| 521 bool set_color_as_default = false; |
| 522 bool set_duplex_as_default = false; |
| 523 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; |
| 524 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; |
| 525 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; |
| 526 |
| 527 #if defined(USE_CUPS) |
| 528 if (!GetPrinterCapabilitiesCUPS(printer_info, |
| 529 printer_name, |
| 530 &set_color_as_default, |
| 531 &printer_color_space_for_color, |
| 532 &printer_color_space_for_black, |
| 533 &set_duplex_as_default, |
| 534 &default_duplex_setting_value)) { |
| 535 return; |
| 536 } |
| 537 #elif defined(OS_WIN) |
| 538 GetPrinterCapabilitiesWin(printer_info, |
| 539 &set_color_as_default, |
| 540 &printer_color_space_for_color, |
| 541 &printer_color_space_for_black, |
| 542 &set_duplex_as_default, |
| 543 &default_duplex_setting_value); |
500 #else | 544 #else |
501 NOTIMPLEMENTED(); | 545 NOTIMPLEMENTED(); |
502 #endif | 546 #endif |
503 disable_color_options = !printer_color_space_for_color || | 547 bool disable_color_options = (!printer_color_space_for_color || |
504 !printer_color_space_for_black || | 548 !printer_color_space_for_black || |
505 (printer_color_space_for_color == | 549 (printer_color_space_for_color == |
506 printer_color_space_for_black); | 550 printer_color_space_for_black)); |
507 | 551 |
508 DictionaryValue settings_info; | 552 DictionaryValue settings_info; |
509 settings_info.SetBoolean(kDisableColorOption, disable_color_options); | 553 settings_info.SetBoolean(kDisableColorOption, disable_color_options); |
510 if (printer_color_space_for_color == printing::UNKNOWN_COLOR_MODEL) | 554 if (printer_color_space_for_color == printing::UNKNOWN_COLOR_MODEL) |
511 printer_color_space_for_color = printing::COLOR; | 555 printer_color_space_for_color = printing::COLOR; |
512 | 556 |
513 if (printer_color_space_for_black == printing::UNKNOWN_COLOR_MODEL) | 557 if (printer_color_space_for_black == printing::UNKNOWN_COLOR_MODEL) |
514 printer_color_space_for_black = printing::GRAY; | 558 printer_color_space_for_black = printing::GRAY; |
515 | 559 |
516 if (disable_color_options || | 560 if (disable_color_options || |
(...skipping 18 matching lines...) Expand all Loading... |
535 base::Bind(&PrintSystemTaskProxy::SendPrinterCapabilities, this, | 579 base::Bind(&PrintSystemTaskProxy::SendPrinterCapabilities, this, |
536 settings_info.DeepCopy())); | 580 settings_info.DeepCopy())); |
537 } | 581 } |
538 | 582 |
539 void PrintSystemTaskProxy::SendPrinterCapabilities( | 583 void PrintSystemTaskProxy::SendPrinterCapabilities( |
540 DictionaryValue* settings_info) { | 584 DictionaryValue* settings_info) { |
541 if (handler_) | 585 if (handler_) |
542 handler_->SendPrinterCapabilities(*settings_info); | 586 handler_->SendPrinterCapabilities(*settings_info); |
543 delete settings_info; | 587 delete settings_info; |
544 } | 588 } |
OLD | NEW |