diff options
Diffstat (limited to 'webkit')
| -rw-r--r-- | webkit/media/crypto/ppapi/DEPS | 3 | ||||
| -rw-r--r-- | webkit/media/crypto/ppapi/cdm_wrapper.cc | 228 | ||||
| -rw-r--r-- | webkit/media/webkit_media.gypi | 34 | ||||
| -rw-r--r-- | webkit/plugins/ppapi/ppapi_plugin_instance.cc | 154 | ||||
| -rw-r--r-- | webkit/plugins/ppapi/ppapi_plugin_instance.h | 46 |
5 files changed, 465 insertions, 0 deletions
diff --git a/webkit/media/crypto/ppapi/DEPS b/webkit/media/crypto/ppapi/DEPS new file mode 100644 index 0000000..bfb45fe --- /dev/null +++ b/webkit/media/crypto/ppapi/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+ppapi", +] diff --git a/webkit/media/crypto/ppapi/cdm_wrapper.cc b/webkit/media/crypto/ppapi/cdm_wrapper.cc new file mode 100644 index 0000000..8b7edf3 --- /dev/null +++ b/webkit/media/crypto/ppapi/cdm_wrapper.cc @@ -0,0 +1,228 @@ +// 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 <cstring> // For std::memcpy. + +#include "base/compiler_specific.h" // For OVERRIDE. +#include "ppapi/c/pp_errors.h" +#include "ppapi/c/pp_stdint.h" +#include "ppapi/cpp/completion_callback.h" +#include "ppapi/cpp/core.h" +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/logging.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/private/content_decryptor_private.h" +#include "ppapi/utility/completion_callback_factory.h" + +namespace { + +struct DecryptorMessage { + DecryptorMessage() : media_error(0), system_code(0) {} + std::string key_system; + std::string session_id; + std::string default_url; + std::string message_data; + int32_t media_error; + int32_t system_code; +}; + +struct DecryptedBlock { + DecryptedBlock() : request_id(0) {} + int32_t request_id; + std::string data; +}; + +void CallOnMain(pp::CompletionCallback cb) { + pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK); +} + +} // namespace + + +// A wrapper class for abstracting away PPAPI interaction and threading for a +// Content Decryption Module (CDM). +class CDMWrapper : public pp::Instance, + public pp::ContentDecryptor_Private { + public: + CDMWrapper(PP_Instance instance, pp::Module* module); + virtual ~CDMWrapper() {} + + // PPP_ContentDecryptor_Private methods + virtual bool GenerateKeyRequest(const std::string& key_system, + pp::VarArrayBuffer init_data) OVERRIDE; + virtual bool AddKey(const std::string& session_id, + pp::VarArrayBuffer key) OVERRIDE; + virtual bool CancelKeyRequest(const std::string& session_id) OVERRIDE; + virtual bool Decrypt(pp::Buffer_Dev encrypted_buffer, + int32_t request_id) OVERRIDE; + + virtual bool DecryptAndDecode(pp::Buffer_Dev encrypted_buffer, + int32_t request_id) 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_Private</code> dispatchers. These are passed to + // <code>callback_factory_</code> to ensure that calls into + // <code>PPP_ContentDecryptor_Private</code> are asynchronous. + void NeedKey(int32_t result, const DecryptorMessage& decryptor_message); + void KeyAdded(int32_t result, const DecryptorMessage& decryptor_message); + void KeyMessage(int32_t result, const DecryptorMessage& decryptor_message); + void KeyError(int32_t result, const DecryptorMessage& decryptor_message); + void DeliverBlock(int32_t result, const DecryptedBlock& decrypted_block); + + pp::CompletionCallbackFactory<CDMWrapper> callback_factory_; +}; + +CDMWrapper::CDMWrapper(PP_Instance instance, + pp::Module* module) + : pp::Instance(instance), + pp::ContentDecryptor_Private(this) { + callback_factory_.Initialize(this); +} + +bool CDMWrapper::GenerateKeyRequest(const std::string& key_system, + pp::VarArrayBuffer init_data) { + PP_DCHECK(!key_system.empty() && init_data.ByteLength()); + + DecryptorMessage decryptor_message; + decryptor_message.key_system = key_system; + decryptor_message.session_id = "0"; + decryptor_message.default_url = "http://www.google.com"; + decryptor_message.message_data = "GenerateKeyRequest"; + + CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, + decryptor_message)); + return true; +} + +bool CDMWrapper::AddKey(const std::string& session_id, + pp::VarArrayBuffer key) { + const std::string key_string(reinterpret_cast<char*>(key.Map()), + key.ByteLength()); + + PP_DCHECK(!session_id.empty() && !key_string.empty()); + + DecryptorMessage decryptor_message; + decryptor_message.key_system = "AddKey"; + decryptor_message.session_id = "0"; + decryptor_message.default_url = "http://www.google.com"; + decryptor_message.message_data = "AddKey"; + CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyAdded, + decryptor_message)); + return true; +} + +bool CDMWrapper::CancelKeyRequest(const std::string& session_id) { + // TODO(tomfinegan): cancel pending key request in CDM. + + PP_DCHECK(!session_id.empty()); + + DecryptorMessage decryptor_message; + decryptor_message.key_system = "CancelKeyRequest"; + decryptor_message.session_id = "0"; + decryptor_message.default_url = "http://www.google.com"; + decryptor_message.message_data = "CancelKeyRequest"; + CallOnMain(callback_factory_.NewCallback(&CDMWrapper::KeyMessage, + decryptor_message)); + return true; +} + +bool CDMWrapper::Decrypt(pp::Buffer_Dev encrypted_buffer, + int32_t request_id) { + PP_DCHECK(!encrypted_buffer.is_null()); + + DecryptedBlock decrypted_block; + decrypted_block.request_id = request_id; + 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; + + std::memcpy(buffer.data(), str.data(), str.size()); + return buffer.detach(); +} + +void CDMWrapper::NeedKey(int32_t result, + const DecryptorMessage& decryptor_message) { + const std::string& message_data = decryptor_message.message_data; + pp::VarArrayBuffer init_data(message_data.size()); + std::memcpy(init_data.Map(), message_data.data(), message_data.size()); + pp::ContentDecryptor_Private::NeedKey(decryptor_message.key_system, + decryptor_message.session_id, + init_data); +} + +void CDMWrapper::KeyAdded(int32_t result, + const DecryptorMessage& decryptor_message) { + pp::ContentDecryptor_Private::KeyAdded(decryptor_message.key_system, + decryptor_message.session_id); +} + +void CDMWrapper::KeyMessage(int32_t result, + const DecryptorMessage& decryptor_message) { + pp::Buffer_Dev message_buffer( + StringToBufferResource(decryptor_message.message_data)); + pp::ContentDecryptor_Private::KeyMessage(decryptor_message.key_system, + decryptor_message.session_id, + message_buffer, + decryptor_message.default_url); +} + +void CDMWrapper::KeyError(int32_t result, + const DecryptorMessage& decryptor_message) { + pp::ContentDecryptor_Private::KeyError(decryptor_message.key_system, + decryptor_message.session_id, + decryptor_message.media_error, + decryptor_message.system_code); +} + +void CDMWrapper::DeliverBlock(int32_t result, + const DecryptedBlock& decrypted_block) { + pp::Buffer_Dev decrypted_buffer( + StringToBufferResource(decrypted_block.data)); + pp::ContentDecryptor_Private::DeliverBlock(decrypted_buffer, + decrypted_block.request_id); +} + +// 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 diff --git a/webkit/media/webkit_media.gypi b/webkit/media/webkit_media.gypi index d152991..834ba48 100644 --- a/webkit/media/webkit_media.gypi +++ b/webkit/media/webkit_media.gypi @@ -83,5 +83,39 @@ }], ], }, + { + 'target_name': 'ppapi_cdm_wrapper', + 'type': 'none', + 'dependencies': [ + '<(DEPTH)/ppapi/ppapi.gyp:ppapi_cpp' + ], + 'conditions': [ + ['os_posix==1 and OS!="mac"', { + 'cflags': ['-fvisibility=hidden'], + 'type': 'shared_library', + # -gstabs, used in the official builds, causes an ICE. Simply remove + # it. + 'cflags!': ['-gstabs'], + }], + ['OS=="win"', { + 'type': 'shared_library', + }], + ['OS=="mac"', { + 'type': 'loadable_module', + 'mac_bundle': 1, + 'product_extension': 'plugin', + 'xcode_settings': { + 'OTHER_LDFLAGS': [ + # Not to strip important symbols by -Wl,-dead_strip. + '-Wl,-exported_symbol,_PPP_GetInterface', + '-Wl,-exported_symbol,_PPP_InitializeModule', + '-Wl,-exported_symbol,_PPP_ShutdownModule' + ]}, + }], + ], + 'sources': [ + 'crypto/ppapi/cdm_wrapper.cc', + ], + } ], } diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.cc b/webkit/plugins/ppapi/ppapi_plugin_instance.cc index 96694f6..0af8dc1 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.cc +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.cc @@ -114,6 +114,7 @@ using ppapi::thunk::PPB_Graphics2D_API; using ppapi::thunk::PPB_Graphics3D_API; using ppapi::thunk::PPB_ImageData_API; using ppapi::Var; +using ppapi::ArrayBufferVar; using ppapi::ViewData; using WebKit::WebBindings; using WebKit::WebCanvas; @@ -294,6 +295,29 @@ scoped_array<const char*> StringVectorToArgArray( return array.Pass(); } +// Creates a PP_Resource containing a PPB_Buffer_Impl, copies |data| into the +// buffer resource, and returns it. Returns a an invalid PP_Resource with an ID +// of 0 on failure. Upon success, the returned Buffer resource has a reference +// count of 1. +PP_Resource MakeBufferResource(PP_Instance instance, + const base::StringPiece& data) { + if (data.empty()) + return 0; + + ScopedPPResource resource(PPB_Buffer_Impl::Create(instance, data.size())); + if (!resource.get()) + return 0; + + EnterResourceNoLock<PPB_Buffer_API> enter(resource, true); + if (enter.failed()) + return 0; + + BufferAutoMapper mapper(enter.object()); + memcpy(mapper.data(), data.data(), data.size()); + + return resource.get(); +} + } // namespace // static @@ -323,6 +347,7 @@ PluginInstance::PluginInstance( has_webkit_focus_(false), has_content_area_focus_(false), find_identifier_(-1), + plugin_decryption_interface_(NULL), plugin_find_interface_(NULL), plugin_input_event_interface_(NULL), plugin_messaging_interface_(NULL), @@ -965,6 +990,16 @@ void PluginInstance::StopFind() { plugin_find_interface_->StopFind(pp_instance()); } +bool PluginInstance::LoadContentDecryptorInterface() { + if (!plugin_decryption_interface_) { + plugin_decryption_interface_ = + static_cast<const PPP_ContentDecryptor_Private*>( + module_->GetPluginInterface( + PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE)); + } + return !!plugin_decryption_interface_; +} + bool PluginInstance::LoadFindInterface() { if (!plugin_find_interface_) { plugin_find_interface_ = @@ -1282,6 +1317,75 @@ void PluginInstance::RotateView(WebPlugin::RotationType type) { // NOTE: plugin instance may have been deleted. } +bool PluginInstance::GenerateKeyRequest(const std::string& key_system, + const std::string& init_data) { + if (!LoadContentDecryptorInterface()) + return false; + if (key_system.empty()) + return false; + PP_Var init_data_array = + PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( + init_data.size(), init_data.data()); + + return PP_ToBool(plugin_decryption_interface_->GenerateKeyRequest( + pp_instance(), + StringVar::StringToPPVar(key_system), + init_data_array)); +} + +bool PluginInstance::AddKey(const std::string& session_id, + const std::string& key) { + if (!LoadContentDecryptorInterface()) + return false; + PP_Var key_array = + PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(key.size(), + key.data()); + + return PP_ToBool(plugin_decryption_interface_->AddKey( + pp_instance(), + StringVar::StringToPPVar(session_id), + key_array)); +} + +bool PluginInstance::CancelKeyRequest(const std::string& session_id) { + if (!LoadContentDecryptorInterface()) + return false; + + return PP_ToBool(plugin_decryption_interface_->CancelKeyRequest( + pp_instance(), + StringVar::StringToPPVar(session_id))); +} + +bool PluginInstance::Decrypt(const base::StringPiece& encrypted_block, + const DecryptedDataCB& callback) { + if (!LoadContentDecryptorInterface()) + return false; + ScopedPPResource encrypted_resource(MakeBufferResource(pp_instance(), + encrypted_block)); + if (!encrypted_resource.get()) + return false; + + // TODO(tomfinegan): Store callback and ID in a map, and pass ID to decryptor. + return PP_ToBool(plugin_decryption_interface_->Decrypt(pp_instance(), + encrypted_resource, + 0)); +} + +bool PluginInstance::DecryptAndDecode(const base::StringPiece& encrypted_block, + const DecryptedDataCB& callback) { + if (!LoadContentDecryptorInterface()) + return false; + ScopedPPResource encrypted_resource(MakeBufferResource(pp_instance(), + encrypted_block)); + if (!encrypted_resource.get()) + return false; + // TODO(tomfinegan): Store callback and ID in a map, and pass ID to decryptor. + return PP_ToBool(plugin_decryption_interface_->DecryptAndDecode( + pp_instance(), + encrypted_resource, + 0)); +} + bool PluginInstance::FlashIsFullscreenOrPending() { return fullscreen_container_ != NULL; } @@ -1890,6 +1994,56 @@ PP_Var PluginInstance::GetFontFamilies(PP_Instance instance) { return PP_MakeUndefined(); } +void PluginInstance::NeedKey(PP_Instance instance, + PP_Var key_system_var, + PP_Var session_id_var, + PP_Var init_data_var) { + // TODO(tomfinegan): send the data to media stack. +} + +void PluginInstance::KeyAdded(PP_Instance instance, + PP_Var key_system_var, + PP_Var session_id_var) { + // TODO(tomfinegan): send the data to media stack. +} + +void PluginInstance::KeyMessage(PP_Instance instance, + PP_Var key_system_var, + PP_Var session_id_var, + PP_Resource message_resource, + PP_Var default_url_var) { + // TODO(tomfinegan): send the data to media stack. +} + +void PluginInstance::KeyError(PP_Instance instance, + PP_Var key_system_var, + PP_Var session_id_var, + int32_t media_error, + int32_t system_code) { + // TODO(tomfinegan): send the data to media stack. +} + +void PluginInstance::DeliverBlock(PP_Instance instance, + PP_Resource decrypted_block, + int32_t request_id) { + // TODO(xhwang): Pass the decrypted block back to media stack. +} + +void PluginInstance::DeliverFrame(PP_Instance instance, + PP_Resource decrypted_frame, + int32_t request_id) { + // TODO(tomfinegan): To be implemented after completion of v0.1 of the + // EME/CDM work. +} + +void PluginInstance::DeliverSamples(PP_Instance instance, + PP_Resource decrypted_samples, + int32_t request_id) { + // TODO(tomfinegan): To be implemented after completion of v0.1 of the + // EME/CDM work. +} + + void PluginInstance::NumberOfFindResultsChanged(PP_Instance instance, int32_t total, PP_Bool final_result) { diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.h b/webkit/plugins/ppapi/ppapi_plugin_instance.h index 24eb715..1d00b3c 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.h +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.h @@ -10,6 +10,7 @@ #include <vector> #include "base/basictypes.h" +#include "base/callback.h" #include "base/compiler_specific.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" @@ -34,6 +35,8 @@ #include "ppapi/c/ppp_input_event.h" #include "ppapi/c/ppp_messaging.h" #include "ppapi/c/ppp_mouse_lock.h" +#include "ppapi/c/private/ppb_content_decryptor_private.h" +#include "ppapi/c/private/ppp_content_decryptor_private.h" #include "ppapi/c/private/ppp_instance_private.h" #include "ppapi/shared_impl/ppb_instance_shared.h" #include "ppapi/shared_impl/ppb_view_shared.h" @@ -236,6 +239,19 @@ class WEBKIT_PLUGINS_EXPORT PluginInstance : void Graphics3DContextLost(); + // Provides access to PPP_ContentDecryptor_Private. + // TODO(tomfinegan): Move decryptor methods to delegate class. + typedef base::Callback<void(void*, int)> DecryptedDataCB; + bool GenerateKeyRequest(const std::string& key_system, + const std::string& init_data); + bool AddKey(const std::string& session_id, + const std::string& key); + bool CancelKeyRequest(const std::string& session_id); + bool Decrypt(const base::StringPiece& encypted_block, + const DecryptedDataCB& callback); + bool DecryptAndDecode(const base::StringPiece& encypted_block, + const DecryptedDataCB& callback); + // There are 2 implementations of the fullscreen interface // PPB_FlashFullscreen is used by Pepper Flash. // PPB_Fullscreen is intended for other applications including NaCl. @@ -409,6 +425,34 @@ class WEBKIT_PLUGINS_EXPORT PluginInstance : PP_Instance instance, PP_URLComponents_Dev* components) OVERRIDE; + // TODO(tomfinegan): Move the next 7 methods to a delegate class. + virtual void NeedKey(PP_Instance instance, + PP_Var key_system, + PP_Var session_id, + PP_Var init_data) OVERRIDE; + virtual void KeyAdded(PP_Instance instance, + PP_Var key_system, + PP_Var session_id) OVERRIDE; + virtual void KeyMessage(PP_Instance instance, + PP_Var key_system, + PP_Var session_id, + PP_Resource message, + PP_Var default_url) OVERRIDE; + virtual void KeyError(PP_Instance instance, + PP_Var key_system, + PP_Var session_id, + int32_t media_error, + int32_t system_code) OVERRIDE; + virtual void DeliverBlock(PP_Instance instance, + PP_Resource decrypted_block, + int32_t request_id) OVERRIDE; + virtual void DeliverFrame(PP_Instance instance, + PP_Resource decrypted_frame, + int32_t request_id) OVERRIDE; + virtual void DeliverSamples(PP_Instance instance, + PP_Resource decrypted_samples, + int32_t request_id) OVERRIDE; + // Reset this instance as proxied. Resets cached interfaces to point to the // proxy and re-sends DidCreate, DidChangeView, and HandleDocumentLoad (if // necessary). @@ -424,6 +468,7 @@ class WEBKIT_PLUGINS_EXPORT PluginInstance : PluginModule* module, ::ppapi::PPP_Instance_Combined* instance_interface); + bool LoadContentDecryptorInterface(); bool LoadFindInterface(); bool LoadInputEventInterface(); bool LoadMessagingInterface(); @@ -555,6 +600,7 @@ class WEBKIT_PLUGINS_EXPORT PluginInstance : scoped_ptr< ::ppapi::thunk::ResourceCreationAPI> resource_creation_; // The plugin-provided interfaces. + const PPP_ContentDecryptor_Private* plugin_decryption_interface_; const PPP_Find_Dev* plugin_find_interface_; const PPP_InputEvent* plugin_input_event_interface_; const PPP_Messaging* plugin_messaging_interface_; |
