Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / protocol / fake_datagram_socket.cc
blob53d31d098276420b140092c0add0c4a22a479eaa
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::Recv(const scoped_refptr<net::IOBuffer>& buf,
55 int buf_len,
56 const net::CompletionCallback& callback) {
57 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
58 if (input_pos_ < static_cast<int>(input_packets_.size())) {
59 return CopyReadData(buf, buf_len);
60 } else {
61 read_buffer_ = buf;
62 read_buffer_size_ = buf_len;
63 read_callback_ = callback;
64 return net::ERR_IO_PENDING;
68 int FakeDatagramSocket::Send(const scoped_refptr<net::IOBuffer>& buf,
69 int buf_len,
70 const net::CompletionCallback& callback) {
71 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
72 written_packets_.push_back(std::string());
73 written_packets_.back().assign(buf->data(), buf->data() + buf_len);
75 if (peer_socket_.get()) {
76 task_runner_->PostTask(
77 FROM_HERE,
78 base::Bind(&FakeDatagramSocket::AppendInputPacket, peer_socket_,
79 std::string(buf->data(), buf->data() + buf_len)));
82 return buf_len;
85 int FakeDatagramSocket::CopyReadData(const scoped_refptr<net::IOBuffer>& buf,
86 int buf_len) {
87 int size = std::min(
88 buf_len, static_cast<int>(input_packets_[input_pos_].size()));
89 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), size);
90 ++input_pos_;
91 return size;
94 FakeDatagramChannelFactory::FakeDatagramChannelFactory()
95 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
96 asynchronous_create_(false),
97 fail_create_(false),
98 weak_factory_(this) {
101 FakeDatagramChannelFactory::~FakeDatagramChannelFactory() {
102 for (ChannelsMap::iterator it = channels_.begin(); it != channels_.end();
103 ++it) {
104 EXPECT_TRUE(it->second == nullptr);
108 void FakeDatagramChannelFactory::PairWith(
109 FakeDatagramChannelFactory* peer_factory) {
110 peer_factory_ = peer_factory->weak_factory_.GetWeakPtr();
111 peer_factory_->peer_factory_ = weak_factory_.GetWeakPtr();
114 FakeDatagramSocket* FakeDatagramChannelFactory::GetFakeChannel(
115 const std::string& name) {
116 return channels_[name].get();
119 void FakeDatagramChannelFactory::CreateChannel(
120 const std::string& name,
121 const ChannelCreatedCallback& callback) {
122 EXPECT_TRUE(channels_[name] == nullptr);
124 scoped_ptr<FakeDatagramSocket> channel(new FakeDatagramSocket());
125 channels_[name] = channel->GetWeakPtr();
127 if (peer_factory_) {
128 FakeDatagramSocket* peer_socket = peer_factory_->GetFakeChannel(name);
129 if (peer_socket)
130 channel->PairWith(peer_socket);
133 if (fail_create_)
134 channel.reset();
136 if (asynchronous_create_) {
137 task_runner_->PostTask(
138 FROM_HERE,
139 base::Bind(&FakeDatagramChannelFactory::NotifyChannelCreated,
140 weak_factory_.GetWeakPtr(), base::Passed(&channel),
141 name, callback));
142 } else {
143 NotifyChannelCreated(channel.Pass(), name, callback);
147 void FakeDatagramChannelFactory::NotifyChannelCreated(
148 scoped_ptr<FakeDatagramSocket> owned_socket,
149 const std::string& name,
150 const ChannelCreatedCallback& callback) {
151 if (channels_.find(name) != channels_.end())
152 callback.Run(owned_socket.Pass());
155 void FakeDatagramChannelFactory::CancelChannelCreation(
156 const std::string& name) {
157 channels_.erase(name);
160 } // namespace protocol
161 } // namespace remoting