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"
23 static const char kPPPContentDecryptorInterface[] =
24 PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
26 void Initialize(PP_Instance instance,
27 PP_Var key_system_arg,
28 PP_Bool can_challenge_platform) {
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(),
40 PP_ToBool(can_challenge_platform));
43 void GenerateKeyRequest(PP_Instance instance,
45 PP_Var init_data_arg) {
47 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
51 pp::Var type_var(pp::PASS_REF, type_arg);
52 if (!type_var.is_string())
55 pp::Var init_data_var(pp::PASS_REF, init_data_arg);
56 if (!init_data_var.is_array_buffer())
58 pp::VarArrayBuffer init_data_array_buffer(init_data_var);
60 static_cast<ContentDecryptor_Private*>(object)->GenerateKeyRequest(
62 init_data_array_buffer);
65 void AddKey(PP_Instance instance,
66 PP_Var session_id_arg,
68 PP_Var init_data_arg) {
70 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
74 pp::Var session_id_var(pp::PASS_REF, session_id_arg);
75 if (!session_id_var.is_string())
78 pp::Var key_var(pp::PASS_REF, key_arg);
79 if (!key_var.is_array_buffer())
81 pp::VarArrayBuffer key(key_var);
83 pp::Var init_data_var(pp::PASS_REF, init_data_arg);
84 if (!init_data_var.is_array_buffer())
86 pp::VarArrayBuffer init_data(init_data_var);
89 static_cast<ContentDecryptor_Private*>(object)->AddKey(
90 session_id_var.AsString(),
95 void CancelKeyRequest(PP_Instance instance, PP_Var session_id_arg) {
97 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
101 pp::Var session_id_var(pp::PASS_REF, session_id_arg);
102 if (!session_id_var.is_string())
105 static_cast<ContentDecryptor_Private*>(object)->CancelKeyRequest(
106 session_id_var.AsString());
110 void Decrypt(PP_Instance instance,
111 PP_Resource encrypted_resource,
112 const PP_EncryptedBlockInfo* encrypted_block_info) {
113 pp::Buffer_Dev encrypted_block(encrypted_resource);
116 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
120 static_cast<ContentDecryptor_Private*>(object)->Decrypt(
122 *encrypted_block_info);
125 void InitializeAudioDecoder(
126 PP_Instance instance,
127 const PP_AudioDecoderConfig* decoder_config,
128 PP_Resource extra_data_resource) {
129 pp::Buffer_Dev extra_data_buffer(extra_data_resource);
132 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
136 static_cast<ContentDecryptor_Private*>(object)->InitializeAudioDecoder(
141 void InitializeVideoDecoder(
142 PP_Instance instance,
143 const PP_VideoDecoderConfig* decoder_config,
144 PP_Resource extra_data_resource) {
145 pp::Buffer_Dev extra_data_buffer(extra_data_resource);
148 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
152 static_cast<ContentDecryptor_Private*>(object)->InitializeVideoDecoder(
157 void DeinitializeDecoder(PP_Instance instance,
158 PP_DecryptorStreamType decoder_type,
159 uint32_t request_id) {
161 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
164 static_cast<ContentDecryptor_Private*>(object)->DeinitializeDecoder(
169 void ResetDecoder(PP_Instance instance,
170 PP_DecryptorStreamType decoder_type,
171 uint32_t request_id) {
173 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
176 static_cast<ContentDecryptor_Private*>(object)->ResetDecoder(decoder_type,
180 void DecryptAndDecode(PP_Instance instance,
181 PP_DecryptorStreamType decoder_type,
182 PP_Resource encrypted_resource,
183 const PP_EncryptedBlockInfo* encrypted_block_info) {
184 pp::Buffer_Dev encrypted_buffer(encrypted_resource);
187 Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
191 static_cast<ContentDecryptor_Private*>(object)->DecryptAndDecode(
194 *encrypted_block_info);
197 const PPP_ContentDecryptor_Private ppp_content_decryptor = {
203 &InitializeAudioDecoder,
204 &InitializeVideoDecoder,
205 &DeinitializeDecoder,
210 template <> const char* interface_name<PPB_ContentDecryptor_Private>() {
211 return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
216 ContentDecryptor_Private::ContentDecryptor_Private(Instance* instance)
217 : associated_instance_(instance) {
218 Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface,
219 &ppp_content_decryptor);
220 instance->AddPerInstanceObject(kPPPContentDecryptorInterface, this);
223 ContentDecryptor_Private::~ContentDecryptor_Private() {
224 Instance::RemovePerInstanceObject(associated_instance_,
225 kPPPContentDecryptorInterface,
229 void ContentDecryptor_Private::KeyAdded(const std::string& key_system,
230 const std::string& session_id) {
231 if (has_interface<PPB_ContentDecryptor_Private>()) {
232 pp::Var key_system_var(key_system);
233 pp::Var session_id_var(session_id);
234 get_interface<PPB_ContentDecryptor_Private>()->KeyAdded(
235 associated_instance_.pp_instance(),
236 key_system_var.pp_var(),
237 session_id_var.pp_var());
241 void ContentDecryptor_Private::KeyMessage(const std::string& key_system,
242 const std::string& session_id,
243 pp::VarArrayBuffer message,
244 const std::string& default_url) {
245 if (has_interface<PPB_ContentDecryptor_Private>()) {
246 pp::Var key_system_var(key_system);
247 pp::Var session_id_var(session_id);
248 pp::Var default_url_var(default_url);
249 get_interface<PPB_ContentDecryptor_Private>()->KeyMessage(
250 associated_instance_.pp_instance(),
251 key_system_var.pp_var(),
252 session_id_var.pp_var(),
254 default_url_var.pp_var());
258 void ContentDecryptor_Private::KeyError(const std::string& key_system,
259 const std::string& session_id,
261 int32_t system_code) {
262 if (has_interface<PPB_ContentDecryptor_Private>()) {
263 pp::Var key_system_var(key_system);
264 pp::Var session_id_var(session_id);
265 get_interface<PPB_ContentDecryptor_Private>()->KeyError(
266 associated_instance_.pp_instance(),
267 key_system_var.pp_var(),
268 session_id_var.pp_var(),
274 void ContentDecryptor_Private::DeliverBlock(
275 pp::Buffer_Dev decrypted_block,
276 const PP_DecryptedBlockInfo& decrypted_block_info) {
277 if (has_interface<PPB_ContentDecryptor_Private>()) {
278 get_interface<PPB_ContentDecryptor_Private>()->DeliverBlock(
279 associated_instance_.pp_instance(),
280 decrypted_block.pp_resource(),
281 &decrypted_block_info);
285 void ContentDecryptor_Private::DecoderInitializeDone(
286 PP_DecryptorStreamType decoder_type,
289 if (has_interface<PPB_ContentDecryptor_Private>()) {
290 get_interface<PPB_ContentDecryptor_Private>()->DecoderInitializeDone(
291 associated_instance_.pp_instance(),
294 PP_FromBool(success));
298 void ContentDecryptor_Private::DecoderDeinitializeDone(
299 PP_DecryptorStreamType decoder_type,
300 uint32_t request_id) {
301 if (has_interface<PPB_ContentDecryptor_Private>()) {
302 get_interface<PPB_ContentDecryptor_Private>()->DecoderDeinitializeDone(
303 associated_instance_.pp_instance(),
309 void ContentDecryptor_Private::DecoderResetDone(
310 PP_DecryptorStreamType decoder_type,
311 uint32_t request_id) {
312 if (has_interface<PPB_ContentDecryptor_Private>()) {
313 get_interface<PPB_ContentDecryptor_Private>()->DecoderResetDone(
314 associated_instance_.pp_instance(),
320 void ContentDecryptor_Private::DeliverFrame(
321 pp::Buffer_Dev decrypted_frame,
322 const PP_DecryptedFrameInfo& decrypted_frame_info) {
323 if (has_interface<PPB_ContentDecryptor_Private>()) {
324 get_interface<PPB_ContentDecryptor_Private>()->DeliverFrame(
325 associated_instance_.pp_instance(),
326 decrypted_frame.pp_resource(),
327 &decrypted_frame_info);
331 void ContentDecryptor_Private::DeliverSamples(
332 pp::Buffer_Dev audio_frames,
333 const PP_DecryptedSampleInfo& decrypted_sample_info) {
334 if (has_interface<PPB_ContentDecryptor_Private>()) {
335 get_interface<PPB_ContentDecryptor_Private>()->DeliverSamples(
336 associated_instance_.pp_instance(),
337 audio_frames.pp_resource(),
338 &decrypted_sample_info);