diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-03 21:37:26 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-03 21:37:26 +0000 |
commit | 47bb44cc2cd35907a90d0b62bb23d2d6db236af1 (patch) | |
tree | a936b0b636dcbdc53f6d7caeb45232e79bbaa6a1 | |
parent | 488d49259d05b6b72e634506124bc02e497e3696 (diff) | |
download | chromium_src-47bb44cc2cd35907a90d0b62bb23d2d6db236af1.zip chromium_src-47bb44cc2cd35907a90d0b62bb23d2d6db236af1.tar.gz chromium_src-47bb44cc2cd35907a90d0b62bb23d2d6db236af1.tar.bz2 |
Rename PepperP2PChannel to PepperTransportSocketAdaptor. Implement net::SocketStream interface.
BUG=51198
TEST=None
Review URL: http://codereview.chromium.org/7549027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95325 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | remoting/protocol/pepper_p2p_channel.cc | 170 | ||||
-rw-r--r-- | remoting/protocol/pepper_p2p_channel.h | 76 | ||||
-rw-r--r-- | remoting/protocol/pepper_transport_socket_adapter.cc | 259 | ||||
-rw-r--r-- | remoting/protocol/pepper_transport_socket_adapter.h | 109 | ||||
-rw-r--r-- | remoting/remoting.gyp | 4 |
5 files changed, 370 insertions, 248 deletions
diff --git a/remoting/protocol/pepper_p2p_channel.cc b/remoting/protocol/pepper_p2p_channel.cc deleted file mode 100644 index 516b7b7..0000000 --- a/remoting/protocol/pepper_p2p_channel.cc +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "remoting/protocol/pepper_p2p_channel.h" - -#include "base/logging.h" -#include "net/base/io_buffer.h" -#include "net/base/net_errors.h" -#include "ppapi/c/pp_errors.h" -#include "ppapi/cpp/dev/transport_dev.h" -#include "ppapi/cpp/var.h" - -namespace remoting { -namespace protocol { - -namespace { - -const char kPepperTransportUdpProtocol[] = "udp"; - -// Maps value returned by Recv() and Send() Pepper methods to net::Error. -int PPErrorToNetError(int result) { - if (result > 0) - return result; - - switch (result) { - case PP_OK: - return net::OK; - case PP_OK_COMPLETIONPENDING: - return net::ERR_IO_PENDING; - default: - return net::ERR_FAILED; - } -} - -} // namespace - -PepperP2PChannel::PepperP2PChannel( - pp::Instance* pp_instance, - const char* name, - const IncomingCandidateCallback& candidate_callback) - : candidate_callback_(candidate_callback), - get_address_pending_(false), - read_callback_(NULL), - write_callback_(NULL) { - transport_.reset( - new pp::Transport_Dev(pp_instance, name, kPepperTransportUdpProtocol)); -} - -bool PepperP2PChannel::Init() { - // This will return false when the GetNextAddress() returns an - // error. Particularly it is useful to detect when the P2P Transport - // API is not supported. - return ProcessCandidates(); -} - -PepperP2PChannel::~PepperP2PChannel() { -} - -void PepperP2PChannel::AddRemoteCandidate(const std::string& candidate) { - DCHECK(CalledOnValidThread()); - transport_->ReceiveRemoteAddress(candidate); -} - -int PepperP2PChannel::Read(net::IOBuffer* buf, int buf_len, - net::CompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK(!read_callback_); - DCHECK(!read_buffer_); - - int result = PPErrorToNetError(transport_->Recv( - buf->data(), buf_len, - pp::CompletionCallback(&PepperP2PChannel::ReadCallback, this))); - - if (result == net::ERR_IO_PENDING) { - read_callback_ = callback; - read_buffer_ = buf; - } - - return result; -} - -int PepperP2PChannel::Write(net::IOBuffer* buf, int buf_len, - net::CompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK(!write_callback_); - DCHECK(!write_buffer_); - - int result = PPErrorToNetError(transport_->Send( - buf->data(), buf_len, - pp::CompletionCallback(&PepperP2PChannel::WriteCallback, this))); - - if (result == net::ERR_IO_PENDING) { - write_callback_ = callback; - write_buffer_ = buf; - } - - return result; -} - -bool PepperP2PChannel::SetReceiveBufferSize(int32 size) { - DCHECK(CalledOnValidThread()); - NOTIMPLEMENTED(); - return false; -} - -bool PepperP2PChannel::SetSendBufferSize(int32 size) { - DCHECK(CalledOnValidThread()); - NOTIMPLEMENTED(); - return false; -} - -bool PepperP2PChannel::ProcessCandidates() { - DCHECK(CalledOnValidThread()); - DCHECK(!get_address_pending_); - - while (true) { - pp::Var address; - int result = transport_->GetNextAddress( - &address, - pp::CompletionCallback(&PepperP2PChannel::NextAddressCallback, this)); - if (result == PP_OK_COMPLETIONPENDING) { - get_address_pending_ = true; - break; - } - - if (result == PP_OK) { - candidate_callback_.Run(address.AsString()); - } else { - LOG(ERROR) << "GetNextAddress() returned an error " << result; - return false; - } - } - return true; -} - -// static -void PepperP2PChannel::NextAddressCallback(void* data, int32_t result) { - PepperP2PChannel* channel = reinterpret_cast<PepperP2PChannel*>(data); - DCHECK(channel->CalledOnValidThread()); - channel->get_address_pending_ = false; - channel->ProcessCandidates(); -} - -// static -void PepperP2PChannel::ReadCallback(void* data, int32_t result) { - PepperP2PChannel* channel = reinterpret_cast<PepperP2PChannel*>(data); - DCHECK(channel->CalledOnValidThread()); - DCHECK(channel->read_callback_); - DCHECK(channel->read_buffer_); - net::CompletionCallback* callback = channel->read_callback_; - channel->read_callback_ = NULL; - channel->read_buffer_ = NULL; - callback->Run(result); -} - -// static -void PepperP2PChannel::WriteCallback(void* data, int32_t result) { - PepperP2PChannel* channel = reinterpret_cast<PepperP2PChannel*>(data); - DCHECK(channel->CalledOnValidThread()); - DCHECK(channel->write_callback_); - DCHECK(channel->write_buffer_); - net::CompletionCallback* callback = channel->write_callback_; - channel->write_callback_ = NULL; - channel->write_buffer_ = NULL; - callback->Run(result); -} - -} // namespace protocol -} // namespace remoting diff --git a/remoting/protocol/pepper_p2p_channel.h b/remoting/protocol/pepper_p2p_channel.h deleted file mode 100644 index b818bc8..0000000 --- a/remoting/protocol/pepper_p2p_channel.h +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef REMOTING_PROTOCOL_PEPPER_P2P_CHANNEL_H_ -#define REMOTING_PROTOCOL_PEPPER_P2P_CHANNEL_H_ - -#include <string> - -#include "base/callback.h" -#include "base/memory/scoped_ptr.h" -#include "base/threading/non_thread_safe.h" -#include "net/socket/socket.h" -#include "ppapi/c/pp_stdint.h" - -namespace pp { -class Instance; -class Transport_Dev; -} // namespace pp - -namespace remoting { -namespace protocol { - -// This class create P2PChannel based on the Pepper P2P Transport API -// and provides net::Socket interface on top of it. -class PepperP2PChannel : public base::NonThreadSafe, - public net::Socket { - public: - // TODO(sergeyu): Use cricket::Candidate instead of strings to - // represent candidates. - typedef base::Callback<void(const std::string&)> IncomingCandidateCallback; - - PepperP2PChannel(pp::Instance* pp_instance, const char* name, - const IncomingCandidateCallback& candidate_callback); - virtual ~PepperP2PChannel(); - - bool Init(); - - // Adds candidate received from the peer. - void AddRemoteCandidate(const std::string& candidate); - - // net::Socket interface. - virtual int Read(net::IOBuffer* buf, int buf_len, - net::CompletionCallback* callback); - virtual int Write(net::IOBuffer* buf, int buf_len, - net::CompletionCallback* callback); - virtual bool SetReceiveBufferSize(int32 size); - virtual bool SetSendBufferSize(int32 size); - - private: - // Callbacks for PPAPI calls. - static void NextAddressCallback(void* data, int32_t result); - static void ReadCallback(void* data, int32_t result); - static void WriteCallback(void* data, int32_t result); - - bool ProcessCandidates(); - - IncomingCandidateCallback candidate_callback_; - - scoped_ptr<pp::Transport_Dev> transport_; - - bool get_address_pending_; - - net::CompletionCallback* read_callback_; - scoped_refptr<net::IOBuffer> read_buffer_; - - net::CompletionCallback* write_callback_; - scoped_refptr<net::IOBuffer> write_buffer_; - - DISALLOW_COPY_AND_ASSIGN(PepperP2PChannel); -}; - -} // namespace protocol -} // namespace remoting - -#endif // REMOTING_PROTOCOL_PEPPER_P2P_CHANNEL_H_ diff --git a/remoting/protocol/pepper_transport_socket_adapter.cc b/remoting/protocol/pepper_transport_socket_adapter.cc new file mode 100644 index 0000000..d247e05 --- /dev/null +++ b/remoting/protocol/pepper_transport_socket_adapter.cc @@ -0,0 +1,259 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/protocol/pepper_transport_socket_adapter.h" + +#include "base/logging.h" +#include "net/base/io_buffer.h" +#include "net/base/net_errors.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/cpp/dev/transport_dev.h" +#include "ppapi/cpp/var.h" + +namespace remoting { +namespace protocol { + +namespace { + +const char kTcpProtocol[] = "tcp"; + +// Maps value returned by Recv() and Send() Pepper methods to net::Error. +int PPErrorToNetError(int result) { + if (result > 0) + return result; + + switch (result) { + case PP_OK: + return net::OK; + case PP_OK_COMPLETIONPENDING: + return net::ERR_IO_PENDING; + case PP_ERROR_NOTSUPPORTED: + case PP_ERROR_NOINTERFACE: + return net::ERR_NOT_IMPLEMENTED; + default: + return net::ERR_FAILED; + } +} + +} // namespace + +PepperTransportSocketAdapter::PepperTransportSocketAdapter( + pp::Instance* pp_instance, + const std::string& name, + Observer* observer) + : name_(name), + observer_(observer), + connected_(false), + get_address_pending_(false), + read_callback_(NULL), + write_callback_(NULL) { + callback_factory_.Initialize(this); + transport_.reset(new pp::Transport_Dev( + pp_instance, name_.c_str(), kTcpProtocol)); +} + +PepperTransportSocketAdapter::~PepperTransportSocketAdapter() { + observer_->OnChannelDeleted(); +} + +void PepperTransportSocketAdapter::AddRemoteCandidate( + const std::string& candidate) { + DCHECK(CalledOnValidThread()); + transport_->ReceiveRemoteAddress(candidate); +} + +int PepperTransportSocketAdapter::Read(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback) { + DCHECK(CalledOnValidThread()); + DCHECK(!read_callback_); + DCHECK(!read_buffer_); + + int result = PPErrorToNetError(transport_->Recv( + buf->data(), buf_len, + callback_factory_.NewOptionalCallback( + &PepperTransportSocketAdapter::OnRead))); + + if (result == net::ERR_IO_PENDING) { + read_callback_ = callback; + read_buffer_ = buf; + } + + return result; +} + +int PepperTransportSocketAdapter::Write(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback) { + DCHECK(CalledOnValidThread()); + DCHECK(!write_callback_); + DCHECK(!write_buffer_); + + int result = PPErrorToNetError(transport_->Send( + buf->data(), buf_len, + callback_factory_.NewOptionalCallback( + &PepperTransportSocketAdapter::OnWrite))); + + if (result == net::ERR_IO_PENDING) { + write_callback_ = callback; + write_buffer_ = buf; + } + + return result; +} + +bool PepperTransportSocketAdapter::SetReceiveBufferSize(int32 size) { + DCHECK(CalledOnValidThread()); + // TODO(sergeyu): Implement this: crbug.com/91439. + NOTIMPLEMENTED(); + return false; +} + +bool PepperTransportSocketAdapter::SetSendBufferSize(int32 size) { + DCHECK(CalledOnValidThread()); + // TODO(sergeyu): Implement this: crbug.com/91439. + NOTIMPLEMENTED(); + return false; +} + +int PepperTransportSocketAdapter::Connect(net::CompletionCallback* callback) { + connect_callback_ = callback; + + // This will return false when GetNextAddress() returns an + // error. This helps to detect when the P2P Transport API is not + // supported. + int result = ProcessCandidates(); + if (result != net::OK) + return result; + + result = transport_->Connect( + callback_factory_.NewRequiredCallback( + &PepperTransportSocketAdapter::OnConnect)); + DCHECK_EQ(result, PP_OK_COMPLETIONPENDING); + + return net::ERR_IO_PENDING; +} + +void PepperTransportSocketAdapter::Disconnect() { + NOTIMPLEMENTED(); +} + +bool PepperTransportSocketAdapter::IsConnected() const { + return connected_; +} + +bool PepperTransportSocketAdapter::IsConnectedAndIdle() const { + NOTIMPLEMENTED(); + return false; +} + +int PepperTransportSocketAdapter::GetPeerAddress( + net::AddressList* address) const { + NOTIMPLEMENTED(); + return net::ERR_FAILED; +} + +int PepperTransportSocketAdapter::GetLocalAddress( + net::IPEndPoint* address) const { + NOTIMPLEMENTED(); + return net::ERR_FAILED; +} + +const net::BoundNetLog& PepperTransportSocketAdapter::NetLog() const { + return net_log_; +} + +void PepperTransportSocketAdapter::SetSubresourceSpeculation() { + NOTIMPLEMENTED(); +} + +void PepperTransportSocketAdapter::SetOmniboxSpeculation() { + NOTIMPLEMENTED(); +} + +bool PepperTransportSocketAdapter::WasEverUsed() const { + NOTIMPLEMENTED(); + return true; +} + +bool PepperTransportSocketAdapter::UsingTCPFastOpen() const { + NOTIMPLEMENTED(); + return true; +} + +int64 PepperTransportSocketAdapter::NumBytesRead() const { + NOTIMPLEMENTED(); + return 0; +} + +base::TimeDelta PepperTransportSocketAdapter::GetConnectTimeMicros() const { + NOTIMPLEMENTED(); + return base::TimeDelta(); +} + + +int PepperTransportSocketAdapter::ProcessCandidates() { + DCHECK(CalledOnValidThread()); + DCHECK(!get_address_pending_); + + while (true) { + pp::Var address; + int result = transport_->GetNextAddress( + &address, callback_factory_.NewOptionalCallback( + &PepperTransportSocketAdapter::OnNextAddress)); + if (result == PP_OK_COMPLETIONPENDING) { + get_address_pending_ = true; + break; + } + + if (result == PP_OK) { + observer_->OnChannelNewLocalCandidate(address.AsString()); + } else { + LOG(ERROR) << "GetNextAddress() returned an error " << result; + return PPErrorToNetError(result); + } + } + return net::OK; +} + +void PepperTransportSocketAdapter::OnNextAddress(int32_t result) { + DCHECK(CalledOnValidThread()); + + get_address_pending_ = false; + ProcessCandidates(); +} + +void PepperTransportSocketAdapter::OnConnect(int result) { + DCHECK(connect_callback_); + + if (result == PP_OK) + connected_ = true; + + net::CompletionCallback* callback = connect_callback_; + connect_callback_ = NULL; + callback->Run(PPErrorToNetError(result)); +} + +void PepperTransportSocketAdapter::OnRead(int32_t result) { + DCHECK(CalledOnValidThread()); + DCHECK(read_callback_); + DCHECK(read_buffer_); + + net::CompletionCallback* callback = read_callback_; + read_callback_ = NULL; + read_buffer_ = NULL; + callback->Run(PPErrorToNetError(result)); +} + +void PepperTransportSocketAdapter::OnWrite(int32_t result) { + DCHECK(CalledOnValidThread()); + DCHECK(write_callback_); + DCHECK(write_buffer_); + + net::CompletionCallback* callback = write_callback_; + write_callback_ = NULL; + write_buffer_ = NULL; + callback->Run(PPErrorToNetError(result)); +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/pepper_transport_socket_adapter.h b/remoting/protocol/pepper_transport_socket_adapter.h new file mode 100644 index 0000000..c69f8d2 --- /dev/null +++ b/remoting/protocol/pepper_transport_socket_adapter.h @@ -0,0 +1,109 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_PROTOCOL_PEPPER_TRANSPORT_SOCKET_ADAPTER_H_ +#define REMOTING_PROTOCOL_PEPPER_TRANSPORT_SOCKET_ADAPTER_H_ + +#include <string> + +#include "base/callback.h" +#include "base/memory/scoped_ptr.h" +#include "base/threading/non_thread_safe.h" +#include "net/base/net_log.h" +#include "net/socket/stream_socket.h" +#include "ppapi/c/pp_stdint.h" +#include "ppapi/cpp/completion_callback.h" + +namespace pp { +class Instance; +class Transport_Dev; +} // namespace pp + +namespace remoting { +namespace protocol { + +// This class implements net::StreamSocket interface on top of the the +// Pepper P2P Transport API. +class PepperTransportSocketAdapter : public base::NonThreadSafe, + public net::StreamSocket { + public: + // Observer is used to notify about new local candidates and + // deletion of the adapter. + class Observer { + public: + Observer() { } + virtual ~Observer() { } + virtual void OnChannelDeleted() = 0; + virtual void OnChannelNewLocalCandidate(const std::string& candidate) = 0; + }; + + PepperTransportSocketAdapter(pp::Instance* pp_instance, + const std::string& name, + Observer* observer); + virtual ~PepperTransportSocketAdapter(); + + const std::string& name() { return name_; } + + // Adds candidate received from the peer. + void AddRemoteCandidate(const std::string& candidate); + + // net::Socket interface. + virtual int Read(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback) OVERRIDE; + virtual int Write(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback) OVERRIDE; + virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; + virtual bool SetSendBufferSize(int32 size) OVERRIDE; + + // net::StreamSocket interface. + virtual int Connect(net::CompletionCallback* callback) OVERRIDE; + virtual void Disconnect() OVERRIDE; + virtual bool IsConnected() const OVERRIDE; + virtual bool IsConnectedAndIdle() const OVERRIDE; + virtual int GetPeerAddress(net::AddressList* address) const OVERRIDE; + virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; + virtual const net::BoundNetLog& NetLog() const OVERRIDE; + virtual void SetSubresourceSpeculation() OVERRIDE; + virtual void SetOmniboxSpeculation() OVERRIDE; + virtual bool WasEverUsed() const OVERRIDE; + virtual bool UsingTCPFastOpen() const OVERRIDE; + virtual int64 NumBytesRead() const OVERRIDE; + virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; + + private: + // Callbacks for PPAPI calls. + void OnConnect(int result); + void OnNextAddress(int32_t result); + void OnRead(int32_t result); + void OnWrite(int32_t result); + + int ProcessCandidates(); + + std::string name_; + Observer* observer_; + + scoped_ptr<pp::Transport_Dev> transport_; + + net::CompletionCallback* connect_callback_; + bool connected_; + + bool get_address_pending_; + + net::CompletionCallback* read_callback_; + scoped_refptr<net::IOBuffer> read_buffer_; + + net::CompletionCallback* write_callback_; + scoped_refptr<net::IOBuffer> write_buffer_; + + net::BoundNetLog net_log_; + + pp::CompletionCallbackFactory<PepperTransportSocketAdapter> callback_factory_; + + DISALLOW_COPY_AND_ASSIGN(PepperTransportSocketAdapter); +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_PEPPER_TRANSPORT_SOCKET_ADAPTER_H_ diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index 1c07e3c..6d8264a 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -636,8 +636,8 @@ 'protocol/message_decoder.h', 'protocol/message_reader.cc', 'protocol/message_reader.h', - 'protocol/pepper_p2p_channel.h', - 'protocol/pepper_p2p_channel.cc', + 'protocol/pepper_transport_socket_adapter.cc', + 'protocol/pepper_transport_socket_adapter.h', 'protocol/protobuf_video_reader.cc', 'protocol/protobuf_video_reader.h', 'protocol/protobuf_video_writer.cc', |