| 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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } | 215 } |
| 216 | 216 |
| 217 void RawChannel::LazyInitialize() { | 217 void RawChannel::LazyInitialize() { |
| 218 read_lock_.AssertAcquired(); | 218 read_lock_.AssertAcquired(); |
| 219 write_lock_.AssertAcquired(); | 219 write_lock_.AssertAcquired(); |
| 220 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); | 220 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); |
| 221 if (initialized_) | 221 if (initialized_) |
| 222 return; | 222 return; |
| 223 initialized_ = true; | 223 initialized_ = true; |
| 224 internal::ChannelStarted(); | 224 internal::ChannelStarted(); |
| 225 base::MessageLoop::current()->AddDestructionObserver(this); |
| 225 | 226 |
| 226 OnInit(); | 227 OnInit(); |
| 227 | 228 |
| 228 if (read_buffer_->num_valid_bytes_) { | 229 if (read_buffer_->num_valid_bytes_) { |
| 229 // We had serialized read buffer data through SetSerializedData call. | 230 // We had serialized read buffer data through SetSerializedData call. |
| 230 // Make sure we read messages out of it now, otherwise the delegate won't | 231 // Make sure we read messages out of it now, otherwise the delegate won't |
| 231 // get notified if no other data gets written to the pipe. | 232 // get notified if no other data gets written to the pipe. |
| 232 // Although this means that we can call back synchronously into the caller, | 233 // Although this means that we can call back synchronously into the caller, |
| 233 // that's easier than posting a task to do this. That is because if we post | 234 // that's easier than posting a task to do this. That is because if we post |
| 234 // a task, a pending read could have started and we wouldn't be able to move | 235 // a task, a pending read could have started and we wouldn't be able to move |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 // doesn't apply when 1) we don't have a handle (for obvious reasons), | 280 // doesn't apply when 1) we don't have a handle (for obvious reasons), |
| 280 // 2) we have a read or write error before (doesn't matter which), or 3) when | 281 // 2) we have a read or write error before (doesn't matter which), or 3) when |
| 281 // there are no pending messages to be written. | 282 // there are no pending messages to be written. |
| 282 if (!IsHandleValid() || error_occurred_ || empty) { | 283 if (!IsHandleValid() || error_occurred_ || empty) { |
| 283 { | 284 { |
| 284 base::AutoLock read_locker(read_lock_); | 285 base::AutoLock read_locker(read_lock_); |
| 285 base::AutoLock locker(write_lock_); | 286 base::AutoLock locker(write_lock_); |
| 286 OnShutdownNoLock(read_buffer_.Pass(), write_buffer_.Pass()); | 287 OnShutdownNoLock(read_buffer_.Pass(), write_buffer_.Pass()); |
| 287 } | 288 } |
| 288 | 289 |
| 289 if (initialized_) | 290 if (initialized_) { |
| 290 internal::ChannelShutdown(); | 291 internal::ChannelShutdown(); |
| 292 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 293 } |
| 291 delete this; | 294 delete this; |
| 292 return; | 295 return; |
| 293 } | 296 } |
| 294 | 297 |
| 295 base::AutoLock read_locker(read_lock_); | 298 base::AutoLock read_locker(read_lock_); |
| 296 base::AutoLock locker(write_lock_); | 299 base::AutoLock locker(write_lock_); |
| 297 DCHECK(read_buffer_->IsEmpty()) << | 300 DCHECK(read_buffer_->IsEmpty()) << |
| 298 "RawChannel::Shutdown called but there is pending data to be read"; | 301 "RawChannel::Shutdown called but there is pending data to be read"; |
| 299 | 302 |
| 300 write_stopped_ = true; | 303 write_stopped_ = true; |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 if (write_buffer_->data_offset_ >= message->total_size()) { | 711 if (write_buffer_->data_offset_ >= message->total_size()) { |
| 709 // Complete write. | 712 // Complete write. |
| 710 CHECK_EQ(write_buffer_->data_offset_, message->total_size()); | 713 CHECK_EQ(write_buffer_->data_offset_, message->total_size()); |
| 711 write_buffer_->message_queue_.DiscardMessage(); | 714 write_buffer_->message_queue_.DiscardMessage(); |
| 712 write_buffer_->platform_handles_offset_ = 0; | 715 write_buffer_->platform_handles_offset_ = 0; |
| 713 write_buffer_->data_offset_ = 0; | 716 write_buffer_->data_offset_ = 0; |
| 714 } | 717 } |
| 715 } | 718 } |
| 716 | 719 |
| 717 void RawChannel::CallOnReadCompleted(IOResult io_result, size_t bytes_read) { | 720 void RawChannel::CallOnReadCompleted(IOResult io_result, size_t bytes_read) { |
| 718 base::AutoLock locker(read_lock()); | 721 base::AutoLock locker(read_lock_); |
| 719 OnReadCompletedNoLock(io_result, bytes_read); | 722 OnReadCompletedNoLock(io_result, bytes_read); |
| 720 } | 723 } |
| 721 | 724 |
| 725 void RawChannel::WillDestroyCurrentMessageLoop() { |
| 726 base::AutoLock locker(read_lock_); |
| 727 OnReadCompletedNoLock(IO_FAILED_SHUTDOWN, 0); |
| 728 } |
| 729 |
| 722 } // namespace edk | 730 } // namespace edk |
| 723 } // namespace mojo | 731 } // namespace mojo |
| OLD | NEW |