Added ScopedPrinterHandle

TEST=it compiles
BUG=none

Review URL: http://codereview.chromium.org/9480026

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124640 0039d316-1c4b-4281-b951-d872f2087c98
parent 747e4e29
// Copyright (c) 2010 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.
...@@ -30,16 +30,17 @@ namespace win { ...@@ -30,16 +30,17 @@ namespace win {
// //
// To explicitly close the handle: // To explicitly close the handle:
// hfile.Close(); // hfile.Close();
class ScopedHandle { template <class Traits>
class GenericScopedHandle {
public: public:
ScopedHandle() : handle_(NULL) { GenericScopedHandle() : handle_(NULL) {
} }
explicit ScopedHandle(HANDLE h) : handle_(NULL) { explicit GenericScopedHandle(HANDLE h) : handle_(NULL) {
Set(h); Set(h);
} }
~ScopedHandle() { ~GenericScopedHandle() {
Close(); Close();
} }
...@@ -63,6 +64,11 @@ class ScopedHandle { ...@@ -63,6 +64,11 @@ class ScopedHandle {
operator HANDLE() { return handle_; } operator HANDLE() { return handle_; }
HANDLE* Receive() {
DCHECK(!handle_) << "Handle must be NULL";
return &handle_;
}
HANDLE Take() { HANDLE Take() {
// transfers ownership away from this object // transfers ownership away from this object
HANDLE h = handle_; HANDLE h = handle_;
...@@ -72,7 +78,7 @@ class ScopedHandle { ...@@ -72,7 +78,7 @@ class ScopedHandle {
void Close() { void Close() {
if (handle_) { if (handle_) {
if (!::CloseHandle(handle_)) { if (!Traits::CloseHandle(handle_)) {
NOTREACHED(); NOTREACHED();
} }
handle_ = NULL; handle_ = NULL;
...@@ -81,9 +87,18 @@ class ScopedHandle { ...@@ -81,9 +87,18 @@ class ScopedHandle {
private: private:
HANDLE handle_; HANDLE handle_;
DISALLOW_COPY_AND_ASSIGN(ScopedHandle); DISALLOW_COPY_AND_ASSIGN(GenericScopedHandle);
}; };
class HandleTraits {
public:
static bool CloseHandle(HANDLE handle) {
return ::CloseHandle(handle) != FALSE;
}
};
typedef GenericScopedHandle<HandleTraits> ScopedHandle;
} // namespace win } // namespace win
} // namespace base } // namespace base
......
// 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.
...@@ -12,11 +12,22 @@ ...@@ -12,11 +12,22 @@
#include <xpsprint.h> #include <xpsprint.h>
#include "base/string16.h" #include "base/string16.h"
#include "base/win/scoped_handle.h"
#include "printing/printing_export.h" #include "printing/printing_export.h"
// These are helper functions for dealing with Windows Printing. // These are helper functions for dealing with Windows Printing.
namespace printing { namespace printing {
class PrinterHandleTraits {
public:
static bool CloseHandle(HANDLE handle) {
return ::ClosePrinter(handle) != FALSE;
}
};
typedef base::win::GenericScopedHandle<PrinterHandleTraits> ScopedPrinterHandle;
// Wrapper class to wrap the XPS APIs (PTxxx APIs). This is needed because these // Wrapper class to wrap the XPS APIs (PTxxx APIs). This is needed because these
// APIs are not available by default on XP. We could delayload prntvpt.dll but // APIs are not available by default on XP. We could delayload prntvpt.dll but
// this would mean having to add that to every binary that links with // this would mean having to add that to every binary that links with
......
...@@ -13,18 +13,11 @@ ...@@ -13,18 +13,11 @@
#include "base/process.h" #include "base/process.h"
#include "base/time.h" #include "base/time.h"
#include "base/timer.h" #include "base/timer.h"
#include "base/win/scoped_handle.h"
#include "base/win/object_watcher.h" #include "base/win/object_watcher.h"
#include "remoting/host/wts_console_observer_win.h" #include "remoting/host/wts_console_observer_win.h"
namespace base {
namespace win {
class ScopedHandle;
} // namespace win
} // namespace base
namespace remoting { namespace remoting {
class WtsConsoleMonitor; class WtsConsoleMonitor;
......
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