| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/system/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/uio.h> | 11 #include <sys/uio.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | |
| 14 #include <algorithm> | 13 #include <algorithm> |
| 15 #include <deque> | 14 #include <deque> |
| 15 #include <utility> |
| 16 | 16 |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/location.h" | 18 #include "base/location.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 22 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
| 23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
| 24 #include "mojo/edk/embedder/embedder_internal.h" | 24 #include "mojo/edk/embedder/embedder_internal.h" |
| 25 #include "mojo/edk/embedder/platform_channel_utils_posix.h" | 25 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 // This is used for posting tasks from write threads to the I/O thread. It | 103 // This is used for posting tasks from write threads to the I/O thread. It |
| 104 // must only be accessed under |write_lock_|. The weak pointers it produces | 104 // must only be accessed under |write_lock_|. The weak pointers it produces |
| 105 // are only used/invalidated on the I/O thread. | 105 // are only used/invalidated on the I/O thread. |
| 106 base::WeakPtrFactory<RawChannelPosix> weak_ptr_factory_; | 106 base::WeakPtrFactory<RawChannelPosix> weak_ptr_factory_; |
| 107 | 107 |
| 108 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelPosix); | 108 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelPosix); |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 RawChannelPosix::RawChannelPosix(ScopedPlatformHandle handle) | 111 RawChannelPosix::RawChannelPosix(ScopedPlatformHandle handle) |
| 112 : fd_(handle.Pass()), | 112 : fd_(std::move(handle)), |
| 113 pending_read_(false), | 113 pending_read_(false), |
| 114 pending_write_(false), | 114 pending_write_(false), |
| 115 weak_ptr_factory_(this) { | 115 weak_ptr_factory_(this) { |
| 116 DCHECK(fd_.is_valid()); | 116 DCHECK(fd_.is_valid()); |
| 117 } | 117 } |
| 118 | 118 |
| 119 RawChannelPosix::~RawChannelPosix() { | 119 RawChannelPosix::~RawChannelPosix() { |
| 120 DCHECK(!pending_read_); | 120 DCHECK(!pending_read_); |
| 121 DCHECK(!pending_write_); | 121 DCHECK(!pending_write_); |
| 122 | 122 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 145 size_t i = 0; | 145 size_t i = 0; |
| 146 for (; platform_handles->size() - i > kPlatformChannelMaxNumHandles; | 146 for (; platform_handles->size() - i > kPlatformChannelMaxNumHandles; |
| 147 i += kPlatformChannelMaxNumHandles) { | 147 i += kPlatformChannelMaxNumHandles) { |
| 148 scoped_ptr<MessageInTransit> fd_message(new MessageInTransit( | 148 scoped_ptr<MessageInTransit> fd_message(new MessageInTransit( |
| 149 MessageInTransit::Type::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES, 0, | 149 MessageInTransit::Type::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES, 0, |
| 150 nullptr)); | 150 nullptr)); |
| 151 ScopedPlatformHandleVectorPtr fds( | 151 ScopedPlatformHandleVectorPtr fds( |
| 152 new PlatformHandleVector( | 152 new PlatformHandleVector( |
| 153 platform_handles->begin() + i, | 153 platform_handles->begin() + i, |
| 154 platform_handles->begin() + i + kPlatformChannelMaxNumHandles)); | 154 platform_handles->begin() + i + kPlatformChannelMaxNumHandles)); |
| 155 fd_message->SetTransportData(make_scoped_ptr( | 155 fd_message->SetTransportData(make_scoped_ptr(new TransportData( |
| 156 new TransportData(fds.Pass(), GetSerializedPlatformHandleSize()))); | 156 std::move(fds), GetSerializedPlatformHandleSize()))); |
| 157 RawChannel::EnqueueMessageNoLock(fd_message.Pass()); | 157 RawChannel::EnqueueMessageNoLock(std::move(fd_message)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Remove the handles that we "moved" into the other messages. | 160 // Remove the handles that we "moved" into the other messages. |
| 161 platform_handles->erase(platform_handles->begin(), | 161 platform_handles->erase(platform_handles->begin(), |
| 162 platform_handles->begin() + i); | 162 platform_handles->begin() + i); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 RawChannel::EnqueueMessageNoLock(message.Pass()); | 166 RawChannel::EnqueueMessageNoLock(std::move(message)); |
| 167 } | 167 } |
| 168 | 168 |
| 169 bool RawChannelPosix::OnReadMessageForRawChannel( | 169 bool RawChannelPosix::OnReadMessageForRawChannel( |
| 170 const MessageInTransit::View& message_view) { | 170 const MessageInTransit::View& message_view) { |
| 171 if (message_view.type() == | 171 if (message_view.type() == |
| 172 MessageInTransit::Type::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES) { | 172 MessageInTransit::Type::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES) { |
| 173 // We don't need to do anything. |RawChannel| won't extract the platform | 173 // We don't need to do anything. |RawChannel| won't extract the platform |
| 174 // handles, and they'll be accumulated in |Read()|. | 174 // handles, and they'll be accumulated in |Read()|. |
| 175 return true; | 175 return true; |
| 176 } | 176 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 189 write_watcher_.reset(); | 189 write_watcher_.reset(); |
| 190 | 190 |
| 191 SerializeReadBuffer(0u, serialized_read_buffer); | 191 SerializeReadBuffer(0u, serialized_read_buffer); |
| 192 SerializeWriteBuffer(0u, 0u, serialized_write_buffer, serialized_write_fds); | 192 SerializeWriteBuffer(0u, 0u, serialized_write_buffer, serialized_write_fds); |
| 193 | 193 |
| 194 while (!read_platform_handles_.empty()) { | 194 while (!read_platform_handles_.empty()) { |
| 195 serialized_read_fds->push_back(read_platform_handles_.front().handle); | 195 serialized_read_fds->push_back(read_platform_handles_.front().handle); |
| 196 read_platform_handles_.pop_front(); | 196 read_platform_handles_.pop_front(); |
| 197 } | 197 } |
| 198 | 198 |
| 199 return fd_.Pass(); | 199 return std::move(fd_); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void RawChannelPosix::SetSerializedFDs( | 202 void RawChannelPosix::SetSerializedFDs( |
| 203 std::vector<int>* serialized_read_fds, | 203 std::vector<int>* serialized_read_fds, |
| 204 std::vector<int>* serialized_write_fds) { | 204 std::vector<int>* serialized_write_fds) { |
| 205 if (serialized_read_fds) { | 205 if (serialized_read_fds) { |
| 206 for(auto i: *serialized_read_fds) | 206 for(auto i: *serialized_read_fds) |
| 207 read_platform_handles_.push_back(PlatformHandle(i)); | 207 read_platform_handles_.push_back(PlatformHandle(i)); |
| 208 } | 208 } |
| 209 | 209 |
| 210 if (serialized_write_fds) { | 210 if (serialized_write_fds) { |
| 211 size_t i = 0; | 211 size_t i = 0; |
| 212 while (i < serialized_write_fds->size()) { | 212 while (i < serialized_write_fds->size()) { |
| 213 size_t batch = std::min(kPlatformChannelMaxNumHandles, | 213 size_t batch = std::min(kPlatformChannelMaxNumHandles, |
| 214 serialized_write_fds->size() - i); | 214 serialized_write_fds->size() - i); |
| 215 scoped_ptr<MessageInTransit> fd_message(new MessageInTransit( | 215 scoped_ptr<MessageInTransit> fd_message(new MessageInTransit( |
| 216 MessageInTransit::Type::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES, 0, | 216 MessageInTransit::Type::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES, 0, |
| 217 nullptr)); | 217 nullptr)); |
| 218 ScopedPlatformHandleVectorPtr fds( | 218 ScopedPlatformHandleVectorPtr fds( |
| 219 new PlatformHandleVector(serialized_write_fds->begin() + i, | 219 new PlatformHandleVector(serialized_write_fds->begin() + i, |
| 220 serialized_write_fds->begin() + i + batch)); | 220 serialized_write_fds->begin() + i + batch)); |
| 221 fd_message->SetTransportData(make_scoped_ptr( | 221 fd_message->SetTransportData(make_scoped_ptr(new TransportData( |
| 222 new TransportData(fds.Pass(), GetSerializedPlatformHandleSize()))); | 222 std::move(fds), GetSerializedPlatformHandleSize()))); |
| 223 RawChannel::EnqueueMessageNoLock(fd_message.Pass()); | 223 RawChannel::EnqueueMessageNoLock(std::move(fd_message)); |
| 224 i += batch; | 224 i += batch; |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 | 228 |
| 229 bool RawChannelPosix::IsHandleValid() { | 229 bool RawChannelPosix::IsHandleValid() { |
| 230 return fd_.is_valid(); | 230 return fd_.is_valid(); |
| 231 } | 231 } |
| 232 | 232 |
| 233 RawChannel::IOResult RawChannelPosix::Read(size_t* bytes_read) { | 233 RawChannel::IOResult RawChannelPosix::Read(size_t* bytes_read) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 262 return ScopedPlatformHandleVectorPtr(); | 262 return ScopedPlatformHandleVectorPtr(); |
| 263 } | 263 } |
| 264 | 264 |
| 265 ScopedPlatformHandleVectorPtr rv( | 265 ScopedPlatformHandleVectorPtr rv( |
| 266 new PlatformHandleVector(num_platform_handles)); | 266 new PlatformHandleVector(num_platform_handles)); |
| 267 rv->assign(read_platform_handles_.begin(), | 267 rv->assign(read_platform_handles_.begin(), |
| 268 read_platform_handles_.begin() + num_platform_handles); | 268 read_platform_handles_.begin() + num_platform_handles); |
| 269 read_platform_handles_.erase( | 269 read_platform_handles_.erase( |
| 270 read_platform_handles_.begin(), | 270 read_platform_handles_.begin(), |
| 271 read_platform_handles_.begin() + num_platform_handles); | 271 read_platform_handles_.begin() + num_platform_handles); |
| 272 return rv.Pass(); | 272 return rv; |
| 273 } | 273 } |
| 274 | 274 |
| 275 size_t RawChannelPosix::SerializePlatformHandles(std::vector<int>* fds) { | 275 size_t RawChannelPosix::SerializePlatformHandles(std::vector<int>* fds) { |
| 276 if (!write_buffer_no_lock()->HavePlatformHandlesToSend()) | 276 if (!write_buffer_no_lock()->HavePlatformHandlesToSend()) |
| 277 return 0u; | 277 return 0u; |
| 278 | 278 |
| 279 size_t num_platform_handles; | 279 size_t num_platform_handles; |
| 280 PlatformHandle* platform_handles; | 280 PlatformHandle* platform_handles; |
| 281 void* serialization_data; // Actually unused. | 281 void* serialization_data; // Actually unused. |
| 282 write_buffer_no_lock()->GetPlatformHandlesToSend( | 282 write_buffer_no_lock()->GetPlatformHandlesToSend( |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 } | 550 } |
| 551 } | 551 } |
| 552 | 552 |
| 553 } // namespace | 553 } // namespace |
| 554 | 554 |
| 555 // ----------------------------------------------------------------------------- | 555 // ----------------------------------------------------------------------------- |
| 556 | 556 |
| 557 // Static factory method declared in raw_channel.h. | 557 // Static factory method declared in raw_channel.h. |
| 558 // static | 558 // static |
| 559 RawChannel* RawChannel::Create(ScopedPlatformHandle handle) { | 559 RawChannel* RawChannel::Create(ScopedPlatformHandle handle) { |
| 560 return new RawChannelPosix(handle.Pass()); | 560 return new RawChannelPosix(std::move(handle)); |
| 561 } | 561 } |
| 562 | 562 |
| 563 size_t RawChannel::GetSerializedPlatformHandleSize() { | 563 size_t RawChannel::GetSerializedPlatformHandleSize() { |
| 564 // We don't actually need any space on POSIX (since we just send FDs). | 564 // We don't actually need any space on POSIX (since we just send FDs). |
| 565 return 0; | 565 return 0; |
| 566 } | 566 } |
| 567 | 567 |
| 568 bool RawChannel::IsOtherEndOf(RawChannel* other) { | 568 bool RawChannel::IsOtherEndOf(RawChannel* other) { |
| 569 #if defined(OFFICIAL_BUILD) | 569 #if defined(OFFICIAL_BUILD) |
| 570 return false; | 570 return false; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 583 socklen_t peek_off_size = sizeof(id1); | 583 socklen_t peek_off_size = sizeof(id1); |
| 584 getsockopt(this_handle.handle, SOL_SOCKET, SO_PEEK_OFF, &id1, &peek_off_size); | 584 getsockopt(this_handle.handle, SOL_SOCKET, SO_PEEK_OFF, &id1, &peek_off_size); |
| 585 getsockopt(other_handle.handle, SOL_SOCKET, SO_PEEK_OFF, &id2, | 585 getsockopt(other_handle.handle, SOL_SOCKET, SO_PEEK_OFF, &id2, |
| 586 &peek_off_size); | 586 &peek_off_size); |
| 587 return id1 == id2; | 587 return id1 == id2; |
| 588 #endif | 588 #endif |
| 589 } | 589 } |
| 590 | 590 |
| 591 } // namespace edk | 591 } // namespace edk |
| 592 } // namespace mojo | 592 } // namespace mojo |
| OLD | NEW |