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