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

Side by Side Diff: webkit/media/crypto/ppapi/cdm_wrapper.cc

Issue 10827280: Add PPAPI decryptor implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed NeedKey, addressed all comments (excluding one of dmichael's). 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
(Empty)
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 <cstring> // For std::memcpy.
6
7 #include "base/compiler_specific.h" // For OVERRIDE.
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/c/pp_stdint.h"
10 #include "ppapi/cpp/completion_callback.h"
11 #include "ppapi/cpp/core.h"
12 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/logging.h"
14 #include "ppapi/cpp/module.h"
15 #include "ppapi/cpp/pass_ref.h"
16 #include "ppapi/cpp/resource.h"
17 #include "ppapi/cpp/var.h"
18 #include "ppapi/cpp/var_array_buffer.h"
19 #include "ppapi/cpp/dev/buffer_dev.h"
20 #include "ppapi/cpp/private/content_decryptor_private.h"
21 #include "ppapi/utility/completion_callback_factory.h"
22
23 namespace {
24
25 struct DecryptorMessage {
26 DecryptorMessage() : media_error(0), system_code(0) {}
27 std::string key_system;
28 std::string session_id;
29 std::string default_url;
30 std::string message_data;
31 int32_t media_error;
32 int32_t system_code;
33 };
34
35 struct DecryptedBlock {
36 DecryptedBlock() : request_id(0) {}
37 int32_t request_id;
38 std::string data;
39 };
40
41 void CallOnMain(pp::CompletionCallback cb) {
42 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK);
43 }
44
45 } // namespace
46
47
48 // A wrapper class for abstracting away PPAPI interaction and threading for a
49 // Content Decryption Module (CDM).
50 class CDMWrapper : public pp::Instance,
51 public pp::ContentDecryptor_Private {
52 public:
53 CDMWrapper(PP_Instance instance, pp::Module* module);
54 virtual ~CDMWrapper() {}
55
56 // PPP_ContentDecryptor_Private methods
57 virtual bool GenerateKeyRequest(const std::string& key_system,
58 pp::VarArrayBuffer init_data) OVERRIDE;
59 virtual bool AddKey(const std::string& session_id,
60 pp::VarArrayBuffer key) OVERRIDE;
61 virtual bool CancelKeyRequest(const std::string& session_id) OVERRIDE;
62 virtual bool Decrypt(pp::Buffer_Dev encrypted_buffer,
63 int32_t request_id) OVERRIDE;
64
65 virtual bool DecryptAndDecode(pp::Buffer_Dev encrypted_buffer,
66 int32_t request_id) OVERRIDE {
67 return false;
68 }
69
70 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
71 return true;
72 }
73
74 private:
75 PP_Resource StringToBufferResource(const std::string& str);
76
77 // <code>PPB_ContentDecryptor_Private</code> dispatchers. These are passed to
78 // <code>callback_factory_</code> to ensure that calls into
79 // <code>PPP_ContentDecryptor_Private</code> are asynchronous.
80 void NeedKey(int32_t result, const DecryptorMessage& decryptor_message);
81 void KeyAdded(int32_t result, const DecryptorMessage& decryptor_message);
82 void KeyMessage(int32_t result, const DecryptorMessage& decryptor_message);
83 void KeyError(int32_t result, const DecryptorMessage& decryptor_message);
84 void DeliverBlock(int32_t result, const DecryptedBlock& decrypted_block);
85
86 pp::CompletionCallbackFactory<CDMWrapper> callback_factory_;
87 };
88
89 CDMWrapper::CDMWrapper(PP_Instance instance,
90 pp::Module* module)
91 : pp::Instance(instance),
92 pp::ContentDecryptor_Private(this) {
93 callback_factory_.Initialize(this);
94 }
95
96 bool CDMWrapper::GenerateKeyRequest(const std::string& key_system,
97 pp::VarArrayBuffer init_data) {
98 PP_DCHECK(!key_system.empty() && init_data.ByteLength());
99
100 DecryptorMessage decryptor_message;
101 decryptor_message.key_system = key_system;
102 decryptor_message.session_id = "0";
103 decryptor_message.default_url = "http://www.google.com";
104 decryptor_message.message_data = "GenerateKeyRequest";
105
106 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage,
107 decryptor_message));
108
109 return true;
110 }
111
112 bool CDMWrapper::AddKey(const std::string& session_id,
113 pp::VarArrayBuffer key) {
114 const std::string key_string(reinterpret_cast<char*>(key.Map()),
115 key.ByteLength());
116
117 PP_DCHECK(!session_id.empty() && !key_string.empty());
118
119 DecryptorMessage decryptor_message;
120 decryptor_message.key_system = "AddKey";
121 decryptor_message.session_id = "0";
122 decryptor_message.default_url = "http://www.google.com";
123 decryptor_message.message_data = "AddKey";
124 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyAdded,
125 decryptor_message));
126 return true;
127 }
128
129 bool CDMWrapper::CancelKeyRequest(const std::string& session_id) {
130 // TODO(tomfinegan): cancel pending key request in CDM.
131
132 PP_DCHECK(!session_id.empty());
133
134 DecryptorMessage decryptor_message;
135 decryptor_message.key_system = "CancelKeyRequest";
136 decryptor_message.session_id = "0";
137 decryptor_message.default_url = "http://www.google.com";
138 decryptor_message.message_data = "CancelKeyRequest";
139 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage,
140 decryptor_message));
141 return true;
142 }
143
144 bool CDMWrapper::Decrypt(pp::Buffer_Dev encrypted_buffer,
145 int32_t request_id) {
146 PP_DCHECK(!encrypted_buffer.is_null());
147
148 DecryptedBlock decrypted_block;
149 decrypted_block.request_id = request_id;
150 decrypted_block.data = "Pretend I'm decrypted data!";
151 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock,
152 decrypted_block));
153 return true;
154 }
155
156 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) {
157 if (str.empty())
158 return 0;
159
160 pp::Buffer_Dev buffer(this, str.size());
161 if (!buffer.data())
162 return 0;
163
164 std::memcpy(buffer.data(), str.data(), str.size());
165 return buffer.detach();
166 }
167
168 void CDMWrapper::NeedKey(int32_t result,
169 const DecryptorMessage& decryptor_message) {
170 const std::string& message_data = decryptor_message.message_data;
171 pp::VarArrayBuffer init_data(message_data.size());
172 std::memcpy(init_data.Map(), message_data.data(), message_data.size());
173 pp::ContentDecryptor_Private::NeedKey(decryptor_message.key_system,
174 decryptor_message.session_id,
175 init_data);
176 }
177
178 void CDMWrapper::KeyAdded(int32_t result,
179 const DecryptorMessage& decryptor_message) {
180 pp::ContentDecryptor_Private::KeyAdded(decryptor_message.key_system,
181 decryptor_message.session_id);
182 }
183
184 void CDMWrapper::KeyMessage(int32_t result,
185 const DecryptorMessage& decryptor_message) {
186 pp::Buffer_Dev message_buffer(
187 StringToBufferResource(decryptor_message.message_data));
188 pp::ContentDecryptor_Private::KeyMessage(decryptor_message.key_system,
189 decryptor_message.session_id,
190 message_buffer,
191 decryptor_message.default_url);
192 }
193
194 void CDMWrapper::KeyError(int32_t result,
195 const DecryptorMessage& decryptor_message) {
196 pp::ContentDecryptor_Private::KeyError(decryptor_message.key_system,
197 decryptor_message.session_id,
198 decryptor_message.media_error,
199 decryptor_message.system_code);
200 }
201
202 void CDMWrapper::DeliverBlock(int32_t result,
203 const DecryptedBlock& decrypted_block) {
204 pp::Buffer_Dev decrypted_buffer(
205 StringToBufferResource(decrypted_block.data));
206 pp::ContentDecryptor_Private::DeliverBlock(decrypted_buffer,
207 decrypted_block.request_id);
208 }
209
210 // This object is the global object representing this plugin library as long
211 // as it is loaded.
212 class MyModule : public pp::Module {
213 public:
214 MyModule() : pp::Module() {}
215 virtual ~MyModule() {}
216
217 virtual pp::Instance* CreateInstance(PP_Instance instance) {
218 return new CDMWrapper(instance, this);
219 }
220 };
221
222 namespace pp {
223
224 // Factory function for your specialization of the Module object.
225 Module* CreateModule() {
226 return new MyModule();
227 }
228
229 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698