| 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 | 10 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 RawChannel::~RawChannel() { | 164 RawChannel::~RawChannel() { |
| 165 DCHECK(!read_buffer_); | 165 DCHECK(!read_buffer_); |
| 166 DCHECK(!write_buffer_); | 166 DCHECK(!write_buffer_); |
| 167 | 167 |
| 168 // No need to take the |write_lock_| here -- if there are still weak pointers | 168 // No need to take the |write_lock_| here -- if there are still weak pointers |
| 169 // outstanding, then we're hosed anyway (since we wouldn't be able to | 169 // outstanding, then we're hosed anyway (since we wouldn't be able to |
| 170 // invalidate them cleanly, since we might not be on the I/O thread). | 170 // invalidate them cleanly, since we might not be on the I/O thread). |
| 171 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 171 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool RawChannel::Init(Delegate* delegate) { | 174 void RawChannel::Init(Delegate* delegate) { |
| 175 DCHECK(delegate); | 175 DCHECK(delegate); |
| 176 | 176 |
| 177 DCHECK(!delegate_); | 177 DCHECK(!delegate_); |
| 178 delegate_ = delegate; | 178 delegate_ = delegate; |
| 179 | 179 |
| 180 CHECK_EQ(base::MessageLoop::current()->type(), base::MessageLoop::TYPE_IO); | 180 CHECK_EQ(base::MessageLoop::current()->type(), base::MessageLoop::TYPE_IO); |
| 181 DCHECK(!message_loop_for_io_); | 181 DCHECK(!message_loop_for_io_); |
| 182 message_loop_for_io_ = | 182 message_loop_for_io_ = |
| 183 static_cast<base::MessageLoopForIO*>(base::MessageLoop::current()); | 183 static_cast<base::MessageLoopForIO*>(base::MessageLoop::current()); |
| 184 | 184 |
| 185 // No need to take the lock. No one should be using us yet. | 185 // No need to take the lock. No one should be using us yet. |
| 186 DCHECK(!read_buffer_); | 186 DCHECK(!read_buffer_); |
| 187 read_buffer_.reset(new ReadBuffer); | 187 read_buffer_.reset(new ReadBuffer); |
| 188 DCHECK(!write_buffer_); | 188 DCHECK(!write_buffer_); |
| 189 write_buffer_.reset(new WriteBuffer(GetSerializedPlatformHandleSize())); | 189 write_buffer_.reset(new WriteBuffer(GetSerializedPlatformHandleSize())); |
| 190 | 190 |
| 191 if (!OnInit()) { | 191 OnInit(); |
| 192 delegate_ = nullptr; | |
| 193 message_loop_for_io_ = nullptr; | |
| 194 read_buffer_.reset(); | |
| 195 write_buffer_.reset(); | |
| 196 return false; | |
| 197 } | |
| 198 | 192 |
| 199 IOResult io_result = ScheduleRead(); | 193 IOResult io_result = ScheduleRead(); |
| 200 if (io_result != IO_PENDING) { | 194 if (io_result != IO_PENDING) { |
| 201 // This will notify the delegate about the read failure. Although we're on | 195 // This will notify the delegate about the read failure. Although we're on |
| 202 // the I/O thread, don't call it in the nested context. | 196 // the I/O thread, don't call it in the nested context. |
| 203 message_loop_for_io_->PostTask( | 197 message_loop_for_io_->PostTask( |
| 204 FROM_HERE, base::Bind(&RawChannel::OnReadCompleted, | 198 FROM_HERE, base::Bind(&RawChannel::OnReadCompleted, |
| 205 weak_ptr_factory_.GetWeakPtr(), io_result, 0)); | 199 weak_ptr_factory_.GetWeakPtr(), io_result, 0)); |
| 206 } | 200 } |
| 207 | 201 // Note: |ScheduleRead()| failure is treated as a read failure (by notifying |
| 208 // ScheduleRead() failure is treated as a read failure (by notifying the | 202 // the delegate), not an initialization failure. |
| 209 // delegate), not as an init failure. | |
| 210 return true; | |
| 211 } | 203 } |
| 212 | 204 |
| 213 void RawChannel::Shutdown() { | 205 void RawChannel::Shutdown() { |
| 214 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); | 206 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); |
| 215 | 207 |
| 216 base::AutoLock locker(write_lock_); | 208 base::AutoLock locker(write_lock_); |
| 217 | 209 |
| 218 LOG_IF(WARNING, !write_buffer_->message_queue_.empty()) | 210 LOG_IF(WARNING, !write_buffer_->message_queue_.empty()) |
| 219 << "Shutting down RawChannel with write buffer nonempty"; | 211 << "Shutting down RawChannel with write buffer nonempty"; |
| 220 | 212 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 | 505 |
| 514 write_stopped_ = true; | 506 write_stopped_ = true; |
| 515 STLDeleteElements(&write_buffer_->message_queue_); | 507 STLDeleteElements(&write_buffer_->message_queue_); |
| 516 write_buffer_->platform_handles_offset_ = 0; | 508 write_buffer_->platform_handles_offset_ = 0; |
| 517 write_buffer_->data_offset_ = 0; | 509 write_buffer_->data_offset_ = 0; |
| 518 return false; | 510 return false; |
| 519 } | 511 } |
| 520 | 512 |
| 521 } // namespace system | 513 } // namespace system |
| 522 } // namespace mojo | 514 } // namespace mojo |
| OLD | NEW |