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/memory/scoped_ptr.h"
16 #include "base/strings/string16.h"
17 #include "base/win/scoped_handle.h"
18 #include "printing/printing_export.h"
20 // These are helper functions for dealing with Windows Printing.
23 struct PRINTING_EXPORT PrinterBasicInfo
;
25 class PrinterHandleTraits
{
27 typedef HANDLE Handle
;
29 static bool CloseHandle(HANDLE handle
) {
30 return ::ClosePrinter(handle
) != FALSE
;
33 static bool IsHandleValid(HANDLE handle
) {
34 return handle
!= NULL
;
37 static HANDLE
NullHandle() {
42 DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterHandleTraits
);
45 class ScopedPrinterHandle
46 : public base::win::GenericScopedHandle
<PrinterHandleTraits
,
47 base::win::VerifierTraits
> {
49 bool OpenPrinter(const wchar_t* printer
) {
51 // ::OpenPrinter may return error but assign some value into handle.
52 if (::OpenPrinter(const_cast<LPTSTR
>(printer
), &temp_handle
, NULL
)) {
59 typedef base::win::GenericScopedHandle
<PrinterHandleTraits
,
60 base::win::VerifierTraits
> Base
;
63 class PrinterChangeHandleTraits
{
65 typedef HANDLE Handle
;
67 static bool CloseHandle(HANDLE handle
) {
68 ::FindClosePrinterChangeNotification(handle
);
72 static bool IsHandleValid(HANDLE handle
) {
73 return handle
!= NULL
;
76 static HANDLE
NullHandle() {
81 DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterChangeHandleTraits
);
84 typedef base::win::GenericScopedHandle
<PrinterChangeHandleTraits
,
85 base::win::DummyVerifierTraits
>
86 ScopedPrinterChangeHandle
;
88 // Wrapper class to wrap the XPS APIs (PTxxx APIs). This is needed because these
89 // APIs are not available by default on XP. We could delayload prntvpt.dll but
90 // this would mean having to add that to every binary that links with
91 // printing.lib (which is a LOT of binaries). So choosing the GetProcAddress
93 class PRINTING_EXPORT XPSModule
{
95 // All the other methods can ONLY be called after a successful call to Init.
96 // Init can be called many times and by multiple threads.
98 static HRESULT
OpenProvider(const base::string16
& printer_name
,
100 HPTPROVIDER
* provider
);
101 static HRESULT
GetPrintCapabilities(HPTPROVIDER provider
,
102 IStream
* print_ticket
,
103 IStream
* capabilities
,
104 BSTR
* error_message
);
105 static HRESULT
ConvertDevModeToPrintTicket(HPTPROVIDER provider
,
106 ULONG devmode_size_in_bytes
,
108 EPrintTicketScope scope
,
109 IStream
* print_ticket
);
110 static HRESULT
ConvertPrintTicketToDevMode(
111 HPTPROVIDER provider
,
112 IStream
* print_ticket
,
113 EDefaultDevmodeType base_devmode_type
,
114 EPrintTicketScope scope
,
115 ULONG
* devmode_byte_count
,
117 BSTR
* error_message
);
118 static HRESULT
MergeAndValidatePrintTicket(HPTPROVIDER provider
,
119 IStream
* base_ticket
,
120 IStream
* delta_ticket
,
121 EPrintTicketScope scope
,
122 IStream
* result_ticket
,
123 BSTR
* error_message
);
124 static HRESULT
ReleaseMemory(PVOID buffer
);
125 static HRESULT
CloseProvider(HPTPROVIDER provider
);
129 static bool InitImpl();
132 // See comments in cc file explaining why we need this.
133 class PRINTING_EXPORT ScopedXPSInitializer
{
135 ScopedXPSInitializer();
136 ~ScopedXPSInitializer();
138 bool initialized() const { return initialized_
; }
144 // Wrapper class to wrap the XPS Print APIs (these are different from the PTxxx
145 // which deal with the XML Print Schema). This is needed because these
146 // APIs are only available on Windows 7 and higher.
147 class PRINTING_EXPORT XPSPrintModule
{
149 // All the other methods can ONLY be called after a successful call to Init.
150 // Init can be called many times and by multiple threads.
152 static HRESULT
StartXpsPrintJob(
153 const LPCWSTR printer_name
,
154 const LPCWSTR job_name
,
155 const LPCWSTR output_file_name
,
156 HANDLE progress_event
,
157 HANDLE completion_event
,
158 UINT8
* printable_pages_on
,
159 UINT32 printable_pages_on_count
,
160 IXpsPrintJob
**xps_print_job
,
161 IXpsPrintJobStream
**document_stream
,
162 IXpsPrintJobStream
**print_ticket_stream
);
165 static bool InitImpl();
168 PRINTING_EXPORT
bool InitBasicPrinterInfo(HANDLE printer
,
169 PrinterBasicInfo
* printer_info
);
171 PRINTING_EXPORT
std::string
GetDriverInfo(HANDLE printer
);
173 PRINTING_EXPORT scoped_ptr
<DEVMODE
, base::FreeDeleter
> XpsTicketToDevMode(
174 const base::string16
& printer_name
,
175 const std::string
& print_ticket
);
177 // Creates default DEVMODE and sets color option. Some devices need special
178 // workaround for color.
179 PRINTING_EXPORT scoped_ptr
<DEVMODE
, base::FreeDeleter
> CreateDevModeWithColor(
181 const base::string16
& printer_name
,
184 // Creates new DEVMODE. If |in| is not NULL copy settings from there.
185 PRINTING_EXPORT scoped_ptr
<DEVMODE
, base::FreeDeleter
> CreateDevMode(
189 // Prompts for new DEVMODE. If |in| is not NULL copy settings from there.
190 PRINTING_EXPORT scoped_ptr
<DEVMODE
, base::FreeDeleter
> PromptDevMode(
192 const base::string16
& printer_name
,
197 } // namespace printing
199 #endif // PRINTING_BACKEND_WIN_HELPER_H_