summaryrefslogtreecommitdiffstats
path: root/content/renderer/p2p/socket_dispatcher.h
blob: acc5ef566f0edda9710441b03b2385c8fec248c0 (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
// Copyright (c) 2011 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.

// P2PSocketDispatcher is a per-renderer object that dispatchers all
// P2P messages received from the browser and relays all P2P messages
// sent to the browser. P2PSocketClient instances register themselves
// with the dispatcher using RegisterClient() and UnregisterClient().
//
// Relationship of classes.
//
//      P2PSocketHost                 P2PSocketClient
//           ^                               ^
//           |                               |
//           v              IPC              v
//      P2PSocketsHost  <--------->  P2PSocketDispatcher
//

#ifndef CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_
#define CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_

#include <vector>

#include "base/id_map.h"
#include "content/common/p2p_sockets.h"
#include "content/renderer/p2p/socket_client.h"
#include "content/renderer/render_view_observer.h"

namespace base {
class MessageLoopProxy;
}  // namespace base

// P2PSocketDispatcher works on the renderer thread. It dispatches all
// messages on that thread, and all its methods must be called on the
// same thread.
class P2PSocketDispatcher : public RenderViewObserver {
 public:
  explicit P2PSocketDispatcher(RenderView* render_view);
  virtual ~P2PSocketDispatcher();

  P2PSocketClient* CreateSocket(P2PSocketType type,
                                const net::IPEndPoint& address,
                                P2PSocketClient::Delegate* delegate);

  // RenderViewObserver overrides.
  virtual bool OnMessageReceived(const IPC::Message& message);

 private:
  friend class P2PSocketClient;

  // Called by P2PSocketClient.
  int RegisterClient(P2PSocketClient* client);
  void UnregisterClient(int id);
  void SendP2PMessage(IPC::Message* msg);
  base::MessageLoopProxy* message_loop();

  // Incoming message handlers.
  void OnSocketCreated(int socket_id, const net::IPEndPoint& address);
  void OnError(int socket_id);
  void OnDataReceived(int socket_id, const net::IPEndPoint& address,
                      const std::vector<char>& data);

  P2PSocketClient* GetClient(int socket_id);

  scoped_refptr<base::MessageLoopProxy> message_loop_;
  IDMap<P2PSocketClient> clients_;

  DISALLOW_COPY_AND_ASSIGN(P2PSocketDispatcher);
};

#endif  // CONTENT_RENDERER_P2P_SOCKET_DISPATCHER_H_