diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-31 00:26:03 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-31 00:26:03 +0000 |
commit | f6da0b2581a106cfb2ad22b1d15993a89dd9a75a (patch) | |
tree | ebe1ffc783015bad357a5dfa1e298ef36a6c6061 /remoting | |
parent | 5a1f7e2418011b542e12f324bea55895f4c2cc56 (diff) | |
download | chromium_src-f6da0b2581a106cfb2ad22b1d15993a89dd9a75a.zip chromium_src-f6da0b2581a106cfb2ad22b1d15993a89dd9a75a.tar.gz chromium_src-f6da0b2581a106cfb2ad22b1d15993a89dd9a75a.tar.bz2 |
Moved socket adapters from remoting/jingle_glue to jingle/glue.
BUG=None
TEST=compiles, unittests
Review URL: http://codereview.chromium.org/6776003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79929 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/jingle_glue/channel_socket_adapter.cc | 171 | ||||
-rw-r--r-- | remoting/jingle_glue/channel_socket_adapter.h | 67 | ||||
-rw-r--r-- | remoting/jingle_glue/channel_socket_adapter_unittest.cc | 121 | ||||
-rw-r--r-- | remoting/jingle_glue/jingle_glue_mock_objects.cc | 13 | ||||
-rw-r--r-- | remoting/jingle_glue/jingle_glue_mock_objects.h | 32 | ||||
-rw-r--r-- | remoting/jingle_glue/ssl_socket_adapter.cc | 9 | ||||
-rw-r--r-- | remoting/jingle_glue/stream_socket_adapter.cc | 238 | ||||
-rw-r--r-- | remoting/jingle_glue/stream_socket_adapter.h | 85 | ||||
-rw-r--r-- | remoting/jingle_glue/stream_socket_adapter_unittest.cc | 151 | ||||
-rw-r--r-- | remoting/jingle_glue/utils.cc | 53 | ||||
-rw-r--r-- | remoting/jingle_glue/utils.h | 15 | ||||
-rw-r--r-- | remoting/protocol/DEPS | 3 | ||||
-rw-r--r-- | remoting/protocol/jingle_session.cc | 14 | ||||
-rw-r--r-- | remoting/protocol/jingle_session.h | 18 | ||||
-rw-r--r-- | remoting/remoting.gyp | 11 |
15 files changed, 24 insertions, 977 deletions
diff --git a/remoting/jingle_glue/channel_socket_adapter.cc b/remoting/jingle_glue/channel_socket_adapter.cc deleted file mode 100644 index 583eed1..0000000 --- a/remoting/jingle_glue/channel_socket_adapter.cc +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright (c) 2010 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/jingle_glue/channel_socket_adapter.h" - -#include <limits> - -#include "base/logging.h" -#include "base/message_loop.h" -#include "net/base/io_buffer.h" -#include "net/base/net_errors.h" -#include "remoting/jingle_glue/utils.h" -#include "third_party/libjingle/source/talk/p2p/base/transportchannel.h" - -namespace remoting { - -TransportChannelSocketAdapter::TransportChannelSocketAdapter( - cricket::TransportChannel* channel) - : channel_(channel), - read_pending_(false), - write_pending_(false), - closed_error_code_(net::OK) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK(channel_); - - channel_->SignalReadPacket.connect( - this, &TransportChannelSocketAdapter::OnNewPacket); - channel_->SignalWritableState.connect( - this, &TransportChannelSocketAdapter::OnWritableState); - channel_->SignalDestroyed.connect( - this, &TransportChannelSocketAdapter::OnChannelDestroyed); -} - -TransportChannelSocketAdapter::~TransportChannelSocketAdapter() { -} - -int TransportChannelSocketAdapter::Read( - net::IOBuffer* buf, int buffer_size, net::CompletionCallback* callback) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK(buf); - CHECK(!read_pending_); - - if (!channel_) { - DCHECK(closed_error_code_ != net::OK); - return closed_error_code_; - } - - read_callback_ = callback; - read_buffer_ = buf; - read_buffer_size_ = buffer_size; - read_pending_ = true; - - return net::ERR_IO_PENDING; -} - -int TransportChannelSocketAdapter::Write( - net::IOBuffer* buffer, int buffer_size, net::CompletionCallback* callback) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK(buffer); - CHECK(!write_pending_); - - if (!channel_) { - DCHECK(closed_error_code_ != net::OK); - return closed_error_code_; - } - - int result = channel_->SendPacket(buffer->data(), buffer_size); - if (result < 0) { - result = MapPosixToChromeError(channel_->GetError()); - if (result == net::ERR_IO_PENDING) { - write_pending_ = true; - write_callback_ = callback; - write_buffer_ = buffer; - write_buffer_size_ = buffer_size; - } - } - return result; -} - -bool TransportChannelSocketAdapter::SetReceiveBufferSize(int32 size) { - NOTIMPLEMENTED(); - return false; -} - -bool TransportChannelSocketAdapter::SetSendBufferSize(int32 size) { - NOTIMPLEMENTED(); - return false; -} - -void TransportChannelSocketAdapter::Close(int error_code) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - - if (!channel_) // Already closed. - return; - - DCHECK(error_code != net::OK); - closed_error_code_ = error_code; - channel_->SignalReadPacket.disconnect(this); - channel_->SignalDestroyed.disconnect(this); - channel_ = NULL; - - if (read_pending_) { - net::CompletionCallback* callback = read_callback_; - read_pending_ = false; - read_buffer_ = NULL; - callback->Run(error_code); - } - - if (write_pending_) { - net::CompletionCallback* callback = write_callback_; - write_pending_ = false; - write_buffer_ = NULL; - callback->Run(error_code); - } -} - -void TransportChannelSocketAdapter::OnNewPacket( - cricket::TransportChannel* channel, const char* data, size_t data_size) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK_EQ(channel, channel_); - if (read_pending_) { - DCHECK(read_buffer_); - CHECK_LT(data_size, static_cast<size_t>(std::numeric_limits<int>::max())); - - if (read_buffer_size_ < static_cast<int>(data_size)) { - LOG(WARNING) << "Data buffer is smaller than the received packet. " - << "Dropping the data that doesn't fit."; - data_size = read_buffer_size_; - } - - memcpy(read_buffer_->data(), data, data_size); - - net::CompletionCallback* callback = read_callback_; - read_pending_ = false; - read_buffer_ = NULL; - - callback->Run(data_size); - } else { - LOG(WARNING) - << "Data was received without a callback. Dropping the packet."; - } -} - -void TransportChannelSocketAdapter::OnWritableState( - cricket::TransportChannel* channel) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - // Try to send the packet if there is a pending write. - if (write_pending_) { - int result = channel_->SendPacket(write_buffer_->data(), - write_buffer_size_); - if (result < 0) - result = MapPosixToChromeError(channel_->GetError()); - - if (result != net::ERR_IO_PENDING) { - net::CompletionCallback* callback = write_callback_; - write_pending_ = false; - write_buffer_ = NULL; - callback->Run(result); - } - } -} - -void TransportChannelSocketAdapter::OnChannelDestroyed( - cricket::TransportChannel* channel) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK_EQ(channel, channel_); - Close(net::ERR_CONNECTION_ABORTED); -} - -} // namespace remoting diff --git a/remoting/jingle_glue/channel_socket_adapter.h b/remoting/jingle_glue/channel_socket_adapter.h deleted file mode 100644 index d50c560e..0000000 --- a/remoting/jingle_glue/channel_socket_adapter.h +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2010 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_JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_ -#define REMOTING_JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_ - -#include "net/socket/socket.h" -#include "third_party/libjingle/source/talk/base/socketaddress.h" -#include "third_party/libjingle/source/talk/base/sigslot.h" - -namespace cricket { -class TransportChannel; -} // namespace cricket - -namespace remoting { - -// TransportChannelSocketAdapter implements net::Socket interface on -// top of libjingle's TransportChannel. It is used by -// JingleChromotocolConnection to provide net::Socket interface for channels. -class TransportChannelSocketAdapter : public net::Socket, - public sigslot::has_slots<> { - public: - // TransportChannel object is always owned by the corresponding session. - explicit TransportChannelSocketAdapter(cricket::TransportChannel* channel); - virtual ~TransportChannelSocketAdapter(); - - // Closes the stream. |error_code| specifies error code that will - // be returned by Read() and Write() after the stream is closed. - // Must be called before the session and the channel are destroyed. - void Close(int error_code); - - // 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: - void OnNewPacket(cricket::TransportChannel* channel, - const char* data, size_t data_size); - void OnWritableState(cricket::TransportChannel* channel); - void OnChannelDestroyed(cricket::TransportChannel* channel); - - cricket::TransportChannel* channel_; - - bool read_pending_; - net::CompletionCallback* read_callback_; // Not owned. - scoped_refptr<net::IOBuffer> read_buffer_; - int read_buffer_size_; - - bool write_pending_; - net::CompletionCallback* write_callback_; // Not owned. - scoped_refptr<net::IOBuffer> write_buffer_; - int write_buffer_size_; - - int closed_error_code_; - - DISALLOW_COPY_AND_ASSIGN(TransportChannelSocketAdapter); -}; - -} // namespace remoting - -#endif // REMOTING_JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_ diff --git a/remoting/jingle_glue/channel_socket_adapter_unittest.cc b/remoting/jingle_glue/channel_socket_adapter_unittest.cc deleted file mode 100644 index 885d308..0000000 --- a/remoting/jingle_glue/channel_socket_adapter_unittest.cc +++ /dev/null @@ -1,121 +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 "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop.h" -#include "net/base/io_buffer.h" -#include "net/base/net_errors.h" -#include "net/socket/socket.h" -#include "remoting/jingle_glue/channel_socket_adapter.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "third_party/libjingle/source/talk/p2p/base/transportchannel.h" - -using net::IOBuffer; - -using testing::_; -using testing::Return; - -namespace remoting { - -namespace { -const int kBufferSize = 4096; -const char kTestData[] = "data"; -const int kTestDataSize = 4; -const int kTestError = -32123; -} // namespace - -class MockTransportChannel : public cricket::TransportChannel { - public: - MockTransportChannel() - : cricket::TransportChannel("", "") { - } - - MOCK_METHOD2(SendPacket, int(const char *data, size_t len)); - MOCK_METHOD2(SetOption, int(talk_base::Socket::Option opt, int value)); - MOCK_METHOD0(GetError, int()); -}; - -class TransportChannelSocketAdapterTest : public testing::Test { - public: - TransportChannelSocketAdapterTest() - : ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &TransportChannelSocketAdapterTest::Callback)), - callback_result_(0) { - } - - protected: - virtual void SetUp() { - target_.reset(new TransportChannelSocketAdapter(&channel_)); - } - - void Callback(int result) { - callback_result_ = result; - } - - MockTransportChannel channel_; - scoped_ptr<TransportChannelSocketAdapter> target_; - net::CompletionCallbackImpl<TransportChannelSocketAdapterTest> callback_; - int callback_result_; - MessageLoopForIO message_loop_; -}; - -// Verify that Read() returns net::ERR_IO_PENDING. -TEST_F(TransportChannelSocketAdapterTest, Read) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - - int result = target_->Read(buffer, kBufferSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - channel_.SignalReadPacket(&channel_, kTestData, kTestDataSize); - EXPECT_EQ(kTestDataSize, callback_result_); -} - -// Verify that Read() after Close() returns error. -TEST_F(TransportChannelSocketAdapterTest, ReadClose) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - - int result = target_->Read(buffer, kBufferSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - target_->Close(kTestError); - EXPECT_EQ(kTestError, callback_result_); - - // All Read() calls after Close() should return the error. - EXPECT_EQ(kTestError, target_->Read(buffer, kBufferSize, &callback_)); -} - -// Verify that Write sends the packet and returns correct result. -TEST_F(TransportChannelSocketAdapterTest, Write) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize)); - - EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize)) - .WillOnce(Return(kTestDataSize)); - - int result = target_->Write(buffer, kTestDataSize, &callback_); - EXPECT_EQ(kTestDataSize, result); -} - -// Verify that the message is still send if Write() is called while -// socket is not open yet, and that the callback is called. -TEST_F(TransportChannelSocketAdapterTest, WritePending) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize)); - - EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize)) - .Times(2) - .WillOnce(Return(SOCKET_ERROR)) - .WillOnce(Return(kTestDataSize)); - - EXPECT_CALL(channel_, GetError()) - .WillOnce(Return(EWOULDBLOCK)); - - int result = target_->Write(buffer, kTestDataSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - channel_.SignalWritableState(&channel_); - EXPECT_EQ(kTestDataSize, callback_result_); -} - -} // namespace remoting diff --git a/remoting/jingle_glue/jingle_glue_mock_objects.cc b/remoting/jingle_glue/jingle_glue_mock_objects.cc deleted file mode 100644 index 4ec59e7..0000000 --- a/remoting/jingle_glue/jingle_glue_mock_objects.cc +++ /dev/null @@ -1,13 +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/jingle_glue/jingle_glue_mock_objects.h" - -namespace remoting { - -MockStream::MockStream() {} - -MockStream::~MockStream() {} - -} // namespace remoting diff --git a/remoting/jingle_glue/jingle_glue_mock_objects.h b/remoting/jingle_glue/jingle_glue_mock_objects.h deleted file mode 100644 index 505e563..0000000 --- a/remoting/jingle_glue/jingle_glue_mock_objects.h +++ /dev/null @@ -1,32 +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_JINGLE_GLUE_JINGLE_GLUE_MOCK_OBJECTS_H_ -#define REMOTING_JINGLE_GLUE_JINGLE_GLUE_MOCK_OBJECTS_H_ - -#include "testing/gmock/include/gmock/gmock.h" -#include "third_party/libjingle/source/talk/base/stream.h" - -namespace remoting { - -class MockStream : public talk_base::StreamInterface { - public: - MockStream(); - virtual ~MockStream(); - - MOCK_CONST_METHOD0(GetState, talk_base::StreamState()); - - MOCK_METHOD4(Read, talk_base::StreamResult(void*, size_t, size_t*, int*)); - MOCK_METHOD4(Write, talk_base::StreamResult(const void*, size_t, - size_t*, int*)); - MOCK_CONST_METHOD1(GetAvailable, bool(size_t*)); - MOCK_METHOD0(Close, void()); - - MOCK_METHOD3(PostEvent, void(talk_base::Thread*, int, int)); - MOCK_METHOD2(PostEvent, void(int, int)); -}; - -} // namespace remoting - -#endif // REMOTING_JINGLE_GLUE_JINGLE_GLUE_MOCK_OBJECTS_H_ diff --git a/remoting/jingle_glue/ssl_socket_adapter.cc b/remoting/jingle_glue/ssl_socket_adapter.cc index ffde85e..251f771 100644 --- a/remoting/jingle_glue/ssl_socket_adapter.cc +++ b/remoting/jingle_glue/ssl_socket_adapter.cc @@ -15,7 +15,6 @@ #include "net/base/sys_addrinfo.h" #include "net/socket/client_socket_factory.h" #include "net/url_request/url_request_context.h" -#include "remoting/jingle_glue/utils.h" namespace remoting { @@ -306,7 +305,7 @@ int TransportSocket::Read(net::IOBuffer* buf, int buf_len, DCHECK(!read_buffer_.get()); int result = socket_->Recv(buf->data(), buf_len); if (result < 0) { - result = MapPosixToChromeError(socket_->GetError()); + result = net::MapSystemError(socket_->GetError()); if (result == net::ERR_IO_PENDING) { read_callback_ = callback; read_buffer_ = buf; @@ -325,7 +324,7 @@ int TransportSocket::Write(net::IOBuffer* buf, int buf_len, DCHECK(!write_buffer_.get()); int result = socket_->Send(buf->data(), buf_len); if (result < 0) { - result = MapPosixToChromeError(socket_->GetError()); + result = net::MapSystemError(socket_->GetError()); if (result == net::ERR_IO_PENDING) { write_callback_ = callback; write_buffer_ = buf; @@ -360,7 +359,7 @@ void TransportSocket::OnReadEvent(talk_base::AsyncSocket* socket) { int result = socket_->Recv(buffer->data(), buffer_len); if (result < 0) { - result = MapPosixToChromeError(socket_->GetError()); + result = net::MapSystemError(socket_->GetError()); if (result == net::ERR_IO_PENDING) { read_callback_ = callback; read_buffer_ = buffer; @@ -386,7 +385,7 @@ void TransportSocket::OnWriteEvent(talk_base::AsyncSocket* socket) { int result = socket_->Send(buffer->data(), buffer_len); if (result < 0) { - result = MapPosixToChromeError(socket_->GetError()); + result = net::MapSystemError(socket_->GetError()); if (result == net::ERR_IO_PENDING) { write_callback_ = callback; write_buffer_ = buffer; diff --git a/remoting/jingle_glue/stream_socket_adapter.cc b/remoting/jingle_glue/stream_socket_adapter.cc deleted file mode 100644 index 4c5e6cf..0000000 --- a/remoting/jingle_glue/stream_socket_adapter.cc +++ /dev/null @@ -1,238 +0,0 @@ -// Copyright (c) 2010 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/jingle_glue/stream_socket_adapter.h" - -#include "base/logging.h" -#include "base/message_loop.h" -#include "net/base/address_list.h" -#include "net/base/io_buffer.h" -#include "net/base/net_errors.h" -#include "remoting/jingle_glue/utils.h" -#include "third_party/libjingle/source/talk/base/stream.h" - -namespace remoting { - -StreamSocketAdapter::StreamSocketAdapter(talk_base::StreamInterface* stream) - : stream_(stream), - read_pending_(false), - write_pending_(false), - closed_error_code_(net::OK) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK(stream); - stream_->SignalEvent.connect(this, &StreamSocketAdapter::OnStreamEvent); -} - -StreamSocketAdapter::~StreamSocketAdapter() { -} - -int StreamSocketAdapter::Connect(net::CompletionCallback* callback) { - return net::OK; -} - -void StreamSocketAdapter::Disconnect() { -} - -bool StreamSocketAdapter::IsConnected() const { - return true; -} - -bool StreamSocketAdapter::IsConnectedAndIdle() const { - return true; -} - -int StreamSocketAdapter::GetPeerAddress(net::AddressList* address) const { - // We actually don't know the peer address. Returning so the upper layers - // won't complain. - net::IPAddressNumber ip_address(4); - *address = net::AddressList(ip_address, 0, false); - return net::OK; -} - -const net::BoundNetLog& StreamSocketAdapter::NetLog() const { - return net_log_; -} - -void StreamSocketAdapter::SetSubresourceSpeculation() { -} - -void StreamSocketAdapter::SetOmniboxSpeculation() { -} - -bool StreamSocketAdapter::WasEverUsed() const { - return true; -} - -bool StreamSocketAdapter::UsingTCPFastOpen() const { - return false; -} - -int StreamSocketAdapter::Read( - net::IOBuffer* buffer, int buffer_size, net::CompletionCallback* callback) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK(buffer); - CHECK(!read_pending_); - - if (!stream_.get()) { - DCHECK(closed_error_code_ != net::OK); - return closed_error_code_; - } - - int result = ReadStream(buffer, buffer_size); - if (result == net::ERR_CONNECTION_CLOSED && - stream_->GetState() == talk_base::SS_OPENING) - result = net::ERR_IO_PENDING; - if (result == net::ERR_IO_PENDING) { - read_pending_ = true; - read_callback_ = callback; - read_buffer_ = buffer; - read_buffer_size_ = buffer_size; - } - return result; -} - -int StreamSocketAdapter::Write( - net::IOBuffer* buffer, int buffer_size, net::CompletionCallback* callback) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - DCHECK(buffer); - CHECK(!write_pending_); - - if (!stream_.get()) { - DCHECK(closed_error_code_ != net::OK); - return closed_error_code_; - } - - int result = WriteStream(buffer, buffer_size); - if (result == net::ERR_CONNECTION_CLOSED && - stream_->GetState() == talk_base::SS_OPENING) - result = net::ERR_IO_PENDING; - - if (result == net::ERR_IO_PENDING) { - write_pending_ = true; - write_callback_ = callback; - write_buffer_ = buffer; - write_buffer_size_ = buffer_size; - } - return result; -} - -bool StreamSocketAdapter::SetReceiveBufferSize(int32 size) { - NOTIMPLEMENTED(); - return false; -} - -bool StreamSocketAdapter::SetSendBufferSize(int32 size) { - NOTIMPLEMENTED(); - return false; -} - -void StreamSocketAdapter::Close(int error_code) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - - if (!stream_.get()) // Already closed. - return; - - DCHECK(error_code != net::OK); - closed_error_code_ = error_code; - stream_->SignalEvent.disconnect(this); - stream_->Close(); - stream_.reset(NULL); - - if (read_pending_) { - net::CompletionCallback* callback = read_callback_; - read_pending_ = false; - read_buffer_ = NULL; - callback->Run(error_code); - } - - if (write_pending_) { - net::CompletionCallback* callback = write_callback_; - write_pending_ = false; - write_buffer_ = NULL; - callback->Run(error_code); - } -} - -void StreamSocketAdapter::OnStreamEvent( - talk_base::StreamInterface* stream, int events, int error) { - DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - - if (events & talk_base::SE_WRITE) - DoWrite(); - - if (events & talk_base::SE_READ) - DoRead(); -} - -void StreamSocketAdapter::DoWrite() { - // Write if there is a pending read. - if (write_buffer_) { - int result = WriteStream(write_buffer_, write_buffer_size_); - if (result != net::ERR_IO_PENDING) { - net::CompletionCallback* callback = write_callback_; - write_pending_ = false; - write_buffer_ = NULL; - callback->Run(result); - } - } -} - -void StreamSocketAdapter::DoRead() { - // Read if there is a pending read. - if (read_pending_) { - int result = ReadStream(read_buffer_, read_buffer_size_); - if (result != net::ERR_IO_PENDING) { - net::CompletionCallback* callback = read_callback_;\ - read_pending_ = false; - read_buffer_ = NULL; - callback->Run(result); - } - } -} - -int StreamSocketAdapter::ReadStream(net::IOBuffer* buffer, int buffer_size) { - size_t bytes_read; - int error; - talk_base::StreamResult result = stream_->Read( - buffer->data(), buffer_size, &bytes_read, &error); - switch (result) { - case talk_base::SR_SUCCESS: - return bytes_read; - - case talk_base::SR_BLOCK: - return net::ERR_IO_PENDING; - - case talk_base::SR_EOS: - return net::ERR_CONNECTION_CLOSED; - - case talk_base::SR_ERROR: - return MapPosixToChromeError(error); - } - NOTREACHED(); - return net::ERR_FAILED; -} - -int StreamSocketAdapter::WriteStream(net::IOBuffer* buffer, int buffer_size) { - size_t bytes_written; - int error; - talk_base::StreamResult result = stream_->Write( - buffer->data(), buffer_size, &bytes_written, &error); - switch (result) { - case talk_base::SR_SUCCESS: - return bytes_written; - - case talk_base::SR_BLOCK: - return net::ERR_IO_PENDING; - - case talk_base::SR_EOS: - return net::ERR_CONNECTION_CLOSED; - - case talk_base::SR_ERROR: - return MapPosixToChromeError(error); - } - NOTREACHED(); - return net::ERR_FAILED; -} - -} // namespace remoting diff --git a/remoting/jingle_glue/stream_socket_adapter.h b/remoting/jingle_glue/stream_socket_adapter.h deleted file mode 100644 index f9b274a..0000000 --- a/remoting/jingle_glue/stream_socket_adapter.h +++ /dev/null @@ -1,85 +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_JINGLE_GLUE_STREAM_SOCKET_ADAPTER_H_ -#define REMOTING_JINGLE_GLUE_STREAM_SOCKET_ADAPTER_H_ - -#include "base/memory/scoped_ptr.h" -#include "net/base/net_log.h" -#include "net/socket/client_socket.h" -#include "third_party/libjingle/source/talk/base/sigslot.h" - -namespace talk_base { -class StreamInterface; -} // namespace talk_base - -namespace remoting { - -// StreamSocketAdapter implements net::Socket interface on top of -// libjingle's StreamInterface. It is used by JingleChromotocolConnection -// to provide net::Socket interface for channels. -class StreamSocketAdapter : public net::ClientSocket, - public sigslot::has_slots<> { - public: - // Ownership of the stream is passed to the adapter. - explicit StreamSocketAdapter(talk_base::StreamInterface* stream); - virtual ~StreamSocketAdapter(); - - // ClientSocket interface. - virtual int Connect(net::CompletionCallback* callback); - virtual void Disconnect(); - virtual bool IsConnected() const; - virtual bool IsConnectedAndIdle() const; - virtual int GetPeerAddress(net::AddressList* address) const; - virtual const net::BoundNetLog& NetLog() const; - virtual void SetSubresourceSpeculation(); - virtual void SetOmniboxSpeculation(); - virtual bool WasEverUsed() const; - virtual bool UsingTCPFastOpen() const; - - // Closes the stream. |error_code| specifies error code that will - // be returned by Read() and Write() after the stream is closed. - void Close(int error_code); - - // Socket interface. - virtual int Read(net::IOBuffer* buffer, int buffer_size, - net::CompletionCallback* callback); - virtual int Write(net::IOBuffer* buffer, int buffer_size, - net::CompletionCallback* callback); - - virtual bool SetReceiveBufferSize(int32 size); - virtual bool SetSendBufferSize(int32 size); - - private: - void OnStreamEvent(talk_base::StreamInterface* stream, - int events, int error); - - void DoWrite(); - void DoRead(); - - int ReadStream(net::IOBuffer* buffer, int buffer_size); - int WriteStream(net::IOBuffer* buffer, int buffer_size); - - scoped_ptr<talk_base::StreamInterface> stream_; - - bool read_pending_; - net::CompletionCallback* read_callback_; - scoped_refptr<net::IOBuffer> read_buffer_; - int read_buffer_size_; - - bool write_pending_; - net::CompletionCallback* write_callback_; - scoped_refptr<net::IOBuffer> write_buffer_; - int write_buffer_size_; - - int closed_error_code_; - - net::BoundNetLog net_log_; - - DISALLOW_COPY_AND_ASSIGN(StreamSocketAdapter); -}; - -} // namespace remoting - -#endif // REMOTING_JINGLE_GLUE_STREAM_SOCKET_ADAPTER_H_ diff --git a/remoting/jingle_glue/stream_socket_adapter_unittest.cc b/remoting/jingle_glue/stream_socket_adapter_unittest.cc deleted file mode 100644 index 2d228db..0000000 --- a/remoting/jingle_glue/stream_socket_adapter_unittest.cc +++ /dev/null @@ -1,151 +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 "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop.h" -#include "net/base/io_buffer.h" -#include "net/base/net_errors.h" -#include "net/socket/socket.h" -#include "remoting/jingle_glue/stream_socket_adapter.h" -#include "remoting/jingle_glue/jingle_glue_mock_objects.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "third_party/libjingle/source/talk/p2p/base/transportchannel.h" - -using net::IOBuffer; - -using testing::_; -using testing::Return; -using testing::SetArgumentPointee; - -namespace remoting { - -namespace { -const int kBufferSize = 4096; -const int kTestDataSize = 4; -const int kTestError = -32123; -} // namespace - -class StreamSocketAdapterTest : public testing::Test { - public: - StreamSocketAdapterTest() - : ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &StreamSocketAdapterTest::Callback)), - callback_result_(0) { - stream_ = new MockStream(); - target_.reset(new StreamSocketAdapter(stream_)); - } - - protected: - void Callback(int result) { - callback_result_ = result; - } - - // |stream_| must be allocated on the heap, because StreamSocketAdapter - // owns the object and it will free it in the end. - MockStream* stream_; - scoped_ptr<StreamSocketAdapter> target_; - net::CompletionCallbackImpl<StreamSocketAdapterTest> callback_; - int callback_result_; - MessageLoopForIO message_loop_; -}; - -// Verify that Read() calls Read() in stream. -TEST_F(StreamSocketAdapterTest, Read) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - - EXPECT_CALL(*stream_, Read(buffer->data(), kBufferSize, _, _)) - .WillOnce(DoAll(SetArgumentPointee<2>(kTestDataSize), - Return(talk_base::SR_SUCCESS))); - - int result = target_->Read(buffer, kBufferSize, &callback_); - EXPECT_EQ(kTestDataSize, result); - EXPECT_EQ(0, callback_result_); -} - -// Verify that read callback is called for pending reads. -TEST_F(StreamSocketAdapterTest, ReadPending) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - - EXPECT_CALL(*stream_, Read(buffer->data(), kBufferSize, _, _)) - .Times(2) - .WillOnce(Return(talk_base::SR_BLOCK)) - .WillOnce(DoAll(SetArgumentPointee<2>(kTestDataSize), - Return(talk_base::SR_SUCCESS))); - - int result = target_->Read(buffer, kBufferSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - stream_->SignalEvent(stream_, talk_base::SE_READ, 0); - EXPECT_EQ(kTestDataSize, callback_result_); -} - -// Verify that Read() returns error after Close(). -TEST_F(StreamSocketAdapterTest, ReadClose) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - - EXPECT_CALL(*stream_, Read(buffer->data(), kBufferSize, _, _)) - .WillOnce(Return(talk_base::SR_BLOCK)); - - int result = target_->Read(buffer, kBufferSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - EXPECT_CALL(*stream_, Close()); - target_->Close(kTestError); - EXPECT_EQ(kTestError, callback_result_); - - // All Read() calls after Close() should return the error. - EXPECT_EQ(kTestError, target_->Read(buffer, kBufferSize, &callback_)); -} - -// Verify that Write() calls stream's Write() and returns result. -TEST_F(StreamSocketAdapterTest, Write) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize)); - - EXPECT_CALL(*stream_, Write(buffer->data(), kTestDataSize, _, _)) - .WillOnce(DoAll(SetArgumentPointee<2>(kTestDataSize), - Return(talk_base::SR_SUCCESS))); - - int result = target_->Write(buffer, kTestDataSize, &callback_); - EXPECT_EQ(kTestDataSize, result); - EXPECT_EQ(0, callback_result_); -} - -// Verify that write callback is called for pending writes. -TEST_F(StreamSocketAdapterTest, WritePending) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize)); - - EXPECT_CALL(*stream_, Write(buffer->data(), kTestDataSize, _, _)) - .Times(2) - .WillOnce(Return(talk_base::SR_BLOCK)) - .WillOnce(DoAll(SetArgumentPointee<2>(kTestDataSize), - Return(talk_base::SR_SUCCESS))); - - int result = target_->Write(buffer, kTestDataSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - stream_->SignalEvent(stream_, talk_base::SE_WRITE, 0); - EXPECT_EQ(kTestDataSize, callback_result_); -} - -// Verify that Write() returns error after Close(). -TEST_F(StreamSocketAdapterTest, WriteClose) { - scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize)); - - EXPECT_CALL(*stream_, Write(buffer->data(), kTestDataSize, _, _)) - .WillOnce(Return(talk_base::SR_BLOCK)); - - int result = target_->Write(buffer, kTestDataSize, &callback_); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - EXPECT_CALL(*stream_, Close()); - target_->Close(kTestError); - EXPECT_EQ(kTestError, callback_result_); - - // All Write() calls after Close() should return the error. - EXPECT_EQ(kTestError, target_->Write(buffer, kTestError, &callback_)); -} - -} // namespace remoting diff --git a/remoting/jingle_glue/utils.cc b/remoting/jingle_glue/utils.cc deleted file mode 100644 index b37d734..0000000 --- a/remoting/jingle_glue/utils.cc +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) 2010 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/jingle_glue/utils.h" - -#include "base/logging.h" -#include "net/base/net_errors.h" -#include "third_party/libjingle/source/talk/base/socket.h" - -namespace remoting { - -// TODO(sergeyu): This is a clone of MapPosixError() from -// net/socket/tcp_client_socket_libevent.cc. Move MapPosixError() to -// net/base/net_errors.cc and use it here. - -// Convert values from <errno.h> to values from "net/base/net_errors.h" -int MapPosixToChromeError(int err) { - // There are numerous posix error codes, but these are the ones we thus far - // find interesting. - switch (err) { - case EAGAIN: -#if EWOULDBLOCK != EAGAIN - case EWOULDBLOCK: -#endif - return net::ERR_IO_PENDING; - case ENETDOWN: - return net::ERR_INTERNET_DISCONNECTED; - case ETIMEDOUT: - return net::ERR_TIMED_OUT; - case ENOTCONN: - return net::ERR_CONNECTION_CLOSED; - case ECONNRESET: - case ENETRESET: // Related to keep-alive - return net::ERR_CONNECTION_RESET; - case ECONNABORTED: - return net::ERR_CONNECTION_ABORTED; - case ECONNREFUSED: - return net::ERR_CONNECTION_REFUSED; - case EHOSTUNREACH: - case ENETUNREACH: - return net::ERR_ADDRESS_UNREACHABLE; - case EADDRNOTAVAIL: - return net::ERR_ADDRESS_INVALID; - case 0: - return net::OK; - default: - LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED"; - return net::ERR_FAILED; - } -} - -} // namespace remoting diff --git a/remoting/jingle_glue/utils.h b/remoting/jingle_glue/utils.h deleted file mode 100644 index 044a9ed..0000000 --- a/remoting/jingle_glue/utils.h +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2010 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_JINGLE_GLUE_UTILS_H_ -#define REMOTING_JINGLE_GLUE_UTILS_H_ - -namespace remoting { - -// Convert values from <errno.h> to values from "net/base/net_errors.h" -int MapPosixToChromeError(int err); - -} // namespace remoting - -#endif // REMOTING_JINGLE_GLUE_UTILS_H_ diff --git a/remoting/protocol/DEPS b/remoting/protocol/DEPS index 82b462c..b1c16e3 100644 --- a/remoting/protocol/DEPS +++ b/remoting/protocol/DEPS @@ -1,6 +1,7 @@ include_rules = [ "+google/protobuf", - "+remoting/jingle_glue", + "+jingle/glue", "+net", + "+remoting/jingle_glue", "+third_party/protobuf/src", ] diff --git a/remoting/protocol/jingle_session.cc b/remoting/protocol/jingle_session.cc index c1adf14..8966363 100644 --- a/remoting/protocol/jingle_session.cc +++ b/remoting/protocol/jingle_session.cc @@ -6,6 +6,8 @@ #include "base/crypto/rsa_private_key.h" #include "base/message_loop.h" +#include "jingle/glue/channel_socket_adapter.h" +#include "jingle/glue/stream_socket_adapter.h" #include "net/base/cert_status_flags.h" #include "net/base/cert_verifier.h" #include "net/base/host_port_pair.h" @@ -16,9 +18,7 @@ #include "net/socket/ssl_client_socket.h" #include "net/socket/ssl_server_socket.h" #include "remoting/base/constants.h" -#include "remoting/jingle_glue/channel_socket_adapter.h" #include "remoting/jingle_glue/jingle_thread.h" -#include "remoting/jingle_glue/stream_socket_adapter.h" #include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/socket_wrapper.h" #include "third_party/libjingle/source/talk/base/thread.h" @@ -365,9 +365,9 @@ void JingleSession::OnInitiate() { } // Create video RTP channels. - video_rtp_channel_.reset(new TransportChannelSocketAdapter( + video_rtp_channel_.reset(new jingle_glue::TransportChannelSocketAdapter( cricket_session_->CreateChannel(content_name, kVideoRtpChannelName))); - video_rtcp_channel_.reset(new TransportChannelSocketAdapter( + video_rtcp_channel_.reset(new jingle_glue::TransportChannelSocketAdapter( cricket_session_->CreateChannel(content_name, kVideoRtcpChannelName))); // Create control channel. @@ -376,7 +376,7 @@ void JingleSession::OnInitiate() { control_channel_->Connect(content_name, kControlChannelName); control_channel_->SetOption(PseudoTcp::OPT_NODELAY, kEnableNoDelay); control_channel_->SetOption(PseudoTcp::OPT_ACKDELAY, kDelayedAckTimeoutMs); - control_channel_adapter_.reset(new StreamSocketAdapter( + control_channel_adapter_.reset(new jingle_glue::StreamSocketAdapter( control_channel_->GetStream())); // Create event channel. @@ -385,7 +385,7 @@ void JingleSession::OnInitiate() { event_channel_->Connect(content_name, kEventChannelName); event_channel_->SetOption(PseudoTcp::OPT_NODELAY, kEnableNoDelay); event_channel_->SetOption(PseudoTcp::OPT_ACKDELAY, kDelayedAckTimeoutMs); - event_channel_adapter_.reset(new StreamSocketAdapter( + event_channel_adapter_.reset(new jingle_glue::StreamSocketAdapter( event_channel_->GetStream())); // Create video channel. @@ -395,7 +395,7 @@ void JingleSession::OnInitiate() { video_channel_->Connect(content_name, kVideoChannelName); video_channel_->SetOption(PseudoTcp::OPT_NODELAY, kEnableNoDelay); video_channel_->SetOption(PseudoTcp::OPT_ACKDELAY, kDelayedAckTimeoutMs); - video_channel_adapter_.reset(new StreamSocketAdapter( + video_channel_adapter_.reset(new jingle_glue::StreamSocketAdapter( video_channel_->GetStream())); if (!cricket_session_->initiator()) diff --git a/remoting/protocol/jingle_session.h b/remoting/protocol/jingle_session.h index 099dc4f6..74b0efe 100644 --- a/remoting/protocol/jingle_session.h +++ b/remoting/protocol/jingle_session.h @@ -16,6 +16,11 @@ namespace cricket { class PseudoTcpChannel; } // namespace cricket +namespace jingle_glue { +class StreamSocketAdapter; +class TransportChannelSocketAdapter; +} // namespace jingle_glue + namespace net { class CertVerifier; class ClientSocket; @@ -26,9 +31,6 @@ class X509Certificate; namespace remoting { -class StreamSocketAdapter; -class TransportChannelSocketAdapter; - namespace protocol { class JingleSessionManager; @@ -155,15 +157,15 @@ class JingleSession : public protocol::Session, // then there is a SocketWrapper created over net::Socket. // SSL socket uses SocketWrapper to provide SSL functionality. cricket::PseudoTcpChannel* control_channel_; - scoped_ptr<StreamSocketAdapter> control_channel_adapter_; + scoped_ptr<jingle_glue::StreamSocketAdapter> control_channel_adapter_; scoped_ptr<SocketWrapper> control_ssl_socket_; cricket::PseudoTcpChannel* event_channel_; - scoped_ptr<StreamSocketAdapter> event_channel_adapter_; + scoped_ptr<jingle_glue::StreamSocketAdapter> event_channel_adapter_; scoped_ptr<SocketWrapper> event_ssl_socket_; cricket::PseudoTcpChannel* video_channel_; - scoped_ptr<StreamSocketAdapter> video_channel_adapter_; + scoped_ptr<jingle_glue::StreamSocketAdapter> video_channel_adapter_; scoped_ptr<SocketWrapper> video_ssl_socket_; // Count the number of SSL connections esblished. @@ -172,8 +174,8 @@ class JingleSession : public protocol::Session, // Used to verify the certificate received in SSLClientSocket. scoped_ptr<net::CertVerifier> cert_verifier_; - scoped_ptr<TransportChannelSocketAdapter> video_rtp_channel_; - scoped_ptr<TransportChannelSocketAdapter> video_rtcp_channel_; + scoped_ptr<jingle_glue::TransportChannelSocketAdapter> video_rtp_channel_; + scoped_ptr<jingle_glue::TransportChannelSocketAdapter> video_rtcp_channel_; // Callback called by the SSL layer. scoped_ptr<net::CompletionCallback> connect_callback_; diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index a8edaf5..6f1e88c 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -362,8 +362,6 @@ '../third_party/libjingle/libjingle.gyp:libjingle_p2p', ], 'sources': [ - 'jingle_glue/channel_socket_adapter.cc', - 'jingle_glue/channel_socket_adapter.h', 'jingle_glue/http_port_allocator.cc', 'jingle_glue/http_port_allocator.h', 'jingle_glue/iq_request.cc', @@ -372,14 +370,10 @@ 'jingle_glue/jingle_client.h', 'jingle_glue/jingle_thread.cc', 'jingle_glue/jingle_thread.h', - 'jingle_glue/stream_socket_adapter.cc', - 'jingle_glue/stream_socket_adapter.h', 'jingle_glue/ssl_adapter.h', 'jingle_glue/ssl_adapter.cc', 'jingle_glue/ssl_socket_adapter.cc', 'jingle_glue/ssl_socket_adapter.h', - 'jingle_glue/utils.cc', - 'jingle_glue/utils.h', 'jingle_glue/xmpp_proxy.h', 'jingle_glue/xmpp_socket_adapter.cc', 'jingle_glue/xmpp_socket_adapter.h', @@ -392,6 +386,7 @@ 'dependencies': [ 'chromoting_base', 'chromoting_jingle_glue', + '../jingle/jingle.gyp:jingle_glue', ], 'export_dependent_settings': [ 'chromoting_jingle_glue', @@ -560,13 +555,9 @@ 'host/json_host_config_unittest.cc', 'host/screen_recorder_unittest.cc', 'host/test_key_pair.h', - 'jingle_glue/channel_socket_adapter_unittest.cc', 'jingle_glue/iq_request_unittest.cc', 'jingle_glue/jingle_client_unittest.cc', - 'jingle_glue/jingle_glue_mock_objects.cc', - 'jingle_glue/jingle_glue_mock_objects.h', 'jingle_glue/jingle_thread_unittest.cc', - 'jingle_glue/stream_socket_adapter_unittest.cc', 'protocol/connection_to_client_unittest.cc', 'protocol/fake_session.cc', 'protocol/fake_session.h', |