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 "components/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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/numerics/safe_conversions.h" | 16 #include "base/numerics/safe_conversions.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "extensions/browser/api/cast_channel/cast_framer.h" | 20 #include "components/cast_channel/cast_framer.h" |
21 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 21 #include "components/cast_channel/cast_message_util.h" |
22 #include "extensions/browser/api/cast_channel/logger.h" | 22 #include "components/cast_channel/logger.h" |
23 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 23 #include "components/cast_channel/proto/cast_channel.pb.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 #include "net/socket/socket.h" | 25 #include "net/socket/socket.h" |
26 | 26 |
27 #define VLOG_WITH_CONNECTION(level) \ | 27 #define VLOG_WITH_CONNECTION(level) \ |
28 VLOG(level) << "[" << ip_endpoint_.ToString() << ", auth=" \ | 28 VLOG(level) << "[" << ip_endpoint_.ToString() << ", auth=" \ |
29 << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ | 29 << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ |
30 << "] " | 30 << "] " |
31 | 31 |
32 namespace extensions { | |
33 namespace api { | |
34 namespace cast_channel { | 32 namespace cast_channel { |
35 | 33 |
36 CastTransportImpl::CastTransportImpl(net::Socket* socket, | 34 CastTransportImpl::CastTransportImpl(net::Socket* socket, |
37 int channel_id, | 35 int channel_id, |
38 const net::IPEndPoint& ip_endpoint, | 36 const net::IPEndPoint& ip_endpoint, |
39 ChannelAuthType channel_auth, | 37 ChannelAuthType channel_auth, |
40 scoped_refptr<Logger> logger) | 38 scoped_refptr<Logger> logger) |
41 : started_(false), | 39 : started_(false), |
42 socket_(socket), | 40 socket_(socket), |
43 write_state_(WRITE_STATE_IDLE), | 41 write_state_(WRITE_STATE_IDLE), |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 164 |
167 void CastTransportImpl::SendMessage(const CastMessage& message, | 165 void CastTransportImpl::SendMessage(const CastMessage& message, |
168 const net::CompletionCallback& callback) { | 166 const net::CompletionCallback& callback) { |
169 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 167 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
170 std::string serialized_message; | 168 std::string serialized_message; |
171 if (!MessageFramer::Serialize(message, &serialized_message)) { | 169 if (!MessageFramer::Serialize(message, &serialized_message)) { |
172 base::ThreadTaskRunnerHandle::Get()->PostTask( | 170 base::ThreadTaskRunnerHandle::Get()->PostTask( |
173 FROM_HERE, base::Bind(callback, net::ERR_FAILED)); | 171 FROM_HERE, base::Bind(callback, net::ERR_FAILED)); |
174 return; | 172 return; |
175 } | 173 } |
176 WriteRequest write_request( | 174 WriteRequest write_request(message.namespace_(), serialized_message, |
177 message.namespace_(), serialized_message, callback); | 175 callback); |
178 | 176 |
179 write_queue_.push(write_request); | 177 write_queue_.push(write_request); |
180 if (write_state_ == WRITE_STATE_IDLE) { | 178 if (write_state_ == WRITE_STATE_IDLE) { |
181 SetWriteState(WRITE_STATE_WRITE); | 179 SetWriteState(WRITE_STATE_WRITE); |
182 OnWriteResult(net::OK); | 180 OnWriteResult(net::OK); |
183 } | 181 } |
184 } | 182 } |
185 | 183 |
186 CastTransportImpl::WriteRequest::WriteRequest( | 184 CastTransportImpl::WriteRequest::WriteRequest( |
187 const std::string& namespace_, | 185 const std::string& namespace_, |
188 const std::string& payload, | 186 const std::string& payload, |
189 const net::CompletionCallback& callback) | 187 const net::CompletionCallback& callback) |
190 : message_namespace(namespace_), callback(callback) { | 188 : message_namespace(namespace_), callback(callback) { |
191 VLOG(2) << "WriteRequest size: " << payload.size(); | 189 VLOG(2) << "WriteRequest size: " << payload.size(); |
192 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(payload), | 190 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(payload), |
193 payload.size()); | 191 payload.size()); |
194 } | 192 } |
195 | 193 |
196 CastTransportImpl::WriteRequest::WriteRequest(const WriteRequest& other) = | 194 CastTransportImpl::WriteRequest::WriteRequest(const WriteRequest& other) = |
197 default; | 195 default; |
198 | 196 |
199 CastTransportImpl::WriteRequest::~WriteRequest() { | 197 CastTransportImpl::WriteRequest::~WriteRequest() {} |
200 } | |
201 | 198 |
202 void CastTransportImpl::SetReadState(ReadState read_state) { | 199 void CastTransportImpl::SetReadState(ReadState read_state) { |
203 if (read_state_ != read_state) | 200 if (read_state_ != read_state) |
204 read_state_ = read_state; | 201 read_state_ = read_state; |
205 } | 202 } |
206 | 203 |
207 void CastTransportImpl::SetWriteState(WriteState write_state) { | 204 void CastTransportImpl::SetWriteState(WriteState write_state) { |
208 if (write_state_ != write_state) | 205 if (write_state_ != write_state) |
209 write_state_ = write_state; | 206 write_state_ = write_state; |
210 } | 207 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 } | 342 } |
346 | 343 |
347 void CastTransportImpl::OnReadResult(int result) { | 344 void CastTransportImpl::OnReadResult(int result) { |
348 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 345 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
349 // Network operations can either finish synchronously or asynchronously. | 346 // Network operations can either finish synchronously or asynchronously. |
350 // This method executes the state machine transitions in a loop so that | 347 // This method executes the state machine transitions in a loop so that |
351 // write state transitions happen even when network operations finish | 348 // write state transitions happen even when network operations finish |
352 // synchronously. | 349 // synchronously. |
353 int rv = result; | 350 int rv = result; |
354 do { | 351 do { |
355 VLOG_WITH_CONNECTION(2) << "OnReadResult(state=" << read_state_ | 352 VLOG_WITH_CONNECTION(2) |
356 << ", result=" << rv << ")"; | 353 << "OnReadResult(state=" << read_state_ << ", result=" << rv << ")"; |
357 ReadState state = read_state_; | 354 ReadState state = read_state_; |
358 read_state_ = READ_STATE_UNKNOWN; | 355 read_state_ = READ_STATE_UNKNOWN; |
359 | 356 |
360 switch (state) { | 357 switch (state) { |
361 case READ_STATE_READ: | 358 case READ_STATE_READ: |
362 rv = DoRead(); | 359 rv = DoRead(); |
363 break; | 360 break; |
364 case READ_STATE_READ_COMPLETE: | 361 case READ_STATE_READ_COMPLETE: |
365 rv = DoReadComplete(rv); | 362 rv = DoReadComplete(rv); |
366 break; | 363 break; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 | 441 |
445 int CastTransportImpl::DoReadHandleError(int result) { | 442 int CastTransportImpl::DoReadHandleError(int result) { |
446 VLOG_WITH_CONNECTION(2) << "DoReadHandleError"; | 443 VLOG_WITH_CONNECTION(2) << "DoReadHandleError"; |
447 DCHECK_NE(ChannelError::NONE, error_state_); | 444 DCHECK_NE(ChannelError::NONE, error_state_); |
448 DCHECK_LE(result, 0); | 445 DCHECK_LE(result, 0); |
449 SetReadState(READ_STATE_ERROR); | 446 SetReadState(READ_STATE_ERROR); |
450 return net::ERR_FAILED; | 447 return net::ERR_FAILED; |
451 } | 448 } |
452 | 449 |
453 } // namespace cast_channel | 450 } // namespace cast_channel |
454 } // namespace api | |
455 } // namespace extensions | |
OLD | NEW |