OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #define NACL_LOG_MODULE_NAME "Plugin_ServiceRuntime" | 7 #define NACL_LOG_MODULE_NAME "Plugin_ServiceRuntime" |
8 | 8 |
9 #include "ppapi/native_client/src/trusted/plugin/service_runtime.h" | 9 #include "ppapi/native_client/src/trusted/plugin/service_runtime.h" |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "ppapi/native_client/src/trusted/plugin/plugin.h" | 44 #include "ppapi/native_client/src/trusted/plugin/plugin.h" |
45 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h" | 45 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h" |
46 #include "ppapi/native_client/src/trusted/plugin/pnacl_resources.h" | 46 #include "ppapi/native_client/src/trusted/plugin/pnacl_resources.h" |
47 #include "ppapi/native_client/src/trusted/plugin/sel_ldr_launcher_chrome.h" | 47 #include "ppapi/native_client/src/trusted/plugin/sel_ldr_launcher_chrome.h" |
48 #include "ppapi/native_client/src/trusted/plugin/srpc_client.h" | 48 #include "ppapi/native_client/src/trusted/plugin/srpc_client.h" |
49 #include "ppapi/native_client/src/trusted/plugin/utility.h" | 49 #include "ppapi/native_client/src/trusted/plugin/utility.h" |
50 #include "ppapi/native_client/src/trusted/weak_ref/call_on_main_thread.h" | 50 #include "ppapi/native_client/src/trusted/weak_ref/call_on_main_thread.h" |
51 | 51 |
52 namespace plugin { | 52 namespace plugin { |
53 | 53 |
54 class OpenManifestEntryAsyncCallback { | |
55 public: | |
56 OpenManifestEntryAsyncCallback(PP_OpenResourceCompletionCallback callback, | |
57 void* callback_user_data) | |
58 : callback_(callback), callback_user_data_(callback_user_data) { | |
59 } | |
60 | |
61 ~OpenManifestEntryAsyncCallback() { | |
62 if (callback_) | |
63 callback_(callback_user_data_, PP_kInvalidFileHandle); | |
64 } | |
65 | |
66 void Run(int32_t pp_error) { | |
67 #if defined(OS_WIN) | |
68 // Currently, this is used only for non-SFI mode, and now the mode is not | |
69 // supported on windows. | |
70 // TODO(hidehiko): Support it on Windows when we switch to use | |
71 // ManifestService also in SFI-mode. | |
72 NACL_NOTREACHED(); | |
73 #elif defined(OS_POSIX) | |
74 // On posix, PlatformFile is the file descriptor. | |
75 callback_(callback_user_data_, (pp_error == PP_OK) ? info_.desc : -1); | |
76 callback_ = NULL; | |
77 #endif | |
78 } | |
79 | |
80 NaClFileInfo* mutable_info() { return &info_; } | |
81 | |
82 private: | |
83 NaClFileInfo info_; | |
84 PP_OpenResourceCompletionCallback callback_; | |
85 void* callback_user_data_; | |
86 DISALLOW_COPY_AND_ASSIGN(OpenManifestEntryAsyncCallback); | |
87 }; | |
88 | |
89 namespace { | 54 namespace { |
90 | 55 |
91 class ManifestService { | 56 class ManifestService { |
92 public: | 57 public: |
93 ManifestService(nacl::WeakRefAnchor* anchor, | 58 ManifestService(nacl::WeakRefAnchor* anchor, |
94 PluginReverseInterface* plugin_reverse) | 59 PluginReverseInterface* plugin_reverse) |
95 : anchor_(anchor), | 60 : anchor_(anchor), |
96 plugin_reverse_(plugin_reverse) { | 61 plugin_reverse_(plugin_reverse) { |
97 } | 62 } |
98 | 63 |
(...skipping 10 matching lines...) Expand all Loading... |
109 // Release this instance if the ServiceRuntime is already destructed. | 74 // Release this instance if the ServiceRuntime is already destructed. |
110 if (anchor_->is_abandoned()) { | 75 if (anchor_->is_abandoned()) { |
111 delete this; | 76 delete this; |
112 return false; | 77 return false; |
113 } | 78 } |
114 | 79 |
115 plugin_reverse_->StartupInitializationComplete(); | 80 plugin_reverse_->StartupInitializationComplete(); |
116 return true; | 81 return true; |
117 } | 82 } |
118 | 83 |
119 bool OpenResource(const char* entry_key, | |
120 PP_OpenResourceCompletionCallback callback, | |
121 void* callback_user_data) { | |
122 // Release this instance if the ServiceRuntime is already destructed. | |
123 if (anchor_->is_abandoned()) { | |
124 callback(callback_user_data, PP_kInvalidFileHandle); | |
125 delete this; | |
126 return false; | |
127 } | |
128 | |
129 OpenManifestEntryAsyncCallback* open_manifest_callback = | |
130 new OpenManifestEntryAsyncCallback(callback, callback_user_data); | |
131 plugin_reverse_->OpenManifestEntryAsync( | |
132 entry_key, | |
133 open_manifest_callback->mutable_info(), | |
134 open_manifest_callback); | |
135 return true; | |
136 } | |
137 | |
138 static PP_Bool QuitTrampoline(void* user_data) { | 84 static PP_Bool QuitTrampoline(void* user_data) { |
139 return PP_FromBool(static_cast<ManifestService*>(user_data)->Quit()); | 85 return PP_FromBool(static_cast<ManifestService*>(user_data)->Quit()); |
140 } | 86 } |
141 | 87 |
142 static PP_Bool StartupInitializationCompleteTrampoline(void* user_data) { | 88 static PP_Bool StartupInitializationCompleteTrampoline(void* user_data) { |
143 return PP_FromBool(static_cast<ManifestService*>(user_data)-> | 89 return PP_FromBool(static_cast<ManifestService*>(user_data)-> |
144 StartupInitializationComplete()); | 90 StartupInitializationComplete()); |
145 } | 91 } |
146 | 92 |
147 static PP_Bool OpenResourceTrampoline( | |
148 void* user_data, | |
149 const char* entry_key, | |
150 PP_OpenResourceCompletionCallback callback, | |
151 void* callback_user_data) { | |
152 return PP_FromBool(static_cast<ManifestService*>(user_data)->OpenResource( | |
153 entry_key, callback, callback_user_data)); | |
154 } | |
155 | |
156 private: | 93 private: |
157 // Weak reference to check if plugin_reverse is legally accessible or not. | 94 // Weak reference to check if plugin_reverse is legally accessible or not. |
158 nacl::WeakRefAnchor* anchor_; | 95 nacl::WeakRefAnchor* anchor_; |
159 PluginReverseInterface* plugin_reverse_; | 96 PluginReverseInterface* plugin_reverse_; |
160 | 97 |
161 DISALLOW_COPY_AND_ASSIGN(ManifestService); | 98 DISALLOW_COPY_AND_ASSIGN(ManifestService); |
162 }; | 99 }; |
163 | 100 |
164 // Vtable to pass functions to LaunchSelLdr. | 101 // Vtable to pass functions to LaunchSelLdr. |
165 const PPP_ManifestService kManifestServiceVTable = { | 102 const PPP_ManifestService kManifestServiceVTable = { |
166 &ManifestService::QuitTrampoline, | 103 &ManifestService::QuitTrampoline, |
167 &ManifestService::StartupInitializationCompleteTrampoline, | 104 &ManifestService::StartupInitializationCompleteTrampoline, |
168 &ManifestService::OpenResourceTrampoline, | |
169 }; | 105 }; |
170 | 106 |
171 } // namespace | 107 } // namespace |
172 | 108 |
173 OpenManifestEntryResource::~OpenManifestEntryResource() { | 109 OpenManifestEntryResource::~OpenManifestEntryResource() { |
174 MaybeRunCallback(PP_ERROR_ABORTED); | |
175 } | |
176 | |
177 void OpenManifestEntryResource::MaybeRunCallback(int32_t pp_error) { | |
178 if (!callback) | |
179 return; | |
180 | |
181 callback->Run(pp_error); | |
182 delete callback; | |
183 callback = NULL; | |
184 } | 110 } |
185 | 111 |
186 PluginReverseInterface::PluginReverseInterface( | 112 PluginReverseInterface::PluginReverseInterface( |
187 nacl::WeakRefAnchor* anchor, | 113 nacl::WeakRefAnchor* anchor, |
188 Plugin* plugin, | 114 Plugin* plugin, |
189 ServiceRuntime* service_runtime, | 115 ServiceRuntime* service_runtime, |
190 pp::CompletionCallback init_done_cb, | 116 pp::CompletionCallback init_done_cb, |
191 pp::CompletionCallback crash_cb) | 117 pp::CompletionCallback crash_cb) |
192 : anchor_(anchor), | 118 : anchor_(anchor), |
193 plugin_(plugin), | 119 plugin_(plugin), |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // and invoke StreamAsFile with a completion callback that invokes | 162 // and invoke StreamAsFile with a completion callback that invokes |
237 // GetPOSIXFileDesc. | 163 // GetPOSIXFileDesc. |
238 bool PluginReverseInterface::OpenManifestEntry(nacl::string url_key, | 164 bool PluginReverseInterface::OpenManifestEntry(nacl::string url_key, |
239 struct NaClFileInfo* info) { | 165 struct NaClFileInfo* info) { |
240 bool op_complete = false; // NB: mu_ and cv_ also controls access to this! | 166 bool op_complete = false; // NB: mu_ and cv_ also controls access to this! |
241 // The to_open object is owned by the weak ref callback. Because this function | 167 // The to_open object is owned by the weak ref callback. Because this function |
242 // waits for the callback to finish, the to_open object will be deallocated on | 168 // waits for the callback to finish, the to_open object will be deallocated on |
243 // the main thread before this function can return. The pointers it contains | 169 // the main thread before this function can return. The pointers it contains |
244 // to stack variables will not leak. | 170 // to stack variables will not leak. |
245 OpenManifestEntryResource* to_open = | 171 OpenManifestEntryResource* to_open = |
246 new OpenManifestEntryResource(url_key, info, &op_complete, NULL); | 172 new OpenManifestEntryResource(url_key, info, &op_complete); |
247 CHECK(to_open != NULL); | 173 CHECK(to_open != NULL); |
248 NaClLog(4, "PluginReverseInterface::OpenManifestEntry: %s\n", | 174 NaClLog(4, "PluginReverseInterface::OpenManifestEntry: %s\n", |
249 url_key.c_str()); | 175 url_key.c_str()); |
250 // This assumes we are not on the main thread. If false, we deadlock. | 176 // This assumes we are not on the main thread. If false, we deadlock. |
251 plugin::WeakRefCallOnMainThread( | 177 plugin::WeakRefCallOnMainThread( |
252 anchor_, | 178 anchor_, |
253 0, | 179 0, |
254 this, | 180 this, |
255 &plugin::PluginReverseInterface::OpenManifestEntry_MainThreadContinuation, | 181 &plugin::PluginReverseInterface::OpenManifestEntry_MainThreadContinuation, |
256 to_open); | 182 to_open); |
(...skipping 29 matching lines...) Expand all Loading... |
286 if (info->desc == -1) { | 212 if (info->desc == -1) { |
287 // TODO(bsy,ncbray): what else should we do with the error? This | 213 // TODO(bsy,ncbray): what else should we do with the error? This |
288 // is a runtime error that may simply be a programming error in | 214 // is a runtime error that may simply be a programming error in |
289 // the untrusted code, or it may be something else wrong w/ the | 215 // the untrusted code, or it may be something else wrong w/ the |
290 // manifest. | 216 // manifest. |
291 NaClLog(4, "OpenManifestEntry: failed for key %s", url_key.c_str()); | 217 NaClLog(4, "OpenManifestEntry: failed for key %s", url_key.c_str()); |
292 } | 218 } |
293 return true; | 219 return true; |
294 } | 220 } |
295 | 221 |
296 void PluginReverseInterface::OpenManifestEntryAsync( | |
297 const nacl::string& entry_key, | |
298 struct NaClFileInfo* info, | |
299 OpenManifestEntryAsyncCallback* callback) { | |
300 bool op_complete = false; | |
301 OpenManifestEntryResource to_open( | |
302 entry_key, info, &op_complete, callback); | |
303 OpenManifestEntry_MainThreadContinuation(&to_open, PP_OK); | |
304 } | |
305 | |
306 // Transfer point from OpenManifestEntry() which runs on the main thread | 222 // Transfer point from OpenManifestEntry() which runs on the main thread |
307 // (Some PPAPI actions -- like StreamAsFile -- can only run on the main thread). | 223 // (Some PPAPI actions -- like StreamAsFile -- can only run on the main thread). |
308 // OpenManifestEntry() is waiting on a condvar for this continuation to | 224 // OpenManifestEntry() is waiting on a condvar for this continuation to |
309 // complete. We Broadcast and awaken OpenManifestEntry() whenever we are done | 225 // complete. We Broadcast and awaken OpenManifestEntry() whenever we are done |
310 // either here, or in a later MainThreadContinuation step, if there are | 226 // either here, or in a later MainThreadContinuation step, if there are |
311 // multiple steps. | 227 // multiple steps. |
312 void PluginReverseInterface::OpenManifestEntry_MainThreadContinuation( | 228 void PluginReverseInterface::OpenManifestEntry_MainThreadContinuation( |
313 OpenManifestEntryResource* p, | 229 OpenManifestEntryResource* p, |
314 int32_t err) { | 230 int32_t err) { |
315 UNREFERENCED_PARAMETER(err); | 231 UNREFERENCED_PARAMETER(err); |
(...skipping 11 matching lines...) Expand all Loading... |
327 &pnacl_options)) { | 243 &pnacl_options)) { |
328 NaClLog(4, "OpenManifestEntry_MainThreadContinuation: ResolveKey failed\n"); | 244 NaClLog(4, "OpenManifestEntry_MainThreadContinuation: ResolveKey failed\n"); |
329 // Failed, and error_info has the details on what happened. Wake | 245 // Failed, and error_info has the details on what happened. Wake |
330 // up requesting thread -- we are done. | 246 // up requesting thread -- we are done. |
331 { | 247 { |
332 nacl::MutexLocker take(&mu_); | 248 nacl::MutexLocker take(&mu_); |
333 *p->op_complete_ptr = true; // done... | 249 *p->op_complete_ptr = true; // done... |
334 p->file_info->desc = -1; // but failed. | 250 p->file_info->desc = -1; // but failed. |
335 NaClXCondVarBroadcast(&cv_); | 251 NaClXCondVarBroadcast(&cv_); |
336 } | 252 } |
337 p->MaybeRunCallback(PP_OK); | |
338 return; | 253 return; |
339 } | 254 } |
340 nacl::string mapped_url = pp::Var(pp_mapped_url).AsString(); | 255 nacl::string mapped_url = pp::Var(pp_mapped_url).AsString(); |
341 NaClLog(4, | 256 NaClLog(4, |
342 "OpenManifestEntry_MainThreadContinuation: " | 257 "OpenManifestEntry_MainThreadContinuation: " |
343 "ResolveKey: %s -> %s (pnacl_translate(%d))\n", | 258 "ResolveKey: %s -> %s (pnacl_translate(%d))\n", |
344 p->url.c_str(), mapped_url.c_str(), pnacl_options.translate); | 259 p->url.c_str(), mapped_url.c_str(), pnacl_options.translate); |
345 | 260 |
346 if (pnacl_options.translate) { | 261 if (pnacl_options.translate) { |
347 // Requires PNaCl translation, but that's not supported. | 262 // Requires PNaCl translation, but that's not supported. |
348 NaClLog(4, | 263 NaClLog(4, |
349 "OpenManifestEntry_MainThreadContinuation: " | 264 "OpenManifestEntry_MainThreadContinuation: " |
350 "Requires PNaCl translation -- not supported\n"); | 265 "Requires PNaCl translation -- not supported\n"); |
351 { | 266 { |
352 nacl::MutexLocker take(&mu_); | 267 nacl::MutexLocker take(&mu_); |
353 *p->op_complete_ptr = true; // done... | 268 *p->op_complete_ptr = true; // done... |
354 p->file_info->desc = -1; // but failed. | 269 p->file_info->desc = -1; // but failed. |
355 NaClXCondVarBroadcast(&cv_); | 270 NaClXCondVarBroadcast(&cv_); |
356 } | 271 } |
357 p->MaybeRunCallback(PP_OK); | |
358 return; | 272 return; |
359 } | 273 } |
360 | 274 |
361 // Because p is owned by the callback of this invocation, so it is necessary | 275 // Because p is owned by the callback of this invocation, so it is necessary |
362 // to create another instance. | 276 // to create another instance. |
363 OpenManifestEntryResource* open_cont = new OpenManifestEntryResource(*p); | 277 OpenManifestEntryResource* open_cont = new OpenManifestEntryResource(*p); |
364 open_cont->url = mapped_url; | 278 open_cont->url = mapped_url; |
365 // Callback is now delegated from p to open_cont. So, here we manually clear | |
366 // complete callback. | |
367 p->callback = NULL; | |
368 | 279 |
369 pp::CompletionCallback stream_cc = WeakRefNewCallback( | 280 pp::CompletionCallback stream_cc = WeakRefNewCallback( |
370 anchor_, | 281 anchor_, |
371 this, | 282 this, |
372 &PluginReverseInterface::StreamAsFile_MainThreadContinuation, | 283 &PluginReverseInterface::StreamAsFile_MainThreadContinuation, |
373 open_cont); | 284 open_cont); |
374 | 285 |
375 GetNaClInterface()->DownloadFile(plugin_->pp_instance(), | 286 GetNaClInterface()->DownloadFile(plugin_->pp_instance(), |
376 mapped_url.c_str(), | 287 mapped_url.c_str(), |
377 &open_cont->pp_file_info, | 288 &open_cont->pp_file_info, |
(...skipping 18 matching lines...) Expand all Loading... |
396 p->file_info->desc); | 307 p->file_info->desc); |
397 } else { | 308 } else { |
398 NaClLog( | 309 NaClLog( |
399 4, | 310 4, |
400 "StreamAsFile_MainThreadContinuation: !PP_OK, setting desc -1\n"); | 311 "StreamAsFile_MainThreadContinuation: !PP_OK, setting desc -1\n"); |
401 p->file_info->desc = -1; | 312 p->file_info->desc = -1; |
402 } | 313 } |
403 *p->op_complete_ptr = true; | 314 *p->op_complete_ptr = true; |
404 NaClXCondVarBroadcast(&cv_); | 315 NaClXCondVarBroadcast(&cv_); |
405 } | 316 } |
406 p->MaybeRunCallback(PP_OK); | |
407 } | 317 } |
408 | 318 |
409 bool PluginReverseInterface::CloseManifestEntry(int32_t desc) { | 319 bool PluginReverseInterface::CloseManifestEntry(int32_t desc) { |
410 // We don't take any action on a call to CloseManifestEntry today, so always | 320 // We don't take any action on a call to CloseManifestEntry today, so always |
411 // return success. | 321 // return success. |
412 return true; | 322 return true; |
413 } | 323 } |
414 | 324 |
415 void PluginReverseInterface::ReportCrash() { | 325 void PluginReverseInterface::ReportCrash() { |
416 NaClLog(4, "PluginReverseInterface::ReportCrash\n"); | 326 NaClLog(4, "PluginReverseInterface::ReportCrash\n"); |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 | 769 |
860 nacl::string ServiceRuntime::GetCrashLogOutput() { | 770 nacl::string ServiceRuntime::GetCrashLogOutput() { |
861 if (NULL != subprocess_.get()) { | 771 if (NULL != subprocess_.get()) { |
862 return subprocess_->GetCrashLogOutput(); | 772 return subprocess_->GetCrashLogOutput(); |
863 } else { | 773 } else { |
864 return std::string(); | 774 return std::string(); |
865 } | 775 } |
866 } | 776 } |
867 | 777 |
868 } // namespace plugin | 778 } // namespace plugin |
OLD | NEW |