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.
5 #include "ppapi/cpp/private/content_decryptor_private.h"
7 #include <cstring> // memcpy
9 #include "ppapi/c/ppb_var.h"
10 #include "ppapi/c/private/ppb_content_decryptor_private.h"
11 #include "ppapi/c/private/ppp_content_decryptor_private.h"
12 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/instance_handle.h"
14 #include "ppapi/cpp/logging.h"
15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/module_impl.h"
17 #include "ppapi/cpp/var.h"
18 #include "ppapi/cpp/var_array.h"
24 static const char kPPPContentDecryptorInterface[] =
25 PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
27 void Initialize(PP_Instance instance,
28 PP_Var key_system_arg) {
30 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
34 pp::Var key_system_var(pp::PASS_REF, key_system_arg);
35 if (!key_system_var.is_string())
38 static_cast<ContentDecryptor_Private*>(object)->Initialize(
39 key_system_var.AsString());
42 void SetServerCertificate(PP_Instance instance,
44 PP_Var server_certificate_arg) {
46 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
50 pp::Var server_certificate_var(server_certificate_arg);
51 if (!server_certificate_var.is_array_buffer())
53 pp::VarArrayBuffer server_certificate(server_certificate_var);
55 static_cast<ContentDecryptor_Private*>(object)
56 ->SetServerCertificate(promise_id, server_certificate);
59 void CreateSession(PP_Instance instance,
61 PP_Var init_data_type_arg,
63 PP_SessionType session_type) {
65 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
69 pp::Var init_data_type_var(pp::PASS_REF, init_data_type_arg);
70 if (!init_data_type_var.is_string())
73 pp::Var init_data_var(pp::PASS_REF, init_data_arg);
74 if (!init_data_var.is_array_buffer())
76 pp::VarArrayBuffer init_data_array_buffer(init_data_var);
78 static_cast<ContentDecryptor_Private*>(object)
79 ->CreateSession(promise_id,
80 init_data_type_var.AsString(),
81 init_data_array_buffer,
85 void LoadSession(PP_Instance instance,
87 PP_Var web_session_id_arg) {
89 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
93 pp::Var web_session_id_var(web_session_id_arg);
94 if (!web_session_id_var.is_string())
97 static_cast<ContentDecryptor_Private*>(object)
98 ->LoadSession(promise_id, web_session_id_var.AsString());
101 void UpdateSession(PP_Instance instance,
103 PP_Var web_session_id_arg,
104 PP_Var response_arg) {
106 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
110 pp::Var web_session_id_var(web_session_id_arg);
111 if (!web_session_id_var.is_string())
114 pp::Var response_var(response_arg);
115 if (!response_var.is_array_buffer())
117 pp::VarArrayBuffer response(response_var);
119 static_cast<ContentDecryptor_Private*>(object)
120 ->UpdateSession(promise_id, web_session_id_var.AsString(), response);
123 void CloseSession(PP_Instance instance,
125 PP_Var web_session_id_arg) {
127 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
131 pp::Var web_session_id_var(web_session_id_arg);
132 if (!web_session_id_var.is_string())
135 static_cast<ContentDecryptor_Private*>(object)
136 ->CloseSession(promise_id, web_session_id_var.AsString());
139 void RemoveSession(PP_Instance instance,
141 PP_Var web_session_id_arg) {
143 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
147 pp::Var web_session_id_var(web_session_id_arg);
148 if (!web_session_id_var.is_string())
151 static_cast<ContentDecryptor_Private*>(object)
152 ->RemoveSession(promise_id, web_session_id_var.AsString());
155 void GetUsableKeyIds(PP_Instance instance,
157 PP_Var web_session_id_arg) {
159 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
163 pp::Var web_session_id_var(web_session_id_arg);
164 if (!web_session_id_var.is_string())
167 static_cast<ContentDecryptor_Private*>(object)
168 ->GetUsableKeyIds(promise_id, web_session_id_var.AsString());
171 void Decrypt(PP_Instance instance,
172 PP_Resource encrypted_resource,
173 const PP_EncryptedBlockInfo* encrypted_block_info) {
174 pp::Buffer_Dev encrypted_block(encrypted_resource);
177 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
181 static_cast<ContentDecryptor_Private*>(object)->Decrypt(
183 *encrypted_block_info);
186 void InitializeAudioDecoder(
187 PP_Instance instance,
188 const PP_AudioDecoderConfig* decoder_config,
189 PP_Resource extra_data_resource) {
190 pp::Buffer_Dev extra_data_buffer(extra_data_resource);
193 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
197 static_cast<ContentDecryptor_Private*>(object)->InitializeAudioDecoder(
202 void InitializeVideoDecoder(
203 PP_Instance instance,
204 const PP_VideoDecoderConfig* decoder_config,
205 PP_Resource extra_data_resource) {
206 pp::Buffer_Dev extra_data_buffer(extra_data_resource);
209 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
213 static_cast<ContentDecryptor_Private*>(object)->InitializeVideoDecoder(
218 void DeinitializeDecoder(PP_Instance instance,
219 PP_DecryptorStreamType decoder_type,
220 uint32_t request_id) {
222 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
225 static_cast<ContentDecryptor_Private*>(object)->DeinitializeDecoder(
230 void ResetDecoder(PP_Instance instance,
231 PP_DecryptorStreamType decoder_type,
232 uint32_t request_id) {
234 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
237 static_cast<ContentDecryptor_Private*>(object)->ResetDecoder(decoder_type,
241 void DecryptAndDecode(PP_Instance instance,
242 PP_DecryptorStreamType decoder_type,
243 PP_Resource encrypted_resource,
244 const PP_EncryptedBlockInfo* encrypted_block_info) {
245 pp::Buffer_Dev encrypted_buffer(encrypted_resource);
248 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
252 static_cast<ContentDecryptor_Private*>(object)->DecryptAndDecode(
255 *encrypted_block_info);
258 const PPP_ContentDecryptor_Private ppp_content_decryptor = {
260 &SetServerCertificate,
268 &InitializeAudioDecoder,
269 &InitializeVideoDecoder,
270 &DeinitializeDecoder,
274 template <> const char* interface_name<PPB_ContentDecryptor_Private>() {
275 return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
280 ContentDecryptor_Private::ContentDecryptor_Private(Instance* instance)
281 : associated_instance_(instance) {
282 Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface,
283 &ppp_content_decryptor);
284 instance->AddPerInstanceObject(kPPPContentDecryptorInterface, this);
287 ContentDecryptor_Private::~ContentDecryptor_Private() {
288 Instance::RemovePerInstanceObject(associated_instance_,
289 kPPPContentDecryptorInterface,
293 void ContentDecryptor_Private::PromiseResolved(uint32_t promise_id) {
294 if (has_interface<PPB_ContentDecryptor_Private>()) {
295 get_interface<PPB_ContentDecryptor_Private>()->PromiseResolved(
296 associated_instance_.pp_instance(), promise_id);
300 void ContentDecryptor_Private::PromiseResolvedWithSession(
302 const std::string& web_session_id) {
303 if (has_interface<PPB_ContentDecryptor_Private>()) {
304 pp::Var web_session_id_var(web_session_id);
305 get_interface<PPB_ContentDecryptor_Private>()->PromiseResolvedWithSession(
306 associated_instance_.pp_instance(),
308 web_session_id_var.pp_var());
312 void ContentDecryptor_Private::PromiseResolvedWithKeyIds(
314 const std::vector<std::vector<uint8_t> >& key_ids) {
315 if (has_interface<PPB_ContentDecryptor_Private>()) {
316 pp::VarArray key_ids_array = pp::VarArray();
317 key_ids_array.SetLength(key_ids.size());
318 for (size_t i = 0; i < key_ids.size(); ++i) {
319 const std::vector<uint8_t>& entry = key_ids[i];
320 pp::VarArrayBuffer array_buffer(entry.size());
321 memcpy(array_buffer.Map(), &entry[0], entry.size());
322 key_ids_array.Set(i, array_buffer);
324 get_interface<PPB_ContentDecryptor_Private>()->PromiseResolvedWithKeyIds(
325 associated_instance_.pp_instance(), promise_id, key_ids_array.pp_var());
329 void ContentDecryptor_Private::PromiseRejected(
331 PP_CdmExceptionCode exception_code,
332 uint32_t system_code,
333 const std::string& error_description) {
334 if (has_interface<PPB_ContentDecryptor_Private>()) {
335 pp::Var error_description_var(error_description);
336 get_interface<PPB_ContentDecryptor_Private>()->PromiseRejected(
337 associated_instance_.pp_instance(),
341 error_description_var.pp_var());
345 void ContentDecryptor_Private::SessionMessage(
346 const std::string& web_session_id,
347 pp::VarArrayBuffer message,
348 const std::string& destination_url) {
349 if (has_interface<PPB_ContentDecryptor_Private>()) {
350 pp::Var web_session_id_var(web_session_id);
351 pp::Var destination_url_var(destination_url);
352 get_interface<PPB_ContentDecryptor_Private>()->SessionMessage(
353 associated_instance_.pp_instance(),
354 web_session_id_var.pp_var(),
356 destination_url_var.pp_var());
360 void ContentDecryptor_Private::SessionKeysChange(
361 const std::string& web_session_id,
362 bool has_additional_usable_key) {
363 if (has_interface<PPB_ContentDecryptor_Private>()) {
364 pp::Var web_session_id_var(web_session_id);
365 get_interface<PPB_ContentDecryptor_Private>()->SessionKeysChange(
366 associated_instance_.pp_instance(),
367 web_session_id_var.pp_var(),
368 PP_FromBool(has_additional_usable_key));
372 void ContentDecryptor_Private::SessionExpirationChange(
373 const std::string& web_session_id,
374 PP_Time new_expiry_time) {
375 if (has_interface<PPB_ContentDecryptor_Private>()) {
376 pp::Var web_session_id_var(web_session_id);
377 get_interface<PPB_ContentDecryptor_Private>()->SessionExpirationChange(
378 associated_instance_.pp_instance(),
379 web_session_id_var.pp_var(),
384 void ContentDecryptor_Private::SessionReady(const std::string& web_session_id) {
385 if (has_interface<PPB_ContentDecryptor_Private>()) {
386 pp::Var web_session_id_var(web_session_id);
387 get_interface<PPB_ContentDecryptor_Private>()->SessionReady(
388 associated_instance_.pp_instance(), web_session_id_var.pp_var());
392 void ContentDecryptor_Private::SessionClosed(
393 const std::string& web_session_id) {
394 if (has_interface<PPB_ContentDecryptor_Private>()) {
395 pp::Var web_session_id_var(web_session_id);
396 get_interface<PPB_ContentDecryptor_Private>()->SessionClosed(
397 associated_instance_.pp_instance(), web_session_id_var.pp_var());
401 void ContentDecryptor_Private::SessionError(
402 const std::string& web_session_id,
403 PP_CdmExceptionCode exception_code,
404 uint32_t system_code,
405 const std::string& error_description) {
406 if (has_interface<PPB_ContentDecryptor_Private>()) {
407 pp::Var web_session_id_var(web_session_id);
408 pp::Var error_description_var(error_description);
409 get_interface<PPB_ContentDecryptor_Private>()->SessionError(
410 associated_instance_.pp_instance(),
411 web_session_id_var.pp_var(),
414 error_description_var.pp_var());
418 void ContentDecryptor_Private::DeliverBlock(
419 pp::Buffer_Dev decrypted_block,
420 const PP_DecryptedBlockInfo& decrypted_block_info) {
421 if (has_interface<PPB_ContentDecryptor_Private>()) {
422 get_interface<PPB_ContentDecryptor_Private>()->DeliverBlock(
423 associated_instance_.pp_instance(),
424 decrypted_block.pp_resource(),
425 &decrypted_block_info);
429 void ContentDecryptor_Private::DecoderInitializeDone(
430 PP_DecryptorStreamType decoder_type,
433 if (has_interface<PPB_ContentDecryptor_Private>()) {
434 get_interface<PPB_ContentDecryptor_Private>()->DecoderInitializeDone(
435 associated_instance_.pp_instance(),
438 PP_FromBool(success));
442 void ContentDecryptor_Private::DecoderDeinitializeDone(
443 PP_DecryptorStreamType decoder_type,
444 uint32_t request_id) {
445 if (has_interface<PPB_ContentDecryptor_Private>()) {
446 get_interface<PPB_ContentDecryptor_Private>()->DecoderDeinitializeDone(
447 associated_instance_.pp_instance(),
453 void ContentDecryptor_Private::DecoderResetDone(
454 PP_DecryptorStreamType decoder_type,
455 uint32_t request_id) {
456 if (has_interface<PPB_ContentDecryptor_Private>()) {
457 get_interface<PPB_ContentDecryptor_Private>()->DecoderResetDone(
458 associated_instance_.pp_instance(),
464 void ContentDecryptor_Private::DeliverFrame(
465 pp::Buffer_Dev decrypted_frame,
466 const PP_DecryptedFrameInfo& decrypted_frame_info) {
467 if (has_interface<PPB_ContentDecryptor_Private>()) {
468 get_interface<PPB_ContentDecryptor_Private>()->DeliverFrame(
469 associated_instance_.pp_instance(),
470 decrypted_frame.pp_resource(),
471 &decrypted_frame_info);
475 void ContentDecryptor_Private::DeliverSamples(
476 pp::Buffer_Dev audio_frames,
477 const PP_DecryptedSampleInfo& decrypted_sample_info) {
478 if (has_interface<PPB_ContentDecryptor_Private>()) {
479 get_interface<PPB_ContentDecryptor_Private>()->DeliverSamples(
480 associated_instance_.pp_instance(),
481 audio_frames.pp_resource(),
482 &decrypted_sample_info);