3 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // A class containing information regarding a socket connection to a
9 // service runtime instance.
11 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_
12 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_
16 #include "native_client/src/include/nacl_macros.h"
17 #include "native_client/src/include/nacl_scoped_ptr.h"
18 #include "native_client/src/include/nacl_string.h"
19 #include "native_client/src/shared/platform/nacl_sync.h"
20 #include "native_client/src/shared/srpc/nacl_srpc.h"
21 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
22 #include "native_client/src/trusted/nonnacl_util/sel_ldr_launcher.h"
23 #include "native_client/src/trusted/reverse_service/reverse_service.h"
24 #include "native_client/src/trusted/weak_ref/weak_ref.h"
26 #include "ppapi/cpp/completion_callback.h"
27 #include "ppapi/native_client/src/trusted/plugin/utility.h"
39 class SelLdrLauncherChrome
;
43 // Struct of params used by StartSelLdr. Use a struct so that callback
44 // creation templates aren't overwhelmed with too many parameters.
45 struct SelLdrStartParams
{
46 SelLdrStartParams(const nacl::string
& url
,
47 const PP_NaClFileInfo
& file_info
,
50 bool enable_dyncode_syscalls
,
51 bool enable_exception_handling
,
52 bool enable_crash_throttling
)
56 uses_ppapi(uses_ppapi
),
57 enable_dyncode_syscalls(enable_dyncode_syscalls
),
58 enable_exception_handling(enable_exception_handling
),
59 enable_crash_throttling(enable_crash_throttling
) {
62 PP_NaClFileInfo file_info
;
65 bool enable_dev_interfaces
;
66 bool enable_dyncode_syscalls
;
67 bool enable_exception_handling
;
68 bool enable_crash_throttling
;
71 // Callback resources are essentially our continuation state.
72 struct OpenManifestEntryResource
{
74 OpenManifestEntryResource(const std::string
& target_url
,
75 struct NaClFileInfo
* finfo
,
79 op_complete_ptr(op_complete
) {}
80 ~OpenManifestEntryResource();
83 struct NaClFileInfo
* file_info
;
84 PP_NaClFileInfo pp_file_info
;
85 bool* op_complete_ptr
;
88 // Do not invoke from the main thread, since the main methods will
89 // invoke CallOnMainThread and then wait on a condvar for the task to
90 // complete: if invoked from the main thread, the main method not
91 // returning (and thus unblocking the main thread) means that the
92 // main-thread continuation methods will never get called, and thus
93 // we'd get a deadlock.
94 class PluginReverseInterface
: public nacl::ReverseInterface
{
96 PluginReverseInterface(nacl::WeakRefAnchor
* anchor
,
97 PP_Instance pp_instance
,
98 ServiceRuntime
* service_runtime
,
99 pp::CompletionCallback init_done_cb
,
100 pp::CompletionCallback crash_cb
);
102 virtual ~PluginReverseInterface();
106 virtual void DoPostMessage(nacl::string message
);
108 virtual void StartupInitializationComplete();
110 virtual bool OpenManifestEntry(nacl::string url_key
,
111 struct NaClFileInfo
*info
);
113 virtual void ReportCrash();
115 virtual void ReportExitStatus(int exit_status
);
117 // TODO(teravest): Remove this method once it's gone from
118 // nacl::ReverseInterface.
119 virtual int64_t RequestQuotaForWrite(nacl::string file_id
,
121 int64_t bytes_to_write
);
124 virtual void OpenManifestEntry_MainThreadContinuation(
125 OpenManifestEntryResource
* p
,
128 virtual void StreamAsFile_MainThreadContinuation(
129 OpenManifestEntryResource
* p
,
133 nacl::WeakRefAnchor
* anchor_
; // holds a ref
134 // Should be used only in main thread in WeakRef-protected callbacks.
135 PP_Instance pp_instance_
;
136 ServiceRuntime
* service_runtime_
;
141 pp::CompletionCallback init_done_cb_
;
142 pp::CompletionCallback crash_cb_
;
145 // ServiceRuntime abstracts a NativeClient sel_ldr instance.
146 class ServiceRuntime
{
148 // TODO(sehr): This class should also implement factory methods, using the
149 // Start method below.
150 ServiceRuntime(Plugin
* plugin
,
151 PP_Instance pp_instance
,
152 bool main_service_runtime
,
153 bool uses_nonsfi_mode
,
154 pp::CompletionCallback init_done_cb
,
155 pp::CompletionCallback crash_cb
);
156 // The destructor terminates the sel_ldr process.
159 // Spawn the sel_ldr instance.
160 void StartSelLdr(const SelLdrStartParams
& params
,
161 pp::CompletionCallback callback
);
163 // If starting sel_ldr from a background thread, wait for sel_ldr to
164 // actually start. Returns |false| if timed out waiting for the process
165 // to start. Otherwise, returns |true| if StartSelLdr is complete
166 // (either successfully or unsuccessfully).
167 bool WaitForSelLdrStart();
169 // Signal to waiting threads that StartSelLdr is complete (either
170 // successfully or unsuccessfully).
171 void SignalStartSelLdrDone();
173 // If starting the nexe from a background thread, wait for the nexe to
174 // actually start. Returns |true| is the nexe started successfully.
175 bool WaitForNexeStart();
177 // Signal to waiting threads that LoadNexeAndStart is complete (either
178 // successfully or unsuccessfully).
179 void SignalNexeStarted(bool ok
);
181 // Establish an SrpcClient to the sel_ldr instance and start the nexe.
182 // This function must be called on the main thread.
183 // This function must only be called once.
186 // Starts the application channel to the nexe.
187 SrpcClient
* SetupAppChannel();
189 bool RemoteLog(int severity
, const nacl::string
& msg
);
190 Plugin
* plugin() const { return plugin_
; }
193 bool main_service_runtime() const { return main_service_runtime_
; }
196 NACL_DISALLOW_COPY_AND_ASSIGN(ServiceRuntime
);
197 bool StartNexeInternal();
199 bool SetupCommandChannel();
200 bool InitReverseService();
204 void ReportLoadError(const ErrorInfo
& error_info
);
206 NaClSrpcChannel command_channel_
;
208 PP_Instance pp_instance_
;
209 bool main_service_runtime_
;
210 bool uses_nonsfi_mode_
;
211 nacl::ReverseService
* reverse_service_
;
212 nacl::scoped_ptr
<SelLdrLauncherChrome
> subprocess_
;
214 nacl::WeakRefAnchor
* anchor_
;
216 PluginReverseInterface
* rev_interface_
;
218 // Mutex and CondVar to protect start_sel_ldr_done_ and nexe_started_.
221 bool start_sel_ldr_done_
;
222 bool start_nexe_done_
;
223 bool nexe_started_ok_
;
225 NaClHandle bootstrap_channel_
;
228 } // namespace plugin
230 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_