[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 5 | #include "printing/printing_context_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
vitalybuka | 12b8758 | 2014-12-08 18:57:10 | [diff] [blame] | 9 | #include "base/bind.h" |
dcheng | db5935f | 2016-03-26 00:16:27 | [diff] [blame^] | 10 | #include "base/memory/free_deleter.h" |
[email protected] | ad3b0501 | 2014-06-16 23:02:15 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 09083eb | 2012-09-20 05:58:43 | [diff] [blame] | 13 | #include "printing/backend/print_backend.h" |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 14 | #include "printing/backend/win_helper.h" |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 15 | #include "printing/print_settings_initializer_win.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 16 | #include "printing/printed_document.h" |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 17 | #include "printing/printing_context_system_dialog_win.h" |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 18 | #include "printing/printing_utils.h" |
[email protected] | e5636a5 | 2011-09-28 21:44:42 | [diff] [blame] | 19 | #include "printing/units.h" |
tomhudson | 828dddb | 2015-12-04 14:34:16 | [diff] [blame] | 20 | #include "skia/ext/skia_utils_win.h" |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 21 | #include "ui/aura/window.h" |
samli | 6d8217f | 2016-01-20 04:12:20 | [diff] [blame] | 22 | #include "ui/aura/window_tree_host.h" |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | namespace printing { |
| 25 | |
vitalybuka | 12b8758 | 2014-12-08 18:57:10 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | void AssingResult(PrintingContext::Result* out, PrintingContext::Result in) { |
| 29 | *out = in; |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 34 | // static |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 35 | scoped_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { |
vitalybuka | e29991c | 2014-11-05 21:15:12 | [diff] [blame] | 36 | #if defined(ENABLE_BASIC_PRINTING) |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 37 | return make_scoped_ptr<PrintingContext>( |
| 38 | new PrintingContextSytemDialogWin(delegate)); |
vitalybuka | e29991c | 2014-11-05 21:15:12 | [diff] [blame] | 39 | #else // ENABLE_BASIC_PRINTING |
| 40 | return make_scoped_ptr<PrintingContext>(new PrintingContextWin(delegate)); |
| 41 | #endif // EENABLE_BASIC_PRINTING |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 42 | } |
| 43 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 44 | PrintingContextWin::PrintingContextWin(Delegate* delegate) |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 45 | : PrintingContext(delegate), context_(NULL) { |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 46 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 48 | PrintingContextWin::~PrintingContextWin() { |
| 49 | ReleaseContext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 52 | void PrintingContextWin::AskUserForSettings( |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 53 | int max_pages, |
| 54 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 55 | bool is_scripted, |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 56 | const PrintSettingsCallback& callback) { |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 57 | NOTIMPLEMENTED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 60 | PrintingContext::Result PrintingContextWin::UseDefaultSettings() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | DCHECK(!in_print_job_); |
| 62 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 63 | scoped_refptr<PrintBackend> backend = PrintBackend::CreateInstance(NULL); |
| 64 | base::string16 default_printer = |
| 65 | base::UTF8ToWide(backend->GetDefaultPrinterName()); |
| 66 | if (!default_printer.empty()) { |
| 67 | ScopedPrinterHandle printer; |
| 68 | if (printer.OpenPrinter(default_printer.c_str())) { |
| 69 | scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 70 | CreateDevMode(printer.Get(), NULL); |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 71 | if (InitializeSettings(default_printer, dev_mode.get()) == OK) |
| 72 | return OK; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | ReleaseContext(); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 77 | |
| 78 | // No default printer configured, do we have any printers at all? |
| 79 | DWORD bytes_needed = 0; |
| 80 | DWORD count_returned = 0; |
| 81 | (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 82 | NULL, 2, NULL, 0, &bytes_needed, &count_returned); |
| 83 | if (bytes_needed) { |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 84 | DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2)); |
[email protected] | fb3a97c | 2013-04-11 04:48:01 | [diff] [blame] | 85 | scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 86 | BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 87 | NULL, 2, printer_info_buffer.get(), |
| 88 | bytes_needed, &bytes_needed, |
| 89 | &count_returned); |
| 90 | if (ret && count_returned) { // have printers |
| 91 | // Open the first successfully found printer. |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 92 | const PRINTER_INFO_2* info_2 = |
| 93 | reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
| 94 | const PRINTER_INFO_2* info_2_end = info_2 + count_returned; |
| 95 | for (; info_2 < info_2_end; ++info_2) { |
| 96 | ScopedPrinterHandle printer; |
| 97 | if (!printer.OpenPrinter(info_2->pPrinterName)) |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 98 | continue; |
[email protected] | 2b364be | 2014-03-11 00:49:49 | [diff] [blame] | 99 | scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 100 | CreateDevMode(printer.Get(), NULL); |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 101 | if (InitializeSettings(info_2->pPrinterName, dev_mode.get()) == OK) |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 102 | return OK; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 103 | } |
| 104 | if (context_) |
| 105 | return OK; |
| 106 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | } |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 108 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 109 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 112 | gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() { |
| 113 | // Default fallback to Letter size. |
| 114 | gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch); |
| 115 | |
| 116 | // Get settings from locale. Paper type buffer length is at most 4. |
| 117 | const int paper_type_buffer_len = 4; |
| 118 | wchar_t paper_type_buffer[paper_type_buffer_len] = {0}; |
| 119 | GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer, |
| 120 | paper_type_buffer_len); |
| 121 | if (wcslen(paper_type_buffer)) { // The call succeeded. |
| 122 | int paper_code = _wtoi(paper_type_buffer); |
| 123 | switch (paper_code) { |
| 124 | case DMPAPER_LEGAL: |
| 125 | paper_size.SetSize(kLegalWidthInch, kLegalHeightInch); |
| 126 | break; |
| 127 | case DMPAPER_A4: |
| 128 | paper_size.SetSize(kA4WidthInch, kA4HeightInch); |
| 129 | break; |
| 130 | case DMPAPER_A3: |
| 131 | paper_size.SetSize(kA3WidthInch, kA3HeightInch); |
| 132 | break; |
| 133 | default: // DMPAPER_LETTER is used for default fallback. |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | return gfx::Size( |
| 138 | paper_size.width() * settings_.device_units_per_inch(), |
| 139 | paper_size.height() * settings_.device_units_per_inch()); |
| 140 | } |
| 141 | |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 142 | PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 143 | bool external_preview, |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 144 | bool show_system_dialog, |
| 145 | int page_count) { |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 146 | DCHECK(!in_print_job_); |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 147 | DCHECK(!external_preview) << "Not implemented"; |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 148 | |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 149 | ScopedPrinterHandle printer; |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 150 | if (!printer.OpenPrinter(settings_.device_name().c_str())) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 151 | return OnError(); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 152 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 153 | // Make printer changes local to Chrome. |
| 154 | // See MSDN documentation regarding DocumentProperties. |
[email protected] | 2b364be | 2014-03-11 00:49:49 | [diff] [blame] | 155 | scoped_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode = |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 156 | CreateDevModeWithColor(printer.Get(), settings_.device_name(), |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 157 | settings_.color() != GRAY); |
| 158 | if (!scoped_dev_mode) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 159 | return OnError(); |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 160 | |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 161 | { |
| 162 | DEVMODE* dev_mode = scoped_dev_mode.get(); |
| 163 | dev_mode->dmCopies = std::max(settings_.copies(), 1); |
[email protected] | 299e3d00 | 2014-06-03 08:46:49 | [diff] [blame] | 164 | if (dev_mode->dmCopies > 1) { // do not change unless multiple copies |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 165 | dev_mode->dmFields |= DM_COPIES; |
| 166 | dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE : |
| 167 | DMCOLLATE_FALSE; |
| 168 | } |
[email protected] | f3256b0d8 | 2011-09-04 23:36:29 | [diff] [blame] | 169 | |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 170 | switch (settings_.duplex_mode()) { |
| 171 | case LONG_EDGE: |
| 172 | dev_mode->dmFields |= DM_DUPLEX; |
| 173 | dev_mode->dmDuplex = DMDUP_VERTICAL; |
| 174 | break; |
| 175 | case SHORT_EDGE: |
| 176 | dev_mode->dmFields |= DM_DUPLEX; |
| 177 | dev_mode->dmDuplex = DMDUP_HORIZONTAL; |
| 178 | break; |
| 179 | case SIMPLEX: |
| 180 | dev_mode->dmFields |= DM_DUPLEX; |
| 181 | dev_mode->dmDuplex = DMDUP_SIMPLEX; |
| 182 | break; |
| 183 | default: // UNKNOWN_DUPLEX_MODE |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | dev_mode->dmFields |= DM_ORIENTATION; |
| 188 | dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE : |
| 189 | DMORIENT_PORTRAIT; |
[email protected] | ad3b0501 | 2014-06-16 23:02:15 | [diff] [blame] | 190 | |
| 191 | const PrintSettings::RequestedMedia& requested_media = |
| 192 | settings_.requested_media(); |
| 193 | static const int kFromUm = 100; // Windows uses 0.1mm. |
| 194 | int width = requested_media.size_microns.width() / kFromUm; |
| 195 | int height = requested_media.size_microns.height() / kFromUm; |
| 196 | unsigned id = 0; |
| 197 | if (base::StringToUint(requested_media.vendor_id, &id) && id) { |
| 198 | dev_mode->dmFields |= DM_PAPERSIZE; |
| 199 | dev_mode->dmPaperSize = static_cast<short>(id); |
| 200 | } else if (width > 0 && height > 0) { |
| 201 | dev_mode->dmFields |= DM_PAPERWIDTH; |
| 202 | dev_mode->dmPaperWidth = width; |
| 203 | dev_mode->dmFields |= DM_PAPERLENGTH; |
| 204 | dev_mode->dmPaperLength = height; |
| 205 | } |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 206 | } |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 207 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 208 | // Update data using DocumentProperties. |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 209 | if (show_system_dialog) { |
vitalybuka | 12b8758 | 2014-12-08 18:57:10 | [diff] [blame] | 210 | PrintingContext::Result result = PrintingContext::FAILED; |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 211 | AskUserForSettings(page_count, false, false, |
| 212 | base::Bind(&AssingResult, &result)); |
vitalybuka | 12b8758 | 2014-12-08 18:57:10 | [diff] [blame] | 213 | return result; |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 214 | } else { |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 215 | scoped_dev_mode = CreateDevMode(printer.Get(), scoped_dev_mode.get()); |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 216 | } |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 217 | // Set printer then refresh printer settings. |
| 218 | return InitializeSettings(settings_.device_name(), scoped_dev_mode.get()); |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 221 | PrintingContext::Result PrintingContextWin::InitWithSettings( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | const PrintSettings& settings) { |
| 223 | DCHECK(!in_print_job_); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 224 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | settings_ = settings; |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 226 | |
| 227 | // TODO(maruel): settings_.ToDEVMODE() |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 228 | ScopedPrinterHandle printer; |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 229 | if (!printer.OpenPrinter(settings_.device_name().c_str())) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | return FAILED; |
| 231 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 232 | scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 233 | CreateDevMode(printer.Get(), NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 235 | return InitializeSettings(settings_.device_name(), dev_mode.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 238 | PrintingContext::Result PrintingContextWin::NewDocument( |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 239 | const base::string16& document_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 241 | if (!context_) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 242 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | |
| 244 | // Set the flag used by the AbortPrintJob dialog procedure. |
| 245 | abort_printing_ = false; |
| 246 | |
| 247 | in_print_job_ = true; |
| 248 | |
| 249 | // Register the application's AbortProc function with GDI. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 250 | if (SP_ERROR == SetAbortProc(context_, &AbortProc)) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 251 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 253 | DCHECK(SimplifyDocumentTitle(document_name) == document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | DOCINFO di = { sizeof(DOCINFO) }; |
[email protected] | 299e3d00 | 2014-06-03 08:46:49 | [diff] [blame] | 255 | di.lpszDocName = document_name.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | |
| 257 | // Is there a debug dump directory specified? If so, force to print to a file. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 258 | base::string16 debug_dump_path = |
| 259 | PrintedDocument::CreateDebugDumpPath(document_name, |
| 260 | FILE_PATH_LITERAL(".prn")).value(); |
| 261 | if (!debug_dump_path.empty()) |
| 262 | di.lpszOutput = debug_dump_path.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 264 | // No message loop running in unit tests. |
[email protected] | f58ef2b | 2013-05-06 22:43:57 | [diff] [blame] | 265 | DCHECK(!base::MessageLoop::current() || |
| 266 | !base::MessageLoop::current()->NestableTasksAllowed()); |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 267 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | // Begin a print job by calling the StartDoc function. |
| 269 | // NOTE: StartDoc() starts a message loop. That causes a lot of problems with |
| 270 | // IPC. Make sure recursive task processing is disabled. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 271 | if (StartDoc(context_, &di) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 272 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | return OK; |
| 275 | } |
| 276 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 277 | PrintingContext::Result PrintingContextWin::NewPage() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | if (abort_printing_) |
| 279 | return CANCEL; |
[email protected] | 3b52c98 | 2010-09-27 20:40:36 | [diff] [blame] | 280 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | DCHECK(in_print_job_); |
| 282 | |
vitalybuka | fdd3212ca | 2014-08-29 03:11:56 | [diff] [blame] | 283 | // Intentional No-op. PdfMetafileSkia::SafePlayback takes care of calling |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 284 | // ::StartPage(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | return OK; |
| 287 | } |
| 288 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 289 | PrintingContext::Result PrintingContextWin::PageDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | if (abort_printing_) |
| 291 | return CANCEL; |
| 292 | DCHECK(in_print_job_); |
| 293 | |
vitalybuka | fdd3212ca | 2014-08-29 03:11:56 | [diff] [blame] | 294 | // Intentional No-op. PdfMetafileSkia::SafePlayback takes care of calling |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 295 | // ::EndPage(). |
| 296 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | return OK; |
| 298 | } |
| 299 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 300 | PrintingContext::Result PrintingContextWin::DocumentDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | if (abort_printing_) |
| 302 | return CANCEL; |
| 303 | DCHECK(in_print_job_); |
[email protected] | 6074541 | 2010-09-27 23:46:07 | [diff] [blame] | 304 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | |
| 306 | // Inform the driver that document has ended. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 307 | if (EndDoc(context_) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 308 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 309 | |
| 310 | ResetSettings(); |
| 311 | return OK; |
| 312 | } |
| 313 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 314 | void PrintingContextWin::Cancel() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | abort_printing_ = true; |
| 316 | in_print_job_ = false; |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 317 | if (context_) |
| 318 | CancelDC(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 321 | void PrintingContextWin::ReleaseContext() { |
| 322 | if (context_) { |
| 323 | DeleteDC(context_); |
| 324 | context_ = NULL; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | gfx::NativeDrawingContext PrintingContextWin::context() const { |
| 329 | return context_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // static |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 333 | BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | if (nCode) { |
| 335 | // TODO(maruel): Need a way to find the right instance to set. Should |
| 336 | // leverage PrintJobManager here? |
| 337 | // abort_printing_ = true; |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 342 | PrintingContext::Result PrintingContextWin::InitializeSettings( |
| 343 | const std::wstring& device_name, |
| 344 | DEVMODE* dev_mode) { |
| 345 | if (!dev_mode) |
| 346 | return OnError(); |
| 347 | |
| 348 | ReleaseContext(); |
| 349 | context_ = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); |
| 350 | if (!context_) |
| 351 | return OnError(); |
| 352 | |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 353 | skia::InitializeDC(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | |
| 355 | DCHECK(!in_print_job_); |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 356 | settings_.set_device_name(device_name); |
| 357 | PrintSettingsInitializerWin::InitPrintSettings( |
| 358 | context_, *dev_mode, &settings_); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 359 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 360 | return OK; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | } |
| 362 | |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 363 | HWND PrintingContextWin::GetRootWindow(gfx::NativeView view) { |
| 364 | HWND window = NULL; |
vitalybuka | a58ec6a | 2015-02-23 18:30:49 | [diff] [blame] | 365 | if (view && view->GetHost()) |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 366 | window = view->GetHost()->GetAcceleratedWidget(); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 367 | if (!window) { |
| 368 | // TODO(maruel): crbug.com/1214347 Get the right browser window instead. |
| 369 | return GetDesktopWindow(); |
| 370 | } |
| 371 | return window; |
| 372 | } |
| 373 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | } // namespace printing |