#ifndef PRINTING_BACKEND_WIN_HELPER_H_
#define PRINTING_BACKEND_WIN_HELPER_H_
#include <objidl.h>
#include <prntvpt.h>
#include <winspool.h>
#include <xpsprint.h>
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/win/scoped_handle.h"
#include "printing/printing_export.h"
namespace printing {
struct PRINTING_EXPORT PrinterBasicInfo;
class PrinterHandleTraits {
public:
typedef HANDLE Handle;
static bool CloseHandle(HANDLE handle) {
return ::ClosePrinter(handle) != FALSE;
}
static bool IsHandleValid(HANDLE handle) {
return handle != NULL;
}
static HANDLE NullHandle() {
return NULL;
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterHandleTraits);
};
class ScopedPrinterHandle
: public base::win::GenericScopedHandle<PrinterHandleTraits,
base::win::VerifierTraits> {
public:
bool OpenPrinter(const wchar_t* printer) {
HANDLE temp_handle;
if (::OpenPrinter(const_cast<LPTSTR>(printer), &temp_handle, NULL)) {
Set(temp_handle);
}
return IsValid();
}
private:
typedef base::win::GenericScopedHandle<PrinterHandleTraits,
base::win::VerifierTraits> Base;
};
class PrinterChangeHandleTraits {
public:
typedef HANDLE Handle;
static bool CloseHandle(HANDLE handle) {
::FindClosePrinterChangeNotification(handle);
return true;
}
static bool IsHandleValid(HANDLE handle) {
return handle != NULL;
}
static HANDLE NullHandle() {
return NULL;
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(PrinterChangeHandleTraits);
};
typedef base::win::GenericScopedHandle<PrinterChangeHandleTraits,
base::win::DummyVerifierTraits>
ScopedPrinterChangeHandle;
class PRINTING_EXPORT XPSModule {
public:
static bool Init();
static HRESULT OpenProvider(const base::string16& printer_name,
DWORD version,
HPTPROVIDER* provider);
static HRESULT GetPrintCapabilities(HPTPROVIDER provider,
IStream* print_ticket,
IStream* capabilities,
BSTR* error_message);
static HRESULT ConvertDevModeToPrintTicket(HPTPROVIDER provider,
ULONG devmode_size_in_bytes,
PDEVMODE devmode,
EPrintTicketScope scope,
IStream* print_ticket);
static HRESULT ConvertPrintTicketToDevMode(
HPTPROVIDER provider,
IStream* print_ticket,
EDefaultDevmodeType base_devmode_type,
EPrintTicketScope scope,
ULONG* devmode_byte_count,
PDEVMODE* devmode,
BSTR* error_message);
static HRESULT MergeAndValidatePrintTicket(HPTPROVIDER provider,
IStream* base_ticket,
IStream* delta_ticket,
EPrintTicketScope scope,
IStream* result_ticket,
BSTR* error_message);
static HRESULT ReleaseMemory(PVOID buffer);
static HRESULT CloseProvider(HPTPROVIDER provider);
private:
XPSModule() { }
static bool InitImpl();
};
class PRINTING_EXPORT ScopedXPSInitializer {
public:
ScopedXPSInitializer();
~ScopedXPSInitializer();
bool initialized() const { return initialized_; }
private:
bool initialized_;
};
class PRINTING_EXPORT XPSPrintModule {
public:
static bool Init();
static HRESULT StartXpsPrintJob(
const LPCWSTR printer_name,
const LPCWSTR job_name,
const LPCWSTR output_file_name,
HANDLE progress_event,
HANDLE completion_event,
UINT8* printable_pages_on,
UINT32 printable_pages_on_count,
IXpsPrintJob **xps_print_job,
IXpsPrintJobStream **document_stream,
IXpsPrintJobStream **print_ticket_stream);
private:
XPSPrintModule() { }
static bool InitImpl();
};
PRINTING_EXPORT bool InitBasicPrinterInfo(HANDLE printer,
PrinterBasicInfo* printer_info);
PRINTING_EXPORT std::string GetDriverInfo(HANDLE printer);
PRINTING_EXPORT scoped_ptr<DEVMODE, base::FreeDeleter> XpsTicketToDevMode(
const base::string16& printer_name,
const std::string& print_ticket);
PRINTING_EXPORT scoped_ptr<DEVMODE, base::FreeDeleter> CreateDevModeWithColor(
HANDLE printer,
const base::string16& printer_name,
bool color);
PRINTING_EXPORT scoped_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(
HANDLE printer,
DEVMODE* in);
}
#endif