| 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/printing_context_win.h" | 5 #include "printing/printing_context_win.h" |
| 6 | 6 |
| 7 #include <winspool.h> | 7 #include <winspool.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
| 12 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "printing/backend/win_helper.h" |
| 17 #include "printing/print_job_constants.h" | 18 #include "printing/print_job_constants.h" |
| 18 #include "printing/print_settings_initializer_win.h" | 19 #include "printing/print_settings_initializer_win.h" |
| 19 #include "printing/printed_document.h" | 20 #include "printing/printed_document.h" |
| 20 #include "printing/units.h" | 21 #include "printing/units.h" |
| 21 #include "skia/ext/platform_device.h" | 22 #include "skia/ext/platform_device.h" |
| 22 | 23 |
| 23 using base::Time; | 24 using base::Time; |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 375 } |
| 375 } | 376 } |
| 376 paper_rect.SetRect(0, 0, paper_size.width(), paper_size.height()); | 377 paper_rect.SetRect(0, 0, paper_size.width(), paper_size.height()); |
| 377 settings_.SetPrinterPrintableArea(paper_size, paper_rect, kPDFDpi); | 378 settings_.SetPrinterPrintableArea(paper_size, paper_rect, kPDFDpi); |
| 378 settings_.set_dpi(kPDFDpi); | 379 settings_.set_dpi(kPDFDpi); |
| 379 settings_.SetOrientation(landscape); | 380 settings_.SetOrientation(landscape); |
| 380 settings_.ranges = ranges; | 381 settings_.ranges = ranges; |
| 381 return OK; | 382 return OK; |
| 382 } | 383 } |
| 383 | 384 |
| 384 HANDLE printer; | 385 ScopedPrinterHandle printer; |
| 385 LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); | 386 LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); |
| 386 if (!OpenPrinter(device_name_wide, &printer, NULL)) | 387 if (!OpenPrinter(device_name_wide, printer.Receive(), NULL)) |
| 387 return OnError(); | 388 return OnError(); |
| 388 | 389 |
| 389 // Make printer changes local to Chrome. | 390 // Make printer changes local to Chrome. |
| 390 // See MSDN documentation regarding DocumentProperties. | 391 // See MSDN documentation regarding DocumentProperties. |
| 391 scoped_array<uint8> buffer; | 392 scoped_array<uint8> buffer; |
| 392 DEVMODE* dev_mode = NULL; | 393 DEVMODE* dev_mode = NULL; |
| 393 LONG buffer_size = DocumentProperties(NULL, printer, device_name_wide, | 394 LONG buffer_size = DocumentProperties(NULL, printer, device_name_wide, |
| 394 NULL, NULL, 0); | 395 NULL, NULL, 0); |
| 395 if (buffer_size > 0) { | 396 if (buffer_size > 0) { |
| 396 buffer.reset(new uint8[buffer_size]); | 397 buffer.reset(new uint8[buffer_size]); |
| 397 memset(buffer.get(), 0, buffer_size); | 398 memset(buffer.get(), 0, buffer_size); |
| 398 if (DocumentProperties(NULL, printer, device_name_wide, | 399 if (DocumentProperties(NULL, printer, device_name_wide, |
| 399 reinterpret_cast<PDEVMODE>(buffer.get()), NULL, | 400 reinterpret_cast<PDEVMODE>(buffer.get()), NULL, |
| 400 DM_OUT_BUFFER) == IDOK) { | 401 DM_OUT_BUFFER) == IDOK) { |
| 401 dev_mode = reinterpret_cast<PDEVMODE>(buffer.get()); | 402 dev_mode = reinterpret_cast<PDEVMODE>(buffer.get()); |
| 402 } | 403 } |
| 403 } | 404 } |
| 404 if (dev_mode == NULL) { | 405 if (dev_mode == NULL) { |
| 405 buffer.reset(); | 406 buffer.reset(); |
| 406 ClosePrinter(printer); | |
| 407 return OnError(); | 407 return OnError(); |
| 408 } | 408 } |
| 409 | 409 |
| 410 if (color == GRAY) | 410 if (color == GRAY) |
| 411 dev_mode->dmColor = DMCOLOR_MONOCHROME; | 411 dev_mode->dmColor = DMCOLOR_MONOCHROME; |
| 412 else | 412 else |
| 413 dev_mode->dmColor = DMCOLOR_COLOR; | 413 dev_mode->dmColor = DMCOLOR_COLOR; |
| 414 | 414 |
| 415 dev_mode->dmCopies = std::max(copies, 1); | 415 dev_mode->dmCopies = std::max(copies, 1); |
| 416 if (dev_mode->dmCopies > 1) // do not change collate unless multiple copies | 416 if (dev_mode->dmCopies > 1) // do not change collate unless multiple copies |
| 417 dev_mode->dmCollate = collate ? DMCOLLATE_TRUE : DMCOLLATE_FALSE; | 417 dev_mode->dmCollate = collate ? DMCOLLATE_TRUE : DMCOLLATE_FALSE; |
| 418 switch (duplex_mode) { | 418 switch (duplex_mode) { |
| 419 case LONG_EDGE: | 419 case LONG_EDGE: |
| 420 dev_mode->dmDuplex = DMDUP_VERTICAL; | 420 dev_mode->dmDuplex = DMDUP_VERTICAL; |
| 421 break; | 421 break; |
| 422 case SHORT_EDGE: | 422 case SHORT_EDGE: |
| 423 dev_mode->dmDuplex = DMDUP_HORIZONTAL; | 423 dev_mode->dmDuplex = DMDUP_HORIZONTAL; |
| 424 break; | 424 break; |
| 425 case SIMPLEX: | 425 case SIMPLEX: |
| 426 dev_mode->dmDuplex = DMDUP_SIMPLEX; | 426 dev_mode->dmDuplex = DMDUP_SIMPLEX; |
| 427 break; | 427 break; |
| 428 default: // UNKNOWN_DUPLEX_MODE | 428 default: // UNKNOWN_DUPLEX_MODE |
| 429 break; | 429 break; |
| 430 } | 430 } |
| 431 dev_mode->dmOrientation = landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT; | 431 dev_mode->dmOrientation = landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT; |
| 432 | 432 |
| 433 // Update data using DocumentProperties. | 433 // Update data using DocumentProperties. |
| 434 if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode, | 434 if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode, |
| 435 DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) { | 435 DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) { |
| 436 ClosePrinter(printer); | |
| 437 return OnError(); | 436 return OnError(); |
| 438 } | 437 } |
| 439 | 438 |
| 440 // Set printer then refresh printer settings. | 439 // Set printer then refresh printer settings. |
| 441 if (!AllocateContext(device_name, dev_mode, &context_)) { | 440 if (!AllocateContext(device_name, dev_mode, &context_)) { |
| 442 ClosePrinter(printer); | |
| 443 return OnError(); | 441 return OnError(); |
| 444 } | 442 } |
| 445 PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode, | 443 PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode, |
| 446 ranges, device_name, | 444 ranges, device_name, |
| 447 false, &settings_); | 445 false, &settings_); |
| 448 ClosePrinter(printer); | |
| 449 return OK; | 446 return OK; |
| 450 } | 447 } |
| 451 | 448 |
| 452 PrintingContext::Result PrintingContextWin::InitWithSettings( | 449 PrintingContext::Result PrintingContextWin::InitWithSettings( |
| 453 const PrintSettings& settings) { | 450 const PrintSettings& settings) { |
| 454 DCHECK(!in_print_job_); | 451 DCHECK(!in_print_job_); |
| 455 | 452 |
| 456 settings_ = settings; | 453 settings_ = settings; |
| 457 | 454 |
| 458 // TODO(maruel): settings_.ToDEVMODE() | 455 // TODO(maruel): settings_.ToDEVMODE() |
| 459 HANDLE printer; | 456 ScopedPrinterHandle printer; |
| 460 if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()), | 457 if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()), |
| 461 &printer, | 458 printer.Receive(), NULL)) |
| 462 NULL)) | |
| 463 return FAILED; | 459 return FAILED; |
| 464 | 460 |
| 465 Result status = OK; | 461 Result status = OK; |
| 466 | 462 |
| 467 if (!GetPrinterSettings(printer, settings_.device_name())) | 463 if (!GetPrinterSettings(printer, settings_.device_name())) |
| 468 status = FAILED; | 464 status = FAILED; |
| 469 | 465 |
| 470 // Close the printer after retrieving the context. | |
| 471 ClosePrinter(printer); | |
| 472 | |
| 473 if (status != OK) | 466 if (status != OK) |
| 474 ResetSettings(); | 467 ResetSettings(); |
| 475 return status; | 468 return status; |
| 476 } | 469 } |
| 477 | 470 |
| 478 PrintingContext::Result PrintingContextWin::NewDocument( | 471 PrintingContext::Result PrintingContextWin::NewDocument( |
| 479 const string16& document_name) { | 472 const string16& document_name) { |
| 480 DCHECK(!in_print_job_); | 473 DCHECK(!in_print_job_); |
| 481 if (!context_) | 474 if (!context_) |
| 482 return OnError(); | 475 return OnError(); |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 if (buf_size) { | 797 if (buf_size) { |
| 805 buffer->reset(new uint8[buf_size]); | 798 buffer->reset(new uint8[buf_size]); |
| 806 memset(buffer->get(), 0, buf_size); | 799 memset(buffer->get(), 0, buf_size); |
| 807 if (!GetPrinter(printer, level, buffer->get(), buf_size, &buf_size)) { | 800 if (!GetPrinter(printer, level, buffer->get(), buf_size, &buf_size)) { |
| 808 buffer->reset(); | 801 buffer->reset(); |
| 809 } | 802 } |
| 810 } | 803 } |
| 811 } | 804 } |
| 812 | 805 |
| 813 } // namespace printing | 806 } // namespace printing |
| OLD | NEW |