Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: ppapi/examples/content_decryptor/content_decryptor.cc

Issue 10836038: Call CDMWrapper from PpapiDecryptor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add PP_DecryptionBuffer_Dev and update Decrypt() PPP call. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <cassert> 5 #include <cassert>
6 #include <string> 6 #include <string>
7 7
8 #include <iostream>
9
8 #include "base/basictypes.h" 10 #include "base/basictypes.h"
9 #include "base/rand_util.h" 11 #include "base/rand_util.h"
10 #include "base/values.h" 12 #include "base/values.h"
11 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "ppapi/c/dev/pp_decryption_buffer_dev.h"
12 #include "ppapi/c/pp_errors.h" 15 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/cpp/completion_callback.h" 16 #include "ppapi/cpp/completion_callback.h"
14 #include "ppapi/cpp/core.h" 17 #include "ppapi/cpp/core.h"
15 #include "ppapi/cpp/instance.h" 18 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/module.h" 19 #include "ppapi/cpp/module.h"
17 #include "ppapi/cpp/resource.h" 20 #include "ppapi/cpp/resource.h"
18 #include "ppapi/cpp/var.h" 21 #include "ppapi/cpp/var.h"
19 #include "ppapi/cpp/dev/buffer_dev.h" 22 #include "ppapi/cpp/dev/buffer_dev.h"
20 #include "ppapi/cpp/dev/content_decryptor_dev.h" 23 #include "ppapi/cpp/dev/content_decryptor_dev.h"
21 #include "ppapi/shared_impl/var.h" 24 #include "ppapi/shared_impl/var.h"
22 #include "ppapi/utility/completion_callback_factory.h" 25 #include "ppapi/utility/completion_callback_factory.h"
26 #include "ppapi/utility/threading/simple_thread.h"
23 27
24 using ppapi::StringVar; 28 using ppapi::StringVar;
25 29
26 namespace { 30 namespace {
27 31
28 struct DecryptorMessage { 32 struct DecryptorMessage {
29 DecryptorMessage() : media_error(0), system_error(0) {} 33 DecryptorMessage() : media_error(0), system_error(0) {}
30 std::string key_system; 34 std::string key_system;
31 std::string session_id; 35 std::string session_id;
32 std::string default_url; 36 std::string default_url;
33 std::string message_data; 37 std::string message_data;
34 uint16 media_error; 38 uint16 media_error;
35 uint16 system_error; 39 uint16 system_error;
36 }; 40 };
37 41
38 struct DecryptedData { 42 struct DecryptedData {
39 DecryptedData() : callback(PP_MakeCompletionCallback(NULL, NULL)) {} 43 DecryptedData() : callback(PP_MakeCompletionCallback(NULL, NULL)) {}
40 PP_CompletionCallback callback; 44 PP_CompletionCallback callback;
41 std::string data; 45 std::string data;
46 uint32_t id;
42 }; 47 };
43 48
44 bool IsMainThread() { 49 bool IsMainThread() {
45 return pp::Module::Get()->core()->IsMainThread(); 50 return pp::Module::Get()->core()->IsMainThread();
46 } 51 }
47 52
48 void CallOnMain(pp::CompletionCallback cb) { 53 void CallOnMain(pp::CompletionCallback cb) {
49 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); 54 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK);
50 } 55 }
51 56
52 } // namespace 57 } // namespace
53 58
54 59
55 // A wrapper class responsible for managing interaction between the browser and 60 // A wrapper class responsible for managing interaction between the browser and
56 // a Content Decryption Module (CDM). 61 // a Content Decryption Module (CDM).
57 class CDMWrapper : public pp::Instance, 62 class CDMWrapper : public pp::Instance,
58 public pp::ContentDecryptor_Dev { 63 public pp::ContentDecryptor_Dev {
59 public: 64 public:
60 const static size_t kSessionIdLength = 8; 65 const static size_t kSessionIdLength = 8;
61 CDMWrapper(PP_Instance instance, pp::Module* module); 66 CDMWrapper(PP_Instance instance, pp::Module* module);
62 virtual ~CDMWrapper() {} 67 virtual ~CDMWrapper();
63 68
64 // PPP_ContentDecryptor_Dev methods 69 // PPP_ContentDecryptor_Dev methods
65 virtual bool GenerateKeyRequest(PP_Var key_system, PP_Resource init_data); 70 virtual bool GenerateKeyRequest(PP_Var key_system, PP_Resource init_data);
66 virtual bool AddKey(PP_Var session_id, PP_Resource key); 71 virtual bool AddKey(PP_Var session_id, PP_Resource key);
67 virtual bool CancelKeyRequest(PP_Var session_id); 72 virtual bool CancelKeyRequest(PP_Var session_id);
68 virtual bool Decrypt(PP_Resource encrypted_block, 73 virtual bool Decrypt(const PP_DecryptionBuffer_Dev& encrypted_buffer);
69 PP_CompletionCallback callback);
70
71 virtual bool DecryptAndDecode(PP_Resource encrypted_block, 74 virtual bool DecryptAndDecode(PP_Resource encrypted_block,
72 PP_CompletionCallback callback) { 75 PP_CompletionCallback callback) {
73 return false; 76 return false;
74 } 77 }
75 78
76 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 79 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
80 std::cout << "Init!!!!!!!!!!!!!!!!!" << std::endl;
81 bool success = thread_->Start();
82 std::cout << "SimpleThread Start() returns: " << success << std::endl;
83 int32_t result = thread_->message_loop().PostWork(
84 callback_factory_.NewCallback(&CDMWrapper::CallOnBackground));
85 std::cout << "PostWork returns: " << result << std::endl;
77 return true; 86 return true;
78 } 87 }
79 88
80 private: 89 private:
90 void CallOnBackground(int32_t result) {
91 std::cout << "CallOnBackground called on Background !!!!!!!!!" << std::endl;
92 }
93
81 PP_Resource StringToBufferResource(const std::string& str); 94 PP_Resource StringToBufferResource(const std::string& str);
82 95
83 // <code>PPB_ContentDecryptor_Dev</code> dispatchers. These are passed to 96 // <code>PPB_ContentDecryptor_Dev</code> dispatchers. These are passed to
84 // <code>callback_factory_</code> to ensure that calls into 97 // <code>callback_factory_</code> to ensure that calls into
85 // <code>PPP_ContentDecryptor_Dev</code> are asynchronous. 98 // <code>PPP_ContentDecryptor_Dev</code> are asynchronous.
86 void NeedKey(int32 result, const DecryptorMessage& decryptor_message); 99 void NeedKey(int32 result, const DecryptorMessage& decryptor_message);
87 void KeyAdded(int32 result, const DecryptorMessage& decryptor_message); 100 void KeyAdded(int32 result, const DecryptorMessage& decryptor_message);
88 void KeyMessage(int32 result, const DecryptorMessage& decryptor_message); 101 void KeyMessage(int32 result, const DecryptorMessage& decryptor_message);
89 void KeyError(int32 result, const DecryptorMessage& decryptor_message); 102 void KeyError(int32 result, const DecryptorMessage& decryptor_message);
90 void DeliverBlock(int32 result, const DecryptedData& decrypted_data); 103 void DeliverBlock(int32 result, const DecryptedData& decrypted_data);
(...skipping 13 matching lines...) Expand all
104 117
105 // Key ID obtained from init data passed to <code>GenerateKeyRequest</code>. 118 // Key ID obtained from init data passed to <code>GenerateKeyRequest</code>.
106 // As-is: WebM video specific; Inadequate for WebM with encrypted audio (A/V 119 // As-is: WebM video specific; Inadequate for WebM with encrypted audio (A/V
107 // will not have same key ID/key/session ID). Probably inadequate for ISO. 120 // will not have same key ID/key/session ID). Probably inadequate for ISO.
108 std::string key_id_; 121 std::string key_id_;
109 122
110 // TODO(tomfinegan): Should these be multimaps of session_id:key_system and 123 // TODO(tomfinegan): Should these be multimaps of session_id:key_system and
111 // session_id:key? Or am I completely confused? :) 124 // session_id:key? Or am I completely confused? :)
112 std::string key_; 125 std::string key_;
113 std::string key_system_; 126 std::string key_system_;
127
128 pp::SimpleThread* thread_;
114 }; 129 };
115 130
116 CDMWrapper::CDMWrapper(PP_Instance instance, 131 CDMWrapper::CDMWrapper(PP_Instance instance,
117 pp::Module* module) 132 pp::Module* module)
118 : pp::Instance(instance), 133 : pp::Instance(instance),
119 pp::ContentDecryptor_Dev(this) { 134 pp::ContentDecryptor_Dev(this) {
135 thread_ = new pp::SimpleThread(this);
120 callback_factory_.Initialize(this); 136 callback_factory_.Initialize(this);
121 } 137 }
122 138
139 CDMWrapper::~CDMWrapper() {
140 delete thread_;
141 }
142
123 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg, 143 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg,
124 PP_Resource init_data) { 144 PP_Resource init_data) {
125 if (init_data) { 145 if (init_data) {
126 pp::Buffer_Dev init_buffer(init_data); 146 pp::Buffer_Dev init_buffer(init_data);
127 if (!init_buffer.data() || init_buffer.size() == 0) { 147 if (!init_buffer.data() || init_buffer.size() == 0) {
128 return false; 148 return false;
129 } 149 }
130 150
131 // TODO(tomfinegan): Testing only implementation; init data will not always 151 // TODO(tomfinegan): Testing only implementation; init data will not always
132 // be the key ID. 152 // be the key ID.
133 key_id_.assign(reinterpret_cast<char*>(init_buffer.data()), 153 key_id_.assign(reinterpret_cast<char*>(init_buffer.data()),
134 init_buffer.size()); 154 init_buffer.size());
135 } 155 }
136 156
137 StringVar* key_system_var = StringVar::FromPPVar(key_system_arg); 157 //StringVar* key_system_var = StringVar::FromPPVar(key_system_arg);
138 158
139 // TODO(tomfinegan): confirm support for the key system. 159 // TODO(tomfinegan): confirm support for the key system.
140 160
141 DecryptorMessage decryptor_message; 161 DecryptorMessage decryptor_message;
142 key_system_ = key_system_var->value(); 162 key_system_ = "org.chromium.externalclearkey";
143 decryptor_message.key_system = key_system_; 163 decryptor_message.key_system = key_system_;
144 session_id_ = base::RandBytesAsString(kSessionIdLength); 164 session_id_ = "1";
145 decryptor_message.session_id = session_id_; 165 decryptor_message.session_id = session_id_;
146 decryptor_message.default_url = "http://www.google.com"; 166 decryptor_message.default_url = "http://www.google.com";
147 decryptor_message.message_data = "key request"; 167 decryptor_message.message_data = "key request";
148 168
149 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, 169 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage,
150 decryptor_message)); 170 decryptor_message));
151 return true; 171 return true;
152 } 172 }
153 173
154 bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) { 174 bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) {
(...skipping 15 matching lines...) Expand all
170 } 190 }
171 191
172 bool CDMWrapper::CancelKeyRequest(PP_Var session_id_var) { 192 bool CDMWrapper::CancelKeyRequest(PP_Var session_id_var) {
173 std::string session_id = StringVar::FromPPVar(session_id_var)->value(); 193 std::string session_id = StringVar::FromPPVar(session_id_var)->value();
174 194
175 // TODO(tomfinegan): cancel pending key request in CDM. 195 // TODO(tomfinegan): cancel pending key request in CDM.
176 196
177 return true; 197 return true;
178 } 198 }
179 199
180 bool CDMWrapper::Decrypt(PP_Resource encrypted_block, 200 bool CDMWrapper::Decrypt(const PP_DecryptionBuffer_Dev& encrypted_buffer) {
181 PP_CompletionCallback callback) { 201 pp::Buffer_Dev block_buffer(encrypted_buffer.data);
182 pp::Buffer_Dev block_buffer(encrypted_block); 202 if (!block_buffer.data())
183 if (!block_buffer.data() || !callback.func) {
184 return false; 203 return false;
185 }
186 204
187 DecryptedData decrypted_data; 205 DecryptedData decrypted_data;
188 decrypted_data.callback = callback;
189 decrypted_data.data = "Pretend I'm decrypted data!"; 206 decrypted_data.data = "Pretend I'm decrypted data!";
207 decrypted_data.id = encrypted_buffer.id;
190 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock, 208 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock,
191 decrypted_data)); 209 decrypted_data));
192 return true; 210 return true;
193 } 211 }
194 212
195 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { 213 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) {
196 if (str.empty()) 214 if (str.empty())
197 return 0; 215 return 0;
198 216
199 pp::Buffer_Dev buffer(this, str.size()); 217 pp::Buffer_Dev buffer(this, str.size());
200 if (!buffer.data()) 218 if (!buffer.data())
201 return 0; 219 return 0;
202 220
203 memcpy(buffer.data(), str.data(), str.size()); 221 memcpy(buffer.data(), str.data(), str.size());
204 return buffer.detach(); 222 return buffer.detach();
205 } 223 }
206 224
207 void CDMWrapper::NeedKey(int32 result, 225 void CDMWrapper::NeedKey(int32 result,
208 const DecryptorMessage& decryptor_message) { 226 const DecryptorMessage& decryptor_message) {
209 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); 227 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system);
210 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); 228 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id);
211 PP_Resource init_data = 229 PP_Resource init_data =
212 StringToBufferResource(decryptor_message.message_data); 230 StringToBufferResource(decryptor_message.message_data);
213 pp::ContentDecryptor_Dev::NeedKey(key_system, session_id, init_data); 231 pp::ContentDecryptor_Dev::NeedKey(key_system, session_id, init_data);
214 } 232 }
215 233
216 void CDMWrapper::KeyAdded(int32 result, 234 void CDMWrapper::KeyAdded(int32 result,
217 const DecryptorMessage& decryptor_message) { 235 const DecryptorMessage& decryptor_message) {
218 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); 236 pp::Var key_system(decryptor_message.key_system);
219 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); 237 pp::Var session_id(decryptor_message.session_id);
220 pp::ContentDecryptor_Dev::KeyAdded(key_system, session_id); 238 pp::ContentDecryptor_Dev::KeyAdded(key_system.pp_var(), session_id.pp_var());
221 } 239 }
222 240
223 void CDMWrapper::KeyMessage(int32 result, 241 void CDMWrapper::KeyMessage(int32 result,
224 const DecryptorMessage& decryptor_message) { 242 const DecryptorMessage& decryptor_message) {
225 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); 243 pp::Var key_system(decryptor_message.key_system);
226 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); 244 pp::Var session_id(decryptor_message.session_id);
227 PP_Resource message = 245 PP_Resource message =
228 StringToBufferResource(decryptor_message.message_data); 246 StringToBufferResource(decryptor_message.message_data);
229 PP_Var default_url = 247 pp::Var default_url(decryptor_message.default_url);
230 StringVar::StringToPPVar(decryptor_message.default_url); 248 pp::ContentDecryptor_Dev::KeyMessage(key_system.pp_var(),
231 pp::ContentDecryptor_Dev::KeyMessage(key_system, 249 session_id.pp_var(),
232 session_id,
233 message, 250 message,
234 default_url); 251 default_url.pp_var());
235 } 252 }
236 253
237 void CDMWrapper::KeyError(int32 result, 254 void CDMWrapper::KeyError(int32 result,
238 const DecryptorMessage& decryptor_message) { 255 const DecryptorMessage& decryptor_message) {
239 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); 256 pp::Var key_system(decryptor_message.key_system);
240 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); 257 pp::Var session_id(decryptor_message.session_id);
241 pp::ContentDecryptor_Dev::KeyError(key_system, 258 pp::ContentDecryptor_Dev::KeyError(key_system.pp_var(),
242 session_id, 259 session_id.pp_var(),
243 decryptor_message.media_error, 260 decryptor_message.media_error,
244 decryptor_message.system_error); 261 decryptor_message.system_error);
245 } 262 }
246 263
247 void CDMWrapper::DeliverBlock(int32 result, 264 void CDMWrapper::DeliverBlock(int32 result,
248 const DecryptedData& decrypted_data) { 265 const DecryptedData& decrypted_data) {
249 PP_Resource decrypted_resource = StringToBufferResource( 266 PP_Resource decrypted_resource = StringToBufferResource(
250 decrypted_data.data); 267 decrypted_data.data);
251 if (decrypted_resource) { 268 if (decrypted_resource) {
252 pp::ContentDecryptor_Dev::DeliverBlock(decrypted_resource, 269 pp::ContentDecryptor_Dev::DeliverBlock(decrypted_resource,
253 decrypted_data.callback); 270 decrypted_data.id);
254 } 271 }
255 } 272 }
256 273
257 // This object is the global object representing this plugin library as long 274 // This object is the global object representing this plugin library as long
258 // as it is loaded. 275 // as it is loaded.
259 class MyModule : public pp::Module { 276 class MyModule : public pp::Module {
260 public: 277 public:
261 MyModule() : pp::Module() {} 278 MyModule() : pp::Module() {}
262 virtual ~MyModule() {} 279 virtual ~MyModule() {}
263 280
264 virtual pp::Instance* CreateInstance(PP_Instance instance) { 281 virtual pp::Instance* CreateInstance(PP_Instance instance) {
265 return new CDMWrapper(instance, this); 282 return new CDMWrapper(instance, this);
266 } 283 }
267 }; 284 };
268 285
269 namespace pp { 286 namespace pp {
270 287
271 // Factory function for your specialization of the Module object. 288 // Factory function for your specialization of the Module object.
272 Module* CreateModule() { 289 Module* CreateModule() {
273 return new MyModule(); 290 return new MyModule();
274 } 291 }
275 292
276 } // namespace pp 293 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698