Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 <cassert> | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "ppapi/c/pp_errors.h" | |
| 11 #include "ppapi/cpp/completion_callback.h" | |
| 12 #include "ppapi/cpp/core.h" | |
| 13 #include "ppapi/cpp/instance.h" | |
| 14 #include "ppapi/cpp/module.h" | |
| 15 #include "ppapi/cpp/resource.h" | |
| 16 #include "ppapi/cpp/var.h" | |
| 17 #include "ppapi/cpp/dev/buffer_dev.h" | |
| 18 #include "ppapi/cpp/dev/content_decryptor_dev.h" | |
| 19 #include "ppapi/shared_impl/var.h" | |
| 20 #include "ppapi/utility/completion_callback_factory.h" | |
| 21 | |
| 22 using ppapi::StringVar; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 struct DecryptorMessage { | |
| 27 DecryptorMessage() | |
| 28 : media_error(0), | |
| 29 system_error(0), | |
| 30 callback(PP_MakeCompletionCallback(NULL, NULL)) {} | |
|
ddorwin
2012/07/26 22:20:17
Prefer (but I don't believe required) init list or
Tom Finegan
2012/07/27 08:55:01
Removed per later comment. Created DecryptedData s
| |
| 31 | |
| 32 PP_CompletionCallback callback; | |
| 33 std::string key_system; | |
| 34 std::string session_id; | |
| 35 std::string default_url; | |
| 36 std::string message_data; | |
| 37 uint16 media_error; | |
| 38 uint16 system_error; | |
| 39 }; | |
| 40 | |
| 41 bool IsMainThread() { | |
| 42 return pp::Module::Get()->core()->IsMainThread(); | |
| 43 } | |
| 44 | |
| 45 void CallOnMain(pp::CompletionCallback cb) { | |
| 46 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 | |
| 52 // A wrapper class responsible for managing interaction between the browser and | |
| 53 // a Content Decryption Module (CDM). | |
| 54 class CDMWrapper : public pp::Instance, | |
| 55 public pp::ContentDecryptor_Dev { | |
| 56 public: | |
| 57 CDMWrapper(PP_Instance instance, pp::Module* module); | |
| 58 virtual ~CDMWrapper() {} | |
| 59 | |
| 60 // PPP_ContentDecryptor_Dev methods | |
| 61 virtual bool GenerateKeyRequest(PP_Var key_system, PP_Resource init_data); | |
| 62 virtual bool AddKey(PP_Var session_id, PP_Resource key); | |
| 63 virtual bool CancelKeyRequest(PP_Var session_id); | |
| 64 virtual bool Decrypt(PP_Resource encrypted_block, | |
| 65 PP_CompletionCallback callback); | |
| 66 | |
| 67 virtual bool DecryptAndDecode(PP_Resource encrypted_block, | |
| 68 PP_CompletionCallback callback) { | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 PP_Resource StringToBufferResource(const std::string& str); | |
| 78 | |
| 79 // <code>PPB_ContentDecryptor_Dev</code> dispatchers. These are passed to | |
| 80 // <code>callback_factory_</code> to ensure that calls into | |
| 81 // <code>PPP_ContentDecryptor_Dev</code> are asynchronous. | |
| 82 void NeedKey(int32 result, const DecryptorMessage& decryptor_message); | |
| 83 void KeyAdded(int32 result, const DecryptorMessage& decryptor_message); | |
| 84 void KeyMessage(int32 result, const DecryptorMessage& decryptor_message); | |
| 85 void KeyError(int32 result, const DecryptorMessage& decryptor_message); | |
| 86 void DeliverBlock(int32 result, const DecryptorMessage& decryptor_message); | |
| 87 | |
| 88 pp::CompletionCallbackFactory<CDMWrapper> callback_factory_; | |
| 89 | |
| 90 // TODO(tomfinegan): Should this be a list of session IDs? Needs removal after | |
| 91 // testing (I think!). | |
| 92 std::string session_id_; | |
| 93 | |
| 94 // TODO(tomfinegan): Should these be multimaps of session_id:key_system and | |
| 95 // session_id:key? Or am I completely confused? :) | |
| 96 std::string key_; | |
| 97 std::string key_system_; | |
| 98 }; | |
| 99 | |
| 100 CDMWrapper::CDMWrapper(PP_Instance instance, | |
| 101 pp::Module* module) | |
| 102 : pp::Instance(instance), | |
| 103 pp::ContentDecryptor_Dev(this) { | |
| 104 callback_factory_.Initialize(this); | |
| 105 } | |
| 106 | |
| 107 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg, | |
| 108 PP_Resource init_data) { | |
| 109 if (init_data) { | |
| 110 pp::Buffer_Dev init_buffer(init_data); | |
| 111 if (!init_buffer.data()) { | |
| 112 return false; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 StringVar* key_system_var = StringVar::FromPPVar(key_system_arg); | |
| 117 | |
| 118 // TODO(tomfinegan): confirm support for the key system. | |
| 119 | |
| 120 DecryptorMessage decryptor_message; | |
| 121 key_system_ = key_system_var->value(); | |
| 122 decryptor_message.key_system = key_system_; | |
| 123 session_id_ = "12345"; | |
| 124 decryptor_message.session_id = session_id_; | |
| 125 decryptor_message.default_url = "http://www.google.com"; | |
| 126 decryptor_message.message_data = "key request"; | |
| 127 | |
| 128 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, | |
| 129 decryptor_message)); | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) { | |
| 134 std::string session_id = StringVar::FromPPVar(session_id_var)->value(); | |
| 135 | |
| 136 pp::Buffer_Dev key_buffer(key); | |
| 137 if (!key_buffer.data()) | |
| 138 return false; | |
| 139 | |
| 140 key_.assign(reinterpret_cast<char*>(key_buffer.data()), key_buffer.size()); | |
| 141 | |
| 142 DecryptorMessage decryptor_message; | |
| 143 decryptor_message.key_system = key_system_; | |
| 144 decryptor_message.session_id = session_id; | |
| 145 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyAdded, | |
| 146 decryptor_message)); | |
| 147 return true; | |
| 148 } | |
| 149 | |
| 150 bool CDMWrapper::CancelKeyRequest(PP_Var session_id_var) { | |
| 151 std::string session_id = StringVar::FromPPVar(session_id_var)->value(); | |
| 152 | |
| 153 // TODO(tomfinegan): cancel pending key request in CDM. | |
| 154 | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 bool CDMWrapper::Decrypt(PP_Resource encrypted_block, | |
| 159 PP_CompletionCallback callback) { | |
| 160 pp::Buffer_Dev block_buffer(encrypted_block); | |
| 161 if (!block_buffer.data()) { | |
| 162 return false; | |
| 163 } | |
| 164 | |
|
ddorwin
2012/07/26 22:20:17
If invalid callback, return false. No sense in dec
Tom Finegan
2012/07/27 08:55:01
Done.
| |
| 165 DecryptorMessage decryptor_message; | |
| 166 decryptor_message.message_data = "Pretend I'm decrypted data!"; | |
|
ddorwin
2012/07/26 22:20:17
Need to set the callback.
Tom Finegan
2012/07/27 08:55:01
Done.
| |
| 167 CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock, | |
|
ddorwin
2012/07/26 22:20:17
This should not be using DecryptorMessage. This is
Tom Finegan
2012/07/27 08:55:01
As above-- DecryptedData.
| |
| 168 decryptor_message)); | |
| 169 return true; | |
| 170 } | |
| 171 | |
| 172 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { | |
| 173 if (str.empty()) | |
| 174 return 0; | |
| 175 | |
| 176 pp::Buffer_Dev buffer(this, str.size()); | |
| 177 if (!buffer.data()) | |
| 178 return 0; | |
| 179 | |
| 180 memcpy(buffer.data(), str.data(), str.size()); | |
| 181 return buffer.detach(); | |
| 182 } | |
| 183 | |
| 184 void CDMWrapper::NeedKey(int32 result, | |
| 185 const DecryptorMessage& decryptor_message) { | |
| 186 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 187 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 188 PP_Resource init_data = | |
| 189 StringToBufferResource(decryptor_message.message_data); | |
| 190 pp::ContentDecryptor_Dev::NeedKey(key_system, session_id, init_data); | |
| 191 } | |
| 192 | |
| 193 void CDMWrapper::KeyAdded(int32 result, | |
| 194 const DecryptorMessage& decryptor_message) { | |
| 195 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 196 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 197 pp::ContentDecryptor_Dev::KeyAdded(key_system, session_id); | |
| 198 } | |
| 199 | |
| 200 void CDMWrapper::KeyMessage(int32 result, | |
| 201 const DecryptorMessage& decryptor_message) { | |
| 202 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 203 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 204 PP_Resource message = | |
| 205 StringToBufferResource(decryptor_message.message_data); | |
| 206 PP_Var default_url = | |
| 207 StringVar::StringToPPVar(decryptor_message.default_url); | |
| 208 pp::ContentDecryptor_Dev::KeyMessage(key_system, | |
| 209 session_id, | |
| 210 message, | |
| 211 default_url); | |
| 212 } | |
| 213 | |
| 214 void CDMWrapper::KeyError(int32 result, | |
| 215 const DecryptorMessage& decryptor_message) { | |
| 216 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 217 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 218 pp::ContentDecryptor_Dev::KeyError(key_system, | |
| 219 session_id, | |
| 220 decryptor_message.media_error, | |
| 221 decryptor_message.system_error); | |
| 222 } | |
| 223 | |
| 224 void CDMWrapper::DeliverBlock(int32 result, | |
| 225 const DecryptorMessage& decryptor_message) { | |
|
ddorwin
2012/07/26 22:20:17
Wrong type as mentioned above.
Tom Finegan
2012/07/27 08:55:01
Done.
| |
| 226 if (decryptor_message.callback.func) { | |
|
ddorwin
2012/07/26 22:20:17
Should this ever be false?
Tom Finegan
2012/07/27 08:55:01
Nope.
| |
| 227 PP_Resource decrypted_resource = StringToBufferResource( | |
| 228 decryptor_message.message_data); | |
| 229 if (decrypted_resource) { | |
| 230 pp::ContentDecryptor_Dev::DeliverBlock(decrypted_resource, | |
| 231 decryptor_message.callback); | |
| 232 } | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 // This object is the global object representing this plugin library as long | |
| 237 // as it is loaded. | |
| 238 class MyModule : public pp::Module { | |
| 239 public: | |
| 240 MyModule() : pp::Module() {} | |
| 241 virtual ~MyModule() {} | |
| 242 | |
| 243 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 244 return new CDMWrapper(instance, this); | |
| 245 } | |
| 246 }; | |
| 247 | |
| 248 namespace pp { | |
| 249 | |
| 250 // Factory function for your specialization of the Module object. | |
| 251 Module* CreateModule() { | |
| 252 return new MyModule(); | |
| 253 } | |
| 254 | |
| 255 } // namespace pp | |
| OLD | NEW |