| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <algorithm> | |
| 8 #include <objidl.h> | 7 #include <objidl.h> |
| 9 #include <winspool.h> | 8 #include <winspool.h> |
| 10 | 9 |
| 11 #include "base/file_path.h" | |
| 12 #include "base/file_version_info.h" | |
| 13 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
| 15 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 16 #include "base/win/scoped_bstr.h" | 13 #include "base/win/scoped_bstr.h" |
| 17 #include "base/win/scoped_comptr.h" | 14 #include "base/win/scoped_comptr.h" |
| 18 #include "base/win/scoped_hglobal.h" | 15 #include "base/win/scoped_hglobal.h" |
| 19 #include "printing/backend/print_backend_consts.h" | 16 #include "printing/backend/print_backend_consts.h" |
| 20 #include "printing/backend/win_helper.h" | 17 #include "printing/backend/win_helper.h" |
| 21 | 18 |
| 22 namespace { | 19 namespace { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 52 | 49 |
| 53 virtual std::string GetPrinterDriverInfo(const std::string& printer_name); | 50 virtual std::string GetPrinterDriverInfo(const std::string& printer_name); |
| 54 | 51 |
| 55 virtual bool IsValidPrinter(const std::string& printer_name); | 52 virtual bool IsValidPrinter(const std::string& printer_name); |
| 56 }; | 53 }; |
| 57 | 54 |
| 58 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { | 55 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { |
| 59 DCHECK(printer_list); | 56 DCHECK(printer_list); |
| 60 DWORD bytes_needed = 0; | 57 DWORD bytes_needed = 0; |
| 61 DWORD count_returned = 0; | 58 DWORD count_returned = 0; |
| 62 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 59 const DWORD kLevel = 4; |
| 63 NULL, 0, &bytes_needed, &count_returned); | 60 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, |
| 61 kLevel, NULL, 0, &bytes_needed, &count_returned); |
| 64 if (!bytes_needed) | 62 if (!bytes_needed) |
| 65 return false; | 63 return false; |
| 66 scoped_array<BYTE> printer_info_buffer(new BYTE[bytes_needed]); | 64 scoped_array<BYTE> printer_info_buffer(new BYTE[bytes_needed]); |
| 67 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 65 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, kLevel, |
| 68 printer_info_buffer.get(), bytes_needed, &bytes_needed, | 66 printer_info_buffer.get(), bytes_needed, &bytes_needed, |
| 69 &count_returned); | 67 &count_returned); |
| 70 DCHECK(ret); | 68 DCHECK(ret); |
| 71 if (!ret) | 69 if (!ret) |
| 72 return false; | 70 return false; |
| 73 | 71 |
| 74 std::string default_printer = GetDefaultPrinterName(); | 72 std::string default_printer = GetDefaultPrinterName(); |
| 75 PRINTER_INFO_2* printer_info = | 73 PRINTER_INFO_4* printer_info = |
| 76 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); | 74 reinterpret_cast<PRINTER_INFO_4*>(printer_info_buffer.get()); |
| 77 for (DWORD index = 0; index < count_returned; index++) { | 75 for (DWORD index = 0; index < count_returned; index++) { |
| 76 ScopedPrinterHandle printer; |
| 77 OpenPrinter(printer_info[index].pPrinterName, printer.Receive(), NULL); |
| 78 PrinterBasicInfo info; | 78 PrinterBasicInfo info; |
| 79 info.printer_name = WideToUTF8(printer_info[index].pPrinterName); | 79 if (InitBasicPrinterInfo(printer, &info)) { |
| 80 info.is_default = (info.printer_name == default_printer); | 80 info.is_default = (info.printer_name == default_printer); |
| 81 if (printer_info[index].pComment) | 81 printer_list->push_back(info); |
| 82 info.printer_description = WideToUTF8(printer_info[index].pComment); | 82 } |
| 83 info.printer_status = printer_info[index].Status; | |
| 84 if (printer_info[index].pLocation) | |
| 85 info.options[kLocationTagName] = | |
| 86 WideToUTF8(printer_info[index].pLocation); | |
| 87 if (printer_info[index].pDriverName) | |
| 88 info.options[kDriverNameTagName] = | |
| 89 WideToUTF8(printer_info[index].pDriverName); | |
| 90 printer_list->push_back(info); | |
| 91 } | 83 } |
| 92 return true; | 84 return true; |
| 93 } | 85 } |
| 94 | 86 |
| 95 std::string PrintBackendWin::GetDefaultPrinterName() { | 87 std::string PrintBackendWin::GetDefaultPrinterName() { |
| 96 DWORD size = MAX_PATH; | 88 DWORD size = MAX_PATH; |
| 97 TCHAR default_printer_name[MAX_PATH]; | 89 TCHAR default_printer_name[MAX_PATH]; |
| 98 if (!::GetDefaultPrinter(default_printer_name, &size)) | 90 if (!::GetDefaultPrinter(default_printer_name, &size)) |
| 99 return std::string(); | 91 return std::string(); |
| 100 return WideToUTF8(default_printer_name); | 92 return WideToUTF8(default_printer_name); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } | 164 } |
| 173 } | 165 } |
| 174 XPSModule::CloseProvider(provider); | 166 XPSModule::CloseProvider(provider); |
| 175 } | 167 } |
| 176 return true; | 168 return true; |
| 177 } | 169 } |
| 178 | 170 |
| 179 // Gets the information about driver for a specific printer. | 171 // Gets the information about driver for a specific printer. |
| 180 std::string PrintBackendWin::GetPrinterDriverInfo( | 172 std::string PrintBackendWin::GetPrinterDriverInfo( |
| 181 const std::string& printer_name) { | 173 const std::string& printer_name) { |
| 182 std::string driver_info; | 174 ScopedPrinterHandle printer; |
| 183 ScopedPrinterHandle printer_handle; | |
| 184 if (!::OpenPrinter(const_cast<LPTSTR>(UTF8ToWide(printer_name).c_str()), | 175 if (!::OpenPrinter(const_cast<LPTSTR>(UTF8ToWide(printer_name).c_str()), |
| 185 printer_handle.Receive(), NULL)) { | 176 printer.Receive(), NULL)) { |
| 186 return driver_info; | 177 return std::string(); |
| 187 } | 178 } |
| 188 DCHECK(printer_handle.IsValid()); | 179 return GetDriverInfo(printer); |
| 189 DWORD bytes_needed = 0; | |
| 190 ::GetPrinterDriver(printer_handle, NULL, 6, NULL, 0, &bytes_needed); | |
| 191 scoped_array<BYTE> driver_info_buffer(new BYTE[bytes_needed]); | |
| 192 if (!bytes_needed || !driver_info_buffer.get()) | |
| 193 return driver_info; | |
| 194 if (!::GetPrinterDriver(printer_handle, NULL, 6, driver_info_buffer.get(), | |
| 195 bytes_needed, &bytes_needed)) { | |
| 196 return driver_info; | |
| 197 } | |
| 198 if (!bytes_needed) | |
| 199 return driver_info; | |
| 200 const DRIVER_INFO_6* driver_info_6 = | |
| 201 reinterpret_cast<DRIVER_INFO_6*>(driver_info_buffer.get()); | |
| 202 | |
| 203 std::string info[4]; | |
| 204 | |
| 205 if (driver_info_6->pName) | |
| 206 info[0] = WideToUTF8(driver_info_6->pName); | |
| 207 | |
| 208 if (driver_info_6->pDriverPath) { | |
| 209 scoped_ptr<FileVersionInfo> version_info( | |
| 210 FileVersionInfo::CreateFileVersionInfo( | |
| 211 FilePath(driver_info_6->pDriverPath))); | |
| 212 info[1] = WideToUTF8(version_info->file_version()); | |
| 213 info[2] = WideToUTF8(version_info->product_name()); | |
| 214 info[3] = WideToUTF8(version_info->product_version()); | |
| 215 } | |
| 216 | |
| 217 for (size_t i = 0; i < arraysize(info); ++i) { | |
| 218 std::replace(info[i].begin(), info[i].end(), ';', ','); | |
| 219 driver_info.append(info[i]); | |
| 220 driver_info.append(";"); | |
| 221 } | |
| 222 | |
| 223 return driver_info; | |
| 224 } | 180 } |
| 225 | 181 |
| 226 bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { | 182 bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { |
| 227 ScopedPrinterHandle printer_handle; | 183 ScopedPrinterHandle printer_handle; |
| 228 OpenPrinter(const_cast<LPTSTR>(UTF8ToWide(printer_name).c_str()), | 184 OpenPrinter(const_cast<LPTSTR>(UTF8ToWide(printer_name).c_str()), |
| 229 printer_handle.Receive(), NULL); | 185 printer_handle.Receive(), NULL); |
| 230 return printer_handle.IsValid(); | 186 return printer_handle.IsValid(); |
| 231 } | 187 } |
| 232 | 188 |
| 233 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 189 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
| 234 const base::DictionaryValue* print_backend_settings) { | 190 const base::DictionaryValue* print_backend_settings) { |
| 235 return new PrintBackendWin; | 191 return new PrintBackendWin; |
| 236 } | 192 } |
| 237 | 193 |
| 238 } // namespace printing | 194 } // namespace printing |
| OLD | NEW |