| Index: content/renderer/p2p/socket_client.cc
|
| diff --git a/content/renderer/p2p/socket_client.cc b/content/renderer/p2p/socket_client.cc
|
| index 74ad2b3c8cacfa1ca7ec3d32cd4aa904673b4e67..8f3a02445a7270b26da5b59fa3edcf741bbea6b0 100644
|
| --- a/content/renderer/p2p/socket_client.cc
|
| +++ b/content/renderer/p2p/socket_client.cc
|
| @@ -7,7 +7,9 @@
|
| #include "base/bind.h"
|
| #include "base/message_loop/message_loop_proxy.h"
|
| #include "content/common/p2p_messages.h"
|
| +#include "content/public/renderer/p2p_socket_client_delegate.h"
|
| #include "content/renderer/p2p/socket_dispatcher.h"
|
| +#include "content/renderer/render_thread_impl.h"
|
| #include "crypto/random.h"
|
|
|
| namespace {
|
| @@ -23,7 +25,18 @@ uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) {
|
|
|
| namespace content {
|
|
|
| -P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher)
|
| +P2PSocketClient* P2PSocketClient::CreateP2PSocket(
|
| + P2PSocketType type,
|
| + const net::IPEndPoint& local_address,
|
| + const net::IPEndPoint& remote_address,
|
| + P2PSocketClientDelegate* delegate) {
|
| + P2PSocketClientImpl *impl = new P2PSocketClientImpl(
|
| + RenderThreadImpl::current()->p2p_socket_dispatcher());
|
| + impl->Init(type, local_address, remote_address, delegate);
|
| + return impl;
|
| +}
|
| +
|
| +P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher)
|
| : dispatcher_(dispatcher),
|
| ipc_message_loop_(dispatcher->message_loop()),
|
| delegate_message_loop_(base::MessageLoopProxy::current()),
|
| @@ -34,27 +47,30 @@ P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher)
|
| crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_));
|
| }
|
|
|
| -P2PSocketClient::~P2PSocketClient() {
|
| +P2PSocketClientImpl::~P2PSocketClientImpl() {
|
| CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED);
|
| }
|
|
|
| -void P2PSocketClient::Init(
|
| +void P2PSocketClientImpl::Init(
|
| P2PSocketType type,
|
| const net::IPEndPoint& local_address,
|
| const net::IPEndPoint& remote_address,
|
| - P2PSocketClient::Delegate* delegate) {
|
| + P2PSocketClientDelegate* delegate) {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| // |delegate_| is only accessesed on |delegate_message_loop_|.
|
| delegate_ = delegate;
|
|
|
| ipc_message_loop_->PostTask(
|
| - FROM_HERE, base::Bind(&P2PSocketClient::DoInit, this, type, local_address,
|
| + FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit,
|
| + this,
|
| + type,
|
| + local_address,
|
| remote_address));
|
| }
|
|
|
| -void P2PSocketClient::DoInit(P2PSocketType type,
|
| - const net::IPEndPoint& local_address,
|
| - const net::IPEndPoint& remote_address) {
|
| +void P2PSocketClientImpl::DoInit(P2PSocketType type,
|
| + const net::IPEndPoint& local_address,
|
| + const net::IPEndPoint& remote_address) {
|
| DCHECK_EQ(state_, STATE_UNINITIALIZED);
|
| DCHECK(delegate_);
|
| state_ = STATE_OPENING;
|
| @@ -63,14 +79,14 @@ void P2PSocketClient::DoInit(P2PSocketType type,
|
| type, socket_id_, local_address, remote_address));
|
| }
|
|
|
| -void P2PSocketClient::SendWithDscp(
|
| +void P2PSocketClientImpl::SendWithDscp(
|
| const net::IPEndPoint& address,
|
| const std::vector<char>& data,
|
| net::DiffServCodePoint dscp) {
|
| if (!ipc_message_loop_->BelongsToCurrentThread()) {
|
| ipc_message_loop_->PostTask(
|
| FROM_HERE, base::Bind(
|
| - &P2PSocketClient::SendWithDscp, this, address, data, dscp));
|
| + &P2PSocketClientImpl::SendWithDscp, this, address, data, dscp));
|
| return;
|
| }
|
|
|
| @@ -84,21 +100,21 @@ void P2PSocketClient::SendWithDscp(
|
| }
|
| }
|
|
|
| -void P2PSocketClient::Send(const net::IPEndPoint& address,
|
| +void P2PSocketClientImpl::Send(const net::IPEndPoint& address,
|
| const std::vector<char>& data) {
|
| SendWithDscp(address, data, net::DSCP_DEFAULT);
|
| }
|
|
|
| -void P2PSocketClient::Close() {
|
| +void P2PSocketClientImpl::Close() {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
|
|
| delegate_ = NULL;
|
|
|
| ipc_message_loop_->PostTask(
|
| - FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this));
|
| + FROM_HERE, base::Bind(&P2PSocketClientImpl::DoClose, this));
|
| }
|
|
|
| -void P2PSocketClient::DoClose() {
|
| +void P2PSocketClientImpl::DoClose() {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
| if (dispatcher_) {
|
| if (state_ == STATE_OPEN || state_ == STATE_OPENING ||
|
| @@ -111,32 +127,39 @@ void P2PSocketClient::DoClose() {
|
| state_ = STATE_CLOSED;
|
| }
|
|
|
| -void P2PSocketClient::set_delegate(Delegate* delegate) {
|
| +int P2PSocketClientImpl::GetSocketID() const {
|
| + return socket_id_;
|
| +}
|
| +
|
| +void P2PSocketClientImpl::SetDelegate(P2PSocketClientDelegate* delegate) {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| delegate_ = delegate;
|
| }
|
|
|
| -void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) {
|
| +void P2PSocketClientImpl::OnSocketCreated(const net::IPEndPoint& address) {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
| DCHECK_EQ(state_, STATE_OPENING);
|
| state_ = STATE_OPEN;
|
|
|
| delegate_message_loop_->PostTask(
|
| FROM_HERE,
|
| - base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address));
|
| + base::Bind(&P2PSocketClientImpl::DeliverOnSocketCreated, this, address));
|
| }
|
|
|
| -void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) {
|
| +void P2PSocketClientImpl::DeliverOnSocketCreated(
|
| + const net::IPEndPoint& address) {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| if (delegate_)
|
| delegate_->OnOpen(address);
|
| }
|
|
|
| -void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) {
|
| +void P2PSocketClientImpl::OnIncomingTcpConnection(
|
| + const net::IPEndPoint& address) {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
| DCHECK_EQ(state_, STATE_OPEN);
|
|
|
| - scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_);
|
| + scoped_refptr<P2PSocketClientImpl> new_client =
|
| + new P2PSocketClientImpl(dispatcher_);
|
| new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get());
|
| new_client->state_ = STATE_OPEN;
|
| new_client->delegate_message_loop_ = delegate_message_loop_;
|
| @@ -145,12 +168,14 @@ void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) {
|
| socket_id_, address, new_client->socket_id_));
|
|
|
| delegate_message_loop_->PostTask(
|
| - FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection,
|
| - this, address, new_client));
|
| + FROM_HERE, base::Bind(
|
| + &P2PSocketClientImpl::DeliverOnIncomingTcpConnection,
|
| + this, address, new_client));
|
| }
|
|
|
| -void P2PSocketClient::DeliverOnIncomingTcpConnection(
|
| - const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) {
|
| +void P2PSocketClientImpl::DeliverOnIncomingTcpConnection(
|
| + const net::IPEndPoint& address,
|
| + scoped_refptr<P2PSocketClient> new_client) {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| if (delegate_) {
|
| delegate_->OnIncomingTcpConnection(address, new_client.get());
|
| @@ -160,50 +185,53 @@ void P2PSocketClient::DeliverOnIncomingTcpConnection(
|
| }
|
| }
|
|
|
| -void P2PSocketClient::OnSendComplete() {
|
| +void P2PSocketClientImpl::OnSendComplete() {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
|
|
| delegate_message_loop_->PostTask(
|
| - FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnSendComplete, this));
|
| + FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this));
|
| }
|
|
|
| -void P2PSocketClient::DeliverOnSendComplete() {
|
| +void P2PSocketClientImpl::DeliverOnSendComplete() {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| if (delegate_)
|
| delegate_->OnSendComplete();
|
| }
|
|
|
| -void P2PSocketClient::OnError() {
|
| +void P2PSocketClientImpl::OnError() {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
| state_ = STATE_ERROR;
|
|
|
| delegate_message_loop_->PostTask(
|
| - FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this));
|
| + FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this));
|
| }
|
|
|
| -void P2PSocketClient::DeliverOnError() {
|
| +void P2PSocketClientImpl::DeliverOnError() {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| if (delegate_)
|
| delegate_->OnError();
|
| }
|
|
|
| -void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address,
|
| - const std::vector<char>& data) {
|
| +void P2PSocketClientImpl::OnDataReceived(const net::IPEndPoint& address,
|
| + const std::vector<char>& data) {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
| DCHECK_EQ(STATE_OPEN, state_);
|
| delegate_message_loop_->PostTask(
|
| FROM_HERE,
|
| - base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data));
|
| + base::Bind(&P2PSocketClientImpl::DeliverOnDataReceived,
|
| + this,
|
| + address,
|
| + data));
|
| }
|
|
|
| -void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address,
|
| - const std::vector<char>& data) {
|
| +void P2PSocketClientImpl::DeliverOnDataReceived(const net::IPEndPoint& address,
|
| + const std::vector<char>& data) {
|
| DCHECK(delegate_message_loop_->BelongsToCurrentThread());
|
| if (delegate_)
|
| delegate_->OnDataReceived(address, data);
|
| }
|
|
|
| -void P2PSocketClient::Detach() {
|
| +void P2PSocketClientImpl::Detach() {
|
| DCHECK(ipc_message_loop_->BelongsToCurrentThread());
|
| dispatcher_ = NULL;
|
| OnError();
|
|
|