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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 namespace printing { | 36 namespace printing { |
37 | 37 |
38 class PrintBackendWin : public PrintBackend { | 38 class PrintBackendWin : public PrintBackend { |
39 public: | 39 public: |
40 PrintBackendWin() {} | 40 PrintBackendWin() {} |
41 virtual ~PrintBackendWin() {} | 41 virtual ~PrintBackendWin() {} |
42 | 42 |
43 virtual bool EnumeratePrinters(PrinterList* printer_list); | 43 virtual bool EnumeratePrinters(PrinterList* printer_list); |
44 | 44 |
| 45 virtual std::string GetDefaultPrinterName(); |
| 46 |
45 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, | 47 virtual bool GetPrinterCapsAndDefaults(const std::string& printer_name, |
46 PrinterCapsAndDefaults* printer_info); | 48 PrinterCapsAndDefaults* printer_info); |
47 | 49 |
48 virtual bool IsValidPrinter(const std::string& printer_name); | 50 virtual bool IsValidPrinter(const std::string& printer_name); |
49 }; | 51 }; |
50 | 52 |
51 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { | 53 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { |
52 DCHECK(printer_list); | 54 DCHECK(printer_list); |
53 DWORD bytes_needed = 0; | 55 DWORD bytes_needed = 0; |
54 DWORD count_returned = 0; | 56 DWORD count_returned = 0; |
55 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 57 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, |
56 NULL, 0, &bytes_needed, &count_returned); | 58 NULL, 0, &bytes_needed, &count_returned); |
57 if (!bytes_needed) | 59 if (!bytes_needed) |
58 return false; | 60 return false; |
59 scoped_array<BYTE> printer_info_buffer(new BYTE[bytes_needed]); | 61 scoped_array<BYTE> printer_info_buffer(new BYTE[bytes_needed]); |
60 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, | 62 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, |
61 printer_info_buffer.get(), bytes_needed, &bytes_needed, | 63 printer_info_buffer.get(), bytes_needed, &bytes_needed, |
62 &count_returned); | 64 &count_returned); |
63 DCHECK(ret); | 65 DCHECK(ret); |
64 if (!ret) | 66 if (!ret) |
65 return false; | 67 return false; |
66 | 68 |
67 // Getting the name of the default printer. | 69 std::string default_printer = GetDefaultPrinterName(); |
68 DWORD size = MAX_PATH; | |
69 TCHAR default_printer_name[MAX_PATH]; | |
70 BOOL default_printer_exists = ::GetDefaultPrinter( | |
71 default_printer_name, &size); | |
72 | |
73 PRINTER_INFO_2* printer_info = | 70 PRINTER_INFO_2* printer_info = |
74 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); | 71 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
75 for (DWORD index = 0; index < count_returned; index++) { | 72 for (DWORD index = 0; index < count_returned; index++) { |
76 PrinterBasicInfo info; | 73 PrinterBasicInfo info; |
77 info.printer_name = WideToUTF8(printer_info[index].pPrinterName); | 74 info.printer_name = WideToUTF8(printer_info[index].pPrinterName); |
78 if (default_printer_exists) | 75 info.is_default = (info.printer_name == default_printer); |
79 info.is_default = (info.printer_name == WideToUTF8(default_printer_name)); | |
80 if (printer_info[index].pComment) | 76 if (printer_info[index].pComment) |
81 info.printer_description = WideToUTF8(printer_info[index].pComment); | 77 info.printer_description = WideToUTF8(printer_info[index].pComment); |
82 info.printer_status = printer_info[index].Status; | 78 info.printer_status = printer_info[index].Status; |
83 if (printer_info[index].pLocation) | 79 if (printer_info[index].pLocation) |
84 info.options[kLocationTagName] = | 80 info.options[kLocationTagName] = |
85 WideToUTF8(printer_info[index].pLocation); | 81 WideToUTF8(printer_info[index].pLocation); |
86 if (printer_info[index].pDriverName) | 82 if (printer_info[index].pDriverName) |
87 info.options[kDriverNameTagName] = | 83 info.options[kDriverNameTagName] = |
88 WideToUTF8(printer_info[index].pDriverName); | 84 WideToUTF8(printer_info[index].pDriverName); |
89 printer_list->push_back(info); | 85 printer_list->push_back(info); |
90 } | 86 } |
91 return true; | 87 return true; |
92 } | 88 } |
93 | 89 |
| 90 std::string PrintBackendWin::GetDefaultPrinterName() { |
| 91 DWORD size = MAX_PATH; |
| 92 TCHAR default_printer_name[MAX_PATH]; |
| 93 if (!::GetDefaultPrinter(default_printer_name, &size)) |
| 94 return std::string(); |
| 95 return WideToUTF8(default_printer_name); |
| 96 } |
| 97 |
94 bool PrintBackendWin::GetPrinterCapsAndDefaults( | 98 bool PrintBackendWin::GetPrinterCapsAndDefaults( |
95 const std::string& printer_name, | 99 const std::string& printer_name, |
96 PrinterCapsAndDefaults* printer_info) { | 100 PrinterCapsAndDefaults* printer_info) { |
97 ScopedXPSInitializer xps_initializer; | 101 ScopedXPSInitializer xps_initializer; |
98 if (!xps_initializer.initialized()) { | 102 if (!xps_initializer.initialized()) { |
99 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) | 103 // TODO(sanjeevr): Handle legacy proxy case (with no prntvpt.dll) |
100 return false; | 104 return false; |
101 } | 105 } |
102 if (!IsValidPrinter(printer_name)) { | 106 if (!IsValidPrinter(printer_name)) { |
103 return false; | 107 return false; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 184 } |
181 return ret; | 185 return ret; |
182 } | 186 } |
183 | 187 |
184 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 188 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
185 const DictionaryValue* print_backend_settings) { | 189 const DictionaryValue* print_backend_settings) { |
186 return new PrintBackendWin; | 190 return new PrintBackendWin; |
187 } | 191 } |
188 | 192 |
189 } // namespace printing | 193 } // namespace printing |
OLD | NEW |