OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "printing/backend/win_helper.h" | 5 #include "printing/backend/win_helper.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/file_version_info.h" | 12 #include "base/file_version_info.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/free_deleter.h" | 16 #include "base/memory/free_deleter.h" |
17 #include "base/numerics/safe_conversions.h" | 17 #include "base/numerics/safe_conversions.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/win/scoped_comptr.h" | 21 #include "base/win/scoped_comptr.h" |
22 #include "base/win/windows_version.h" | |
22 #include "printing/backend/print_backend.h" | 23 #include "printing/backend/print_backend.h" |
23 #include "printing/backend/print_backend_consts.h" | 24 #include "printing/backend/print_backend_consts.h" |
24 #include "printing/backend/printing_info_win.h" | 25 #include "printing/backend/printing_info_win.h" |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 typedef HRESULT (WINAPI* PTOpenProviderProc)(PCWSTR printer_name, | 29 typedef HRESULT (WINAPI* PTOpenProviderProc)(PCWSTR printer_name, |
29 DWORD version, | 30 DWORD version, |
30 HPTPROVIDER* provider); | 31 HPTPROVIDER* provider); |
31 | 32 |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 const char* xps_color = color ? kXpsTicketColor : kXpsTicketMonochrome; | 464 const char* xps_color = color ? kXpsTicketColor : kXpsTicketMonochrome; |
464 std::string xps_ticket = base::StringPrintf(kXpsTicketTemplate, xps_color); | 465 std::string xps_ticket = base::StringPrintf(kXpsTicketTemplate, xps_color); |
465 std::unique_ptr<DEVMODE, base::FreeDeleter> ticket = | 466 std::unique_ptr<DEVMODE, base::FreeDeleter> ticket = |
466 printing::XpsTicketToDevMode(printer_name, xps_ticket); | 467 printing::XpsTicketToDevMode(printer_name, xps_ticket); |
467 if (!ticket) | 468 if (!ticket) |
468 return default_ticket; | 469 return default_ticket; |
469 | 470 |
470 return ticket; | 471 return ticket; |
471 } | 472 } |
472 | 473 |
474 bool IsPrinterRPCSOnly(HANDLE printer) { | |
475 PrinterInfo5 info_5; | |
476 if (!info_5.Init(printer)) | |
477 return false; | |
478 const wchar_t* name = info_5.get()->pPrinterName; | |
479 const wchar_t* port = info_5.get()->pPortName; | |
480 int numLanguages = DeviceCapabilities(name, port, DC_PERSONALITY, NULL, NULL); | |
Lei Zhang
2017/03/08 23:32:20
num_languages
rbpotter
2017/03/09 00:42:48
Done.
| |
481 if (numLanguages != 1) | |
482 return false; | |
483 std::vector<wchar_t> buf(33); | |
484 memset(buf.data(), 0, 33 * sizeof(wchar_t)); | |
Lei Zhang
2017/03/08 23:32:20
std::vector<wchar_t> buf(33, 0);
rbpotter
2017/03/09 00:42:48
Done.
| |
485 DeviceCapabilities(name, port, DC_PERSONALITY, buf.data(), NULL); | |
486 static constexpr wchar_t kRPCSLanguage[] = L"RPCS"; | |
487 return wcscmp(buf.data(), kRPCSLanguage) == 0; | |
488 } | |
489 | |
473 std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer, | 490 std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer, |
474 DEVMODE* in) { | 491 DEVMODE* in) { |
475 LONG buffer_size = DocumentProperties( | 492 LONG buffer_size = DocumentProperties( |
476 NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0); | 493 NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0); |
477 if (buffer_size < static_cast<int>(sizeof(DEVMODE))) | 494 if (buffer_size < static_cast<int>(sizeof(DEVMODE))) |
478 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | 495 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); |
479 | 496 |
480 // Some drivers request buffers with size smaller than dmSize + dmDriverExtra. | 497 // Some drivers request buffers with size smaller than dmSize + dmDriverExtra. |
481 // crbug.com/421402 | 498 // crbug.com/421402 |
482 buffer_size *= 2; | 499 buffer_size *= 2; |
483 | 500 |
484 std::unique_ptr<DEVMODE, base::FreeDeleter> out( | 501 std::unique_ptr<DEVMODE, base::FreeDeleter> out( |
485 reinterpret_cast<DEVMODE*>(calloc(buffer_size, 1))); | 502 reinterpret_cast<DEVMODE*>(calloc(buffer_size, 1))); |
486 DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER; | 503 DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER; |
504 | |
505 // Check for RPCS drivers on Windows 8+ as DocumentProperties will crash if | |
506 // called on one of these printers. See crbug.com/679160 | |
507 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && | |
508 IsPrinterRPCSOnly(printer)) { | |
509 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | |
510 } | |
511 | |
487 if (DocumentProperties( | 512 if (DocumentProperties( |
488 NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) != | 513 NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) != |
489 IDOK) { | 514 IDOK) { |
490 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | 515 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); |
491 } | 516 } |
492 int size = out->dmSize; | 517 int size = out->dmSize; |
493 int extra_size = out->dmDriverExtra; | 518 int extra_size = out->dmDriverExtra; |
494 CHECK_GE(buffer_size, size + extra_size); | 519 CHECK_GE(buffer_size, size + extra_size); |
495 return out; | 520 return out; |
496 } | 521 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 *canceled = (result == IDCANCEL); | 553 *canceled = (result == IDCANCEL); |
529 if (result != IDOK) | 554 if (result != IDOK) |
530 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | 555 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); |
531 int size = out->dmSize; | 556 int size = out->dmSize; |
532 int extra_size = out->dmDriverExtra; | 557 int extra_size = out->dmDriverExtra; |
533 CHECK_GE(buffer_size, size + extra_size); | 558 CHECK_GE(buffer_size, size + extra_size); |
534 return out; | 559 return out; |
535 } | 560 } |
536 | 561 |
537 } // namespace printing | 562 } // namespace printing |
OLD | NEW |