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"
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"
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 {
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())
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() {
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
,
59 bool result
= VInvokeSrpcMethod(method_name
, input_signature
, params
, vl
);
64 bool NaClSubprocess::VInvokeSrpcMethod(const std::string
& method_name
,
65 const std::string
& input_signature
,
68 if (NULL
== srpc_client_
.get()) {
69 PLUGIN_PRINTF(("VInvokeSrpcMethod (no srpc_client_)\n"));
72 if (!srpc_client_
->HasMethod(method_name
)) {
73 PLUGIN_PRINTF(("VInvokeSrpcMethod (no %s method found)\n",
74 method_name
.c_str()));
77 if (!srpc_client_
->InitParams(method_name
, params
)) {
78 PLUGIN_PRINTF(("VInvokeSrpcMethod (InitParams failed)\n"));
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.
88 PLUGIN_PRINTF(("PnaclSrpcLib::InvokeSrpcMethod unhandled type: %c\n",
91 case NACL_SRPC_ARG_TYPE_BOOL
: {
92 int input
= va_arg(vl
, int);
93 params
->ins()[i
]->u
.bval
= input
;
96 case NACL_SRPC_ARG_TYPE_DOUBLE
: {
97 double input
= va_arg(vl
, double);
98 params
->ins()[i
]->u
.dval
= input
;
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
);
107 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n"));
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
);
115 case NACL_SRPC_ARG_TYPE_HANDLE
: {
116 NaClSrpcImcDescType input
= va_arg(vl
, NaClSrpcImcDescType
);
117 params
->ins()[i
]->u
.hval
= input
;
120 case NACL_SRPC_ARG_TYPE_INT
: {
121 int32_t input
= va_arg(vl
, int32_t);
122 params
->ins()[i
]->u
.ival
= input
;
125 case NACL_SRPC_ARG_TYPE_LONG
: {
126 int64_t input
= va_arg(vl
, int64_t);
127 params
->ins()[i
]->u
.lval
= input
;
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
);
135 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n"));
138 params
->ins()[i
]->arrays
.str
= input
;
143 return srpc_client_
->Invoke(method_name
, params
);
146 } // namespace plugin