blob: 11651a009930011c37929e71ca213cd79ced8987 [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]51e8d9352010-10-06 22:21:175#include "printing/printing_context_win.h"
initial.commit09911bf2008-07-26 23:55:296
Lei Zhang151b1b92019-05-24 20:48:307#include <winspool.h>
8
[email protected]19f09a62011-05-04 23:37:479#include <algorithm>
Vladislav Kuzkokov19998222019-08-12 14:26:0910#include <utility>
thestigb50a0c332016-09-30 04:22:4511#include <vector>
[email protected]19f09a62011-05-04 23:37:4712
vitalybuka12b87582014-12-08 18:57:1013#include "base/bind.h"
Alan Screene9ed54a2021-11-24 18:22:4614#include "base/check.h"
Alan Screen4e284e9cb2021-12-04 00:38:2915#include "base/check_op.h"
dchengdb5935f2016-03-26 00:16:2716#include "base/memory/free_deleter.h"
[email protected]ad3b05012014-06-16 23:02:1517#include "base/strings/string_number_conversions.h"
[email protected]906265872013-06-07 22:40:4518#include "base/strings/utf_string_conversions.h"
Carlos Caballerob25fe8472020-07-17 10:27:1719#include "base/task/current_thread.h"
[email protected]09083eb2012-09-20 05:58:4320#include "printing/backend/print_backend.h"
[email protected]55e8e9f2012-03-02 22:50:5421#include "printing/backend/win_helper.h"
Scott Violet318a55f2018-03-30 19:08:1922#include "printing/buildflags/buildflags.h"
Alan Screen4e284e9cb2021-12-04 00:38:2923#include "printing/metafile.h"
Alan Screen13ec987ca2019-12-10 19:23:0724#include "printing/metafile_skia.h"
Julie Jeongeun Kime454f2602020-04-30 05:20:0725#include "printing/mojom/print.mojom.h"
Alan Screen4e284e9cb2021-12-04 00:38:2926#include "printing/page_setup.h"
[email protected]4993f342010-10-26 17:57:5227#include "printing/print_settings_initializer_win.h"
[email protected]8ff1d422009-07-07 21:31:3928#include "printing/printed_document.h"
Alan Screen4e284e9cb2021-12-04 00:38:2929#include "printing/printed_page_win.h"
vitalybukaf9d0c0c2014-09-09 19:53:3330#include "printing/printing_context_system_dialog_win.h"
Alan Screen113d0142020-01-31 06:11:2131#include "printing/printing_features.h"
[email protected]ec91bfa2013-11-09 17:24:2732#include "printing/printing_utils.h"
[email protected]e5636a52011-09-28 21:44:4233#include "printing/units.h"
tomhudson828dddb2015-12-04 14:34:1634#include "skia/ext/skia_utils_win.h"
[email protected]6b7bfd22013-05-28 23:55:1535#include "ui/aura/window.h"
samli6d8217f2016-01-20 04:12:2036#include "ui/aura/window_tree_host.h"
[email protected]6b7bfd22013-05-28 23:55:1537
initial.commit09911bf2008-07-26 23:55:2938namespace printing {
39
vitalybuka12b87582014-12-08 18:57:1040namespace {
41
Alan Screen4e284e9cb2021-12-04 00:38:2942// Helper class to ensure that a saved device context state gets restored at end
43// of scope.
44class ScopedSavedState {
45 public:
46 ScopedSavedState(HDC context)
47 : context_(context), saved_state_(SaveDC(context)) {
48 DCHECK_NE(saved_state_, 0);
49 }
50 ~ScopedSavedState() {
51 BOOL res = RestoreDC(context_, saved_state_);
52 DCHECK_NE(res, 0);
53 }
54
55 private:
56 HDC context_;
57 int saved_state_;
58};
59
Alan Screen202e49a2021-09-21 06:34:2860void AssignResult(mojom::ResultCode* out, mojom::ResultCode in) {
vitalybuka12b87582014-12-08 18:57:1061 *out = in;
62}
63
Alan Screen4e284e9cb2021-12-04 00:38:2964void SimpleModifyWorldTransform(HDC context,
65 int offset_x,
66 int offset_y,
67 float shrink_factor) {
68 XFORM xform = {0};
69 xform.eDx = static_cast<float>(offset_x);
70 xform.eDy = static_cast<float>(offset_y);
71 xform.eM11 = xform.eM22 = 1.f / shrink_factor;
72 BOOL res = ModifyWorldTransform(context, &xform, MWT_LEFTMULTIPLY);
73 DCHECK_NE(res, 0);
74}
75
vitalybuka12b87582014-12-08 18:57:1076} // namespace
77
[email protected]51e8d9352010-10-06 22:21:1778// static
Alan Screenf2fbeb7a2021-08-28 05:27:3879std::unique_ptr<PrintingContext> PrintingContext::CreateImpl(
Alan Screene9ed54a2021-11-24 18:22:4680 Delegate* delegate,
81 bool skip_system_calls) {
82 std::unique_ptr<PrintingContext> context;
Alan Screene9ed54a2021-11-24 18:22:4683 context = std::make_unique<PrintingContextSystemDialogWin>(delegate);
Alan Screene9ed54a2021-11-24 18:22:4684#if BUILDFLAG(ENABLE_OOP_PRINTING)
85 if (skip_system_calls)
86 context->set_skip_system_calls();
87#endif
88 return context;
[email protected]51e8d9352010-10-06 22:21:1789}
90
Vitaly Bukabd7c9812014-08-26 08:57:5491PrintingContextWin::PrintingContextWin(Delegate* delegate)
thestigb50a0c332016-09-30 04:22:4592 : PrintingContext(delegate), context_(nullptr) {}
initial.commit09911bf2008-07-26 23:55:2993
[email protected]51e8d9352010-10-06 22:21:1794PrintingContextWin::~PrintingContextWin() {
95 ReleaseContext();
initial.commit09911bf2008-07-26 23:55:2996}
97
Vladislav Kuzkokov48ceab22018-02-14 16:29:2898void PrintingContextWin::AskUserForSettings(int max_pages,
99 bool has_selection,
100 bool is_scripted,
101 PrintSettingsCallback callback) {
vitalybuka92ab8ce2014-08-26 23:41:45102 NOTIMPLEMENTED();
initial.commit09911bf2008-07-26 23:55:29103}
104
Alan Screen202e49a2021-09-21 06:34:28105mojom::ResultCode PrintingContextWin::UseDefaultSettings() {
initial.commit09911bf2008-07-26 23:55:29106 DCHECK(!in_print_job_);
107
Daniel Hosseinian733106e2019-12-05 21:23:58108 scoped_refptr<PrintBackend> backend =
Alan Screene714d4b2020-08-14 19:44:14109 PrintBackend::CreateInstance(delegate_->GetAppLocale());
Alan Screen322ed6182021-06-03 15:26:47110 std::string default_printer_name;
111 mojom::ResultCode result =
112 backend->GetDefaultPrinterName(default_printer_name);
113 if (result != mojom::ResultCode::kSuccess)
Alan Screen202e49a2021-09-21 06:34:28114 return result;
Alan Screen322ed6182021-06-03 15:26:47115
116 std::wstring default_printer = base::UTF8ToWide(default_printer_name);
vitalybuka92ab8ce2014-08-26 23:41:45117 if (!default_printer.empty()) {
118 ScopedPrinterHandle printer;
Lei Zhang151b1b92019-05-24 20:48:30119 if (printer.OpenPrinterWithName(default_printer.c_str())) {
dchengc3df9ba2016-04-07 23:09:32120 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode =
thestigb50a0c332016-09-30 04:22:45121 CreateDevMode(printer.Get(), nullptr);
Alan Screen202e49a2021-09-21 06:34:28122 if (InitializeSettings(default_printer, dev_mode.get()) ==
123 mojom::ResultCode::kSuccess) {
124 return mojom::ResultCode::kSuccess;
125 }
vitalybuka92ab8ce2014-08-26 23:41:45126 }
127 }
128
129 ReleaseContext();
[email protected]ff3ccc22011-04-18 21:35:48130
131 // No default printer configured, do we have any printers at all?
132 DWORD bytes_needed = 0;
133 DWORD count_returned = 0;
thestigb50a0c332016-09-30 04:22:45134 (void)::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, nullptr,
135 2, nullptr, 0, &bytes_needed, &count_returned);
[email protected]ff3ccc22011-04-18 21:35:48136 if (bytes_needed) {
[email protected]1b248882014-02-25 04:39:14137 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2));
thestigb50a0c332016-09-30 04:22:45138 std::vector<BYTE> printer_info_buffer(bytes_needed);
139 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
140 nullptr, 2, printer_info_buffer.data(),
141 bytes_needed, &bytes_needed, &count_returned);
[email protected]ff3ccc22011-04-18 21:35:48142 if (ret && count_returned) { // have printers
143 // Open the first successfully found printer.
[email protected]1b248882014-02-25 04:39:14144 const PRINTER_INFO_2* info_2 =
thestigb50a0c332016-09-30 04:22:45145 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.data());
[email protected]1b248882014-02-25 04:39:14146 const PRINTER_INFO_2* info_2_end = info_2 + count_returned;
147 for (; info_2 < info_2_end; ++info_2) {
148 ScopedPrinterHandle printer;
Jan Wilken Dörrie00632a12021-02-24 09:42:00149 if (!printer.OpenPrinterWithName(info_2->pPrinterName))
[email protected]ff3ccc22011-04-18 21:35:48150 continue;
dchengc3df9ba2016-04-07 23:09:32151 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode =
thestigb50a0c332016-09-30 04:22:45152 CreateDevMode(printer.Get(), nullptr);
Alan Screen202e49a2021-09-21 06:34:28153 if (InitializeSettings(info_2->pPrinterName, dev_mode.get()) ==
154 mojom::ResultCode::kSuccess) {
155 return mojom::ResultCode::kSuccess;
156 }
[email protected]ff3ccc22011-04-18 21:35:48157 }
158 if (context_)
Alan Screen202e49a2021-09-21 06:34:28159 return mojom::ResultCode::kSuccess;
[email protected]ff3ccc22011-04-18 21:35:48160 }
initial.commit09911bf2008-07-26 23:55:29161 }
[email protected]ff3ccc22011-04-18 21:35:48162
vitalybuka92ab8ce2014-08-26 23:41:45163 return OnError();
initial.commit09911bf2008-07-26 23:55:29164}
165
[email protected]4c9054b2013-11-04 18:34:29166gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() {
167 // Default fallback to Letter size.
168 gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch);
169
170 // Get settings from locale. Paper type buffer length is at most 4.
171 const int paper_type_buffer_len = 4;
172 wchar_t paper_type_buffer[paper_type_buffer_len] = {0};
173 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer,
174 paper_type_buffer_len);
175 if (wcslen(paper_type_buffer)) { // The call succeeded.
176 int paper_code = _wtoi(paper_type_buffer);
177 switch (paper_code) {
178 case DMPAPER_LEGAL:
179 paper_size.SetSize(kLegalWidthInch, kLegalHeightInch);
180 break;
181 case DMPAPER_A4:
182 paper_size.SetSize(kA4WidthInch, kA4HeightInch);
183 break;
184 case DMPAPER_A3:
185 paper_size.SetSize(kA3WidthInch, kA3HeightInch);
186 break;
187 default: // DMPAPER_LETTER is used for default fallback.
188 break;
189 }
190 }
Vladislav Kuzkokov19998222019-08-12 14:26:09191 return gfx::Size(paper_size.width() * settings_->device_units_per_inch(),
192 paper_size.height() * settings_->device_units_per_inch());
[email protected]4c9054b2013-11-04 18:34:29193}
194
Alan Screen202e49a2021-09-21 06:34:28195mojom::ResultCode PrintingContextWin::UpdatePrinterSettings(
Lei Zhanga3fdad442021-11-15 23:55:33196 const PrinterSettings& printer_settings) {
[email protected]7868ecab2011-03-05 00:12:53197 DCHECK(!in_print_job_);
198
[email protected]55e8e9f2012-03-02 22:50:54199 ScopedPrinterHandle printer;
Jan Wilken Dörrie00632a12021-02-24 09:42:00200 if (!printer.OpenPrinterWithName(base::as_wcstr(settings_->device_name())))
[email protected]bd5baaf2011-04-21 20:48:33201 return OnError();
[email protected]afbdbf112011-03-28 22:09:37202
[email protected]30aaa81a2011-05-03 21:08:39203 // Make printer changes local to Chrome.
204 // See MSDN documentation regarding DocumentProperties.
dchengc3df9ba2016-04-07 23:09:32205 std::unique_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode =
Jan Wilken Dörrie00632a12021-02-24 09:42:00206 CreateDevModeWithColor(printer.Get(),
207 base::UTF16ToWide(settings_->device_name()),
Alan Screen3fb63bf2020-08-19 00:11:23208 settings_->color() != mojom::ColorModel::kGray);
[email protected]b3b6b6f2014-02-14 20:19:54209 if (!scoped_dev_mode)
[email protected]bd5baaf2011-04-21 20:48:33210 return OnError();
[email protected]7868ecab2011-03-05 00:12:53211
[email protected]b3b6b6f2014-02-14 20:19:54212 {
213 DEVMODE* dev_mode = scoped_dev_mode.get();
Vladislav Kuzkokov19998222019-08-12 14:26:09214 dev_mode->dmCopies = std::max(settings_->copies(), 1);
[email protected]299e3d002014-06-03 08:46:49215 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies
[email protected]b3b6b6f2014-02-14 20:19:54216 dev_mode->dmFields |= DM_COPIES;
Lei Zhang01a1d3c02019-05-21 04:59:10217 dev_mode->dmCollate =
Vladislav Kuzkokov19998222019-08-12 14:26:09218 settings_->collate() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE;
[email protected]b3b6b6f2014-02-14 20:19:54219 }
[email protected]f3256b0d82011-09-04 23:36:29220
Vladislav Kuzkokov19998222019-08-12 14:26:09221 switch (settings_->duplex_mode()) {
Julie Jeongeun Kime454f2602020-04-30 05:20:07222 case mojom::DuplexMode::kLongEdge:
[email protected]b3b6b6f2014-02-14 20:19:54223 dev_mode->dmFields |= DM_DUPLEX;
224 dev_mode->dmDuplex = DMDUP_VERTICAL;
225 break;
Julie Jeongeun Kime454f2602020-04-30 05:20:07226 case mojom::DuplexMode::kShortEdge:
[email protected]b3b6b6f2014-02-14 20:19:54227 dev_mode->dmFields |= DM_DUPLEX;
228 dev_mode->dmDuplex = DMDUP_HORIZONTAL;
229 break;
Julie Jeongeun Kime454f2602020-04-30 05:20:07230 case mojom::DuplexMode::kSimplex:
[email protected]b3b6b6f2014-02-14 20:19:54231 dev_mode->dmFields |= DM_DUPLEX;
232 dev_mode->dmDuplex = DMDUP_SIMPLEX;
233 break;
Julie Jeongeun Kime454f2602020-04-30 05:20:07234 default: // kUnknownDuplexMode
[email protected]b3b6b6f2014-02-14 20:19:54235 break;
236 }
237
238 dev_mode->dmFields |= DM_ORIENTATION;
Lei Zhang01a1d3c02019-05-21 04:59:10239 dev_mode->dmOrientation =
Vladislav Kuzkokov19998222019-08-12 14:26:09240 settings_->landscape() ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
[email protected]ad3b05012014-06-16 23:02:15241
Vladislav Kuzkokov19998222019-08-12 14:26:09242 if (settings_->dpi_horizontal() > 0) {
243 dev_mode->dmPrintQuality = settings_->dpi_horizontal();
rbpotter116c2e12017-04-04 19:21:28244 dev_mode->dmFields |= DM_PRINTQUALITY;
245 }
Vladislav Kuzkokov19998222019-08-12 14:26:09246 if (settings_->dpi_vertical() > 0) {
247 dev_mode->dmYResolution = settings_->dpi_vertical();
rbpotter116c2e12017-04-04 19:21:28248 dev_mode->dmFields |= DM_YRESOLUTION;
249 }
250
[email protected]ad3b05012014-06-16 23:02:15251 const PrintSettings::RequestedMedia& requested_media =
Vladislav Kuzkokov19998222019-08-12 14:26:09252 settings_->requested_media();
[email protected]ad3b05012014-06-16 23:02:15253 unsigned id = 0;
rbpotter1142f502017-09-29 18:09:19254 // If the paper size is a custom user size, setting by ID may not work.
255 if (base::StringToUint(requested_media.vendor_id, &id) && id &&
256 id < DMPAPER_USER) {
[email protected]ad3b05012014-06-16 23:02:15257 dev_mode->dmFields |= DM_PAPERSIZE;
258 dev_mode->dmPaperSize = static_cast<short>(id);
Lei Zhang9a9fe642017-10-03 22:07:24259 } else if (!requested_media.size_microns.IsEmpty()) {
260 static constexpr int kFromUm = 100; // Windows uses 0.1mm.
261 dev_mode->dmFields |= DM_PAPERWIDTH | DM_PAPERLENGTH;
262 dev_mode->dmPaperWidth = requested_media.size_microns.width() / kFromUm;
263 dev_mode->dmPaperLength = requested_media.size_microns.height() / kFromUm;
[email protected]ad3b05012014-06-16 23:02:15264 }
[email protected]e5324b52013-10-29 03:16:37265 }
[email protected]bd5baaf2011-04-21 20:48:33266
[email protected]30aaa81a2011-05-03 21:08:39267 // Update data using DocumentProperties.
Lei Zhanga3fdad442021-11-15 23:55:33268 if (printer_settings.show_system_dialog) {
Alan Screen202e49a2021-09-21 06:34:28269 mojom::ResultCode result = mojom::ResultCode::kFailed;
Lei Zhanga3fdad442021-11-15 23:55:33270 AskUserForSettings(printer_settings.page_count, /*has_selection=*/false,
271 /*is_scripted=*/false,
tzikea7713cd2018-03-09 09:39:09272 base::BindOnce(&AssignResult, &result));
vitalybuka12b87582014-12-08 18:57:10273 return result;
[email protected]bd5baaf2011-04-21 20:48:33274 }
vitalybuka92ab8ce2014-08-26 23:41:45275 // Set printer then refresh printer settings.
thestige85e6b62016-08-25 00:00:06276 scoped_dev_mode = CreateDevMode(printer.Get(), scoped_dev_mode.get());
Alan Screen830ededb2020-03-18 17:59:15277 if (!scoped_dev_mode)
278 return OnError();
279
280 // Since CreateDevMode() doesn't honor color settings through the GDI call
281 // to DocumentProperties(), ensure the requested values persist here.
282 scoped_dev_mode->dmFields |= DM_COLOR;
Alan Screen3fb63bf2020-08-19 00:11:23283 scoped_dev_mode->dmColor = settings_->color() != mojom::ColorModel::kGray
284 ? DMCOLOR_COLOR
285 : DMCOLOR_MONOCHROME;
Alan Screen830ededb2020-03-18 17:59:15286
Jan Wilken Dörrie00632a12021-02-24 09:42:00287 return InitializeSettings(base::UTF16ToWide(settings_->device_name()),
288 scoped_dev_mode.get());
[email protected]7868ecab2011-03-05 00:12:53289}
290
Alan Screen202e49a2021-09-21 06:34:28291mojom::ResultCode PrintingContextWin::InitWithSettingsForTest(
Vladislav Kuzkokov19998222019-08-12 14:26:09292 std::unique_ptr<PrintSettings> settings) {
initial.commit09911bf2008-07-26 23:55:29293 DCHECK(!in_print_job_);
[email protected]4993f342010-10-26 17:57:52294
Vladislav Kuzkokov19998222019-08-12 14:26:09295 settings_ = std::move(settings);
[email protected]4993f342010-10-26 17:57:52296
297 // TODO(maruel): settings_.ToDEVMODE()
[email protected]55e8e9f2012-03-02 22:50:54298 ScopedPrinterHandle printer;
Alan Screen39279cf02021-09-22 07:16:41299 if (!printer.OpenPrinterWithName(base::as_wcstr(settings_->device_name()))) {
300 return logging::GetLastSystemErrorCode() == ERROR_ACCESS_DENIED
301 ? mojom::ResultCode::kAccessDenied
302 : mojom::ResultCode::kFailed;
303 }
initial.commit09911bf2008-07-26 23:55:29304
dchengc3df9ba2016-04-07 23:09:32305 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode =
thestigb50a0c332016-09-30 04:22:45306 CreateDevMode(printer.Get(), nullptr);
initial.commit09911bf2008-07-26 23:55:29307
Jan Wilken Dörrie00632a12021-02-24 09:42:00308 return InitializeSettings(base::UTF16ToWide(settings_->device_name()),
309 dev_mode.get());
initial.commit09911bf2008-07-26 23:55:29310}
311
Alan Screen202e49a2021-09-21 06:34:28312mojom::ResultCode PrintingContextWin::NewDocument(
Jan Wilken Dörrie739ccc212021-03-11 18:13:05313 const std::u16string& document_name) {
initial.commit09911bf2008-07-26 23:55:29314 DCHECK(!in_print_job_);
Alan Screene9ed54a2021-11-24 18:22:46315 if (!context_ && !skip_system_calls())
[email protected]c8ad40c2009-06-08 17:05:21316 return OnError();
initial.commit09911bf2008-07-26 23:55:29317
318 // Set the flag used by the AbortPrintJob dialog procedure.
319 abort_printing_ = false;
320
321 in_print_job_ = true;
322
Alan Screene9ed54a2021-11-24 18:22:46323 if (skip_system_calls())
324 return mojom::ResultCode::kSuccess;
325
Alan Screen202e49a2021-09-21 06:34:28326 if (base::FeatureList::IsEnabled(printing::features::kUseXpsForPrinting)) {
327 // This is all the new document context needed when using XPS.
328 return mojom::ResultCode::kSuccess;
329 }
Alan Screen13ec987ca2019-12-10 19:23:07330
331 // Need more context setup when using GDI.
332
initial.commit09911bf2008-07-26 23:55:29333 // Register the application's AbortProc function with GDI.
[email protected]b75dca82009-10-13 18:46:21334 if (SP_ERROR == SetAbortProc(context_, &AbortProc))
[email protected]c8ad40c2009-06-08 17:05:21335 return OnError();
initial.commit09911bf2008-07-26 23:55:29336
[email protected]ec91bfa2013-11-09 17:24:27337 DCHECK(SimplifyDocumentTitle(document_name) == document_name);
Lei Zhang01a1d3c02019-05-21 04:59:10338 DOCINFO di = {sizeof(DOCINFO)};
Peter Kasting3c73bbc2021-02-02 13:01:34339 di.lpszDocName = base::as_wcstr(document_name);
initial.commit09911bf2008-07-26 23:55:29340
341 // Is there a debug dump directory specified? If so, force to print to a file.
Lei Zhangbdd63812017-12-12 06:29:51342 if (PrintedDocument::HasDebugDumpPath()) {
343 base::FilePath debug_dump_path = PrintedDocument::CreateDebugDumpPath(
344 document_name, FILE_PATH_LITERAL(".prn"));
345 if (!debug_dump_path.empty())
346 di.lpszOutput = debug_dump_path.value().c_str();
347 }
initial.commit09911bf2008-07-26 23:55:29348
[email protected]daee4972009-07-09 14:28:24349 // No message loop running in unit tests.
Carlos Caballerob25fe8472020-07-17 10:27:17350 DCHECK(!base::CurrentThread::Get() ||
351 !base::CurrentThread::Get()->NestableTasksAllowed());
[email protected]daee4972009-07-09 14:28:24352
initial.commit09911bf2008-07-26 23:55:29353 // Begin a print job by calling the StartDoc function.
354 // NOTE: StartDoc() starts a message loop. That causes a lot of problems with
355 // IPC. Make sure recursive task processing is disabled.
[email protected]b75dca82009-10-13 18:46:21356 if (StartDoc(context_, &di) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21357 return OnError();
initial.commit09911bf2008-07-26 23:55:29358
Alan Screen202e49a2021-09-21 06:34:28359 return mojom::ResultCode::kSuccess;
initial.commit09911bf2008-07-26 23:55:29360}
361
Alan Screen4e284e9cb2021-12-04 00:38:29362mojom::ResultCode PrintingContextWin::RenderPage(const PrintedPage& page,
363 const PageSetup& page_setup) {
364 if (abort_printing_)
365 return mojom::ResultCode::kCanceled;
366 DCHECK(context_);
367 DCHECK(in_print_job_);
368
369 gfx::Rect content_area = GetCenteredPageContentRect(
370 page_setup.physical_size(), page.page_size(), page.page_content_rect());
371
372 // Save the state to make sure the context this function call does not modify
373 // the device context.
374 ScopedSavedState saved_state(context_);
375 skia::InitializeDC(context_);
376 {
377 // Save the state (again) to apply the necessary world transformation.
378 ScopedSavedState saved_state_inner(context_);
379
380 // Setup the matrix to translate and scale to the right place. Take in
381 // account the actual shrinking factor.
382 // Note that the printing output is relative to printable area of the page.
383 // That is 0,0 is offset by PHYSICALOFFSETX/Y from the page.
384 SimpleModifyWorldTransform(
385 context_, content_area.x() - page_setup.printable_area().x(),
386 content_area.y() - page_setup.printable_area().y(),
387 page.shrink_factor());
388
389 if (::StartPage(context_) <= 0)
390 return mojom::ResultCode::kFailed;
391 bool played_back = page.metafile()->SafePlayback(context_);
392 DCHECK(played_back);
393 if (::EndPage(context_) <= 0)
394 return mojom::ResultCode::kFailed;
395 }
396
397 return mojom::ResultCode::kSuccess;
398}
399
Alan Screen4e284e9cb2021-12-04 00:38:29400mojom::ResultCode PrintingContextWin::PrintDocument(
401 const MetafilePlayer& metafile,
402 const PrintSettings& settings,
403 uint32_t num_pages) {
404 // TODO(crbug.com/1008222)
405 NOTIMPLEMENTED();
406 return mojom::ResultCode::kFailed;
407}
408
Alan Screen202e49a2021-09-21 06:34:28409mojom::ResultCode PrintingContextWin::DocumentDone() {
initial.commit09911bf2008-07-26 23:55:29410 if (abort_printing_)
Alan Screen202e49a2021-09-21 06:34:28411 return mojom::ResultCode::kCanceled;
initial.commit09911bf2008-07-26 23:55:29412 DCHECK(in_print_job_);
[email protected]60745412010-09-27 23:46:07413 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29414
415 // Inform the driver that document has ended.
[email protected]b75dca82009-10-13 18:46:21416 if (EndDoc(context_) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21417 return OnError();
initial.commit09911bf2008-07-26 23:55:29418
Vladislav Kuzkokov56e6bbbd2019-08-21 18:48:18419 ResetSettings();
Alan Screen202e49a2021-09-21 06:34:28420 return mojom::ResultCode::kSuccess;
initial.commit09911bf2008-07-26 23:55:29421}
422
[email protected]51e8d9352010-10-06 22:21:17423void PrintingContextWin::Cancel() {
initial.commit09911bf2008-07-26 23:55:29424 abort_printing_ = true;
425 in_print_job_ = false;
[email protected]b75dca82009-10-13 18:46:21426 if (context_)
427 CancelDC(context_);
initial.commit09911bf2008-07-26 23:55:29428}
429
[email protected]51e8d9352010-10-06 22:21:17430void PrintingContextWin::ReleaseContext() {
431 if (context_) {
432 DeleteDC(context_);
thestigb50a0c332016-09-30 04:22:45433 context_ = nullptr;
[email protected]51e8d9352010-10-06 22:21:17434 }
435}
436
Nico Weber8e559562017-10-03 01:25:26437printing::NativeDrawingContext PrintingContextWin::context() const {
[email protected]51e8d9352010-10-06 22:21:17438 return context_;
initial.commit09911bf2008-07-26 23:55:29439}
440
441// static
[email protected]51e8d9352010-10-06 22:21:17442BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) {
initial.commit09911bf2008-07-26 23:55:29443 if (nCode) {
444 // TODO(maruel): Need a way to find the right instance to set. Should
445 // leverage PrintJobManager here?
446 // abort_printing_ = true;
447 }
448 return true;
449}
450
Alan Screen202e49a2021-09-21 06:34:28451mojom::ResultCode PrintingContextWin::InitializeSettings(
Jan Wilken Dörrie00632a12021-02-24 09:42:00452 const std::wstring& device_name,
vitalybuka92ab8ce2014-08-26 23:41:45453 DEVMODE* dev_mode) {
454 if (!dev_mode)
455 return OnError();
456
457 ReleaseContext();
Jan Wilken Dörrie00632a12021-02-24 09:42:00458 context_ = CreateDC(L"WINSPOOL", device_name.c_str(), nullptr, dev_mode);
vitalybuka92ab8ce2014-08-26 23:41:45459 if (!context_)
460 return OnError();
461
[email protected]62f2e802011-05-26 14:28:35462 skia::InitializeDC(context_);
initial.commit09911bf2008-07-26 23:55:29463
464 DCHECK(!in_print_job_);
Jan Wilken Dörrie00632a12021-02-24 09:42:00465 settings_->set_device_name(base::WideToUTF16(device_name));
Lei Zhang01a1d3c02019-05-21 04:59:10466 PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode,
Vladislav Kuzkokov19998222019-08-12 14:26:09467 settings_.get());
[email protected]4993f342010-10-26 17:57:52468
Alan Screen202e49a2021-09-21 06:34:28469 return mojom::ResultCode::kSuccess;
initial.commit09911bf2008-07-26 23:55:29470}
471
Alan Screen39279cf02021-09-22 07:16:41472mojom::ResultCode PrintingContextWin::OnError() {
473 mojom::ResultCode result;
474 if (abort_printing_) {
475 result = mojom::ResultCode::kCanceled;
476 } else {
477 result = logging::GetLastSystemErrorCode() == ERROR_ACCESS_DENIED
478 ? mojom::ResultCode::kAccessDenied
479 : mojom::ResultCode::kFailed;
480 }
481 ResetSettings();
482 return result;
483}
484
vitalybukaf9d0c0c2014-09-09 19:53:33485HWND PrintingContextWin::GetRootWindow(gfx::NativeView view) {
thestigb50a0c332016-09-30 04:22:45486 HWND window = nullptr;
vitalybukaa58ec6a2015-02-23 18:30:49487 if (view && view->GetHost())
vitalybukaf9d0c0c2014-09-09 19:53:33488 window = view->GetHost()->GetAcceleratedWidget();
vitalybukaf9d0c0c2014-09-09 19:53:33489 if (!window) {
thestigb50a0c332016-09-30 04:22:45490 // TODO(maruel): b/1214347 Get the right browser window instead.
vitalybukaf9d0c0c2014-09-09 19:53:33491 return GetDesktopWindow();
492 }
493 return window;
494}
495
initial.commit09911bf2008-07-26 23:55:29496} // namespace printing