| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/print_backend.h" | 5 #include "printing/backend/print_backend.h" |
| 6 | 6 |
| 7 #include <objidl.h> | 7 #include <objidl.h> |
| 8 #include <winspool.h> | 8 #include <winspool.h> |
| 9 | 9 |
| 10 #include "base/file_path.h" |
| 11 #include "base/file_version_info.h" |
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
| 12 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 13 #include "base/win/scoped_bstr.h" | 15 #include "base/win/scoped_bstr.h" |
| 14 #include "base/win/scoped_comptr.h" | 16 #include "base/win/scoped_comptr.h" |
| 15 #include "base/win/scoped_hglobal.h" | 17 #include "base/win/scoped_hglobal.h" |
| 16 #include "printing/backend/print_backend_consts.h" | 18 #include "printing/backend/print_backend_consts.h" |
| 17 #include "printing/backend/win_helper.h" | 19 #include "printing/backend/win_helper.h" |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 40 PrintBackendWin() {} | 42 PrintBackendWin() {} |
| 41 virtual ~PrintBackendWin() {} | 43 virtual ~PrintBackendWin() {} |
| 42 | 44 |
| 43 virtual bool EnumeratePrinters(PrinterList* printer_list); | 45 virtual bool EnumeratePrinters(PrinterList* printer_list); |
| 44 | 46 |
| 45 virtual std::string GetDefaultPrinterName(); | 47 virtual std::string GetDefaultPrinterName(); |
| 46 | 48 |
| 47 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, | 49 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, |
| 48 PrinterCapsAndDefaults* printer_info); | 50 PrinterCapsAndDefaults* printer_info); |
| 49 | 51 |
| 52 virtual bool GetPrinterDriverInfo(const std::string& printer_name, |
| 53 PrinterDriverInfo* driver_info); |
| 54 |
| 50 virtual bool IsValidPrinter(const std::string& printer_name); | 55 virtual bool IsValidPrinter(const std::string& printer_name); |
| 51 }; | 56 }; |
| 52 | 57 |
| 53 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { | 58 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { |
| 54 DCHECK(printer_list); | 59 DCHECK(printer_list); |
| 55 DWORD bytes_needed = 0; | 60 DWORD bytes_needed = 0; |
| 56 DWORD count_returned = 0; | 61 DWORD count_returned = 0; |
| 57 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 62 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, |
| 58 NULL, 0, &bytes_needed, &count_returned); | 63 NULL, 0, &bytes_needed, &count_returned); |
| 59 if (!bytes_needed) | 64 if (!bytes_needed) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 error.Receive()); | 129 error.Receive()); |
| 125 DCHECK(SUCCEEDED(hr)); | 130 DCHECK(SUCCEEDED(hr)); |
| 126 if (FAILED(hr)) { | 131 if (FAILED(hr)) { |
| 127 return false; | 132 return false; |
| 128 } | 133 } |
| 129 hr = StreamOnHGlobalToString(print_capabilities_stream.get(), | 134 hr = StreamOnHGlobalToString(print_capabilities_stream.get(), |
| 130 &printer_info->printer_capabilities); | 135 &printer_info->printer_capabilities); |
| 131 DCHECK(SUCCEEDED(hr)); | 136 DCHECK(SUCCEEDED(hr)); |
| 132 printer_info->caps_mime_type = "text/xml"; | 137 printer_info->caps_mime_type = "text/xml"; |
| 133 } | 138 } |
| 134 // TODO(sanjeevr): Add ScopedPrinterHandle | 139 ScopedPrinterHandle printer_handle; |
| 135 HANDLE printer_handle = NULL; | 140 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), |
| 136 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, | 141 printer_handle.Receive(), NULL); |
| 137 NULL); | |
| 138 DCHECK(printer_handle); | 142 DCHECK(printer_handle); |
| 139 if (printer_handle) { | 143 if (printer_handle.IsValid()) { |
| 140 LONG devmode_size = DocumentProperties( | 144 LONG devmode_size = DocumentProperties( |
| 141 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), | 145 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), |
| 142 NULL, NULL, 0); | 146 NULL, NULL, 0); |
| 143 if (devmode_size <= 0) | 147 if (devmode_size <= 0) |
| 144 return false; | 148 return false; |
| 145 scoped_array<BYTE> devmode_out_buffer(new BYTE[devmode_size]); | 149 scoped_array<BYTE> devmode_out_buffer(new BYTE[devmode_size]); |
| 146 DEVMODE* devmode_out = | 150 DEVMODE* devmode_out = |
| 147 reinterpret_cast<DEVMODE*>(devmode_out_buffer.get()); | 151 reinterpret_cast<DEVMODE*>(devmode_out_buffer.get()); |
| 148 DocumentProperties( | 152 DocumentProperties( |
| 149 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), | 153 NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), |
| 150 devmode_out, NULL, DM_OUT_BUFFER); | 154 devmode_out, NULL, DM_OUT_BUFFER); |
| 151 base::win::ScopedComPtr<IStream> printer_defaults_stream; | 155 base::win::ScopedComPtr<IStream> printer_defaults_stream; |
| 152 hr = CreateStreamOnHGlobal(NULL, TRUE, | 156 hr = CreateStreamOnHGlobal(NULL, TRUE, |
| 153 printer_defaults_stream.Receive()); | 157 printer_defaults_stream.Receive()); |
| 154 DCHECK(SUCCEEDED(hr)); | 158 DCHECK(SUCCEEDED(hr)); |
| 155 if (printer_defaults_stream) { | 159 if (printer_defaults_stream) { |
| 156 hr = XPSModule::ConvertDevModeToPrintTicket(provider, | 160 hr = XPSModule::ConvertDevModeToPrintTicket(provider, |
| 157 devmode_size, | 161 devmode_size, |
| 158 devmode_out, | 162 devmode_out, |
| 159 kPTJobScope, | 163 kPTJobScope, |
| 160 printer_defaults_stream); | 164 printer_defaults_stream); |
| 161 DCHECK(SUCCEEDED(hr)); | 165 DCHECK(SUCCEEDED(hr)); |
| 162 if (SUCCEEDED(hr)) { | 166 if (SUCCEEDED(hr)) { |
| 163 hr = StreamOnHGlobalToString(printer_defaults_stream.get(), | 167 hr = StreamOnHGlobalToString(printer_defaults_stream.get(), |
| 164 &printer_info->printer_defaults); | 168 &printer_info->printer_defaults); |
| 165 DCHECK(SUCCEEDED(hr)); | 169 DCHECK(SUCCEEDED(hr)); |
| 166 printer_info->defaults_mime_type = "text/xml"; | 170 printer_info->defaults_mime_type = "text/xml"; |
| 167 } | 171 } |
| 168 } | 172 } |
| 169 ClosePrinter(printer_handle); | |
| 170 } | 173 } |
| 171 XPSModule::CloseProvider(provider); | 174 XPSModule::CloseProvider(provider); |
| 172 } | 175 } |
| 173 return true; | 176 return true; |
| 174 } | 177 } |
| 175 | 178 |
| 179 // Gets the information about driver for a specific printer. |
| 180 bool PrintBackendWin::GetPrinterDriverInfo(const std::string& printer_name, |
| 181 PrinterDriverInfo* driver_info) { |
| 182 DCHECK(driver_info); |
| 183 ScopedPrinterHandle printer_handle; |
| 184 std::wstring printer_name_wide = UTF8ToWide(printer_name); |
| 185 if (!::OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), |
| 186 printer_handle.Receive(), NULL)) { |
| 187 return false; |
| 188 } |
| 189 DCHECK(printer_handle.IsValid()); |
| 190 DWORD bytes_needed = 0; |
| 191 ::GetPrinterDriver(printer_handle, NULL, 6, NULL, 0, &bytes_needed); |
| 192 scoped_array<BYTE> driver_info_buffer(new BYTE[bytes_needed]); |
| 193 if (!::GetPrinterDriver(printer_handle, NULL, 6, driver_info_buffer.get(), |
| 194 bytes_needed, &bytes_needed)) { |
| 195 return false; |
| 196 } |
| 197 if (!bytes_needed) |
| 198 return false; |
| 199 const DRIVER_INFO_6* driver_info_6 = |
| 200 reinterpret_cast<DRIVER_INFO_6*>(driver_info_buffer.get()); |
| 201 |
| 202 if (driver_info_6->pName) |
| 203 driver_info->driver_name = WideToUTF8(driver_info_6->pName); |
| 204 |
| 205 if (driver_info_6->pDriverPath) { |
| 206 scoped_ptr<FileVersionInfo> version_info( |
| 207 FileVersionInfo::CreateFileVersionInfo( |
| 208 FilePath(driver_info_6->pDriverPath))); |
| 209 driver_info->driver_version = WideToUTF8(version_info->file_version()); |
| 210 driver_info->product_name = WideToUTF8(version_info->product_name()); |
| 211 driver_info->product_version = WideToUTF8(version_info->product_version()); |
| 212 } |
| 213 |
| 214 return true; |
| 215 } |
| 216 |
| 176 bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { | 217 bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { |
| 177 std::wstring printer_name_wide = UTF8ToWide(printer_name); | 218 std::wstring printer_name_wide = UTF8ToWide(printer_name); |
| 178 HANDLE printer_handle = NULL; | 219 ScopedPrinterHandle printer_handle; |
| 179 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, | 220 OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), |
| 180 NULL); | 221 printer_handle.Receive(), NULL); |
| 181 bool ret = false; | 222 return printer_handle.IsValid(); |
| 182 if (printer_handle) { | |
| 183 ret = true; | |
| 184 ClosePrinter(printer_handle); | |
| 185 } | |
| 186 return ret; | |
| 187 } | 223 } |
| 188 | 224 |
| 189 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 225 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
| 190 const base::DictionaryValue* print_backend_settings) { | 226 const base::DictionaryValue* print_backend_settings) { |
| 191 return new PrintBackendWin; | 227 return new PrintBackendWin; |
| 192 } | 228 } |
| 193 | 229 |
| 194 } // namespace printing | 230 } // namespace printing |
| OLD | NEW |