OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2008 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can | |
4 * be found in the LICENSE file. | |
5 */ | |
6 | |
7 | |
8 #include <string.h> | |
9 | |
10 #include "native_client/src/trusted/plugin/srpc/srt_socket.h" | |
11 | |
12 #include "native_client/src/include/nacl_string.h" | |
13 #include "native_client/src/trusted/plugin/srpc/browser_interface.h" | |
14 #include "native_client/src/trusted/plugin/srpc/plugin.h" | |
15 #include "native_client/src/trusted/plugin/srpc/service_runtime.h" | |
16 #include "native_client/src/trusted/plugin/srpc/srpc_client.h" | |
17 #include "native_client/src/trusted/plugin/srpc/utility.h" | |
18 | |
19 namespace { | |
20 | |
21 // Not really constants. Do not modify. Use only after at least | |
22 // one SrtSocket instance has been constructed. | |
23 uintptr_t kSetOriginIdent; | |
24 uintptr_t kStartModuleIdent; | |
25 uintptr_t kLogIdent; | |
26 uintptr_t kLoadModule; | |
27 uintptr_t kInitHandlePassing; | |
28 | |
29 // NB: InitializeIdentifiers is not thread-safe. | |
30 void InitializeIdentifiers(plugin::BrowserInterface* browser_interface) { | |
31 static bool initialized = false; | |
32 | |
33 if (!initialized) { // branch likely | |
34 kSetOriginIdent = | |
35 browser_interface->StringToIdentifier("set_origin"); | |
36 kStartModuleIdent = | |
37 browser_interface->StringToIdentifier("start_module"); | |
38 kLogIdent = browser_interface->StringToIdentifier("log"); | |
39 kLoadModule = | |
40 browser_interface->StringToIdentifier("load_module"); | |
41 kInitHandlePassing = | |
42 browser_interface->StringToIdentifier("init_handle_passing"); | |
43 initialized = true; | |
44 } | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 namespace plugin { | |
50 | |
51 SrtSocket::SrtSocket(ScriptableHandle* s, BrowserInterface* browser_interface) | |
52 : connected_socket_(s), | |
53 browser_interface_(browser_interface) { | |
54 connected_socket_->AddRef(); | |
55 InitializeIdentifiers(browser_interface_); // inlineable tail call. | |
56 } | |
57 | |
58 SrtSocket::~SrtSocket() { | |
59 connected_socket_->Unref(); | |
60 } | |
61 | |
62 bool SrtSocket::SetOrigin(nacl::string origin) { | |
63 if (!(connected_socket()->HasMethod(kSetOriginIdent, METHOD_CALL))) { | |
64 PLUGIN_PRINTF(("No set_origin method was found\n")); | |
65 return false; | |
66 } | |
67 SrpcParams params; | |
68 bool success; | |
69 success = connected_socket()->InitParams(kSetOriginIdent, | |
70 METHOD_CALL, | |
71 ¶ms); | |
72 if (!success) { | |
73 return false; | |
74 } | |
75 | |
76 params.ins()[0]->u.sval = strdup(origin.c_str()); | |
77 bool rpc_result = (connected_socket()->Invoke(kSetOriginIdent, | |
78 METHOD_CALL, | |
79 ¶ms)); | |
80 return rpc_result; | |
81 } | |
82 | |
83 | |
84 bool SrtSocket::LoadModule(NaClSrpcImcDescType desc) { | |
85 if (!(connected_socket()->HasMethod(kLoadModule, METHOD_CALL))) { | |
86 PLUGIN_PRINTF(("No load_module method was found\n")); | |
87 return false; | |
88 } | |
89 SrpcParams params; | |
90 bool success; | |
91 success = connected_socket()->InitParams(kLoadModule, | |
92 METHOD_CALL, | |
93 ¶ms); | |
94 if (!success) { | |
95 return false; | |
96 } | |
97 | |
98 params.ins()[0]->u.hval = desc; | |
99 | |
100 bool rpc_result = connected_socket()->Invoke(kLoadModule, | |
101 METHOD_CALL, | |
102 ¶ms); | |
103 return rpc_result; | |
104 } | |
105 #if NACL_WINDOWS && !defined(NACL_STANDALONE) | |
106 bool SrtSocket::InitHandlePassing(NaClDesc* desc, | |
107 nacl::Handle sel_ldr_handle) { | |
108 if (!(connected_socket()->HasMethod(kInitHandlePassing, METHOD_CALL))) { | |
109 PLUGIN_PRINTF(("No load_module method was found\n")); | |
110 return false; | |
111 } | |
112 SrpcParams params; | |
113 bool success; | |
114 DWORD my_pid = GetCurrentProcessId(); | |
115 nacl::Handle my_handle = GetCurrentProcess(); | |
116 nacl::Handle my_handle_in_selldr; | |
117 | |
118 if (!DuplicateHandle(GetCurrentProcess(), | |
119 my_handle, | |
120 sel_ldr_handle, | |
121 &my_handle_in_selldr, | |
122 PROCESS_DUP_HANDLE, | |
123 FALSE, | |
124 0)) { | |
125 return false; | |
126 } | |
127 | |
128 success = connected_socket()->InitParams(kInitHandlePassing, | |
129 METHOD_CALL, | |
130 ¶ms); | |
131 if (!success) { | |
132 return false; | |
133 } | |
134 | |
135 params.ins()[0]->u.hval = desc; | |
136 params.ins()[1]->u.ival = my_pid; | |
137 params.ins()[2]->u.ival = reinterpret_cast<int>(my_handle_in_selldr); | |
138 | |
139 bool rpc_result = connected_socket()->Invoke(kInitHandlePassing, | |
140 METHOD_CALL, | |
141 ¶ms); | |
142 return rpc_result; | |
143 } | |
144 #endif | |
145 | |
146 // make the start_module rpc | |
147 bool SrtSocket::StartModule(int *load_status) { | |
148 if (!(connected_socket()->HasMethod(kStartModuleIdent, METHOD_CALL))) { | |
149 PLUGIN_PRINTF(("No start_module method was found\n")); | |
150 return false; | |
151 } | |
152 SrpcParams params; | |
153 bool success; | |
154 success = connected_socket()->InitParams(kStartModuleIdent, | |
155 METHOD_CALL, | |
156 ¶ms); | |
157 if (!success) { | |
158 return false; | |
159 } | |
160 bool rpc_result = connected_socket()->Invoke(kStartModuleIdent, | |
161 METHOD_CALL, | |
162 ¶ms); | |
163 if (rpc_result) { | |
164 if (NACL_SRPC_ARG_TYPE_INT == params.outs()[0]->tag) { | |
165 int status = params.outs()[0]->u.ival; | |
166 PLUGIN_PRINTF(("StartModule: start_module RPC returned status code %d\n", | |
167 status)); | |
168 if (NULL != load_status) { | |
169 *load_status = status; | |
170 } | |
171 } | |
172 } | |
173 return rpc_result; | |
174 } | |
175 | |
176 bool SrtSocket::Log(int severity, nacl::string msg) { | |
177 if (!connected_socket()->HasMethod(kLogIdent, METHOD_CALL)) { | |
178 PLUGIN_PRINTF(("No log method was found\n")); | |
179 return false; | |
180 } | |
181 SrpcParams params; | |
182 bool success; | |
183 success = connected_socket()->InitParams(kLogIdent, | |
184 METHOD_CALL, | |
185 ¶ms); | |
186 if (!success) { | |
187 return false; | |
188 } | |
189 params.ins()[0]->u.ival = severity; | |
190 params.ins()[1]->u.sval = strdup(msg.c_str()); | |
191 bool rpc_result = (connected_socket()->Invoke(kLogIdent, | |
192 METHOD_CALL, | |
193 ¶ms)); | |
194 return rpc_result; | |
195 } | |
196 | |
197 } // namespace plugin | |
OLD | NEW |