Index: mojo/system/raw_channel_posix.cc |
diff --git a/mojo/system/raw_channel_posix.cc b/mojo/system/raw_channel_posix.cc |
index fa9d5f29239dddb1bc99bc9ba4522916fefb35d7..f5234fa8fb69eb963f7f87869be4769ea30f580f 100644 |
--- a/mojo/system/raw_channel_posix.cc |
+++ b/mojo/system/raw_channel_posix.cc |
@@ -23,7 +23,7 @@ |
#include "base/posix/eintr_wrapper.h" |
#include "base/synchronization/lock.h" |
#include "mojo/system/message_in_transit.h" |
-#include "mojo/system/platform_channel_handle.h" |
+#include "mojo/system/platform_handle.h" |
namespace mojo { |
namespace system { |
@@ -35,7 +35,7 @@ const size_t kReadSize = 4096; |
class RawChannelPosix : public RawChannel, |
public base::MessageLoopForIO::Watcher { |
public: |
- RawChannelPosix(const PlatformChannelHandle& handle, |
+ RawChannelPosix(ScopedPlatformHandle handle, |
Delegate* delegate, |
base::MessageLoop* message_loop); |
virtual ~RawChannelPosix(); |
@@ -72,7 +72,7 @@ class RawChannelPosix : public RawChannel, |
return static_cast<base::MessageLoopForIO*>(message_loop()); |
} |
- int fd_; |
+ ScopedPlatformHandle fd_; |
// Only used on the I/O thread: |
scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> read_watcher_; |
@@ -97,22 +97,22 @@ class RawChannelPosix : public RawChannel, |
DISALLOW_COPY_AND_ASSIGN(RawChannelPosix); |
}; |
-RawChannelPosix::RawChannelPosix(const PlatformChannelHandle& handle, |
+RawChannelPosix::RawChannelPosix(ScopedPlatformHandle handle, |
Delegate* delegate, |
base::MessageLoop* message_loop) |
: RawChannel(delegate, message_loop), |
- fd_(handle.fd), |
+ fd_(handle.Pass()), |
read_buffer_num_valid_bytes_(0), |
is_dead_(false), |
write_message_offset_(0), |
weak_ptr_factory_(this) { |
CHECK_EQ(RawChannel::message_loop()->type(), base::MessageLoop::TYPE_IO); |
- DCHECK_NE(fd_, -1); |
+ DCHECK(fd_.is_valid()); |
} |
RawChannelPosix::~RawChannelPosix() { |
DCHECK(is_dead_); |
- DCHECK_EQ(fd_, -1); |
+ DCHECK(!fd_.is_valid()); |
// No need to take the |write_lock_| here -- if there are still weak pointers |
// outstanding, then we're hosed anyway (since we wouldn't be able to |
@@ -135,7 +135,7 @@ bool RawChannelPosix::Init() { |
// No need to take the lock. No one should be using us yet. |
DCHECK(write_message_queue_.empty()); |
- if (!message_loop_for_io()->WatchFileDescriptor(fd_, true, |
+ if (!message_loop_for_io()->WatchFileDescriptor(fd_.get().fd, true, |
base::MessageLoopForIO::WATCH_READ, read_watcher_.get(), this)) { |
// TODO(vtl): I'm not sure |WatchFileDescriptor()| actually fails cleanly |
// (in the sense of returning the message loop's state to what it was before |
@@ -155,10 +155,8 @@ void RawChannelPosix::Shutdown() { |
if (!is_dead_) |
CancelPendingWritesNoLock(); |
- DCHECK_NE(fd_, -1); |
- if (close(fd_) != 0) |
- PLOG(ERROR) << "close"; |
- fd_ = -1; |
+ DCHECK(fd_.is_valid()); |
+ fd_.reset(); |
weak_ptr_factory_.InvalidateWeakPtrs(); |
@@ -208,7 +206,7 @@ bool RawChannelPosix::WriteMessage(MessageInTransit* message) { |
} |
void RawChannelPosix::OnFileCanReadWithoutBlocking(int fd) { |
- DCHECK_EQ(fd, fd_); |
+ DCHECK_EQ(fd, fd_.get().fd); |
DCHECK_EQ(base::MessageLoop::current(), message_loop()); |
bool did_dispatch_message = false; |
@@ -233,7 +231,7 @@ void RawChannelPosix::OnFileCanReadWithoutBlocking(int fd) { |
} |
ssize_t bytes_read = HANDLE_EINTR( |
- read(fd_, |
+ read(fd_.get().fd, |
&read_buffer_[read_buffer_start + read_buffer_num_valid_bytes_], |
kReadSize)); |
if (bytes_read < 0) { |
@@ -305,7 +303,7 @@ void RawChannelPosix::OnFileCanReadWithoutBlocking(int fd) { |
} |
void RawChannelPosix::OnFileCanWriteWithoutBlocking(int fd) { |
- DCHECK_EQ(fd, fd_); |
+ DCHECK_EQ(fd, fd_.get().fd); |
DCHECK_EQ(base::MessageLoop::current(), message_loop()); |
bool did_fail = false; |
@@ -331,8 +329,8 @@ void RawChannelPosix::WaitToWrite() { |
DCHECK(write_watcher_.get()); |
bool result = message_loop_for_io()->WatchFileDescriptor( |
- fd_, false, base::MessageLoopForIO::WATCH_WRITE, write_watcher_.get(), |
- this); |
+ fd_.get().fd, false, base::MessageLoopForIO::WATCH_WRITE, |
+ write_watcher_.get(), this); |
DCHECK(result); |
} |
@@ -353,7 +351,7 @@ bool RawChannelPosix::WriteFrontMessageNoLock() { |
size_t bytes_to_write = |
message->size_with_header_and_padding() - write_message_offset_; |
ssize_t bytes_written = HANDLE_EINTR( |
- write(fd_, |
+ write(fd_.get().fd, |
reinterpret_cast<char*>(message) + write_message_offset_, |
bytes_to_write)); |
if (bytes_written < 0) { |
@@ -402,10 +400,10 @@ void RawChannelPosix::CancelPendingWritesNoLock() { |
// Static factory method declared in raw_channel.h. |
// static |
-RawChannel* RawChannel::Create(const PlatformChannelHandle& handle, |
+RawChannel* RawChannel::Create(ScopedPlatformHandle handle, |
Delegate* delegate, |
base::MessageLoop* message_loop) { |
- return new RawChannelPosix(handle, delegate, message_loop); |
+ return new RawChannelPosix(handle.Pass(), delegate, message_loop); |
} |
} // namespace system |