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..b4807222bc21b4186c26d2895f32fd67990ae1c7 |
| --- /dev/null |
| +++ b/ppapi/examples/content_decryptor/content_decryptor.cc |
| @@ -0,0 +1,217 @@ |
| +// 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/pp_var.h" |
|
ddorwin
2012/07/23 23:53:36
Shouldn't need anything from c/ except. cpp/ shoul
Tom Finegan
2012/07/24 00:26:21
Removed some of the c/ stuff. I'll look into this
|
| +#include "ppapi/c/ppb_var.h" |
| +#include "ppapi/c/dev/ppb_buffer_dev.h" |
| +#include "ppapi/c/dev/ppb_console_dev.h" |
| +#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" |
| + |
| +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; |
| +}; |
| + |
| +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); |
| + 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) { |
| + return false; |
| + } |
| + |
| + 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); |
| + void NeedKey(int32 result, void* data); |
| + void KeyAdded(int32 result, void* data); |
| + void KeyMessage(int32 result, void* data); |
| + void KeyError(int32 result, void* data); |
| + void DeliverBlock(int32 result, void* data); |
| + |
| + |
| + // Browser interfaces. |
| + const PPB_Buffer_Dev* buffer_if_; |
| + const PPB_ContentDecryptor_Dev* decryptor_if_; |
| + |
| + pp::CompletionCallbackFactory<CDMWrapper> factory_; |
| + std::string key_; |
|
ddorwin
2012/07/23 23:53:36
I assume this is temporary for testing? Probably:
Tom Finegan
2012/07/24 00:26:21
I have a TODO there as a result of today's work--
|
| +}; |
| + |
| +// 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); |
| + } |
| +}; |
| + |
| +CDMWrapper::CDMWrapper(PP_Instance instance, |
| + pp::Module* module) |
| + : pp::Instance(instance), |
| + pp::ContentDecryptor_Dev(this) { |
| + decryptor_if_ = static_cast<const PPB_ContentDecryptor_Dev*>( |
|
ddorwin
2012/07/23 23:53:36
TODO(tomfinegan): Replace with use of C++ interfac
Tom Finegan
2012/07/24 00:26:21
Done.
|
| + module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_DEV_INTERFACE)); |
| + assert(decryptor_if_); |
| + buffer_if_ = static_cast<const PPB_Buffer_Dev*>( |
| + 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); |
|
ddorwin
2012/07/23 23:53:36
You'll probably need this variable below, meaning
Tom Finegan
2012/07/24 00:26:21
Yeah-- I'll move it once I'm actually doing someth
|
| + if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) || |
|
ddorwin
2012/07/23 23:53:36
.pp_resource() appears to be unnecessary (line 151
Tom Finegan
2012/07/24 00:26:21
Doesn't compile. key is a PP_Resource, but key_buf
ddorwin
2012/07/24 18:57:26
Ahh, the difference with 151 was subtle. See new c
|
| + !init_buffer.data()) { |
| + return false; |
| + } |
| + } |
| + |
| + using ppapi::StringVar; |
|
ddorwin
2012/07/23 23:53:36
using should be at the top of the file.
Same for 1
Tom Finegan
2012/07/24 00:26:21
Done.
|
| + StringVar* key_system_var = ppapi::StringVar::FromPPVar(key_system_arg); |
|
ddorwin
2012/07/23 23:53:36
no need for ppapi:: here.
Update to match 148, whi
Tom Finegan
2012/07/24 00:26:21
Done.
|
| + |
| + DecryptorMessage* decryptor_message = new DecryptorMessage; |
| + if (!decryptor_message) |
| + return false; |
| + |
| + decryptor_message->key_system = key_system_var->value(); |
| + decryptor_message->session_id = "12345"; |
| + decryptor_message->default_url = "http://www.google.com"; |
| + decryptor_message->message_data = "key request"; |
| + |
| + void* data = reinterpret_cast<void*>(decryptor_message); |
| + CallOnMain(factory_.NewCallback(&CDMWrapper::KeyMessage, data)); |
| + return true; |
| +} |
| + |
| +bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) { |
| + using ppapi::StringVar; |
| + std::string session_id = StringVar::FromPPVar(session_id_var)->value(); |
| + |
| + pp::Buffer_Dev key_buffer(key); |
| + if (!buffer_if_->IsBuffer(key) || !key_buffer.data()) |
| + return false; |
| + |
| + key_.assign(reinterpret_cast<char*>(key_buffer.data()), key_buffer.size()); |
| + 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.size()) |
| + 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, void* data) { |
| + |
| +} |
| + |
| +void CDMWrapper::KeyMessage(int32 result, void* data) { |
| + scoped_ptr<DecryptorMessage> decryptor_message( |
| + reinterpret_cast<DecryptorMessage*>(data)); |
| + if (decryptor_message.get()) { |
|
ddorwin
2012/07/23 23:53:36
assuming else is an error, fail early and de-inden
Tom Finegan
2012/07/24 00:26:21
Passing const DecryptorMessage& instead of a void*
ddorwin
2012/07/24 18:57:26
I'm not sure of the mechanism, but pass by value,
|
| + using ppapi::StringVar; |
| + 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); |
| + } |
| +} |
| + |
| +namespace pp { |
| + |
| +// Factory function for your specialization of the Module object. |
| +Module* CreateModule() { |
| + return new MyModule(); |
| +} |
| + |
| +} // namespace pp |