Index: ppapi/examples/content_decryption_module/content_decryption_module.cc |
diff --git a/ppapi/examples/content_decryption_module/content_decryption_module.cc b/ppapi/examples/content_decryption_module/content_decryption_module.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..16993ad7a032d7f4bdf78c905463a4319c4c628c |
--- /dev/null |
+++ b/ppapi/examples/content_decryption_module/content_decryption_module.cc |
@@ -0,0 +1,134 @@ |
+// 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 "base/string_piece.h" |
+#include "ppapi/c/dev/ppb_buffer_dev.h" |
+#include "ppapi/c/dev/ppb_console_dev.h" |
+#include "ppapi/c/dev/ppb_content_decryption_module_dev.h" |
+#include "ppapi/cpp/instance.h" |
+#include "ppapi/cpp/module.h" |
+#include "ppapi/cpp/resource.h" |
+#include "ppapi/cpp/dev/buffer_dev.h" |
+#include "ppapi/cpp/dev/content_decryption_module_dev.h" |
+#include "ppapi/shared_impl/var.h" |
+ |
+class CDMInstance : public pp::Instance, |
+ public pp::ContentDecryptionModule_Dev { |
+ public: |
+ explicit CDMInstance(PP_Instance instance, pp::Module* module); |
+ virtual ~CDMInstance() {} |
+ |
+ // PPP_ContentDecryptionModule_Dev methods |
+ virtual bool GenerateKeyRequest(PP_Var key_system, |
+ PP_Resource init_data); |
+ |
+ virtual bool AddKey(PP_Var session_id, |
+ PP_Resource key) { |
+ return false; |
+ } |
+ |
+ 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: |
+ // Browser interfaces. |
+ const PPB_Buffer_Dev* buffer_if_; |
+ const PPB_Console_Dev* console_if_; |
+ const PPB_ContentDecryptionModule_Dev* browser_cdm_if_; |
+}; |
+ |
+// 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() {} |
+ |
+ // Override CreateInstance to create your customized Instance object. |
+ virtual pp::Instance* CreateInstance(PP_Instance instance) { |
+ return new CDMInstance(instance, this); |
+ } |
+}; |
+ |
+CDMInstance::CDMInstance(PP_Instance instance, |
+ pp::Module* module) |
+ : pp::Instance(instance), |
+ pp::ContentDecryptionModule_Dev(this) { |
+ console_if_ = static_cast<const PPB_Console_Dev*>( |
+ module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)); |
+ assert(console_if_); |
+ browser_cdm_if_ = static_cast<const PPB_ContentDecryptionModule_Dev*>( |
+ module->GetBrowserInterface(PPB_CONTENTDECRYPTIONMODULE_DEV_INTERFACE)); |
+ assert(browser_cdm_if_); |
+ buffer_if_ = static_cast<const PPB_Buffer_Dev*>( |
+ module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE)); |
+ assert(buffer_if_); |
+} |
+ |
+bool CDMInstance::GenerateKeyRequest(PP_Var key_system_var, |
+ PP_Resource init_data) { |
+ using ppapi::StringVar; |
+ |
+ // Extract the key system string from |key_system_var|. |
+ StringVar* key_system_string_var = StringVar::FromPPVar(key_system_var); |
+ base::StringPiece key_system = key_system_string_var->value(); |
+ |
+ if (init_data) { |
+ // TODO(tomfinegan): Is init_data always required, or will it be key_system |
+ // specific? |
+ pp::Buffer_Dev init_buffer(init_data); |
+ if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) || |
+ !init_buffer.data()) { |
+ return false; |
+ } |
+ } |
+ // Do something with the init data buffer from |init_data|. |
+ |
+ return false; |
+} |
+ |
+bool CDMInstance::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 base::StringPiece kDummyDecryptedData = "Pretend I'm decrypted data!"; |
+ pp::Buffer_Dev decrypted_buffer(this, kDummyDecryptedData.size()); |
+ if (!buffer_if_->IsBuffer(decrypted_buffer.pp_resource()) || |
+ !decrypted_buffer.data()) { |
+ return false; |
+ } |
+ |
+ memcpy(decrypted_buffer.data(), kDummyDecryptedData.data(), |
+ kDummyDecryptedData.size()); |
+ |
+ PP_Resource decrypted_resource = decrypted_buffer.detach(); |
+ browser_cdm_if_->DeliverBlock(pp_instance(), decrypted_resource, callback); |
+ return true; |
+} |
+ |
+namespace pp { |
+ |
+// Factory function for your specialization of the Module object. |
+Module* CreateModule() { |
+ return new MyModule(); |
+} |
+ |
+} // namespace pp |