OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/proxy/ppp_content_decryptor_private_proxy.h" |
| 6 |
| 7 #include "base/platform_file.h" |
| 8 #include "ppapi/c/pp_bool.h" |
| 9 #include "ppapi/c/ppb_core.h" |
| 10 #include "ppapi/proxy/host_dispatcher.h" |
| 11 #include "ppapi/proxy/plugin_globals.h" |
| 12 #include "ppapi/proxy/plugin_resource_tracker.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" |
| 14 #include "ppapi/proxy/ppb_buffer_proxy.h" |
| 15 #include "ppapi/proxy/serialized_var.h" |
| 16 #include "ppapi/shared_impl/var_tracker.h" |
| 17 #include "ppapi/thunk/enter.h" |
| 18 #include "ppapi/thunk/ppb_buffer_api.h" |
| 19 #include "ppapi/thunk/ppb_buffer_trusted_api.h" |
| 20 #include "ppapi/thunk/ppb_instance_api.h" |
| 21 #include "ppapi/thunk/thunk.h" |
| 22 |
| 23 using ppapi::thunk::EnterResourceNoLock; |
| 24 using ppapi::thunk::PPB_Buffer_API; |
| 25 using ppapi::thunk::PPB_BufferTrusted_API; |
| 26 using ppapi::thunk::PPB_Instance_API; |
| 27 |
| 28 namespace ppapi { |
| 29 namespace proxy { |
| 30 |
| 31 namespace { |
| 32 |
| 33 PP_Bool DescribeHostBufferResource(PP_Resource resource, uint32_t* size) { |
| 34 EnterResourceNoLock<PPB_Buffer_API> enter(resource, true); |
| 35 if (enter.failed()) |
| 36 return PP_FALSE; |
| 37 return enter.object()->Describe(size); |
| 38 } |
| 39 |
| 40 // TODO(dmichael): Refactor so this handle sharing code is in one place. |
| 41 PP_Bool ShareHostBufferResourceToPlugin( |
| 42 HostDispatcher* dispatcher, |
| 43 PP_Resource resource, |
| 44 base::SharedMemoryHandle* shared_mem_handle) { |
| 45 if (!dispatcher || resource == 0 || !shared_mem_handle) |
| 46 return PP_FALSE; |
| 47 EnterResourceNoLock<PPB_BufferTrusted_API> enter(resource, true); |
| 48 if (enter.failed()) |
| 49 return PP_FALSE; |
| 50 int handle; |
| 51 int32_t result = enter.object()->GetSharedMemory(&handle); |
| 52 if (result != PP_OK) |
| 53 return PP_FALSE; |
| 54 base::PlatformFile platform_file = |
| 55 #if defined(OS_WIN) |
| 56 reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| 57 #elif defined(OS_POSIX) |
| 58 handle; |
| 59 #else |
| 60 #error Not implemented. |
| 61 #endif |
| 62 |
| 63 *shared_mem_handle = dispatcher->ShareHandleWithRemote(platform_file, false); |
| 64 return PP_TRUE; |
| 65 } |
| 66 |
| 67 // SerializedVarReceiveInput will decrement the reference count, but we want |
| 68 // to give the recipient a reference. This utility function takes care of that |
| 69 // work for the message handlers defined below. |
| 70 PP_Var ExtractReceivedVarAndAddRef(Dispatcher* dispatcher, |
| 71 SerializedVarReceiveInput* serialized_var) { |
| 72 PP_Var var = serialized_var->Get(dispatcher); |
| 73 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var); |
| 74 return var; |
| 75 } |
| 76 |
| 77 PP_Bool GenerateKeyRequest(PP_Instance instance, |
| 78 PP_Var key_system, |
| 79 PP_Var init_data) { |
| 80 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 81 if (!dispatcher) { |
| 82 NOTREACHED(); |
| 83 return PP_FALSE; |
| 84 } |
| 85 |
| 86 return PP_FromBool(dispatcher->Send( |
| 87 new PpapiMsg_PPPContentDecryptor_GenerateKeyRequest( |
| 88 API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE, |
| 89 instance, |
| 90 SerializedVarSendInput(dispatcher, key_system), |
| 91 SerializedVarSendInput(dispatcher, init_data)))); |
| 92 } |
| 93 |
| 94 PP_Bool AddKey(PP_Instance instance, |
| 95 PP_Var session_id, |
| 96 PP_Var key) { |
| 97 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 98 if (!dispatcher) { |
| 99 NOTREACHED(); |
| 100 return PP_FALSE; |
| 101 } |
| 102 |
| 103 return PP_FromBool(dispatcher->Send( |
| 104 new PpapiMsg_PPPContentDecryptor_AddKey( |
| 105 API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE, |
| 106 instance, |
| 107 SerializedVarSendInput(dispatcher, session_id), |
| 108 SerializedVarSendInput(dispatcher, key)))); |
| 109 } |
| 110 |
| 111 PP_Bool CancelKeyRequest(PP_Instance instance, PP_Var session_id) { |
| 112 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 113 if (!dispatcher) { |
| 114 NOTREACHED(); |
| 115 return PP_FALSE; |
| 116 } |
| 117 |
| 118 return PP_FromBool(dispatcher->Send( |
| 119 new PpapiMsg_PPPContentDecryptor_CancelKeyRequest( |
| 120 API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE, |
| 121 instance, |
| 122 SerializedVarSendInput(dispatcher, session_id)))); |
| 123 } |
| 124 |
| 125 PP_Bool Decrypt(PP_Instance instance, |
| 126 PP_Resource encrypted_block, |
| 127 int32_t request_id) { |
| 128 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 129 if (!dispatcher) { |
| 130 NOTREACHED(); |
| 131 return PP_FALSE; |
| 132 } |
| 133 const PPB_Core* core = static_cast<const PPB_Core*>( |
| 134 dispatcher->local_get_interface()(PPB_CORE_INTERFACE)); |
| 135 if (!core) { |
| 136 NOTREACHED(); |
| 137 return PP_FALSE; |
| 138 } |
| 139 |
| 140 // We need to take a ref on the resource now. The browser may drop |
| 141 // references once we return from here, but we're sending an asynchronous |
| 142 // message. The plugin side takes ownership of that reference. |
| 143 core->AddRefResource(encrypted_block); |
| 144 |
| 145 HostResource host_resource; |
| 146 host_resource.SetHostResource(instance, encrypted_block); |
| 147 |
| 148 uint32_t size = 0; |
| 149 if (DescribeHostBufferResource(encrypted_block, &size) == PP_FALSE) |
| 150 return PP_FALSE; |
| 151 |
| 152 base::SharedMemoryHandle handle; |
| 153 if (ShareHostBufferResourceToPlugin(dispatcher, |
| 154 encrypted_block, |
| 155 &handle) == PP_FALSE) |
| 156 return PP_FALSE; |
| 157 |
| 158 PPPDecryptor_Buffer buffer; |
| 159 buffer.resource = host_resource; |
| 160 buffer.handle = handle; |
| 161 buffer.size = size; |
| 162 |
| 163 return PP_FromBool(dispatcher->Send( |
| 164 new PpapiMsg_PPPContentDecryptor_Decrypt( |
| 165 API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE, |
| 166 instance, |
| 167 buffer, |
| 168 request_id))); |
| 169 } |
| 170 |
| 171 PP_Bool DecryptAndDecode(PP_Instance instance, |
| 172 PP_Resource encrypted_block, |
| 173 int32_t request_id) { |
| 174 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 175 if (!dispatcher) { |
| 176 NOTREACHED(); |
| 177 return PP_FALSE; |
| 178 } |
| 179 |
| 180 HostResource host_resource; |
| 181 host_resource.SetHostResource(instance, encrypted_block); |
| 182 |
| 183 return PP_FromBool(dispatcher->Send( |
| 184 new PpapiMsg_PPPContentDecryptor_DecryptAndDecode( |
| 185 API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE, |
| 186 instance, |
| 187 host_resource, |
| 188 request_id))); |
| 189 } |
| 190 |
| 191 static const PPP_ContentDecryptor_Private content_decryptor_interface = { |
| 192 &GenerateKeyRequest, |
| 193 &AddKey, |
| 194 &CancelKeyRequest, |
| 195 &Decrypt, |
| 196 &DecryptAndDecode |
| 197 }; |
| 198 |
| 199 InterfaceProxy* CreateContentDecryptorPPPProxy(Dispatcher* dispatcher) { |
| 200 return new PPP_ContentDecryptor_Private_Proxy(dispatcher); |
| 201 } |
| 202 |
| 203 } // namespace |
| 204 |
| 205 PPP_ContentDecryptor_Private_Proxy::PPP_ContentDecryptor_Private_Proxy( |
| 206 Dispatcher* dispatcher) |
| 207 : InterfaceProxy(dispatcher), |
| 208 ppp_decryptor_impl_(NULL) { |
| 209 if (dispatcher->IsPlugin()) { |
| 210 ppp_decryptor_impl_ = static_cast<const PPP_ContentDecryptor_Private*>( |
| 211 dispatcher->local_get_interface()( |
| 212 PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE)); |
| 213 } |
| 214 } |
| 215 |
| 216 PPP_ContentDecryptor_Private_Proxy::~PPP_ContentDecryptor_Private_Proxy() { |
| 217 } |
| 218 |
| 219 // static |
| 220 const PPP_ContentDecryptor_Private* |
| 221 PPP_ContentDecryptor_Private_Proxy::GetProxyInterface() { |
| 222 return &content_decryptor_interface; |
| 223 } |
| 224 |
| 225 bool PPP_ContentDecryptor_Private_Proxy::OnMessageReceived( |
| 226 const IPC::Message& msg) { |
| 227 bool handled = true; |
| 228 IPC_BEGIN_MESSAGE_MAP(PPP_ContentDecryptor_Private_Proxy, msg) |
| 229 IPC_MESSAGE_HANDLER(PpapiMsg_PPPContentDecryptor_GenerateKeyRequest, |
| 230 OnMsgGenerateKeyRequest) |
| 231 IPC_MESSAGE_HANDLER(PpapiMsg_PPPContentDecryptor_AddKey, |
| 232 OnMsgAddKey) |
| 233 IPC_MESSAGE_HANDLER(PpapiMsg_PPPContentDecryptor_CancelKeyRequest, |
| 234 OnMsgCancelKeyRequest) |
| 235 IPC_MESSAGE_HANDLER(PpapiMsg_PPPContentDecryptor_Decrypt, |
| 236 OnMsgDecrypt) |
| 237 IPC_MESSAGE_HANDLER(PpapiMsg_PPPContentDecryptor_DecryptAndDecode, |
| 238 OnMsgDecryptAndDecode) |
| 239 IPC_MESSAGE_UNHANDLED(handled = false) |
| 240 IPC_END_MESSAGE_MAP() |
| 241 DCHECK(handled); |
| 242 return handled; |
| 243 } |
| 244 |
| 245 void PPP_ContentDecryptor_Private_Proxy::OnMsgGenerateKeyRequest( |
| 246 PP_Instance instance, |
| 247 SerializedVarReceiveInput key_system, |
| 248 SerializedVarReceiveInput init_data) { |
| 249 if (ppp_decryptor_impl_) { |
| 250 CallWhileUnlocked(ppp_decryptor_impl_->GenerateKeyRequest, |
| 251 instance, |
| 252 ExtractReceivedVarAndAddRef(dispatcher(), &key_system), |
| 253 ExtractReceivedVarAndAddRef(dispatcher(), &init_data)); |
| 254 } |
| 255 } |
| 256 |
| 257 void PPP_ContentDecryptor_Private_Proxy::OnMsgAddKey( |
| 258 PP_Instance instance, |
| 259 SerializedVarReceiveInput session_id, |
| 260 SerializedVarReceiveInput key) { |
| 261 if (ppp_decryptor_impl_) { |
| 262 CallWhileUnlocked(ppp_decryptor_impl_->AddKey, |
| 263 instance, |
| 264 ExtractReceivedVarAndAddRef(dispatcher(), &session_id), |
| 265 ExtractReceivedVarAndAddRef(dispatcher(), &key)); |
| 266 } |
| 267 } |
| 268 |
| 269 void PPP_ContentDecryptor_Private_Proxy::OnMsgCancelKeyRequest( |
| 270 PP_Instance instance, |
| 271 SerializedVarReceiveInput session_id) { |
| 272 if (ppp_decryptor_impl_) { |
| 273 CallWhileUnlocked(ppp_decryptor_impl_->CancelKeyRequest, |
| 274 instance, |
| 275 ExtractReceivedVarAndAddRef(dispatcher(), &session_id)); |
| 276 } |
| 277 } |
| 278 |
| 279 void PPP_ContentDecryptor_Private_Proxy::OnMsgDecrypt( |
| 280 PP_Instance instance, |
| 281 const PPPDecryptor_Buffer& encrypted_buffer, |
| 282 int32_t request_id) { |
| 283 if (ppp_decryptor_impl_) { |
| 284 PP_Resource plugin_resource = |
| 285 PPB_Buffer_Proxy::AddProxyResource(encrypted_buffer.resource, |
| 286 encrypted_buffer.handle, |
| 287 encrypted_buffer.size); |
| 288 CallWhileUnlocked(ppp_decryptor_impl_->Decrypt, |
| 289 instance, |
| 290 plugin_resource, |
| 291 request_id); |
| 292 } |
| 293 } |
| 294 |
| 295 void PPP_ContentDecryptor_Private_Proxy::OnMsgDecryptAndDecode( |
| 296 PP_Instance instance, |
| 297 const HostResource& encrypted_block, |
| 298 int32_t request_id) { |
| 299 if (ppp_decryptor_impl_) { |
| 300 PP_Resource plugin_resource = |
| 301 PluginGlobals::Get()->plugin_resource_tracker()-> |
| 302 PluginResourceForHostResource(encrypted_block); |
| 303 CallWhileUnlocked(ppp_decryptor_impl_->DecryptAndDecode, |
| 304 instance, |
| 305 plugin_resource, |
| 306 request_id); |
| 307 } |
| 308 } |
| 309 |
| 310 } // namespace proxy |
| 311 } // namespace ppapi |
OLD | NEW |