OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/proxy/ppb_flash_net_connector_proxy.h" | 5 #include "ppapi/proxy/ppb_flash_net_connector_proxy.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/c/private/ppb_flash_net_connector.h" | 10 #include "ppapi/c/private/ppb_flash_net_connector.h" |
11 #include "ppapi/proxy/enter_proxy.h" | 11 #include "ppapi/proxy/enter_proxy.h" |
12 #include "ppapi/proxy/plugin_dispatcher.h" | 12 #include "ppapi/proxy/plugin_dispatcher.h" |
13 #include "ppapi/proxy/ppapi_messages.h" | 13 #include "ppapi/proxy/ppapi_messages.h" |
14 #include "ppapi/proxy/serialized_var.h" | 14 #include "ppapi/proxy/serialized_var.h" |
15 #include "ppapi/thunk/enter.h" | 15 #include "ppapi/thunk/enter.h" |
16 #include "ppapi/thunk/ppb_flash_net_connector_api.h" | 16 #include "ppapi/thunk/ppb_flash_net_connector_api.h" |
17 #include "ppapi/thunk/resource_creation_api.h" | 17 #include "ppapi/thunk/resource_creation_api.h" |
18 #include "ppapi/thunk/thunk.h" | 18 #include "ppapi/thunk/thunk.h" |
19 | 19 |
20 using ppapi::thunk::EnterFunctionNoLock; | 20 using ppapi::thunk::EnterFunctionNoLock; |
21 using ppapi::thunk::PPB_Flash_NetConnector_API; | 21 using ppapi::thunk::PPB_Flash_NetConnector_API; |
22 using ppapi::thunk::ResourceCreationAPI; | 22 using ppapi::thunk::ResourceCreationAPI; |
23 | 23 |
24 namespace ppapi { | 24 namespace ppapi { |
25 namespace proxy { | 25 namespace proxy { |
26 | 26 |
27 std::string NetAddressToString(const PP_Flash_NetAddress& addr) { | 27 std::string NetAddressToString(const PP_NetAddress_Private& addr) { |
28 return std::string(addr.data, std::min(static_cast<size_t>(addr.size), | 28 return std::string(addr.data, std::min(static_cast<size_t>(addr.size), |
29 sizeof(addr.data))); | 29 sizeof(addr.data))); |
30 } | 30 } |
31 | 31 |
32 void StringToNetAddress(const std::string& str, PP_Flash_NetAddress* addr) { | 32 void StringToNetAddress(const std::string& str, PP_NetAddress_Private* addr) { |
33 addr->size = std::min(str.size(), sizeof(addr->data)); | 33 addr->size = std::min(str.size(), sizeof(addr->data)); |
34 memcpy(addr->data, str.data(), addr->size); | 34 memcpy(addr->data, str.data(), addr->size); |
35 } | 35 } |
36 | 36 |
37 class AbortCallbackTask : public Task { | 37 class AbortCallbackTask : public Task { |
38 public: | 38 public: |
39 AbortCallbackTask(PP_CompletionCallback callback) | 39 AbortCallbackTask(PP_CompletionCallback callback) |
40 : callback_(callback) {} | 40 : callback_(callback) {} |
41 | 41 |
42 virtual void Run() { | 42 virtual void Run() { |
(...skipping 10 matching lines...) Expand all Loading... |
53 explicit FlashNetConnector(const HostResource& resource); | 53 explicit FlashNetConnector(const HostResource& resource); |
54 virtual ~FlashNetConnector(); | 54 virtual ~FlashNetConnector(); |
55 | 55 |
56 // Resource overrides. | 56 // Resource overrides. |
57 virtual PPB_Flash_NetConnector_API* AsPPB_Flash_NetConnector_API() OVERRIDE; | 57 virtual PPB_Flash_NetConnector_API* AsPPB_Flash_NetConnector_API() OVERRIDE; |
58 | 58 |
59 // PPB_Flash_NetConnector_API implementation. | 59 // PPB_Flash_NetConnector_API implementation. |
60 virtual int32_t ConnectTcp(const char* host, | 60 virtual int32_t ConnectTcp(const char* host, |
61 uint16_t port, | 61 uint16_t port, |
62 PP_FileHandle* socket_out, | 62 PP_FileHandle* socket_out, |
63 PP_Flash_NetAddress* local_addr_out, | 63 PP_NetAddress_Private* local_addr_out, |
64 PP_Flash_NetAddress* remote_addr_out, | 64 PP_NetAddress_Private* remote_addr_out, |
65 PP_CompletionCallback callback) OVERRIDE; | 65 PP_CompletionCallback callback) OVERRIDE; |
66 virtual int32_t ConnectTcpAddress(const PP_Flash_NetAddress* addr, | 66 virtual int32_t ConnectTcpAddress(const PP_NetAddress_Private* addr, |
67 PP_FileHandle* socket_out, | 67 PP_FileHandle* socket_out, |
68 PP_Flash_NetAddress* local_addr_out, | 68 PP_NetAddress_Private* local_addr_out, |
69 PP_Flash_NetAddress* remote_addr_out, | 69 PP_NetAddress_Private* remote_addr_out, |
70 PP_CompletionCallback callback) OVERRIDE; | 70 PP_CompletionCallback callback) OVERRIDE; |
71 | 71 |
72 void ConnectComplete(int32_t result, | 72 void ConnectComplete(int32_t result, |
73 base::PlatformFile file, | 73 base::PlatformFile file, |
74 const std::string& local_addr_as_string, | 74 const std::string& local_addr_as_string, |
75 const std::string& remote_addr_as_string); | 75 const std::string& remote_addr_as_string); |
76 | 76 |
77 private: | 77 private: |
78 // Backend for both ConnectTcp and ConnectTcpAddress. To keep things generic, | 78 // Backend for both ConnectTcp and ConnectTcpAddress. To keep things generic, |
79 // the message is passed in (on error, it's deleted). | 79 // the message is passed in (on error, it's deleted). |
80 int32_t ConnectWithMessage(IPC::Message* msg, | 80 int32_t ConnectWithMessage(IPC::Message* msg, |
81 PP_FileHandle* socket_out, | 81 PP_FileHandle* socket_out, |
82 PP_Flash_NetAddress* local_addr_out, | 82 PP_NetAddress_Private* local_addr_out, |
83 PP_Flash_NetAddress* remote_addr_out, | 83 PP_NetAddress_Private* remote_addr_out, |
84 PP_CompletionCallback callback); | 84 PP_CompletionCallback callback); |
85 | 85 |
86 PP_CompletionCallback callback_; | 86 PP_CompletionCallback callback_; |
87 PP_FileHandle* socket_out_; | 87 PP_FileHandle* socket_out_; |
88 PP_Flash_NetAddress* local_addr_out_; | 88 PP_NetAddress_Private* local_addr_out_; |
89 PP_Flash_NetAddress* remote_addr_out_; | 89 PP_NetAddress_Private* remote_addr_out_; |
90 }; | 90 }; |
91 | 91 |
92 FlashNetConnector::FlashNetConnector(const HostResource& resource) | 92 FlashNetConnector::FlashNetConnector(const HostResource& resource) |
93 : Resource(resource), | 93 : Resource(resource), |
94 callback_(PP_BlockUntilComplete()), | 94 callback_(PP_BlockUntilComplete()), |
95 socket_out_(NULL), | 95 socket_out_(NULL), |
96 local_addr_out_(NULL), | 96 local_addr_out_(NULL), |
97 remote_addr_out_(NULL) { | 97 remote_addr_out_(NULL) { |
98 } | 98 } |
99 | 99 |
100 FlashNetConnector::~FlashNetConnector() { | 100 FlashNetConnector::~FlashNetConnector() { |
101 if (callback_.func) { | 101 if (callback_.func) { |
102 MessageLoop::current()->PostTask(FROM_HERE, | 102 MessageLoop::current()->PostTask(FROM_HERE, |
103 new AbortCallbackTask(callback_)); | 103 new AbortCallbackTask(callback_)); |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 PPB_Flash_NetConnector_API* FlashNetConnector::AsPPB_Flash_NetConnector_API() { | 107 PPB_Flash_NetConnector_API* FlashNetConnector::AsPPB_Flash_NetConnector_API() { |
108 return this; | 108 return this; |
109 } | 109 } |
110 | 110 |
111 int32_t FlashNetConnector::ConnectTcp( | 111 int32_t FlashNetConnector::ConnectTcp( |
112 const char* host, | 112 const char* host, |
113 uint16_t port, | 113 uint16_t port, |
114 PP_FileHandle* socket_out, | 114 PP_FileHandle* socket_out, |
115 PP_Flash_NetAddress* local_addr_out, | 115 PP_NetAddress_Private* local_addr_out, |
116 PP_Flash_NetAddress* remote_addr_out, | 116 PP_NetAddress_Private* remote_addr_out, |
117 PP_CompletionCallback callback) { | 117 PP_CompletionCallback callback) { |
118 return ConnectWithMessage( | 118 return ConnectWithMessage( |
119 new PpapiHostMsg_PPBFlashNetConnector_ConnectTcp( | 119 new PpapiHostMsg_PPBFlashNetConnector_ConnectTcp( |
120 API_ID_PPB_FLASH_NETCONNECTOR, host_resource(), host, port), | 120 API_ID_PPB_FLASH_NETCONNECTOR, host_resource(), host, port), |
121 socket_out, local_addr_out, remote_addr_out, callback); | 121 socket_out, local_addr_out, remote_addr_out, callback); |
122 } | 122 } |
123 | 123 |
124 int32_t FlashNetConnector::ConnectTcpAddress( | 124 int32_t FlashNetConnector::ConnectTcpAddress( |
125 const PP_Flash_NetAddress* addr, | 125 const PP_NetAddress_Private* addr, |
126 PP_FileHandle* socket_out, | 126 PP_FileHandle* socket_out, |
127 PP_Flash_NetAddress* local_addr_out, | 127 PP_NetAddress_Private* local_addr_out, |
128 PP_Flash_NetAddress* remote_addr_out, | 128 PP_NetAddress_Private* remote_addr_out, |
129 PP_CompletionCallback callback) { | 129 PP_CompletionCallback callback) { |
130 return ConnectWithMessage( | 130 return ConnectWithMessage( |
131 new PpapiHostMsg_PPBFlashNetConnector_ConnectTcpAddress( | 131 new PpapiHostMsg_PPBFlashNetConnector_ConnectTcpAddress( |
132 API_ID_PPB_FLASH_NETCONNECTOR, | 132 API_ID_PPB_FLASH_NETCONNECTOR, |
133 host_resource(), NetAddressToString(*addr)), | 133 host_resource(), NetAddressToString(*addr)), |
134 socket_out, local_addr_out, remote_addr_out, callback); | 134 socket_out, local_addr_out, remote_addr_out, callback); |
135 } | 135 } |
136 | 136 |
137 void FlashNetConnector::ConnectComplete( | 137 void FlashNetConnector::ConnectComplete( |
138 int32_t result, | 138 int32_t result, |
139 base::PlatformFile file, | 139 base::PlatformFile file, |
140 const std::string& local_addr_as_string, | 140 const std::string& local_addr_as_string, |
141 const std::string& remote_addr_as_string) { | 141 const std::string& remote_addr_as_string) { |
142 if (!callback_.func) { | 142 if (!callback_.func) { |
143 base::ClosePlatformFile(file); | 143 base::ClosePlatformFile(file); |
144 return; | 144 return; |
145 } | 145 } |
146 | 146 |
147 *socket_out_ = static_cast<PP_FileHandle>(file); | 147 *socket_out_ = static_cast<PP_FileHandle>(file); |
148 StringToNetAddress(local_addr_as_string, local_addr_out_); | 148 StringToNetAddress(local_addr_as_string, local_addr_out_); |
149 StringToNetAddress(remote_addr_as_string, remote_addr_out_); | 149 StringToNetAddress(remote_addr_as_string, remote_addr_out_); |
150 | 150 |
151 PP_RunAndClearCompletionCallback(&callback_, result); | 151 PP_RunAndClearCompletionCallback(&callback_, result); |
152 } | 152 } |
153 | 153 |
154 int32_t FlashNetConnector::ConnectWithMessage( | 154 int32_t FlashNetConnector::ConnectWithMessage( |
155 IPC::Message* msg, | 155 IPC::Message* msg, |
156 PP_FileHandle* socket_out, | 156 PP_FileHandle* socket_out, |
157 PP_Flash_NetAddress* local_addr_out, | 157 PP_NetAddress_Private* local_addr_out, |
158 PP_Flash_NetAddress* remote_addr_out, | 158 PP_NetAddress_Private* remote_addr_out, |
159 PP_CompletionCallback callback) { | 159 PP_CompletionCallback callback) { |
160 scoped_ptr<IPC::Message> msg_deletor(msg); | 160 scoped_ptr<IPC::Message> msg_deletor(msg); |
161 if (callback_.func != NULL) | 161 if (callback_.func != NULL) |
162 return PP_ERROR_INPROGRESS; // Can only have one pending request. | 162 return PP_ERROR_INPROGRESS; // Can only have one pending request. |
163 | 163 |
164 // Send the request, it will call us back via ConnectACK. | 164 // Send the request, it will call us back via ConnectACK. |
165 PluginDispatcher::GetForResource(this)->Send(msg_deletor.release()); | 165 PluginDispatcher::GetForResource(this)->Send(msg_deletor.release()); |
166 | 166 |
167 callback_ = callback; | 167 callback_ = callback; |
168 socket_out_ = socket_out; | 168 socket_out_ = socket_out; |
169 local_addr_out_ = local_addr_out; | 169 local_addr_out_ = local_addr_out; |
170 remote_addr_out_ = remote_addr_out; | 170 remote_addr_out_ = remote_addr_out; |
171 return PP_OK_COMPLETIONPENDING; | 171 return PP_OK_COMPLETIONPENDING; |
172 } | 172 } |
173 | 173 |
174 // Contains the data that the host interface will write to so we can send it | 174 // Contains the data that the host interface will write to so we can send it |
175 // to the plugin. This is created when a request is initiated, and deleted in | 175 // to the plugin. This is created when a request is initiated, and deleted in |
176 // the callback handler. | 176 // the callback handler. |
177 struct PPB_Flash_NetConnector_Proxy::ConnectCallbackInfo { | 177 struct PPB_Flash_NetConnector_Proxy::ConnectCallbackInfo { |
178 ConnectCallbackInfo(const HostResource& r) : resource(r), handle(0) { | 178 ConnectCallbackInfo(const HostResource& r) : resource(r), handle(0) { |
179 memset(&local_addr, 0, sizeof(local_addr)); | 179 memset(&local_addr, 0, sizeof(local_addr)); |
180 memset(&remote_addr, 0, sizeof(remote_addr)); | 180 memset(&remote_addr, 0, sizeof(remote_addr)); |
181 } | 181 } |
182 | 182 |
183 HostResource resource; | 183 HostResource resource; |
184 | 184 |
185 PP_FileHandle handle; | 185 PP_FileHandle handle; |
186 PP_Flash_NetAddress local_addr; | 186 PP_NetAddress_Private local_addr; |
187 PP_Flash_NetAddress remote_addr; | 187 PP_NetAddress_Private remote_addr; |
188 }; | 188 }; |
189 | 189 |
190 PPB_Flash_NetConnector_Proxy::PPB_Flash_NetConnector_Proxy( | 190 PPB_Flash_NetConnector_Proxy::PPB_Flash_NetConnector_Proxy( |
191 Dispatcher* dispatcher) | 191 Dispatcher* dispatcher) |
192 : InterfaceProxy(dispatcher), | 192 : InterfaceProxy(dispatcher), |
193 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 193 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
194 } | 194 } |
195 | 195 |
196 PPB_Flash_NetConnector_Proxy::~PPB_Flash_NetConnector_Proxy() { | 196 PPB_Flash_NetConnector_Proxy::~PPB_Flash_NetConnector_Proxy() { |
197 } | 197 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 OnCompleteCallbackInHost(result, info); | 256 OnCompleteCallbackInHost(result, info); |
257 } | 257 } |
258 | 258 |
259 void PPB_Flash_NetConnector_Proxy::OnMsgConnectTcpAddress( | 259 void PPB_Flash_NetConnector_Proxy::OnMsgConnectTcpAddress( |
260 const HostResource& resource, | 260 const HostResource& resource, |
261 const std::string& net_address_as_string) { | 261 const std::string& net_address_as_string) { |
262 ConnectCallbackInfo* info = new ConnectCallbackInfo(resource); | 262 ConnectCallbackInfo* info = new ConnectCallbackInfo(resource); |
263 pp::CompletionCallback callback = callback_factory_.NewOptionalCallback( | 263 pp::CompletionCallback callback = callback_factory_.NewOptionalCallback( |
264 &PPB_Flash_NetConnector_Proxy::OnCompleteCallbackInHost, info); | 264 &PPB_Flash_NetConnector_Proxy::OnCompleteCallbackInHost, info); |
265 | 265 |
266 PP_Flash_NetAddress net_address; | 266 PP_NetAddress_Private net_address; |
267 StringToNetAddress(net_address_as_string, &net_address); | 267 StringToNetAddress(net_address_as_string, &net_address); |
268 | 268 |
269 EnterHostFromHostResource<PPB_Flash_NetConnector_API> enter(resource); | 269 EnterHostFromHostResource<PPB_Flash_NetConnector_API> enter(resource); |
270 int32_t result = PP_ERROR_BADRESOURCE; | 270 int32_t result = PP_ERROR_BADRESOURCE; |
271 if (enter.succeeded()) { | 271 if (enter.succeeded()) { |
272 result = enter.object()->ConnectTcpAddress( | 272 result = enter.object()->ConnectTcpAddress( |
273 &net_address, &info->handle, &info->local_addr, &info->remote_addr, | 273 &net_address, &info->handle, &info->local_addr, &info->remote_addr, |
274 callback.pp_completion_callback()); | 274 callback.pp_completion_callback()); |
275 } | 275 } |
276 if (result != PP_OK_COMPLETIONPENDING) | 276 if (result != PP_OK_COMPLETIONPENDING) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 } else { | 313 } else { |
314 dispatcher()->Send(new PpapiMsg_PPBFlashNetConnector_ConnectACK( | 314 dispatcher()->Send(new PpapiMsg_PPBFlashNetConnector_ConnectACK( |
315 API_ID_PPB_FLASH_NETCONNECTOR, | 315 API_ID_PPB_FLASH_NETCONNECTOR, |
316 info->resource, result, | 316 info->resource, result, |
317 IPC::InvalidPlatformFileForTransit(), std::string(), std::string())); | 317 IPC::InvalidPlatformFileForTransit(), std::string(), std::string())); |
318 } | 318 } |
319 } | 319 } |
320 | 320 |
321 } // namespace proxy | 321 } // namespace proxy |
322 } // namespace ppapi | 322 } // namespace ppapi |
OLD | NEW |