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 "ppapi/native_client/src/trusted/plugin/nacl_subprocess.h"
9 #include "native_client/src/shared/srpc/nacl_srpc.h"
10 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
11 #include "ppapi/native_client/src/trusted/plugin/srpc_params.h"
15 nacl::string
NaClSubprocess::detailed_description() const {
16 nacl::stringstream ss
;
18 << "={ this=" << static_cast<const void*>(this)
19 << ", srpc_client=" << static_cast<void*>(srpc_client_
.get())
20 << ", service_runtime=" << static_cast<void*>(service_runtime_
.get())
25 // Shutdown the socket connection and service runtime, in that order.
26 void NaClSubprocess::Shutdown() {
27 srpc_client_
.reset(NULL
);
28 if (service_runtime_
.get() != NULL
) {
29 service_runtime_
->Shutdown();
30 service_runtime_
.reset(NULL
);
34 NaClSubprocess::~NaClSubprocess() {
38 bool NaClSubprocess::StartSrpcServices() {
39 srpc_client_
.reset(service_runtime_
->SetupAppChannel());
40 return NULL
!= srpc_client_
.get();
43 bool NaClSubprocess::InvokeSrpcMethod(const nacl::string
& method_name
,
44 const nacl::string
& input_signature
,
49 bool result
= VInvokeSrpcMethod(method_name
, input_signature
, params
, vl
);
54 bool NaClSubprocess::VInvokeSrpcMethod(const nacl::string
& method_name
,
55 const nacl::string
& input_signature
,
58 if (NULL
== srpc_client_
.get()) {
59 PLUGIN_PRINTF(("VInvokeSrpcMethod (no srpc_client_)\n"));
62 if (!srpc_client_
->HasMethod(method_name
)) {
63 PLUGIN_PRINTF(("VInvokeSrpcMethod (no %s method found)\n",
64 method_name
.c_str()));
67 if (!srpc_client_
->InitParams(method_name
, params
)) {
68 PLUGIN_PRINTF(("VInvokeSrpcMethod (InitParams failed)\n"));
72 for (size_t i
= 0; i
< input_signature
.length(); ++i
) {
73 char c
= input_signature
[i
];
74 // Only handle the limited number of SRPC types used for PNaCl.
75 // Add more as needed.
78 PLUGIN_PRINTF(("PnaclSrpcLib::InvokeSrpcMethod unhandled type: %c\n",
81 case NACL_SRPC_ARG_TYPE_BOOL
: {
82 int input
= va_arg(vl
, int);
83 params
->ins()[i
]->u
.bval
= input
;
86 case NACL_SRPC_ARG_TYPE_DOUBLE
: {
87 double input
= va_arg(vl
, double);
88 params
->ins()[i
]->u
.dval
= input
;
91 case NACL_SRPC_ARG_TYPE_CHAR_ARRAY
: {
92 // SrpcParam's destructor *should* free the allocated array
93 const char* orig_arr
= va_arg(vl
, const char*);
94 size_t len
= va_arg(vl
, size_t);
95 char* input
= (char *)malloc(len
);
97 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n"));
100 memcpy(input
, orig_arr
, len
);
101 params
->ins()[i
]->arrays
.carr
= input
;
102 params
->ins()[i
]->u
.count
= static_cast<nacl_abi_size_t
>(len
);
105 case NACL_SRPC_ARG_TYPE_HANDLE
: {
106 NaClSrpcImcDescType input
= va_arg(vl
, NaClSrpcImcDescType
);
107 params
->ins()[i
]->u
.hval
= input
;
110 case NACL_SRPC_ARG_TYPE_INT
: {
111 int32_t input
= va_arg(vl
, int32_t);
112 params
->ins()[i
]->u
.ival
= input
;
115 case NACL_SRPC_ARG_TYPE_LONG
: {
116 int64_t input
= va_arg(vl
, int64_t);
117 params
->ins()[i
]->u
.lval
= input
;
120 case NACL_SRPC_ARG_TYPE_STRING
: {
121 // SrpcParam's destructor *should* free the dup'ed string.
122 const char* orig_str
= va_arg(vl
, const char*);
123 char* input
= strdup(orig_str
);
125 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n"));
128 params
->ins()[i
]->arrays
.str
= input
;
133 return srpc_client_
->Invoke(method_name
, params
);
136 } // namespace plugin