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

Unified Diff: webkit/media/crypto/content_decryptor.cc

Issue 10545036: Add PPAPI decryptor interfaces. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes in response to dmichael's comments/xhwang's testing. 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
Index: webkit/media/crypto/content_decryptor.cc
diff --git a/webkit/media/crypto/content_decryptor.cc b/webkit/media/crypto/content_decryptor.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8747afff1e5fc11cd14dd9220ec1744534bd73f6
--- /dev/null
+++ b/webkit/media/crypto/content_decryptor.cc
@@ -0,0 +1,272 @@
+// 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/string_number_conversions.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/pass_ref.h"
+#include "ppapi/cpp/resource.h"
+#include "ppapi/cpp/var.h"
+#include "ppapi/cpp/var_array_buffer.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 {
+ DecryptorMessage() : media_error(0), system_error(0) {}
+ std::string key_system;
+ std::string session_id;
+ std::string default_url;
+ std::string message_data;
+ uint16 media_error;
+ uint16 system_error;
+};
+
+struct DecryptedBlock {
+ DecryptedBlock() : callback(PP_MakeCompletionCallback(NULL, NULL)) {}
+ PP_CompletionCallback callback;
+ std::string data;
+};
+
+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_Var init_data) OVERRIDE;
+ virtual bool AddKey(PP_Var session_id, PP_Var key) OVERRIDE;
+ virtual bool CancelKeyRequest(PP_Var session_id) OVERRIDE;
+ virtual bool Decrypt(PP_Resource encrypted_block,
+ PP_CompletionCallback callback) OVERRIDE;
+
+ virtual bool DecryptAndDecode(PP_Resource encrypted_block,
+ PP_CompletionCallback callback) OVERRIDE {
+ 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 DecryptedBlock& decrypted_block);
+
+ pp::CompletionCallbackFactory<CDMWrapper> callback_factory_;
+
+ virtual bool IsValidSessionId(std::string session_id) {
+ // TODO(tomfinegan): The CDM MUST handle multiple session IDs. This simple
+ // implementation exists for testing purposes.
+ return (!session_id_.empty() && session_id == session_id_);
+ }
+
+ // TODO(tomfinegan): This needs to be mappable to init data (key IDs in the
+ // WebM case). For WebM it most likely will be 1 key ID and 1 session per
+ // stream. For now we'll just use a random number.
+ uint32 next_session_id_;
+ std::string session_id_;
+
+ // Key ID obtained from init data passed to <code>GenerateKeyRequest</code>.
+ // As-is: WebM video specific; Inadequate for WebM with encrypted audio (A/V
+ // will not have same key ID/key/session ID). Probably inadequate for ISO.
+ std::string key_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),
+ next_session_id_(0) {
+ callback_factory_.Initialize(this);
+}
+
+bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg,
+ PP_Var init_data_arg) {
+
+ pp::Var init_data(pp::PASS_REF, init_data_arg);
+
+ // TODO(tomfinegan): Testing only implementation; init data will not always
+ // be the key ID.
+ pp::Var key_system_var(pp::PASS_REF, key_system_arg);
+
+ // TODO(tomfinegan): confirm support for the key system.
+
+ DecryptorMessage decryptor_message;
+ key_system_ = key_system_var.AsString();
+ decryptor_message.key_system = key_system_;
+ session_id_ = base::UintToString(next_session_id_++);
+ 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_Var key_arg) {
+ pp::Var session_id(pp::PASS_REF, session_id_var);
+ if (!IsValidSessionId(session_id.AsString()))
+ return false;
+
+ pp::VarArrayBuffer key(pp::Var(pp::PASS_REF, key_arg));
+ if (!key.is_array_buffer())
+ return false;
+
+ key_.assign(reinterpret_cast<char*>(key.Map()), key.ByteLength());
+
+ 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_arg) {
+ pp::Var session_id_var(pp::PASS_REF, session_id_arg);
+
+ if (!IsValidSessionId(session_id_var.AsString()))
+ return false;
+
+ // 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() || !callback.func) {
+ return false;
+ }
+
+ DecryptedBlock decrypted_block;
+ decrypted_block.callback = callback;
+ decrypted_block.data = "Pretend I'm decrypted data!";
+ CallOnMain(callback_factory_.NewCallback(&CDMWrapper::DeliverBlock,
+ decrypted_block));
+ 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(decryptor_message.key_system);
+ pp::Var session_id(decryptor_message.session_id);
+ pp::Var init_data(decryptor_message.message_data);
+ pp::ContentDecryptor_Dev::NeedKey(key_system.pp_var(),
+ session_id.pp_var(),
+ init_data.pp_var());
+}
+
+void CDMWrapper::KeyAdded(int32 result,
+ const DecryptorMessage& decryptor_message) {
+ PP_Var key_system = pp::Var(decryptor_message.key_system).pp_var();
+ PP_Var session_id = pp::Var(decryptor_message.session_id).pp_var();
+ pp::ContentDecryptor_Dev::KeyAdded(key_system, session_id);
+}
+
+void CDMWrapper::KeyMessage(int32 result,
+ const DecryptorMessage& decryptor_message) {
+ PP_Var key_system = pp::Var(decryptor_message.key_system).pp_var();
+ PP_Var session_id = pp::Var(decryptor_message.session_id).pp_var();
+ PP_Resource message =
+ StringToBufferResource(decryptor_message.message_data);
+ PP_Var default_url = pp::Var(decryptor_message.default_url).pp_var();
+ pp::ContentDecryptor_Dev::KeyMessage(key_system,
+ session_id,
+ message,
+ default_url);
+}
+
+void CDMWrapper::KeyError(int32 result,
+ const DecryptorMessage& decryptor_message) {
+ PP_Var key_system = pp::Var(decryptor_message.key_system).pp_var();
+ PP_Var session_id = pp::Var(decryptor_message.session_id).pp_var();
+ pp::ContentDecryptor_Dev::KeyError(key_system,
+ session_id,
+ decryptor_message.media_error,
+ decryptor_message.system_error);
+}
+
+void CDMWrapper::DeliverBlock(int32 result,
+ const DecryptedBlock& decrypted_block) {
+ PP_Resource decrypted_resource = StringToBufferResource(
+ decrypted_block.data);
+ if (decrypted_resource) {
+ pp::ContentDecryptor_Dev::DeliverBlock(decrypted_resource,
+ decrypted_block.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

Powered by Google App Engine
This is Rietveld 408576698