Upstream version 5.34.104.0
[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   void* object =
29       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
30   if (!object)
31     return;
32
33   pp::Var key_system_var(pp::PASS_REF, key_system_arg);
34   if (!key_system_var.is_string())
35     return;
36
37   static_cast<ContentDecryptor_Private*>(object)->Initialize(
38       key_system_var.AsString());
39 }
40
41 void CreateSession(PP_Instance instance,
42                    uint32_t session_id,
43                    PP_Var type_arg,
44                    PP_Var init_data_arg) {
45   void* object =
46       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
47   if (!object)
48     return;
49
50   pp::Var type_var(pp::PASS_REF, type_arg);
51   if (!type_var.is_string())
52     return;
53
54   pp::Var init_data_var(pp::PASS_REF, init_data_arg);
55   if (!init_data_var.is_array_buffer())
56     return;
57   pp::VarArrayBuffer init_data_array_buffer(init_data_var);
58
59   static_cast<ContentDecryptor_Private*>(object)
60       ->CreateSession(session_id, type_var.AsString(), init_data_array_buffer);
61 }
62
63 void LoadSession(PP_Instance instance,
64                  uint32_t session_id,
65                  PP_Var web_session_id_arg) {
66   void* object =
67       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
68   if (!object)
69     return;
70
71   pp::Var web_session_id_var(pp::PASS_REF, web_session_id_arg);
72   if (!web_session_id_var.is_string())
73     return;
74
75   static_cast<ContentDecryptor_Private*>(object)
76       ->LoadSession(session_id, web_session_id_var.AsString());
77 }
78
79 void UpdateSession(PP_Instance instance,
80                    uint32_t session_id,
81                    PP_Var response_arg) {
82   void* object =
83       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
84   if (!object)
85     return;
86
87   pp::Var response_var(pp::PASS_REF, response_arg);
88   if (!response_var.is_array_buffer())
89     return;
90   pp::VarArrayBuffer response(response_var);
91
92   static_cast<ContentDecryptor_Private*>(object)
93       ->UpdateSession(session_id, response);
94 }
95
96 void ReleaseSession(PP_Instance instance, uint32_t session_id) {
97   void* object =
98       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
99   if (!object)
100     return;
101
102   static_cast<ContentDecryptor_Private*>(object)->ReleaseSession(session_id);
103 }
104
105
106 void Decrypt(PP_Instance instance,
107              PP_Resource encrypted_resource,
108              const PP_EncryptedBlockInfo* encrypted_block_info) {
109   pp::Buffer_Dev encrypted_block(encrypted_resource);
110
111   void* object =
112       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
113   if (!object)
114     return;
115
116   static_cast<ContentDecryptor_Private*>(object)->Decrypt(
117       encrypted_block,
118       *encrypted_block_info);
119 }
120
121 void InitializeAudioDecoder(
122     PP_Instance instance,
123     const PP_AudioDecoderConfig* decoder_config,
124     PP_Resource extra_data_resource) {
125   pp::Buffer_Dev extra_data_buffer(extra_data_resource);
126
127   void* object =
128       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
129   if (!object)
130     return;
131
132   static_cast<ContentDecryptor_Private*>(object)->InitializeAudioDecoder(
133       *decoder_config,
134       extra_data_buffer);
135 }
136
137 void InitializeVideoDecoder(
138     PP_Instance instance,
139     const PP_VideoDecoderConfig* decoder_config,
140     PP_Resource extra_data_resource) {
141   pp::Buffer_Dev extra_data_buffer(extra_data_resource);
142
143   void* object =
144       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
145   if (!object)
146     return;
147
148   static_cast<ContentDecryptor_Private*>(object)->InitializeVideoDecoder(
149       *decoder_config,
150       extra_data_buffer);
151 }
152
153 void DeinitializeDecoder(PP_Instance instance,
154                          PP_DecryptorStreamType decoder_type,
155                          uint32_t request_id) {
156   void* object =
157       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
158   if (!object)
159     return;
160   static_cast<ContentDecryptor_Private*>(object)->DeinitializeDecoder(
161       decoder_type,
162       request_id);
163 }
164
165 void ResetDecoder(PP_Instance instance,
166                   PP_DecryptorStreamType decoder_type,
167                   uint32_t request_id) {
168   void* object =
169       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
170   if (!object)
171     return;
172   static_cast<ContentDecryptor_Private*>(object)->ResetDecoder(decoder_type,
173                                                                request_id);
174 }
175
176 void DecryptAndDecode(PP_Instance instance,
177                       PP_DecryptorStreamType decoder_type,
178                       PP_Resource encrypted_resource,
179                       const PP_EncryptedBlockInfo* encrypted_block_info) {
180   pp::Buffer_Dev encrypted_buffer(encrypted_resource);
181
182   void* object =
183       Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
184   if (!object)
185     return;
186
187   static_cast<ContentDecryptor_Private*>(object)->DecryptAndDecode(
188       decoder_type,
189       encrypted_buffer,
190       *encrypted_block_info);
191 }
192
193 const PPP_ContentDecryptor_Private ppp_content_decryptor = {
194   &Initialize,
195   &CreateSession,
196   &LoadSession,
197   &UpdateSession,
198   &ReleaseSession,
199   &Decrypt,
200   &InitializeAudioDecoder,
201   &InitializeVideoDecoder,
202   &DeinitializeDecoder,
203   &ResetDecoder,
204   &DecryptAndDecode
205 };
206
207 template <> const char* interface_name<PPB_ContentDecryptor_Private>() {
208   return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
209 }
210
211 }  // namespace
212
213 ContentDecryptor_Private::ContentDecryptor_Private(Instance* instance)
214     : associated_instance_(instance) {
215   Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface,
216                                     &ppp_content_decryptor);
217   instance->AddPerInstanceObject(kPPPContentDecryptorInterface, this);
218 }
219
220 ContentDecryptor_Private::~ContentDecryptor_Private() {
221   Instance::RemovePerInstanceObject(associated_instance_,
222                                     kPPPContentDecryptorInterface,
223                                     this);
224 }
225
226 void ContentDecryptor_Private::SessionCreated(
227     uint32_t session_id,
228     const std::string& web_session_id) {
229   if (has_interface<PPB_ContentDecryptor_Private>()) {
230     pp::Var web_session_id_var(web_session_id);
231     get_interface<PPB_ContentDecryptor_Private>()->SessionCreated(
232         associated_instance_.pp_instance(),
233         session_id,
234         web_session_id_var.pp_var());
235   }
236 }
237
238 void ContentDecryptor_Private::SessionMessage(uint32_t session_id,
239                                               pp::VarArrayBuffer message,
240                                               const std::string& default_url) {
241   if (has_interface<PPB_ContentDecryptor_Private>()) {
242     pp::Var default_url_var(default_url);
243     get_interface<PPB_ContentDecryptor_Private>()->SessionMessage(
244         associated_instance_.pp_instance(),
245         session_id,
246         message.pp_var(),
247         default_url_var.pp_var());
248   }
249 }
250
251 void ContentDecryptor_Private::SessionReady(uint32_t session_id) {
252   if (has_interface<PPB_ContentDecryptor_Private>()) {
253     get_interface<PPB_ContentDecryptor_Private>()->SessionReady(
254         associated_instance_.pp_instance(), session_id);
255   }
256 }
257
258 void ContentDecryptor_Private::SessionClosed(uint32_t session_id) {
259   if (has_interface<PPB_ContentDecryptor_Private>()) {
260     get_interface<PPB_ContentDecryptor_Private>()->SessionClosed(
261         associated_instance_.pp_instance(), session_id);
262   }
263 }
264
265 void ContentDecryptor_Private::SessionError(uint32_t session_id,
266                                             int32_t media_error,
267                                             int32_t system_code) {
268   if (has_interface<PPB_ContentDecryptor_Private>()) {
269     get_interface<PPB_ContentDecryptor_Private>()->SessionError(
270         associated_instance_.pp_instance(),
271         session_id,
272         media_error,
273         system_code);
274   }
275 }
276
277 void ContentDecryptor_Private::DeliverBlock(
278     pp::Buffer_Dev decrypted_block,
279     const PP_DecryptedBlockInfo& decrypted_block_info) {
280   if (has_interface<PPB_ContentDecryptor_Private>()) {
281     get_interface<PPB_ContentDecryptor_Private>()->DeliverBlock(
282         associated_instance_.pp_instance(),
283         decrypted_block.pp_resource(),
284         &decrypted_block_info);
285   }
286 }
287
288 void ContentDecryptor_Private::DecoderInitializeDone(
289     PP_DecryptorStreamType decoder_type,
290     uint32_t request_id,
291     bool success) {
292   if (has_interface<PPB_ContentDecryptor_Private>()) {
293     get_interface<PPB_ContentDecryptor_Private>()->DecoderInitializeDone(
294         associated_instance_.pp_instance(),
295         decoder_type,
296         request_id,
297         PP_FromBool(success));
298   }
299 }
300
301 void ContentDecryptor_Private::DecoderDeinitializeDone(
302     PP_DecryptorStreamType decoder_type,
303     uint32_t request_id) {
304   if (has_interface<PPB_ContentDecryptor_Private>()) {
305     get_interface<PPB_ContentDecryptor_Private>()->DecoderDeinitializeDone(
306         associated_instance_.pp_instance(),
307         decoder_type,
308         request_id);
309   }
310 }
311
312 void ContentDecryptor_Private::DecoderResetDone(
313     PP_DecryptorStreamType decoder_type,
314     uint32_t request_id) {
315   if (has_interface<PPB_ContentDecryptor_Private>()) {
316     get_interface<PPB_ContentDecryptor_Private>()->DecoderResetDone(
317         associated_instance_.pp_instance(),
318         decoder_type,
319         request_id);
320   }
321 }
322
323 void ContentDecryptor_Private::DeliverFrame(
324     pp::Buffer_Dev decrypted_frame,
325     const PP_DecryptedFrameInfo& decrypted_frame_info) {
326   if (has_interface<PPB_ContentDecryptor_Private>()) {
327     get_interface<PPB_ContentDecryptor_Private>()->DeliverFrame(
328         associated_instance_.pp_instance(),
329         decrypted_frame.pp_resource(),
330         &decrypted_frame_info);
331   }
332 }
333
334 void ContentDecryptor_Private::DeliverSamples(
335     pp::Buffer_Dev audio_frames,
336     const PP_DecryptedSampleInfo& decrypted_sample_info) {
337   if (has_interface<PPB_ContentDecryptor_Private>()) {
338     get_interface<PPB_ContentDecryptor_Private>()->DeliverSamples(
339         associated_instance_.pp_instance(),
340         audio_frames.pp_resource(),
341         &decrypted_sample_info);
342   }
343 }
344
345 }  // namespace pp