OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/nacl/renderer/plugin/nacl_subprocess.h" | 5 #include "components/nacl/renderer/plugin/nacl_subprocess.h" |
6 | 6 |
7 #include <stdarg.h> | 7 #include <stdarg.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <sstream> | 11 #include <sstream> |
12 | 12 |
13 #include "components/nacl/renderer/plugin/plugin_error.h" | 13 #include "components/nacl/renderer/plugin/plugin_error.h" |
14 #include "components/nacl/renderer/plugin/srpc_params.h" | |
15 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
16 | 14 |
17 namespace plugin { | 15 namespace plugin { |
18 | 16 |
19 NaClSubprocess::NaClSubprocess(const std::string& description, | 17 NaClSubprocess::NaClSubprocess(const std::string& description, |
20 ServiceRuntime* service_runtime, | 18 ServiceRuntime* service_runtime) |
21 SrpcClient* srpc_client) | |
22 : description_(description), | 19 : description_(description), |
23 service_runtime_(service_runtime), | 20 service_runtime_(service_runtime) { |
24 srpc_client_(srpc_client) { | |
25 } | 21 } |
26 | 22 |
27 std::string NaClSubprocess::detailed_description() const { | 23 std::string NaClSubprocess::detailed_description() const { |
28 std::stringstream ss; | 24 std::stringstream ss; |
29 ss << description() | 25 ss << description() |
30 << "={ this=" << static_cast<const void*>(this) | 26 << "={ this=" << static_cast<const void*>(this) |
31 << ", srpc_client=" << static_cast<void*>(srpc_client_.get()) | |
32 << ", service_runtime=" << static_cast<void*>(service_runtime_.get()) | 27 << ", service_runtime=" << static_cast<void*>(service_runtime_.get()) |
33 << " }"; | 28 << " }"; |
34 return ss.str(); | 29 return ss.str(); |
35 } | 30 } |
36 | 31 |
37 // Shutdown the socket connection and service runtime, in that order. | 32 // Shutdown the socket connection and service runtime, in that order. |
38 void NaClSubprocess::Shutdown() { | 33 void NaClSubprocess::Shutdown() { |
39 srpc_client_.reset(NULL); | |
40 if (service_runtime_.get() != NULL) { | 34 if (service_runtime_.get() != NULL) { |
41 service_runtime_->Shutdown(); | 35 service_runtime_->Shutdown(); |
42 service_runtime_.reset(NULL); | 36 service_runtime_.reset(NULL); |
43 } | 37 } |
44 } | 38 } |
45 | 39 |
46 NaClSubprocess::~NaClSubprocess() { | 40 NaClSubprocess::~NaClSubprocess() { |
47 Shutdown(); | 41 Shutdown(); |
48 } | 42 } |
49 | 43 |
50 bool NaClSubprocess::StartSrpcServices() { | |
51 srpc_client_.reset(service_runtime_->SetupAppChannel()); | |
52 return NULL != srpc_client_.get(); | |
53 } | |
54 | |
55 bool NaClSubprocess::InvokeSrpcMethod(const std::string& method_name, | |
56 const std::string& input_signature, | |
57 SrpcParams* params, | |
58 ...) { | |
59 va_list vl; | |
60 va_start(vl, params); | |
61 bool result = VInvokeSrpcMethod(method_name, input_signature, params, vl); | |
62 va_end(vl); | |
63 return result; | |
64 } | |
65 | |
66 bool NaClSubprocess::VInvokeSrpcMethod(const std::string& method_name, | |
67 const std::string& input_signature, | |
68 SrpcParams* params, | |
69 va_list vl) { | |
70 if (NULL == srpc_client_.get()) { | |
71 PLUGIN_PRINTF(("VInvokeSrpcMethod (no srpc_client_)\n")); | |
72 return false; | |
73 } | |
74 if (!srpc_client_->HasMethod(method_name)) { | |
75 PLUGIN_PRINTF(("VInvokeSrpcMethod (no %s method found)\n", | |
76 method_name.c_str())); | |
77 return false; | |
78 } | |
79 if (!srpc_client_->InitParams(method_name, params)) { | |
80 PLUGIN_PRINTF(("VInvokeSrpcMethod (InitParams failed)\n")); | |
81 return false; | |
82 } | |
83 // Marshall inputs. | |
84 for (size_t i = 0; i < input_signature.length(); ++i) { | |
85 char c = input_signature[i]; | |
86 // Only handle the limited number of SRPC types used for PNaCl. | |
87 // Add more as needed. | |
88 switch (c) { | |
89 default: | |
90 PLUGIN_PRINTF(("PnaclSrpcLib::InvokeSrpcMethod unhandled type: %c\n", | |
91 c)); | |
92 return false; | |
93 case NACL_SRPC_ARG_TYPE_BOOL: { | |
94 int input = va_arg(vl, int); | |
95 params->ins()[i]->u.bval = input; | |
96 break; | |
97 } | |
98 case NACL_SRPC_ARG_TYPE_DOUBLE: { | |
99 double input = va_arg(vl, double); | |
100 params->ins()[i]->u.dval = input; | |
101 break; | |
102 } | |
103 case NACL_SRPC_ARG_TYPE_CHAR_ARRAY: { | |
104 // SrpcParam's destructor *should* free the allocated array | |
105 const char* orig_arr = va_arg(vl, const char*); | |
106 size_t len = va_arg(vl, size_t); | |
107 char* input = (char *)malloc(len); | |
108 if (!input) { | |
109 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n")); | |
110 return false; | |
111 } | |
112 memcpy(input, orig_arr, len); | |
113 params->ins()[i]->arrays.carr = input; | |
114 params->ins()[i]->u.count = static_cast<nacl_abi_size_t>(len); | |
115 break; | |
116 } | |
117 case NACL_SRPC_ARG_TYPE_HANDLE: { | |
118 NaClSrpcImcDescType input = va_arg(vl, NaClSrpcImcDescType); | |
119 params->ins()[i]->u.hval = input; | |
120 break; | |
121 } | |
122 case NACL_SRPC_ARG_TYPE_INT: { | |
123 int32_t input = va_arg(vl, int32_t); | |
124 params->ins()[i]->u.ival = input; | |
125 break; | |
126 } | |
127 case NACL_SRPC_ARG_TYPE_LONG: { | |
128 int64_t input = va_arg(vl, int64_t); | |
129 params->ins()[i]->u.lval = input; | |
130 break; | |
131 } | |
132 case NACL_SRPC_ARG_TYPE_STRING: { | |
133 // SrpcParam's destructor *should* free the dup'ed string. | |
134 const char* orig_str = va_arg(vl, const char*); | |
135 char* input = strdup(orig_str); | |
136 if (!input) { | |
137 PLUGIN_PRINTF(("VInvokeSrpcMethod (allocation failure)\n")); | |
138 return false; | |
139 } | |
140 params->ins()[i]->arrays.str = input; | |
141 break; | |
142 } | |
143 } | |
144 } | |
145 return srpc_client_->Invoke(method_name, params); | |
146 } | |
147 | |
148 } // namespace plugin | 44 } // namespace plugin |
OLD | NEW |