1// Copyright (c) 2012 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.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_DISPATCHER_HOST_H_
6#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_DISPATCHER_HOST_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "content/browser/renderer_host/p2p/socket_host_throttler.h"
14#include "content/common/p2p_socket_type.h"
15#include "content/public/browser/browser_message_filter.h"
16#include "content/public/browser/browser_thread.h"
17#include "content/public/browser/render_process_host.h"
18#include "net/base/ip_endpoint.h"
19#include "net/base/network_change_notifier.h"
20
21namespace net {
22class URLRequestContextGetter;
23}
24
25namespace rtc {
26struct PacketOptions;
27}
28
29namespace content {
30
31class P2PSocketHost;
32class ResourceContext;
33
34class P2PSocketDispatcherHost
35    : public content::BrowserMessageFilter,
36      public net::NetworkChangeNotifier::IPAddressObserver {
37 public:
38  P2PSocketDispatcherHost(content::ResourceContext* resource_context,
39                          net::URLRequestContextGetter* url_context);
40
41  // content::BrowserMessageFilter overrides.
42  virtual void OnChannelClosing() OVERRIDE;
43  virtual void OnDestruct() const OVERRIDE;
44  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
45
46  // net::NetworkChangeNotifier::IPAddressObserver interface.
47  virtual void OnIPAddressChanged() OVERRIDE;
48
49  // Starts the RTP packet header dumping. Must be called on the IO thread.
50  void StartRtpDump(
51      bool incoming,
52      bool outgoing,
53      const RenderProcessHost::WebRtcRtpPacketCallback& packet_callback);
54
55  // Stops the RTP packet header dumping. Must be Called on the UI thread.
56  void StopRtpDumpOnUIThread(bool incoming, bool outgoing);
57
58 protected:
59  virtual ~P2PSocketDispatcherHost();
60
61 private:
62  friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
63  friend class base::DeleteHelper<P2PSocketDispatcherHost>;
64
65  typedef std::map<int, P2PSocketHost*> SocketsMap;
66
67  class DnsRequest;
68
69  P2PSocketHost* LookupSocket(int socket_id);
70
71  // Handlers for the messages coming from the renderer.
72  void OnStartNetworkNotifications();
73  void OnStopNetworkNotifications();
74  void OnGetHostAddress(const std::string& host_name,
75                        int32 request_id);
76
77  void OnCreateSocket(P2PSocketType type,
78                      int socket_id,
79                      const net::IPEndPoint& local_address,
80                      const P2PHostAndIPEndPoint& remote_address);
81  void OnAcceptIncomingTcpConnection(int listen_socket_id,
82                                     const net::IPEndPoint& remote_address,
83                                     int connected_socket_id);
84  void OnSend(int socket_id,
85              const net::IPEndPoint& socket_address,
86              const std::vector<char>& data,
87              const rtc::PacketOptions& options,
88              uint64 packet_id);
89  void OnSetOption(int socket_id, P2PSocketOption option, int value);
90  void OnDestroySocket(int socket_id);
91
92  void DoGetNetworkList();
93  void SendNetworkList(const net::NetworkInterfaceList& list);
94
95  void OnAddressResolved(DnsRequest* request,
96                         const net::IPAddressList& addresses);
97
98  void StopRtpDumpOnIOThread(bool incoming, bool outgoing);
99
100  content::ResourceContext* resource_context_;
101  scoped_refptr<net::URLRequestContextGetter> url_context_;
102
103  SocketsMap sockets_;
104
105  bool monitoring_networks_;
106
107  std::set<DnsRequest*> dns_requests_;
108  P2PMessageThrottler throttler_;
109
110  bool dump_incoming_rtp_packet_;
111  bool dump_outgoing_rtp_packet_;
112  RenderProcessHost::WebRtcRtpPacketCallback packet_callback_;
113
114  DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcherHost);
115};
116
117}  // namespace content
118
119#endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_DISPATCHER_HOST_H_
120