Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1219)

Side by Side Diff: printing/backend/print_backend_win.cc

Issue 1979903002: Print Preview: Get printer info with capabilities. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cups_14
Patch Set: nit Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « printing/backend/print_backend_dummy.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 8 #include <stddef.h>
9 #include <winspool.h> 9 #include <winspool.h>
10 10
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 155
156 } // namespace 156 } // namespace
157 157
158 class PrintBackendWin : public PrintBackend { 158 class PrintBackendWin : public PrintBackend {
159 public: 159 public:
160 PrintBackendWin() {} 160 PrintBackendWin() {}
161 161
162 // PrintBackend implementation. 162 // PrintBackend implementation.
163 bool EnumeratePrinters(PrinterList* printer_list) override; 163 bool EnumeratePrinters(PrinterList* printer_list) override;
164 std::string GetDefaultPrinterName() override; 164 std::string GetDefaultPrinterName() override;
165 bool GetPrinterBasicInfo(const std::string& printer_name,
166 PrinterBasicInfo* printer_info) override;
165 bool GetPrinterSemanticCapsAndDefaults( 167 bool GetPrinterSemanticCapsAndDefaults(
166 const std::string& printer_name, 168 const std::string& printer_name,
167 PrinterSemanticCapsAndDefaults* printer_info) override; 169 PrinterSemanticCapsAndDefaults* printer_info) override;
168 bool GetPrinterCapsAndDefaults( 170 bool GetPrinterCapsAndDefaults(
169 const std::string& printer_name, 171 const std::string& printer_name,
170 PrinterCapsAndDefaults* printer_info) override; 172 PrinterCapsAndDefaults* printer_info) override;
171 std::string GetPrinterDriverInfo( 173 std::string GetPrinterDriverInfo(
172 const std::string& printer_name) override; 174 const std::string& printer_name) override;
173 bool IsValidPrinter(const std::string& printer_name) override; 175 bool IsValidPrinter(const std::string& printer_name) override;
174 176
175 protected: 177 protected:
176 ~PrintBackendWin() override {} 178 ~PrintBackendWin() override {}
177 }; 179 };
178 180
179 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) { 181 bool PrintBackendWin::EnumeratePrinters(PrinterList* printer_list) {
180 DCHECK(printer_list); 182 DCHECK(printer_list);
181 DWORD bytes_needed = 0; 183 DWORD bytes_needed = 0;
182 DWORD count_returned = 0; 184 DWORD count_returned = 0;
183 const DWORD kLevel = 4; 185 const DWORD kLevel = 4;
184 BOOL ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 186 EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, nullptr, kLevel,
185 kLevel, NULL, 0, &bytes_needed, &count_returned); 187 nullptr, 0, &bytes_needed, &count_returned);
186 if (!bytes_needed) 188 if (!bytes_needed)
187 return false; 189 return false;
188 std::unique_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); 190 std::unique_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]);
189 ret = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, kLevel, 191 if (!EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, nullptr,
190 printer_info_buffer.get(), bytes_needed, &bytes_needed, 192 kLevel, printer_info_buffer.get(), bytes_needed,
191 &count_returned); 193 &bytes_needed, &count_returned)) {
192 DCHECK(ret); 194 NOTREACHED();
193 if (!ret)
194 return false; 195 return false;
196 }
195 197
196 std::string default_printer = GetDefaultPrinterName(); 198 std::string default_printer = GetDefaultPrinterName();
197 PRINTER_INFO_4* printer_info = 199 PRINTER_INFO_4* printer_info =
198 reinterpret_cast<PRINTER_INFO_4*>(printer_info_buffer.get()); 200 reinterpret_cast<PRINTER_INFO_4*>(printer_info_buffer.get());
199 for (DWORD index = 0; index < count_returned; index++) { 201 for (DWORD index = 0; index < count_returned; index++) {
200 ScopedPrinterHandle printer; 202 ScopedPrinterHandle printer;
201 PrinterBasicInfo info; 203 PrinterBasicInfo info;
202 if (printer.OpenPrinter(printer_info[index].pPrinterName) && 204 if (printer.OpenPrinter(printer_info[index].pPrinterName) &&
203 InitBasicPrinterInfo(printer.Get(), &info)) { 205 InitBasicPrinterInfo(printer.Get(), &info)) {
204 info.is_default = (info.printer_name == default_printer); 206 info.is_default = (info.printer_name == default_printer);
205 printer_list->push_back(info); 207 printer_list->push_back(info);
206 } 208 }
207 } 209 }
208 return true; 210 return true;
209 } 211 }
210 212
211 std::string PrintBackendWin::GetDefaultPrinterName() { 213 std::string PrintBackendWin::GetDefaultPrinterName() {
212 DWORD size = MAX_PATH; 214 DWORD size = MAX_PATH;
213 TCHAR default_printer_name[MAX_PATH]; 215 TCHAR default_printer_name[MAX_PATH];
214 if (!::GetDefaultPrinter(default_printer_name, &size)) 216 std::string ret;
215 return std::string(); 217 if (::GetDefaultPrinter(default_printer_name, &size))
216 return base::WideToUTF8(default_printer_name); 218 ret = base::WideToUTF8(default_printer_name);
219 return ret;
220 }
221
222 bool PrintBackendWin::GetPrinterBasicInfo(const std::string& printer_name,
223 PrinterBasicInfo* printer_info) {
224 ScopedPrinterHandle printer_handle;
225 if (!printer_handle.OpenPrinter(base::UTF8ToWide(printer_name).c_str()))
226 return false;
227
228 if (!InitBasicPrinterInfo(printer_handle.Get(), printer_info))
229 return false;
230
231 std::string default_printer = GetDefaultPrinterName();
232 printer_info->is_default = (printer_info->printer_name == default_printer);
233 return true;
217 } 234 }
218 235
219 bool PrintBackendWin::GetPrinterSemanticCapsAndDefaults( 236 bool PrintBackendWin::GetPrinterSemanticCapsAndDefaults(
220 const std::string& printer_name, 237 const std::string& printer_name,
221 PrinterSemanticCapsAndDefaults* printer_info) { 238 PrinterSemanticCapsAndDefaults* printer_info) {
222 ScopedPrinterHandle printer_handle; 239 ScopedPrinterHandle printer_handle;
223 if (!printer_handle.OpenPrinter(base::UTF8ToWide(printer_name).c_str())) { 240 if (!printer_handle.OpenPrinter(base::UTF8ToWide(printer_name).c_str())) {
224 LOG(WARNING) << "Failed to open printer, error = " << GetLastError(); 241 LOG(WARNING) << "Failed to open printer, error = " << GetLastError();
225 return false; 242 return false;
226 } 243 }
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 ScopedPrinterHandle printer_handle; 380 ScopedPrinterHandle printer_handle;
364 return printer_handle.OpenPrinter(base::UTF8ToWide(printer_name).c_str()); 381 return printer_handle.OpenPrinter(base::UTF8ToWide(printer_name).c_str());
365 } 382 }
366 383
367 scoped_refptr<PrintBackend> PrintBackend::CreateInstance( 384 scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
368 const base::DictionaryValue* print_backend_settings) { 385 const base::DictionaryValue* print_backend_settings) {
369 return new PrintBackendWin; 386 return new PrintBackendWin;
370 } 387 }
371 388
372 } // namespace printing 389 } // namespace printing
OLDNEW
« no previous file with comments | « printing/backend/print_backend_dummy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698