- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / private / content_decryptor_private.cc
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/cpp/private/content_decryptor_private.h"
6
7 #include <cstring>  // memcpy
8
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
19 namespace pp {
20
21 namespace {
22
23 static const char kPPPContentDecryptorInterface[] =
24     PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
25
26 void Initialize(PP_Instance instance,
27                 PP_Var key_system_arg,
28                 PP_Bool can_challenge_platform) {
29   void* object =
30       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
31   if (!object)
32     return;
33
34   pp::Var key_system_var(pp::PASS_REF, key_system_arg);
35   if (!key_system_var.is_string())
36     return;
37
38   static_cast<ContentDecryptor_Private*>(object)->Initialize(
39       key_system_var.AsString(),
40       PP_ToBool(can_challenge_platform));
41 }
42
43 void GenerateKeyRequest(PP_Instance instance,
44                         PP_Var type_arg,
45                         PP_Var init_data_arg) {
46   void* object =
47       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
48   if (!object)
49     return;
50
51   pp::Var type_var(pp::PASS_REF, type_arg);
52   if (!type_var.is_string())
53     return;
54
55   pp::Var init_data_var(pp::PASS_REF, init_data_arg);
56   if (!init_data_var.is_array_buffer())
57     return;
58   pp::VarArrayBuffer init_data_array_buffer(init_data_var);
59
60   static_cast<ContentDecryptor_Private*>(object)->GenerateKeyRequest(
61       type_var.AsString(),
62       init_data_array_buffer);
63 }
64
65 void AddKey(PP_Instance instance,
66             PP_Var session_id_arg,
67             PP_Var key_arg,
68             PP_Var init_data_arg) {
69   void* object =
70       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
71   if (!object)
72     return;
73
74   pp::Var session_id_var(pp::PASS_REF, session_id_arg);
75   if (!session_id_var.is_string())
76     return;
77
78   pp::Var key_var(pp::PASS_REF, key_arg);
79   if (!key_var.is_array_buffer())
80     return;
81   pp::VarArrayBuffer key(key_var);
82
83   pp::Var init_data_var(pp::PASS_REF, init_data_arg);
84   if (!init_data_var.is_array_buffer())
85     return;
86   pp::VarArrayBuffer init_data(init_data_var);
87
88
89   static_cast<ContentDecryptor_Private*>(object)->AddKey(
90       session_id_var.AsString(),
91       key,
92       init_data);
93 }
94
95 void CancelKeyRequest(PP_Instance instance, PP_Var session_id_arg) {
96   void* object =
97       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
98   if (!object)
99     return;
100
101   pp::Var session_id_var(pp::PASS_REF, session_id_arg);
102   if (!session_id_var.is_string())
103     return;
104
105   static_cast<ContentDecryptor_Private*>(object)->CancelKeyRequest(
106       session_id_var.AsString());
107 }
108
109
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);
114
115   void* object =
116       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
117   if (!object)
118     return;
119
120   static_cast<ContentDecryptor_Private*>(object)->Decrypt(
121       encrypted_block,
122       *encrypted_block_info);
123 }
124
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);
130
131   void* object =
132       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
133   if (!object)
134     return;
135
136   static_cast<ContentDecryptor_Private*>(object)->InitializeAudioDecoder(
137       *decoder_config,
138       extra_data_buffer);
139 }
140
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);
146
147   void* object =
148       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
149   if (!object)
150     return;
151
152   static_cast<ContentDecryptor_Private*>(object)->InitializeVideoDecoder(
153       *decoder_config,
154       extra_data_buffer);
155 }
156
157 void DeinitializeDecoder(PP_Instance instance,
158                          PP_DecryptorStreamType decoder_type,
159                          uint32_t request_id) {
160   void* object =
161       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
162   if (!object)
163     return;
164   static_cast<ContentDecryptor_Private*>(object)->DeinitializeDecoder(
165       decoder_type,
166       request_id);
167 }
168
169 void ResetDecoder(PP_Instance instance,
170                   PP_DecryptorStreamType decoder_type,
171                   uint32_t request_id) {
172   void* object =
173       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
174   if (!object)
175     return;
176   static_cast<ContentDecryptor_Private*>(object)->ResetDecoder(decoder_type,
177                                                                request_id);
178 }
179
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);
185
186   void* object =
187       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
188   if (!object)
189     return;
190
191   static_cast<ContentDecryptor_Private*>(object)->DecryptAndDecode(
192       decoder_type,
193       encrypted_buffer,
194       *encrypted_block_info);
195 }
196
197 const PPP_ContentDecryptor_Private ppp_content_decryptor = {
198   &Initialize,
199   &GenerateKeyRequest,
200   &AddKey,
201   &CancelKeyRequest,
202   &Decrypt,
203   &InitializeAudioDecoder,
204   &InitializeVideoDecoder,
205   &DeinitializeDecoder,
206   &ResetDecoder,
207   &DecryptAndDecode
208 };
209
210 template <> const char* interface_name<PPB_ContentDecryptor_Private>() {
211   return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
212 }
213
214 }  // namespace
215
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);
221 }
222
223 ContentDecryptor_Private::~ContentDecryptor_Private() {
224   Instance::RemovePerInstanceObject(associated_instance_,
225                                     kPPPContentDecryptorInterface,
226                                     this);
227 }
228
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());
238   }
239 }
240
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(),
253         message.pp_var(),
254         default_url_var.pp_var());
255   }
256 }
257
258 void ContentDecryptor_Private::KeyError(const std::string& key_system,
259                                         const std::string& session_id,
260                                         int32_t media_error,
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(),
269         media_error,
270         system_code);
271   }
272 }
273
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);
282   }
283 }
284
285 void ContentDecryptor_Private::DecoderInitializeDone(
286     PP_DecryptorStreamType decoder_type,
287     uint32_t request_id,
288     bool success) {
289   if (has_interface<PPB_ContentDecryptor_Private>()) {
290     get_interface<PPB_ContentDecryptor_Private>()->DecoderInitializeDone(
291         associated_instance_.pp_instance(),
292         decoder_type,
293         request_id,
294         PP_FromBool(success));
295   }
296 }
297
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(),
304         decoder_type,
305         request_id);
306   }
307 }
308
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(),
315         decoder_type,
316         request_id);
317   }
318 }
319
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);
328   }
329 }
330
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);
339   }
340 }
341
342 }  // namespace pp