| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "mojo/edk/system/message_in_transit.h" | 16 #include "mojo/edk/system/message_in_transit.h" |
| 17 #include "mojo/edk/system/transport_data.h" | 17 #include "mojo/edk/system/transport_data.h" |
| 18 | 18 |
| 19 using mojo::platform::PlatformHandle; | 19 using mojo::platform::PlatformHandle; |
| 20 using mojo::platform::PlatformHandleWatcher; |
| 20 using mojo::platform::ScopedPlatformHandle; | 21 using mojo::platform::ScopedPlatformHandle; |
| 22 using mojo::platform::TaskRunner; |
| 21 using mojo::util::MutexLocker; | 23 using mojo::util::MutexLocker; |
| 24 using mojo::util::RefPtr; |
| 22 | 25 |
| 23 namespace mojo { | 26 namespace mojo { |
| 24 namespace system { | 27 namespace system { |
| 25 | 28 |
| 26 const size_t kReadSize = 4096; | 29 const size_t kReadSize = 4096; |
| 27 | 30 |
| 28 // RawChannel::ReadBuffer ------------------------------------------------------ | 31 // RawChannel::ReadBuffer ------------------------------------------------------ |
| 29 | 32 |
| 30 RawChannel::ReadBuffer::ReadBuffer() : buffer_(kReadSize), num_valid_bytes_(0) { | 33 RawChannel::ReadBuffer::ReadBuffer() : buffer_(kReadSize), num_valid_bytes_(0) { |
| 31 } | 34 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 Buffer buffer2 = { | 155 Buffer buffer2 = { |
| 153 static_cast<const char*>(message->transport_data()->buffer()), | 156 static_cast<const char*>(message->transport_data()->buffer()), |
| 154 transport_data_buffer_size}; | 157 transport_data_buffer_size}; |
| 155 buffers->push_back(buffer2); | 158 buffers->push_back(buffer2); |
| 156 } | 159 } |
| 157 | 160 |
| 158 // RawChannel ------------------------------------------------------------------ | 161 // RawChannel ------------------------------------------------------------------ |
| 159 | 162 |
| 160 RawChannel::RawChannel() | 163 RawChannel::RawChannel() |
| 161 : message_loop_for_io_(nullptr), | 164 : message_loop_for_io_(nullptr), |
| 165 io_watcher_(nullptr), |
| 162 delegate_(nullptr), | 166 delegate_(nullptr), |
| 163 set_on_shutdown_(nullptr), | 167 set_on_shutdown_(nullptr), |
| 164 write_stopped_(false), | 168 write_stopped_(false), |
| 165 weak_ptr_factory_(this) { | 169 weak_ptr_factory_(this) {} |
| 166 } | |
| 167 | 170 |
| 168 RawChannel::~RawChannel() { | 171 RawChannel::~RawChannel() { |
| 169 DCHECK(!read_buffer_); | 172 DCHECK(!read_buffer_); |
| 170 DCHECK(!write_buffer_); | 173 DCHECK(!write_buffer_); |
| 171 | 174 |
| 172 // No need to take |write_mutex_| here -- if there are still weak pointers | 175 // No need to take |write_mutex_| here -- if there are still weak pointers |
| 173 // outstanding, then we're hosed anyway (since we wouldn't be able to | 176 // outstanding, then we're hosed anyway (since we wouldn't be able to |
| 174 // invalidate them cleanly, since we might not be on the I/O thread). | 177 // invalidate them cleanly, since we might not be on the I/O thread). |
| 175 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 178 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
| 176 } | 179 } |
| 177 | 180 |
| 178 void RawChannel::Init(Delegate* delegate) { | 181 void RawChannel::Init(RefPtr<TaskRunner>&& io_task_runner, |
| 182 PlatformHandleWatcher* io_watcher, |
| 183 Delegate* delegate) { |
| 184 DCHECK(io_task_runner); |
| 185 DCHECK(io_watcher); |
| 179 DCHECK(delegate); | 186 DCHECK(delegate); |
| 180 | 187 |
| 181 DCHECK(!delegate_); | 188 DCHECK(!delegate_); |
| 182 delegate_ = delegate; | 189 delegate_ = delegate; |
| 190 DCHECK(!io_task_runner_); |
| 191 io_task_runner_ = io_task_runner; |
| 192 DCHECK(!io_watcher_); |
| 193 io_watcher_ = io_watcher; |
| 183 | 194 |
| 184 CHECK_EQ(base::MessageLoop::current()->type(), base::MessageLoop::TYPE_IO); | 195 CHECK_EQ(base::MessageLoop::current()->type(), base::MessageLoop::TYPE_IO); |
| 185 DCHECK(!message_loop_for_io_); | 196 DCHECK(!message_loop_for_io_); |
| 186 message_loop_for_io_ = | 197 message_loop_for_io_ = |
| 187 static_cast<base::MessageLoopForIO*>(base::MessageLoop::current()); | 198 static_cast<base::MessageLoopForIO*>(base::MessageLoop::current()); |
| 188 | 199 |
| 189 // No need to take the lock. No one should be using us yet. | 200 // No need to take the lock. No one should be using us yet. |
| 190 DCHECK(!read_buffer_); | 201 DCHECK(!read_buffer_); |
| 191 read_buffer_.reset(new ReadBuffer); | 202 read_buffer_.reset(new ReadBuffer); |
| 192 DCHECK(!write_buffer_); | 203 DCHECK(!write_buffer_); |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 | 520 |
| 510 write_stopped_ = true; | 521 write_stopped_ = true; |
| 511 write_buffer_->message_queue_.Clear(); | 522 write_buffer_->message_queue_.Clear(); |
| 512 write_buffer_->platform_handles_offset_ = 0; | 523 write_buffer_->platform_handles_offset_ = 0; |
| 513 write_buffer_->data_offset_ = 0; | 524 write_buffer_->data_offset_ = 0; |
| 514 return false; | 525 return false; |
| 515 } | 526 } |
| 516 | 527 |
| 517 } // namespace system | 528 } // namespace system |
| 518 } // namespace mojo | 529 } // namespace mojo |
| OLD | NEW |