Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: extensions/browser/api/cast_channel/cast_transport.cc

Issue 2891923004: [cast_channel] Make cast_channel related files not depend on "cast_channel.h" (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
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 "extensions/browser/api/cast_channel/cast_framer.h"
21 #include "extensions/browser/api/cast_channel/cast_message_util.h" 21 #include "extensions/browser/api/cast_channel/cast_message_util.h"
22 #include "extensions/browser/api/cast_channel/logger.h" 22 #include "extensions/browser/api/cast_channel/logger.h"
23 #include "extensions/common/api/cast_channel/cast_channel.pb.h" 23 #include "extensions/common/api/cast_channel/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=" << channel_auth_ \ 28 VLOG(level) << "[" << ip_endpoint_.ToString() \
29 << "] " 29 << ", auth=" << ::cast_channel::ToString(channel_auth_) << "] "
30 30
31 namespace extensions { 31 namespace extensions {
32 namespace api { 32 namespace api {
33 namespace cast_channel { 33 namespace cast_channel {
34 34
35 CastTransportImpl::CastTransportImpl(net::Socket* socket, 35 CastTransportImpl::CastTransportImpl(net::Socket* socket,
36 int channel_id, 36 int channel_id,
37 const net::IPEndPoint& ip_endpoint, 37 const net::IPEndPoint& ip_endpoint,
38 ChannelAuthType channel_auth, 38 ChannelAuthType channel_auth,
39 scoped_refptr<Logger> logger) 39 scoped_refptr<Logger> logger)
40 : started_(false), 40 : started_(false),
41 socket_(socket), 41 socket_(socket),
42 write_state_(WRITE_STATE_IDLE), 42 write_state_(WRITE_STATE_IDLE),
43 read_state_(READ_STATE_READ), 43 read_state_(READ_STATE_READ),
44 error_state_(CHANNEL_ERROR_NONE), 44 error_state_(ChannelError::NONE),
45 channel_id_(channel_id), 45 channel_id_(channel_id),
46 ip_endpoint_(ip_endpoint), 46 ip_endpoint_(ip_endpoint),
47 channel_auth_(channel_auth), 47 channel_auth_(channel_auth),
48 logger_(logger) { 48 logger_(logger) {
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());
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 return proto::WRITE_STATE_ERROR; 112 return proto::WRITE_STATE_ERROR;
113 default: 113 default:
114 NOTREACHED(); 114 NOTREACHED();
115 return proto::WRITE_STATE_UNKNOWN; 115 return proto::WRITE_STATE_UNKNOWN;
116 } 116 }
117 } 117 }
118 118
119 // static 119 // static
120 proto::ErrorState CastTransportImpl::ErrorStateToProto(ChannelError state) { 120 proto::ErrorState CastTransportImpl::ErrorStateToProto(ChannelError state) {
121 switch (state) { 121 switch (state) {
122 case CHANNEL_ERROR_NONE: 122 case ChannelError::NONE:
123 return proto::CHANNEL_ERROR_NONE; 123 return proto::CHANNEL_ERROR_NONE;
124 case CHANNEL_ERROR_CHANNEL_NOT_OPEN: 124 case ChannelError::CHANNEL_NOT_OPEN:
125 return proto::CHANNEL_ERROR_CHANNEL_NOT_OPEN; 125 return proto::CHANNEL_ERROR_CHANNEL_NOT_OPEN;
126 case CHANNEL_ERROR_AUTHENTICATION_ERROR: 126 case ChannelError::AUTHENTICATION_ERROR:
127 return proto::CHANNEL_ERROR_AUTHENTICATION_ERROR; 127 return proto::CHANNEL_ERROR_AUTHENTICATION_ERROR;
128 case CHANNEL_ERROR_CONNECT_ERROR: 128 case ChannelError::CONNECT_ERROR:
129 return proto::CHANNEL_ERROR_CONNECT_ERROR; 129 return proto::CHANNEL_ERROR_CONNECT_ERROR;
130 case CHANNEL_ERROR_SOCKET_ERROR: 130 case ChannelError::SOCKET_ERROR:
131 return proto::CHANNEL_ERROR_SOCKET_ERROR; 131 return proto::CHANNEL_ERROR_SOCKET_ERROR;
132 case CHANNEL_ERROR_TRANSPORT_ERROR: 132 case ChannelError::TRANSPORT_ERROR:
133 return proto::CHANNEL_ERROR_TRANSPORT_ERROR; 133 return proto::CHANNEL_ERROR_TRANSPORT_ERROR;
134 case CHANNEL_ERROR_INVALID_MESSAGE: 134 case ChannelError::INVALID_MESSAGE:
135 return proto::CHANNEL_ERROR_INVALID_MESSAGE; 135 return proto::CHANNEL_ERROR_INVALID_MESSAGE;
136 case CHANNEL_ERROR_INVALID_CHANNEL_ID: 136 case ChannelError::INVALID_CHANNEL_ID:
137 return proto::CHANNEL_ERROR_INVALID_CHANNEL_ID; 137 return proto::CHANNEL_ERROR_INVALID_CHANNEL_ID;
138 case CHANNEL_ERROR_CONNECT_TIMEOUT: 138 case ChannelError::CONNECT_TIMEOUT:
139 return proto::CHANNEL_ERROR_CONNECT_TIMEOUT; 139 return proto::CHANNEL_ERROR_CONNECT_TIMEOUT;
140 case CHANNEL_ERROR_UNKNOWN: 140 case ChannelError::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(CalledOnValidThread());
150 DCHECK(delegate); 150 DCHECK(delegate);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 if (read_state_ != read_state) 202 if (read_state_ != read_state)
203 read_state_ = read_state; 203 read_state_ = read_state;
204 } 204 }
205 205
206 void CastTransportImpl::SetWriteState(WriteState write_state) { 206 void CastTransportImpl::SetWriteState(WriteState write_state) {
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: "
213 << ::cast_channel::ToString(error_state);
213 error_state_ = error_state; 214 error_state_ = error_state;
214 } 215 }
215 216
216 void CastTransportImpl::OnWriteResult(int result) { 217 void CastTransportImpl::OnWriteResult(int result) {
217 DCHECK(CalledOnValidThread()); 218 DCHECK(CalledOnValidThread());
218 DCHECK_NE(WRITE_STATE_IDLE, write_state_); 219 DCHECK_NE(WRITE_STATE_IDLE, write_state_);
219 if (write_queue_.empty()) { 220 if (write_queue_.empty()) {
220 SetWriteState(WRITE_STATE_IDLE); 221 SetWriteState(WRITE_STATE_IDLE);
221 return; 222 return;
222 } 223 }
(...skipping 20 matching lines...) Expand all
243 case WRITE_STATE_DO_CALLBACK: 244 case WRITE_STATE_DO_CALLBACK:
244 rv = DoWriteCallback(); 245 rv = DoWriteCallback();
245 break; 246 break;
246 case WRITE_STATE_HANDLE_ERROR: 247 case WRITE_STATE_HANDLE_ERROR:
247 rv = DoWriteHandleError(rv); 248 rv = DoWriteHandleError(rv);
248 DCHECK_EQ(WRITE_STATE_ERROR, write_state_); 249 DCHECK_EQ(WRITE_STATE_ERROR, write_state_);
249 break; 250 break;
250 default: 251 default:
251 NOTREACHED() << "Unknown state in write state machine: " << state; 252 NOTREACHED() << "Unknown state in write state machine: " << state;
252 SetWriteState(WRITE_STATE_ERROR); 253 SetWriteState(WRITE_STATE_ERROR);
253 SetErrorState(CHANNEL_ERROR_UNKNOWN); 254 SetErrorState(ChannelError::UNKNOWN);
254 rv = net::ERR_FAILED; 255 rv = net::ERR_FAILED;
255 break; 256 break;
256 } 257 }
257 } while (rv != net::ERR_IO_PENDING && !IsTerminalWriteState(write_state_)); 258 } while (rv != net::ERR_IO_PENDING && !IsTerminalWriteState(write_state_));
258 259
259 if (write_state_ == WRITE_STATE_ERROR) { 260 if (write_state_ == WRITE_STATE_ERROR) {
260 FlushWriteQueue(); 261 FlushWriteQueue();
261 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); 262 DCHECK_NE(ChannelError::NONE, error_state_);
262 VLOG_WITH_CONNECTION(2) << "Sending OnError()."; 263 VLOG_WITH_CONNECTION(2) << "Sending OnError().";
263 delegate_->OnError(error_state_); 264 delegate_->OnError(error_state_);
264 } 265 }
265 } 266 }
266 267
267 int CastTransportImpl::DoWrite() { 268 int CastTransportImpl::DoWrite() {
268 DCHECK(!write_queue_.empty()); 269 DCHECK(!write_queue_.empty());
269 WriteRequest& request = write_queue_.front(); 270 WriteRequest& request = write_queue_.front();
270 271
271 VLOG_WITH_CONNECTION(2) << "WriteData byte_count = " 272 VLOG_WITH_CONNECTION(2) << "WriteData byte_count = "
272 << request.io_buffer->size() << " bytes_written " 273 << request.io_buffer->size() << " bytes_written "
273 << request.io_buffer->BytesConsumed(); 274 << request.io_buffer->BytesConsumed();
274 275
275 SetWriteState(WRITE_STATE_WRITE_COMPLETE); 276 SetWriteState(WRITE_STATE_WRITE_COMPLETE);
276 277
277 int rv = socket_->Write( 278 int rv = socket_->Write(
278 request.io_buffer.get(), request.io_buffer->BytesRemaining(), 279 request.io_buffer.get(), request.io_buffer->BytesRemaining(),
279 base::Bind(&CastTransportImpl::OnWriteResult, base::Unretained(this))); 280 base::Bind(&CastTransportImpl::OnWriteResult, base::Unretained(this)));
280 return rv; 281 return rv;
281 } 282 }
282 283
283 int CastTransportImpl::DoWriteComplete(int result) { 284 int CastTransportImpl::DoWriteComplete(int result) {
284 VLOG_WITH_CONNECTION(2) << "DoWriteComplete result=" << result; 285 VLOG_WITH_CONNECTION(2) << "DoWriteComplete result=" << result;
285 DCHECK(!write_queue_.empty()); 286 DCHECK(!write_queue_.empty());
286 if (result <= 0) { // NOTE that 0 also indicates an error 287 if (result <= 0) { // NOTE that 0 also indicates an error
287 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_WRITE, result); 288 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_WRITE, result);
288 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); 289 SetErrorState(ChannelError::SOCKET_ERROR);
289 SetWriteState(WRITE_STATE_HANDLE_ERROR); 290 SetWriteState(WRITE_STATE_HANDLE_ERROR);
290 return result == 0 ? net::ERR_FAILED : result; 291 return result == 0 ? net::ERR_FAILED : result;
291 } 292 }
292 293
293 // Some bytes were successfully written 294 // Some bytes were successfully written
294 WriteRequest& request = write_queue_.front(); 295 WriteRequest& request = write_queue_.front();
295 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; 296 scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer;
296 io_buffer->DidConsume(result); 297 io_buffer->DidConsume(result);
297 if (io_buffer->BytesRemaining() == 0) { // Message fully sent 298 if (io_buffer->BytesRemaining() == 0) { // Message fully sent
298 SetWriteState(WRITE_STATE_DO_CALLBACK); 299 SetWriteState(WRITE_STATE_DO_CALLBACK);
(...skipping 17 matching lines...) Expand all
316 SetWriteState(WRITE_STATE_IDLE); 317 SetWriteState(WRITE_STATE_IDLE);
317 } else { 318 } else {
318 SetWriteState(WRITE_STATE_WRITE); 319 SetWriteState(WRITE_STATE_WRITE);
319 } 320 }
320 321
321 return net::OK; 322 return net::OK;
322 } 323 }
323 324
324 int CastTransportImpl::DoWriteHandleError(int result) { 325 int CastTransportImpl::DoWriteHandleError(int result) {
325 VLOG_WITH_CONNECTION(2) << "DoWriteHandleError result=" << result; 326 VLOG_WITH_CONNECTION(2) << "DoWriteHandleError result=" << result;
326 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); 327 DCHECK_NE(ChannelError::NONE, error_state_);
327 DCHECK_LT(result, 0); 328 DCHECK_LT(result, 0);
328 SetWriteState(WRITE_STATE_ERROR); 329 SetWriteState(WRITE_STATE_ERROR);
329 return net::ERR_FAILED; 330 return net::ERR_FAILED;
330 } 331 }
331 332
332 void CastTransportImpl::Start() { 333 void CastTransportImpl::Start() {
333 DCHECK(CalledOnValidThread()); 334 DCHECK(CalledOnValidThread());
334 DCHECK(!started_); 335 DCHECK(!started_);
335 DCHECK_EQ(READ_STATE_READ, read_state_); 336 DCHECK_EQ(READ_STATE_READ, read_state_);
336 DCHECK(delegate_) << "Read delegate must be set prior to calling Start()"; 337 DCHECK(delegate_) << "Read delegate must be set prior to calling Start()";
(...skipping 28 matching lines...) Expand all
365 case READ_STATE_DO_CALLBACK: 366 case READ_STATE_DO_CALLBACK:
366 rv = DoReadCallback(); 367 rv = DoReadCallback();
367 break; 368 break;
368 case READ_STATE_HANDLE_ERROR: 369 case READ_STATE_HANDLE_ERROR:
369 rv = DoReadHandleError(rv); 370 rv = DoReadHandleError(rv);
370 DCHECK_EQ(read_state_, READ_STATE_ERROR); 371 DCHECK_EQ(read_state_, READ_STATE_ERROR);
371 break; 372 break;
372 default: 373 default:
373 NOTREACHED() << "Unknown state in read state machine: " << state; 374 NOTREACHED() << "Unknown state in read state machine: " << state;
374 SetReadState(READ_STATE_ERROR); 375 SetReadState(READ_STATE_ERROR);
375 SetErrorState(CHANNEL_ERROR_UNKNOWN); 376 SetErrorState(ChannelError::UNKNOWN);
376 rv = net::ERR_FAILED; 377 rv = net::ERR_FAILED;
377 break; 378 break;
378 } 379 }
379 } while (rv != net::ERR_IO_PENDING && !IsTerminalReadState(read_state_)); 380 } while (rv != net::ERR_IO_PENDING && !IsTerminalReadState(read_state_));
380 381
381 if (IsTerminalReadState(read_state_)) { 382 if (IsTerminalReadState(read_state_)) {
382 DCHECK_EQ(READ_STATE_ERROR, read_state_); 383 DCHECK_EQ(READ_STATE_ERROR, read_state_);
383 VLOG_WITH_CONNECTION(2) << "Sending OnError()."; 384 VLOG_WITH_CONNECTION(2) << "Sending OnError().";
384 delegate_->OnError(error_state_); 385 delegate_->OnError(error_state_);
385 } 386 }
(...skipping 11 matching lines...) Expand all
397 return socket_->Read( 398 return socket_->Read(
398 read_buffer_.get(), base::checked_cast<uint32_t>(num_bytes_to_read), 399 read_buffer_.get(), base::checked_cast<uint32_t>(num_bytes_to_read),
399 base::Bind(&CastTransportImpl::OnReadResult, base::Unretained(this))); 400 base::Bind(&CastTransportImpl::OnReadResult, base::Unretained(this)));
400 } 401 }
401 402
402 int CastTransportImpl::DoReadComplete(int result) { 403 int CastTransportImpl::DoReadComplete(int result) {
403 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result; 404 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result;
404 if (result <= 0) { 405 if (result <= 0) {
405 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_READ, result); 406 logger_->LogSocketEventWithRv(channel_id_, proto::SOCKET_READ, result);
406 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket."; 407 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket.";
407 SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); 408 SetErrorState(ChannelError::SOCKET_ERROR);
408 SetReadState(READ_STATE_HANDLE_ERROR); 409 SetReadState(READ_STATE_HANDLE_ERROR);
409 return result == 0 ? net::ERR_FAILED : result; 410 return result == 0 ? net::ERR_FAILED : result;
410 } 411 }
411 412
412 size_t message_size; 413 size_t message_size;
413 DCHECK(!current_message_); 414 DCHECK(!current_message_);
414 ChannelError framing_error; 415 ChannelError framing_error;
415 current_message_ = framer_->Ingest(result, &message_size, &framing_error); 416 current_message_ = framer_->Ingest(result, &message_size, &framing_error);
416 if (current_message_.get() && (framing_error == CHANNEL_ERROR_NONE)) { 417 if (current_message_.get() && (framing_error == ChannelError::NONE)) {
417 DCHECK_GT(message_size, static_cast<size_t>(0)); 418 DCHECK_GT(message_size, static_cast<size_t>(0));
418 SetReadState(READ_STATE_DO_CALLBACK); 419 SetReadState(READ_STATE_DO_CALLBACK);
419 } else if (framing_error != CHANNEL_ERROR_NONE) { 420 } else if (framing_error != ChannelError::NONE) {
420 DCHECK(!current_message_); 421 DCHECK(!current_message_);
421 SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); 422 SetErrorState(ChannelError::INVALID_MESSAGE);
422 SetReadState(READ_STATE_HANDLE_ERROR); 423 SetReadState(READ_STATE_HANDLE_ERROR);
423 } else { 424 } else {
424 DCHECK(!current_message_); 425 DCHECK(!current_message_);
425 SetReadState(READ_STATE_READ); 426 SetReadState(READ_STATE_READ);
426 } 427 }
427 return net::OK; 428 return net::OK;
428 } 429 }
429 430
430 int CastTransportImpl::DoReadCallback() { 431 int CastTransportImpl::DoReadCallback() {
431 VLOG_WITH_CONNECTION(2) << "DoReadCallback"; 432 VLOG_WITH_CONNECTION(2) << "DoReadCallback";
432 if (!IsCastMessageValid(*current_message_)) { 433 if (!IsCastMessageValid(*current_message_)) {
433 SetReadState(READ_STATE_HANDLE_ERROR); 434 SetReadState(READ_STATE_HANDLE_ERROR);
434 SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); 435 SetErrorState(ChannelError::INVALID_MESSAGE);
435 return net::ERR_INVALID_RESPONSE; 436 return net::ERR_INVALID_RESPONSE;
436 } 437 }
437 SetReadState(READ_STATE_READ); 438 SetReadState(READ_STATE_READ);
438 delegate_->OnMessage(*current_message_); 439 delegate_->OnMessage(*current_message_);
439 current_message_.reset(); 440 current_message_.reset();
440 return net::OK; 441 return net::OK;
441 } 442 }
442 443
443 int CastTransportImpl::DoReadHandleError(int result) { 444 int CastTransportImpl::DoReadHandleError(int result) {
444 VLOG_WITH_CONNECTION(2) << "DoReadHandleError"; 445 VLOG_WITH_CONNECTION(2) << "DoReadHandleError";
445 DCHECK_NE(CHANNEL_ERROR_NONE, error_state_); 446 DCHECK_NE(ChannelError::NONE, error_state_);
446 DCHECK_LE(result, 0); 447 DCHECK_LE(result, 0);
447 SetReadState(READ_STATE_ERROR); 448 SetReadState(READ_STATE_ERROR);
448 return net::ERR_FAILED; 449 return net::ERR_FAILED;
449 } 450 }
450 451
451 } // namespace cast_channel 452 } // namespace cast_channel
452 } // namespace api 453 } // namespace api
453 } // namespace extensions 454 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698