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 <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" |
11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/win/scoped_bstr.h" | 13 #include "base/win/scoped_bstr.h" |
14 #include "base/win/scoped_comptr.h" | 14 #include "base/win/scoped_comptr.h" |
15 #include "base/win/scoped_hglobal.h" | 15 #include "base/win/scoped_hglobal.h" |
16 #include "printing/backend/print_backend_consts.h" | 16 #include "printing/backend/print_backend_consts.h" |
17 #include "printing/backend/win_helper.h" | 17 #include "printing/backend/win_helper.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // This class is designed to work with PRINTER_INFO_X structures | 21 // This class is designed to work with PRINTER_INFO_X structures |
22 // and calls GetPrinter internally with correctly allocated buffer. | 22 // and calls GetPrinter internally with correctly allocated buffer. |
23 template <typename T> | 23 template <typename T> |
24 class PrinterInfo { | 24 class PrinterInfo { |
25 public: | 25 public: |
26 bool GetPrinterInfo(HANDLE printer, int level) { | 26 bool GetPrinterInfo(HANDLE printer, int level) { |
27 DWORD buf_size = 0; | 27 DWORD buf_size = 0; |
28 GetPrinter(printer, level, NULL, 0, &buf_size); | 28 GetPrinter(printer, level, NULL, 0, &buf_size); |
29 if (buf_size == 0) | 29 if (buf_size == 0) { |
| 30 LOG(WARNING) << "Failed to GetPrinter, error = " << GetLastError(); |
30 return false; | 31 return false; |
| 32 } |
31 buffer_.reset(new uint8[buf_size]); | 33 buffer_.reset(new uint8[buf_size]); |
32 memset(buffer_.get(), 0, buf_size); | 34 memset(buffer_.get(), 0, buf_size); |
33 return !!GetPrinter(printer, level, buffer_.get(), buf_size, &buf_size); | 35 return !!GetPrinter(printer, level, buffer_.get(), buf_size, &buf_size); |
34 } | 36 } |
35 | 37 |
36 const T* get() const { | 38 const T* get() const { |
37 return reinterpret_cast<T*>(buffer_.get()); | 39 return reinterpret_cast<T*>(buffer_.get()); |
38 } | 40 } |
39 | 41 |
40 private: | 42 private: |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 return WideToUTF8(default_printer_name); | 121 return WideToUTF8(default_printer_name); |
120 } | 122 } |
121 | 123 |
122 bool PrintBackendWin::GetPrinterSemanticCapsAndDefaults( | 124 bool PrintBackendWin::GetPrinterSemanticCapsAndDefaults( |
123 const std::string& printer_name, | 125 const std::string& printer_name, |
124 PrinterSemanticCapsAndDefaults* printer_info) { | 126 PrinterSemanticCapsAndDefaults* printer_info) { |
125 ScopedPrinterHandle printer_handle; | 127 ScopedPrinterHandle printer_handle; |
126 OpenPrinter(const_cast<LPTSTR>(UTF8ToWide(printer_name).c_str()), | 128 OpenPrinter(const_cast<LPTSTR>(UTF8ToWide(printer_name).c_str()), |
127 printer_handle.Receive(), NULL); | 129 printer_handle.Receive(), NULL); |
128 DCHECK(printer_handle); | 130 DCHECK(printer_handle); |
129 if (!printer_handle.IsValid()) | 131 if (!printer_handle.IsValid()) { |
| 132 LOG(WARNING) << "Failed to open printer, error = " << GetLastError(); |
130 return false; | 133 return false; |
| 134 } |
131 | 135 |
132 PrinterInfo<PRINTER_INFO_5> info_5; | 136 PrinterInfo<PRINTER_INFO_5> info_5; |
133 if (!info_5.GetPrinterInfo(printer_handle, 5)) | 137 if (!info_5.GetPrinterInfo(printer_handle, 5)) { |
| 138 LOG(WARNING) << "Failed to get PRINTER_INFO_5, error = " << GetLastError(); |
134 return false; | 139 return false; |
| 140 } |
135 | 141 |
136 // Get printer capabilities. For more info see here: | 142 // Get printer capabilities. For more info see here: |
137 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd183552(v=vs.85).a
spx | 143 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd183552(v=vs.85).a
spx |
138 bool color_supported = (DeviceCapabilities(info_5.get()->pPrinterName, | 144 bool color_supported = (DeviceCapabilities(info_5.get()->pPrinterName, |
139 info_5.get()->pPortName, | 145 info_5.get()->pPortName, |
140 DC_COLORDEVICE, | 146 DC_COLORDEVICE, |
141 NULL, | 147 NULL, |
142 NULL) == 1); | 148 NULL) == 1); |
143 | 149 |
144 bool duplex_supported = (DeviceCapabilities(info_5.get()->pPrinterName, | 150 bool duplex_supported = (DeviceCapabilities(info_5.get()->pPrinterName, |
145 info_5.get()->pPortName, | 151 info_5.get()->pPortName, |
146 DC_DUPLEX, | 152 DC_DUPLEX, |
147 NULL, | 153 NULL, |
148 NULL) == 1); | 154 NULL) == 1); |
149 | 155 |
| 156 DEVMODE* devmode = NULL; |
150 // PRINTER_INFO_9 retrieves current user settings. | 157 // PRINTER_INFO_9 retrieves current user settings. |
151 PrinterInfo<PRINTER_INFO_9> info_9; | 158 PrinterInfo<PRINTER_INFO_9> info_9; |
152 if (!info_9.GetPrinterInfo(printer_handle, 9)) | 159 if (info_9.GetPrinterInfo(printer_handle, 9)) { |
153 return false; | 160 devmode = info_9.get()->pDevMode; |
154 DEVMODE* devmode = info_9.get()->pDevMode; | 161 } else { |
| 162 LOG(WARNING) << "Failed to get PRINTER_INFO_9, error = " << GetLastError(); |
| 163 } |
155 | 164 |
156 // Sometimes user settings are not available (have not been setted up yet). | 165 // Sometimes user settings are not available (have not been setted up yet). |
157 // Use printer default settings (PRINTER_INFO_8) in this case. | 166 // Use printer default settings (PRINTER_INFO_8) in this case. |
158 PrinterInfo<PRINTER_INFO_8> info_8; | 167 PrinterInfo<PRINTER_INFO_8> info_8; |
159 if (!devmode) { | 168 if (!devmode) { |
160 if (info_8.GetPrinterInfo(printer_handle, 8)) | 169 if (info_8.GetPrinterInfo(printer_handle, 8)) { |
161 devmode = info_8.get()->pDevMode; | 170 devmode = info_8.get()->pDevMode; |
| 171 } else { |
| 172 LOG(WARNING) << "Failed to get PRINTER_INFO_8, error = " << |
| 173 GetLastError(); |
| 174 } |
162 } | 175 } |
163 if (!devmode) | 176 if (!devmode) |
164 return false; | 177 return false; |
165 | 178 |
166 PrinterSemanticCapsAndDefaults caps; | 179 PrinterSemanticCapsAndDefaults caps; |
167 caps.color_capable = color_supported; | 180 caps.color_capable = color_supported; |
168 if ((devmode->dmFields & DM_COLOR) == DM_COLOR) | 181 if ((devmode->dmFields & DM_COLOR) == DM_COLOR) |
169 caps.color_default = (devmode->dmColor == DMCOLOR_COLOR); | 182 caps.color_default = (devmode->dmColor == DMCOLOR_COLOR); |
170 | 183 |
171 caps.duplex_capable = duplex_supported; | 184 caps.duplex_capable = duplex_supported; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 printer_handle.Receive(), NULL); | 294 printer_handle.Receive(), NULL); |
282 return printer_handle.IsValid(); | 295 return printer_handle.IsValid(); |
283 } | 296 } |
284 | 297 |
285 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( | 298 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( |
286 const base::DictionaryValue* print_backend_settings) { | 299 const base::DictionaryValue* print_backend_settings) { |
287 return new PrintBackendWin; | 300 return new PrintBackendWin; |
288 } | 301 } |
289 | 302 |
290 } // namespace printing | 303 } // namespace printing |
OLD | NEW |