Ensure that media requests from Chrome Extension/WebUI pages can be auto-granted
[chromium-blink-merge.git] / remoting / test / fake_socket_factory.h
blob9a4366dddafb830b8070869d15e952e9123f76f5
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 #ifndef REMOTING_TEST_FAKE_SOCKET_FACTORY_H_
6 #define REMOTING_TEST_FAKE_SOCKET_FACTORY_H_
8 #include <list>
10 #include "base/callback_forward.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "remoting/test/fake_network_dispatcher.h"
14 #include "third_party/webrtc/p2p/base/packetsocketfactory.h"
16 namespace remoting {
18 class FakeNetworkDispatcher;
19 class LeakyBucket;
21 class FakePacketSocketFactory : public rtc::PacketSocketFactory,
22 public FakeNetworkDispatcher::Node {
23 public:
24 // |dispatcher| must outlive the factory.
25 explicit FakePacketSocketFactory(FakeNetworkDispatcher* dispatcher);
26 ~FakePacketSocketFactory() override;
28 void OnSocketDestroyed(int port);
30 // |bandwidth| - simulated link bandwidth in bytes/second. 0 indicates that
31 // bandwidth is unlimited.
32 // |max_buffer| - size of buffers in bytes. Ignored when |bandwidth| is 0.
33 void SetBandwidth(int bandwidth, int max_buffer);
35 // Specifies parameters for simulated network latency. Latency is generated
36 // with normal distribution around |average| with the given |stddev|. Random
37 // latency calculated based on these parameters is added to the buffering
38 // delay (which is calculated based on the parameters passed to
39 // SetBandwidth()). I.e. total latency for each packet is calculated using the
40 // following formula
42 // l = NormalRand(average, stddev) + bytes_buffered / bandwidth .
44 // Where bytes_buffered is the current level in the leaky bucket used to
45 // control bandwidth.
46 void SetLatency(base::TimeDelta average, base::TimeDelta stddev);
48 void set_out_of_order_rate(double out_of_order_rate) {
49 out_of_order_rate_ = out_of_order_rate;
52 // rtc::PacketSocketFactory interface.
53 rtc::AsyncPacketSocket* CreateUdpSocket(
54 const rtc::SocketAddress& local_address,
55 uint16 min_port,
56 uint16 max_port) override;
57 rtc::AsyncPacketSocket* CreateServerTcpSocket(
58 const rtc::SocketAddress& local_address,
59 uint16 min_port,
60 uint16 max_port,
61 int opts) override;
62 rtc::AsyncPacketSocket* CreateClientTcpSocket(
63 const rtc::SocketAddress& local_address,
64 const rtc::SocketAddress& remote_address,
65 const rtc::ProxyInfo& proxy_info,
66 const std::string& user_agent,
67 int opts) override;
68 rtc::AsyncResolverInterface* CreateAsyncResolver() override;
70 // FakeNetworkDispatcher::Node interface.
71 const scoped_refptr<base::SingleThreadTaskRunner>& GetThread() const override;
72 const rtc::IPAddress& GetAddress() const override;
73 void ReceivePacket(const rtc::SocketAddress& from,
74 const rtc::SocketAddress& to,
75 const scoped_refptr<net::IOBuffer>& data,
76 int data_size) override;
78 private:
79 struct PendingPacket {
80 PendingPacket();
81 PendingPacket(
82 const rtc::SocketAddress& from,
83 const rtc::SocketAddress& to,
84 const scoped_refptr<net::IOBuffer>& data,
85 int data_size);
86 ~PendingPacket();
88 rtc::SocketAddress from;
89 rtc::SocketAddress to;
90 scoped_refptr<net::IOBuffer> data;
91 int data_size;
94 typedef base::Callback<void(const rtc::SocketAddress& from,
95 const rtc::SocketAddress& to,
96 const scoped_refptr<net::IOBuffer>& data,
97 int data_size)> ReceiveCallback;
98 typedef std::map<uint16_t, ReceiveCallback> UdpSocketsMap;
100 void DoReceivePacket();
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
103 scoped_refptr<FakeNetworkDispatcher> dispatcher_;
105 rtc::IPAddress address_;
107 scoped_ptr<LeakyBucket> leaky_bucket_;
108 base::TimeDelta latency_average_;
109 base::TimeDelta latency_stddev_;
110 double out_of_order_rate_;
112 UdpSocketsMap udp_sockets_;
113 uint16_t next_port_;
115 std::list<PendingPacket> pending_packets_;
117 base::WeakPtrFactory<FakePacketSocketFactory> weak_factory_;
119 DISALLOW_COPY_AND_ASSIGN(FakePacketSocketFactory);
122 } // namespace remoting
124 #endif // REMOTING_TEST_FAKE_SOCKET_FACTORY_H_