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

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: CPP interface migration complete 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..e44d9cc9e106759a611b4eed071ab2aa4883df59
--- /dev/null
+++ b/ppapi/examples/content_decryptor/content_decryptor.cc
@@ -0,0 +1,255 @@
+// 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/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 {
+ DecryptorMessage()
+ : media_error(0),
+ system_error(0),
+ callback(PP_MakeCompletionCallback(NULL, NULL)) {}
ddorwin 2012/07/26 22:20:17 Prefer (but I don't believe required) init list or
Tom Finegan 2012/07/27 08:55:01 Removed per later comment. Created DecryptedData s
+
+ PP_CompletionCallback callback;
+ 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:
+ 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);
+ 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>callback_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);
+
+ pp::CompletionCallbackFactory<CDMWrapper> callback_factory_;
+
+ // TODO(tomfinegan): Should this be a list of session IDs? Needs removal after
+ // testing (I think!).
+ std::string session_id_;
+
+ // TODO(tomfinegan): Should these be multimaps of session_id:key_system and
+ // 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) {
+ callback_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 (!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;
+ 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(callback_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 (!key_buffer.data())
+ return false;
+
+ key_.assign(reinterpret_cast<char*>(key_buffer.data()), key_buffer.size());
+
+ DecryptorMessage decryptor_message;
+ decryptor_message.key_system = key_system_;
+ decryptor_message.session_id = session_id;
+ CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyAdded,
+ decryptor_message));
+ return true;
+}
+
+bool CDMWrapper::CancelKeyRequest(PP_Var session_id_var) {
+ std::string session_id = StringVar::FromPPVar(session_id_var)->value();
+
+ // TODO(tomfinegan): cancel pending key request in CDM.
+
+ return true;
+}
+
+bool CDMWrapper::Decrypt(PP_Resource encrypted_block,
+ PP_CompletionCallback callback) {
+ pp::Buffer_Dev block_buffer(encrypted_block);
+ if (!block_buffer.data()) {
+ return false;
+ }
+
ddorwin 2012/07/26 22:20:17 If invalid callback, return false. No sense in dec
Tom Finegan 2012/07/27 08:55:01 Done.
+ DecryptorMessage decryptor_message;
+ decryptor_message.message_data = "Pretend I'm decrypted data!";
ddorwin 2012/07/26 22:20:17 Need to set the callback.
Tom Finegan 2012/07/27 08:55:01 Done.
+ CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock,
ddorwin 2012/07/26 22:20:17 This should not be using DecryptorMessage. This is
Tom Finegan 2012/07/27 08:55:01 As above-- DecryptedData.
+ decryptor_message));
+ return true;
+}
+
+PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) {
+ if (str.empty())
+ return 0;
+
+ pp::Buffer_Dev buffer(this, str.size());
+ if (!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);
+ pp::ContentDecryptor_Dev::NeedKey(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);
+ pp::ContentDecryptor_Dev::KeyAdded(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);
+ pp::ContentDecryptor_Dev::KeyMessage(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);
+ pp::ContentDecryptor_Dev::KeyError(key_system,
+ session_id,
+ decryptor_message.media_error,
+ decryptor_message.system_error);
+}
+
+void CDMWrapper::DeliverBlock(int32 result,
+ const DecryptorMessage& decryptor_message) {
ddorwin 2012/07/26 22:20:17 Wrong type as mentioned above.
Tom Finegan 2012/07/27 08:55:01 Done.
+ if (decryptor_message.callback.func) {
ddorwin 2012/07/26 22:20:17 Should this ever be false?
Tom Finegan 2012/07/27 08:55:01 Nope.
+ PP_Resource decrypted_resource = StringToBufferResource(
+ decryptor_message.message_data);
+ if (decrypted_resource) {
+ pp::ContentDecryptor_Dev::DeliverBlock(decrypted_resource,
+ decryptor_message.callback);
+ }
+ }
+}
+
+// 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
« 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