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

Side by Side 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: First attempt at CallOnMainThread usage 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
« no previous file with comments | « ppapi/cpp/dev/content_decryptor_dev.cc ('k') | ppapi/ppapi_shared.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/basictypes.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/core.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/resource.h"
17 #include "ppapi/cpp/dev/buffer_dev.h"
18 #include "ppapi/cpp/dev/content_decryptor_dev.h"
19 #include "ppapi/utility/completion_callback_factory.h"
20
21 namespace {
22
23 struct DecryptorMessage {
24 PP_Var key_system;
25 PP_Var session_id;
26 PP_Var default_url;
27 PP_Resource message_data;
28 uint16 media_error;
29 uint16 system_error;
30 };
31
32 // Helper function for obtaining a std::string given a PP_Var of type
33 // PP_VARTYPE_STRING. Returns an empty string when things go wrong, or the
34 // contents of the PP_Var (which could also be empty).
35 std::string VarToString(const PPB_Var* var_if, const PP_Var* var) {
36 std::string var_string;
37
38 if (var_if && var && var->type == PP_VARTYPE_STRING) {
39 // Extract the key system string from |key_system_arg|. The extracted
40 // string is NOT NULL TERMINATED.
41 uint32_t length = 0;
42 const char* p_var_string = var_if->VarToUtf8(*var, &length);
43
44 if (p_var_string && length)
45 var_string.assign(p_var_string, length);
46 }
47
48 return var_string;
49 }
50
51 // Creates PP_Var from |str| using |var_if|, AddRef's it, and returns the
52 // PP_Var. Returns empty var when |var_if| is NULL, or when |str| is empty.
53 PP_Var StringToVar(const PPB_Var* var_if, const std::string& str) {
54 if (!var_if || str.empty())
55 return PP_MakeNull();
56
57 PP_Var var = var_if->VarFromUtf8(str.c_str(), str.length());
58 var_if->AddRef(var);
59 return var;
60 }
61
62 } // namespace
63
64
65 // A wrapper class responsible for managing interaction between the browser and
66 // a Content Decryption Module (CDM).
67 class CDMWrapper : public pp::Instance,
68 public pp::ContentDecryptor_Dev {
69 public:
70 explicit CDMWrapper(PP_Instance instance, pp::Module* module);
71 virtual ~CDMWrapper() {}
72
73 // PPP_ContentDecryptor_Dev methods
74 virtual bool GenerateKeyRequest(PP_Var key_system,
75 PP_Resource init_data);
76
77 virtual bool AddKey(PP_Var session_id,
78 PP_Resource key) {
79 return false;
80 }
81
82 virtual bool CancelKeyRequest(PP_Var session_id) {
83 return false;
84 }
85
86 virtual bool Decrypt(PP_Resource encrypted_block,
87 PP_CompletionCallback callback);
88
89 virtual bool DecryptAndDecode(PP_Resource encrypted_block,
90 PP_CompletionCallback callback) {
91 return false;
92 }
93
94 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
95 return true;
96 }
97
98 private:
99 PP_Resource StringToBufferResource(const std::string& str);
100 void DispatchKeyMessage(int32 result, void* data);
101
102 // Browser interfaces.
103 const PPB_Buffer_Dev* buffer_if_;
104 const PPB_Console_Dev* console_if_;
105 const PPB_ContentDecryptor_Dev* cdm_if_;
106 const PPB_Var* var_if_;
107
108 pp::CompletionCallbackFactory<CDMWrapper> factory_;
109 };
110
111 // This object is the global object representing this plugin library as long
112 // as it is loaded.
113 class MyModule : public pp::Module {
114 public:
115 MyModule() : pp::Module() {}
116 virtual ~MyModule() {}
117
118 virtual pp::Instance* CreateInstance(PP_Instance instance) {
119 return new CDMWrapper(instance, this);
120 }
121 };
122
123 CDMWrapper::CDMWrapper(PP_Instance instance,
124 pp::Module* module)
125 : pp::Instance(instance),
126 pp::ContentDecryptor_Dev(this) {
127 console_if_ = static_cast<const PPB_Console_Dev*>(
128 module->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE));
129 assert(console_if_);
130 cdm_if_ = static_cast<const PPB_ContentDecryptor_Dev*>(
131 module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_DEV_INTERFACE));
132 assert(cdm_if_);
133 buffer_if_ = static_cast<const PPB_Buffer_Dev*>(
134 module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE));
135 assert(buffer_if_);
136 var_if_ = static_cast<const PPB_Var*>(
137 module->GetBrowserInterface(PPB_VAR_INTERFACE));
138 assert(var_if_);
139 factory_.Initialize(this);
140 }
141
142 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg,
143 PP_Resource init_data) {
144 std::string key_system = VarToString(var_if_, &key_system_arg);
145 if (key_system.empty())
146 return false;
147
148 if (init_data) {
149 pp::Buffer_Dev init_buffer(init_data);
150 if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) ||
151 !init_buffer.data()) {
152 return false;
153 }
154 }
155
156 // TODO(tomfinegan): this is a testing hack, remove.
157 const std::string message = "key request";
158 PP_Resource message_resource = StringToBufferResource(message);
159
160 if (!message_resource)
161 return false;
162
163 const std::string session_id = "12345";
164 PP_Var session_id_var = StringToVar(var_if_, session_id);
165
166 const std::string default_url = "http://www.google.com";
167 PP_Var default_url_var = StringToVar(var_if_, default_url);
168
169 if (!pp::Module::Get()->core()->IsMainThread()) {
ddorwin 2012/07/18 21:48:36 As this function is written, this statement will n
Tom Finegan 2012/07/23 18:41:53 Done.
170 DecryptorMessage* dm = new (std::nothrow) DecryptorMessage;
ddorwin 2012/07/18 21:48:36 s/dm/message/
ddorwin 2012/07/18 21:48:36 Why nothrow? We're not checking the pointer. I thi
Tom Finegan 2012/07/23 18:41:53 Removed the nothrow.
Tom Finegan 2012/07/23 18:41:53 Renamed to decryptor_message (because I already ha
171 dm->key_system = key_system_arg;
172 dm->session_id = session_id_var;
173 dm->default_url = default_url_var;
174 dm->message_data = message_resource;
175 void* data = reinterpret_cast<void*>(&dm);
176
177 pp::Module::Get()->core()->CallOnMainThread(
178 0, factory_.NewCallback(&CDMWrapper::DispatchKeyMessage, data), 0);
ddorwin 2012/07/18 21:48:36 data will be leaked if the callback is not called
Tom Finegan 2012/07/23 18:41:53 Ah-- I'll update to pass by value in the next pass
179 } else {
180 cdm_if_->KeyMessage(pp_instance(), key_system_arg, session_id_var,
181 message_resource, default_url_var);
182 }
183
184 return true;
185 }
186
187 bool CDMWrapper::Decrypt(PP_Resource encrypted_block,
188 PP_CompletionCallback callback) {
189 pp::Buffer_Dev block_buffer(encrypted_block);
190 if (!buffer_if_->IsBuffer(block_buffer.pp_resource()) ||
191 !block_buffer.data()) {
192 return false;
193 }
194
195 const std::string kDummyDecryptedData = "Pretend I'm decrypted data!";
196 PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData);
197 if (!decrypted_resource)
198 return false;
199
200 cdm_if_->DeliverBlock(pp_instance(), decrypted_resource, callback);
201 return true;
202 }
203
204 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) {
205 if (!str.size())
206 return 0;
207
208 pp::Buffer_Dev buffer(this, str.size());
209 if (!buffer_if_->IsBuffer(buffer.pp_resource()) || !buffer.data())
210 return 0;
211
212 memcpy(buffer.data(), str.data(), str.size());
213 return buffer.detach();
214 }
215
216 void CDMWrapper::DispatchKeyMessage(int32 result, void* data) {
217 DecryptorMessage* message = reinterpret_cast<DecryptorMessage*>(data);
ddorwin 2012/07/18 21:48:36 This should be moot based on above, but I would ju
Tom Finegan 2012/07/23 18:41:53 Done.
218 if (message) {
219 cdm_if_->KeyMessage(pp_instance(),
220 message->key_system,
221 message->session_id,
222 message->message_data,
223 message->default_url);
224 delete data;
225 }
226 }
227
228 namespace pp {
229
230 // Factory function for your specialization of the Module object.
231 Module* CreateModule() {
232 return new MyModule();
233 }
234
235 } // namespace pp
OLDNEW
« 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