OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/string_piece.h" |
| 8 #include "ppapi/c/pp_var.h" |
| 9 #include "ppapi/c/ppb_var.h" |
| 10 #include "ppapi/c/dev/ppb_buffer_dev.h" |
| 11 #include "ppapi/c/dev/ppb_console_dev.h" |
| 12 #include "ppapi/c/dev/ppb_content_decryptor_dev.h" |
| 13 #include "ppapi/cpp/instance.h" |
| 14 #include "ppapi/cpp/module.h" |
| 15 #include "ppapi/cpp/resource.h" |
| 16 #include "ppapi/cpp/var.h" |
| 17 #include "ppapi/cpp/dev/buffer_dev.h" |
| 18 #include "ppapi/cpp/dev/content_decryptor_dev.h" |
| 19 #include "ppapi/shared_impl/var.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Helper function for obtaining a std::string given a PP_Var of type |
| 24 // PP_VARTYPE_STRING. Returns an empty string when things go wrong, or the |
| 25 // contents of the PP_Var (which could also be empty). |
| 26 std::string VarToString(const PPB_Var* var_if, const PP_Var* var) { |
| 27 std::string var_string; |
| 28 |
| 29 if (var_if && var && var->type == PP_VARTYPE_STRING) { |
| 30 // Extract the key system string from |key_system_arg|. The extracted |
| 31 // string is NOT NULL TERMINATED. |
| 32 uint32_t length = 0; |
| 33 const char* p_var_string = var_if->VarToUtf8(*var, &length); |
| 34 |
| 35 if (p_var_string && length) |
| 36 var_string.assign(p_var_string, length); |
| 37 } |
| 38 |
| 39 return var_string; |
| 40 } |
| 41 |
| 42 // Creates PP_Var from |str| using |var_if|, AddRef's it, and returns the |
| 43 // PP_Var. Returns empty var when |var_if| is NULL, or when |str| is empty. |
| 44 PP_Var StringToVar(const PPB_Var* var_if, const std::string& str) { |
| 45 if (!var_if || str.empty()) |
| 46 return PP_MakeNull(); |
| 47 |
| 48 PP_Var var = var_if->VarFromUtf8(str.c_str(), str.length()); |
| 49 var_if->AddRef(var); |
| 50 return var; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 |
| 56 // A wrapper class responsible for managing interaction between the browser and |
| 57 // a Content Decryption Module (CDM). |
| 58 class CDMWrapper : public pp::Instance, |
| 59 public pp::ContentDecryptor_Dev { |
| 60 public: |
| 61 explicit CDMWrapper(PP_Instance instance, pp::Module* module); |
| 62 virtual ~CDMWrapper() {} |
| 63 |
| 64 // PPP_ContentDecryptionModule_Dev methods |
| 65 virtual bool GenerateKeyRequest(PP_Var key_system, |
| 66 PP_Resource init_data); |
| 67 |
| 68 virtual bool AddKey(PP_Var session_id, |
| 69 PP_Resource key) { |
| 70 return false; |
| 71 } |
| 72 |
| 73 virtual bool CancelKeyRequest(PP_Var session_id) { |
| 74 return false; |
| 75 } |
| 76 |
| 77 virtual bool Decrypt(PP_Resource encrypted_block, |
| 78 PP_CompletionCallback callback); |
| 79 |
| 80 virtual bool DecryptAndDecode(PP_Resource encrypted_block, |
| 81 PP_CompletionCallback callback) { |
| 82 return false; |
| 83 } |
| 84 |
| 85 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 86 return true; |
| 87 } |
| 88 |
| 89 private: |
| 90 PP_Resource StringToBufferResource(const std::string& str); |
| 91 |
| 92 // Browser interfaces. |
| 93 const PPB_Buffer_Dev* buffer_if_; |
| 94 const PPB_Console_Dev* console_if_; |
| 95 const PPB_ContentDecryptor_Dev* cdm_if_; |
| 96 const PPB_Var* var_if_; |
| 97 }; |
| 98 |
| 99 // This object is the global object representing this plugin library as long |
| 100 // as it is loaded. |
| 101 class MyModule : public pp::Module { |
| 102 public: |
| 103 MyModule() : pp::Module() {} |
| 104 virtual ~MyModule() {} |
| 105 |
| 106 // Override CreateInstance to create your customized Instance object. |
| 107 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 108 return new CDMWrapper(instance, this); |
| 109 } |
| 110 }; |
| 111 |
| 112 CDMWrapper::CDMWrapper(PP_Instance instance, |
| 113 pp::Module* module) |
| 114 : pp::Instance(instance), |
| 115 pp::ContentDecryptor_Dev(this) { |
| 116 console_if_ = static_cast<const PPB_Console_Dev*>( |
| 117 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE)); |
| 118 assert(console_if_); |
| 119 cdm_if_ = static_cast<const PPB_ContentDecryptor_Dev*>( |
| 120 module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_DEV_INTERFACE)); |
| 121 assert(cdm_if_); |
| 122 buffer_if_ = static_cast<const PPB_Buffer_Dev*>( |
| 123 module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE)); |
| 124 assert(buffer_if_); |
| 125 var_if_ = static_cast<const PPB_Var*>( |
| 126 module->GetBrowserInterface(PPB_VAR_INTERFACE)); |
| 127 assert(var_if_); |
| 128 } |
| 129 |
| 130 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg, |
| 131 PP_Resource init_data) { |
| 132 std::string key_system = VarToString(var_if_, &key_system_arg); |
| 133 if (key_system.empty()) |
| 134 return false; |
| 135 |
| 136 if (init_data) { |
| 137 pp::Buffer_Dev init_buffer(init_data); |
| 138 if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) || |
| 139 !init_buffer.data()) { |
| 140 return false; |
| 141 } |
| 142 } |
| 143 |
| 144 // TODO(tomfinegan): this is a testing hack, remove. |
| 145 const std::string message = "key request"; |
| 146 PP_Resource message_resource = StringToBufferResource(message); |
| 147 |
| 148 if (!message_resource) |
| 149 return false; |
| 150 |
| 151 const std::string session_id = "12345"; |
| 152 PP_Var session_id_var = StringToVar(var_if_, session_id); |
| 153 |
| 154 const std::string default_url = "http://www.google.com"; |
| 155 PP_Var default_url_var = StringToVar(var_if_, default_url); |
| 156 |
| 157 cdm_if_->KeyMessage(pp_instance(), key_system_arg, session_id_var, |
| 158 message_resource, default_url_var); |
| 159 return true; |
| 160 } |
| 161 |
| 162 bool CDMWrapper::Decrypt(PP_Resource encrypted_block, |
| 163 PP_CompletionCallback callback) { |
| 164 pp::Buffer_Dev block_buffer(encrypted_block); |
| 165 if (!buffer_if_->IsBuffer(block_buffer.pp_resource()) || |
| 166 !block_buffer.data()) { |
| 167 return false; |
| 168 } |
| 169 |
| 170 const std::string kDummyDecryptedData = "Pretend I'm decrypted data!"; |
| 171 PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData); |
| 172 if (!decrypted_resource) |
| 173 return false; |
| 174 |
| 175 cdm_if_->DeliverBlock(pp_instance(), decrypted_resource, callback); |
| 176 return true; |
| 177 } |
| 178 |
| 179 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) { |
| 180 if (!str.size()) |
| 181 return 0; |
| 182 |
| 183 pp::Buffer_Dev buffer(this, str.size()); |
| 184 if (!buffer_if_->IsBuffer(buffer.pp_resource()) || !buffer.data()) |
| 185 return 0; |
| 186 |
| 187 memcpy(buffer.data(), str.data(), str.size()); |
| 188 return buffer.detach(); |
| 189 } |
| 190 |
| 191 namespace pp { |
| 192 |
| 193 // Factory function for your specialization of the Module object. |
| 194 Module* CreateModule() { |
| 195 return new MyModule(); |
| 196 } |
| 197 |
| 198 } // namespace pp |
OLD | NEW |