Enable the following tests back under ThreadSanitizer v2:
[chromium-blink-merge.git] / printing / backend / win_helper.h
blob9ac9df3a47f98b1d752ebfd37f0e6149748e5f5e
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_
8 #include <objidl.h>
9 #include <winspool.h>
10 #include <prntvpt.h>
11 #include <xpsprint.h>
13 #include <string>
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.
20 namespace printing {
22 struct PRINTING_EXPORT PrinterBasicInfo;
24 class PrinterHandleTraits {
25 public:
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() {
37 return NULL;
40 private:
41 DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterHandleTraits);
44 class ScopedPrinterHandle
45 : public base::win::GenericScopedHandle<PrinterHandleTraits,
46 base::win::VerifierTraits> {
47 public:
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)) {
51 Take();
53 return IsValid();
56 private:
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
69 // route instead).
70 class PRINTING_EXPORT XPSModule {
71 public:
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.
74 static bool Init();
75 static HRESULT OpenProvider(const base::string16& printer_name,
76 DWORD version,
77 HPTPROVIDER* provider);
78 static HRESULT GetPrintCapabilities(HPTPROVIDER provider,
79 IStream* print_ticket,
80 IStream* capabilities,
81 BSTR* error_message);
82 static HRESULT ConvertDevModeToPrintTicket(HPTPROVIDER provider,
83 ULONG devmode_size_in_bytes,
84 PDEVMODE devmode,
85 EPrintTicketScope scope,
86 IStream* print_ticket);
87 static HRESULT ConvertPrintTicketToDevMode(
88 HPTPROVIDER provider,
89 IStream* print_ticket,
90 EDefaultDevmodeType base_devmode_type,
91 EPrintTicketScope scope,
92 ULONG* devmode_byte_count,
93 PDEVMODE* devmode,
94 BSTR* error_message);
95 static HRESULT MergeAndValidatePrintTicket(HPTPROVIDER provider,
96 IStream* base_ticket,
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);
104 private:
105 XPSModule() { }
106 static bool InitImpl();
109 // See comments in cc file explaining why we need this.
110 class PRINTING_EXPORT ScopedXPSInitializer {
111 public:
112 ScopedXPSInitializer();
113 ~ScopedXPSInitializer();
115 bool initialized() const { return initialized_; }
117 private:
118 bool 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 {
125 public:
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.
128 static bool Init();
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);
140 private:
141 XPSPrintModule() { }
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_