| 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_broker_proxy.h" | 5 #include "ppapi/proxy/ppb_broker_proxy.h" |
| 6 | 6 |
| 7 #include "ppapi/c/pp_errors.h" | 7 #include "ppapi/c/pp_errors.h" |
| 8 #include "ppapi/c/trusted/ppb_broker_trusted.h" | 8 #include "ppapi/c/trusted/ppb_broker_trusted.h" |
| 9 #include "ppapi/proxy/enter_proxy.h" | 9 #include "ppapi/proxy/enter_proxy.h" |
| 10 #include "ppapi/proxy/plugin_dispatcher.h" | 10 #include "ppapi/proxy/plugin_dispatcher.h" |
| 11 #include "ppapi/proxy/plugin_resource.h" | 11 #include "ppapi/proxy/plugin_resource.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" | 12 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/thunk/ppb_broker_api.h" | 13 #include "ppapi/thunk/ppb_broker_api.h" |
| 14 #include "ppapi/thunk/enter.h" | 14 #include "ppapi/thunk/enter.h" |
| 15 #include "ppapi/thunk/thunk.h" | 15 #include "ppapi/thunk/thunk.h" |
| 16 | 16 |
| 17 using ppapi::thunk::PPB_Broker_API; |
| 18 |
| 17 namespace pp { | 19 namespace pp { |
| 18 namespace proxy { | 20 namespace proxy { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 base::PlatformFile IntToPlatformFile(int32_t handle) { | 24 base::PlatformFile IntToPlatformFile(int32_t handle) { |
| 23 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 24 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); | 26 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| 25 #elif defined(OS_POSIX) | 27 #elif defined(OS_POSIX) |
| 26 return handle; | 28 return handle; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 39 #endif | 41 #endif |
| 40 } | 42 } |
| 41 | 43 |
| 42 InterfaceProxy* CreateBrokerProxy(Dispatcher* dispatcher, | 44 InterfaceProxy* CreateBrokerProxy(Dispatcher* dispatcher, |
| 43 const void* target_interface) { | 45 const void* target_interface) { |
| 44 return new PPB_Broker_Proxy(dispatcher, target_interface); | 46 return new PPB_Broker_Proxy(dispatcher, target_interface); |
| 45 } | 47 } |
| 46 | 48 |
| 47 } // namespace | 49 } // namespace |
| 48 | 50 |
| 49 class Broker : public ppapi::thunk::PPB_Broker_API, | 51 class Broker : public PPB_Broker_API, public PluginResource { |
| 50 public PluginResource { | |
| 51 public: | 52 public: |
| 52 explicit Broker(const HostResource& resource); | 53 explicit Broker(const HostResource& resource); |
| 53 virtual ~Broker(); | 54 virtual ~Broker(); |
| 54 | 55 |
| 55 // ResourceObjectBase overries. | 56 // ResourceObjectBase overries. |
| 56 virtual ppapi::thunk::PPB_Broker_API* AsPPB_Broker_API() OVERRIDE; | 57 virtual PPB_Broker_API* AsPPB_Broker_API() OVERRIDE; |
| 57 | 58 |
| 58 // PPB_Broker_API implementation. | 59 // PPB_Broker_API implementation. |
| 59 virtual int32_t Connect(PP_CompletionCallback connect_callback) OVERRIDE; | 60 virtual int32_t Connect(PP_CompletionCallback connect_callback) OVERRIDE; |
| 60 virtual int32_t GetHandle(int32_t* handle) OVERRIDE; | 61 virtual int32_t GetHandle(int32_t* handle) OVERRIDE; |
| 61 | 62 |
| 62 // Called by the proxy when the host side has completed the request. | 63 // Called by the proxy when the host side has completed the request. |
| 63 void ConnectComplete(IPC::PlatformFileForTransit socket_handle, | 64 void ConnectComplete(IPC::PlatformFileForTransit socket_handle, |
| 64 int32_t result); | 65 int32_t result); |
| 65 | 66 |
| 66 private: | 67 private: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 89 // TODO(brettw) the callbacks at this level should be refactored with a | 90 // TODO(brettw) the callbacks at this level should be refactored with a |
| 90 // more automatic tracking system like we have in the renderer. | 91 // more automatic tracking system like we have in the renderer. |
| 91 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( | 92 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( |
| 92 current_connect_callback_.func, current_connect_callback_.user_data, | 93 current_connect_callback_.func, current_connect_callback_.user_data, |
| 93 static_cast<int32_t>(PP_ERROR_ABORTED))); | 94 static_cast<int32_t>(PP_ERROR_ABORTED))); |
| 94 } | 95 } |
| 95 | 96 |
| 96 socket_handle_ = base::kInvalidPlatformFileValue; | 97 socket_handle_ = base::kInvalidPlatformFileValue; |
| 97 } | 98 } |
| 98 | 99 |
| 99 ppapi::thunk::PPB_Broker_API* Broker::AsPPB_Broker_API() { | 100 PPB_Broker_API* Broker::AsPPB_Broker_API() { |
| 100 return this; | 101 return this; |
| 101 } | 102 } |
| 102 | 103 |
| 103 int32_t Broker::Connect(PP_CompletionCallback connect_callback) { | 104 int32_t Broker::Connect(PP_CompletionCallback connect_callback) { |
| 104 if (!connect_callback.func) { | 105 if (!connect_callback.func) { |
| 105 // Synchronous calls are not supported. | 106 // Synchronous calls are not supported. |
| 106 return PP_ERROR_BADARGUMENT; | 107 return PP_ERROR_BADARGUMENT; |
| 107 } | 108 } |
| 108 | 109 |
| 109 if (current_connect_callback_.func) | 110 if (current_connect_callback_.func) |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 } | 198 } |
| 198 | 199 |
| 199 void PPB_Broker_Proxy::OnMsgCreate(PP_Instance instance, | 200 void PPB_Broker_Proxy::OnMsgCreate(PP_Instance instance, |
| 200 HostResource* result_resource) { | 201 HostResource* result_resource) { |
| 201 result_resource->SetHostResource( | 202 result_resource->SetHostResource( |
| 202 instance, | 203 instance, |
| 203 ppb_broker_target()->CreateTrusted(instance)); | 204 ppb_broker_target()->CreateTrusted(instance)); |
| 204 } | 205 } |
| 205 | 206 |
| 206 void PPB_Broker_Proxy::OnMsgConnect(const HostResource& broker) { | 207 void PPB_Broker_Proxy::OnMsgConnect(const HostResource& broker) { |
| 207 CompletionCallback callback = callback_factory_.NewOptionalCallback( | 208 EnterHostFromHostResourceForceCallback<PPB_Broker_API> enter( |
| 209 broker, callback_factory_, |
| 208 &PPB_Broker_Proxy::ConnectCompleteInHost, broker); | 210 &PPB_Broker_Proxy::ConnectCompleteInHost, broker); |
| 209 | 211 if (enter.succeeded()) |
| 210 int32_t result = ppb_broker_target()->Connect( | 212 enter.SetResult(enter.object()->Connect(enter.callback())); |
| 211 broker.host_resource(), | |
| 212 callback.pp_completion_callback()); | |
| 213 if (result != PP_OK_COMPLETIONPENDING) | |
| 214 callback.Run(result); | |
| 215 } | 213 } |
| 216 | 214 |
| 217 // Called in the plugin to handle the connect callback. | 215 // Called in the plugin to handle the connect callback. |
| 218 // The proxy owns the handle and transfers it to the Broker. At that point, | 216 // The proxy owns the handle and transfers it to the Broker. At that point, |
| 219 // the plugin owns the handle and is responsible for closing it. | 217 // the plugin owns the handle and is responsible for closing it. |
| 220 // The caller guarantees that socket_handle is not valid if result is not PP_OK. | 218 // The caller guarantees that socket_handle is not valid if result is not PP_OK. |
| 221 void PPB_Broker_Proxy::OnMsgConnectComplete( | 219 void PPB_Broker_Proxy::OnMsgConnectComplete( |
| 222 const HostResource& resource, | 220 const HostResource& resource, |
| 223 IPC::PlatformFileForTransit socket_handle, | 221 IPC::PlatformFileForTransit socket_handle, |
| 224 int32_t result) { | 222 int32_t result) { |
| 225 DCHECK(result == PP_OK || | 223 DCHECK(result == PP_OK || |
| 226 socket_handle == IPC::InvalidPlatformFileForTransit()); | 224 socket_handle == IPC::InvalidPlatformFileForTransit()); |
| 227 | 225 |
| 228 EnterPluginFromHostResource<ppapi::thunk::PPB_Broker_API> enter(resource); | 226 EnterPluginFromHostResource<PPB_Broker_API> enter(resource); |
| 229 if (enter.failed()) { | 227 if (enter.failed()) { |
| 230 // As in Broker::ConnectComplete, we need to close the resource on error. | 228 // As in Broker::ConnectComplete, we need to close the resource on error. |
| 231 base::SyncSocket temp_socket( | 229 base::SyncSocket temp_socket( |
| 232 IPC::PlatformFileForTransitToPlatformFile(socket_handle)); | 230 IPC::PlatformFileForTransitToPlatformFile(socket_handle)); |
| 233 } else { | 231 } else { |
| 234 static_cast<Broker*>(enter.object())->ConnectComplete(socket_handle, | 232 static_cast<Broker*>(enter.object())->ConnectComplete(socket_handle, |
| 235 result); | 233 result); |
| 236 } | 234 } |
| 237 } | 235 } |
| 238 | 236 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // The easiest way to clean it up is to just put it in an object | 270 // The easiest way to clean it up is to just put it in an object |
| 273 // and then close it. This failure case is not performance critical. | 271 // and then close it. This failure case is not performance critical. |
| 274 // The handle could still leak if Send succeeded but the IPC later failed. | 272 // The handle could still leak if Send succeeded but the IPC later failed. |
| 275 base::SyncSocket temp_socket( | 273 base::SyncSocket temp_socket( |
| 276 IPC::PlatformFileForTransitToPlatformFile(foreign_socket_handle)); | 274 IPC::PlatformFileForTransitToPlatformFile(foreign_socket_handle)); |
| 277 } | 275 } |
| 278 } | 276 } |
| 279 | 277 |
| 280 } // namespace proxy | 278 } // namespace proxy |
| 281 } // namespace pp | 279 } // namespace pp |
| OLD | NEW |