diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-21 19:22:22 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-21 19:22:22 +0000 |
commit | 0e47224e384538d0f06e64e7f12e6847c22618ed (patch) | |
tree | 03f14743effce82bd41c02c3d4b871fa68ec1c2a | |
parent | aafb5903c8a2b8fc896f46fc47e5a1d1ced834e9 (diff) | |
download | chromium_src-0e47224e384538d0f06e64e7f12e6847c22618ed.zip chromium_src-0e47224e384538d0f06e64e7f12e6847c22618ed.tar.gz chromium_src-0e47224e384538d0f06e64e7f12e6847c22618ed.tar.bz2 |
Mojo: Convert MessageInTransit* -> scoped_ptr<MessageInTransit>, part 1.
R=yzshen@chromium.org
Review URL: https://codereview.chromium.org/170013003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252607 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | mojo/system/channel.cc | 4 | ||||
-rw-r--r-- | mojo/system/channel.h | 2 | ||||
-rw-r--r-- | mojo/system/proxy_message_pipe_endpoint.cc | 3 | ||||
-rw-r--r-- | mojo/system/raw_channel.h | 3 | ||||
-rw-r--r-- | mojo/system/raw_channel_posix.cc | 24 | ||||
-rw-r--r-- | mojo/system/raw_channel_posix_unittest.cc | 14 |
6 files changed, 24 insertions, 26 deletions
diff --git a/mojo/system/channel.cc b/mojo/system/channel.cc index 7add49a..93a0559 100644 --- a/mojo/system/channel.cc +++ b/mojo/system/channel.cc @@ -107,7 +107,7 @@ void Channel::RunMessagePipeEndpoint(MessageInTransit::EndpointId local_id, endpoint_info.message_pipe->Run(endpoint_info.port, remote_id); } -bool Channel::WriteMessage(MessageInTransit* message) { +bool Channel::WriteMessage(scoped_ptr<MessageInTransit> message) { base::AutoLock locker(lock_); if (!raw_channel_.get()) { // TODO(vtl): I think this is probably not an error condition, but I should @@ -116,7 +116,7 @@ bool Channel::WriteMessage(MessageInTransit* message) { return false; } - return raw_channel_->WriteMessage(message); + return raw_channel_->WriteMessage(message.Pass()); } void Channel::DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id) { diff --git a/mojo/system/channel.h b/mojo/system/channel.h index 13d023d..e0285dc 100644 --- a/mojo/system/channel.h +++ b/mojo/system/channel.h @@ -81,7 +81,7 @@ class MOJO_SYSTEM_IMPL_EXPORT Channel MessageInTransit::EndpointId remote_id); // This forwards |message| verbatim to |raw_channel_|. - bool WriteMessage(MessageInTransit* message); + bool WriteMessage(scoped_ptr<MessageInTransit> message); // This removes the message pipe/port's endpoint (with the given local ID, // returned by |AttachMessagePipeEndpoint()| from this channel. After this is diff --git a/mojo/system/proxy_message_pipe_endpoint.cc b/mojo/system/proxy_message_pipe_endpoint.cc index 2bb8777..e3a79a0 100644 --- a/mojo/system/proxy_message_pipe_endpoint.cc +++ b/mojo/system/proxy_message_pipe_endpoint.cc @@ -129,7 +129,8 @@ void ProxyMessagePipeEndpoint::EnqueueMessageInternal( // If it fails at this point, the message gets dropped. (This is no // different from any other in-transit errors.) // Note: |WriteMessage()| will destroy the message even on failure. - if (!channel_->WriteMessage(message)) + // TODO(vtl): Convert |message| (and so on "upstream") to a |scoped_ptr|. + if (!channel_->WriteMessage(make_scoped_ptr(message))) LOG(WARNING) << "Failed to write message to channel"; } else { paused_message_queue_.push_back(message); diff --git a/mojo/system/raw_channel.h b/mojo/system/raw_channel.h index 30f67db..4b14edd 100644 --- a/mojo/system/raw_channel.h +++ b/mojo/system/raw_channel.h @@ -8,6 +8,7 @@ #include <vector> #include "base/macros.h" +#include "base/memory/scoped_ptr.h" #include "mojo/system/constants.h" #include "mojo/system/embedder/scoped_platform_handle.h" #include "mojo/system/system_impl_export.h" @@ -84,7 +85,7 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel { // This is thread-safe. It takes ownership of |message| (always, even on // failure). Returns true on success. - virtual bool WriteMessage(MessageInTransit* message) = 0; + virtual bool WriteMessage(scoped_ptr<MessageInTransit> message) = 0; protected: RawChannel(Delegate* delegate, base::MessageLoopForIO* message_loop_for_io) diff --git a/mojo/system/raw_channel_posix.cc b/mojo/system/raw_channel_posix.cc index 9c187b5..433ea4f 100644 --- a/mojo/system/raw_channel_posix.cc +++ b/mojo/system/raw_channel_posix.cc @@ -21,6 +21,7 @@ #include "base/memory/weak_ptr.h" #include "base/message_loop/message_loop.h" #include "base/posix/eintr_wrapper.h" +#include "base/stl_util.h" #include "base/synchronization/lock.h" #include "mojo/system/embedder/platform_handle.h" #include "mojo/system/message_in_transit.h" @@ -43,7 +44,7 @@ class RawChannelPosix : public RawChannel, // |RawChannel| implementation: virtual bool Init() OVERRIDE; virtual void Shutdown() OVERRIDE; - virtual bool WriteMessage(MessageInTransit* message) OVERRIDE; + virtual bool WriteMessage(scoped_ptr<MessageInTransit> message) OVERRIDE; private: // |base::MessageLoopForIO::Watcher| implementation: @@ -83,6 +84,8 @@ class RawChannelPosix : public RawChannel, base::Lock write_lock_; // Protects the following members. bool write_stopped_; + // TODO(vtl): When C++11 is available, switch this to a deque of + // |scoped_ptr|/|unique_ptr|s. std::deque<MessageInTransit*> write_message_queue_; size_t write_message_offset_; // This is used for posting tasks from write threads to the I/O thread. It @@ -162,19 +165,17 @@ void RawChannelPosix::Shutdown() { } // Reminder: This must be thread-safe, and takes ownership of |message|. -bool RawChannelPosix::WriteMessage(MessageInTransit* message) { +bool RawChannelPosix::WriteMessage(scoped_ptr<MessageInTransit> message) { base::AutoLock locker(write_lock_); - if (write_stopped_) { - delete message; + if (write_stopped_) return false; - } if (!write_message_queue_.empty()) { - write_message_queue_.push_back(message); + write_message_queue_.push_back(message.release()); return true; } - write_message_queue_.push_front(message); + write_message_queue_.push_front(message.release()); DCHECK_EQ(write_message_offset_, 0u); bool result = WriteFrontMessageNoLock(); DCHECK(result || write_message_queue_.empty()); @@ -384,8 +385,8 @@ bool RawChannelPosix::WriteFrontMessageNoLock() { // Complete write. DCHECK_EQ(static_cast<size_t>(bytes_written), bytes_to_write); write_message_queue_.pop_front(); - write_message_offset_ = 0; delete message; + write_message_offset_ = 0; } return true; @@ -396,12 +397,7 @@ void RawChannelPosix::CancelPendingWritesNoLock() { DCHECK(!write_stopped_); write_stopped_ = true; - for (std::deque<MessageInTransit*>::iterator it = - write_message_queue_.begin(); it != write_message_queue_.end(); - ++it) { - delete *it; - } - write_message_queue_.clear(); + STLDeleteElements(&write_message_queue_); } } // namespace diff --git a/mojo/system/raw_channel_posix_unittest.cc b/mojo/system/raw_channel_posix_unittest.cc index 210668c..4158bb7 100644 --- a/mojo/system/raw_channel_posix_unittest.cc +++ b/mojo/system/raw_channel_posix_unittest.cc @@ -39,14 +39,15 @@ namespace mojo { namespace system { namespace { -MessageInTransit* MakeTestMessage(uint32_t num_bytes) { +scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { std::vector<unsigned char> bytes(num_bytes, 0); for (size_t i = 0; i < num_bytes; i++) bytes[i] = static_cast<unsigned char>(i + num_bytes); - return new MessageInTransit(MessageInTransit::OWNED_BUFFER, - MessageInTransit::kTypeMessagePipeEndpoint, - MessageInTransit::kSubtypeMessagePipeEndpointData, - num_bytes, 0, bytes.data()); + return make_scoped_ptr( + new MessageInTransit(MessageInTransit::OWNED_BUFFER, + MessageInTransit::kTypeMessagePipeEndpoint, + MessageInTransit::kSubtypeMessagePipeEndpointData, + num_bytes, 0, bytes.data())); } bool CheckMessageData(const void* bytes, uint32_t num_bytes) { @@ -64,12 +65,11 @@ void InitOnIOThread(RawChannel* raw_channel) { bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, uint32_t num_bytes) { - MessageInTransit* message = MakeTestMessage(num_bytes); + scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); ssize_t write_size = HANDLE_EINTR( write(handle.fd, message->main_buffer(), message->main_buffer_size())); bool result = write_size == static_cast<ssize_t>(message->main_buffer_size()); - delete message; return result; } |