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

Side by Side Diff: printing/printing_context_win.cc

Issue 2374293002: Clean up printing/emf_win.cc. (Closed)
Patch Set: build Created 4 years, 2 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/printing_context_win.h ('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/printing_context_win.h" 5 #include "printing/printing_context_win.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/memory/free_deleter.h" 11 #include "base/memory/free_deleter.h"
11 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
14 #include "printing/backend/print_backend.h" 15 #include "printing/backend/print_backend.h"
15 #include "printing/backend/win_helper.h" 16 #include "printing/backend/win_helper.h"
16 #include "printing/print_settings_initializer_win.h" 17 #include "printing/print_settings_initializer_win.h"
17 #include "printing/printed_document.h" 18 #include "printing/printed_document.h"
(...skipping 17 matching lines...) Expand all
35 // static 36 // static
36 std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { 37 std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) {
37 #if defined(ENABLE_BASIC_PRINTING) 38 #if defined(ENABLE_BASIC_PRINTING)
38 return base::WrapUnique(new PrintingContextSystemDialogWin(delegate)); 39 return base::WrapUnique(new PrintingContextSystemDialogWin(delegate));
39 #else 40 #else
40 return base::WrapUnique(new PrintingContextWin(delegate)); 41 return base::WrapUnique(new PrintingContextWin(delegate));
41 #endif 42 #endif
42 } 43 }
43 44
44 PrintingContextWin::PrintingContextWin(Delegate* delegate) 45 PrintingContextWin::PrintingContextWin(Delegate* delegate)
45 : PrintingContext(delegate), context_(NULL) { 46 : PrintingContext(delegate), context_(nullptr) {}
46 }
47 47
48 PrintingContextWin::~PrintingContextWin() { 48 PrintingContextWin::~PrintingContextWin() {
49 ReleaseContext(); 49 ReleaseContext();
50 } 50 }
51 51
52 void PrintingContextWin::AskUserForSettings( 52 void PrintingContextWin::AskUserForSettings(
53 int max_pages, 53 int max_pages,
54 bool has_selection, 54 bool has_selection,
55 bool is_scripted, 55 bool is_scripted,
56 const PrintSettingsCallback& callback) { 56 const PrintSettingsCallback& callback) {
57 NOTIMPLEMENTED(); 57 NOTIMPLEMENTED();
58 } 58 }
59 59
60 PrintingContext::Result PrintingContextWin::UseDefaultSettings() { 60 PrintingContext::Result PrintingContextWin::UseDefaultSettings() {
61 DCHECK(!in_print_job_); 61 DCHECK(!in_print_job_);
62 62
63 scoped_refptr<PrintBackend> backend = PrintBackend::CreateInstance(NULL); 63 scoped_refptr<PrintBackend> backend = PrintBackend::CreateInstance(nullptr);
64 base::string16 default_printer = 64 base::string16 default_printer =
65 base::UTF8ToWide(backend->GetDefaultPrinterName()); 65 base::UTF8ToWide(backend->GetDefaultPrinterName());
66 if (!default_printer.empty()) { 66 if (!default_printer.empty()) {
67 ScopedPrinterHandle printer; 67 ScopedPrinterHandle printer;
68 if (printer.OpenPrinter(default_printer.c_str())) { 68 if (printer.OpenPrinter(default_printer.c_str())) {
69 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode = 69 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode =
70 CreateDevMode(printer.Get(), NULL); 70 CreateDevMode(printer.Get(), nullptr);
71 if (InitializeSettings(default_printer, dev_mode.get()) == OK) 71 if (InitializeSettings(default_printer, dev_mode.get()) == OK)
72 return OK; 72 return OK;
73 } 73 }
74 } 74 }
75 75
76 ReleaseContext(); 76 ReleaseContext();
77 77
78 // No default printer configured, do we have any printers at all? 78 // No default printer configured, do we have any printers at all?
79 DWORD bytes_needed = 0; 79 DWORD bytes_needed = 0;
80 DWORD count_returned = 0; 80 DWORD count_returned = 0;
81 (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, 81 (void)::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, nullptr,
82 NULL, 2, NULL, 0, &bytes_needed, &count_returned); 82 2, nullptr, 0, &bytes_needed, &count_returned);
83 if (bytes_needed) { 83 if (bytes_needed) {
84 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2)); 84 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2));
85 std::unique_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); 85 std::vector<BYTE> printer_info_buffer(bytes_needed);
86 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, 86 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
87 NULL, 2, printer_info_buffer.get(), 87 nullptr, 2, printer_info_buffer.data(),
88 bytes_needed, &bytes_needed, 88 bytes_needed, &bytes_needed, &count_returned);
89 &count_returned);
90 if (ret && count_returned) { // have printers 89 if (ret && count_returned) { // have printers
91 // Open the first successfully found printer. 90 // Open the first successfully found printer.
92 const PRINTER_INFO_2* info_2 = 91 const PRINTER_INFO_2* info_2 =
93 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); 92 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.data());
94 const PRINTER_INFO_2* info_2_end = info_2 + count_returned; 93 const PRINTER_INFO_2* info_2_end = info_2 + count_returned;
95 for (; info_2 < info_2_end; ++info_2) { 94 for (; info_2 < info_2_end; ++info_2) {
96 ScopedPrinterHandle printer; 95 ScopedPrinterHandle printer;
97 if (!printer.OpenPrinter(info_2->pPrinterName)) 96 if (!printer.OpenPrinter(info_2->pPrinterName))
98 continue; 97 continue;
99 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode = 98 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode =
100 CreateDevMode(printer.Get(), NULL); 99 CreateDevMode(printer.Get(), nullptr);
101 if (InitializeSettings(info_2->pPrinterName, dev_mode.get()) == OK) 100 if (InitializeSettings(info_2->pPrinterName, dev_mode.get()) == OK)
102 return OK; 101 return OK;
103 } 102 }
104 if (context_) 103 if (context_)
105 return OK; 104 return OK;
106 } 105 }
107 } 106 }
108 107
109 return OnError(); 108 return OnError();
110 } 109 }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 DCHECK(!in_print_job_); 221 DCHECK(!in_print_job_);
223 222
224 settings_ = settings; 223 settings_ = settings;
225 224
226 // TODO(maruel): settings_.ToDEVMODE() 225 // TODO(maruel): settings_.ToDEVMODE()
227 ScopedPrinterHandle printer; 226 ScopedPrinterHandle printer;
228 if (!printer.OpenPrinter(settings_.device_name().c_str())) 227 if (!printer.OpenPrinter(settings_.device_name().c_str()))
229 return FAILED; 228 return FAILED;
230 229
231 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode = 230 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode =
232 CreateDevMode(printer.Get(), NULL); 231 CreateDevMode(printer.Get(), nullptr);
233 232
234 return InitializeSettings(settings_.device_name(), dev_mode.get()); 233 return InitializeSettings(settings_.device_name(), dev_mode.get());
235 } 234 }
236 235
237 PrintingContext::Result PrintingContextWin::NewDocument( 236 PrintingContext::Result PrintingContextWin::NewDocument(
238 const base::string16& document_name) { 237 const base::string16& document_name) {
239 DCHECK(!in_print_job_); 238 DCHECK(!in_print_job_);
240 if (!context_) 239 if (!context_)
241 return OnError(); 240 return OnError();
242 241
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 void PrintingContextWin::Cancel() { 312 void PrintingContextWin::Cancel() {
314 abort_printing_ = true; 313 abort_printing_ = true;
315 in_print_job_ = false; 314 in_print_job_ = false;
316 if (context_) 315 if (context_)
317 CancelDC(context_); 316 CancelDC(context_);
318 } 317 }
319 318
320 void PrintingContextWin::ReleaseContext() { 319 void PrintingContextWin::ReleaseContext() {
321 if (context_) { 320 if (context_) {
322 DeleteDC(context_); 321 DeleteDC(context_);
323 context_ = NULL; 322 context_ = nullptr;
324 } 323 }
325 } 324 }
326 325
327 gfx::NativeDrawingContext PrintingContextWin::context() const { 326 gfx::NativeDrawingContext PrintingContextWin::context() const {
328 return context_; 327 return context_;
329 } 328 }
330 329
331 // static 330 // static
332 BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) { 331 BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) {
333 if (nCode) { 332 if (nCode) {
334 // TODO(maruel): Need a way to find the right instance to set. Should 333 // TODO(maruel): Need a way to find the right instance to set. Should
335 // leverage PrintJobManager here? 334 // leverage PrintJobManager here?
336 // abort_printing_ = true; 335 // abort_printing_ = true;
337 } 336 }
338 return true; 337 return true;
339 } 338 }
340 339
341 PrintingContext::Result PrintingContextWin::InitializeSettings( 340 PrintingContext::Result PrintingContextWin::InitializeSettings(
342 const std::wstring& device_name, 341 const std::wstring& device_name,
343 DEVMODE* dev_mode) { 342 DEVMODE* dev_mode) {
344 if (!dev_mode) 343 if (!dev_mode)
345 return OnError(); 344 return OnError();
346 345
347 ReleaseContext(); 346 ReleaseContext();
348 context_ = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); 347 context_ = CreateDC(L"WINSPOOL", device_name.c_str(), nullptr, dev_mode);
349 if (!context_) 348 if (!context_)
350 return OnError(); 349 return OnError();
351 350
352 skia::InitializeDC(context_); 351 skia::InitializeDC(context_);
353 352
354 DCHECK(!in_print_job_); 353 DCHECK(!in_print_job_);
355 settings_.set_device_name(device_name); 354 settings_.set_device_name(device_name);
356 PrintSettingsInitializerWin::InitPrintSettings( 355 PrintSettingsInitializerWin::InitPrintSettings(
357 context_, *dev_mode, &settings_); 356 context_, *dev_mode, &settings_);
358 357
359 return OK; 358 return OK;
360 } 359 }
361 360
362 HWND PrintingContextWin::GetRootWindow(gfx::NativeView view) { 361 HWND PrintingContextWin::GetRootWindow(gfx::NativeView view) {
363 HWND window = NULL; 362 HWND window = nullptr;
364 if (view && view->GetHost()) 363 if (view && view->GetHost())
365 window = view->GetHost()->GetAcceleratedWidget(); 364 window = view->GetHost()->GetAcceleratedWidget();
366 if (!window) { 365 if (!window) {
367 // TODO(maruel): crbug.com/1214347 Get the right browser window instead. 366 // TODO(maruel): b/1214347 Get the right browser window instead.
dcheng 2016/09/30 02:38:40 Hmm...
Lei Zhang 2016/09/30 04:16:40 Mmmhmm.
368 return GetDesktopWindow(); 367 return GetDesktopWindow();
369 } 368 }
370 return window; 369 return window;
371 } 370 }
372 371
373 } // namespace printing 372 } // namespace printing
OLDNEW
« no previous file with comments | « printing/printing_context_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698