printing_context_no_system_dialog.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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
  3. // found in the LICENSE file.
  4. #include "printing/printing_context_no_system_dialog.h"
  5. #include <stdint.h>
  6. #include <unicode/ulocdata.h>
  7. #include <memory>
  8. #include <utility>
  9. #include "base/logging.h"
  10. #include "base/values.h"
  11. #include "printing/metafile.h"
  12. #include "printing/print_job_constants.h"
  13. #include "printing/units.h"
  14. namespace printing {
  15. #if !defined(USE_CUPS)
  16. // static
  17. std::unique_ptr<PrintingContext> PrintingContext::CreateImpl(
  18. Delegate* delegate,
  19. bool skip_system_calls) {
  20. return std::make_unique<PrintingContextNoSystemDialog>(delegate);
  21. }
  22. #endif // !defined(USE_CUPS)
  23. PrintingContextNoSystemDialog::PrintingContextNoSystemDialog(Delegate* delegate)
  24. : PrintingContext(delegate) {}
  25. PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() {
  26. ReleaseContext();
  27. }
  28. void PrintingContextNoSystemDialog::AskUserForSettings(
  29. int max_pages,
  30. bool has_selection,
  31. bool is_scripted,
  32. PrintSettingsCallback callback) {
  33. // We don't want to bring up a dialog here. Ever. Just signal the callback.
  34. std::move(callback).Run(mojom::ResultCode::kSuccess);
  35. }
  36. mojom::ResultCode PrintingContextNoSystemDialog::UseDefaultSettings() {
  37. DCHECK(!in_print_job_);
  38. ResetSettings();
  39. settings_->set_dpi(kDefaultPdfDpi);
  40. gfx::Size physical_size = GetPdfPaperSizeDeviceUnits();
  41. // Assume full page is printable for now.
  42. gfx::Rect printable_area(0, 0, physical_size.width(), physical_size.height());
  43. DCHECK_EQ(settings_->device_units_per_inch(), kDefaultPdfDpi);
  44. settings_->SetPrinterPrintableArea(physical_size, printable_area, true);
  45. return mojom::ResultCode::kSuccess;
  46. }
  47. gfx::Size PrintingContextNoSystemDialog::GetPdfPaperSizeDeviceUnits() {
  48. int32_t width = 0;
  49. int32_t height = 0;
  50. UErrorCode error = U_ZERO_ERROR;
  51. ulocdata_getPaperSize(delegate_->GetAppLocale().c_str(), &height, &width,
  52. &error);
  53. if (error > U_ZERO_ERROR) {
  54. // If the call failed, assume a paper size of 8.5 x 11 inches.
  55. LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: "
  56. << error;
  57. width =
  58. static_cast<int>(kLetterWidthInch * settings_->device_units_per_inch());
  59. height = static_cast<int>(kLetterHeightInch *
  60. settings_->device_units_per_inch());
  61. } else {
  62. // ulocdata_getPaperSize returns the width and height in mm.
  63. // Convert this to pixels based on the dpi.
  64. float multiplier = settings_->device_units_per_inch() / kMicronsPerMil;
  65. width *= multiplier;
  66. height *= multiplier;
  67. }
  68. return gfx::Size(width, height);
  69. }
  70. mojom::ResultCode PrintingContextNoSystemDialog::UpdatePrinterSettings(
  71. const PrinterSettings& printer_settings) {
  72. DCHECK(!printer_settings.show_system_dialog);
  73. if (settings_->dpi() == 0)
  74. UseDefaultSettings();
  75. return mojom::ResultCode::kSuccess;
  76. }
  77. mojom::ResultCode PrintingContextNoSystemDialog::NewDocument(
  78. const std::u16string& document_name) {
  79. DCHECK(!in_print_job_);
  80. in_print_job_ = true;
  81. return mojom::ResultCode::kSuccess;
  82. }
  83. mojom::ResultCode PrintingContextNoSystemDialog::PrintDocument(
  84. const MetafilePlayer& metafile,
  85. const PrintSettings& settings,
  86. uint32_t num_pages) {
  87. if (abort_printing_)
  88. return mojom::ResultCode::kCanceled;
  89. DCHECK(in_print_job_);
  90. // Intentional No-op.
  91. return mojom::ResultCode::kSuccess;
  92. }
  93. mojom::ResultCode PrintingContextNoSystemDialog::DocumentDone() {
  94. if (abort_printing_)
  95. return mojom::ResultCode::kCanceled;
  96. DCHECK(in_print_job_);
  97. ResetSettings();
  98. return mojom::ResultCode::kSuccess;
  99. }
  100. void PrintingContextNoSystemDialog::Cancel() {
  101. abort_printing_ = true;
  102. in_print_job_ = false;
  103. }
  104. void PrintingContextNoSystemDialog::ReleaseContext() {
  105. // Intentional No-op.
  106. }
  107. printing::NativeDrawingContext PrintingContextNoSystemDialog::context() const {
  108. // Intentional No-op.
  109. return nullptr;
  110. }
  111. } // namespace printing