summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/p2p/socket_dispatcher_host.h
blob: 5b4ef05c2c0b597c40c40388271f823e2aa7e07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2012 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 CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_DISPATCHER_HOST_H_
#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_DISPATCHER_HOST_H_

#include <stdint.h>

#include <map>
#include <set>
#include <string>
#include <vector>

#include "base/macros.h"
#include "content/browser/renderer_host/p2p/socket_host_throttler.h"
#include "content/common/p2p_socket_type.h"
#include "content/public/browser/browser_message_filter.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/network_change_notifier.h"

namespace net {
class URLRequestContextGetter;
}

namespace rtc {
struct PacketOptions;
}

namespace content {

class P2PSocketHost;
class ResourceContext;

class P2PSocketDispatcherHost
    : public content::BrowserMessageFilter,
      public net::NetworkChangeNotifier::IPAddressObserver {
 public:
  P2PSocketDispatcherHost(content::ResourceContext* resource_context,
                          net::URLRequestContextGetter* url_context);

  // content::BrowserMessageFilter overrides.
  void OnChannelClosing() override;
  void OnDestruct() const override;
  bool OnMessageReceived(const IPC::Message& message) override;

  // net::NetworkChangeNotifier::IPAddressObserver interface.
  void OnIPAddressChanged() override;

  // Starts the RTP packet header dumping. Must be called on the IO thread.
  void StartRtpDump(
      bool incoming,
      bool outgoing,
      const RenderProcessHost::WebRtcRtpPacketCallback& packet_callback);

  // Stops the RTP packet header dumping. Must be Called on the UI thread.
  void StopRtpDumpOnUIThread(bool incoming, bool outgoing);

 protected:
  ~P2PSocketDispatcherHost() override;

 private:
  friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
  friend class base::DeleteHelper<P2PSocketDispatcherHost>;

  typedef std::map<int, P2PSocketHost*> SocketsMap;

  class DnsRequest;

  P2PSocketHost* LookupSocket(int socket_id);

  // Handlers for the messages coming from the renderer.
  void OnStartNetworkNotifications();
  void OnStopNetworkNotifications();
  void OnGetHostAddress(const std::string& host_name, int32_t request_id);

  void OnCreateSocket(P2PSocketType type,
                      int socket_id,
                      const net::IPEndPoint& local_address,
                      const P2PHostAndIPEndPoint& remote_address);
  void OnAcceptIncomingTcpConnection(int listen_socket_id,
                                     const net::IPEndPoint& remote_address,
                                     int connected_socket_id);
  void OnSend(int socket_id,
              const net::IPEndPoint& socket_address,
              const std::vector<char>& data,
              const rtc::PacketOptions& options,
              uint64_t packet_id);
  void OnSetOption(int socket_id, P2PSocketOption option, int value);
  void OnDestroySocket(int socket_id);

  void DoGetNetworkList();
  void SendNetworkList(const net::NetworkInterfaceList& list,
                       const net::IPAddress& default_ipv4_local_address,
                       const net::IPAddress& default_ipv6_local_address);

  // This connects a UDP socket to a public IP address and gets local
  // address. Since it binds to the "any" address (0.0.0.0 or ::) internally, it
  // retrieves the default local address.
  net::IPAddress GetDefaultLocalAddress(int family);

  void OnAddressResolved(DnsRequest* request,
                         const net::IPAddressList& addresses);

  void StopRtpDumpOnIOThread(bool incoming, bool outgoing);

  content::ResourceContext* resource_context_;
  scoped_refptr<net::URLRequestContextGetter> url_context_;

  SocketsMap sockets_;

  bool monitoring_networks_;

  std::set<DnsRequest*> dns_requests_;
  P2PMessageThrottler throttler_;

  net::IPAddress default_ipv4_local_address_;
  net::IPAddress default_ipv6_local_address_;

  bool dump_incoming_rtp_packet_;
  bool dump_outgoing_rtp_packet_;
  RenderProcessHost::WebRtcRtpPacketCallback packet_callback_;

  DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcherHost);
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_DISPATCHER_HOST_H_