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..dd25e0efd65baa963a1abb2e41697b5f380327c7 |
--- /dev/null |
+++ b/ppapi/examples/content_decryption_module/content_decryption_module.cc |
@@ -0,0 +1,196 @@ |
+// 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 <string> |
+ |
+#include "base/string_piece.h" |
+#include "ppapi/c/pp_var.h" |
+#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_decryption_module_dev.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_decryption_module_dev.h" |
+#include "ppapi/shared_impl/var.h" |
+ |
+namespace { |
+ |
+// Helper function for obtaining a std::string given a PP_Var of type |
+// PP_VARTYPE_STRING. Returns an empty string when things go wrong, or the |
+// contents of the PP_Var (which could also be empty). |
+std::string VarToString(const PPB_Var* var_if, const PP_Var* var) { |
+ std::string var_string; |
+ |
+ if (var_if && var && var->type == PP_VARTYPE_STRING) { |
+ // Extract the key system string from |key_system_arg|. The extracted |
+ // string is NOT NULL TERMINATED. |
+ uint32_t length = 0; |
+ const char* p_var_string = var_if->VarToUtf8(*var, &length); |
+ |
+ if (p_var_string && length) |
+ var_string.assign(p_var_string, length); |
+ } |
+ |
+ return var_string; |
+} |
+ |
+// Creates PP_Var from |str| using |var_if|, AddRef's it, and returns the |
+// PP_Var. Returns empty var when |var_if| is NULL, or when |str| is empty. |
+PP_Var StringToVar(const PPB_Var* var_if, const std::string& str) { |
+ const PP_Var empty_var = { PP_VARTYPE_NULL, 0, {PP_FALSE} }; |
+ if (!var_if || str.empty()) |
+ return empty_var; |
+ |
+ PP_Var var = var_if->VarFromUtf8(str.c_str(), str.length()); |
+ var_if->AddRef(var); |
+ return var; |
+} |
+ |
+} // namespace |
+ |
+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: |
+ PP_Resource StringToBufferResource(const std::string& str); |
+ |
+ // Browser interfaces. |
+ const PPB_Buffer_Dev* buffer_if_; |
+ const PPB_Console_Dev* console_if_; |
+ const PPB_ContentDecryptionModule_Dev* cdm_if_; |
+ const PPB_Var* var_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_); |
+ cdm_if_ = static_cast<const PPB_ContentDecryptionModule_Dev*>( |
+ module->GetBrowserInterface(PPB_CONTENTDECRYPTIONMODULE_DEV_INTERFACE)); |
+ assert(cdm_if_); |
+ buffer_if_ = static_cast<const PPB_Buffer_Dev*>( |
+ module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE)); |
+ assert(buffer_if_); |
+ var_if_ = static_cast<const PPB_Var*>( |
+ module->GetBrowserInterface(PPB_VAR_INTERFACE)); |
+ assert(var_if_); |
+} |
+ |
+bool CDMInstance::GenerateKeyRequest(PP_Var key_system_arg, |
+ PP_Resource init_data) { |
+ std::string key_system = VarToString(var_if_, &key_system_arg); |
+ if (key_system.empty()) |
+ return false; |
+ |
+ if (init_data) { |
+ pp::Buffer_Dev init_buffer(init_data); |
+ if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) || |
+ !init_buffer.data()) { |
+ return false; |
+ } |
+ } |
+ |
+ // TODO(tomfinegan): this is a testing hack, remove. |
+ const std::string message = "key request"; |
+ PP_Resource message_resource = StringToBufferResource(message); |
+ |
+ if (!message_resource) |
+ return false; |
+ |
+ const std::string session_id = "12345"; |
+ PP_Var session_id_var = StringToVar(var_if_, session_id); |
+ |
+ const std::string default_url = "http://www.google.com"; |
+ PP_Var default_url_var = StringToVar(var_if_, default_url); |
+ |
+ cdm_if_->KeyMessage(pp_instance(), key_system_arg, session_id_var, |
+ message_resource, default_url_var); |
+ return true; |
+} |
+ |
+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 std::string kDummyDecryptedData = "Pretend I'm decrypted data!"; |
+ PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData); |
+ if (!decrypted_resource) |
+ return false; |
+ |
+ cdm_if_->DeliverBlock(pp_instance(), decrypted_resource, callback); |
+ return true; |
+} |
+ |
+PP_Resource CDMInstance::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(); |
+} |
+ |
+namespace pp { |
+ |
+// Factory function for your specialization of the Module object. |
+Module* CreateModule() { |
+ return new MyModule(); |
+} |
+ |
+} // namespace pp |