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
parent ea0b72bc
...@@ -33,6 +33,16 @@ ...@@ -33,6 +33,16 @@
namespace { namespace {
class PrinterChangeHandleTraits {
public:
static bool CloseHandle(HANDLE handle) {
return ::FindClosePrinterChangeNotification(handle) != FALSE;
}
};
typedef base::win::GenericScopedHandle<PrinterChangeHandleTraits>
ScopedPrinterChangeHandle;
class DevMode { class DevMode {
public: public:
DevMode() : dm_(NULL) {} DevMode() : dm_(NULL) {}
...@@ -122,9 +132,7 @@ namespace cloud_print { ...@@ -122,9 +132,7 @@ namespace cloud_print {
class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate { class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate {
public: public:
PrintSystemWatcherWin() PrintSystemWatcherWin()
: printer_(NULL), : delegate_(NULL),
printer_change_(NULL),
delegate_(NULL),
did_signal_(false) { did_signal_(false) {
} }
~PrintSystemWatcherWin() { ~PrintSystemWatcherWin() {
...@@ -151,11 +159,11 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate { ...@@ -151,11 +159,11 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate {
printer_name_to_use = const_cast<LPTSTR>(printer_name_wide.c_str()); printer_name_to_use = const_cast<LPTSTR>(printer_name_wide.c_str());
} }
bool ret = false; bool ret = false;
OpenPrinter(printer_name_to_use, &printer_, NULL); OpenPrinter(printer_name_to_use, printer_.Receive(), NULL);
if (printer_) { if (printer_.IsValid()) {
printer_change_ = FindFirstPrinterChangeNotification( printer_change_.Set(FindFirstPrinterChangeNotification(
printer_, PRINTER_CHANGE_PRINTER|PRINTER_CHANGE_JOB, 0, NULL); printer_, PRINTER_CHANGE_PRINTER|PRINTER_CHANGE_JOB, 0, NULL));
if (printer_change_) { if (printer_change_.IsValid()) {
ret = watcher_.StartWatching(printer_change_, this); ret = watcher_.StartWatching(printer_change_, this);
} }
} }
...@@ -166,14 +174,8 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate { ...@@ -166,14 +174,8 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate {
} }
bool Stop() { bool Stop() {
watcher_.StopWatching(); watcher_.StopWatching();
if (printer_) { printer_.Close();
ClosePrinter(printer_); printer_change_.Close();
printer_ = NULL;
}
if (printer_change_) {
FindClosePrinterChangeNotification(printer_change_);
printer_change_ = NULL;
}
return true; return true;
} }
...@@ -203,7 +205,7 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate { ...@@ -203,7 +205,7 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate {
bool GetCurrentPrinterInfo(printing::PrinterBasicInfo* printer_info) { bool GetCurrentPrinterInfo(printing::PrinterBasicInfo* printer_info) {
DCHECK(printer_info); DCHECK(printer_info);
if (!printer_) if (!printer_.IsValid())
return false; return false;
DWORD bytes_needed = 0; DWORD bytes_needed = 0;
...@@ -234,10 +236,11 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate { ...@@ -234,10 +236,11 @@ class PrintSystemWatcherWin : public base::win::ObjectWatcher::Delegate {
private: private:
base::win::ObjectWatcher watcher_; base::win::ObjectWatcher watcher_;
HANDLE printer_; // The printer being watched printing::ScopedPrinterHandle printer_; // The printer being watched
HANDLE printer_change_; // Returned by FindFirstPrinterChangeNotifier // Returned by FindFirstPrinterChangeNotifier.
Delegate* delegate_; // Delegate to notify ScopedPrinterChangeHandle printer_change_;
bool did_signal_; // DoneWaiting was called Delegate* delegate_; // Delegate to notify
bool did_signal_; // DoneWaiting was called
}; };
// This typedef is to workaround the issue with certain versions of // This typedef is to workaround the issue with certain versions of
...@@ -804,13 +807,13 @@ bool PrintSystemWin::GetJobDetails(const std::string& printer_name, ...@@ -804,13 +807,13 @@ bool PrintSystemWin::GetJobDetails(const std::string& printer_name,
PlatformJobId job_id, PlatformJobId job_id,
PrintJobDetails *job_details) { PrintJobDetails *job_details) {
DCHECK(job_details); DCHECK(job_details);
HANDLE printer_handle = NULL; printing::ScopedPrinterHandle printer_handle;
std::wstring printer_name_wide = UTF8ToWide(printer_name); std::wstring printer_name_wide = UTF8ToWide(printer_name);
OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()),
NULL); printer_handle.Receive(), NULL);
DCHECK(printer_handle); DCHECK(printer_handle.IsValid());
bool ret = false; bool ret = false;
if (printer_handle) { if (printer_handle.IsValid()) {
DWORD bytes_needed = 0; DWORD bytes_needed = 0;
GetJob(printer_handle, job_id, 1, NULL, 0, &bytes_needed); GetJob(printer_handle, job_id, 1, NULL, 0, &bytes_needed);
DWORD last_error = GetLastError(); DWORD last_error = GetLastError();
...@@ -840,7 +843,6 @@ bool PrintSystemWin::GetJobDetails(const std::string& printer_name, ...@@ -840,7 +843,6 @@ bool PrintSystemWin::GetJobDetails(const std::string& printer_name,
ret = true; ret = true;
} }
} }
ClosePrinter(printer_handle);
} }
return ret; return ret;
} }
......
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -131,12 +131,11 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults( ...@@ -131,12 +131,11 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults(
DCHECK(SUCCEEDED(hr)); DCHECK(SUCCEEDED(hr));
printer_info->caps_mime_type = "text/xml"; printer_info->caps_mime_type = "text/xml";
} }
// TODO(sanjeevr): Add ScopedPrinterHandle ScopedPrinterHandle printer_handle;
HANDLE printer_handle = NULL; OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()),
OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, printer_handle.Receive(), NULL);
NULL);
DCHECK(printer_handle); DCHECK(printer_handle);
if (printer_handle) { if (printer_handle.IsValid()) {
LONG devmode_size = DocumentProperties( LONG devmode_size = DocumentProperties(
NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()), NULL, printer_handle, const_cast<LPTSTR>(printer_name_wide.c_str()),
NULL, NULL, 0); NULL, NULL, 0);
...@@ -166,7 +165,6 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults( ...@@ -166,7 +165,6 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults(
printer_info->defaults_mime_type = "text/xml"; printer_info->defaults_mime_type = "text/xml";
} }
} }
ClosePrinter(printer_handle);
} }
XPSModule::CloseProvider(provider); XPSModule::CloseProvider(provider);
} }
...@@ -175,15 +173,10 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults( ...@@ -175,15 +173,10 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults(
bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) { bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) {
std::wstring printer_name_wide = UTF8ToWide(printer_name); std::wstring printer_name_wide = UTF8ToWide(printer_name);
HANDLE printer_handle = NULL; ScopedPrinterHandle printer_handle;
OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()), &printer_handle, OpenPrinter(const_cast<LPTSTR>(printer_name_wide.c_str()),
NULL); printer_handle.Receive(), NULL);
bool ret = false; return printer_handle.IsValid();
if (printer_handle) {
ret = true;
ClosePrinter(printer_handle);
}
return ret;
} }
scoped_refptr<PrintBackend> PrintBackend::CreateInstance( scoped_refptr<PrintBackend> PrintBackend::CreateInstance(
......
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/time.h" #include "base/time.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "base/values.h" #include "base/values.h"
#include "printing/backend/win_helper.h"
#include "printing/print_job_constants.h" #include "printing/print_job_constants.h"
#include "printing/print_settings_initializer_win.h" #include "printing/print_settings_initializer_win.h"
#include "printing/printed_document.h" #include "printing/printed_document.h"
...@@ -381,9 +382,9 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( ...@@ -381,9 +382,9 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
return OK; return OK;
} }
HANDLE printer; ScopedPrinterHandle printer;
LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); 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(); return OnError();
// Make printer changes local to Chrome. // Make printer changes local to Chrome.
...@@ -403,7 +404,6 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( ...@@ -403,7 +404,6 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
} }
if (dev_mode == NULL) { if (dev_mode == NULL) {
buffer.reset(); buffer.reset();
ClosePrinter(printer);
return OnError(); return OnError();
} }
...@@ -433,19 +433,16 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( ...@@ -433,19 +433,16 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
// Update data using DocumentProperties. // Update data using DocumentProperties.
if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode, if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode,
DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) { DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) {
ClosePrinter(printer);
return OnError(); return OnError();
} }
// Set printer then refresh printer settings. // Set printer then refresh printer settings.
if (!AllocateContext(device_name, dev_mode, &context_)) { if (!AllocateContext(device_name, dev_mode, &context_)) {
ClosePrinter(printer);
return OnError(); return OnError();
} }
PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode, PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode,
ranges, device_name, ranges, device_name,
false, &settings_); false, &settings_);
ClosePrinter(printer);
return OK; return OK;
} }
...@@ -456,10 +453,9 @@ PrintingContext::Result PrintingContextWin::InitWithSettings( ...@@ -456,10 +453,9 @@ PrintingContext::Result PrintingContextWin::InitWithSettings(
settings_ = settings; settings_ = settings;
// TODO(maruel): settings_.ToDEVMODE() // TODO(maruel): settings_.ToDEVMODE()
HANDLE printer; ScopedPrinterHandle printer;
if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()), if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()),
&printer, printer.Receive(), NULL))
NULL))
return FAILED; return FAILED;
Result status = OK; Result status = OK;
...@@ -467,9 +463,6 @@ PrintingContext::Result PrintingContextWin::InitWithSettings( ...@@ -467,9 +463,6 @@ PrintingContext::Result PrintingContextWin::InitWithSettings(
if (!GetPrinterSettings(printer, settings_.device_name())) if (!GetPrinterSettings(printer, settings_.device_name()))
status = FAILED; status = FAILED;
// Close the printer after retrieving the context.
ClosePrinter(printer);
if (status != OK) if (status != OK)
ResetSettings(); ResetSettings();
return status; return status;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment