| 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/system/raw_channel.h" | 5 #include "mojo/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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 212 |
| 213 // Reminder: This must be thread-safe. | 213 // Reminder: This must be thread-safe. |
| 214 bool RawChannel::WriteMessage(scoped_ptr<MessageInTransit> message) { | 214 bool RawChannel::WriteMessage(scoped_ptr<MessageInTransit> message) { |
| 215 DCHECK(message); | 215 DCHECK(message); |
| 216 | 216 |
| 217 base::AutoLock locker(write_lock_); | 217 base::AutoLock locker(write_lock_); |
| 218 if (write_stopped_) | 218 if (write_stopped_) |
| 219 return false; | 219 return false; |
| 220 | 220 |
| 221 if (!write_buffer_->message_queue_.empty()) { | 221 if (!write_buffer_->message_queue_.empty()) { |
| 222 write_buffer_->message_queue_.push_back(message.release()); | 222 EnqueueMessageNoLock(message.Pass()); |
| 223 return true; | 223 return true; |
| 224 } | 224 } |
| 225 | 225 |
| 226 write_buffer_->message_queue_.push_front(message.release()); | 226 EnqueueMessageNoLock(message.Pass()); |
| 227 DCHECK_EQ(write_buffer_->data_offset_, 0u); | 227 DCHECK_EQ(write_buffer_->data_offset_, 0u); |
| 228 | 228 |
| 229 size_t platform_handles_written = 0; | 229 size_t platform_handles_written = 0; |
| 230 size_t bytes_written = 0; | 230 size_t bytes_written = 0; |
| 231 IOResult io_result = WriteNoLock(&platform_handles_written, &bytes_written); | 231 IOResult io_result = WriteNoLock(&platform_handles_written, &bytes_written); |
| 232 if (io_result == IO_PENDING) | 232 if (io_result == IO_PENDING) |
| 233 return true; | 233 return true; |
| 234 | 234 |
| 235 bool result = OnWriteCompletedNoLock(io_result == IO_SUCCEEDED, | 235 bool result = OnWriteCompletedNoLock(io_result == IO_SUCCEEDED, |
| 236 platform_handles_written, | 236 platform_handles_written, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 247 | 247 |
| 248 return result; | 248 return result; |
| 249 } | 249 } |
| 250 | 250 |
| 251 // Reminder: This must be thread-safe. | 251 // Reminder: This must be thread-safe. |
| 252 bool RawChannel::IsWriteBufferEmpty() { | 252 bool RawChannel::IsWriteBufferEmpty() { |
| 253 base::AutoLock locker(write_lock_); | 253 base::AutoLock locker(write_lock_); |
| 254 return write_buffer_->message_queue_.empty(); | 254 return write_buffer_->message_queue_.empty(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 RawChannel::ReadBuffer* RawChannel::read_buffer() { | |
| 258 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); | |
| 259 return read_buffer_.get(); | |
| 260 } | |
| 261 | |
| 262 RawChannel::WriteBuffer* RawChannel::write_buffer_no_lock() { | |
| 263 write_lock_.AssertAcquired(); | |
| 264 return write_buffer_.get(); | |
| 265 } | |
| 266 | |
| 267 void RawChannel::OnReadCompleted(bool result, size_t bytes_read) { | 257 void RawChannel::OnReadCompleted(bool result, size_t bytes_read) { |
| 268 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); | 258 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); |
| 269 | 259 |
| 270 if (read_stopped_) { | 260 if (read_stopped_) { |
| 271 NOTREACHED(); | 261 NOTREACHED(); |
| 272 return; | 262 return; |
| 273 } | 263 } |
| 274 | 264 |
| 275 IOResult io_result = result ? IO_SUCCEEDED : IO_FAILED; | 265 IOResult io_result = result ? IO_SUCCEEDED : IO_FAILED; |
| 276 | 266 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 | 406 |
| 417 did_fail = !OnWriteCompletedNoLock(result, | 407 did_fail = !OnWriteCompletedNoLock(result, |
| 418 platform_handles_written, | 408 platform_handles_written, |
| 419 bytes_written); | 409 bytes_written); |
| 420 } | 410 } |
| 421 | 411 |
| 422 if (did_fail) | 412 if (did_fail) |
| 423 CallOnFatalError(Delegate::FATAL_ERROR_FAILED_WRITE); | 413 CallOnFatalError(Delegate::FATAL_ERROR_FAILED_WRITE); |
| 424 } | 414 } |
| 425 | 415 |
| 416 void RawChannel::EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message) { |
| 417 write_lock_.AssertAcquired(); |
| 418 write_buffer_->message_queue_.push_back(message.release()); |
| 419 } |
| 420 |
| 426 void RawChannel::CallOnFatalError(Delegate::FatalError fatal_error) { | 421 void RawChannel::CallOnFatalError(Delegate::FatalError fatal_error) { |
| 427 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); | 422 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io_); |
| 428 // TODO(vtl): Add a "write_lock_.AssertNotAcquired()"? | 423 // TODO(vtl): Add a "write_lock_.AssertNotAcquired()"? |
| 429 if (delegate_) | 424 if (delegate_) |
| 430 delegate_->OnFatalError(fatal_error); | 425 delegate_->OnFatalError(fatal_error); |
| 431 } | 426 } |
| 432 | 427 |
| 433 bool RawChannel::OnWriteCompletedNoLock(bool result, | 428 bool RawChannel::OnWriteCompletedNoLock(bool result, |
| 434 size_t platform_handles_written, | 429 size_t platform_handles_written, |
| 435 size_t bytes_written) { | 430 size_t bytes_written) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 464 | 459 |
| 465 write_stopped_ = true; | 460 write_stopped_ = true; |
| 466 STLDeleteElements(&write_buffer_->message_queue_); | 461 STLDeleteElements(&write_buffer_->message_queue_); |
| 467 write_buffer_->platform_handles_offset_ = 0; | 462 write_buffer_->platform_handles_offset_ = 0; |
| 468 write_buffer_->data_offset_ = 0; | 463 write_buffer_->data_offset_ = 0; |
| 469 return false; | 464 return false; |
| 470 } | 465 } |
| 471 | 466 |
| 472 } // namespace system | 467 } // namespace system |
| 473 } // namespace mojo | 468 } // namespace mojo |
| OLD | NEW |