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/c/dev/ppb_buffer_dev.h" | |
|
ddorwin
2012/07/24 18:57:26
Shouldn't need.
Tom Finegan
2012/07/25 02:00:07
Done.
| |
| 12 #include "ppapi/c/dev/ppb_content_decryptor_dev.h" | |
| 13 #include "ppapi/cpp/completion_callback.h" | |
| 14 #include "ppapi/cpp/core.h" | |
| 15 #include "ppapi/cpp/instance.h" | |
| 16 #include "ppapi/cpp/module.h" | |
| 17 #include "ppapi/cpp/resource.h" | |
| 18 #include "ppapi/cpp/var.h" | |
| 19 #include "ppapi/cpp/dev/buffer_dev.h" | |
| 20 #include "ppapi/cpp/dev/content_decryptor_dev.h" | |
| 21 #include "ppapi/shared_impl/var.h" | |
| 22 #include "ppapi/utility/completion_callback_factory.h" | |
| 23 | |
| 24 using ppapi::StringVar; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 struct DecryptorMessage { | |
| 29 std::string key_system; | |
| 30 std::string session_id; | |
| 31 std::string default_url; | |
| 32 std::string message_data; | |
| 33 uint16 media_error; | |
| 34 uint16 system_error; | |
| 35 }; | |
| 36 | |
| 37 DecryptorMessage DM_Empty() { | |
|
ddorwin
2012/07/24 18:57:26
I think this is discouraged because of the static
Tom Finegan
2012/07/25 02:00:07
Done/added ctor.
| |
| 38 DecryptorMessage decryptor_message = { "", "", "", "", 0, 0 }; | |
| 39 return decryptor_message; | |
| 40 } | |
| 41 | |
| 42 bool IsMainThread() { | |
| 43 return pp::Module::Get()->core()->IsMainThread(); | |
| 44 } | |
| 45 | |
| 46 void CallOnMain(pp::CompletionCallback cb) { | |
| 47 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 | |
| 53 // A wrapper class responsible for managing interaction between the browser and | |
| 54 // a Content Decryption Module (CDM). | |
| 55 class CDMWrapper : public pp::Instance, | |
| 56 public pp::ContentDecryptor_Dev { | |
| 57 public: | |
| 58 explicit CDMWrapper(PP_Instance instance, pp::Module* module); | |
|
fgalligan1
2012/07/24 04:33:41
Don't need explicit.
Tom Finegan
2012/07/25 02:00:07
Done/that's what I get for copy/pasting. :)
| |
| 59 virtual ~CDMWrapper() {} | |
| 60 | |
| 61 // PPP_ContentDecryptor_Dev methods | |
| 62 virtual bool GenerateKeyRequest(PP_Var key_system, PP_Resource init_data); | |
| 63 virtual bool AddKey(PP_Var session_id, PP_Resource key); | |
| 64 virtual bool CancelKeyRequest(PP_Var session_id); | |
| 65 virtual bool Decrypt(PP_Resource encrypted_block, | |
| 66 PP_CompletionCallback callback); | |
| 67 | |
| 68 virtual bool DecryptAndDecode(PP_Resource encrypted_block, | |
| 69 PP_CompletionCallback callback) { | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 PP_Resource StringToBufferResource(const std::string& str); | |
| 79 | |
| 80 // <code>PPB_ContentDecryptor_Dev</code> dispatchers. These are passed to | |
| 81 // <code>factory_</code> to ensure that calls into | |
| 82 // <code>PPP_ContentDecryptor_Dev</code> are asynchronous. | |
| 83 void NeedKey(int32 result, const DecryptorMessage& decryptor_message); | |
| 84 void KeyAdded(int32 result, const DecryptorMessage& decryptor_message); | |
| 85 void KeyMessage(int32 result, const DecryptorMessage& decryptor_message); | |
| 86 void KeyError(int32 result, const DecryptorMessage& decryptor_message); | |
| 87 void DeliverBlock(int32 result, const DecryptorMessage& decryptor_message); | |
| 88 | |
| 89 // Browser interfaces. | |
| 90 const PPB_Buffer_Dev* buffer_if_; | |
| 91 const PPB_ContentDecryptor_Dev* decryptor_if_; | |
| 92 | |
| 93 pp::CompletionCallbackFactory<CDMWrapper> factory_; | |
|
ddorwin
2012/07/24 18:57:26
Unless you see this named used elsewhere, you shou
Tom Finegan
2012/07/25 02:00:07
Stole it from:
http://code.google.com/p/chromium/s
| |
| 94 | |
| 95 // TODO(tomfinegan): Should this be a list of session IDs? Needs removal after | |
| 96 // testing (I think!). | |
|
ddorwin
2012/07/24 18:57:26
The EME API will eventually be based on object ses
| |
| 97 std::string session_id_; | |
| 98 | |
| 99 // TODO(tomfinegan): Should these be multimaps of session_id:key_system and | |
|
ddorwin
2012/07/24 18:57:26
There is only one key system per CDM instance. (If
| |
| 100 // session_id:key? Or am I completely confused? :) | |
| 101 std::string key_; | |
| 102 std::string key_system_; | |
| 103 }; | |
| 104 | |
| 105 CDMWrapper::CDMWrapper(PP_Instance instance, | |
| 106 pp::Module* module) | |
| 107 : pp::Instance(instance), | |
| 108 pp::ContentDecryptor_Dev(this) { | |
| 109 // TODO(tomfinegan): Replace with use of C++ interfaces when ready. Need to | |
| 110 // add the PPB interface methods to the cpp/ wrapper first. | |
| 111 decryptor_if_ = static_cast<const PPB_ContentDecryptor_Dev*>( | |
| 112 module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_DEV_INTERFACE)); | |
| 113 assert(decryptor_if_); | |
| 114 buffer_if_ = static_cast<const PPB_Buffer_Dev*>( | |
|
ddorwin
2012/07/24 18:57:26
Just use the C++ interface. Should simplify things
Tom Finegan
2012/07/25 02:00:07
Done.
| |
| 115 module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE)); | |
| 116 assert(buffer_if_); | |
| 117 factory_.Initialize(this); | |
| 118 } | |
| 119 | |
| 120 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg, | |
| 121 PP_Resource init_data) { | |
| 122 if (init_data) { | |
| 123 pp::Buffer_Dev init_buffer(init_data); | |
| 124 if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) || | |
| 125 !init_buffer.data()) { | |
| 126 return false; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 StringVar* key_system_var = StringVar::FromPPVar(key_system_arg); | |
| 131 | |
| 132 // TODO(tomfinegan): confirm support for the key system. | |
| 133 | |
| 134 DecryptorMessage decryptor_message = DM_Empty(); | |
|
ddorwin
2012/07/24 18:57:26
You're just using an assignment operator here. Ins
Tom Finegan
2012/07/25 02:00:07
Done.
| |
| 135 key_system_ = key_system_var->value(); | |
| 136 decryptor_message.key_system = key_system_; | |
| 137 session_id_ = "12345"; | |
| 138 decryptor_message.session_id = session_id_; | |
| 139 decryptor_message.default_url = "http://www.google.com"; | |
| 140 decryptor_message.message_data = "key request"; | |
| 141 | |
| 142 CallOnMain(factory_.NewCallback(&CDMWrapper::KeyMessage, decryptor_message)); | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) { | |
| 147 std::string session_id = StringVar::FromPPVar(session_id_var)->value(); | |
| 148 | |
| 149 pp::Buffer_Dev key_buffer(key); | |
| 150 if (!buffer_if_->IsBuffer(key) || !key_buffer.data()) | |
|
ddorwin
2012/07/24 18:57:26
Shouldn't you be checking IsBuffer(key_buffer.pp_r
Tom Finegan
2012/07/25 02:00:07
Removed in favor of using the cpp wrapper.
| |
| 151 return false; | |
| 152 | |
| 153 key_.assign(reinterpret_cast<char*>(key_buffer.data()), key_buffer.size()); | |
| 154 | |
| 155 DecryptorMessage decryptor_message = DM_Empty(); | |
|
ddorwin
2012/07/24 18:57:26
same
Tom Finegan
2012/07/25 02:00:07
Ditto.
| |
| 156 // TODO(tomfinegan): determine where the key system is coming from at this | |
| 157 // point. | |
| 158 decryptor_message.key_system = "look up in multimap maybe?"; | |
|
ddorwin
2012/07/24 18:57:26
just use the member - it can't change.
Tom Finegan
2012/07/25 02:00:07
Done.
| |
| 159 decryptor_message.session_id = session_id; | |
| 160 CallOnMain(factory_.NewCallback(&CDMWrapper::KeyAdded, decryptor_message)); | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 bool CDMWrapper::CancelKeyRequest(PP_Var session_id_var) { | |
| 165 // TODO(tomfinegan): This seems extremely heavy handed-- will one CDMWrapper | |
| 166 // instance ever have multiple sessions? | |
|
ddorwin
2012/07/24 18:57:26
Yes.
This is more for teardown. Not sure we need t
Tom Finegan
2012/07/25 02:00:07
Removed.
| |
| 167 factory_.CancelAll(); | |
| 168 | |
| 169 std::string session_id = StringVar::FromPPVar(session_id_var)->value(); | |
| 170 | |
| 171 DecryptorMessage decryptor_message = DM_Empty(); | |
|
ddorwin
2012/07/24 18:57:26
There is no message to send for cancelKeyRequest.
Tom Finegan
2012/07/25 02:00:07
Ah, ok. Removed. I'll read over the EME stuff some
| |
| 172 decryptor_message.session_id = session_id; | |
| 173 decryptor_message.message_data = "Request cancelled for session "; | |
| 174 decryptor_message.message_data.append(session_id); | |
| 175 | |
| 176 CallOnMain(factory_.NewCallback(&CDMWrapper::KeyMessage, decryptor_message)); | |
| 177 return true; | |
| 178 } | |
| 179 | |
| 180 bool CDMWrapper::Decrypt(PP_Resource encrypted_block, | |
| 181 PP_CompletionCallback callback) { | |
| 182 pp::Buffer_Dev block_buffer(encrypted_block); | |
| 183 if (!buffer_if_->IsBuffer(block_buffer.pp_resource()) || | |
| 184 !block_buffer.data()) { | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 const std::string kDummyDecryptedData = "Pretend I'm decrypted data!"; | |
| 189 PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData); | |
| 190 if (!decrypted_resource) | |
| 191 return false; | |
| 192 | |
| 193 decryptor_if_->DeliverBlock(pp_instance(), decrypted_resource, callback); | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { | |
| 198 if (str.empty()) | |
| 199 return 0; | |
| 200 | |
| 201 pp::Buffer_Dev buffer(this, str.size()); | |
| 202 if (!buffer_if_->IsBuffer(buffer.pp_resource()) || !buffer.data()) | |
| 203 return 0; | |
| 204 | |
| 205 memcpy(buffer.data(), str.data(), str.size()); | |
| 206 return buffer.detach(); | |
| 207 } | |
| 208 | |
| 209 void CDMWrapper::NeedKey(int32 result, | |
| 210 const DecryptorMessage& decryptor_message) { | |
| 211 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 212 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 213 PP_Resource init_data = | |
| 214 StringToBufferResource(decryptor_message.message_data); | |
| 215 decryptor_if_->NeedKey(pp_instance(), key_system, session_id, init_data); | |
| 216 } | |
| 217 | |
| 218 void CDMWrapper::KeyAdded(int32 result, | |
| 219 const DecryptorMessage& decryptor_message) { | |
| 220 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 221 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 222 decryptor_if_->KeyAdded(pp_instance(), key_system, session_id); | |
| 223 } | |
| 224 | |
| 225 void CDMWrapper::KeyMessage(int32 result, | |
| 226 const DecryptorMessage& decryptor_message) { | |
| 227 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 228 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 229 PP_Resource message = | |
| 230 StringToBufferResource(decryptor_message.message_data); | |
| 231 PP_Var default_url = | |
| 232 StringVar::StringToPPVar(decryptor_message.default_url); | |
| 233 decryptor_if_->KeyMessage(pp_instance(), | |
| 234 key_system, | |
| 235 session_id, | |
| 236 message, | |
| 237 default_url); | |
| 238 } | |
| 239 | |
| 240 void CDMWrapper::KeyError(int32 result, | |
| 241 const DecryptorMessage& decryptor_message) { | |
| 242 PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); | |
| 243 PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); | |
| 244 decryptor_if_->KeyError(pp_instance(), | |
| 245 key_system, | |
| 246 session_id, | |
| 247 decryptor_message.media_error, | |
|
ddorwin
2012/07/24 18:57:26
You could do all of the params in these calls like
| |
| 248 decryptor_message.system_error); | |
| 249 } | |
| 250 | |
| 251 void CDMWrapper::DeliverBlock(int32 result, | |
| 252 const DecryptorMessage& decryptor_message) { | |
| 253 } | |
| 254 | |
| 255 // This object is the global object representing this plugin library as long | |
| 256 // as it is loaded. | |
| 257 class MyModule : public pp::Module { | |
| 258 public: | |
| 259 MyModule() : pp::Module() {} | |
| 260 virtual ~MyModule() {} | |
| 261 | |
| 262 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 263 return new CDMWrapper(instance, this); | |
| 264 } | |
| 265 }; | |
| 266 | |
| 267 namespace pp { | |
| 268 | |
| 269 // Factory function for your specialization of the Module object. | |
| 270 Module* CreateModule() { | |
| 271 return new MyModule(); | |
| 272 } | |
| 273 | |
| 274 } // namespace pp | |
| OLD | NEW |