• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CloseHandle(HANDLE handle)28   static bool CloseHandle(HANDLE handle) {
29     return ::ClosePrinter(handle) != FALSE;
30   }
31 
IsHandleValid(HANDLE handle)32   static bool IsHandleValid(HANDLE handle) {
33     return handle != NULL;
34   }
35 
NullHandle()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:
OpenPrinter(const wchar_t * printer)48   bool OpenPrinter(const wchar_t* printer) {
49     HANDLE temp_handle;
50     // ::OpenPrinter may return error but assign some value into handle.
51     if (::OpenPrinter(const_cast<LPTSTR>(printer), &temp_handle, NULL)) {
52       Set(temp_handle);
53     }
54     return IsValid();
55   }
56 
57  private:
58   typedef base::win::GenericScopedHandle<PrinterHandleTraits,
59                                          base::win::VerifierTraits> Base;
60 };
61 
62 class PrinterChangeHandleTraits {
63  public:
64   typedef HANDLE Handle;
65 
CloseHandle(HANDLE handle)66   static bool CloseHandle(HANDLE handle) {
67     ::FindClosePrinterChangeNotification(handle);
68     return true;
69   }
70 
IsHandleValid(HANDLE handle)71   static bool IsHandleValid(HANDLE handle) {
72     return handle != NULL;
73   }
74 
NullHandle()75   static HANDLE NullHandle() {
76     return NULL;
77   }
78 
79  private:
80   DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterChangeHandleTraits);
81 };
82 
83 typedef base::win::GenericScopedHandle<PrinterChangeHandleTraits,
84                                        base::win::DummyVerifierTraits>
85     ScopedPrinterChangeHandle;
86 
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
91 // route instead).
92 class PRINTING_EXPORT XPSModule {
93  public:
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.
96   static bool Init();
97   static HRESULT OpenProvider(const base::string16& printer_name,
98                               DWORD version,
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,
106                                              PDEVMODE devmode,
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,
115       PDEVMODE* devmode,
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);
125 
126  private:
XPSModule()127   XPSModule() { }
128   static bool InitImpl();
129 };
130 
131 // See comments in cc file explaining why we need this.
132 class PRINTING_EXPORT ScopedXPSInitializer {
133  public:
134   ScopedXPSInitializer();
135   ~ScopedXPSInitializer();
136 
initialized()137   bool initialized() const { return initialized_; }
138 
139  private:
140   bool initialized_;
141 };
142 
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 {
147  public:
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.
150   static bool Init();
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);
162  private:
XPSPrintModule()163   XPSPrintModule() { }
164   static bool InitImpl();
165 };
166 
167 PRINTING_EXPORT bool InitBasicPrinterInfo(HANDLE printer,
168                                           PrinterBasicInfo* printer_info);
169 
170 PRINTING_EXPORT std::string GetDriverInfo(HANDLE printer);
171 
172 }  // namespace printing
173 
174 #endif  // PRINTING_BACKEND_WIN_HELPER_H_
175