| 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/printing_context_win.h" | 5 #include "printing/printing_context_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/free_deleter.h" | 10 #include "base/memory/free_deleter.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "printing/backend/print_backend.h" | 14 #include "printing/backend/print_backend.h" |
| 14 #include "printing/backend/win_helper.h" | 15 #include "printing/backend/win_helper.h" |
| 15 #include "printing/print_settings_initializer_win.h" | 16 #include "printing/print_settings_initializer_win.h" |
| 16 #include "printing/printed_document.h" | 17 #include "printing/printed_document.h" |
| 17 #include "printing/printing_context_system_dialog_win.h" | 18 #include "printing/printing_context_system_dialog_win.h" |
| 18 #include "printing/printing_utils.h" | 19 #include "printing/printing_utils.h" |
| 19 #include "printing/units.h" | 20 #include "printing/units.h" |
| 20 #include "skia/ext/skia_utils_win.h" | 21 #include "skia/ext/skia_utils_win.h" |
| 21 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
| 22 #include "ui/aura/window_tree_host.h" | 23 #include "ui/aura/window_tree_host.h" |
| 23 | 24 |
| 24 namespace printing { | 25 namespace printing { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 void AssingResult(PrintingContext::Result* out, PrintingContext::Result in) { | 29 void AssingResult(PrintingContext::Result* out, PrintingContext::Result in) { |
| 29 *out = in; | 30 *out = in; |
| 30 } | 31 } |
| 31 | 32 |
| 32 } // namespace | 33 } // namespace |
| 33 | 34 |
| 34 // static | 35 // static |
| 35 scoped_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { | 36 std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { |
| 36 #if defined(ENABLE_BASIC_PRINTING) | 37 #if defined(ENABLE_BASIC_PRINTING) |
| 37 return make_scoped_ptr<PrintingContext>( | 38 return base::WrapUnique(new PrintingContextSytemDialogWin(delegate)); |
| 38 new PrintingContextSytemDialogWin(delegate)); | |
| 39 #else // ENABLE_BASIC_PRINTING | 39 #else // ENABLE_BASIC_PRINTING |
| 40 return make_scoped_ptr<PrintingContext>(new PrintingContextWin(delegate)); | 40 return base::WrapUnique(new PrintingContextWin(delegate)); |
| 41 #endif // EENABLE_BASIC_PRINTING | 41 #endif // EENABLE_BASIC_PRINTING |
| 42 } | 42 } |
| 43 | 43 |
| 44 PrintingContextWin::PrintingContextWin(Delegate* delegate) | 44 PrintingContextWin::PrintingContextWin(Delegate* delegate) |
| 45 : PrintingContext(delegate), context_(NULL) { | 45 : PrintingContext(delegate), context_(NULL) { |
| 46 } | 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(NULL); |
| 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 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = | 69 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
| 70 CreateDevMode(printer.Get(), NULL); | 70 CreateDevMode(printer.Get(), NULL); |
| 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, |
| 82 NULL, 2, NULL, 0, &bytes_needed, &count_returned); | 82 NULL, 2, NULL, 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 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); | 85 std::unique_ptr<BYTE[]> printer_info_buffer(new BYTE[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 NULL, 2, printer_info_buffer.get(), |
| 88 bytes_needed, &bytes_needed, | 88 bytes_needed, &bytes_needed, |
| 89 &count_returned); | 89 &count_returned); |
| 90 if (ret && count_returned) { // have printers | 90 if (ret && count_returned) { // have printers |
| 91 // Open the first successfully found printer. | 91 // Open the first successfully found printer. |
| 92 const PRINTER_INFO_2* info_2 = | 92 const PRINTER_INFO_2* info_2 = |
| 93 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); | 93 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
| 94 const PRINTER_INFO_2* info_2_end = info_2 + count_returned; | 94 const PRINTER_INFO_2* info_2_end = info_2 + count_returned; |
| 95 for (; info_2 < info_2_end; ++info_2) { | 95 for (; info_2 < info_2_end; ++info_2) { |
| 96 ScopedPrinterHandle printer; | 96 ScopedPrinterHandle printer; |
| 97 if (!printer.OpenPrinter(info_2->pPrinterName)) | 97 if (!printer.OpenPrinter(info_2->pPrinterName)) |
| 98 continue; | 98 continue; |
| 99 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = | 99 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
| 100 CreateDevMode(printer.Get(), NULL); | 100 CreateDevMode(printer.Get(), NULL); |
| 101 if (InitializeSettings(info_2->pPrinterName, dev_mode.get()) == OK) | 101 if (InitializeSettings(info_2->pPrinterName, dev_mode.get()) == OK) |
| 102 return OK; | 102 return OK; |
| 103 } | 103 } |
| 104 if (context_) | 104 if (context_) |
| 105 return OK; | 105 return OK; |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 return OnError(); | 109 return OnError(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 int page_count) { | 145 int page_count) { |
| 146 DCHECK(!in_print_job_); | 146 DCHECK(!in_print_job_); |
| 147 DCHECK(!external_preview) << "Not implemented"; | 147 DCHECK(!external_preview) << "Not implemented"; |
| 148 | 148 |
| 149 ScopedPrinterHandle printer; | 149 ScopedPrinterHandle printer; |
| 150 if (!printer.OpenPrinter(settings_.device_name().c_str())) | 150 if (!printer.OpenPrinter(settings_.device_name().c_str())) |
| 151 return OnError(); | 151 return OnError(); |
| 152 | 152 |
| 153 // Make printer changes local to Chrome. | 153 // Make printer changes local to Chrome. |
| 154 // See MSDN documentation regarding DocumentProperties. | 154 // See MSDN documentation regarding DocumentProperties. |
| 155 scoped_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode = | 155 std::unique_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode = |
| 156 CreateDevModeWithColor(printer.Get(), settings_.device_name(), | 156 CreateDevModeWithColor(printer.Get(), settings_.device_name(), |
| 157 settings_.color() != GRAY); | 157 settings_.color() != GRAY); |
| 158 if (!scoped_dev_mode) | 158 if (!scoped_dev_mode) |
| 159 return OnError(); | 159 return OnError(); |
| 160 | 160 |
| 161 { | 161 { |
| 162 DEVMODE* dev_mode = scoped_dev_mode.get(); | 162 DEVMODE* dev_mode = scoped_dev_mode.get(); |
| 163 dev_mode->dmCopies = std::max(settings_.copies(), 1); | 163 dev_mode->dmCopies = std::max(settings_.copies(), 1); |
| 164 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies | 164 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies |
| 165 dev_mode->dmFields |= DM_COPIES; | 165 dev_mode->dmFields |= DM_COPIES; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 const PrintSettings& settings) { | 222 const PrintSettings& settings) { |
| 223 DCHECK(!in_print_job_); | 223 DCHECK(!in_print_job_); |
| 224 | 224 |
| 225 settings_ = settings; | 225 settings_ = settings; |
| 226 | 226 |
| 227 // TODO(maruel): settings_.ToDEVMODE() | 227 // TODO(maruel): settings_.ToDEVMODE() |
| 228 ScopedPrinterHandle printer; | 228 ScopedPrinterHandle printer; |
| 229 if (!printer.OpenPrinter(settings_.device_name().c_str())) | 229 if (!printer.OpenPrinter(settings_.device_name().c_str())) |
| 230 return FAILED; | 230 return FAILED; |
| 231 | 231 |
| 232 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = | 232 std::unique_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
| 233 CreateDevMode(printer.Get(), NULL); | 233 CreateDevMode(printer.Get(), NULL); |
| 234 | 234 |
| 235 return InitializeSettings(settings_.device_name(), dev_mode.get()); | 235 return InitializeSettings(settings_.device_name(), dev_mode.get()); |
| 236 } | 236 } |
| 237 | 237 |
| 238 PrintingContext::Result PrintingContextWin::NewDocument( | 238 PrintingContext::Result PrintingContextWin::NewDocument( |
| 239 const base::string16& document_name) { | 239 const base::string16& document_name) { |
| 240 DCHECK(!in_print_job_); | 240 DCHECK(!in_print_job_); |
| 241 if (!context_) | 241 if (!context_) |
| 242 return OnError(); | 242 return OnError(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 if (view && view->GetHost()) | 365 if (view && view->GetHost()) |
| 366 window = view->GetHost()->GetAcceleratedWidget(); | 366 window = view->GetHost()->GetAcceleratedWidget(); |
| 367 if (!window) { | 367 if (!window) { |
| 368 // TODO(maruel): crbug.com/1214347 Get the right browser window instead. | 368 // TODO(maruel): crbug.com/1214347 Get the right browser window instead. |
| 369 return GetDesktopWindow(); | 369 return GetDesktopWindow(); |
| 370 } | 370 } |
| 371 return window; | 371 return window; |
| 372 } | 372 } |
| 373 | 373 |
| 374 } // namespace printing | 374 } // namespace printing |
| OLD | NEW |