Chromium Code Reviews| 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); | |
| 481 if (numLanguages != 1) | |
| 482 return false; | |
| 483 wchar_t* buf = static_cast<wchar_t*>(calloc(33 * sizeof(wchar_t), 1)); | |
|
Lei Zhang
2017/03/08 20:27:22
Did we forget to free |buf| ? Maybe use std::vecto
rbpotter
2017/03/08 23:25:25
Done.
| |
| 484 memset(buf, 0, 33 * sizeof(wchar_t)); | |
| 485 DeviceCapabilities(name, port, DC_PERSONALITY, buf, NULL); | |
| 486 static constexpr wchar_t kRPCSLanguage[] = L"RPCS"; | |
| 487 if (wcscmp(buf, kRPCSLanguage) == 0) | |
|
Lei Zhang
2017/03/08 20:27:22
Just: return wcscmp(buf, kRPCSLanguage) == 0;
rbpotter
2017/03/08 23:25:25
Done.
| |
| 488 return true; | |
| 489 return false; | |
| 490 } | |
| 491 | |
| 473 std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer, | 492 std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer, |
| 474 DEVMODE* in) { | 493 DEVMODE* in) { |
| 475 LONG buffer_size = DocumentProperties( | 494 LONG buffer_size = DocumentProperties( |
| 476 NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0); | 495 NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0); |
| 477 if (buffer_size < static_cast<int>(sizeof(DEVMODE))) | 496 if (buffer_size < static_cast<int>(sizeof(DEVMODE))) |
| 478 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | 497 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); |
| 479 | 498 |
| 480 // Some drivers request buffers with size smaller than dmSize + dmDriverExtra. | 499 // Some drivers request buffers with size smaller than dmSize + dmDriverExtra. |
| 481 // crbug.com/421402 | 500 // crbug.com/421402 |
| 482 buffer_size *= 2; | 501 buffer_size *= 2; |
| 483 | 502 |
| 484 std::unique_ptr<DEVMODE, base::FreeDeleter> out( | 503 std::unique_ptr<DEVMODE, base::FreeDeleter> out( |
| 485 reinterpret_cast<DEVMODE*>(calloc(buffer_size, 1))); | 504 reinterpret_cast<DEVMODE*>(calloc(buffer_size, 1))); |
| 486 DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER; | 505 DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER; |
| 506 | |
| 507 // Check for RPCS drivers on Windows 8+ as DocumentProperties will crash if | |
| 508 // called on one of these printers. See crbug.com/679160 | |
| 509 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && | |
| 510 IsPrinterRPCSOnly(printer)) { | |
| 511 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | |
| 512 } | |
| 513 | |
| 487 if (DocumentProperties( | 514 if (DocumentProperties( |
| 488 NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) != | 515 NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) != |
| 489 IDOK) { | 516 IDOK) { |
| 490 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | 517 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); |
| 491 } | 518 } |
| 492 int size = out->dmSize; | 519 int size = out->dmSize; |
| 493 int extra_size = out->dmDriverExtra; | 520 int extra_size = out->dmDriverExtra; |
| 494 CHECK_GE(buffer_size, size + extra_size); | 521 CHECK_GE(buffer_size, size + extra_size); |
| 495 return out; | 522 return out; |
| 496 } | 523 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 *canceled = (result == IDCANCEL); | 555 *canceled = (result == IDCANCEL); |
| 529 if (result != IDOK) | 556 if (result != IDOK) |
| 530 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); | 557 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); |
| 531 int size = out->dmSize; | 558 int size = out->dmSize; |
| 532 int extra_size = out->dmDriverExtra; | 559 int extra_size = out->dmDriverExtra; |
| 533 CHECK_GE(buffer_size, size + extra_size); | 560 CHECK_GE(buffer_size, size + extra_size); |
| 534 return out; | 561 return out; |
| 535 } | 562 } |
| 536 | 563 |
| 537 } // namespace printing | 564 } // namespace printing |
| OLD | NEW |