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( |
39 } | 40 instance, socket_id, routing_id))->GetReference(); |
40 | |
41 PP_Resource PPB_TCPSocket_Private_Impl::CreateConnectedSocket( | |
42 PP_Instance instance, | |
43 uint32 socket_id, | |
44 const PP_NetAddress_Private& local_addr, | |
45 const PP_NetAddress_Private& remote_addr) { | |
46 PepperHelperImpl* helper = GetHelper(instance); | |
47 if (!helper) | |
48 return 0; | |
49 | |
50 PPB_TCPSocket_Private_Impl* socket = | |
51 new PPB_TCPSocket_Private_Impl(instance, socket_id); | |
52 | |
53 socket->connection_state_ = PPB_TCPSocket_Private_Impl::CONNECTED; | |
54 socket->local_addr_ = local_addr; | |
55 socket->remote_addr_ = remote_addr; | |
56 | |
57 helper->RegisterTCPSocket(socket, socket_id); | |
58 | |
59 return socket->GetReference(); | |
60 } | 41 } |
61 | 42 |
62 void PPB_TCPSocket_Private_Impl::SendConnect(const std::string& host, | 43 void PPB_TCPSocket_Private_Impl::SendConnect(const std::string& host, |
63 uint16_t port) { | 44 uint16_t port) { |
64 PepperHelperImpl* helper = GetHelper(pp_instance()); | 45 RenderThreadImpl::current()->Send(new PpapiHostMsg_PPBTCPSocket_Connect( |
65 if (!helper) | 46 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 } | 47 } |
72 | 48 |
73 void PPB_TCPSocket_Private_Impl::SendConnectWithNetAddress( | 49 void PPB_TCPSocket_Private_Impl::SendConnectWithNetAddress( |
74 const PP_NetAddress_Private& addr) { | 50 const PP_NetAddress_Private& addr) { |
75 PepperHelperImpl* helper = GetHelper(pp_instance()); | 51 RenderThreadImpl::current()->Send( |
76 if (!helper) | 52 new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress( |
77 return; | 53 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 } | 54 } |
83 | 55 |
84 void PPB_TCPSocket_Private_Impl::SendSSLHandshake( | 56 void PPB_TCPSocket_Private_Impl::SendSSLHandshake( |
85 const std::string& server_name, | 57 const std::string& server_name, |
86 uint16_t server_port, | 58 uint16_t server_port, |
87 const std::vector<std::vector<char> >& trusted_certs, | 59 const std::vector<std::vector<char> >& trusted_certs, |
88 const std::vector<std::vector<char> >& untrusted_certs) { | 60 const std::vector<std::vector<char> >& untrusted_certs) { |
89 PepperHelperImpl* helper = GetHelper(pp_instance()); | 61 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)); | 62 socket_id_, server_name, server_port, trusted_certs, untrusted_certs)); |
95 } | 63 } |
96 | 64 |
97 void PPB_TCPSocket_Private_Impl::SendRead(int32_t bytes_to_read) { | 65 void PPB_TCPSocket_Private_Impl::SendRead(int32_t bytes_to_read) { |
98 PepperHelperImpl* helper = GetHelper(pp_instance()); | 66 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)); | 67 socket_id_, bytes_to_read)); |
104 } | 68 } |
105 | 69 |
106 | 70 |
107 void PPB_TCPSocket_Private_Impl::SendWrite(const std::string& buffer) { | 71 void PPB_TCPSocket_Private_Impl::SendWrite(const std::string& buffer) { |
108 PepperHelperImpl* helper = GetHelper(pp_instance()); | 72 RenderThreadImpl::current()->Send( |
109 if (!helper) | 73 new PpapiHostMsg_PPBTCPSocket_Write(socket_id_, buffer)); |
110 return; | |
111 | |
112 helper->Send(new PpapiHostMsg_PPBTCPSocket_Write( | |
113 socket_id_, buffer)); | |
114 } | 74 } |
115 | 75 |
116 void PPB_TCPSocket_Private_Impl::SendDisconnect() { | 76 void PPB_TCPSocket_Private_Impl::SendDisconnect() { |
117 PepperHelperImpl* helper = GetHelper(pp_instance()); | 77 RenderThreadImpl::current()->Send( |
118 if (!helper) | 78 new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_)); |
119 return; | |
120 | |
121 helper->Send(new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_)); | |
122 } | 79 } |
123 | 80 |
124 void PPB_TCPSocket_Private_Impl::SendSetOption( | 81 void PPB_TCPSocket_Private_Impl::SendSetOption( |
125 PP_TCPSocket_Option name, | 82 PP_TCPSocket_Option name, |
126 const ::ppapi::SocketOptionData& value) { | 83 const ::ppapi::SocketOptionData& value) { |
127 PepperHelperImpl* helper = GetHelper(pp_instance()); | 84 RenderThreadImpl::current()->Send( |
128 if (!helper) | 85 new PpapiHostMsg_PPBTCPSocket_SetOption(socket_id_, name, value)); |
129 return; | |
130 | |
131 helper->Send(new PpapiHostMsg_PPBTCPSocket_SetOption( | |
132 socket_id_, name, value)); | |
133 } | 86 } |
134 | 87 |
135 PepperHelperImpl* PPB_TCPSocket_Private_Impl::GetHelper(PP_Instance instance) { | 88 bool PPB_TCPSocket_Private_Impl::OnMessageReceived( |
136 PepperPluginInstanceImpl* plugin_instance = | 89 const IPC::Message& message) { |
137 HostGlobals::Get()->GetInstance(instance); | 90 bool handled = true; |
138 if (!plugin_instance) | 91 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Impl, message) |
139 return NULL; | 92 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK, OnTCPSocketConnectACK) |
140 return plugin_instance->helper(); | 93 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SSLHandshakeACK, |
| 94 OnTCPSocketSSLHandshakeACK) |
| 95 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK, OnTCPSocketReadACK) |
| 96 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK, OnTCPSocketWriteACK) |
| 97 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SetOptionACK, |
| 98 OnTCPSocketSetOptionACK) |
| 99 IPC_MESSAGE_UNHANDLED(handled = false) |
| 100 IPC_END_MESSAGE_MAP() |
| 101 return handled; |
| 102 } |
| 103 |
| 104 void PPB_TCPSocket_Private_Impl::OnTCPSocketConnectACK( |
| 105 uint32 plugin_dispatcher_id, |
| 106 uint32 socket_id, |
| 107 int32_t result, |
| 108 const PP_NetAddress_Private& local_addr, |
| 109 const PP_NetAddress_Private& remote_addr) { |
| 110 OnConnectCompleted(result, local_addr, remote_addr); |
| 111 } |
| 112 |
| 113 void PPB_TCPSocket_Private_Impl::OnTCPSocketSSLHandshakeACK( |
| 114 uint32 plugin_dispatcher_id, |
| 115 uint32 socket_id, |
| 116 bool succeeded, |
| 117 const ppapi::PPB_X509Certificate_Fields& certificate_fields) { |
| 118 OnSSLHandshakeCompleted(succeeded, certificate_fields); |
| 119 } |
| 120 |
| 121 void PPB_TCPSocket_Private_Impl::OnTCPSocketReadACK(uint32 plugin_dispatcher_id, |
| 122 uint32 socket_id, |
| 123 int32_t result, |
| 124 const std::string& data) { |
| 125 OnReadCompleted(result, data); |
| 126 } |
| 127 |
| 128 void PPB_TCPSocket_Private_Impl::OnTCPSocketWriteACK( |
| 129 uint32 plugin_dispatcher_id, |
| 130 uint32 socket_id, |
| 131 int32_t result) { |
| 132 OnWriteCompleted(result); |
| 133 } |
| 134 |
| 135 void PPB_TCPSocket_Private_Impl::OnTCPSocketSetOptionACK( |
| 136 uint32 plugin_dispatcher_id, |
| 137 uint32 socket_id, |
| 138 int32_t result) { |
| 139 OnSetOptionCompleted(result); |
141 } | 140 } |
142 | 141 |
143 } // namespace content | 142 } // namespace content |
OLD | NEW |