win_helper.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2012 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. #ifndef PRINTING_BACKEND_WIN_HELPER_H_
  5. #define PRINTING_BACKEND_WIN_HELPER_H_
  6. #include <objidl.h>
  7. #include <prntvpt.h>
  8. // Important to include wincrypt_shim.h before xpsprint.h since
  9. // xpsprint.h includes <wincrypt.h> (xpsprint.h -> msopc.h ->
  10. // wincrypt.h) which in its normal state is incompatible with
  11. // OpenSSL/BoringSSL.
  12. #include "base/win/wincrypt_shim.h"
  13. #include <xpsprint.h>
  14. #include <memory>
  15. #include <string>
  16. #include "base/component_export.h"
  17. #include "base/memory/free_deleter.h"
  18. #include "base/win/scoped_handle.h"
  19. // These are helper functions for dealing with Windows Printing.
  20. namespace printing {
  21. struct COMPONENT_EXPORT(PRINT_BACKEND) PrinterBasicInfo;
  22. class COMPONENT_EXPORT(PRINT_BACKEND) PrinterHandleTraits {
  23. public:
  24. PrinterHandleTraits() = delete;
  25. PrinterHandleTraits(const PrinterHandleTraits&) = delete;
  26. PrinterHandleTraits& operator=(const PrinterHandleTraits&) = delete;
  27. using Handle = HANDLE;
  28. static bool CloseHandle(HANDLE handle);
  29. static bool IsHandleValid(HANDLE handle) { return !!handle; }
  30. static HANDLE NullHandle() { return nullptr; }
  31. };
  32. class COMPONENT_EXPORT(PRINT_BACKEND) ScopedPrinterHandle
  33. : public base::win::GenericScopedHandle<PrinterHandleTraits,
  34. base::win::DummyVerifierTraits> {
  35. public:
  36. bool OpenPrinterWithName(const wchar_t* printer);
  37. };
  38. class COMPONENT_EXPORT(PRINT_BACKEND) PrinterChangeHandleTraits {
  39. public:
  40. PrinterChangeHandleTraits() = delete;
  41. PrinterChangeHandleTraits(const PrinterChangeHandleTraits&) = delete;
  42. PrinterChangeHandleTraits& operator=(const PrinterChangeHandleTraits&) =
  43. delete;
  44. using Handle = HANDLE;
  45. static bool CloseHandle(HANDLE handle);
  46. static bool IsHandleValid(HANDLE handle) { return !!handle; }
  47. static HANDLE NullHandle() { return nullptr; }
  48. };
  49. using ScopedPrinterChangeHandle =
  50. base::win::GenericScopedHandle<PrinterChangeHandleTraits,
  51. base::win::DummyVerifierTraits>;
  52. // Wrapper class to wrap the XPS APIs (PTxxx APIs). This is needed because these
  53. // APIs are not available by default on XP. We could delayload prntvpt.dll but
  54. // this would mean having to add that to every binary that links with
  55. // printing.lib (which is a LOT of binaries). So choosing the GetProcAddress
  56. // route instead).
  57. class COMPONENT_EXPORT(PRINT_BACKEND) XPSModule {
  58. public:
  59. // All the other methods can ONLY be called after a successful call to Init.
  60. // Init can be called many times and by multiple threads.
  61. static bool Init();
  62. static HRESULT OpenProvider(const std::wstring& printer_name,
  63. DWORD version,
  64. HPTPROVIDER* provider);
  65. static HRESULT GetPrintCapabilities(HPTPROVIDER provider,
  66. IStream* print_ticket,
  67. IStream* capabilities,
  68. BSTR* error_message);
  69. static HRESULT ConvertDevModeToPrintTicket(HPTPROVIDER provider,
  70. ULONG devmode_size_in_bytes,
  71. PDEVMODE devmode,
  72. EPrintTicketScope scope,
  73. IStream* print_ticket);
  74. static HRESULT ConvertPrintTicketToDevMode(
  75. HPTPROVIDER provider,
  76. IStream* print_ticket,
  77. EDefaultDevmodeType base_devmode_type,
  78. EPrintTicketScope scope,
  79. ULONG* devmode_byte_count,
  80. PDEVMODE* devmode,
  81. BSTR* error_message);
  82. static HRESULT MergeAndValidatePrintTicket(HPTPROVIDER provider,
  83. IStream* base_ticket,
  84. IStream* delta_ticket,
  85. EPrintTicketScope scope,
  86. IStream* result_ticket,
  87. BSTR* error_message);
  88. static HRESULT ReleaseMemory(PVOID buffer);
  89. static HRESULT CloseProvider(HPTPROVIDER provider);
  90. private:
  91. XPSModule() {}
  92. static bool InitImpl();
  93. };
  94. // See comments in cc file explaining why we need this.
  95. class COMPONENT_EXPORT(PRINT_BACKEND) ScopedXPSInitializer {
  96. public:
  97. ScopedXPSInitializer();
  98. ScopedXPSInitializer(const ScopedXPSInitializer&) = delete;
  99. ScopedXPSInitializer& operator=(const ScopedXPSInitializer&) = delete;
  100. ~ScopedXPSInitializer();
  101. bool initialized() const { return initialized_; }
  102. private:
  103. bool initialized_;
  104. };
  105. // Wrapper class to wrap the XPS Print APIs (these are different from the PTxxx
  106. // which deal with the XML Print Schema). This is needed because these
  107. // APIs are only available on Windows 7 and higher.
  108. class COMPONENT_EXPORT(PRINT_BACKEND) XPSPrintModule {
  109. public:
  110. // All the other methods can ONLY be called after a successful call to Init.
  111. // Init can be called many times and by multiple threads.
  112. static bool Init();
  113. static HRESULT StartXpsPrintJob(const LPCWSTR printer_name,
  114. const LPCWSTR job_name,
  115. const LPCWSTR output_file_name,
  116. HANDLE progress_event,
  117. HANDLE completion_event,
  118. UINT8* printable_pages_on,
  119. UINT32 printable_pages_on_count,
  120. IXpsPrintJob** xps_print_job,
  121. IXpsPrintJobStream** document_stream,
  122. IXpsPrintJobStream** print_ticket_stream);
  123. private:
  124. XPSPrintModule() {}
  125. static bool InitImpl();
  126. };
  127. // Sets the function that gets friendly names for network printers.
  128. COMPONENT_EXPORT(PRINT_BACKEND)
  129. void SetGetDisplayNameFunction(
  130. std::string (*get_display_name_func)(const std::string& printer_name));
  131. COMPONENT_EXPORT(PRINT_BACKEND)
  132. bool InitBasicPrinterInfo(HANDLE printer, PrinterBasicInfo* printer_info);
  133. COMPONENT_EXPORT(PRINT_BACKEND) std::string GetDriverInfo(HANDLE printer);
  134. // Determines if the specified printer driver is known to cause a file save
  135. // UI dialog to be displayed when printing a document.
  136. COMPONENT_EXPORT(PRINT_BACKEND)
  137. bool DoesDriverDisplayFileDialogForPrinting(const std::string& printer_name);
  138. COMPONENT_EXPORT(PRINT_BACKEND)
  139. std::unique_ptr<DEVMODE, base::FreeDeleter> XpsTicketToDevMode(
  140. const std::wstring& printer_name,
  141. const std::string& print_ticket);
  142. COMPONENT_EXPORT(PRINT_BACKEND) bool IsDevModeWithColor(const DEVMODE* devmode);
  143. // Creates default DEVMODE and sets color option. Some devices need special
  144. // workaround for color.
  145. COMPONENT_EXPORT(PRINT_BACKEND)
  146. std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevModeWithColor(
  147. HANDLE printer,
  148. const std::wstring& printer_name,
  149. bool color);
  150. // Creates new DEVMODE. If `in` is not NULL copy settings from there.
  151. COMPONENT_EXPORT(PRINT_BACKEND)
  152. std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer,
  153. DEVMODE* in);
  154. // Prompts for new DEVMODE. If `in` is not NULL copy settings from there.
  155. COMPONENT_EXPORT(PRINT_BACKEND)
  156. std::unique_ptr<DEVMODE, base::FreeDeleter> PromptDevMode(
  157. HANDLE printer,
  158. const std::wstring& printer_name,
  159. DEVMODE* in,
  160. HWND window,
  161. bool* canceled);
  162. } // namespace printing
  163. #endif // PRINTING_BACKEND_WIN_HELPER_H_