Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / nacl / renderer / plugin / nacl_subprocess.cc
blobf9f5510045ae0bbe1d08e9f29d7661e316785bed
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.
5 #include "components/nacl/renderer/plugin/nacl_subprocess.h"
7 #include <stdarg.h>
9 #include <sstream>
11 #include "components/nacl/renderer/plugin/plugin_error.h"
12 #include "components/nacl/renderer/plugin/srpc_params.h"
13 #include "native_client/src/shared/srpc/nacl_srpc.h"
15 namespace plugin {
17 NaClSubprocess::NaClSubprocess(const std::string& description,
18 ServiceRuntime* service_runtime,
19 SrpcClient* srpc_client)
20 : description_(description),
21 service_runtime_(service_runtime),
22 srpc_client_(srpc_client) {
25 std::string NaClSubprocess::detailed_description() const {
26 std::stringstream ss;
27 ss << description()
28 << "={ this=" << static_cast<const void*>(this)
29 << ", srpc_client=" << static_cast<void*>(srpc_client_.get())
30 << ", service_runtime=" << static_cast<void*>(service_runtime_.get())
31 << " }";
32 return ss.str();
35 // Shutdown the socket connection and service runtime, in that order.
36 void NaClSubprocess::Shutdown() {
37 srpc_client_.reset(NULL);
38 if (service_runtime_.get() != NULL) {
39 service_runtime_->Shutdown();
40 service_runtime_.reset(NULL);
44 NaClSubprocess::~NaClSubprocess() {
45 Shutdown();
48 bool NaClSubprocess::StartSrpcServices() {
49 srpc_client_.reset(service_runtime_->SetupAppChannel());
50 return NULL != srpc_client_.get();
53 bool NaClSubprocess::InvokeSrpcMethod(const std::string& method_name,
54 const std::string& input_signature,
55 SrpcParams* params,
56 ...) {
57 va_list vl;
58 va_start(vl, params);
59 bool result = VInvokeSrpcMethod(method_name, input_signature, params, vl);
60 va_end(vl);
61 return result;
64 bool NaClSubprocess::VInvokeSrpcMethod(const std::string& method_name,
65 const std::string& input_signature,
66 SrpcParams* params,
67 va_list vl) {
68 if (NULL == srpc_client_.get()) {
69 PLUGIN_PRINTF(("VInvokeSrpcMethod (no srpc_client_)\n"));
70 return false;
72 if (!srpc_client_->HasMethod(method_name)) {
73 PLUGIN_PRINTF(("VInvokeSrpcMethod (no %s method found)\n",
74 method_name.c_str()));
75 return false;
77 if (!srpc_client_->InitParams(method_name, params)) {
78 PLUGIN_PRINTF(("VInvokeSrpcMethod (InitParams failed)\n"));
79 return false;
81 // Marshall inputs.
82 for (size_t i = 0; i < input_signature.length(); ++i) {
83 char c = input_signature[i];
84 // Only handle the limited number of SRPC types used for PNaCl.
85 // Add more as needed.
86 switch (c) {
87 default:
88 PLUGIN_PRINTF(("PnaclSrpcLib::InvokeSrpcMethod unhandled type: %c\n",
89 c));
90 return false;
91 case NACL_SRPC_ARG_TYPE_BOOL: {
92 int input = va_arg(vl, int);
93 params->ins()[i]->u.bval = input;
94 break;
96 case NACL_SRPC_ARG_TYPE_DOUBLE: {
97 double input = va_arg(vl, double);
98 params->ins()[i]->u.dval = input;
99 break;
101 case NACL_SRPC_ARG_TYPE_CHAR_ARRAY: {
102 // SrpcParam's destructor *should* free the allocated array
103 const char* orig_arr = va_arg(vl, const char*);
104 size_t len = va_arg(vl, size_t);
105 char* input = (char *)malloc(len);
106 if (!input) {
107 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n"));
108 return false;
110 memcpy(input, orig_arr, len);
111 params->ins()[i]->arrays.carr = input;
112 params->ins()[i]->u.count = static_cast<nacl_abi_size_t>(len);
113 break;
115 case NACL_SRPC_ARG_TYPE_HANDLE: {
116 NaClSrpcImcDescType input = va_arg(vl, NaClSrpcImcDescType);
117 params->ins()[i]->u.hval = input;
118 break;
120 case NACL_SRPC_ARG_TYPE_INT: {
121 int32_t input = va_arg(vl, int32_t);
122 params->ins()[i]->u.ival = input;
123 break;
125 case NACL_SRPC_ARG_TYPE_LONG: {
126 int64_t input = va_arg(vl, int64_t);
127 params->ins()[i]->u.lval = input;
128 break;
130 case NACL_SRPC_ARG_TYPE_STRING: {
131 // SrpcParam's destructor *should* free the dup'ed string.
132 const char* orig_str = va_arg(vl, const char*);
133 char* input = strdup(orig_str);
134 if (!input) {
135 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n"));
136 return false;
138 params->ins()[i]->arrays.str = input;
139 break;
143 return srpc_client_->Invoke(method_name, params);
146 } // namespace plugin