OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/backend/win_helper.h" | 5 #include "printing/backend/win_helper.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_version_info.h" |
7 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "printing/backend/print_backend.h" |
| 14 #include "printing/backend/print_backend_consts.h" |
8 | 15 |
9 namespace { | 16 namespace { |
10 typedef HRESULT (WINAPI *PTOpenProviderProc)(PCWSTR printer_name, | 17 typedef HRESULT (WINAPI *PTOpenProviderProc)(PCWSTR printer_name, |
11 DWORD version, | 18 DWORD version, |
12 HPTPROVIDER *provider); | 19 HPTPROVIDER *provider); |
13 typedef HRESULT (WINAPI *PTGetPrintCapabilitiesProc)(HPTPROVIDER provider, | 20 typedef HRESULT (WINAPI *PTGetPrintCapabilitiesProc)(HPTPROVIDER provider, |
14 IStream *print_ticket, | 21 IStream *print_ticket, |
15 IStream *capabilities, | 22 IStream *capabilities, |
16 BSTR* error_message); | 23 BSTR* error_message); |
17 typedef HRESULT (WINAPI *PTConvertDevModeToPrintTicketProc)( | 24 typedef HRESULT (WINAPI *PTConvertDevModeToPrintTicketProc)( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 IXpsPrintJobStream **print_ticket_stream); | 57 IXpsPrintJobStream **print_ticket_stream); |
51 | 58 |
52 PTOpenProviderProc g_open_provider_proc = NULL; | 59 PTOpenProviderProc g_open_provider_proc = NULL; |
53 PTGetPrintCapabilitiesProc g_get_print_capabilities_proc = NULL; | 60 PTGetPrintCapabilitiesProc g_get_print_capabilities_proc = NULL; |
54 PTConvertDevModeToPrintTicketProc g_convert_devmode_to_print_ticket_proc = NULL; | 61 PTConvertDevModeToPrintTicketProc g_convert_devmode_to_print_ticket_proc = NULL; |
55 PTConvertPrintTicketToDevModeProc g_convert_print_ticket_to_devmode_proc = NULL; | 62 PTConvertPrintTicketToDevModeProc g_convert_print_ticket_to_devmode_proc = NULL; |
56 PTMergeAndValidatePrintTicketProc g_merge_and_validate_print_ticket_proc = NULL; | 63 PTMergeAndValidatePrintTicketProc g_merge_and_validate_print_ticket_proc = NULL; |
57 PTReleaseMemoryProc g_release_memory_proc = NULL; | 64 PTReleaseMemoryProc g_release_memory_proc = NULL; |
58 PTCloseProviderProc g_close_provider_proc = NULL; | 65 PTCloseProviderProc g_close_provider_proc = NULL; |
59 StartXpsPrintJobProc g_start_xps_print_job_proc = NULL; | 66 StartXpsPrintJobProc g_start_xps_print_job_proc = NULL; |
| 67 |
| 68 // Returns pointer to structure allocated in |buffer|. So pointer is only valide |
| 69 // until |buffer| is destroyed. |
| 70 const PRINTER_INFO_2* GetPrinterInfo2(HANDLE printer, |
| 71 scoped_array<BYTE>* buffer) { |
| 72 DCHECK(printer); |
| 73 DCHECK(buffer); |
| 74 DWORD bytes_needed = 0; |
| 75 const DWORD kLevel = 2; |
| 76 ::GetPrinter(printer, kLevel, NULL, 0, &bytes_needed); |
| 77 if (!bytes_needed) |
| 78 return NULL; |
| 79 buffer->reset(new BYTE[bytes_needed]); |
| 80 if (!buffer->get()) |
| 81 return NULL; |
| 82 if (!::GetPrinter(printer, kLevel, buffer->get(), bytes_needed, |
| 83 &bytes_needed)) { |
| 84 return NULL; |
| 85 } |
| 86 return reinterpret_cast<const PRINTER_INFO_2*>(buffer->get()); |
| 87 } |
| 88 |
| 89 // Returns pointer to structure allocated in |buffer|. So pointer is only valide |
| 90 // until |buffer| is destroyed. |
| 91 const DRIVER_INFO_6* GetDriverInfo6(HANDLE printer, |
| 92 scoped_array<BYTE>* buffer) { |
| 93 DCHECK(printer); |
| 94 DCHECK(buffer); |
| 95 DWORD bytes_needed = 0; |
| 96 const DWORD kLevel = 6; |
| 97 ::GetPrinterDriver(printer, NULL, kLevel, NULL, 0, &bytes_needed); |
| 98 if (!bytes_needed) |
| 99 return NULL; |
| 100 buffer->reset(new BYTE[bytes_needed]); |
| 101 if (!buffer->get()) |
| 102 return NULL; |
| 103 if (!::GetPrinterDriver(printer, NULL, kLevel, buffer->get(), bytes_needed, |
| 104 &bytes_needed)) { |
| 105 return NULL; |
| 106 } |
| 107 return reinterpret_cast<const DRIVER_INFO_6*>(buffer->get()); |
| 108 } |
| 109 |
60 } | 110 } |
61 | 111 |
62 namespace printing { | 112 namespace printing { |
63 | 113 |
64 bool XPSModule::Init() { | 114 bool XPSModule::Init() { |
65 static bool initialized = InitImpl(); | 115 static bool initialized = InitImpl(); |
66 return initialized; | 116 return initialized; |
67 } | 117 } |
68 | 118 |
69 bool XPSModule::InitImpl() { | 119 bool XPSModule::InitImpl() { |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 output_file_name, | 303 output_file_name, |
254 progress_event, | 304 progress_event, |
255 completion_event, | 305 completion_event, |
256 printable_pages_on, | 306 printable_pages_on, |
257 printable_pages_on_count, | 307 printable_pages_on_count, |
258 xps_print_job, | 308 xps_print_job, |
259 document_stream, | 309 document_stream, |
260 print_ticket_stream); | 310 print_ticket_stream); |
261 } | 311 } |
262 | 312 |
| 313 bool InitBasicPrinterInfo(HANDLE printer, PrinterBasicInfo* printer_info) { |
| 314 DCHECK(printer); |
| 315 DCHECK(printer_info); |
| 316 if (!printer) |
| 317 return false; |
| 318 |
| 319 scoped_array<BYTE> printer_info_buffer; |
| 320 const PRINTER_INFO_2* info2 = GetPrinterInfo2(printer, &printer_info_buffer); |
| 321 |
| 322 if (!info2) |
| 323 return false; |
| 324 printer_info->printer_name = WideToUTF8(info2->pPrinterName); |
| 325 if (info2->pComment) |
| 326 printer_info->printer_description = |
| 327 WideToUTF8(info2->pComment); |
| 328 if (info2->pLocation) |
| 329 printer_info->options[kLocationTagName] = |
| 330 WideToUTF8(info2->pLocation); |
| 331 if (info2->pDriverName) |
| 332 printer_info->options[kDriverNameTagName] = |
| 333 WideToUTF8(info2->pDriverName); |
| 334 printer_info->printer_status = info2->Status; |
| 335 |
| 336 std::string driver_info = GetDriverInfo(printer); |
| 337 if (!driver_info.empty()) |
| 338 printer_info->options[kDriverInfoTagName] = driver_info; |
| 339 return true; |
| 340 } |
| 341 |
| 342 std::string GetDriverInfo(HANDLE printer) { |
| 343 DCHECK(printer); |
| 344 std::string driver_info; |
| 345 |
| 346 if (!printer) |
| 347 return driver_info; |
| 348 |
| 349 scoped_array<BYTE> driver_info_buffer; |
| 350 const DRIVER_INFO_6* driver = GetDriverInfo6(printer, &driver_info_buffer); |
| 351 if (!driver) |
| 352 return driver_info; |
| 353 |
| 354 std::string info[4]; |
| 355 if (driver->pName) |
| 356 info[0] = WideToUTF8(driver->pName); |
| 357 |
| 358 if (driver->pDriverPath) { |
| 359 scoped_ptr<FileVersionInfo> version_info( |
| 360 FileVersionInfo::CreateFileVersionInfo( |
| 361 FilePath(driver->pDriverPath))); |
| 362 info[1] = WideToUTF8(version_info->file_version()); |
| 363 info[2] = WideToUTF8(version_info->product_name()); |
| 364 info[3] = WideToUTF8(version_info->product_version()); |
| 365 } |
| 366 |
| 367 for (size_t i = 0; i < arraysize(info); ++i) { |
| 368 std::replace(info[i].begin(), info[i].end(), ';', ','); |
| 369 driver_info.append(info[i]); |
| 370 driver_info.append(";"); |
| 371 } |
| 372 return driver_info; |
| 373 } |
| 374 |
263 } // namespace printing | 375 } // namespace printing |
OLD | NEW |