| 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 "extensions/browser/api/cast_channel/cast_transport.h" | 5 #include "extensions/browser/api/cast_channel/cast_transport.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 DCHECK(socket); | 49 DCHECK(socket); |
| 50 | 50 |
| 51 // Buffer is reused across messages to minimize unnecessary buffer | 51 // Buffer is reused across messages to minimize unnecessary buffer |
| 52 // [re]allocations. | 52 // [re]allocations. |
| 53 read_buffer_ = new net::GrowableIOBuffer(); | 53 read_buffer_ = new net::GrowableIOBuffer(); |
| 54 read_buffer_->SetCapacity(MessageFramer::MessageHeader::max_message_size()); | 54 read_buffer_->SetCapacity(MessageFramer::MessageHeader::max_message_size()); |
| 55 framer_.reset(new MessageFramer(read_buffer_)); | 55 framer_.reset(new MessageFramer(read_buffer_)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 CastTransportImpl::~CastTransportImpl() { | 58 CastTransportImpl::~CastTransportImpl() { |
| 59 DCHECK(CalledOnValidThread()); | 59 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 60 FlushWriteQueue(); | 60 FlushWriteQueue(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool CastTransportImpl::IsTerminalWriteState( | 63 bool CastTransportImpl::IsTerminalWriteState( |
| 64 CastTransportImpl::WriteState write_state) { | 64 CastTransportImpl::WriteState write_state) { |
| 65 return write_state == WRITE_STATE_ERROR || write_state == WRITE_STATE_IDLE; | 65 return write_state == WRITE_STATE_ERROR || write_state == WRITE_STATE_IDLE; |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool CastTransportImpl::IsTerminalReadState( | 68 bool CastTransportImpl::IsTerminalReadState( |
| 69 CastTransportImpl::ReadState read_state) { | 69 CastTransportImpl::ReadState read_state) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 return proto::CHANNEL_ERROR_CONNECT_TIMEOUT; | 139 return proto::CHANNEL_ERROR_CONNECT_TIMEOUT; |
| 140 case CHANNEL_ERROR_UNKNOWN: | 140 case CHANNEL_ERROR_UNKNOWN: |
| 141 return proto::CHANNEL_ERROR_UNKNOWN; | 141 return proto::CHANNEL_ERROR_UNKNOWN; |
| 142 default: | 142 default: |
| 143 NOTREACHED(); | 143 NOTREACHED(); |
| 144 return proto::CHANNEL_ERROR_NONE; | 144 return proto::CHANNEL_ERROR_NONE; |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 void CastTransportImpl::SetReadDelegate(std::unique_ptr<Delegate> delegate) { | 148 void CastTransportImpl::SetReadDelegate(std::unique_ptr<Delegate> delegate) { |
| 149 DCHECK(CalledOnValidThread()); | 149 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 150 DCHECK(delegate); | 150 DCHECK(delegate); |
| 151 delegate_ = std::move(delegate); | 151 delegate_ = std::move(delegate); |
| 152 if (started_) { | 152 if (started_) { |
| 153 delegate_->Start(); | 153 delegate_->Start(); |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 void CastTransportImpl::FlushWriteQueue() { | 157 void CastTransportImpl::FlushWriteQueue() { |
| 158 for (; !write_queue_.empty(); write_queue_.pop()) { | 158 for (; !write_queue_.empty(); write_queue_.pop()) { |
| 159 net::CompletionCallback& callback = write_queue_.front().callback; | 159 net::CompletionCallback& callback = write_queue_.front().callback; |
| 160 base::ThreadTaskRunnerHandle::Get()->PostTask( | 160 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 161 FROM_HERE, base::Bind(callback, net::ERR_FAILED)); | 161 FROM_HERE, base::Bind(callback, net::ERR_FAILED)); |
| 162 callback.Reset(); | 162 callback.Reset(); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 void CastTransportImpl::SendMessage(const CastMessage& message, | 166 void CastTransportImpl::SendMessage(const CastMessage& message, |
| 167 const net::CompletionCallback& callback) { | 167 const net::CompletionCallback& callback) { |
| 168 DCHECK(CalledOnValidThread()); | 168 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 169 std::string serialized_message; | 169 std::string serialized_message; |
| 170 if (!MessageFramer::Serialize(message, &serialized_message)) { | 170 if (!MessageFramer::Serialize(message, &serialized_message)) { |
| 171 base::ThreadTaskRunnerHandle::Get()->PostTask( | 171 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 172 FROM_HERE, base::Bind(callback, net::ERR_FAILED)); | 172 FROM_HERE, base::Bind(callback, net::ERR_FAILED)); |
| 173 return; | 173 return; |
| 174 } | 174 } |
| 175 WriteRequest write_request( | 175 WriteRequest write_request( |
| 176 message.namespace_(), serialized_message, callback); | 176 message.namespace_(), serialized_message, callback); |
| 177 | 177 |
| 178 write_queue_.push(write_request); | 178 write_queue_.push(write_request); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 207 if (write_state_ != write_state) | 207 if (write_state_ != write_state) |
| 208 write_state_ = write_state; | 208 write_state_ = write_state; |
| 209 } | 209 } |
| 210 | 210 |
| 211 void CastTransportImpl::SetErrorState(ChannelError error_state) { | 211 void CastTransportImpl::SetErrorState(ChannelError error_state) { |
| 212 VLOG_WITH_CONNECTION(2) << "SetErrorState: " << error_state; | 212 VLOG_WITH_CONNECTION(2) << "SetErrorState: " << error_state; |
| 213 error_state_ = error_state; | 213 error_state_ = error_state; |
| 214 } | 214 } |
| 215 | 215 |
| 216 void CastTransportImpl::OnWriteResult(int result) { | 216 void CastTransportImpl::OnWriteResult(int result) { |
| 217 DCHECK(CalledOnValidThread()); | 217 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 218 DCHECK_NE(WRITE_STATE_IDLE, write_state_); | 218 DCHECK_NE(WRITE_STATE_IDLE, write_state_); |
| 219 if (write_queue_.empty()) { | 219 if (write_queue_.empty()) { |
| 220 SetWriteState(WRITE_STATE_IDLE); | 220 SetWriteState(WRITE_STATE_IDLE); |
| 221 return; | 221 return; |
| 222 } | 222 } |
| 223 | 223 |
| 224 // Network operations can either finish synchronously or asynchronously. | 224 // Network operations can either finish synchronously or asynchronously. |
| 225 // This method executes the state machine transitions in a loop so that | 225 // This method executes the state machine transitions in a loop so that |
| 226 // write state transitions happen even when network operations finish | 226 // write state transitions happen even when network operations finish |
| 227 // synchronously. | 227 // synchronously. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 | 323 |
| 324 int CastTransportImpl::DoWriteHandleError(int result) { | 324 int CastTransportImpl::DoWriteHandleError(int result) { |
| 325 VLOG_WITH_CONNECTION(2) << "DoWriteHandleError result=" << result; | 325 VLOG_WITH_CONNECTION(2) << "DoWriteHandleError result=" << result; |
| 326 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); | 326 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); |
| 327 DCHECK_LT(result, 0); | 327 DCHECK_LT(result, 0); |
| 328 SetWriteState(WRITE_STATE_ERROR); | 328 SetWriteState(WRITE_STATE_ERROR); |
| 329 return net::ERR_FAILED; | 329 return net::ERR_FAILED; |
| 330 } | 330 } |
| 331 | 331 |
| 332 void CastTransportImpl::Start() { | 332 void CastTransportImpl::Start() { |
| 333 DCHECK(CalledOnValidThread()); | 333 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 334 DCHECK(!started_); | 334 DCHECK(!started_); |
| 335 DCHECK_EQ(READ_STATE_READ, read_state_); | 335 DCHECK_EQ(READ_STATE_READ, read_state_); |
| 336 DCHECK(delegate_) << "Read delegate must be set prior to calling Start()"; | 336 DCHECK(delegate_) << "Read delegate must be set prior to calling Start()"; |
| 337 started_ = true; | 337 started_ = true; |
| 338 delegate_->Start(); | 338 delegate_->Start(); |
| 339 SetReadState(READ_STATE_READ); | 339 SetReadState(READ_STATE_READ); |
| 340 | 340 |
| 341 // Start the read state machine. | 341 // Start the read state machine. |
| 342 OnReadResult(net::OK); | 342 OnReadResult(net::OK); |
| 343 } | 343 } |
| 344 | 344 |
| 345 void CastTransportImpl::OnReadResult(int result) { | 345 void CastTransportImpl::OnReadResult(int result) { |
| 346 DCHECK(CalledOnValidThread()); | 346 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 347 // Network operations can either finish synchronously or asynchronously. | 347 // Network operations can either finish synchronously or asynchronously. |
| 348 // This method executes the state machine transitions in a loop so that | 348 // This method executes the state machine transitions in a loop so that |
| 349 // write state transitions happen even when network operations finish | 349 // write state transitions happen even when network operations finish |
| 350 // synchronously. | 350 // synchronously. |
| 351 int rv = result; | 351 int rv = result; |
| 352 do { | 352 do { |
| 353 VLOG_WITH_CONNECTION(2) << "OnReadResult(state=" << read_state_ | 353 VLOG_WITH_CONNECTION(2) << "OnReadResult(state=" << read_state_ |
| 354 << ", result=" << rv << ")"; | 354 << ", result=" << rv << ")"; |
| 355 ReadState state = read_state_; | 355 ReadState state = read_state_; |
| 356 read_state_ = READ_STATE_UNKNOWN; | 356 read_state_ = READ_STATE_UNKNOWN; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 VLOG_WITH_CONNECTION(2) << "DoReadHandleError"; | 444 VLOG_WITH_CONNECTION(2) << "DoReadHandleError"; |
| 445 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); | 445 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); |
| 446 DCHECK_LE(result, 0); | 446 DCHECK_LE(result, 0); |
| 447 SetReadState(READ_STATE_ERROR); | 447 SetReadState(READ_STATE_ERROR); |
| 448 return net::ERR_FAILED; | 448 return net::ERR_FAILED; |
| 449 } | 449 } |
| 450 | 450 |
| 451 } // namespace cast_channel | 451 } // namespace cast_channel |
| 452 } // namespace api | 452 } // namespace api |
| 453 } // namespace extensions | 453 } // namespace extensions |
| OLD | NEW |