Implement GoogleURLTrackerFactory and GoogleURLTrackerClient on iOS
[chromium-blink-merge.git] / remoting / protocol / fake_datagram_socket.cc
blob9bd4ede48b336e0356aebfa8b7342676f68dd1cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/protocol/fake_datagram_socket.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/base/address_list.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace remoting {
18 namespace protocol {
20 FakeDatagramSocket::FakeDatagramSocket()
21 : input_pos_(0),
22 task_runner_(base::ThreadTaskRunnerHandle::Get()),
23 weak_factory_(this) {
26 FakeDatagramSocket::~FakeDatagramSocket() {
27 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
30 void FakeDatagramSocket::AppendInputPacket(const std::string& data) {
31 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
32 input_packets_.push_back(data);
34 // Complete pending read if any.
35 if (!read_callback_.is_null()) {
36 DCHECK_EQ(input_pos_, static_cast<int>(input_packets_.size()) - 1);
37 int result = CopyReadData(read_buffer_.get(), read_buffer_size_);
38 read_buffer_ = nullptr;
40 base::ResetAndReturn(&read_callback_).Run(result);
44 void FakeDatagramSocket::PairWith(FakeDatagramSocket* peer_socket) {
45 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
46 peer_socket_ = peer_socket->GetWeakPtr();
47 peer_socket->peer_socket_ = GetWeakPtr();
50 base::WeakPtr<FakeDatagramSocket> FakeDatagramSocket::GetWeakPtr() {
51 return weak_factory_.GetWeakPtr();
54 int FakeDatagramSocket::Read(net::IOBuffer* buf, int buf_len,
55 const net::CompletionCallback& callback) {
56 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
57 if (input_pos_ < static_cast<int>(input_packets_.size())) {
58 return CopyReadData(buf, buf_len);
59 } else {
60 read_buffer_ = buf;
61 read_buffer_size_ = buf_len;
62 read_callback_ = callback;
63 return net::ERR_IO_PENDING;
67 int FakeDatagramSocket::Write(net::IOBuffer* buf, int buf_len,
68 const net::CompletionCallback& callback) {
69 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
70 written_packets_.push_back(std::string());
71 written_packets_.back().assign(buf->data(), buf->data() + buf_len);
73 if (peer_socket_.get()) {
74 task_runner_->PostTask(
75 FROM_HERE,
76 base::Bind(&FakeDatagramSocket::AppendInputPacket,
77 peer_socket_,
78 std::string(buf->data(), buf->data() + buf_len)));
81 return buf_len;
84 int FakeDatagramSocket::SetReceiveBufferSize(int32 size) {
85 NOTIMPLEMENTED();
86 return net::ERR_NOT_IMPLEMENTED;
89 int FakeDatagramSocket::SetSendBufferSize(int32 size) {
90 NOTIMPLEMENTED();
91 return net::ERR_NOT_IMPLEMENTED;
94 int FakeDatagramSocket::CopyReadData(net::IOBuffer* buf, int buf_len) {
95 int size = std::min(
96 buf_len, static_cast<int>(input_packets_[input_pos_].size()));
97 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), size);
98 ++input_pos_;
99 return size;
102 FakeDatagramChannelFactory::FakeDatagramChannelFactory()
103 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
104 asynchronous_create_(false),
105 fail_create_(false),
106 weak_factory_(this) {
109 FakeDatagramChannelFactory::~FakeDatagramChannelFactory() {
110 for (ChannelsMap::iterator it = channels_.begin(); it != channels_.end();
111 ++it) {
112 EXPECT_TRUE(it->second == nullptr);
116 void FakeDatagramChannelFactory::PairWith(
117 FakeDatagramChannelFactory* peer_factory) {
118 peer_factory_ = peer_factory->weak_factory_.GetWeakPtr();
119 peer_factory_->peer_factory_ = weak_factory_.GetWeakPtr();
122 FakeDatagramSocket* FakeDatagramChannelFactory::GetFakeChannel(
123 const std::string& name) {
124 return channels_[name].get();
127 void FakeDatagramChannelFactory::CreateChannel(
128 const std::string& name,
129 const ChannelCreatedCallback& callback) {
130 EXPECT_TRUE(channels_[name] == nullptr);
132 scoped_ptr<FakeDatagramSocket> channel(new FakeDatagramSocket());
133 channels_[name] = channel->GetWeakPtr();
135 if (peer_factory_) {
136 FakeDatagramSocket* peer_socket = peer_factory_->GetFakeChannel(name);
137 if (peer_socket)
138 channel->PairWith(peer_socket);
141 if (fail_create_)
142 channel.reset();
144 if (asynchronous_create_) {
145 task_runner_->PostTask(
146 FROM_HERE,
147 base::Bind(&FakeDatagramChannelFactory::NotifyChannelCreated,
148 weak_factory_.GetWeakPtr(), base::Passed(&channel),
149 name, callback));
150 } else {
151 NotifyChannelCreated(channel.Pass(), name, callback);
155 void FakeDatagramChannelFactory::NotifyChannelCreated(
156 scoped_ptr<FakeDatagramSocket> owned_socket,
157 const std::string& name,
158 const ChannelCreatedCallback& callback) {
159 if (channels_.find(name) != channels_.end())
160 callback.Run(owned_socket.Pass());
163 void FakeDatagramChannelFactory::CancelChannelCreation(
164 const std::string& name) {
165 channels_.erase(name);
168 } // namespace protocol
169 } // namespace remoting