| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "content/common/p2p_messages.h" | 10 #include "content/common/p2p_messages.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 public: | 48 public: |
| 49 FakeSocket(std::string* written_data); | 49 FakeSocket(std::string* written_data); |
| 50 virtual ~FakeSocket(); | 50 virtual ~FakeSocket(); |
| 51 | 51 |
| 52 void AppendInputData(const char* data, int data_size); | 52 void AppendInputData(const char* data, int data_size); |
| 53 int input_pos() const { return input_pos_; } | 53 int input_pos() const { return input_pos_; } |
| 54 bool read_pending() const { return read_pending_; } | 54 bool read_pending() const { return read_pending_; } |
| 55 void SetPeerAddress(const net::IPEndPoint& peer_address); | 55 void SetPeerAddress(const net::IPEndPoint& peer_address); |
| 56 void SetLocalAddress(const net::IPEndPoint& local_address); | 56 void SetLocalAddress(const net::IPEndPoint& local_address); |
| 57 | 57 |
| 58 // net::Socket interface. | 58 // net::Socket implementation. |
| 59 virtual int Read(net::IOBuffer* buf, int buf_len, | 59 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 60 net::OldCompletionCallback* callback) OVERRIDE; | 60 net::OldCompletionCallback* callback) OVERRIDE; |
| 61 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 62 const net::CompletionCallback& callback) OVERRIDE; |
| 61 virtual int Write(net::IOBuffer* buf, int buf_len, | 63 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 62 net::OldCompletionCallback* callback) OVERRIDE; | 64 net::OldCompletionCallback* callback) OVERRIDE; |
| 63 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; | 65 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; |
| 64 virtual bool SetSendBufferSize(int32 size) OVERRIDE; | 66 virtual bool SetSendBufferSize(int32 size) OVERRIDE; |
| 65 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; | 67 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; |
| 66 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; | 68 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; |
| 67 virtual void Disconnect() OVERRIDE; | 69 virtual void Disconnect() OVERRIDE; |
| 68 virtual bool IsConnected() const OVERRIDE; | 70 virtual bool IsConnected() const OVERRIDE; |
| 69 virtual bool IsConnectedAndIdle() const OVERRIDE; | 71 virtual bool IsConnectedAndIdle() const OVERRIDE; |
| 70 virtual int GetPeerAddress(net::AddressList* address) const OVERRIDE; | 72 virtual int GetPeerAddress(net::AddressList* address) const OVERRIDE; |
| 71 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; | 73 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; |
| 72 virtual const net::BoundNetLog& NetLog() const OVERRIDE; | 74 virtual const net::BoundNetLog& NetLog() const OVERRIDE; |
| 73 virtual void SetSubresourceSpeculation() OVERRIDE; | 75 virtual void SetSubresourceSpeculation() OVERRIDE; |
| 74 virtual void SetOmniboxSpeculation() OVERRIDE; | 76 virtual void SetOmniboxSpeculation() OVERRIDE; |
| 75 virtual bool WasEverUsed() const OVERRIDE; | 77 virtual bool WasEverUsed() const OVERRIDE; |
| 76 virtual bool UsingTCPFastOpen() const OVERRIDE; | 78 virtual bool UsingTCPFastOpen() const OVERRIDE; |
| 77 virtual int64 NumBytesRead() const OVERRIDE; | 79 virtual int64 NumBytesRead() const OVERRIDE; |
| 78 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; | 80 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; |
| 79 | 81 |
| 80 private: | 82 private: |
| 81 bool read_pending_; | 83 bool read_pending_; |
| 82 scoped_refptr<net::IOBuffer> read_buffer_; | 84 scoped_refptr<net::IOBuffer> read_buffer_; |
| 83 int read_buffer_size_; | 85 int read_buffer_size_; |
| 84 net::OldCompletionCallback* read_callback_; | 86 net::OldCompletionCallback* old_read_callback_; |
| 87 net::CompletionCallback read_callback_; |
| 85 | 88 |
| 86 std::string* written_data_; | 89 std::string* written_data_; |
| 87 std::string input_data_; | 90 std::string input_data_; |
| 88 int input_pos_; | 91 int input_pos_; |
| 89 | 92 |
| 90 net::IPEndPoint peer_address_; | 93 net::IPEndPoint peer_address_; |
| 91 net::IPEndPoint local_address_; | 94 net::IPEndPoint local_address_; |
| 92 | 95 |
| 93 net::BoundNetLog net_log_; | 96 net::BoundNetLog net_log_; |
| 94 }; | 97 }; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 input_data_.insert(input_data_.end(), data, data + data_size); | 108 input_data_.insert(input_data_.end(), data, data + data_size); |
| 106 // Complete pending read if any. | 109 // Complete pending read if any. |
| 107 if (read_pending_) { | 110 if (read_pending_) { |
| 108 read_pending_ = false; | 111 read_pending_ = false; |
| 109 int result = std::min(read_buffer_size_, | 112 int result = std::min(read_buffer_size_, |
| 110 static_cast<int>(input_data_.size() - input_pos_)); | 113 static_cast<int>(input_data_.size() - input_pos_)); |
| 111 CHECK(result > 0); | 114 CHECK(result > 0); |
| 112 memcpy(read_buffer_->data(), &input_data_[0] + input_pos_, result); | 115 memcpy(read_buffer_->data(), &input_data_[0] + input_pos_, result); |
| 113 input_pos_ += result; | 116 input_pos_ += result; |
| 114 read_buffer_ = NULL; | 117 read_buffer_ = NULL; |
| 115 net::OldCompletionCallback* cb = read_callback_; | 118 if (old_read_callback_) { |
| 116 read_callback_ = NULL; | 119 net::OldCompletionCallback* cb = old_read_callback_; |
| 117 cb->Run(result); | 120 old_read_callback_ = NULL; |
| 121 cb->Run(result); |
| 122 } else { |
| 123 net::CompletionCallback cb = read_callback_; |
| 124 read_callback_.Reset(); |
| 125 cb.Run(result); |
| 126 } |
| 118 } | 127 } |
| 119 } | 128 } |
| 120 | 129 |
| 121 void FakeSocket::SetPeerAddress(const net::IPEndPoint& peer_address) { | 130 void FakeSocket::SetPeerAddress(const net::IPEndPoint& peer_address) { |
| 122 peer_address_ = peer_address; | 131 peer_address_ = peer_address; |
| 123 } | 132 } |
| 124 | 133 |
| 125 void FakeSocket::SetLocalAddress(const net::IPEndPoint& local_address) { | 134 void FakeSocket::SetLocalAddress(const net::IPEndPoint& local_address) { |
| 126 local_address_ = local_address; | 135 local_address_ = local_address; |
| 127 } | 136 } |
| 128 | 137 |
| 129 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, | 138 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
| 130 net::OldCompletionCallback* callback) { | 139 net::OldCompletionCallback* callback) { |
| 131 DCHECK(buf); | 140 DCHECK(buf); |
| 132 if (input_pos_ < static_cast<int>(input_data_.size())){ | 141 if (input_pos_ < static_cast<int>(input_data_.size())){ |
| 133 int result = std::min(buf_len, | 142 int result = std::min(buf_len, |
| 134 static_cast<int>(input_data_.size()) - input_pos_); | 143 static_cast<int>(input_data_.size()) - input_pos_); |
| 135 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 144 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
| 136 input_pos_ += result; | 145 input_pos_ += result; |
| 137 return result; | 146 return result; |
| 138 } else { | 147 } else { |
| 139 read_pending_ = true; | 148 read_pending_ = true; |
| 140 read_buffer_ = buf; | 149 read_buffer_ = buf; |
| 141 read_buffer_size_ = buf_len; | 150 read_buffer_size_ = buf_len; |
| 151 old_read_callback_ = callback; |
| 152 return net::ERR_IO_PENDING; |
| 153 } |
| 154 } |
| 155 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
| 156 const net::CompletionCallback& callback) { |
| 157 DCHECK(buf); |
| 158 if (input_pos_ < static_cast<int>(input_data_.size())){ |
| 159 int result = std::min(buf_len, |
| 160 static_cast<int>(input_data_.size()) - input_pos_); |
| 161 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
| 162 input_pos_ += result; |
| 163 return result; |
| 164 } else { |
| 165 read_pending_ = true; |
| 166 read_buffer_ = buf; |
| 167 read_buffer_size_ = buf_len; |
| 142 read_callback_ = callback; | 168 read_callback_ = callback; |
| 143 return net::ERR_IO_PENDING; | 169 return net::ERR_IO_PENDING; |
| 144 } | 170 } |
| 145 } | 171 } |
| 146 | 172 |
| 147 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 173 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
| 148 net::OldCompletionCallback* callback) { | 174 net::OldCompletionCallback* callback) { |
| 149 DCHECK(buf); | 175 DCHECK(buf); |
| 150 if (written_data_) { | 176 if (written_data_) { |
| 151 written_data_->insert(written_data_->end(), | 177 written_data_->insert(written_data_->end(), |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 return false; | 304 return false; |
| 279 P2PMsg_OnIncomingTcpConnection::Param params; | 305 P2PMsg_OnIncomingTcpConnection::Param params; |
| 280 P2PMsg_OnIncomingTcpConnection::Read( | 306 P2PMsg_OnIncomingTcpConnection::Read( |
| 281 arg, ¶ms); | 307 arg, ¶ms); |
| 282 return params.b == address; | 308 return params.b == address; |
| 283 } | 309 } |
| 284 | 310 |
| 285 } // namespace | 311 } // namespace |
| 286 | 312 |
| 287 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ | 313 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ |
| OLD | NEW |