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

Side by Side Diff: ppapi/examples/content_decryption_module/content_decryption_module.cc

Issue 10545036: Add PPAPI decryptor interfaces. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Implement enough thunk stuff to call into PluginInstance CDM stubs 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_decryption_module_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_decryption_module_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 const PP_Var empty_var = { PP_VARTYPE_NULL, 0, {PP_FALSE} };
46 if (!var_if || str.empty())
47 return empty_var;
48
49 PP_Var var = var_if->VarFromUtf8(str.c_str(), str.length());
50 var_if->AddRef(var);
51 return var;
52 }
53
54 } // namespace
55
56 class CDMInstance : public pp::Instance,
57 public pp::ContentDecryptionModule_Dev {
58 public:
59 explicit CDMInstance(PP_Instance instance, pp::Module* module);
60 virtual ~CDMInstance() {}
61
62 // PPP_ContentDecryptionModule_Dev methods
63 virtual bool GenerateKeyRequest(PP_Var key_system,
64 PP_Resource init_data);
65
66 virtual bool AddKey(PP_Var session_id,
67 PP_Resource key) {
68 return false;
69 }
70
71 virtual bool CancelKeyRequest(PP_Var session_id) {
72 return false;
73 }
74
75 virtual bool Decrypt(PP_Resource encrypted_block,
76 PP_CompletionCallback callback);
77
78 virtual bool DecryptAndDecode(PP_Resource encrypted_block,
79 PP_CompletionCallback callback) {
80 return false;
81 }
82
83 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
84 return true;
85 }
86
87 private:
88 PP_Resource StringToBufferResource(const std::string& str);
89
90 // Browser interfaces.
91 const PPB_Buffer_Dev* buffer_if_;
92 const PPB_Console_Dev* console_if_;
93 const PPB_ContentDecryptionModule_Dev* cdm_if_;
94 const PPB_Var* var_if_;
95 };
96
97 // This object is the global object representing this plugin library as long
98 // as it is loaded.
99 class MyModule : public pp::Module {
100 public:
101 MyModule() : pp::Module() {}
102 virtual ~MyModule() {}
103
104 // Override CreateInstance to create your customized Instance object.
105 virtual pp::Instance* CreateInstance(PP_Instance instance) {
106 return new CDMInstance(instance, this);
107 }
108 };
109
110 CDMInstance::CDMInstance(PP_Instance instance,
111 pp::Module* module)
112 : pp::Instance(instance),
113 pp::ContentDecryptionModule_Dev(this) {
114 console_if_ = static_cast<const PPB_Console_Dev*>(
115 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE));
116 assert(console_if_);
117 cdm_if_ = static_cast<const PPB_ContentDecryptionModule_Dev*>(
118 module->GetBrowserInterface(PPB_CONTENTDECRYPTIONMODULE_DEV_INTERFACE));
119 assert(cdm_if_);
120 buffer_if_ = static_cast<const PPB_Buffer_Dev*>(
121 module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE));
122 assert(buffer_if_);
123 var_if_ = static_cast<const PPB_Var*>(
124 module->GetBrowserInterface(PPB_VAR_INTERFACE));
125 assert(var_if_);
126 }
127
128 bool CDMInstance::GenerateKeyRequest(PP_Var key_system_arg,
129 PP_Resource init_data) {
130 std::string key_system = VarToString(var_if_, &key_system_arg);
131 if (key_system.empty())
132 return false;
133
134 if (init_data) {
135 pp::Buffer_Dev init_buffer(init_data);
136 if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) ||
137 !init_buffer.data()) {
138 return false;
139 }
140 }
141
142 // TODO(tomfinegan): this is a testing hack, remove.
143 const std::string message = "key request";
144 PP_Resource message_resource = StringToBufferResource(message);
145
146 if (!message_resource)
147 return false;
148
149 const std::string session_id = "12345";
150 PP_Var session_id_var = StringToVar(var_if_, session_id);
151
152 const std::string default_url = "http://www.google.com";
153 PP_Var default_url_var = StringToVar(var_if_, default_url);
154
155 cdm_if_->KeyMessage(pp_instance(), key_system_arg, session_id_var,
156 message_resource, default_url_var);
157 return true;
158 }
159
160 bool CDMInstance::Decrypt(PP_Resource encrypted_block,
161 PP_CompletionCallback callback) {
162 pp::Buffer_Dev block_buffer(encrypted_block);
163 if (!buffer_if_->IsBuffer(block_buffer.pp_resource()) ||
164 !block_buffer.data()) {
165 return false;
166 }
167
168 const std::string kDummyDecryptedData = "Pretend I'm decrypted data!";
169 PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData);
170 if (!decrypted_resource)
171 return false;
172
173 cdm_if_->DeliverBlock(pp_instance(), decrypted_resource, callback);
174 return true;
175 }
176
177 PP_Resource CDMInstance::StringToBufferResource(const std::string& str) {
178 if (!str.size())
179 return 0;
180
181 pp::Buffer_Dev buffer(this, str.size());
182 if (!buffer_if_->IsBuffer(buffer.pp_resource()) || !buffer.data())
183 return 0;
184
185 memcpy(buffer.data(), str.data(), str.size());
186 return buffer.detach();
187 }
188
189 namespace pp {
190
191 // Factory function for your specialization of the Module object.
192 Module* CreateModule() {
193 return new MyModule();
194 }
195
196 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698