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
) {
49 // ::OpenPrinter may return error but assign some value into handle.
50 if (!::OpenPrinter(const_cast<LPTSTR
>(printer
), Receive(), NULL
)) {
57 typedef base::win::GenericScopedHandle
<PrinterHandleTraits
,
58 base::win::VerifierTraits
> Base
;
59 // Hide Receive to avoid assigning handle when ::OpenPrinter returned error.
60 Base::Receiver
Receive() {
61 return Base::Receive();
65 // Wrapper class to wrap the XPS APIs (PTxxx APIs). This is needed because these
66 // APIs are not available by default on XP. We could delayload prntvpt.dll but
67 // this would mean having to add that to every binary that links with
68 // printing.lib (which is a LOT of binaries). So choosing the GetProcAddress
70 class PRINTING_EXPORT XPSModule
{
72 // All the other methods can ONLY be called after a successful call to Init.
73 // Init can be called many times and by multiple threads.
75 static HRESULT
OpenProvider(const base::string16
& printer_name
,
77 HPTPROVIDER
* provider
);
78 static HRESULT
GetPrintCapabilities(HPTPROVIDER provider
,
79 IStream
* print_ticket
,
80 IStream
* capabilities
,
82 static HRESULT
ConvertDevModeToPrintTicket(HPTPROVIDER provider
,
83 ULONG devmode_size_in_bytes
,
85 EPrintTicketScope scope
,
86 IStream
* print_ticket
);
87 static HRESULT
ConvertPrintTicketToDevMode(
89 IStream
* print_ticket
,
90 EDefaultDevmodeType base_devmode_type
,
91 EPrintTicketScope scope
,
92 ULONG
* devmode_byte_count
,
95 static HRESULT
MergeAndValidatePrintTicket(HPTPROVIDER provider
,
97 IStream
* delta_ticket
,
98 EPrintTicketScope scope
,
99 IStream
* result_ticket
,
100 BSTR
* error_message
);
101 static HRESULT
ReleaseMemory(PVOID buffer
);
102 static HRESULT
CloseProvider(HPTPROVIDER provider
);
106 static bool InitImpl();
109 // See comments in cc file explaining why we need this.
110 class PRINTING_EXPORT ScopedXPSInitializer
{
112 ScopedXPSInitializer();
113 ~ScopedXPSInitializer();
115 bool initialized() const { return initialized_
; }
121 // Wrapper class to wrap the XPS Print APIs (these are different from the PTxxx
122 // which deal with the XML Print Schema). This is needed because these
123 // APIs are only available on Windows 7 and higher.
124 class PRINTING_EXPORT XPSPrintModule
{
126 // All the other methods can ONLY be called after a successful call to Init.
127 // Init can be called many times and by multiple threads.
129 static HRESULT
StartXpsPrintJob(
130 const LPCWSTR printer_name
,
131 const LPCWSTR job_name
,
132 const LPCWSTR output_file_name
,
133 HANDLE progress_event
,
134 HANDLE completion_event
,
135 UINT8
* printable_pages_on
,
136 UINT32 printable_pages_on_count
,
137 IXpsPrintJob
**xps_print_job
,
138 IXpsPrintJobStream
**document_stream
,
139 IXpsPrintJobStream
**print_ticket_stream
);
142 static bool InitImpl();
145 PRINTING_EXPORT
bool InitBasicPrinterInfo(HANDLE printer
,
146 PrinterBasicInfo
* printer_info
);
148 PRINTING_EXPORT
std::string
GetDriverInfo(HANDLE printer
);
150 } // namespace printing
152 #endif // PRINTING_BACKEND_WIN_HELPER_H_