Chromium Code Reviews| Index: ppapi/examples/content_decryptor/content_decryptor.cc |
| diff --git a/ppapi/examples/content_decryptor/content_decryptor.cc b/ppapi/examples/content_decryptor/content_decryptor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..451a7948339bbc5b043fba0229d422a695b35b65 |
| --- /dev/null |
| +++ b/ppapi/examples/content_decryptor/content_decryptor.cc |
| @@ -0,0 +1,274 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <cassert> |
| +#include <string> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#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.
|
| +#include "ppapi/c/dev/ppb_content_decryptor_dev.h" |
| +#include "ppapi/cpp/completion_callback.h" |
| +#include "ppapi/cpp/core.h" |
| +#include "ppapi/cpp/instance.h" |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/cpp/resource.h" |
| +#include "ppapi/cpp/var.h" |
| +#include "ppapi/cpp/dev/buffer_dev.h" |
| +#include "ppapi/cpp/dev/content_decryptor_dev.h" |
| +#include "ppapi/shared_impl/var.h" |
| +#include "ppapi/utility/completion_callback_factory.h" |
| + |
| +using ppapi::StringVar; |
| + |
| +namespace { |
| + |
| +struct DecryptorMessage { |
| + std::string key_system; |
| + std::string session_id; |
| + std::string default_url; |
| + std::string message_data; |
| + uint16 media_error; |
| + uint16 system_error; |
| +}; |
| + |
| +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.
|
| + DecryptorMessage decryptor_message = { "", "", "", "", 0, 0 }; |
| + return decryptor_message; |
| +} |
| + |
| +bool IsMainThread() { |
| + return pp::Module::Get()->core()->IsMainThread(); |
| +} |
| + |
| +void CallOnMain(pp::CompletionCallback cb) { |
| + pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); |
| +} |
| + |
| +} // namespace |
| + |
| + |
| +// A wrapper class responsible for managing interaction between the browser and |
| +// a Content Decryption Module (CDM). |
| +class CDMWrapper : public pp::Instance, |
| + public pp::ContentDecryptor_Dev { |
| + public: |
| + 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. :)
|
| + virtual ~CDMWrapper() {} |
| + |
| + // PPP_ContentDecryptor_Dev methods |
| + virtual bool GenerateKeyRequest(PP_Var key_system, PP_Resource init_data); |
| + virtual bool AddKey(PP_Var session_id, PP_Resource key); |
| + virtual bool CancelKeyRequest(PP_Var session_id); |
| + virtual bool Decrypt(PP_Resource encrypted_block, |
| + PP_CompletionCallback callback); |
| + |
| + virtual bool DecryptAndDecode(PP_Resource encrypted_block, |
| + PP_CompletionCallback callback) { |
| + return false; |
| + } |
| + |
| + virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| + return true; |
| + } |
| + |
| + private: |
| + PP_Resource StringToBufferResource(const std::string& str); |
| + |
| + // <code>PPB_ContentDecryptor_Dev</code> dispatchers. These are passed to |
| + // <code>factory_</code> to ensure that calls into |
| + // <code>PPP_ContentDecryptor_Dev</code> are asynchronous. |
| + void NeedKey(int32 result, const DecryptorMessage& decryptor_message); |
| + void KeyAdded(int32 result, const DecryptorMessage& decryptor_message); |
| + void KeyMessage(int32 result, const DecryptorMessage& decryptor_message); |
| + void KeyError(int32 result, const DecryptorMessage& decryptor_message); |
| + void DeliverBlock(int32 result, const DecryptorMessage& decryptor_message); |
| + |
| + // Browser interfaces. |
| + const PPB_Buffer_Dev* buffer_if_; |
| + const PPB_ContentDecryptor_Dev* decryptor_if_; |
| + |
| + 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
|
| + |
| + // TODO(tomfinegan): Should this be a list of session IDs? Needs removal after |
| + // testing (I think!). |
|
ddorwin
2012/07/24 18:57:26
The EME API will eventually be based on object ses
|
| + std::string session_id_; |
| + |
| + // 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
|
| + // session_id:key? Or am I completely confused? :) |
| + std::string key_; |
| + std::string key_system_; |
| +}; |
| + |
| +CDMWrapper::CDMWrapper(PP_Instance instance, |
| + pp::Module* module) |
| + : pp::Instance(instance), |
| + pp::ContentDecryptor_Dev(this) { |
| + // TODO(tomfinegan): Replace with use of C++ interfaces when ready. Need to |
| + // add the PPB interface methods to the cpp/ wrapper first. |
| + decryptor_if_ = static_cast<const PPB_ContentDecryptor_Dev*>( |
| + module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_DEV_INTERFACE)); |
| + assert(decryptor_if_); |
| + 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.
|
| + module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE)); |
| + assert(buffer_if_); |
| + factory_.Initialize(this); |
| +} |
| + |
| +bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg, |
| + PP_Resource init_data) { |
| + if (init_data) { |
| + pp::Buffer_Dev init_buffer(init_data); |
| + if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) || |
| + !init_buffer.data()) { |
| + return false; |
| + } |
| + } |
| + |
| + StringVar* key_system_var = StringVar::FromPPVar(key_system_arg); |
| + |
| + // TODO(tomfinegan): confirm support for the key system. |
| + |
| + 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.
|
| + key_system_ = key_system_var->value(); |
| + decryptor_message.key_system = key_system_; |
| + session_id_ = "12345"; |
| + decryptor_message.session_id = session_id_; |
| + decryptor_message.default_url = "http://www.google.com"; |
| + decryptor_message.message_data = "key request"; |
| + |
| + CallOnMain(factory_.NewCallback(&CDMWrapper::KeyMessage, decryptor_message)); |
| + return true; |
| +} |
| + |
| +bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) { |
| + std::string session_id = StringVar::FromPPVar(session_id_var)->value(); |
| + |
| + pp::Buffer_Dev key_buffer(key); |
| + 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.
|
| + return false; |
| + |
| + key_.assign(reinterpret_cast<char*>(key_buffer.data()), key_buffer.size()); |
| + |
| + DecryptorMessage decryptor_message = DM_Empty(); |
|
ddorwin
2012/07/24 18:57:26
same
Tom Finegan
2012/07/25 02:00:07
Ditto.
|
| + // TODO(tomfinegan): determine where the key system is coming from at this |
| + // point. |
| + 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.
|
| + decryptor_message.session_id = session_id; |
| + CallOnMain(factory_.NewCallback(&CDMWrapper::KeyAdded, decryptor_message)); |
| + return true; |
| +} |
| + |
| +bool CDMWrapper::CancelKeyRequest(PP_Var session_id_var) { |
| + // TODO(tomfinegan): This seems extremely heavy handed-- will one CDMWrapper |
| + // 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.
|
| + factory_.CancelAll(); |
| + |
| + std::string session_id = StringVar::FromPPVar(session_id_var)->value(); |
| + |
| + 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
|
| + decryptor_message.session_id = session_id; |
| + decryptor_message.message_data = "Request cancelled for session "; |
| + decryptor_message.message_data.append(session_id); |
| + |
| + CallOnMain(factory_.NewCallback(&CDMWrapper::KeyMessage, decryptor_message)); |
| + return true; |
| +} |
| + |
| +bool CDMWrapper::Decrypt(PP_Resource encrypted_block, |
| + PP_CompletionCallback callback) { |
| + pp::Buffer_Dev block_buffer(encrypted_block); |
| + if (!buffer_if_->IsBuffer(block_buffer.pp_resource()) || |
| + !block_buffer.data()) { |
| + return false; |
| + } |
| + |
| + const std::string kDummyDecryptedData = "Pretend I'm decrypted data!"; |
| + PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData); |
| + if (!decrypted_resource) |
| + return false; |
| + |
| + decryptor_if_->DeliverBlock(pp_instance(), decrypted_resource, callback); |
| + return true; |
| +} |
| + |
| +PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { |
| + if (str.empty()) |
| + return 0; |
| + |
| + pp::Buffer_Dev buffer(this, str.size()); |
| + if (!buffer_if_->IsBuffer(buffer.pp_resource()) || !buffer.data()) |
| + return 0; |
| + |
| + memcpy(buffer.data(), str.data(), str.size()); |
| + return buffer.detach(); |
| +} |
| + |
| +void CDMWrapper::NeedKey(int32 result, |
| + const DecryptorMessage& decryptor_message) { |
| + PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); |
| + PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); |
| + PP_Resource init_data = |
| + StringToBufferResource(decryptor_message.message_data); |
| + decryptor_if_->NeedKey(pp_instance(), key_system, session_id, init_data); |
| +} |
| + |
| +void CDMWrapper::KeyAdded(int32 result, |
| + const DecryptorMessage& decryptor_message) { |
| + PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); |
| + PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); |
| + decryptor_if_->KeyAdded(pp_instance(), key_system, session_id); |
| +} |
| + |
| +void CDMWrapper::KeyMessage(int32 result, |
| + const DecryptorMessage& decryptor_message) { |
| + PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); |
| + PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); |
| + PP_Resource message = |
| + StringToBufferResource(decryptor_message.message_data); |
| + PP_Var default_url = |
| + StringVar::StringToPPVar(decryptor_message.default_url); |
| + decryptor_if_->KeyMessage(pp_instance(), |
| + key_system, |
| + session_id, |
| + message, |
| + default_url); |
| +} |
| + |
| +void CDMWrapper::KeyError(int32 result, |
| + const DecryptorMessage& decryptor_message) { |
| + PP_Var key_system = StringVar::StringToPPVar(decryptor_message.key_system); |
| + PP_Var session_id = StringVar::StringToPPVar(decryptor_message.session_id); |
| + decryptor_if_->KeyError(pp_instance(), |
| + key_system, |
| + session_id, |
| + decryptor_message.media_error, |
|
ddorwin
2012/07/24 18:57:26
You could do all of the params in these calls like
|
| + decryptor_message.system_error); |
| +} |
| + |
| +void CDMWrapper::DeliverBlock(int32 result, |
| + const DecryptorMessage& decryptor_message) { |
| +} |
| + |
| +// This object is the global object representing this plugin library as long |
| +// as it is loaded. |
| +class MyModule : public pp::Module { |
| + public: |
| + MyModule() : pp::Module() {} |
| + virtual ~MyModule() {} |
| + |
| + virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| + return new CDMWrapper(instance, this); |
| + } |
| +}; |
| + |
| +namespace pp { |
| + |
| +// Factory function for your specialization of the Module object. |
| +Module* CreateModule() { |
| + return new MyModule(); |
| +} |
| + |
| +} // namespace pp |