Commit 8c2d75c0 authored by rvargas's avatar rvargas Committed by Commit bot

IPC: Use ScopedHandle instead of a raw HANDLE for the private members.

BUG=387876
R=cpu@chromium.org

Review URL: https://codereview.chromium.org/588383003

Cr-Commit-Position: refs/heads/master@{#297011}
parent 539e3e94
...@@ -59,7 +59,6 @@ ChannelWin::ChannelWin(const IPC::ChannelHandle &channel_handle, ...@@ -59,7 +59,6 @@ ChannelWin::ChannelWin(const IPC::ChannelHandle &channel_handle,
: ChannelReader(listener), : ChannelReader(listener),
input_state_(this), input_state_(this),
output_state_(this), output_state_(this),
pipe_(INVALID_HANDLE_VALUE),
peer_pid_(base::kNullProcessId), peer_pid_(base::kNullProcessId),
waiting_connect_(mode & MODE_SERVER_FLAG), waiting_connect_(mode & MODE_SERVER_FLAG),
processing_incoming_(false), processing_incoming_(false),
...@@ -85,14 +84,12 @@ void ChannelWin::Close() { ...@@ -85,14 +84,12 @@ void ChannelWin::Close() {
debug_flags_ |= CLOSED; debug_flags_ |= CLOSED;
if (input_state_.is_pending || output_state_.is_pending) if (input_state_.is_pending || output_state_.is_pending)
CancelIo(pipe_); CancelIo(pipe_.Get());
// Closing the handle at this point prevents us from issuing more requests // Closing the handle at this point prevents us from issuing more requests
// form OnIOCompleted(). // form OnIOCompleted().
if (pipe_ != INVALID_HANDLE_VALUE) { if (pipe_.IsValid())
CloseHandle(pipe_); pipe_.Close();
pipe_ = INVALID_HANDLE_VALUE;
}
if (input_state_.is_pending) if (input_state_.is_pending)
debug_flags_ |= WAIT_FOR_READ; debug_flags_ |= WAIT_FOR_READ;
...@@ -158,12 +155,12 @@ ChannelWin::ReadState ChannelWin::ReadData( ...@@ -158,12 +155,12 @@ ChannelWin::ReadState ChannelWin::ReadData(
char* buffer, char* buffer,
int buffer_len, int buffer_len,
int* /* bytes_read */) { int* /* bytes_read */) {
if (INVALID_HANDLE_VALUE == pipe_) if (!pipe_.IsValid())
return READ_FAILED; return READ_FAILED;
debug_flags_ |= READ_MSG; debug_flags_ |= READ_MSG;
DWORD bytes_read = 0; DWORD bytes_read = 0;
BOOL ok = ReadFile(pipe_, buffer, buffer_len, BOOL ok = ReadFile(pipe_.Get(), buffer, buffer_len,
&bytes_read, &input_state_.context.overlapped); &bytes_read, &input_state_.context.overlapped);
if (!ok) { if (!ok) {
DWORD err = GetLastError(); DWORD err = GetLastError();
...@@ -245,10 +242,14 @@ const base::string16 ChannelWin::PipeName( ...@@ -245,10 +242,14 @@ const base::string16 ChannelWin::PipeName(
bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle, bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle,
Mode mode) { Mode mode) {
DCHECK_EQ(INVALID_HANDLE_VALUE, pipe_); DCHECK(!pipe_.IsValid());
base::string16 pipe_name; base::string16 pipe_name;
// If we already have a valid pipe for channel just copy it. // If we already have a valid pipe for channel just copy it.
if (channel_handle.pipe.handle) { if (channel_handle.pipe.handle) {
// TODO(rvargas) crbug.com/415294: ChannelHandle should either go away in
// favor of two independent entities (name/file), or it should be a move-
// only type with a base::File member. In any case, this code should not
// call DuplicateHandle.
DCHECK(channel_handle.name.empty()); DCHECK(channel_handle.name.empty());
pipe_name = L"Not Available"; // Just used for LOG pipe_name = L"Not Available"; // Just used for LOG
// Check that the given pipe confirms to the specified mode. We can // Check that the given pipe confirms to the specified mode. We can
...@@ -262,46 +263,48 @@ bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle, ...@@ -262,46 +263,48 @@ bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle,
LOG(WARNING) << "Inconsistent open mode. Mode :" << mode; LOG(WARNING) << "Inconsistent open mode. Mode :" << mode;
return false; return false;
} }
HANDLE local_handle;
if (!DuplicateHandle(GetCurrentProcess(), if (!DuplicateHandle(GetCurrentProcess(),
channel_handle.pipe.handle, channel_handle.pipe.handle,
GetCurrentProcess(), GetCurrentProcess(),
&pipe_, &local_handle,
0, 0,
FALSE, FALSE,
DUPLICATE_SAME_ACCESS)) { DUPLICATE_SAME_ACCESS)) {
LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError(); LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError();
return false; return false;
} }
pipe_.Set(local_handle);
} else if (mode & MODE_SERVER_FLAG) { } else if (mode & MODE_SERVER_FLAG) {
DCHECK(!channel_handle.pipe.handle); DCHECK(!channel_handle.pipe.handle);
const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
FILE_FLAG_FIRST_PIPE_INSTANCE; FILE_FLAG_FIRST_PIPE_INSTANCE;
pipe_name = PipeName(channel_handle.name, &client_secret_); pipe_name = PipeName(channel_handle.name, &client_secret_);
validate_client_ = !!client_secret_; validate_client_ = !!client_secret_;
pipe_ = CreateNamedPipeW(pipe_name.c_str(), pipe_.Set(CreateNamedPipeW(pipe_name.c_str(),
open_mode, open_mode,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
1, 1,
Channel::kReadBufferSize, Channel::kReadBufferSize,
Channel::kReadBufferSize, Channel::kReadBufferSize,
5000, 5000,
NULL); NULL));
} else if (mode & MODE_CLIENT_FLAG) { } else if (mode & MODE_CLIENT_FLAG) {
DCHECK(!channel_handle.pipe.handle); DCHECK(!channel_handle.pipe.handle);
pipe_name = PipeName(channel_handle.name, &client_secret_); pipe_name = PipeName(channel_handle.name, &client_secret_);
pipe_ = CreateFileW(pipe_name.c_str(), pipe_.Set(CreateFileW(pipe_name.c_str(),
GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE,
0, 0,
NULL, NULL,
OPEN_EXISTING, OPEN_EXISTING,
SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION | SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION |
FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED,
NULL); NULL));
} else { } else {
NOTREACHED(); NOTREACHED();
} }
if (pipe_ == INVALID_HANDLE_VALUE) { if (!pipe_.IsValid()) {
// If this process is being closed, the pipe may be gone already. // If this process is being closed, the pipe may be gone already.
PLOG(WARNING) << "Unable to create pipe \"" << pipe_name << "\" in " PLOG(WARNING) << "Unable to create pipe \"" << pipe_name << "\" in "
<< (mode & MODE_SERVER_FLAG ? "server" : "client") << " mode"; << (mode & MODE_SERVER_FLAG ? "server" : "client") << " mode";
...@@ -318,8 +321,7 @@ bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle, ...@@ -318,8 +321,7 @@ bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle,
int32 secret = validate_client_ ? 0 : client_secret_; int32 secret = validate_client_ ? 0 : client_secret_;
if (!m->WriteInt(GetCurrentProcessId()) || if (!m->WriteInt(GetCurrentProcessId()) ||
(secret && !m->WriteUInt32(secret))) { (secret && !m->WriteUInt32(secret))) {
CloseHandle(pipe_); pipe_.Close();
pipe_ = INVALID_HANDLE_VALUE;
return false; return false;
} }
...@@ -335,10 +337,10 @@ bool ChannelWin::Connect() { ...@@ -335,10 +337,10 @@ bool ChannelWin::Connect() {
if (!thread_check_.get()) if (!thread_check_.get())
thread_check_.reset(new base::ThreadChecker()); thread_check_.reset(new base::ThreadChecker());
if (pipe_ == INVALID_HANDLE_VALUE) if (!pipe_.IsValid())
return false; return false;
base::MessageLoopForIO::current()->RegisterIOHandler(pipe_, this); base::MessageLoopForIO::current()->RegisterIOHandler(pipe_.Get(), this);
// Check to see if there is a client connected to our pipe... // Check to see if there is a client connected to our pipe...
if (waiting_connect_) if (waiting_connect_)
...@@ -368,10 +370,10 @@ bool ChannelWin::ProcessConnection() { ...@@ -368,10 +370,10 @@ bool ChannelWin::ProcessConnection() {
input_state_.is_pending = false; input_state_.is_pending = false;
// Do we have a client connected to our pipe? // Do we have a client connected to our pipe?
if (INVALID_HANDLE_VALUE == pipe_) if (!pipe_.IsValid())
return false; return false;
BOOL ok = ConnectNamedPipe(pipe_, &input_state_.context.overlapped); BOOL ok = ConnectNamedPipe(pipe_.Get(), &input_state_.context.overlapped);
debug_flags_ |= CALLED_CONNECT; debug_flags_ |= CALLED_CONNECT;
DWORD err = GetLastError(); DWORD err = GetLastError();
...@@ -427,7 +429,7 @@ bool ChannelWin::ProcessOutgoingMessages( ...@@ -427,7 +429,7 @@ bool ChannelWin::ProcessOutgoingMessages(
if (output_queue_.empty()) if (output_queue_.empty())
return true; return true;
if (INVALID_HANDLE_VALUE == pipe_) if (!pipe_.IsValid())
return false; return false;
// Write to pipe... // Write to pipe...
...@@ -438,7 +440,7 @@ bool ChannelWin::ProcessOutgoingMessages( ...@@ -438,7 +440,7 @@ bool ChannelWin::ProcessOutgoingMessages(
writing_ = true; writing_ = true;
write_size_ = static_cast<uint32>(m->size()); write_size_ = static_cast<uint32>(m->size());
write_error_ = 0; write_error_ = 0;
BOOL ok = WriteFile(pipe_, BOOL ok = WriteFile(pipe_.Get(),
m->data(), m->data(),
write_size_, write_size_,
NULL, NULL,
...@@ -501,7 +503,7 @@ void ChannelWin::OnIOCompleted( ...@@ -501,7 +503,7 @@ void ChannelWin::OnIOCompleted(
input_state_.is_pending = false; input_state_.is_pending = false;
if (!bytes_transfered) if (!bytes_transfered)
ok = false; ok = false;
else if (pipe_ != INVALID_HANDLE_VALUE) else if (pipe_.IsValid())
ok = AsyncReadComplete(bytes_transfered); ok = AsyncReadComplete(bytes_transfered);
} else { } else {
DCHECK(!bytes_transfered); DCHECK(!bytes_transfered);
...@@ -522,7 +524,7 @@ void ChannelWin::OnIOCompleted( ...@@ -522,7 +524,7 @@ void ChannelWin::OnIOCompleted(
} }
ok = ProcessOutgoingMessages(context, bytes_transfered); ok = ProcessOutgoingMessages(context, bytes_transfered);
} }
if (!ok && INVALID_HANDLE_VALUE != pipe_) { if (!ok && pipe_.IsValid()) {
// We don't want to re-enter Close(). // We don't want to re-enter Close().
Close(); Close();
listener()->OnChannelError(); listener()->OnChannelError();
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/win/scoped_handle.h"
#include "ipc/ipc_channel_reader.h" #include "ipc/ipc_channel_reader.h"
namespace base { namespace base {
...@@ -73,7 +74,7 @@ class ChannelWin : public Channel, ...@@ -73,7 +74,7 @@ class ChannelWin : public Channel,
State input_state_; State input_state_;
State output_state_; State output_state_;
HANDLE pipe_; base::win::ScopedHandle pipe_;
base::ProcessId peer_pid_; base::ProcessId peer_pid_;
......
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