Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / ppapi / native_client / src / trusted / plugin / service_runtime.h
1 /* -*- c++ -*- */
2 /*
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.
6  */
7
8 // A class containing information regarding a socket connection to a
9 // service runtime instance.
10
11 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_
12 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_
13
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"
20
21 #include "ppapi/cpp/completion_callback.h"
22 #include "ppapi/native_client/src/trusted/plugin/utility.h"
23
24 struct NaClFileInfo;
25
26 namespace plugin {
27
28 class ErrorInfo;
29 class Plugin;
30 class SelLdrLauncherChrome;
31 class SrpcClient;
32 class ServiceRuntime;
33
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)
40       : url(url),
41         file_info(file_info),
42         process_type(process_type) {
43   }
44   std::string url;
45   PP_NaClFileInfo file_info;
46   PP_NaClAppProcessType process_type;
47 };
48
49 // Callback resources are essentially our continuation state.
50 struct OpenManifestEntryResource {
51  public:
52   OpenManifestEntryResource(const std::string& target_url,
53                             struct NaClFileInfo* finfo,
54                             bool* op_complete)
55       : url(target_url),
56         file_info(finfo),
57         op_complete_ptr(op_complete) {}
58   ~OpenManifestEntryResource();
59
60   std::string url;
61   struct NaClFileInfo* file_info;
62   PP_NaClFileInfo pp_file_info;
63   bool* op_complete_ptr;
64 };
65
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 {
73  public:
74   PluginReverseInterface(nacl::WeakRefAnchor* anchor,
75                          PP_Instance pp_instance,
76                          ServiceRuntime* service_runtime,
77                          pp::CompletionCallback init_done_cb);
78
79   virtual ~PluginReverseInterface();
80
81   void ShutDown();
82
83   virtual void DoPostMessage(std::string message);
84
85   virtual void StartupInitializationComplete();
86
87   virtual bool OpenManifestEntry(std::string url_key,
88                                  struct NaClFileInfo *info);
89
90   virtual void ReportCrash();
91
92   virtual void ReportExitStatus(int exit_status);
93
94   // TODO(teravest): Remove this method once it's gone from
95   // nacl::ReverseInterface.
96   virtual int64_t RequestQuotaForWrite(std::string file_id,
97                                        int64_t offset,
98                                        int64_t bytes_to_write);
99
100  protected:
101   virtual void OpenManifestEntry_MainThreadContinuation(
102       OpenManifestEntryResource* p,
103       int32_t err);
104
105   virtual void StreamAsFile_MainThreadContinuation(
106       OpenManifestEntryResource* p,
107       int32_t result);
108
109  private:
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_;
114   NaClMutex mu_;
115   NaClCondVar cv_;
116   bool shutting_down_;
117
118   pp::CompletionCallback init_done_cb_;
119 };
120
121 //  ServiceRuntime abstracts a NativeClient sel_ldr instance.
122 class ServiceRuntime {
123  public:
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.
130   ~ServiceRuntime();
131
132   // Spawn the sel_ldr instance.
133   void StartSelLdr(const SelLdrStartParams& params,
134                    pp::CompletionCallback callback);
135
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();
141
142   // Signal to waiting threads that StartSelLdr is complete (either
143   // successfully or unsuccessfully).
144   void SignalStartSelLdrDone();
145
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();
149
150   // Signal to waiting threads that LoadNexeAndStart is complete (either
151   // successfully or unsuccessfully).
152   void SignalNexeStarted(bool ok);
153
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.
157   void StartNexe();
158
159   // Starts the application channel to the nexe.
160   SrpcClient* SetupAppChannel();
161
162   bool RemoteLog(int severity, const std::string& msg);
163   Plugin* plugin() const { return plugin_; }
164   void Shutdown();
165
166   bool main_service_runtime() const { return main_service_runtime_; }
167
168  private:
169   NACL_DISALLOW_COPY_AND_ASSIGN(ServiceRuntime);
170   bool StartNexeInternal();
171
172   bool SetupCommandChannel();
173   bool InitReverseService();
174   bool StartModule();
175   void ReapLogs();
176
177   void ReportLoadError(const ErrorInfo& error_info);
178
179   NaClSrpcChannel command_channel_;
180   Plugin* plugin_;
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_;
186
187   nacl::WeakRefAnchor* anchor_;
188
189   PluginReverseInterface* rev_interface_;
190
191   // Mutex and CondVar to protect start_sel_ldr_done_ and nexe_started_.
192   NaClMutex mu_;
193   NaClCondVar cond_;
194   bool start_sel_ldr_done_;
195   bool start_nexe_done_;
196   bool nexe_started_ok_;
197
198   NaClHandle bootstrap_channel_;
199 };
200
201 }  // namespace plugin
202
203 #endif  // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SERVICE_RUNTIME_H_