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

Side by Side Diff: ppapi/native_client/src/trusted/plugin/scriptable_handle.cc

Issue 9390028: Remove browser support for non-PPAPI nexes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 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 // Scriptable handle implementation.
6
7 #include "native_client/src/trusted/plugin/scriptable_handle.h"
8
9 #include <stdio.h>
10 #include <string.h>
11
12 #include <assert.h>
13 #include <set>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17
18
19 #include "native_client/src/include/checked_cast.h"
20 #include "native_client/src/include/nacl_macros.h"
21 #include "native_client/src/include/nacl_string.h"
22 #include "native_client/src/include/portability.h"
23 #include "native_client/src/shared/platform/nacl_check.h"
24 #include "native_client/src/shared/srpc/nacl_srpc.h"
25 #include "native_client/src/trusted/plugin/array_ppapi.h"
26 #include "native_client/src/trusted/plugin/browser_interface.h"
27 #include "native_client/src/trusted/plugin/desc_based_handle.h"
28 #include "native_client/src/trusted/plugin/method_map.h"
29 #include "native_client/src/trusted/plugin/plugin.h"
30 #include "native_client/src/trusted/plugin/utility.h"
31 #include "native_client/src/trusted/plugin/var_utils.h"
32
33
34 namespace plugin {
35
36 namespace {
37
38 // For security we keep track of the set of scriptable handles that were
39 // created.
40
41 std::set<const plugin::ScriptableHandle*>* g_ValidHandles = 0;
42
43 void RememberValidHandle(const ScriptableHandle* handle) {
44 // Initialize the set.
45 // BUG: this is not thread safe. We may leak sets, or worse, may not
46 // correctly insert valid handles into the set.
47 // TODO(sehr): use pthread_once or similar initialization.
48 if (NULL == g_ValidHandles) {
49 g_ValidHandles = new(std::nothrow) std::set<const ScriptableHandle*>;
50 if (NULL == g_ValidHandles) {
51 return;
52 }
53 }
54 // Remember the handle.
55 g_ValidHandles->insert(handle);
56 }
57
58 pp::Var Error(const nacl::string& call_name, const char* caller,
59 const char* error, pp::Var* exception) {
60 nacl::stringstream error_stream;
61 error_stream << call_name << ": " << error;
62 if (!exception->is_undefined()) {
63 error_stream << " - " + exception->AsString();
64 }
65 // Get the error string in 2 steps; otherwise, the temporary string returned
66 // by the stream is destructed, causing a dangling pointer.
67 std::string str = error_stream.str();
68 const char* e = str.c_str();
69 PLUGIN_PRINTF(("ScriptableHandle::%s (%s)\n", caller, e));
70 *exception = pp::Var(e);
71 return pp::Var();
72 }
73
74 // Helper functionality common to HasProperty and HasMethod.
75 bool HasCallType(Plugin* plugin,
76 CallType call_type,
77 const nacl::string& call_name,
78 const char* caller) {
79 uintptr_t id = plugin->browser_interface()->StringToIdentifier(call_name);
80 PLUGIN_PRINTF(("ScriptableHandle::%s (id=%"NACL_PRIxPTR")\n",
81 caller, id));
82 return plugin->HasMethod(id, call_type);
83 }
84
85 // Helper functionality common to GetProperty, SetProperty and Call.
86 // If |call_type| is PROPERTY_GET, ignores args and expects 1 return var.
87 // If |call_type| is PROPERTY_SET, expects 1 arg and returns void var.
88 // Sets |exception| on failure.
89 pp::Var Invoke(Plugin* plugin,
90 CallType call_type,
91 const nacl::string& call_name,
92 const char* caller,
93 const std::vector<pp::Var>& args,
94 pp::Var* exception) {
95 uintptr_t id = plugin->browser_interface()->StringToIdentifier(call_name);
96
97 // Initialize input/output parameters.
98 SrpcParams params;
99 NaClSrpcArg** inputs = params.ins();
100 NaClSrpcArg** outputs = params.outs();
101 if (!plugin->InitParams(id, call_type, &params)) {
102 return Error(call_name, caller,
103 "srpc parameter initialization failed", exception);
104 }
105 uint32_t input_length = params.InputLength();
106 int32_t output_length = params.OutputLength();
107 PLUGIN_PRINTF(("ScriptableHandle::%s (initialized %"NACL_PRIu32" ins, %"
108 NACL_PRId32" outs)\n", caller, input_length, output_length));
109
110 // Verify input/output parameter list length.
111 if (args.size() != params.SignatureLength()) {
112 return Error(call_name, caller,
113 "incompatible srpc parameter list", exception);
114 }
115 PLUGIN_PRINTF(("ScriptableHandle::%s (verified signature)\n", caller));
116
117 // Marshall input parameters.
118 if (input_length > 0) {
119 assert(call_type != PROPERTY_GET); // expect no inputs for "get"
120 for (int i = 0; (i < NACL_SRPC_MAX_ARGS) && (inputs[i] != NULL); ++i) {
121 if (!PPVarToNaClSrpcArg(args[i], inputs[i], exception)) {
122 return Error(call_name, caller,
123 "srpc input marshalling failed", exception);
124 }
125 }
126 }
127 if (call_type == PROPERTY_SET) assert(input_length == 1);
128 PLUGIN_PRINTF(("ScriptableHandle::%s (marshalled inputs)\n", caller));
129
130 // Allocate array-typed output parameters.
131 if (args.size() > input_length) {
132 for (int i = 0; (i < NACL_SRPC_MAX_ARGS) && (outputs[i] != NULL); ++i) {
133 if (!PPVarToAllocateNaClSrpcArg(args[input_length + i],
134 outputs[i], exception)) {
135 return Error(call_name, caller, "srpc output array allocation failed",
136 exception);
137 }
138 }
139 }
140 PLUGIN_PRINTF(("ScriptableHandle::%s (output array allocation done)\n",
141 caller));
142
143 // Invoke.
144 if (!plugin->Invoke(id, call_type, &params)) {
145 nacl::string err = nacl::string(caller) + "('" + call_name + "') failed\n";
146 if (params.exception_string() != NULL) {
147 err = params.exception_string();
148 }
149 *exception = pp::Var(err.c_str());
150 return Error(call_name, caller, "invocation failed", exception);
151 }
152 PLUGIN_PRINTF(("ScriptableHandle::%s (invocation done)\n", caller));
153
154 // Marshall output parameters.
155 pp::Var retvar;
156 if (output_length > 0) {
157 assert(call_type != PROPERTY_SET); // expect no outputs for "set"
158 retvar = NaClSrpcArgToPPVar(outputs[0], plugin, exception);
159 if (output_length > 1) {
160 ArrayPpapi* array = new(std::nothrow) ArrayPpapi(plugin);
161 if (array == NULL) {
162 *exception = pp::Var("failed to allocate output array");
163 } else {
164 array->SetProperty(pp::Var(0), retvar, exception);
165 for (int32_t i = 1; i < output_length; ++i) {
166 pp::Var v = NaClSrpcArgToPPVar(outputs[i], plugin, exception);
167 array->SetProperty(pp::Var(i), v, exception);
168 }
169 }
170
171 retvar = pp::VarPrivate(plugin, array);
172 }
173 if (!exception->is_undefined()) {
174 return Error(call_name, caller, "srpc output marshalling failed",
175 exception);
176 }
177 }
178 if (call_type == PROPERTY_GET) assert(output_length == 1);
179 return retvar;
180 }
181
182 } // namespace
183
184 ScriptableHandle::ScriptableHandle(Plugin* plugin)
185 : var_(NULL), num_unref_calls_(0), plugin_(plugin), desc_handle_(NULL) {
186 PLUGIN_PRINTF(("ScriptableHandle::ScriptableHandle (this=%p, plugin=%p)\n",
187 static_cast<void*>(this),
188 static_cast<void*>(plugin)));
189 RememberValidHandle(this);
190 PLUGIN_PRINTF(("ScriptableHandle::ScriptableHandle (this=%p)\n",
191 static_cast<void*>(this)));
192 }
193
194 ScriptableHandle::ScriptableHandle(DescBasedHandle* desc_handle)
195 : var_(NULL), num_unref_calls_(0), plugin_(NULL), desc_handle_(desc_handle) {
196 PLUGIN_PRINTF(("ScriptableHandle::ScriptableHandle (this=%p,"
197 " desc_handle=%p)\n",
198 static_cast<void*>(this),
199 static_cast<void*>(desc_handle)));
200 RememberValidHandle(this);
201 PLUGIN_PRINTF(("ScriptableHandle::ScriptableHandle (this=%p)\n",
202 static_cast<void*>(this)));
203 }
204
205 ScriptableHandle::~ScriptableHandle() {
206 PLUGIN_PRINTF(("ScriptableHandle::~ScriptableHandle (this=%p)\n",
207 static_cast<void*>(this)));
208 // If the set was empty, just return.
209 if (NULL == g_ValidHandles) {
210 return;
211 }
212 // Remove the scriptable handle from the set of valid handles.
213 g_ValidHandles->erase(this);
214 // If handle is a plugin, the browser is deleting it (and might have
215 // already done so). Otherwise, delete here.
216 if (desc_handle_ != NULL) {
217 PLUGIN_PRINTF(("ScriptableHandle::~ScriptableHandle "
218 "(this=%p, delete desc_handle=%p)\n",
219 static_cast<void*>(this), static_cast<void*>(desc_handle_)));
220 delete desc_handle_;
221 desc_handle_ = NULL;
222 }
223 PLUGIN_PRINTF(("ScriptableHandle::~ScriptableHandle (this=%p, return)\n",
224 static_cast<void*>(this)));
225 }
226
227 // Check that an object is a validly created ScriptableHandle.
228 bool ScriptableHandle::is_valid(const ScriptableHandle* handle) {
229 PLUGIN_PRINTF(("ScriptableHandle::is_valid (handle=%p)\n",
230 static_cast<void*>(const_cast<ScriptableHandle*>(handle))));
231 if (NULL == g_ValidHandles) {
232 PLUGIN_PRINTF(("ScriptableHandle::is_valid (return 0)\n"));
233 return false;
234 }
235 size_t count =
236 g_ValidHandles->count(static_cast<const ScriptableHandle*>(handle));
237 PLUGIN_PRINTF(("ScriptableHandle::is_valid (handle=%p, count=%"
238 NACL_PRIuS")\n",
239 static_cast<void*>(const_cast<ScriptableHandle*>(handle)),
240 count));
241 return 0 != count;
242 }
243
244 void ScriptableHandle::Unref(ScriptableHandle** handle) {
245 if (*handle != NULL) {
246 (*handle)->Unref();
247 *handle = NULL;
248 }
249 }
250
251 ScriptableHandle* ScriptableHandle::NewPlugin(Plugin* plugin) {
252 PLUGIN_PRINTF(("ScriptableHandle::NewPlugin (plugin=%p)\n",
253 static_cast<void*>(plugin)));
254 if (plugin == NULL) {
255 return NULL;
256 }
257 ScriptableHandle* scriptable_handle =
258 new(std::nothrow) ScriptableHandle(plugin);
259 if (scriptable_handle == NULL) {
260 return NULL;
261 }
262 PLUGIN_PRINTF(("ScriptableHandle::NewPlugin (return %p)\n",
263 static_cast<void*>(scriptable_handle)));
264 return scriptable_handle;
265 }
266
267
268 ScriptableHandle* ScriptableHandle::NewDescHandle(
269 DescBasedHandle* desc_handle) {
270 PLUGIN_PRINTF(("ScriptableHandle::NewDescHandle (desc_handle=%p)\n",
271 static_cast<void*>(desc_handle)));
272 if (desc_handle == NULL) {
273 return NULL;
274 }
275 ScriptableHandle* scriptable_handle =
276 new(std::nothrow) ScriptableHandle(desc_handle);
277 if (scriptable_handle == NULL) {
278 return NULL;
279 }
280 PLUGIN_PRINTF(("ScriptableHandle::NewDescHandle (return %p)\n",
281 static_cast<void*>(scriptable_handle)));
282 return scriptable_handle;
283 }
284
285
286 bool ScriptableHandle::HasProperty(const pp::Var& name, pp::Var* exception) {
287 UNREFERENCED_PARAMETER(exception);
288 PLUGIN_PRINTF(("ScriptableHandle::HasProperty (this=%p, name=%s)\n",
289 static_cast<void*>(this), name.DebugString().c_str()));
290 if (plugin_ == NULL) {
291 return false;
292 }
293 if (!name.is_string() && !name.is_int())
294 return false;
295 bool has_property = HasCallType(plugin_,
296 PROPERTY_GET,
297 name.AsString(),
298 "HasProperty");
299 PLUGIN_PRINTF(("ScriptableHandle::HasProperty (has_property=%d)\n",
300 has_property));
301 return has_property;
302 }
303
304
305 bool ScriptableHandle::HasMethod(const pp::Var& name, pp::Var* exception) {
306 UNREFERENCED_PARAMETER(exception);
307 PLUGIN_PRINTF(("ScriptableHandle::HasMethod (this=%p, name='%s')\n",
308 static_cast<void*>(this), name.DebugString().c_str()));
309 if (plugin_ == NULL) {
310 return false;
311 }
312 if (!name.is_string())
313 return false;
314 bool has_method = HasCallType(plugin_,
315 METHOD_CALL,
316 name.AsString(),
317 "HasMethod");
318 PLUGIN_PRINTF(("ScriptableHandle::HasMethod (has_method=%d)\n",
319 has_method));
320 return has_method;
321 }
322
323
324 pp::Var ScriptableHandle::GetProperty(const pp::Var& name,
325 pp::Var* exception) {
326 PLUGIN_PRINTF(("ScriptableHandle::GetProperty (name=%s)\n",
327 name.DebugString().c_str()));
328 if (plugin_ == NULL) {
329 return pp::Var();
330 }
331 pp::Var property = Invoke(plugin_,
332 PROPERTY_GET,
333 NameAsString(name),
334 "GetProperty",
335 std::vector<pp::Var>(), exception);
336 PLUGIN_PRINTF(("ScriptableHandle::GetProperty (property=%s)\n",
337 property.DebugString().c_str()));
338 return property;
339 }
340
341
342 void ScriptableHandle::SetProperty(const pp::Var& name,
343 const pp::Var& value,
344 pp::Var* exception) {
345 PLUGIN_PRINTF(("ScriptableHandle::SetProperty (name=%s, value=%s)\n",
346 name.DebugString().c_str(), value.DebugString().c_str()));
347 if (plugin_ == NULL) {
348 return;
349 }
350 std::vector<pp::Var> args;
351 args.push_back(pp::Var(pp::Var::DontManage(), value.pp_var()));
352 Invoke(plugin_,
353 PROPERTY_SET,
354 NameAsString(name),
355 "SetProperty",
356 args,
357 exception);
358 std::string exception_string("NULL");
359 if (!exception->is_undefined()) {
360 exception_string = exception->DebugString();
361 }
362 PLUGIN_PRINTF(("ScriptableHandle::SetProperty (exception=%s)\n",
363 exception_string.c_str()));
364 }
365
366
367 void ScriptableHandle::RemoveProperty(const pp::Var& name,
368 pp::Var* exception) {
369 PLUGIN_PRINTF(("ScriptableHandle::RemoveProperty (name=%s)\n",
370 name.DebugString().c_str()));
371 Error(NameAsString(name), "RemoveProperty",
372 "property removal is not supported", exception);
373 }
374
375 // TODO(polina): should methods also be added?
376 // This is currently never called and the exact semantics is not clear.
377 // http://code.google.com/p/chromium/issues/detail?id=51089
378 void ScriptableHandle::GetAllPropertyNames(std::vector<pp::Var>* properties,
379 pp::Var* exception) {
380 UNREFERENCED_PARAMETER(exception);
381 PLUGIN_PRINTF(("ScriptableHandle::GetAllPropertyNames ()\n"));
382 if (plugin_ == NULL) {
383 return;
384 }
385 std::vector<uintptr_t>* ids = plugin_->GetPropertyIdentifiers();
386 if (ids == NULL) {
387 PLUGIN_PRINTF(("ScriptableHandle::GetAllPropertyNames "
388 "(ids=%p)\n", reinterpret_cast<void*>(ids)));
389 return;
390 }
391 PLUGIN_PRINTF(("ScriptableHandle::GetAllPropertyNames "
392 "(ids->size()=%"NACL_PRIuS")\n", ids->size()));
393 for (size_t i = 0; i < ids->size(); ++i) {
394 nacl::string name =
395 plugin_->browser_interface()->IdentifierToString(ids->at(i));
396 properties->push_back(pp::Var(name));
397 }
398 PLUGIN_PRINTF(("ScriptableHandle::GetAllPropertyNames "
399 "(properties=%"NACL_PRIuS")\n", properties->size()));
400 }
401
402
403 pp::Var ScriptableHandle::Call(const pp::Var& name,
404 const std::vector<pp::Var>& args,
405 pp::Var* exception) {
406 PLUGIN_PRINTF(("ScriptableHandle::Call (name=%s, %"NACL_PRIuS
407 " args)\n", name.DebugString().c_str(), args.size()));
408 if (plugin_ == NULL) {
409 return pp::Var();
410 }
411 if (name.is_undefined()) // invoke default
412 return pp::Var();
413 assert(name.is_string());
414 pp::Var return_var = Invoke(plugin_,
415 METHOD_CALL,
416 name.AsString(),
417 "Call",
418 args,
419 exception);
420 PLUGIN_PRINTF(("ScriptableHandle::Call (return=%s)\n",
421 return_var.DebugString().c_str()));
422 return return_var;
423 }
424
425
426 pp::Var ScriptableHandle::Construct(const std::vector<pp::Var>& args,
427 pp::Var* exception) {
428 PLUGIN_PRINTF(("ScriptableHandle::Construct (%"NACL_PRIuS
429 " args)\n", args.size()));
430 return Error("constructor", "Construct", "constructor is not supported",
431 exception);
432 }
433
434
435 ScriptableHandle* ScriptableHandle::AddRef() {
436 // This is called when we are about to share this object with the browser,
437 // and we need to make sure we have an internal plugin reference, so this
438 // object doesn't get deallocated when the browser discards its references.
439 if (var_ == NULL) {
440 var_ = new(std::nothrow) pp::VarPrivate(plugin_, this);
441 CHECK(var_ != NULL);
442 }
443 PLUGIN_PRINTF(("ScriptableHandle::AddRef (this=%p, var=%p)\n",
444 static_cast<void*>(this), static_cast<void*>(var_)));
445 return this;
446 }
447
448
449 void ScriptableHandle::Unref() {
450 // We should have no more than one internal owner of this object, so this
451 // should be called no more than once.
452 CHECK(++num_unref_calls_ == 1);
453 PLUGIN_PRINTF(("ScriptableHandle::Unref (this=%p, var=%p)\n",
454 static_cast<void*>(this), static_cast<void*>(var_)));
455 if (var_ != NULL) {
456 // We have shared this with the browser while keeping our own var
457 // reference, but we no longer need ours. If the browser has copies,
458 // it will clean things up later, otherwise this object will get
459 // deallocated right away.
460 PLUGIN_PRINTF(("ScriptableHandle::Unref (delete var)\n"));
461 pp::Var* var = var_;
462 var_ = NULL;
463 delete var;
464 } else {
465 // Neither the browser nor plugin ever var referenced this object,
466 // so it can safely discarded.
467 PLUGIN_PRINTF(("ScriptableHandle::Unref (delete this)\n"));
468 CHECK(var_ == NULL);
469 delete this;
470 }
471 }
472
473
474 } // namespace plugin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698