summaryrefslogtreecommitdiffstats
path: root/content/renderer/pepper/ppb_tcp_server_socket_private_impl.cc
blob: 695774bff8c52ecac6e417df093f447c92b66ee8 (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
// 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.

#include "content/renderer/pepper/ppb_tcp_server_socket_private_impl.h"

#include "base/logging.h"
#include "content/renderer/pepper/host_globals.h"
#include "content/renderer/pepper/pepper_plugin_delegate_impl.h"
#include "content/renderer/pepper/ppb_tcp_socket_private_impl.h"
#include "content/renderer/pepper/resource_helper.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace content {

PPB_TCPServerSocket_Private_Impl::PPB_TCPServerSocket_Private_Impl(
    PP_Instance instance)
    : ::ppapi::PPB_TCPServerSocket_Shared(instance) {
}

PPB_TCPServerSocket_Private_Impl::~PPB_TCPServerSocket_Private_Impl() {
  StopListening();
}

PP_Resource PPB_TCPServerSocket_Private_Impl::CreateResource(
    PP_Instance instance) {
  PPB_TCPServerSocket_Private_Impl* socket =
      new PPB_TCPServerSocket_Private_Impl(instance);
  return socket->GetReference();
}

void PPB_TCPServerSocket_Private_Impl::OnAcceptCompleted(
    bool succeeded,
    uint32 accepted_socket_id,
    const PP_NetAddress_Private& local_addr,
    const PP_NetAddress_Private& remote_addr) {
  if (!::ppapi::TrackedCallback::IsPending(accept_callback_) ||
      !tcp_socket_buffer_) {
    NOTREACHED();
    return;
  }

  if (succeeded) {
    *tcp_socket_buffer_ =
        PPB_TCPSocket_Private_Impl::CreateConnectedSocket(pp_instance(),
                                                          accepted_socket_id,
                                                          local_addr,
                                                          remote_addr);
  }
  tcp_socket_buffer_ = NULL;

  accept_callback_->Run(succeeded ? PP_OK : PP_ERROR_FAILED);
}

void PPB_TCPServerSocket_Private_Impl::SendListen(
    const PP_NetAddress_Private& addr,
    int32_t backlog) {
  PepperPluginDelegateImpl* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return;

  plugin_delegate->Send(new PpapiHostMsg_PPBTCPServerSocket_Listen(
      plugin_delegate->routing_id(), 0, pp_resource(), addr, backlog));
}

void PPB_TCPServerSocket_Private_Impl::SendAccept() {
  PepperPluginDelegateImpl* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return;

  plugin_delegate->Send(new PpapiHostMsg_PPBTCPServerSocket_Accept(
      plugin_delegate->routing_id(), socket_id_));
}

void PPB_TCPServerSocket_Private_Impl::SendStopListening() {
  PepperPluginDelegateImpl* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return;

  if (socket_id_ != 0) {
    plugin_delegate->Send(new PpapiHostMsg_PPBTCPServerSocket_Destroy(
        socket_id_));
    plugin_delegate->TCPServerSocketStopListening(socket_id_);
  }
}

PepperPluginDelegateImpl*
    PPB_TCPServerSocket_Private_Impl::GetPluginDelegate() {
  return ResourceHelper::GetPluginDelegate(this);
}

}  // namespace content