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
3 // found in the LICENSE file.
5 #ifndef PRINTING_BACKEND_WIN_HELPER_H_
6 #define PRINTING_BACKEND_WIN_HELPER_H_
15 #include "base/strings/string16.h"
16 #include "base/win/scoped_handle.h"
17 #include "printing/printing_export.h"
19 // These are helper functions for dealing with Windows Printing.
22 struct PRINTING_EXPORT PrinterBasicInfo
;
24 class PrinterHandleTraits
{
26 typedef HANDLE Handle
;
28 static bool CloseHandle(HANDLE handle
) {
29 return ::ClosePrinter(handle
) != FALSE
;
32 static bool IsHandleValid(HANDLE handle
) {
33 return handle
!= NULL
;
36 static HANDLE
NullHandle() {
41 DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterHandleTraits
);
44 class ScopedPrinterHandle
45 : public base::win::GenericScopedHandle
<PrinterHandleTraits
,
46 base::win::VerifierTraits
> {
48 bool OpenPrinter(const wchar_t* printer
) {
50 // ::OpenPrinter may return error but assign some value into handle.
51 if (::OpenPrinter(const_cast<LPTSTR
>(printer
), &temp_handle
, NULL
)) {
58 typedef base::win::GenericScopedHandle
<PrinterHandleTraits
,
59 base::win::VerifierTraits
> Base
;
62 class PrinterChangeHandleTraits
{
64 typedef HANDLE Handle
;
66 static bool CloseHandle(HANDLE handle
) {
67 ::FindClosePrinterChangeNotification(handle
);
71 static bool IsHandleValid(HANDLE handle
) {
72 return handle
!= NULL
;
75 static HANDLE
NullHandle() {
80 DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterChangeHandleTraits
);
83 typedef base::win::GenericScopedHandle
<PrinterChangeHandleTraits
,
84 base::win::DummyVerifierTraits
>
85 ScopedPrinterChangeHandle
;
87 // Wrapper class to wrap the XPS APIs (PTxxx APIs). This is needed because these
88 // APIs are not available by default on XP. We could delayload prntvpt.dll but
89 // this would mean having to add that to every binary that links with
90 // printing.lib (which is a LOT of binaries). So choosing the GetProcAddress
92 class PRINTING_EXPORT XPSModule
{
94 // All the other methods can ONLY be called after a successful call to Init.
95 // Init can be called many times and by multiple threads.
97 static HRESULT
OpenProvider(const base::string16
& printer_name
,
99 HPTPROVIDER
* provider
);
100 static HRESULT
GetPrintCapabilities(HPTPROVIDER provider
,
101 IStream
* print_ticket
,
102 IStream
* capabilities
,
103 BSTR
* error_message
);
104 static HRESULT
ConvertDevModeToPrintTicket(HPTPROVIDER provider
,
105 ULONG devmode_size_in_bytes
,
107 EPrintTicketScope scope
,
108 IStream
* print_ticket
);
109 static HRESULT
ConvertPrintTicketToDevMode(
110 HPTPROVIDER provider
,
111 IStream
* print_ticket
,
112 EDefaultDevmodeType base_devmode_type
,
113 EPrintTicketScope scope
,
114 ULONG
* devmode_byte_count
,
116 BSTR
* error_message
);
117 static HRESULT
MergeAndValidatePrintTicket(HPTPROVIDER provider
,
118 IStream
* base_ticket
,
119 IStream
* delta_ticket
,
120 EPrintTicketScope scope
,
121 IStream
* result_ticket
,
122 BSTR
* error_message
);
123 static HRESULT
ReleaseMemory(PVOID buffer
);
124 static HRESULT
CloseProvider(HPTPROVIDER provider
);
128 static bool InitImpl();
131 // See comments in cc file explaining why we need this.
132 class PRINTING_EXPORT ScopedXPSInitializer
{
134 ScopedXPSInitializer();
135 ~ScopedXPSInitializer();
137 bool initialized() const { return initialized_
; }
143 // Wrapper class to wrap the XPS Print APIs (these are different from the PTxxx
144 // which deal with the XML Print Schema). This is needed because these
145 // APIs are only available on Windows 7 and higher.
146 class PRINTING_EXPORT XPSPrintModule
{
148 // All the other methods can ONLY be called after a successful call to Init.
149 // Init can be called many times and by multiple threads.
151 static HRESULT
StartXpsPrintJob(
152 const LPCWSTR printer_name
,
153 const LPCWSTR job_name
,
154 const LPCWSTR output_file_name
,
155 HANDLE progress_event
,
156 HANDLE completion_event
,
157 UINT8
* printable_pages_on
,
158 UINT32 printable_pages_on_count
,
159 IXpsPrintJob
**xps_print_job
,
160 IXpsPrintJobStream
**document_stream
,
161 IXpsPrintJobStream
**print_ticket_stream
);
164 static bool InitImpl();
167 PRINTING_EXPORT
bool InitBasicPrinterInfo(HANDLE printer
,
168 PrinterBasicInfo
* printer_info
);
170 PRINTING_EXPORT
std::string
GetDriverInfo(HANDLE printer
);
172 } // namespace printing
174 #endif // PRINTING_BACKEND_WIN_HELPER_H_