Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / plugins / ppapi / ppb_tcp_socket_private_impl.cc
blobb2c80d15f9307fbffab5ec3c867960f0ad39011a
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.
5 #include "webkit/plugins/ppapi/ppb_tcp_socket_private_impl.h"
7 #include "webkit/plugins/ppapi/host_globals.h"
8 #include "webkit/plugins/ppapi/plugin_delegate.h"
9 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
10 #include "webkit/plugins/ppapi/resource_helper.h"
12 namespace webkit {
13 namespace ppapi {
15 PPB_TCPSocket_Private_Impl::PPB_TCPSocket_Private_Impl(
16 PP_Instance instance, uint32 socket_id)
17 : ::ppapi::TCPSocketPrivateImpl(instance, socket_id) {
20 PPB_TCPSocket_Private_Impl::~PPB_TCPSocket_Private_Impl() {
21 Disconnect();
24 PP_Resource PPB_TCPSocket_Private_Impl::CreateResource(PP_Instance instance) {
25 PluginDelegate* plugin_delegate = GetPluginDelegate(instance);
26 if (!plugin_delegate)
27 return 0;
29 uint32 socket_id = plugin_delegate->TCPSocketCreate();
30 if (!socket_id)
31 return 0;
33 return (new PPB_TCPSocket_Private_Impl(instance, socket_id))->GetReference();
36 PP_Resource PPB_TCPSocket_Private_Impl::CreateConnectedSocket(
37 PP_Instance instance,
38 uint32 socket_id,
39 const PP_NetAddress_Private& local_addr,
40 const PP_NetAddress_Private& remote_addr) {
41 PluginDelegate* plugin_delegate = GetPluginDelegate(instance);
42 if (!plugin_delegate)
43 return 0;
45 PPB_TCPSocket_Private_Impl* socket =
46 new PPB_TCPSocket_Private_Impl(instance, socket_id);
48 socket->connection_state_ = PPB_TCPSocket_Private_Impl::CONNECTED;
49 socket->local_addr_ = local_addr;
50 socket->remote_addr_ = remote_addr;
52 plugin_delegate->RegisterTCPSocket(socket, socket_id);
54 return socket->GetReference();
57 void PPB_TCPSocket_Private_Impl::SendConnect(const std::string& host,
58 uint16_t port) {
59 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
60 if (!plugin_delegate)
61 return;
63 plugin_delegate->TCPSocketConnect(this, socket_id_, host, port);
66 void PPB_TCPSocket_Private_Impl::SendConnectWithNetAddress(
67 const PP_NetAddress_Private& addr) {
68 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
69 if (!plugin_delegate)
70 return;
72 plugin_delegate->TCPSocketConnectWithNetAddress(this, socket_id_, addr);
75 void PPB_TCPSocket_Private_Impl::SendSSLHandshake(
76 const std::string& server_name,
77 uint16_t server_port,
78 const std::vector<std::vector<char> >& trusted_certs,
79 const std::vector<std::vector<char> >& untrusted_certs) {
80 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
81 if (!plugin_delegate)
82 return;
84 plugin_delegate->TCPSocketSSLHandshake(socket_id_, server_name, server_port,
85 trusted_certs, untrusted_certs);
88 void PPB_TCPSocket_Private_Impl::SendRead(int32_t bytes_to_read) {
89 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
90 if (!plugin_delegate)
91 return;
93 plugin_delegate->TCPSocketRead(socket_id_, bytes_to_read);
97 void PPB_TCPSocket_Private_Impl::SendWrite(const std::string& buffer) {
98 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
99 if (!plugin_delegate)
100 return;
102 plugin_delegate->TCPSocketWrite(socket_id_, buffer);
105 void PPB_TCPSocket_Private_Impl::SendDisconnect() {
106 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
107 if (!plugin_delegate)
108 return;
110 plugin_delegate->TCPSocketDisconnect(socket_id_);
113 void PPB_TCPSocket_Private_Impl::SendSetBoolOption(
114 PP_TCPSocketOption_Private name,
115 bool value) {
116 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
117 if (!plugin_delegate)
118 return;
120 plugin_delegate->TCPSocketSetBoolOption(socket_id_, name, value);
123 PluginDelegate* PPB_TCPSocket_Private_Impl::GetPluginDelegate(
124 PP_Instance instance) {
125 PluginInstance* plugin_instance = HostGlobals::Get()->GetInstance(instance);
126 if (!plugin_instance)
127 return NULL;
128 return plugin_instance->delegate();
131 } // namespace ppapi
132 } // namespace webkit