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