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_
14 #include "native_client/src/include/nacl_macros.h"
15 #include "native_client/src/include/nacl_scoped_ptr.h"
16 #include "native_client/src/shared/platform/nacl_sync.h"
17 #include "native_client/src/shared/srpc/nacl_srpc.h"
18 #include "native_client/src/trusted/reverse_service/reverse_service.h"
19 #include "native_client/src/trusted/weak_ref/weak_ref.h"
21 #include "ppapi/cpp/completion_callback.h"
22 #include "ppapi/native_client/src/trusted/plugin/utility.h"
30 class SelLdrLauncherChrome
;
34 // Struct of params used by StartSelLdr. Use a struct so that callback
35 // creation templates aren't overwhelmed with too many parameters.
36 struct SelLdrStartParams
{
37 SelLdrStartParams(const std::string
& url
,
38 const PP_NaClFileInfo
& file_info
,
39 PP_NaClAppProcessType process_type
)
42 process_type(process_type
) {
45 PP_NaClFileInfo file_info
;
46 PP_NaClAppProcessType process_type
;
49 // Callback resources are essentially our continuation state.
50 struct OpenManifestEntryResource
{
52 OpenManifestEntryResource(const std::string
& target_url
,
53 struct NaClFileInfo
* finfo
,
57 op_complete_ptr(op_complete
) {}
58 ~OpenManifestEntryResource();
61 struct NaClFileInfo
* file_info
;
62 PP_NaClFileInfo pp_file_info
;
63 bool* op_complete_ptr
;
66 // Do not invoke from the main thread, since the main methods will
67 // invoke CallOnMainThread and then wait on a condvar for the task to
68 // complete: if invoked from the main thread, the main method not
69 // returning (and thus unblocking the main thread) means that the
70 // main-thread continuation methods will never get called, and thus
71 // we'd get a deadlock.
72 class PluginReverseInterface
: public nacl::ReverseInterface
{
74 PluginReverseInterface(nacl::WeakRefAnchor
* anchor
,
75 PP_Instance pp_instance
,
76 ServiceRuntime
* service_runtime
,
77 pp::CompletionCallback init_done_cb
);
79 virtual ~PluginReverseInterface();
83 virtual void DoPostMessage(std::string message
);
85 virtual void StartupInitializationComplete();
87 virtual bool OpenManifestEntry(std::string url_key
,
88 struct NaClFileInfo
*info
);
90 virtual void ReportCrash();
92 virtual void ReportExitStatus(int exit_status
);
94 // TODO(teravest): Remove this method once it's gone from
95 // nacl::ReverseInterface.
96 virtual int64_t RequestQuotaForWrite(std::string file_id
,
98 int64_t bytes_to_write
);
101 virtual void OpenManifestEntry_MainThreadContinuation(
102 OpenManifestEntryResource
* p
,
105 virtual void StreamAsFile_MainThreadContinuation(
106 OpenManifestEntryResource
* p
,
110 nacl::WeakRefAnchor
* anchor_
; // holds a ref
111 // Should be used only in main thread in WeakRef-protected callbacks.
112 PP_Instance pp_instance_
;
113 ServiceRuntime
* service_runtime_
;
118 pp::CompletionCallback init_done_cb_
;
121 // ServiceRuntime abstracts a NativeClient sel_ldr instance.
122 class ServiceRuntime
{
124 ServiceRuntime(Plugin
* plugin
,
125 PP_Instance pp_instance
,
126 bool main_service_runtime
,
127 bool uses_nonsfi_mode
,
128 pp::CompletionCallback init_done_cb
);
129 // The destructor terminates the sel_ldr process.
132 // Spawn the sel_ldr instance.
133 void StartSelLdr(const SelLdrStartParams
& params
,
134 pp::CompletionCallback callback
);
136 // If starting sel_ldr from a background thread, wait for sel_ldr to
137 // actually start. Returns |false| if timed out waiting for the process
138 // to start. Otherwise, returns |true| if StartSelLdr is complete
139 // (either successfully or unsuccessfully).
140 bool WaitForSelLdrStart();
142 // Signal to waiting threads that StartSelLdr is complete (either
143 // successfully or unsuccessfully).
144 void SignalStartSelLdrDone();
146 // If starting the nexe from a background thread, wait for the nexe to
147 // actually start. Returns |true| is the nexe started successfully.
148 bool WaitForNexeStart();
150 // Signal to waiting threads that LoadNexeAndStart is complete (either
151 // successfully or unsuccessfully).
152 void SignalNexeStarted(bool ok
);
154 // Establish an SrpcClient to the sel_ldr instance and start the nexe.
155 // This function must be called on the main thread.
156 // This function must only be called once.
159 // Starts the application channel to the nexe.
160 SrpcClient
* SetupAppChannel();
162 bool RemoteLog(int severity
, const std::string
& msg
);
163 Plugin
* plugin() const { return plugin_
; }
166 bool main_service_runtime() const { return main_service_runtime_
; }
169 NACL_DISALLOW_COPY_AND_ASSIGN(ServiceRuntime
);
170 bool StartNexeInternal();
172 bool SetupCommandChannel();
173 bool InitReverseService();
177 void ReportLoadError(const ErrorInfo
& error_info
);
179 NaClSrpcChannel command_channel_
;
181 PP_Instance pp_instance_
;
182 bool main_service_runtime_
;
183 bool uses_nonsfi_mode_
;
184 nacl::ReverseService
* reverse_service_
;
185 nacl::scoped_ptr
<SelLdrLauncherChrome
> subprocess_
;
187 nacl::WeakRefAnchor
* anchor_
;
189 PluginReverseInterface
* rev_interface_
;
191 // Mutex and CondVar to protect start_sel_ldr_done_ and nexe_started_.
194 bool start_sel_ldr_done_
;
195 bool start_nexe_done_
;
196 bool nexe_started_ok_
;
198 NaClHandle bootstrap_channel_
;
201 } // namespace plugin
203 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_