diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-02 22:50:54 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-02 22:50:54 +0000 |
commit | 55e8e9fcaa7c61d44564a2c59338dc6abb1404a7 (patch) | |
tree | 291de084668ac5eac141b0a76ffc3355208728cc /printing | |
parent | ea0b72bc5bba3ba3829ae849a201f320e99dcd5f (diff) | |
download | chromium_src-55e8e9fcaa7c61d44564a2c59338dc6abb1404a7.zip chromium_src-55e8e9fcaa7c61d44564a2c59338dc6abb1404a7.tar.gz chromium_src-55e8e9fcaa7c61d44564a2c59338dc6abb1404a7.tar.bz2 |
Improved resource management by using ScopedPrinterHandle.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/9569029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124755 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r-- | printing/backend/print_backend_win.cc | 25 | ||||
-rw-r--r-- | printing/printing_context_win.cc | 19 |
2 files changed, 15 insertions, 29 deletions
diff --git a/printing/backend/print_backend_win.cc b/printing/backend/print_backend_win.cc index 01be277..9995005 100644 --- a/printing/backend/print_backend_win.cc +++ b/printing/backend/print_backend_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -131,12 +131,11 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults( DCHECK(SUCCEEDED(hr)); printer_info->caps_mime_type = "text/xml"; } - // TODO(sanjeevr): Add ScopedPrinterHandle - HANDLE printer_handle = NULL; - OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, - NULL); + ScopedPrinterHandle printer_handle; + OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), + printer_handle.Receive(), NULL); DCHECK(printer_handle); - if (printer_handle) { + if (printer_handle.IsValid()) { LONG devmode_size = DocumentProperties( NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), NULL, NULL, 0); @@ -166,7 +165,6 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults( printer_info->defaults_mime_type = "text/xml"; } } - ClosePrinter(printer_handle); } XPSModule::CloseProvider(provider); } @@ -175,15 +173,10 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults( bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { std::wstring printer_name_wide = UTF8ToWide(printer_name); - HANDLE printer_handle = NULL; - OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, - NULL); - bool ret = false; - if (printer_handle) { - ret = true; - ClosePrinter(printer_handle); - } - return ret; + ScopedPrinterHandle printer_handle; + OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), + printer_handle.Receive(), NULL); + return printer_handle.IsValid(); } scoped_refptr<PrintBackend> PrintBackend::CreateInstance( diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc index 46093e7..ebe9d18 100644 --- a/printing/printing_context_win.cc +++ b/printing/printing_context_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -14,6 +14,7 @@ #include "base/time.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "printing/backend/win_helper.h" #include "printing/print_job_constants.h" #include "printing/print_settings_initializer_win.h" #include "printing/printed_document.h" @@ -381,9 +382,9 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( return OK; } - HANDLE printer; + ScopedPrinterHandle printer; LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); - if (!OpenPrinter(device_name_wide, &printer, NULL)) + if (!OpenPrinter(device_name_wide, printer.Receive(), NULL)) return OnError(); // Make printer changes local to Chrome. @@ -403,7 +404,6 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( } if (dev_mode == NULL) { buffer.reset(); - ClosePrinter(printer); return OnError(); } @@ -433,19 +433,16 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( // Update data using DocumentProperties. if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode, DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) { - ClosePrinter(printer); return OnError(); } // Set printer then refresh printer settings. if (!AllocateContext(device_name, dev_mode, &context_)) { - ClosePrinter(printer); return OnError(); } PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode, ranges, device_name, false, &settings_); - ClosePrinter(printer); return OK; } @@ -456,10 +453,9 @@ PrintingContext::Result PrintingContextWin::InitWithSettings( settings_ = settings; // TODO(maruel): settings_.ToDEVMODE() - HANDLE printer; + ScopedPrinterHandle printer; if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()), - &printer, - NULL)) + printer.Receive(), NULL)) return FAILED; Result status = OK; @@ -467,9 +463,6 @@ PrintingContext::Result PrintingContextWin::InitWithSettings( if (!GetPrinterSettings(printer, settings_.device_name())) status = FAILED; - // Close the printer after retrieving the context. - ClosePrinter(printer); - if (status != OK) ResetSettings(); return status; |