Commit f1624ddc authored by kmarshall's avatar kmarshall Committed by Commit bot

Make protobuf enums canonical for ReadState/WriteState/ConnectState.

Delete redundant non-proto ReadState/WriteState/ConnectState enums.
Add cast protogen rule to extension registration deps.

BUG=

Review URL: https://codereview.chromium.org/576483003

Cr-Commit-Position: refs/heads/master@{#295602}
parent eaa3d4fa
...@@ -427,7 +427,7 @@ source_set("browser") { ...@@ -427,7 +427,7 @@ source_set("browser") {
"//device/hid", "//device/hid",
"//device/serial", "//device/serial",
"//device/usb", "//device/usb",
"//extensions/browser/api/cast_channel:cast_channel_proto", "//extensions/common/api/cast_channel:cast_channel_proto",
] ]
if (use_openssl) { if (use_openssl) {
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
['enable_extensions==1', { ['enable_extensions==1', {
'dependencies': [ 'dependencies': [
'<(DEPTH)/device/serial/serial.gyp:device_serial_mojo', '<(DEPTH)/device/serial/serial.gyp:device_serial_mojo',
'<(DEPTH)/extensions/common/api/api.gyp:cast_channel_proto',
'<(DEPTH)/skia/skia.gyp:skia', '<(DEPTH)/skia/skia.gyp:skia',
], ],
}], }],
......
...@@ -66,117 +66,6 @@ ApiResourceManager<core_api::cast_channel::CastSocket>::GetFactoryInstance() { ...@@ -66,117 +66,6 @@ ApiResourceManager<core_api::cast_channel::CastSocket>::GetFactoryInstance() {
namespace core_api { namespace core_api {
namespace cast_channel { namespace cast_channel {
namespace {
proto::ReadyState ReadyStateToProto(ReadyState state) {
switch (state) {
case READY_STATE_NONE:
return proto::READY_STATE_NONE;
case READY_STATE_CONNECTING:
return proto::READY_STATE_CONNECTING;
case READY_STATE_OPEN:
return proto::READY_STATE_OPEN;
case READY_STATE_CLOSING:
return proto::READY_STATE_CLOSING;
case READY_STATE_CLOSED:
return proto::READY_STATE_CLOSED;
default:
NOTREACHED();
return proto::READY_STATE_NONE;
}
}
proto::ConnectionState ConnectStateToProto(CastSocket::ConnectionState state) {
switch (state) {
case CastSocket::CONN_STATE_NONE:
return proto::CONN_STATE_NONE;
case CastSocket::CONN_STATE_TCP_CONNECT:
return proto::CONN_STATE_TCP_CONNECT;
case CastSocket::CONN_STATE_TCP_CONNECT_COMPLETE:
return proto::CONN_STATE_TCP_CONNECT_COMPLETE;
case CastSocket::CONN_STATE_SSL_CONNECT:
return proto::CONN_STATE_SSL_CONNECT;
case CastSocket::CONN_STATE_SSL_CONNECT_COMPLETE:
return proto::CONN_STATE_SSL_CONNECT_COMPLETE;
case CastSocket::CONN_STATE_AUTH_CHALLENGE_SEND:
return proto::CONN_STATE_AUTH_CHALLENGE_SEND;
case CastSocket::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE:
return proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE;
case CastSocket::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE:
return proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE;
default:
NOTREACHED();
return proto::CONN_STATE_NONE;
}
}
proto::ReadState ReadStateToProto(CastSocket::ReadState state) {
switch (state) {
case CastSocket::READ_STATE_NONE:
return proto::READ_STATE_NONE;
case CastSocket::READ_STATE_READ:
return proto::READ_STATE_READ;
case CastSocket::READ_STATE_READ_COMPLETE:
return proto::READ_STATE_READ_COMPLETE;
case CastSocket::READ_STATE_DO_CALLBACK:
return proto::READ_STATE_DO_CALLBACK;
case CastSocket::READ_STATE_ERROR:
return proto::READ_STATE_ERROR;
default:
NOTREACHED();
return proto::READ_STATE_NONE;
}
}
proto::WriteState WriteStateToProto(CastSocket::WriteState state) {
switch (state) {
case CastSocket::WRITE_STATE_NONE:
return proto::WRITE_STATE_NONE;
case CastSocket::WRITE_STATE_WRITE:
return proto::WRITE_STATE_WRITE;
case CastSocket::WRITE_STATE_WRITE_COMPLETE:
return proto::WRITE_STATE_WRITE_COMPLETE;
case CastSocket::WRITE_STATE_DO_CALLBACK:
return proto::WRITE_STATE_DO_CALLBACK;
case CastSocket::WRITE_STATE_ERROR:
return proto::WRITE_STATE_ERROR;
default:
NOTREACHED();
return proto::WRITE_STATE_NONE;
}
}
proto::ErrorState ErrorStateToProto(ChannelError state) {
switch (state) {
case CHANNEL_ERROR_NONE:
return proto::CHANNEL_ERROR_NONE;
case CHANNEL_ERROR_CHANNEL_NOT_OPEN:
return proto::CHANNEL_ERROR_CHANNEL_NOT_OPEN;
case CHANNEL_ERROR_AUTHENTICATION_ERROR:
return proto::CHANNEL_ERROR_AUTHENTICATION_ERROR;
case CHANNEL_ERROR_CONNECT_ERROR:
return proto::CHANNEL_ERROR_CONNECT_ERROR;
case CHANNEL_ERROR_SOCKET_ERROR:
return proto::CHANNEL_ERROR_SOCKET_ERROR;
case CHANNEL_ERROR_TRANSPORT_ERROR:
return proto::CHANNEL_ERROR_TRANSPORT_ERROR;
case CHANNEL_ERROR_INVALID_MESSAGE:
return proto::CHANNEL_ERROR_INVALID_MESSAGE;
case CHANNEL_ERROR_INVALID_CHANNEL_ID:
return proto::CHANNEL_ERROR_INVALID_CHANNEL_ID;
case CHANNEL_ERROR_CONNECT_TIMEOUT:
return proto::CHANNEL_ERROR_CONNECT_TIMEOUT;
case CHANNEL_ERROR_UNKNOWN:
return proto::CHANNEL_ERROR_UNKNOWN;
default:
NOTREACHED();
return proto::CHANNEL_ERROR_NONE;
}
}
} // namespace
CastSocket::CastSocket(const std::string& owner_extension_id, CastSocket::CastSocket(const std::string& owner_extension_id,
const net::IPEndPoint& ip_endpoint, const net::IPEndPoint& ip_endpoint,
ChannelAuthType channel_auth, ChannelAuthType channel_auth,
...@@ -194,9 +83,9 @@ CastSocket::CastSocket(const std::string& owner_extension_id, ...@@ -194,9 +83,9 @@ CastSocket::CastSocket(const std::string& owner_extension_id,
connect_timeout_(timeout), connect_timeout_(timeout),
connect_timeout_timer_(new base::OneShotTimer<CastSocket>), connect_timeout_timer_(new base::OneShotTimer<CastSocket>),
is_canceled_(false), is_canceled_(false),
connect_state_(CONN_STATE_NONE), connect_state_(proto::CONN_STATE_NONE),
write_state_(WRITE_STATE_NONE), write_state_(proto::WRITE_STATE_NONE),
read_state_(READ_STATE_NONE), read_state_(proto::READ_STATE_NONE),
error_state_(CHANNEL_ERROR_NONE), error_state_(CHANNEL_ERROR_NONE),
ready_state_(READY_STATE_NONE) { ready_state_(READY_STATE_NONE) {
DCHECK(net_log_); DCHECK(net_log_);
...@@ -304,7 +193,7 @@ void CastSocket::Connect(const net::CompletionCallback& callback) { ...@@ -304,7 +193,7 @@ void CastSocket::Connect(const net::CompletionCallback& callback) {
connect_callback_ = callback; connect_callback_ = callback;
SetReadyState(READY_STATE_CONNECTING); SetReadyState(READY_STATE_CONNECTING);
SetConnectState(CONN_STATE_TCP_CONNECT); SetConnectState(proto::CONN_STATE_TCP_CONNECT);
if (connect_timeout_.InMicroseconds() > 0) { if (connect_timeout_.InMicroseconds() > 0) {
DCHECK(connect_timeout_callback_.IsCancelled()); DCHECK(connect_timeout_callback_.IsCancelled());
...@@ -352,47 +241,46 @@ void CastSocket::DoConnectLoop(int result) { ...@@ -352,47 +241,46 @@ void CastSocket::DoConnectLoop(int result) {
// synchronously. // synchronously.
int rv = result; int rv = result;
do { do {
ConnectionState state = connect_state_; proto::ConnectionState state = connect_state_;
// Default to CONN_STATE_NONE, which breaks the processing loop if any // Default to CONN_STATE_NONE, which breaks the processing loop if any
// handler fails to transition to another state to continue processing. // handler fails to transition to another state to continue processing.
connect_state_ = CONN_STATE_NONE; connect_state_ = proto::CONN_STATE_NONE;
switch (state) { switch (state) {
case CONN_STATE_TCP_CONNECT: case proto::CONN_STATE_TCP_CONNECT:
rv = DoTcpConnect(); rv = DoTcpConnect();
break; break;
case CONN_STATE_TCP_CONNECT_COMPLETE: case proto::CONN_STATE_TCP_CONNECT_COMPLETE:
rv = DoTcpConnectComplete(rv); rv = DoTcpConnectComplete(rv);
break; break;
case CONN_STATE_SSL_CONNECT: case proto::CONN_STATE_SSL_CONNECT:
DCHECK_EQ(net::OK, rv); DCHECK_EQ(net::OK, rv);
rv = DoSslConnect(); rv = DoSslConnect();
break; break;
case CONN_STATE_SSL_CONNECT_COMPLETE: case proto::CONN_STATE_SSL_CONNECT_COMPLETE:
rv = DoSslConnectComplete(rv); rv = DoSslConnectComplete(rv);
break; break;
case CONN_STATE_AUTH_CHALLENGE_SEND: case proto::CONN_STATE_AUTH_CHALLENGE_SEND:
rv = DoAuthChallengeSend(); rv = DoAuthChallengeSend();
break; break;
case CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE:
rv = DoAuthChallengeSendComplete(rv); rv = DoAuthChallengeSendComplete(rv);
break; break;
case CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE:
rv = DoAuthChallengeReplyComplete(rv); rv = DoAuthChallengeReplyComplete(rv);
break; break;
default: default:
NOTREACHED() << "BUG in connect flow. Unknown state: " << state; NOTREACHED() << "BUG in connect flow. Unknown state: " << state;
break; break;
} }
} while (rv != net::ERR_IO_PENDING && connect_state_ != CONN_STATE_NONE); } while (rv != net::ERR_IO_PENDING &&
// Get out of the loop either when: connect_state_ != proto::CONN_STATE_NONE);
// a. A network operation is pending, OR // Get out of the loop either when: // a. A network operation is pending, OR
// b. The Do* method called did not change state // b. The Do* method called did not change state
// No state change occurred in do-while loop above. This means state has // No state change occurred in do-while loop above. This means state has
// transitioned to NONE. // transitioned to NONE.
if (connect_state_ == CONN_STATE_NONE) { if (connect_state_ == proto::CONN_STATE_NONE) {
logger_->LogSocketConnectState(channel_id_, logger_->LogSocketConnectState(channel_id_, connect_state_);
ConnectStateToProto(connect_state_));
} }
// Connect loop is finished: if there is no pending IO invoke the callback. // Connect loop is finished: if there is no pending IO invoke the callback.
...@@ -405,7 +293,7 @@ void CastSocket::DoConnectLoop(int result) { ...@@ -405,7 +293,7 @@ void CastSocket::DoConnectLoop(int result) {
int CastSocket::DoTcpConnect() { int CastSocket::DoTcpConnect() {
DCHECK(connect_loop_callback_.IsCancelled()); DCHECK(connect_loop_callback_.IsCancelled());
VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; VLOG_WITH_CONNECTION(1) << "DoTcpConnect";
SetConnectState(CONN_STATE_TCP_CONNECT_COMPLETE); SetConnectState(proto::CONN_STATE_TCP_CONNECT_COMPLETE);
tcp_socket_ = CreateTcpSocket(); tcp_socket_ = CreateTcpSocket();
int rv = tcp_socket_->Connect( int rv = tcp_socket_->Connect(
...@@ -422,7 +310,7 @@ int CastSocket::DoTcpConnectComplete(int result) { ...@@ -422,7 +310,7 @@ int CastSocket::DoTcpConnectComplete(int result) {
LOG_IF(WARNING, !result) << "Failed to SetKeepAlive."; LOG_IF(WARNING, !result) << "Failed to SetKeepAlive.";
logger_->LogSocketEventWithRv( logger_->LogSocketEventWithRv(
channel_id_, proto::TCP_SOCKET_SET_KEEP_ALIVE, result ? 1 : 0); channel_id_, proto::TCP_SOCKET_SET_KEEP_ALIVE, result ? 1 : 0);
SetConnectState(CONN_STATE_SSL_CONNECT); SetConnectState(proto::CONN_STATE_SSL_CONNECT);
} }
return result; return result;
} }
...@@ -430,7 +318,7 @@ int CastSocket::DoTcpConnectComplete(int result) { ...@@ -430,7 +318,7 @@ int CastSocket::DoTcpConnectComplete(int result) {
int CastSocket::DoSslConnect() { int CastSocket::DoSslConnect() {
DCHECK(connect_loop_callback_.IsCancelled()); DCHECK(connect_loop_callback_.IsCancelled());
VLOG_WITH_CONNECTION(1) << "DoSslConnect"; VLOG_WITH_CONNECTION(1) << "DoSslConnect";
SetConnectState(CONN_STATE_SSL_CONNECT_COMPLETE); SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE);
socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>()); socket_ = CreateSslSocket(tcp_socket_.PassAs<net::StreamSocket>());
int rv = socket_->Connect( int rv = socket_->Connect(
...@@ -443,17 +331,17 @@ int CastSocket::DoSslConnectComplete(int result) { ...@@ -443,17 +331,17 @@ int CastSocket::DoSslConnectComplete(int result) {
VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result;
if (result == net::ERR_CERT_AUTHORITY_INVALID && if (result == net::ERR_CERT_AUTHORITY_INVALID &&
peer_cert_.empty() && ExtractPeerCert(&peer_cert_)) { peer_cert_.empty() && ExtractPeerCert(&peer_cert_)) {
SetConnectState(CONN_STATE_TCP_CONNECT); SetConnectState(proto::CONN_STATE_TCP_CONNECT);
} else if (result == net::OK && } else if (result == net::OK &&
channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) { channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) {
SetConnectState(CONN_STATE_AUTH_CHALLENGE_SEND); SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND);
} }
return result; return result;
} }
int CastSocket::DoAuthChallengeSend() { int CastSocket::DoAuthChallengeSend() {
VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend";
SetConnectState(CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE);
CastMessage challenge_message; CastMessage challenge_message;
CreateAuthChallengeMessage(&challenge_message); CreateAuthChallengeMessage(&challenge_message);
...@@ -489,7 +377,7 @@ int CastSocket::DoAuthChallengeSendComplete(int result) { ...@@ -489,7 +377,7 @@ int CastSocket::DoAuthChallengeSendComplete(int result) {
if (result < 0) { if (result < 0) {
return result; return result;
} }
SetConnectState(CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE);
// Post a task to start read loop so that DoReadLoop is not nested inside // Post a task to start read loop so that DoReadLoop is not nested inside
// DoConnectLoop. This is not strictly necessary but keeps the read loop // DoConnectLoop. This is not strictly necessary but keeps the read loop
...@@ -615,8 +503,8 @@ void CastSocket::SendCastMessageInternal( ...@@ -615,8 +503,8 @@ void CastSocket::SendCastMessageInternal(
proto::MESSAGE_ENQUEUED, proto::MESSAGE_ENQUEUED,
message.namespace_(), message.namespace_(),
base::StringPrintf("Queue size: %" PRIuS, write_queue_.size())); base::StringPrintf("Queue size: %" PRIuS, write_queue_.size()));
if (write_state_ == WRITE_STATE_NONE) { if (write_state_ == proto::WRITE_STATE_NONE) {
SetWriteState(WRITE_STATE_WRITE); SetWriteState(proto::WRITE_STATE_WRITE);
DoWriteLoop(net::OK); DoWriteLoop(net::OK);
} }
} }
...@@ -626,7 +514,7 @@ void CastSocket::DoWriteLoop(int result) { ...@@ -626,7 +514,7 @@ void CastSocket::DoWriteLoop(int result) {
VLOG_WITH_CONNECTION(1) << "DoWriteLoop queue size: " << write_queue_.size(); VLOG_WITH_CONNECTION(1) << "DoWriteLoop queue size: " << write_queue_.size();
if (write_queue_.empty()) { if (write_queue_.empty()) {
SetWriteState(WRITE_STATE_NONE); SetWriteState(proto::WRITE_STATE_NONE);
return; return;
} }
...@@ -636,39 +524,38 @@ void CastSocket::DoWriteLoop(int result) { ...@@ -636,39 +524,38 @@ void CastSocket::DoWriteLoop(int result) {
// synchronously. // synchronously.
int rv = result; int rv = result;
do { do {
WriteState state = write_state_; proto::WriteState state = write_state_;
write_state_ = WRITE_STATE_NONE; write_state_ = proto::WRITE_STATE_NONE;
switch (state) { switch (state) {
case WRITE_STATE_WRITE: case proto::WRITE_STATE_WRITE:
rv = DoWrite(); rv = DoWrite();
break; break;
case WRITE_STATE_WRITE_COMPLETE: case proto::WRITE_STATE_WRITE_COMPLETE:
rv = DoWriteComplete(rv); rv = DoWriteComplete(rv);
break; break;
case WRITE_STATE_DO_CALLBACK: case proto::WRITE_STATE_DO_CALLBACK:
rv = DoWriteCallback(); rv = DoWriteCallback();
break; break;
case WRITE_STATE_ERROR: case proto::WRITE_STATE_ERROR:
rv = DoWriteError(rv); rv = DoWriteError(rv);
break; break;
default: default:
NOTREACHED() << "BUG in write flow. Unknown state: " << state; NOTREACHED() << "BUG in write flow. Unknown state: " << state;
break; break;
} }
} while (!write_queue_.empty() && } while (!write_queue_.empty() && rv != net::ERR_IO_PENDING &&
rv != net::ERR_IO_PENDING && write_state_ != proto::WRITE_STATE_NONE);
write_state_ != WRITE_STATE_NONE);
// No state change occurred in do-while loop above. This means state has // No state change occurred in do-while loop above. This means state has
// transitioned to NONE. // transitioned to NONE.
if (write_state_ == WRITE_STATE_NONE) { if (write_state_ == proto::WRITE_STATE_NONE) {
logger_->LogSocketWriteState(channel_id_, WriteStateToProto(write_state_)); logger_->LogSocketWriteState(channel_id_, write_state_);
} }
// If write loop is done because the queue is empty then set write // If write loop is done because the queue is empty then set write
// state to NONE // state to NONE
if (write_queue_.empty()) { if (write_queue_.empty()) {
SetWriteState(WRITE_STATE_NONE); SetWriteState(proto::WRITE_STATE_NONE);
} }
// Write loop is done - if the result is ERR_FAILED then close with error. // Write loop is done - if the result is ERR_FAILED then close with error.
...@@ -685,7 +572,7 @@ int CastSocket::DoWrite() { ...@@ -685,7 +572,7 @@ int CastSocket::DoWrite() {
<< request.io_buffer->size() << " bytes_written " << request.io_buffer->size() << " bytes_written "
<< request.io_buffer->BytesConsumed(); << request.io_buffer->BytesConsumed();
SetWriteState(WRITE_STATE_WRITE_COMPLETE); SetWriteState(proto::WRITE_STATE_WRITE_COMPLETE);
int rv = socket_->Write( int rv = socket_->Write(
request.io_buffer.get(), request.io_buffer.get(),
...@@ -700,7 +587,7 @@ int CastSocket::DoWriteComplete(int result) { ...@@ -700,7 +587,7 @@ int CastSocket::DoWriteComplete(int result) {
DCHECK(!write_queue_.empty()); DCHECK(!write_queue_.empty());
if (result <= 0) { // NOTE that 0 also indicates an error if (result <= 0) { // NOTE that 0 also indicates an error
SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); SetErrorState(CHANNEL_ERROR_SOCKET_ERROR);
SetWriteState(WRITE_STATE_ERROR); SetWriteState(proto::WRITE_STATE_ERROR);
return result == 0 ? net::ERR_FAILED : result; return result == 0 ? net::ERR_FAILED : result;
} }
...@@ -709,9 +596,9 @@ int CastSocket::DoWriteComplete(int result) { ...@@ -709,9 +596,9 @@ int CastSocket::DoWriteComplete(int result) {
scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer; scoped_refptr<net::DrainableIOBuffer> io_buffer = request.io_buffer;
io_buffer->DidConsume(result); io_buffer->DidConsume(result);
if (io_buffer->BytesRemaining() == 0) { // Message fully sent if (io_buffer->BytesRemaining() == 0) { // Message fully sent
SetWriteState(WRITE_STATE_DO_CALLBACK); SetWriteState(proto::WRITE_STATE_DO_CALLBACK);
} else { } else {
SetWriteState(WRITE_STATE_WRITE); SetWriteState(proto::WRITE_STATE_WRITE);
} }
return net::OK; return net::OK;
...@@ -720,7 +607,7 @@ int CastSocket::DoWriteComplete(int result) { ...@@ -720,7 +607,7 @@ int CastSocket::DoWriteComplete(int result) {
int CastSocket::DoWriteCallback() { int CastSocket::DoWriteCallback() {
DCHECK(!write_queue_.empty()); DCHECK(!write_queue_.empty());
SetWriteState(WRITE_STATE_WRITE); SetWriteState(proto::WRITE_STATE_WRITE);
WriteRequest& request = write_queue_.front(); WriteRequest& request = write_queue_.front();
int bytes_consumed = request.io_buffer->BytesConsumed(); int bytes_consumed = request.io_buffer->BytesConsumed();
...@@ -769,8 +656,8 @@ void CastSocket::PostTaskToStartReadLoop() { ...@@ -769,8 +656,8 @@ void CastSocket::PostTaskToStartReadLoop() {
void CastSocket::StartReadLoop() { void CastSocket::StartReadLoop() {
read_loop_callback_.Cancel(); read_loop_callback_.Cancel();
// Read loop would have already been started if read state is not NONE // Read loop would have already been started if read state is not NONE
if (read_state_ == READ_STATE_NONE) { if (read_state_ == proto::READ_STATE_NONE) {
SetReadState(READ_STATE_READ); SetReadState(proto::READ_STATE_READ);
DoReadLoop(net::OK); DoReadLoop(net::OK);
} }
} }
...@@ -783,33 +670,33 @@ void CastSocket::DoReadLoop(int result) { ...@@ -783,33 +670,33 @@ void CastSocket::DoReadLoop(int result) {
// synchronously. // synchronously.
int rv = result; int rv = result;
do { do {
ReadState state = read_state_; proto::ReadState state = read_state_;
read_state_ = READ_STATE_NONE; read_state_ = proto::READ_STATE_NONE;
switch (state) { switch (state) {
case READ_STATE_READ: case proto::READ_STATE_READ:
rv = DoRead(); rv = DoRead();
break; break;
case READ_STATE_READ_COMPLETE: case proto::READ_STATE_READ_COMPLETE:
rv = DoReadComplete(rv); rv = DoReadComplete(rv);
break; break;
case READ_STATE_DO_CALLBACK: case proto::READ_STATE_DO_CALLBACK:
rv = DoReadCallback(); rv = DoReadCallback();
break; break;
case READ_STATE_ERROR: case proto::READ_STATE_ERROR:
rv = DoReadError(rv); rv = DoReadError(rv);
DCHECK_EQ(read_state_, READ_STATE_NONE); DCHECK_EQ(read_state_, proto::READ_STATE_NONE);
break; break;
default: default:
NOTREACHED() << "BUG in read flow. Unknown state: " << state; NOTREACHED() << "BUG in read flow. Unknown state: " << state;
break; break;
} }
} while (rv != net::ERR_IO_PENDING && read_state_ != READ_STATE_NONE); } while (rv != net::ERR_IO_PENDING && read_state_ != proto::READ_STATE_NONE);
// No state change occurred in do-while loop above. This means state has // No state change occurred in do-while loop above. This means state has
// transitioned to NONE. // transitioned to NONE.
if (read_state_ == READ_STATE_NONE) { if (read_state_ == proto::READ_STATE_NONE) {
logger_->LogSocketReadState(channel_id_, ReadStateToProto(read_state_)); logger_->LogSocketReadState(channel_id_, read_state_);
} }
if (rv == net::ERR_FAILED) { if (rv == net::ERR_FAILED) {
...@@ -827,7 +714,7 @@ void CastSocket::DoReadLoop(int result) { ...@@ -827,7 +714,7 @@ void CastSocket::DoReadLoop(int result) {
} }
int CastSocket::DoRead() { int CastSocket::DoRead() {
SetReadState(READ_STATE_READ_COMPLETE); SetReadState(proto::READ_STATE_READ_COMPLETE);
// Determine how many bytes need to be read. // Determine how many bytes need to be read.
size_t num_bytes_to_read = framer_->BytesRequested(); size_t num_bytes_to_read = framer_->BytesRequested();
...@@ -848,7 +735,7 @@ int CastSocket::DoReadComplete(int result) { ...@@ -848,7 +735,7 @@ int CastSocket::DoReadComplete(int result) {
if (result <= 0) { // 0 means EOF: the peer closed the socket if (result <= 0) { // 0 means EOF: the peer closed the socket
VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket"; VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket";
SetErrorState(CHANNEL_ERROR_SOCKET_ERROR); SetErrorState(CHANNEL_ERROR_SOCKET_ERROR);
SetReadState(READ_STATE_ERROR); SetReadState(proto::READ_STATE_ERROR);
return result == 0 ? net::ERR_FAILED : result; return result == 0 ? net::ERR_FAILED : result;
} }
...@@ -864,19 +751,19 @@ int CastSocket::DoReadComplete(int result) { ...@@ -864,19 +751,19 @@ int CastSocket::DoReadComplete(int result) {
current_message_->namespace_(), current_message_->namespace_(),
base::StringPrintf("Message size: %u", base::StringPrintf("Message size: %u",
static_cast<uint32>(message_size))); static_cast<uint32>(message_size)));
SetReadState(READ_STATE_DO_CALLBACK); SetReadState(proto::READ_STATE_DO_CALLBACK);
} else if (error_state_ != CHANNEL_ERROR_NONE) { } else if (error_state_ != CHANNEL_ERROR_NONE) {
DCHECK(current_message_.get() == NULL); DCHECK(current_message_.get() == NULL);
SetReadState(READ_STATE_ERROR); SetReadState(proto::READ_STATE_ERROR);
} else { } else {
DCHECK(current_message_.get() == NULL); DCHECK(current_message_.get() == NULL);
SetReadState(READ_STATE_READ); SetReadState(proto::READ_STATE_READ);
} }
return net::OK; return net::OK;
} }
int CastSocket::DoReadCallback() { int CastSocket::DoReadCallback() {
SetReadState(READ_STATE_READ); SetReadState(proto::READ_STATE_READ);
const CastMessage& message = *current_message_; const CastMessage& message = *current_message_;
if (ready_state_ == READY_STATE_CONNECTING) { if (ready_state_ == READY_STATE_CONNECTING) {
if (IsAuthMessage(message)) { if (IsAuthMessage(message)) {
...@@ -886,7 +773,7 @@ int CastSocket::DoReadCallback() { ...@@ -886,7 +773,7 @@ int CastSocket::DoReadCallback() {
current_message_.reset(); current_message_.reset();
return net::OK; return net::OK;
} else { } else {
SetReadState(READ_STATE_ERROR); SetReadState(proto::READ_STATE_ERROR);
SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE);
current_message_.reset(); current_message_.reset();
return net::ERR_INVALID_RESPONSE; return net::ERR_INVALID_RESPONSE;
...@@ -896,7 +783,7 @@ int CastSocket::DoReadCallback() { ...@@ -896,7 +783,7 @@ int CastSocket::DoReadCallback() {
MessageInfo message_info; MessageInfo message_info;
if (!CastMessageToMessageInfo(message, &message_info)) { if (!CastMessageToMessageInfo(message, &message_info)) {
current_message_.reset(); current_message_.reset();
SetReadState(READ_STATE_ERROR); SetReadState(proto::READ_STATE_ERROR);
SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE); SetErrorState(CHANNEL_ERROR_INVALID_MESSAGE);
return net::ERR_INVALID_RESPONSE; return net::ERR_INVALID_RESPONSE;
} }
...@@ -939,11 +826,10 @@ base::Timer* CastSocket::GetTimer() { ...@@ -939,11 +826,10 @@ base::Timer* CastSocket::GetTimer() {
return connect_timeout_timer_.get(); return connect_timeout_timer_.get();
} }
void CastSocket::SetConnectState(ConnectionState connect_state) { void CastSocket::SetConnectState(proto::ConnectionState connect_state) {
if (connect_state_ != connect_state) { if (connect_state_ != connect_state) {
connect_state_ = connect_state; connect_state_ = connect_state;
logger_->LogSocketConnectState(channel_id_, logger_->LogSocketConnectState(channel_id_, connect_state_);
ConnectStateToProto(connect_state_));
} }
} }
...@@ -961,17 +847,17 @@ void CastSocket::SetErrorState(ChannelError error_state) { ...@@ -961,17 +847,17 @@ void CastSocket::SetErrorState(ChannelError error_state) {
} }
} }
void CastSocket::SetReadState(ReadState read_state) { void CastSocket::SetReadState(proto::ReadState read_state) {
if (read_state_ != read_state) { if (read_state_ != read_state) {
read_state_ = read_state; read_state_ = read_state;
logger_->LogSocketReadState(channel_id_, ReadStateToProto(read_state_)); logger_->LogSocketReadState(channel_id_, read_state_);
} }
} }
void CastSocket::SetWriteState(WriteState write_state) { void CastSocket::SetWriteState(proto::WriteState write_state) {
if (write_state_ != write_state) { if (write_state_ != write_state) {
write_state_ = write_state; write_state_ = write_state;
logger_->LogSocketWriteState(channel_id_, WriteStateToProto(write_state_)); logger_->LogSocketWriteState(channel_id_, write_state_);
} }
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "extensions/browser/api/api_resource.h" #include "extensions/browser/api/api_resource.h"
#include "extensions/browser/api/api_resource_manager.h" #include "extensions/browser/api/api_resource_manager.h"
#include "extensions/browser/api/cast_channel/logging.pb.h"
#include "extensions/common/api/cast_channel.h" #include "extensions/common/api/cast_channel.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
...@@ -126,36 +127,6 @@ class CastSocket : public ApiResource { ...@@ -126,36 +127,6 @@ class CastSocket : public ApiResource {
// It is fine to delete the CastSocket object in |callback|. // It is fine to delete the CastSocket object in |callback|.
virtual void Close(const net::CompletionCallback& callback); virtual void Close(const net::CompletionCallback& callback);
// Internal connection states.
enum ConnectionState {
CONN_STATE_NONE,
CONN_STATE_TCP_CONNECT,
CONN_STATE_TCP_CONNECT_COMPLETE,
CONN_STATE_SSL_CONNECT,
CONN_STATE_SSL_CONNECT_COMPLETE,
CONN_STATE_AUTH_CHALLENGE_SEND,
CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE,
CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE,
};
// Internal write states.
enum WriteState {
WRITE_STATE_NONE,
WRITE_STATE_WRITE,
WRITE_STATE_WRITE_COMPLETE,
WRITE_STATE_DO_CALLBACK,
WRITE_STATE_ERROR,
};
// Internal read states.
enum ReadState {
READ_STATE_NONE,
READ_STATE_READ,
READ_STATE_READ_COMPLETE,
READ_STATE_DO_CALLBACK,
READ_STATE_ERROR,
};
private: private:
friend class ApiResourceManager<CastSocket>; friend class ApiResourceManager<CastSocket>;
friend class CastSocketTest; friend class CastSocketTest;
...@@ -260,11 +231,11 @@ class CastSocket : public ApiResource { ...@@ -260,11 +231,11 @@ class CastSocket : public ApiResource {
virtual base::Timer* GetTimer(); virtual base::Timer* GetTimer();
void SetConnectState(ConnectionState connect_state); void SetConnectState(proto::ConnectionState connect_state);
void SetReadyState(ReadyState ready_state); void SetReadyState(ReadyState ready_state);
void SetErrorState(ChannelError error_state); void SetErrorState(ChannelError error_state);
void SetReadState(ReadState read_state); void SetReadState(proto::ReadState read_state);
void SetWriteState(WriteState write_state); void SetWriteState(proto::WriteState write_state);
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
...@@ -322,11 +293,11 @@ class CastSocket : public ApiResource { ...@@ -322,11 +293,11 @@ class CastSocket : public ApiResource {
scoped_ptr<CastMessage> current_message_; scoped_ptr<CastMessage> current_message_;
// Connection flow state machine state. // Connection flow state machine state.
ConnectionState connect_state_; proto::ConnectionState connect_state_;
// Write flow state machine state. // Write flow state machine state.
WriteState write_state_; proto::WriteState write_state_;
// Read flow state machine state. // Read flow state machine state.
ReadState read_state_; proto::ReadState read_state_;
// The last error encountered by the channel. // The last error encountered by the channel.
ChannelError error_state_; ChannelError error_state_;
// The current status of the channel. // The current status of the channel.
......
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "extensions/browser/api/cast_channel/logger_util.h" #include "extensions/browser/api/cast_channel/logger_util.h"
#include "extensions/browser/api/cast_channel/logging.pb.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
namespace extensions { namespace extensions {
namespace core_api { namespace core_api {
namespace cast_channel { namespace cast_channel {
LastErrors::LastErrors() LastErrors::LastErrors()
: event_type(proto::EVENT_TYPE_UNKNOWN), : event_type(proto::EVENT_TYPE_UNKNOWN),
challenge_reply_error_type(proto::CHALLENGE_REPLY_ERROR_NONE), challenge_reply_error_type(proto::CHALLENGE_REPLY_ERROR_NONE),
...@@ -19,6 +19,51 @@ LastErrors::LastErrors() ...@@ -19,6 +19,51 @@ LastErrors::LastErrors()
LastErrors::~LastErrors() { LastErrors::~LastErrors() {
} }
proto::ErrorState ErrorStateToProto(ChannelError state) {
switch (state) {
case CHANNEL_ERROR_NONE:
return proto::CHANNEL_ERROR_NONE;
case CHANNEL_ERROR_CHANNEL_NOT_OPEN:
return proto::CHANNEL_ERROR_CHANNEL_NOT_OPEN;
case CHANNEL_ERROR_AUTHENTICATION_ERROR:
return proto::CHANNEL_ERROR_AUTHENTICATION_ERROR;
case CHANNEL_ERROR_CONNECT_ERROR:
return proto::CHANNEL_ERROR_CONNECT_ERROR;
case CHANNEL_ERROR_SOCKET_ERROR:
return proto::CHANNEL_ERROR_SOCKET_ERROR;
case CHANNEL_ERROR_TRANSPORT_ERROR:
return proto::CHANNEL_ERROR_TRANSPORT_ERROR;
case CHANNEL_ERROR_INVALID_MESSAGE:
return proto::CHANNEL_ERROR_INVALID_MESSAGE;
case CHANNEL_ERROR_INVALID_CHANNEL_ID:
return proto::CHANNEL_ERROR_INVALID_CHANNEL_ID;
case CHANNEL_ERROR_CONNECT_TIMEOUT:
return proto::CHANNEL_ERROR_CONNECT_TIMEOUT;
case CHANNEL_ERROR_UNKNOWN:
return proto::CHANNEL_ERROR_UNKNOWN;
default:
NOTREACHED();
return proto::CHANNEL_ERROR_NONE;
}
}
proto::ReadyState ReadyStateToProto(ReadyState state) {
switch (state) {
case READY_STATE_NONE:
return proto::READY_STATE_NONE;
case READY_STATE_CONNECTING:
return proto::READY_STATE_CONNECTING;
case READY_STATE_OPEN:
return proto::READY_STATE_OPEN;
case READY_STATE_CLOSING:
return proto::READY_STATE_CLOSING;
case READY_STATE_CLOSED:
return proto::READY_STATE_CLOSED;
default:
NOTREACHED();
return proto::READY_STATE_NONE;
}
}
} // namespace cast_channel } // namespace cast_channel
} // namespace api } // namespace api
} // namespace extensions } // namespace extensions
...@@ -6,13 +6,16 @@ ...@@ -6,13 +6,16 @@
#define EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_UTIL_H_ #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_UTIL_H_
#include "extensions/browser/api/cast_channel/logging.pb.h" #include "extensions/browser/api/cast_channel/logging.pb.h"
#include "extensions/common/api/cast_channel.h"
namespace extensions { namespace extensions {
namespace core_api { namespace core_api {
namespace cast_channel { namespace cast_channel {
// Converts an IDL "ChannelError" to a proto enum "ErrorState".
proto::ErrorState ErrorStateToProto(ChannelError state);
// TODO(mfoltz): Move the *ToProto functions from cast_socket.cc here. // Converts an IDL "ReadyState" to a proto enum "ReadyState".
// CastSocket should not need to know details of the logging protocol message. proto::ReadyState ReadyStateToProto(ReadyState state);
// Holds the most recent errors encountered by a CastSocket. // Holds the most recent errors encountered by a CastSocket.
struct LastErrors { struct LastErrors {
......
...@@ -20,6 +20,7 @@ generated_extensions_api("api_registration") { ...@@ -20,6 +20,7 @@ generated_extensions_api("api_registration") {
if (!is_android) { if (!is_android) {
deps += [ deps += [
"//device/serial", "//device/serial",
"//extensions/common/api/cast_channel:cast_channel_proto",
"//skia", "//skia",
] ]
} }
......
...@@ -16,5 +16,20 @@ ...@@ -16,5 +16,20 @@
'schemas.gypi', 'schemas.gypi',
], ],
}, },
{
# Protobuf compiler / generator for chrome.cast.channel-related protocol buffers.
# GN version: //extensions/browser/api/cast_channel:cast_channel_proto
'target_name': 'cast_channel_proto',
'type': 'static_library',
'sources': [
'cast_channel/cast_channel.proto',
'cast_channel/logging.proto'
],
'variables': {
'proto_in_dir': 'cast_channel',
'proto_out_dir': 'extensions/common/api/cast_channel',
},
'includes': [ '../../../build/protoc.gypi' ]
},
], ],
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import("//third_party/protobuf/proto_library.gni") import("//third_party/protobuf/proto_library.gni")
# GYP version: extensions/extensions.gyp:cast_channel_proto # GYP version: extensions/common/api/api.gyp:cast_channel_proto
proto_library("cast_channel_proto") { proto_library("cast_channel_proto") {
sources = [ sources = [
"cast_channel.proto", "cast_channel.proto",
......
...@@ -284,7 +284,7 @@ ...@@ -284,7 +284,7 @@
'../skia/skia.gyp:skia', '../skia/skia.gyp:skia',
'../third_party/leveldatabase/leveldatabase.gyp:leveldatabase', '../third_party/leveldatabase/leveldatabase.gyp:leveldatabase',
'browser/api/api_registration.gyp:extensions_api_registration', 'browser/api/api_registration.gyp:extensions_api_registration',
'cast_channel_proto', 'common/api/api.gyp:cast_channel_proto',
'common/api/api.gyp:extensions_api', 'common/api/api.gyp:extensions_api',
'extensions_common', 'extensions_common',
'extensions_strings.gyp:extensions_strings', 'extensions_strings.gyp:extensions_strings',
...@@ -1027,7 +1027,7 @@ ...@@ -1027,7 +1027,7 @@
'../testing/gmock.gyp:gmock', '../testing/gmock.gyp:gmock',
'../testing/gtest.gyp:gtest', '../testing/gtest.gyp:gtest',
'../third_party/leveldatabase/leveldatabase.gyp:leveldatabase', '../third_party/leveldatabase/leveldatabase.gyp:leveldatabase',
'cast_channel_proto', 'common/api/api.gyp:cast_channel_proto',
'extensions_common', 'extensions_common',
'extensions_renderer', 'extensions_renderer',
'extensions_resources.gyp:extensions_resources', 'extensions_resources.gyp:extensions_resources',
...@@ -1135,20 +1135,5 @@ ...@@ -1135,20 +1135,5 @@
}], }],
], ],
}, },
{
# Protobuf compiler / generator for chrome.cast.channel-related protocol buffers.
# GN version: //extensions/browser/api/cast_channel:cast_channel_proto
'target_name': 'cast_channel_proto',
'type': 'static_library',
'sources': [
'browser/api/cast_channel/cast_channel.proto',
'browser/api/cast_channel/logging.proto'
],
'variables': {
'proto_in_dir': 'browser/api/cast_channel',
'proto_out_dir': 'extensions/browser/api/cast_channel',
},
'includes': [ '../build/protoc.gypi' ]
},
] ]
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment