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

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: More plumbing 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 <cassert>
6 #include <string>
7
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/c/pp_var.h"
ddorwin 2012/07/23 23:53:36 Shouldn't need anything from c/ except. cpp/ shoul
Tom Finegan 2012/07/24 00:26:21 Removed some of the c/ stuff. I'll look into this
12 #include "ppapi/c/ppb_var.h"
13 #include "ppapi/c/dev/ppb_buffer_dev.h"
14 #include "ppapi/c/dev/ppb_console_dev.h"
15 #include "ppapi/c/dev/ppb_content_decryptor_dev.h"
16 #include "ppapi/cpp/completion_callback.h"
17 #include "ppapi/cpp/core.h"
18 #include "ppapi/cpp/instance.h"
19 #include "ppapi/cpp/module.h"
20 #include "ppapi/cpp/resource.h"
21 #include "ppapi/cpp/var.h"
22 #include "ppapi/cpp/dev/buffer_dev.h"
23 #include "ppapi/cpp/dev/content_decryptor_dev.h"
24 #include "ppapi/shared_impl/var.h"
25 #include "ppapi/utility/completion_callback_factory.h"
26
27 namespace {
28
29 struct DecryptorMessage {
30 std::string key_system;
31 std::string session_id;
32 std::string default_url;
33 std::string message_data;
34 uint16 media_error;
35 uint16 system_error;
36 };
37
38 bool IsMainThread() {
39 return pp::Module::Get()->core()->IsMainThread();
40 }
41
42 void CallOnMain(pp::CompletionCallback cb) {
43 pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK);
44 }
45
46 } // namespace
47
48
49 // A wrapper class responsible for managing interaction between the browser and
50 // a Content Decryption Module (CDM).
51 class CDMWrapper : public pp::Instance,
52 public pp::ContentDecryptor_Dev {
53 public:
54 explicit CDMWrapper(PP_Instance instance, pp::Module* module);
55 virtual ~CDMWrapper() {}
56
57 // PPP_ContentDecryptor_Dev methods
58 virtual bool GenerateKeyRequest(PP_Var key_system, PP_Resource init_data);
59 virtual bool AddKey(PP_Var session_id, PP_Resource key);
60
61 virtual bool CancelKeyRequest(PP_Var session_id) {
62 return false;
63 }
64
65 virtual bool Decrypt(PP_Resource encrypted_block,
66 PP_CompletionCallback callback);
67
68 virtual bool DecryptAndDecode(PP_Resource encrypted_block,
69 PP_CompletionCallback callback) {
70 return false;
71 }
72
73 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
74 return true;
75 }
76
77 private:
78 PP_Resource StringToBufferResource(const std::string& str);
79 void NeedKey(int32 result, void* data);
80 void KeyAdded(int32 result, void* data);
81 void KeyMessage(int32 result, void* data);
82 void KeyError(int32 result, void* data);
83 void DeliverBlock(int32 result, void* data);
84
85
86 // Browser interfaces.
87 const PPB_Buffer_Dev* buffer_if_;
88 const PPB_ContentDecryptor_Dev* decryptor_if_;
89
90 pp::CompletionCallbackFactory<CDMWrapper> factory_;
91 std::string key_;
ddorwin 2012/07/23 23:53:36 I assume this is temporary for testing? Probably:
Tom Finegan 2012/07/24 00:26:21 I have a TODO there as a result of today's work--
92 };
93
94 // This object is the global object representing this plugin library as long
95 // as it is loaded.
96 class MyModule : public pp::Module {
97 public:
98 MyModule() : pp::Module() {}
99 virtual ~MyModule() {}
100
101 virtual pp::Instance* CreateInstance(PP_Instance instance) {
102 return new CDMWrapper(instance, this);
103 }
104 };
105
106 CDMWrapper::CDMWrapper(PP_Instance instance,
107 pp::Module* module)
108 : pp::Instance(instance),
109 pp::ContentDecryptor_Dev(this) {
110 decryptor_if_ = static_cast<const PPB_ContentDecryptor_Dev*>(
ddorwin 2012/07/23 23:53:36 TODO(tomfinegan): Replace with use of C++ interfac
Tom Finegan 2012/07/24 00:26:21 Done.
111 module->GetBrowserInterface(PPB_CONTENTDECRYPTOR_DEV_INTERFACE));
112 assert(decryptor_if_);
113 buffer_if_ = static_cast<const PPB_Buffer_Dev*>(
114 module->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE));
115 assert(buffer_if_);
116 factory_.Initialize(this);
117 }
118
119 bool CDMWrapper::GenerateKeyRequest(PP_Var key_system_arg,
120 PP_Resource init_data) {
121 if (init_data) {
122 pp::Buffer_Dev init_buffer(init_data);
ddorwin 2012/07/23 23:53:36 You'll probably need this variable below, meaning
Tom Finegan 2012/07/24 00:26:21 Yeah-- I'll move it once I'm actually doing someth
123 if (!buffer_if_->IsBuffer(init_buffer.pp_resource()) ||
ddorwin 2012/07/23 23:53:36 .pp_resource() appears to be unnecessary (line 151
Tom Finegan 2012/07/24 00:26:21 Doesn't compile. key is a PP_Resource, but key_buf
ddorwin 2012/07/24 18:57:26 Ahh, the difference with 151 was subtle. See new c
124 !init_buffer.data()) {
125 return false;
126 }
127 }
128
129 using ppapi::StringVar;
ddorwin 2012/07/23 23:53:36 using should be at the top of the file. Same for 1
Tom Finegan 2012/07/24 00:26:21 Done.
130 StringVar* key_system_var = ppapi::StringVar::FromPPVar(key_system_arg);
ddorwin 2012/07/23 23:53:36 no need for ppapi:: here. Update to match 148, whi
Tom Finegan 2012/07/24 00:26:21 Done.
131
132 DecryptorMessage* decryptor_message = new DecryptorMessage;
133 if (!decryptor_message)
134 return false;
135
136 decryptor_message->key_system = key_system_var->value();
137 decryptor_message->session_id = "12345";
138 decryptor_message->default_url = "http://www.google.com";
139 decryptor_message->message_data = "key request";
140
141 void* data = reinterpret_cast<void*>(decryptor_message);
142 CallOnMain(factory_.NewCallback(&CDMWrapper::KeyMessage, data));
143 return true;
144 }
145
146 bool CDMWrapper::AddKey(PP_Var session_id_var, PP_Resource key) {
147 using ppapi::StringVar;
148 std::string session_id = StringVar::FromPPVar(session_id_var)->value();
149
150 pp::Buffer_Dev key_buffer(key);
151 if (!buffer_if_->IsBuffer(key) || !key_buffer.data())
152 return false;
153
154 key_.assign(reinterpret_cast<char*>(key_buffer.data()), key_buffer.size());
155 return true;
156 }
157
158 bool CDMWrapper::Decrypt(PP_Resource encrypted_block,
159 PP_CompletionCallback callback) {
160 pp::Buffer_Dev block_buffer(encrypted_block);
161 if (!buffer_if_->IsBuffer(block_buffer.pp_resource()) ||
162 !block_buffer.data()) {
163 return false;
164 }
165
166 const std::string kDummyDecryptedData = "Pretend I'm decrypted data!";
167 PP_Resource decrypted_resource = StringToBufferResource(kDummyDecryptedData);
168 if (!decrypted_resource)
169 return false;
170
171 decryptor_if_->DeliverBlock(pp_instance(), decrypted_resource, callback);
172 return true;
173 }
174
175 PP_Resource CDMWrapper::StringToBufferResource(const std::string& str) {
176 if (!str.size())
177 return 0;
178
179 pp::Buffer_Dev buffer(this, str.size());
180 if (!buffer_if_->IsBuffer(buffer.pp_resource()) || !buffer.data())
181 return 0;
182
183 memcpy(buffer.data(), str.data(), str.size());
184 return buffer.detach();
185 }
186
187 void CDMWrapper::NeedKey(int32 result, void* data) {
188
189 }
190
191 void CDMWrapper::KeyMessage(int32 result, void* data) {
192 scoped_ptr<DecryptorMessage> decryptor_message(
193 reinterpret_cast<DecryptorMessage*>(data));
194 if (decryptor_message.get()) {
ddorwin 2012/07/23 23:53:36 assuming else is an error, fail early and de-inden
Tom Finegan 2012/07/24 00:26:21 Passing const DecryptorMessage& instead of a void*
ddorwin 2012/07/24 18:57:26 I'm not sure of the mechanism, but pass by value,
195 using ppapi::StringVar;
196 PP_Var key_system = StringVar::StringToPPVar(decryptor_message->key_system);
197 PP_Var session_id = StringVar::StringToPPVar(decryptor_message->session_id);
198 PP_Resource message =
199 StringToBufferResource(decryptor_message->message_data);
200 PP_Var default_url =
201 StringVar::StringToPPVar(decryptor_message->default_url);
202 decryptor_if_->KeyMessage(pp_instance(),
203 key_system,
204 session_id,
205 message,
206 default_url);
207 }
208 }
209
210 namespace pp {
211
212 // Factory function for your specialization of the Module object.
213 Module* CreateModule() {
214 return new MyModule();
215 }
216
217 } // 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