Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Unified Diff: ppapi/examples/content_decryptor/content_decryptor.cc

Issue 10545036: Add PPAPI decryptor interfaces. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: First attempt at CallOnMainThread usage Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/cpp/dev/content_decryptor_dev.cc ('k') | ppapi/ppapi_shared.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..8e4a4c0a0fbd52114efa4769469bed27e977aee4
--- /dev/null
+++ b/ppapi/examples/content_decryptor/content_decryptor.cc
@@ -0,0 +1,235 @@
+// 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/basictypes.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_decryptor_dev.h"
+#include "ppapi/cpp/core.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_decryptor_dev.h"
+#include "ppapi/utility/completion_callback_factory.h"
+
+namespace {
+
+struct DecryptorMessage {
+ PP_Var key_system;
+ PP_Var session_id;
+ PP_Var default_url;
+ PP_Resource message_data;
+ uint16 media_error;
+ uint16 system_error;
+};
+
+// 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) {
+ if (!var_if || str.empty())
+ return PP_MakeNull();
+
+ PP_Var var = var_if->VarFromUtf8(str.c_str(), str.length());
+ var_if->AddRef(var);
+ return var;
+}
+
+} // 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) {
+ 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);
+ void DispatchKeyMessage(int32 result, void* data);
+
+ // Browser interfaces.
+ const PPB_Buffer_Dev* buffer_if_;
+ const PPB_Console_Dev* console_if_;
+ const PPB_ContentDecryptor_Dev* cdm_if_;
+ const PPB_Var* var_if_;
+
+ pp::CompletionCallbackFactory<CDMWrapper> factory_;
+};
+
+// 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) {
+ console_if_ = static_cast<const PPB_Console_Dev*>(
+ module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE));
+ assert(console_if_);
+ cdm_if_ = static_cast<const PPB_ContentDecryptor_Dev*>(
+ module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_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_);
+ factory_.Initialize(this);
+}
+
+bool CDMWrapper::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);
+
+ if (!pp::Module::Get()->core()->IsMainThread()) {
ddorwin 2012/07/18 21:48:36 As this function is written, this statement will n
Tom Finegan 2012/07/23 18:41:53 Done.
+ DecryptorMessage* dm = new (std::nothrow) DecryptorMessage;
ddorwin 2012/07/18 21:48:36 s/dm/message/
ddorwin 2012/07/18 21:48:36 Why nothrow? We're not checking the pointer. I thi
Tom Finegan 2012/07/23 18:41:53 Removed the nothrow.
Tom Finegan 2012/07/23 18:41:53 Renamed to decryptor_message (because I already ha
+ dm->key_system = key_system_arg;
+ dm->session_id = session_id_var;
+ dm->default_url = default_url_var;
+ dm->message_data = message_resource;
+ void* data = reinterpret_cast<void*>(&dm);
+
+ pp::Module::Get()->core()->CallOnMainThread(
+ 0, factory_.NewCallback(&CDMWrapper::DispatchKeyMessage, data), 0);
ddorwin 2012/07/18 21:48:36 data will be leaked if the callback is not called
Tom Finegan 2012/07/23 18:41:53 Ah-- I'll update to pass by value in the next pass
+ } else {
+ cdm_if_->KeyMessage(pp_instance(), key_system_arg, session_id_var,
+ message_resource, default_url_var);
+ }
+
+ 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;
+
+ cdm_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::DispatchKeyMessage(int32 result, void* data) {
+ DecryptorMessage* message = reinterpret_cast<DecryptorMessage*>(data);
ddorwin 2012/07/18 21:48:36 This should be moot based on above, but I would ju
Tom Finegan 2012/07/23 18:41:53 Done.
+ if (message) {
+ cdm_if_->KeyMessage(pp_instance(),
+ message->key_system,
+ message->session_id,
+ message->message_data,
+ message->default_url);
+ delete data;
+ }
+}
+
+namespace pp {
+
+// Factory function for your specialization of the Module object.
+Module* CreateModule() {
+ return new MyModule();
+}
+
+} // namespace pp
« no previous file with comments | « ppapi/cpp/dev/content_decryptor_dev.cc ('k') | ppapi/ppapi_shared.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698