blob: b67c68ef4deb072b2d81fe2f9aac415ce1faf0eb [file] [log] [blame]
[email protected]ea5e81d92011-11-08 18:45:151// 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
5#include "printing/printing_context_no_system_dialog.h"
6
avi126e93c2015-12-21 21:48:167#include <stdint.h>
[email protected]ea5e81d92011-11-08 18:45:158#include <unicode/ulocdata.h>
9
skaub7931952016-07-27 18:04:5110#include <memory>
11
[email protected]ea5e81d92011-11-08 18:45:1512#include "base/logging.h"
dchengc3df9ba2016-04-07 23:09:3213#include "base/memory/ptr_util.h"
[email protected]ea5e81d92011-11-08 18:45:1514#include "base/values.h"
15#include "printing/metafile.h"
16#include "printing/print_job_constants.h"
17#include "printing/units.h"
18
19namespace printing {
20
skaub7931952016-07-27 18:04:5121#if !defined(USE_CUPS)
[email protected]ea5e81d92011-11-08 18:45:1522// static
dchengc3df9ba2016-04-07 23:09:3223std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) {
skaub7931952016-07-27 18:04:5124 return base::MakeUnique<PrintingContextNoSystemDialog>(delegate);
[email protected]ea5e81d92011-11-08 18:45:1525}
skaub7931952016-07-27 18:04:5126#endif // !defined(USE_CUPS)
[email protected]ea5e81d92011-11-08 18:45:1527
Vitaly Bukabd7c9812014-08-26 08:57:5428PrintingContextNoSystemDialog::PrintingContextNoSystemDialog(Delegate* delegate)
29 : PrintingContext(delegate) {
[email protected]ea5e81d92011-11-08 18:45:1530}
31
32PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() {
33 ReleaseContext();
34}
35
36void PrintingContextNoSystemDialog::AskUserForSettings(
[email protected]ea5e81d92011-11-08 18:45:1537 int max_pages,
38 bool has_selection,
dgn4c172eea2014-12-15 21:11:2339 bool is_scripted,
[email protected]abe48112011-11-19 01:58:3840 const PrintSettingsCallback& callback) {
[email protected]ea5e81d92011-11-08 18:45:1541 // We don't want to bring up a dialog here. Ever. Just signal the callback.
[email protected]abe48112011-11-19 01:58:3842 callback.Run(OK);
[email protected]ea5e81d92011-11-08 18:45:1543}
44
45PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() {
46 DCHECK(!in_print_job_);
47
48 ResetSettings();
[email protected]4c9054b2013-11-04 18:34:2949 settings_.set_dpi(kDefaultPdfDpi);
50 gfx::Size physical_size = GetPdfPaperSizeDeviceUnits();
51 // Assume full page is printable for now.
52 gfx::Rect printable_area(0, 0, physical_size.width(), physical_size.height());
53 DCHECK_EQ(settings_.device_units_per_inch(), kDefaultPdfDpi);
54 settings_.SetPrinterPrintableArea(physical_size, printable_area, true);
55 return OK;
56}
57
58gfx::Size PrintingContextNoSystemDialog::GetPdfPaperSizeDeviceUnits() {
[email protected]ea5e81d92011-11-08 18:45:1559 int32_t width = 0;
60 int32_t height = 0;
61 UErrorCode error = U_ZERO_ERROR;
Vitaly Bukabd7c9812014-08-26 08:57:5462 ulocdata_getPaperSize(
63 delegate_->GetAppLocale().c_str(), &height, &width, &error);
[email protected]d1bee422012-08-07 23:44:3164 if (error > U_ZERO_ERROR) {
[email protected]ea5e81d92011-11-08 18:45:1565 // If the call failed, assume a paper size of 8.5 x 11 inches.
66 LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: "
67 << error;
[email protected]4c9054b2013-11-04 18:34:2968 width = static_cast<int>(
69 kLetterWidthInch * settings_.device_units_per_inch());
70 height = static_cast<int>(
71 kLetterHeightInch * settings_.device_units_per_inch());
[email protected]ea5e81d92011-11-08 18:45:1572 } else {
73 // ulocdata_getPaperSize returns the width and height in mm.
74 // Convert this to pixels based on the dpi.
[email protected]4c9054b2013-11-04 18:34:2975 float multiplier = 100 * settings_.device_units_per_inch();
76 multiplier /= kHundrethsMMPerInch;
77 width *= multiplier;
78 height *= multiplier;
[email protected]ea5e81d92011-11-08 18:45:1579 }
[email protected]4c9054b2013-11-04 18:34:2980 return gfx::Size(width, height);
[email protected]ea5e81d92011-11-08 18:45:1581}
82
83PrintingContext::Result PrintingContextNoSystemDialog::UpdatePrinterSettings(
vitalybuka92ab8ce2014-08-26 23:41:4584 bool external_preview,
vitalybuka95fa3c92015-05-05 03:03:3285 bool show_system_dialog,
86 int page_count) {
vitalybuka92ab8ce2014-08-26 23:41:4587 DCHECK(!show_system_dialog);
[email protected]0c5e3022013-11-01 14:25:0188
89 if (settings_.dpi() == 0)
90 UseDefaultSettings();
91
[email protected]ea5e81d92011-11-08 18:45:1592 return OK;
93}
94
[email protected]ea5e81d92011-11-08 18:45:1595PrintingContext::Result PrintingContextNoSystemDialog::NewDocument(
[email protected]b5fa4ee2013-10-01 07:19:0796 const base::string16& document_name) {
[email protected]ea5e81d92011-11-08 18:45:1597 DCHECK(!in_print_job_);
98 in_print_job_ = true;
99
100 return OK;
101}
102
103PrintingContext::Result PrintingContextNoSystemDialog::NewPage() {
104 if (abort_printing_)
105 return CANCEL;
106 DCHECK(in_print_job_);
107
108 // Intentional No-op.
109
110 return OK;
111}
112
113PrintingContext::Result PrintingContextNoSystemDialog::PageDone() {
114 if (abort_printing_)
115 return CANCEL;
116 DCHECK(in_print_job_);
117
118 // Intentional No-op.
119
120 return OK;
121}
122
123PrintingContext::Result PrintingContextNoSystemDialog::DocumentDone() {
124 if (abort_printing_)
125 return CANCEL;
126 DCHECK(in_print_job_);
127
128 ResetSettings();
129 return OK;
130}
131
132void PrintingContextNoSystemDialog::Cancel() {
133 abort_printing_ = true;
134 in_print_job_ = false;
135}
136
137void PrintingContextNoSystemDialog::ReleaseContext() {
138 // Intentional No-op.
139}
140
Nico Weber8e559562017-10-03 01:25:26141printing::NativeDrawingContext PrintingContextNoSystemDialog::context() const {
[email protected]ea5e81d92011-11-08 18:45:15142 // Intentional No-op.
skaub7931952016-07-27 18:04:51143 return nullptr;
[email protected]ea5e81d92011-11-08 18:45:15144}
145
146} // namespace printing
147