summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/p2p_sockets_host.cc
blob: 8cc28437880222d2a91fa1d6d145091391675ab1 (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
// 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.

#include "content/browser/renderer_host/p2p_sockets_host.h"

#include "content/browser/renderer_host/p2p_socket_host.h"
#include "content/common/p2p_messages.h"

P2PSocketsHost::P2PSocketsHost() {
}

P2PSocketsHost::~P2PSocketsHost() {
}

void P2PSocketsHost::OnChannelClosing() {
  BrowserMessageFilter::OnChannelClosing();

  // Since the IPC channel is gone, close pending connections.
  for (IDMap<P2PSocketHost, IDMapOwnPointer>::iterator i(&sockets_);
       !i.IsAtEnd(); i.Advance()) {
    sockets_.Remove(i.GetCurrentKey());
  }
}

void P2PSocketsHost::OnDestruct() const {
  BrowserThread::DeleteOnIOThread::Destruct(this);
}

bool P2PSocketsHost::OnMessageReceived(const IPC::Message& message,
                                       bool* message_was_ok) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketsHost, message, *message_was_ok)
    IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket)
    IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend)
    IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP_EX()
  return handled;
}

void P2PSocketsHost::OnCreateSocket(
    const IPC::Message& msg, P2PSocketType type, int socket_id,
    const net::IPEndPoint& remote_address) {
  if (sockets_.Lookup(socket_id)) {
    LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
        "that already exists.";
    return;
  }

  if (type != P2P_SOCKET_UDP) {
    Send(new P2PMsg_OnError(msg.routing_id(), socket_id));
    return;
  }

  scoped_ptr<P2PSocketHost> socket(
      P2PSocketHost::Create(this, msg.routing_id(), socket_id, type));

  if (!socket.get()) {
    Send(new P2PMsg_OnError(msg.routing_id(), socket_id));
    return;
  }

  if (socket->Init()) {
    sockets_.AddWithID(socket.release(), socket_id);
  }
}

void P2PSocketsHost::OnSend(const IPC::Message& msg, int socket_id,
                            const net::IPEndPoint& socket_address,
                            const std::vector<char>& data) {
  P2PSocketHost* socket = sockets_.Lookup(socket_id);
  if (!socket) {
    LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id.";
    return;
  }
  socket->Send(socket_address, data);
}

void P2PSocketsHost::OnDestroySocket(const IPC::Message& msg, int socket_id) {
  sockets_.Remove(socket_id);
}