OLD | NEW |
---|---|
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 <cstring> // For std::memcpy. | 5 #include <cstring> // For std::memcpy. |
6 | 6 |
7 #include "base/compiler_specific.h" // For OVERRIDE. | 7 #include "base/compiler_specific.h" // For OVERRIDE. |
8 #include "ppapi/c/pp_errors.h" | 8 #include "ppapi/c/pp_errors.h" |
9 #include "ppapi/c/pp_stdint.h" | 9 #include "ppapi/c/pp_stdint.h" |
10 #include "ppapi/c/private/pp_content_decryptor.h" | |
10 #include "ppapi/cpp/completion_callback.h" | 11 #include "ppapi/cpp/completion_callback.h" |
11 #include "ppapi/cpp/core.h" | 12 #include "ppapi/cpp/core.h" |
12 #include "ppapi/cpp/instance.h" | 13 #include "ppapi/cpp/instance.h" |
13 #include "ppapi/cpp/logging.h" | 14 #include "ppapi/cpp/logging.h" |
14 #include "ppapi/cpp/module.h" | 15 #include "ppapi/cpp/module.h" |
15 #include "ppapi/cpp/pass_ref.h" | 16 #include "ppapi/cpp/pass_ref.h" |
16 #include "ppapi/cpp/resource.h" | 17 #include "ppapi/cpp/resource.h" |
17 #include "ppapi/cpp/var.h" | 18 #include "ppapi/cpp/var.h" |
18 #include "ppapi/cpp/var_array_buffer.h" | 19 #include "ppapi/cpp/var_array_buffer.h" |
19 #include "ppapi/cpp/dev/buffer_dev.h" | 20 #include "ppapi/cpp/dev/buffer_dev.h" |
20 #include "ppapi/cpp/private/content_decryptor_private.h" | 21 #include "ppapi/cpp/private/content_decryptor_private.h" |
21 #include "ppapi/utility/completion_callback_factory.h" | 22 #include "ppapi/utility/completion_callback_factory.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 struct DecryptorMessage { | 26 struct DecryptorMessage { |
26 DecryptorMessage() : media_error(0), system_code(0) {} | 27 DecryptorMessage() : media_error(0), system_code(0) {} |
27 std::string key_system; | 28 std::string key_system; |
28 std::string session_id; | 29 std::string session_id; |
29 std::string default_url; | 30 std::string default_url; |
30 std::string message_data; | 31 std::string message_data; |
31 int32_t media_error; | 32 int32_t media_error; |
32 int32_t system_code; | 33 int32_t system_code; |
33 }; | 34 }; |
34 | 35 |
35 struct DecryptedBlock { | 36 struct DecryptedBlock { |
36 DecryptedBlock() : request_id(0) {} | 37 DecryptedBlock() { |
37 int32_t request_id; | 38 std::memset(reinterpret_cast<void*>(&decrypted_block_info), |
ddorwin
2012/08/21 23:18:25
May be able to use a constructor. xhang is looking
| |
38 std::string data; | 39 sizeof(decrypted_block_info), |
40 0); | |
41 } | |
42 std::string decrypted_data; | |
43 PP_DecryptedBlockInfo decrypted_block_info; | |
39 }; | 44 }; |
40 | 45 |
46 bool IsMainThread() { | |
47 return pp::Module::Get()->core()->IsMainThread(); | |
48 } | |
49 | |
41 void CallOnMain(pp::CompletionCallback cb) { | 50 void CallOnMain(pp::CompletionCallback cb) { |
42 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); | 51 if (IsMainThread()) |
52 cb.Run(PP_OK); | |
53 else | |
54 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); | |
dmichael (off chromium)
2012/08/21 23:05:14
May be good to add a TODO comment here... AIUI, t
| |
43 } | 55 } |
44 | 56 |
45 } // namespace | 57 } // namespace |
46 | 58 |
47 | 59 |
48 // A wrapper class for abstracting away PPAPI interaction and threading for a | 60 // A wrapper class for abstracting away PPAPI interaction and threading for a |
49 // Content Decryption Module (CDM). | 61 // Content Decryption Module (CDM). |
50 class CDMWrapper : public pp::Instance, | 62 class CDMWrapper : public pp::Instance, |
51 public pp::ContentDecryptor_Private { | 63 public pp::ContentDecryptor_Private { |
52 public: | 64 public: |
53 CDMWrapper(PP_Instance instance, pp::Module* module); | 65 CDMWrapper(PP_Instance instance, pp::Module* module); |
54 virtual ~CDMWrapper() {} | 66 virtual ~CDMWrapper() {} |
55 | 67 |
56 // PPP_ContentDecryptor_Private methods | 68 // PPP_ContentDecryptor_Private methods |
57 virtual bool GenerateKeyRequest(const std::string& key_system, | 69 virtual bool GenerateKeyRequest(const std::string& key_system, |
58 pp::VarArrayBuffer init_data) OVERRIDE; | 70 pp::VarArrayBuffer init_data) OVERRIDE; |
59 virtual bool AddKey(const std::string& session_id, | 71 virtual bool AddKey(const std::string& session_id, |
60 pp::VarArrayBuffer key) OVERRIDE; | 72 pp::VarArrayBuffer key, |
73 pp::VarArrayBuffer init_data) OVERRIDE; | |
61 virtual bool CancelKeyRequest(const std::string& session_id) OVERRIDE; | 74 virtual bool CancelKeyRequest(const std::string& session_id) OVERRIDE; |
62 virtual bool Decrypt(pp::Buffer_Dev encrypted_buffer, | 75 virtual bool Decrypt( |
63 int32_t request_id) OVERRIDE; | 76 pp::Buffer_Dev encrypted_buffer, |
77 const PP_EncryptedBlockInfo& encrypted_block_info) OVERRIDE; | |
ddorwin
2012/08/21 23:18:25
This is no longer an override of PPP_ContentDecryp
ddorwin
2012/08/21 23:21:09
Nevermind. I was looking at the C header, not the
| |
64 | 78 |
65 virtual bool DecryptAndDecode(pp::Buffer_Dev encrypted_buffer, | 79 virtual bool DecryptAndDecode( |
66 int32_t request_id) OVERRIDE { | 80 pp::Buffer_Dev encrypted_buffer, |
81 const PP_EncryptedBlockInfo& encrypted_block_info) OVERRIDE { | |
67 return false; | 82 return false; |
68 } | 83 } |
69 | 84 |
70 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 85 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
71 return true; | 86 return true; |
72 } | 87 } |
73 | 88 |
74 private: | 89 private: |
75 PP_Resource StringToBufferResource(const std::string& str); | 90 PP_Resource StringToBufferResource(const std::string& str); |
76 | 91 |
(...skipping 25 matching lines...) Expand all Loading... | |
102 decryptor_message.session_id = "0"; | 117 decryptor_message.session_id = "0"; |
103 decryptor_message.default_url = "http://www.google.com"; | 118 decryptor_message.default_url = "http://www.google.com"; |
104 decryptor_message.message_data = "GenerateKeyRequest"; | 119 decryptor_message.message_data = "GenerateKeyRequest"; |
105 | 120 |
106 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, | 121 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, |
107 decryptor_message)); | 122 decryptor_message)); |
108 return true; | 123 return true; |
109 } | 124 } |
110 | 125 |
111 bool CDMWrapper::AddKey(const std::string& session_id, | 126 bool CDMWrapper::AddKey(const std::string& session_id, |
112 pp::VarArrayBuffer key) { | 127 pp::VarArrayBuffer key, |
128 pp::VarArrayBuffer init_data) { | |
113 const std::string key_string(reinterpret_cast<char*>(key.Map()), | 129 const std::string key_string(reinterpret_cast<char*>(key.Map()), |
114 key.ByteLength()); | 130 key.ByteLength()); |
131 const std::string init_data_string(reinterpret_cast<char*>(init_data.Map()), | |
132 init_data.ByteLength()); | |
115 | 133 |
116 PP_DCHECK(!session_id.empty() && !key_string.empty()); | 134 PP_DCHECK(!session_id.empty() && !key_string.empty()); |
117 | 135 |
118 DecryptorMessage decryptor_message; | 136 DecryptorMessage decryptor_message; |
119 decryptor_message.key_system = "AddKey"; | 137 decryptor_message.key_system = "AddKey"; |
120 decryptor_message.session_id = "0"; | 138 decryptor_message.session_id = "0"; |
121 decryptor_message.default_url = "http://www.google.com"; | 139 decryptor_message.default_url = "http://www.google.com"; |
122 decryptor_message.message_data = "AddKey"; | 140 decryptor_message.message_data = "AddKey"; |
123 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyAdded, | 141 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyAdded, |
124 decryptor_message)); | 142 decryptor_message)); |
125 return true; | 143 return true; |
126 } | 144 } |
127 | 145 |
128 bool CDMWrapper::CancelKeyRequest(const std::string& session_id) { | 146 bool CDMWrapper::CancelKeyRequest(const std::string& session_id) { |
129 // TODO(tomfinegan): cancel pending key request in CDM. | 147 // TODO(tomfinegan): cancel pending key request in CDM. |
130 | 148 |
131 PP_DCHECK(!session_id.empty()); | 149 PP_DCHECK(!session_id.empty()); |
132 | 150 |
133 DecryptorMessage decryptor_message; | 151 DecryptorMessage decryptor_message; |
134 decryptor_message.key_system = "CancelKeyRequest"; | 152 decryptor_message.key_system = "CancelKeyRequest"; |
135 decryptor_message.session_id = "0"; | 153 decryptor_message.session_id = "0"; |
136 decryptor_message.default_url = "http://www.google.com"; | 154 decryptor_message.default_url = "http://www.google.com"; |
137 decryptor_message.message_data = "CancelKeyRequest"; | 155 decryptor_message.message_data = "CancelKeyRequest"; |
156 | |
138 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, | 157 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, |
139 decryptor_message)); | 158 decryptor_message)); |
140 return true; | 159 return true; |
141 } | 160 } |
142 | 161 |
143 bool CDMWrapper::Decrypt(pp::Buffer_Dev encrypted_buffer, | 162 bool CDMWrapper::Decrypt(pp::Buffer_Dev encrypted_buffer, |
144 int32_t request_id) { | 163 const PP_EncryptedBlockInfo& encrypted_block_info) { |
145 PP_DCHECK(!encrypted_buffer.is_null()); | 164 PP_DCHECK(!encrypted_buffer.is_null()); |
146 | 165 |
147 DecryptedBlock decrypted_block; | 166 DecryptedBlock decrypted_block; |
148 decrypted_block.request_id = request_id; | 167 decrypted_block.decrypted_data = "Pretend I'm decrypted data!"; |
149 decrypted_block.data = "Pretend I'm decrypted data!"; | 168 decrypted_block.decrypted_block_info.result = PP_DECRYPTRESULT_SUCCESS; |
169 decrypted_block.decrypted_block_info.tracking_info = | |
170 encrypted_block_info.tracking_info; | |
171 | |
172 // TODO(tomfinegan): This would end up copying a lot of data in the real | |
173 // implementation if we continue passing std::strings around. It *might* not | |
174 // be such a big deal w/a real CDM. We may be able to simply pass a pointer | |
175 // into the CDM. Otherwise we could look into using std::tr1::shared_ptr | |
176 // instead of passing a giant std::string filled with encrypted data. | |
ddorwin
2012/08/21 23:18:25
This will eventually be a PPB_Buffer_Dev ID and a
| |
150 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock, | 177 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock, |
151 decrypted_block)); | 178 decrypted_block)); |
152 return true; | 179 return true; |
153 } | 180 } |
154 | 181 |
155 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { | 182 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { |
156 if (str.empty()) | 183 if (str.empty()) |
157 return 0; | 184 return 0; |
158 | 185 |
159 pp::Buffer_Dev buffer(this, str.size()); | 186 pp::Buffer_Dev buffer(this, str.size()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 const DecryptorMessage& decryptor_message) { | 221 const DecryptorMessage& decryptor_message) { |
195 pp::ContentDecryptor_Private::KeyError(decryptor_message.key_system, | 222 pp::ContentDecryptor_Private::KeyError(decryptor_message.key_system, |
196 decryptor_message.session_id, | 223 decryptor_message.session_id, |
197 decryptor_message.media_error, | 224 decryptor_message.media_error, |
198 decryptor_message.system_code); | 225 decryptor_message.system_code); |
199 } | 226 } |
200 | 227 |
201 void CDMWrapper::DeliverBlock(int32_t result, | 228 void CDMWrapper::DeliverBlock(int32_t result, |
202 const DecryptedBlock& decrypted_block) { | 229 const DecryptedBlock& decrypted_block) { |
203 pp::Buffer_Dev decrypted_buffer( | 230 pp::Buffer_Dev decrypted_buffer( |
204 StringToBufferResource(decrypted_block.data)); | 231 StringToBufferResource(decrypted_block.decrypted_data)); |
205 pp::ContentDecryptor_Private::DeliverBlock(decrypted_buffer, | 232 pp::ContentDecryptor_Private::DeliverBlock( |
206 decrypted_block.request_id); | 233 decrypted_buffer, |
234 decrypted_block.decrypted_block_info); | |
207 } | 235 } |
208 | 236 |
209 // This object is the global object representing this plugin library as long | 237 // This object is the global object representing this plugin library as long |
210 // as it is loaded. | 238 // as it is loaded. |
211 class MyModule : public pp::Module { | 239 class MyModule : public pp::Module { |
212 public: | 240 public: |
213 MyModule() : pp::Module() {} | 241 MyModule() : pp::Module() {} |
214 virtual ~MyModule() {} | 242 virtual ~MyModule() {} |
215 | 243 |
216 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 244 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
217 return new CDMWrapper(instance, this); | 245 return new CDMWrapper(instance, this); |
218 } | 246 } |
219 }; | 247 }; |
220 | 248 |
221 namespace pp { | 249 namespace pp { |
222 | 250 |
223 // Factory function for your specialization of the Module object. | 251 // Factory function for your specialization of the Module object. |
224 Module* CreateModule() { | 252 Module* CreateModule() { |
225 return new MyModule(); | 253 return new MyModule(); |
226 } | 254 } |
227 | 255 |
228 } // namespace pp | 256 } // namespace pp |
OLD | NEW |