| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/pepper/ppb_tcp_socket_private_impl.h" | 5 #include "content/renderer/pepper/ppb_tcp_socket_private_impl.h" |
| 6 | 6 |
| 7 #include "content/common/pepper_messages.h" | 7 #include "content/common/pepper_messages.h" |
| 8 #include "content/renderer/pepper/host_globals.h" | 8 #include "content/renderer/pepper/host_globals.h" |
| 9 #include "content/renderer/pepper/pepper_helper_impl.h" | 9 #include "content/renderer/pepper/pepper_helper_impl.h" |
| 10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 11 #include "content/renderer/pepper/resource_helper.h" | |
| 12 #include "content/renderer/render_thread_impl.h" | 11 #include "content/renderer/render_thread_impl.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" | 12 #include "ppapi/proxy/ppapi_messages.h" |
| 14 #include "ppapi/shared_impl/socket_option_data.h" | 13 #include "ppapi/shared_impl/socket_option_data.h" |
| 15 | 14 |
| 16 namespace content { | 15 namespace content { |
| 17 | 16 |
| 18 PPB_TCPSocket_Private_Impl::PPB_TCPSocket_Private_Impl( | 17 PPB_TCPSocket_Private_Impl::PPB_TCPSocket_Private_Impl( |
| 19 PP_Instance instance, uint32 socket_id) | 18 PP_Instance instance, |
| 20 : ::ppapi::TCPSocketPrivateImpl(instance, socket_id) { | 19 uint32 socket_id, |
| 20 int routing_id) |
| 21 : ::ppapi::TCPSocketPrivateImpl(instance, socket_id), |
| 22 routing_id_(routing_id) { |
| 23 ChildThread::current()->AddRoute(routing_id, this); |
| 21 } | 24 } |
| 22 | 25 |
| 23 PPB_TCPSocket_Private_Impl::~PPB_TCPSocket_Private_Impl() { | 26 PPB_TCPSocket_Private_Impl::~PPB_TCPSocket_Private_Impl() { |
| 27 ChildThread::current()->RemoveRoute(routing_id_); |
| 24 Disconnect(); | 28 Disconnect(); |
| 25 } | 29 } |
| 26 | 30 |
| 27 PP_Resource PPB_TCPSocket_Private_Impl::CreateResource(PP_Instance instance) { | 31 PP_Resource PPB_TCPSocket_Private_Impl::CreateResource(PP_Instance instance) { |
| 28 PepperHelperImpl* helper = GetHelper(instance); | 32 int routing_id = RenderThreadImpl::current()->GenerateRoutingID(); |
| 29 if (!helper) | |
| 30 return 0; | |
| 31 | |
| 32 uint32 socket_id = 0; | 33 uint32 socket_id = 0; |
| 33 helper->Send(new PpapiHostMsg_PPBTCPSocket_CreatePrivate( | 34 RenderThreadImpl::current()->Send(new PpapiHostMsg_PPBTCPSocket_CreatePrivate( |
| 34 helper->routing_id(), 0, &socket_id)); | 35 routing_id, 0, &socket_id)); |
| 35 if (!socket_id) | 36 if (!socket_id) |
| 36 return 0; | 37 return 0; |
| 37 | 38 |
| 38 return (new PPB_TCPSocket_Private_Impl(instance, socket_id))->GetReference(); | 39 return (new PPB_TCPSocket_Private_Impl( |
| 40 instance, socket_id, routing_id))->GetReference(); |
| 39 } | 41 } |
| 40 | 42 |
| 41 PP_Resource PPB_TCPSocket_Private_Impl::CreateConnectedSocket( | 43 PP_Resource PPB_TCPSocket_Private_Impl::CreateConnectedSocket( |
| 42 PP_Instance instance, | 44 PP_Instance instance, |
| 45 int routing_id, |
| 43 uint32 socket_id, | 46 uint32 socket_id, |
| 44 const PP_NetAddress_Private& local_addr, | 47 const PP_NetAddress_Private& local_addr, |
| 45 const PP_NetAddress_Private& remote_addr) { | 48 const PP_NetAddress_Private& remote_addr) { |
| 46 PepperHelperImpl* helper = GetHelper(instance); | 49 PPB_TCPSocket_Private_Impl* socket = new PPB_TCPSocket_Private_Impl( |
| 47 if (!helper) | 50 instance, socket_id, routing_id); |
| 48 return 0; | |
| 49 | |
| 50 PPB_TCPSocket_Private_Impl* socket = | |
| 51 new PPB_TCPSocket_Private_Impl(instance, socket_id); | |
| 52 | 51 |
| 53 socket->connection_state_ = PPB_TCPSocket_Private_Impl::CONNECTED; | 52 socket->connection_state_ = PPB_TCPSocket_Private_Impl::CONNECTED; |
| 54 socket->local_addr_ = local_addr; | 53 socket->local_addr_ = local_addr; |
| 55 socket->remote_addr_ = remote_addr; | 54 socket->remote_addr_ = remote_addr; |
| 56 | 55 |
| 57 helper->RegisterTCPSocket(socket, socket_id); | |
| 58 | |
| 59 return socket->GetReference(); | 56 return socket->GetReference(); |
| 60 } | 57 } |
| 61 | 58 |
| 62 void PPB_TCPSocket_Private_Impl::SendConnect(const std::string& host, | 59 void PPB_TCPSocket_Private_Impl::SendConnect(const std::string& host, |
| 63 uint16_t port) { | 60 uint16_t port) { |
| 64 PepperHelperImpl* helper = GetHelper(pp_instance()); | 61 RenderThreadImpl::current()->Send(new PpapiHostMsg_PPBTCPSocket_Connect( |
| 65 if (!helper) | 62 routing_id_, socket_id_, host, port)); |
| 66 return; | |
| 67 | |
| 68 helper->RegisterTCPSocket(this, socket_id_); | |
| 69 helper->Send(new PpapiHostMsg_PPBTCPSocket_Connect( | |
| 70 helper->routing_id(), socket_id_, host, port)); | |
| 71 } | 63 } |
| 72 | 64 |
| 73 void PPB_TCPSocket_Private_Impl::SendConnectWithNetAddress( | 65 void PPB_TCPSocket_Private_Impl::SendConnectWithNetAddress( |
| 74 const PP_NetAddress_Private& addr) { | 66 const PP_NetAddress_Private& addr) { |
| 75 PepperHelperImpl* helper = GetHelper(pp_instance()); | 67 RenderThreadImpl::current()->Send( |
| 76 if (!helper) | 68 new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress( |
| 77 return; | 69 routing_id_, socket_id_, addr)); |
| 78 | |
| 79 helper->RegisterTCPSocket(this, socket_id_); | |
| 80 helper->Send(new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress( | |
| 81 helper->routing_id(), socket_id_, addr)); | |
| 82 } | 70 } |
| 83 | 71 |
| 84 void PPB_TCPSocket_Private_Impl::SendSSLHandshake( | 72 void PPB_TCPSocket_Private_Impl::SendSSLHandshake( |
| 85 const std::string& server_name, | 73 const std::string& server_name, |
| 86 uint16_t server_port, | 74 uint16_t server_port, |
| 87 const std::vector<std::vector<char> >& trusted_certs, | 75 const std::vector<std::vector<char> >& trusted_certs, |
| 88 const std::vector<std::vector<char> >& untrusted_certs) { | 76 const std::vector<std::vector<char> >& untrusted_certs) { |
| 89 PepperHelperImpl* helper = GetHelper(pp_instance()); | 77 RenderThreadImpl::current()->Send(new PpapiHostMsg_PPBTCPSocket_SSLHandshake( |
| 90 if (!helper) | |
| 91 return; | |
| 92 | |
| 93 helper->Send(new PpapiHostMsg_PPBTCPSocket_SSLHandshake( | |
| 94 socket_id_, server_name, server_port, trusted_certs, untrusted_certs)); | 78 socket_id_, server_name, server_port, trusted_certs, untrusted_certs)); |
| 95 } | 79 } |
| 96 | 80 |
| 97 void PPB_TCPSocket_Private_Impl::SendRead(int32_t bytes_to_read) { | 81 void PPB_TCPSocket_Private_Impl::SendRead(int32_t bytes_to_read) { |
| 98 PepperHelperImpl* helper = GetHelper(pp_instance()); | 82 RenderThreadImpl::current()->Send(new PpapiHostMsg_PPBTCPSocket_Read( |
| 99 if (!helper) | |
| 100 return; | |
| 101 | |
| 102 helper->Send(new PpapiHostMsg_PPBTCPSocket_Read( | |
| 103 socket_id_, bytes_to_read)); | 83 socket_id_, bytes_to_read)); |
| 104 } | 84 } |
| 105 | 85 |
| 106 | 86 |
| 107 void PPB_TCPSocket_Private_Impl::SendWrite(const std::string& buffer) { | 87 void PPB_TCPSocket_Private_Impl::SendWrite(const std::string& buffer) { |
| 108 PepperHelperImpl* helper = GetHelper(pp_instance()); | 88 RenderThreadImpl::current()->Send( |
| 109 if (!helper) | 89 new PpapiHostMsg_PPBTCPSocket_Write(socket_id_, buffer)); |
| 110 return; | |
| 111 | |
| 112 helper->Send(new PpapiHostMsg_PPBTCPSocket_Write( | |
| 113 socket_id_, buffer)); | |
| 114 } | 90 } |
| 115 | 91 |
| 116 void PPB_TCPSocket_Private_Impl::SendDisconnect() { | 92 void PPB_TCPSocket_Private_Impl::SendDisconnect() { |
| 117 PepperHelperImpl* helper = GetHelper(pp_instance()); | 93 RenderThreadImpl::current()->Send( |
| 118 if (!helper) | 94 new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_)); |
| 119 return; | |
| 120 | |
| 121 helper->Send(new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_)); | |
| 122 } | 95 } |
| 123 | 96 |
| 124 void PPB_TCPSocket_Private_Impl::SendSetOption( | 97 void PPB_TCPSocket_Private_Impl::SendSetOption( |
| 125 PP_TCPSocket_Option name, | 98 PP_TCPSocket_Option name, |
| 126 const ::ppapi::SocketOptionData& value) { | 99 const ::ppapi::SocketOptionData& value) { |
| 127 PepperHelperImpl* helper = GetHelper(pp_instance()); | 100 RenderThreadImpl::current()->Send( |
| 128 if (!helper) | 101 new PpapiHostMsg_PPBTCPSocket_SetOption(socket_id_, name, value)); |
| 129 return; | |
| 130 | |
| 131 helper->Send(new PpapiHostMsg_PPBTCPSocket_SetOption( | |
| 132 socket_id_, name, value)); | |
| 133 } | 102 } |
| 134 | 103 |
| 135 PepperHelperImpl* PPB_TCPSocket_Private_Impl::GetHelper(PP_Instance instance) { | 104 bool PPB_TCPSocket_Private_Impl::OnMessageReceived( |
| 136 PepperPluginInstanceImpl* plugin_instance = | 105 const IPC::Message& message) { |
| 137 HostGlobals::Get()->GetInstance(instance); | 106 bool handled = true; |
| 138 if (!plugin_instance) | 107 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Impl, message) |
| 139 return NULL; | 108 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK, OnTCPSocketConnectACK) |
| 140 return plugin_instance->helper(); | 109 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SSLHandshakeACK, |
| 110 OnTCPSocketSSLHandshakeACK) |
| 111 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK, OnTCPSocketReadACK) |
| 112 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK, OnTCPSocketWriteACK) |
| 113 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SetOptionACK, |
| 114 OnTCPSocketSetOptionACK) |
| 115 IPC_MESSAGE_UNHANDLED(handled = false) |
| 116 IPC_END_MESSAGE_MAP() |
| 117 return handled; |
| 118 } |
| 119 |
| 120 void PPB_TCPSocket_Private_Impl::OnTCPSocketConnectACK( |
| 121 uint32 plugin_dispatcher_id, |
| 122 uint32 socket_id, |
| 123 int32_t result, |
| 124 const PP_NetAddress_Private& local_addr, |
| 125 const PP_NetAddress_Private& remote_addr) { |
| 126 OnConnectCompleted(result, local_addr, remote_addr); |
| 127 } |
| 128 |
| 129 void PPB_TCPSocket_Private_Impl::OnTCPSocketSSLHandshakeACK( |
| 130 uint32 plugin_dispatcher_id, |
| 131 uint32 socket_id, |
| 132 bool succeeded, |
| 133 const ppapi::PPB_X509Certificate_Fields& certificate_fields) { |
| 134 OnSSLHandshakeCompleted(succeeded, certificate_fields); |
| 135 } |
| 136 |
| 137 void PPB_TCPSocket_Private_Impl::OnTCPSocketReadACK(uint32 plugin_dispatcher_id, |
| 138 uint32 socket_id, |
| 139 int32_t result, |
| 140 const std::string& data) { |
| 141 OnReadCompleted(result, data); |
| 142 } |
| 143 |
| 144 void PPB_TCPSocket_Private_Impl::OnTCPSocketWriteACK( |
| 145 uint32 plugin_dispatcher_id, |
| 146 uint32 socket_id, |
| 147 int32_t result) { |
| 148 OnWriteCompleted(result); |
| 149 } |
| 150 |
| 151 void PPB_TCPSocket_Private_Impl::OnTCPSocketSetOptionACK( |
| 152 uint32 plugin_dispatcher_id, |
| 153 uint32 socket_id, |
| 154 int32_t result) { |
| 155 OnSetOptionCompleted(result); |
| 141 } | 156 } |
| 142 | 157 |
| 143 } // namespace content | 158 } // namespace content |
| OLD | NEW |