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. 4 5 #ifndef PRINTING_BACKEND_WIN_HELPER_H_ 6 #define PRINTING_BACKEND_WIN_HELPER_H_ 7 8 #include <objidl.h> 9 #include <winspool.h> 10 #include <prntvpt.h> 11 #include <xpsprint.h> 12 13 #include <string> 14 15 #include "base/strings/string16.h" 16 #include "base/win/scoped_handle.h" 17 #include "printing/printing_export.h" 18 19 // These are helper functions for dealing with Windows Printing. 20 namespace printing { 21 22 struct PRINTING_EXPORT PrinterBasicInfo; 23 24 class PrinterHandleTraits { 25 public: 26 typedef HANDLE Handle; 27 28 static bool CloseHandle(HANDLE handle) { 29 return ::ClosePrinter(handle) != FALSE; 30 } 31 32 static bool IsHandleValid(HANDLE handle) { 33 return handle != NULL; 34 } 35 36 static HANDLE NullHandle() { 37 return NULL; 38 } 39 40 private: 41 DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterHandleTraits); 42 }; 43 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(); 52 } 53 return IsValid(); 54 } 55 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(); 62 } 63 }; 64 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 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); 103 104 private: 105 XPSModule() { } 106 static bool InitImpl(); 107 }; 108 109 // See comments in cc file explaining why we need this. 110 class PRINTING_EXPORT ScopedXPSInitializer { 111 public: 112 ScopedXPSInitializer(); 113 ~ScopedXPSInitializer(); 114 115 bool initialized() const { return initialized_; } 116 117 private: 118 bool initialized_; 119 }; 120 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(); 143 }; 144 145 PRINTING_EXPORT bool InitBasicPrinterInfo(HANDLE printer, 146 PrinterBasicInfo* printer_info); 147 148 PRINTING_EXPORT std::string GetDriverInfo(HANDLE printer); 149 150 } // namespace printing 151 152 #endif // PRINTING_BACKEND_WIN_HELPER_H_ 153