| 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/ppapi_messages.h" | 11 #include "ppapi/proxy/ppapi_messages.h" |
| 12 #include "ppapi/thunk/ppb_broker_api.h" | 12 #include "ppapi/thunk/ppb_broker_api.h" |
| 13 #include "ppapi/thunk/enter.h" | 13 #include "ppapi/thunk/enter.h" |
| 14 #include "ppapi/thunk/resource_creation_api.h" |
| 14 #include "ppapi/thunk/thunk.h" | 15 #include "ppapi/thunk/thunk.h" |
| 15 | 16 |
| 16 using ppapi::thunk::PPB_Broker_API; | 17 using ppapi::thunk::PPB_Broker_API; |
| 17 | 18 |
| 18 namespace ppapi { | 19 namespace ppapi { |
| 19 namespace proxy { | 20 namespace proxy { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 base::PlatformFile IntToPlatformFile(int32_t handle) { | 24 base::PlatformFile IntToPlatformFile(int32_t handle) { |
| 24 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 25 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); | 26 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| 26 #elif defined(OS_POSIX) | 27 #elif defined(OS_POSIX) |
| 27 return handle; | 28 return handle; |
| 28 #else | 29 #else |
| 29 #error Not implemented. | 30 #error Not implemented. |
| 30 #endif | 31 #endif |
| 31 } | 32 } |
| 32 | 33 |
| 33 int32_t PlatformFileToInt(base::PlatformFile handle) { | 34 int32_t PlatformFileToInt(base::PlatformFile handle) { |
| 34 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 35 return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle)); | 36 return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle)); |
| 36 #elif defined(OS_POSIX) | 37 #elif defined(OS_POSIX) |
| 37 return handle; | 38 return handle; |
| 38 #else | 39 #else |
| 39 #error Not implemented. | 40 #error Not implemented. |
| 40 #endif | 41 #endif |
| 41 } | 42 } |
| 42 | 43 |
| 43 InterfaceProxy* CreateBrokerProxy(Dispatcher* dispatcher, | |
| 44 const void* target_interface) { | |
| 45 return new PPB_Broker_Proxy(dispatcher, target_interface); | |
| 46 } | |
| 47 | |
| 48 } // namespace | 44 } // namespace |
| 49 | 45 |
| 50 class Broker : public PPB_Broker_API, public Resource { | 46 class Broker : public PPB_Broker_API, public Resource { |
| 51 public: | 47 public: |
| 52 explicit Broker(const HostResource& resource); | 48 explicit Broker(const HostResource& resource); |
| 53 virtual ~Broker(); | 49 virtual ~Broker(); |
| 54 | 50 |
| 55 // Resource overries. | 51 // Resource overries. |
| 56 virtual PPB_Broker_API* AsPPB_Broker_API() OVERRIDE; | 52 virtual PPB_Broker_API* AsPPB_Broker_API() OVERRIDE; |
| 57 | 53 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 136 } |
| 141 | 137 |
| 142 if (!current_connect_callback_.func) { | 138 if (!current_connect_callback_.func) { |
| 143 // The handle might leak if the plugin never calls GetHandle(). | 139 // The handle might leak if the plugin never calls GetHandle(). |
| 144 return; | 140 return; |
| 145 } | 141 } |
| 146 | 142 |
| 147 PP_RunAndClearCompletionCallback(¤t_connect_callback_, result); | 143 PP_RunAndClearCompletionCallback(¤t_connect_callback_, result); |
| 148 } | 144 } |
| 149 | 145 |
| 150 PPB_Broker_Proxy::PPB_Broker_Proxy(Dispatcher* dispatcher, | 146 PPB_Broker_Proxy::PPB_Broker_Proxy(Dispatcher* dispatcher) |
| 151 const void* target_interface) | 147 : InterfaceProxy(dispatcher), |
| 152 : InterfaceProxy(dispatcher, target_interface) , | |
| 153 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)){ | 148 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)){ |
| 154 } | 149 } |
| 155 | 150 |
| 156 PPB_Broker_Proxy::~PPB_Broker_Proxy() { | 151 PPB_Broker_Proxy::~PPB_Broker_Proxy() { |
| 157 } | 152 } |
| 158 | 153 |
| 159 // static | 154 // static |
| 160 const InterfaceProxy::Info* PPB_Broker_Proxy::GetInfo() { | |
| 161 static const Info info = { | |
| 162 ppapi::thunk::GetPPB_Broker_Thunk(), | |
| 163 PPB_BROKER_TRUSTED_INTERFACE, | |
| 164 INTERFACE_ID_PPB_BROKER, | |
| 165 true, | |
| 166 &CreateBrokerProxy, | |
| 167 }; | |
| 168 return &info; | |
| 169 } | |
| 170 | |
| 171 // static | |
| 172 PP_Resource PPB_Broker_Proxy::CreateProxyResource(PP_Instance instance) { | 155 PP_Resource PPB_Broker_Proxy::CreateProxyResource(PP_Instance instance) { |
| 173 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 156 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 174 if (!dispatcher) | 157 if (!dispatcher) |
| 175 return 0; | 158 return 0; |
| 176 | 159 |
| 177 HostResource result; | 160 HostResource result; |
| 178 dispatcher->Send(new PpapiHostMsg_PPBBroker_Create( | 161 dispatcher->Send(new PpapiHostMsg_PPBBroker_Create( |
| 179 INTERFACE_ID_PPB_BROKER, instance, &result)); | 162 INTERFACE_ID_PPB_BROKER, instance, &result)); |
| 180 if (result.is_null()) | 163 if (result.is_null()) |
| 181 return 0; | 164 return 0; |
| 182 return (new Broker(result))->GetReference(); | 165 return (new Broker(result))->GetReference(); |
| 183 } | 166 } |
| 184 | 167 |
| 185 bool PPB_Broker_Proxy::OnMessageReceived(const IPC::Message& msg) { | 168 bool PPB_Broker_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 186 bool handled = true; | 169 bool handled = true; |
| 187 IPC_BEGIN_MESSAGE_MAP(PPB_Broker_Proxy, msg) | 170 IPC_BEGIN_MESSAGE_MAP(PPB_Broker_Proxy, msg) |
| 188 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_Create, OnMsgCreate) | 171 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_Create, OnMsgCreate) |
| 189 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_Connect, OnMsgConnect) | 172 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_Connect, OnMsgConnect) |
| 190 IPC_MESSAGE_HANDLER(PpapiMsg_PPBBroker_ConnectComplete, | 173 IPC_MESSAGE_HANDLER(PpapiMsg_PPBBroker_ConnectComplete, |
| 191 OnMsgConnectComplete) | 174 OnMsgConnectComplete) |
| 192 IPC_MESSAGE_UNHANDLED(handled = false) | 175 IPC_MESSAGE_UNHANDLED(handled = false) |
| 193 IPC_END_MESSAGE_MAP() | 176 IPC_END_MESSAGE_MAP() |
| 194 return handled; | 177 return handled; |
| 195 } | 178 } |
| 196 | 179 |
| 197 void PPB_Broker_Proxy::OnMsgCreate(PP_Instance instance, | 180 void PPB_Broker_Proxy::OnMsgCreate(PP_Instance instance, |
| 198 HostResource* result_resource) { | 181 HostResource* result_resource) { |
| 199 result_resource->SetHostResource( | 182 thunk::EnterResourceCreation enter(instance); |
| 200 instance, | 183 if (enter.succeeded()) { |
| 201 ppb_broker_target()->CreateTrusted(instance)); | 184 result_resource->SetHostResource( |
| 185 instance, |
| 186 enter.functions()->CreateBroker(instance)); |
| 187 } |
| 202 } | 188 } |
| 203 | 189 |
| 204 void PPB_Broker_Proxy::OnMsgConnect(const HostResource& broker) { | 190 void PPB_Broker_Proxy::OnMsgConnect(const HostResource& broker) { |
| 205 EnterHostFromHostResourceForceCallback<PPB_Broker_API> enter( | 191 EnterHostFromHostResourceForceCallback<PPB_Broker_API> enter( |
| 206 broker, callback_factory_, | 192 broker, callback_factory_, |
| 207 &PPB_Broker_Proxy::ConnectCompleteInHost, broker); | 193 &PPB_Broker_Proxy::ConnectCompleteInHost, broker); |
| 208 if (enter.succeeded()) | 194 if (enter.succeeded()) |
| 209 enter.SetResult(enter.object()->Connect(enter.callback())); | 195 enter.SetResult(enter.object()->Connect(enter.callback())); |
| 210 } | 196 } |
| 211 | 197 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 233 | 219 |
| 234 // Callback on the host side. | 220 // Callback on the host side. |
| 235 // Transfers ownership of the handle to the plugin side. This function must | 221 // Transfers ownership of the handle to the plugin side. This function must |
| 236 // either successfully call the callback or close the handle. | 222 // either successfully call the callback or close the handle. |
| 237 void PPB_Broker_Proxy::ConnectCompleteInHost(int32_t result, | 223 void PPB_Broker_Proxy::ConnectCompleteInHost(int32_t result, |
| 238 const HostResource& broker) { | 224 const HostResource& broker) { |
| 239 IPC::PlatformFileForTransit foreign_socket_handle = | 225 IPC::PlatformFileForTransit foreign_socket_handle = |
| 240 IPC::InvalidPlatformFileForTransit(); | 226 IPC::InvalidPlatformFileForTransit(); |
| 241 if (result == PP_OK) { | 227 if (result == PP_OK) { |
| 242 int32_t socket_handle = PlatformFileToInt(base::kInvalidPlatformFileValue); | 228 int32_t socket_handle = PlatformFileToInt(base::kInvalidPlatformFileValue); |
| 243 result = ppb_broker_target()->GetHandle(broker.host_resource(), | 229 EnterHostFromHostResource<PPB_Broker_API> enter(broker); |
| 244 &socket_handle); | 230 if (enter.succeeded()) |
| 231 result = enter.object()->GetHandle(&socket_handle); |
| 245 DCHECK(result == PP_OK || | 232 DCHECK(result == PP_OK || |
| 246 socket_handle == PlatformFileToInt(base::kInvalidPlatformFileValue)); | 233 socket_handle == PlatformFileToInt(base::kInvalidPlatformFileValue)); |
| 247 | 234 |
| 248 if (result == PP_OK) { | 235 if (result == PP_OK) { |
| 249 foreign_socket_handle = | 236 foreign_socket_handle = |
| 250 dispatcher()->ShareHandleWithRemote(IntToPlatformFile(socket_handle), | 237 dispatcher()->ShareHandleWithRemote(IntToPlatformFile(socket_handle), |
| 251 true); | 238 true); |
| 252 if (foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) { | 239 if (foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) { |
| 253 result = PP_ERROR_FAILED; | 240 result = PP_ERROR_FAILED; |
| 254 // Assume the local handle was closed even if the foreign handle could | 241 // Assume the local handle was closed even if the foreign handle could |
| (...skipping 12 matching lines...) Expand all Loading... |
| 267 // The easiest way to clean it up is to just put it in an object | 254 // The easiest way to clean it up is to just put it in an object |
| 268 // and then close it. This failure case is not performance critical. | 255 // and then close it. This failure case is not performance critical. |
| 269 // The handle could still leak if Send succeeded but the IPC later failed. | 256 // The handle could still leak if Send succeeded but the IPC later failed. |
| 270 base::SyncSocket temp_socket( | 257 base::SyncSocket temp_socket( |
| 271 IPC::PlatformFileForTransitToPlatformFile(foreign_socket_handle)); | 258 IPC::PlatformFileForTransitToPlatformFile(foreign_socket_handle)); |
| 272 } | 259 } |
| 273 } | 260 } |
| 274 | 261 |
| 275 } // namespace proxy | 262 } // namespace proxy |
| 276 } // namespace ppapi | 263 } // namespace ppapi |
| OLD | NEW |