OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "native_client/src/trusted/plugin/pnacl_coordinator.h" | 5 #include "native_client/src/trusted/plugin/pnacl_coordinator.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "native_client/src/include/portability_io.h" | 10 #include "native_client/src/include/portability_io.h" |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 translate_notify_callback_(translate_notify_callback), | 232 translate_notify_callback_(translate_notify_callback), |
233 file_system_(new pp::FileSystem(plugin, PP_FILESYSTEMTYPE_LOCALTEMPORARY)), | 233 file_system_(new pp::FileSystem(plugin, PP_FILESYSTEMTYPE_LOCALTEMPORARY)), |
234 manifest_(new ExtensionManifest(plugin->url_util())), | 234 manifest_(new ExtensionManifest(plugin->url_util())), |
235 pexe_url_(pexe_url), | 235 pexe_url_(pexe_url), |
236 cache_identity_(cache_identity), | 236 cache_identity_(cache_identity), |
237 error_already_reported_(false) { | 237 error_already_reported_(false) { |
238 PLUGIN_PRINTF(("PnaclCoordinator::PnaclCoordinator (this=%p, plugin=%p)\n", | 238 PLUGIN_PRINTF(("PnaclCoordinator::PnaclCoordinator (this=%p, plugin=%p)\n", |
239 static_cast<void*>(this), static_cast<void*>(plugin))); | 239 static_cast<void*>(this), static_cast<void*>(plugin))); |
240 callback_factory_.Initialize(this); | 240 callback_factory_.Initialize(this); |
241 ld_manifest_.reset(new PnaclLDManifest(plugin_->manifest(), manifest_.get())); | 241 ld_manifest_.reset(new PnaclLDManifest(plugin_->manifest(), manifest_.get())); |
242 // TODO(dschuff): stream ALL the translations! | |
243 do_streaming_translation_ = (getenv("NACL_STREAMING_TRANSLATION") != NULL); | |
244 } | 242 } |
245 | 243 |
246 PnaclCoordinator::~PnaclCoordinator() { | 244 PnaclCoordinator::~PnaclCoordinator() { |
247 PLUGIN_PRINTF(("PnaclCoordinator::~PnaclCoordinator (this=%p, " | 245 PLUGIN_PRINTF(("PnaclCoordinator::~PnaclCoordinator (this=%p, " |
248 "translate_thread=%p\n", | 246 "translate_thread=%p\n", |
249 static_cast<void*>(this), translate_thread_.get())); | 247 static_cast<void*>(this), translate_thread_.get())); |
250 // Stopping the translate thread will cause the translate thread to try to | 248 // Stopping the translate thread will cause the translate thread to try to |
251 // run translation_complete_callback_ on the main thread. This destructor is | 249 // run translation_complete_callback_ on the main thread. This destructor is |
252 // running from the main thread, and by the time it exits, callback_factory_ | 250 // running from the main thread, and by the time it exits, callback_factory_ |
253 // will have been destroyed. This will result in the cancellation of | 251 // will have been destroyed. This will result in the cancellation of |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 454 } |
457 } | 455 } |
458 | 456 |
459 void PnaclCoordinator::CachedFileDidOpen(int32_t pp_error) { | 457 void PnaclCoordinator::CachedFileDidOpen(int32_t pp_error) { |
460 PLUGIN_PRINTF(("PnaclCoordinator::CachedFileDidOpen (pp_error=%" | 458 PLUGIN_PRINTF(("PnaclCoordinator::CachedFileDidOpen (pp_error=%" |
461 NACL_PRId32")\n", pp_error)); | 459 NACL_PRId32")\n", pp_error)); |
462 if (pp_error == PP_OK) { | 460 if (pp_error == PP_OK) { |
463 NexeReadDidOpen(PP_OK); | 461 NexeReadDidOpen(PP_OK); |
464 return; | 462 return; |
465 } | 463 } |
466 if (do_streaming_translation_) { | 464 |
467 // Create the translation thread object immediately. This ensures that any | 465 // Create the translation thread object immediately. This ensures that any |
468 // pieces of the file that get downloaded before the compilation thread | 466 // pieces of the file that get downloaded before the compilation thread |
469 // is accepting SRPCs won't get dropped. | 467 // is accepting SRPCs won't get dropped. |
470 translate_thread_.reset(new PnaclStreamingTranslateThread()); | 468 translate_thread_.reset(new PnaclStreamingTranslateThread()); |
471 if (translate_thread_ == NULL) { | 469 if (translate_thread_ == NULL) { |
472 ReportNonPpapiError("could not allocate translation thread."); | 470 ReportNonPpapiError("could not allocate translation thread."); |
473 return; | 471 return; |
474 } | 472 } |
475 // In the streaming case we also want to open the object file now so the | 473 // In the streaming case we also want to open the object file now so the |
476 // translator can start writing to it during streaming translation. | 474 // translator can start writing to it during streaming translation. |
477 // In the non-streaming case this can wait until the bitcode download is | 475 // In the non-streaming case this can wait until the bitcode download is |
478 // finished. | 476 // finished. |
479 obj_file_.reset(new LocalTempFile(plugin_, file_system_.get(), | 477 obj_file_.reset(new LocalTempFile(plugin_, file_system_.get(), |
480 nacl::string(kPnaclTempDir))); | 478 nacl::string(kPnaclTempDir))); |
481 pp::CompletionCallback obj_cb = | 479 pp::CompletionCallback obj_cb = |
482 callback_factory_.NewCallback(&PnaclCoordinator::ObjectWriteDidOpen); | 480 callback_factory_.NewCallback(&PnaclCoordinator::ObjectWriteDidOpen); |
483 obj_file_->OpenWrite(obj_cb); | 481 obj_file_->OpenWrite(obj_cb); |
484 | 482 |
485 streaming_downloader_.reset(new FileDownloader()); | 483 streaming_downloader_.reset(new FileDownloader()); |
486 streaming_downloader_->Initialize(plugin_); | 484 streaming_downloader_->Initialize(plugin_); |
487 pp::CompletionCallback cb = | 485 pp::CompletionCallback cb = |
488 callback_factory_.NewCallback( | 486 callback_factory_.NewCallback( |
489 &PnaclCoordinator::BitcodeStreamDidFinish); | 487 &PnaclCoordinator::BitcodeStreamDidFinish); |
490 | 488 |
491 // TODO(dschuff): need to use url_util_->ResolveRelativeToURL? | 489 // TODO(dschuff): need to use url_util_->ResolveRelativeToURL? |
492 if (!streaming_downloader_->OpenStream(pexe_url_, cb, this)) { | 490 if (!streaming_downloader_->OpenStream(pexe_url_, cb, this)) { |
493 ReportNonPpapiError(nacl::string("failed to open stream ") + pexe_url_); | 491 ReportNonPpapiError(nacl::string("failed to open stream ") + pexe_url_); |
494 } | |
495 | |
496 } else { | |
497 translate_thread_.reset(new PnaclTranslateThread()); | |
498 if (translate_thread_ == NULL) { | |
499 ReportNonPpapiError("could not allocate translation thread."); | |
500 return; | |
501 } | |
502 // load the pexe and set up temp files for translation. | |
503 pp::CompletionCallback cb = | |
504 callback_factory_.NewCallback(&PnaclCoordinator::BitcodeFileDidOpen); | |
505 if (!plugin_->StreamAsFile(pexe_url_, cb.pp_completion_callback())) { | |
506 ReportNonPpapiError(nacl::string("failed to download ") + pexe_url_+ "."); | |
507 } | |
508 } | 492 } |
509 } | 493 } |
510 | 494 |
511 void PnaclCoordinator::BitcodeStreamDidFinish(int32_t pp_error) { | 495 void PnaclCoordinator::BitcodeStreamDidFinish(int32_t pp_error) { |
512 PLUGIN_PRINTF(("PnaclCoordinator::BitcodeStreamDidFinish (pp_error=%" | 496 PLUGIN_PRINTF(("PnaclCoordinator::BitcodeStreamDidFinish (pp_error=%" |
513 NACL_PRId32")\n", pp_error)); | 497 NACL_PRId32")\n", pp_error)); |
514 if (pp_error != PP_OK) { | 498 if (pp_error != PP_OK) { |
515 // Defer reporting the error and obj_file/nexe_file cleanup until after | 499 // Defer reporting the error and obj_file/nexe_file cleanup until after |
516 // the translation thread returns, because it may be accessing the | 500 // the translation thread returns, because it may be accessing the |
517 // coordinator's objects or writing to the files. | 501 // coordinator's objects or writing to the files. |
(...skipping 12 matching lines...) Expand all Loading... |
530 static_cast<PnaclStreamingTranslateThread*>(translate_thread_.get()); | 514 static_cast<PnaclStreamingTranslateThread*>(translate_thread_.get()); |
531 DCHECK(thread); | 515 DCHECK(thread); |
532 thread->PutBytes(data, pp_error); | 516 thread->PutBytes(data, pp_error); |
533 } | 517 } |
534 | 518 |
535 StreamCallback PnaclCoordinator::GetCallback() { | 519 StreamCallback PnaclCoordinator::GetCallback() { |
536 return callback_factory_.NewCallbackWithOutput( | 520 return callback_factory_.NewCallbackWithOutput( |
537 &PnaclCoordinator::BitcodeStreamGotData); | 521 &PnaclCoordinator::BitcodeStreamGotData); |
538 } | 522 } |
539 | 523 |
540 void PnaclCoordinator::BitcodeFileDidOpen(int32_t pp_error) { | |
541 PLUGIN_PRINTF(("PnaclCoordinator::BitcodeFileDidOpen (pp_error=%" | |
542 NACL_PRId32")\n", pp_error)); | |
543 // We have to get the fd immediately after streaming, otherwise it | |
544 // seems like the temp file will get GC'ed. | |
545 int32_t fd = GetLoadedFileDesc(pp_error, pexe_url_, "pexe"); | |
546 if (fd < 0) { | |
547 // Error already reported by GetLoadedFileDesc(). | |
548 return; | |
549 } | |
550 pexe_wrapper_.reset(plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY)); | |
551 | |
552 obj_file_.reset(new LocalTempFile(plugin_, file_system_.get(), | |
553 nacl::string(kPnaclTempDir))); | |
554 pp::CompletionCallback cb = | |
555 callback_factory_.NewCallback(&PnaclCoordinator::ObjectWriteDidOpen); | |
556 obj_file_->OpenWrite(cb); | |
557 } | |
558 | |
559 void PnaclCoordinator::ObjectWriteDidOpen(int32_t pp_error) { | 524 void PnaclCoordinator::ObjectWriteDidOpen(int32_t pp_error) { |
560 PLUGIN_PRINTF(("PnaclCoordinator::ObjectWriteDidOpen (pp_error=%" | 525 PLUGIN_PRINTF(("PnaclCoordinator::ObjectWriteDidOpen (pp_error=%" |
561 NACL_PRId32")\n", pp_error)); | 526 NACL_PRId32")\n", pp_error)); |
562 if (pp_error != PP_OK) { | 527 if (pp_error != PP_OK) { |
563 ReportPpapiError(pp_error); | 528 ReportPpapiError(pp_error); |
564 return; | 529 return; |
565 } | 530 } |
566 pp::CompletionCallback cb = | 531 pp::CompletionCallback cb = |
567 callback_factory_.NewCallback(&PnaclCoordinator::ObjectReadDidOpen); | 532 callback_factory_.NewCallback(&PnaclCoordinator::ObjectReadDidOpen); |
568 obj_file_->OpenRead(cb); | 533 obj_file_->OpenRead(cb); |
(...skipping 29 matching lines...) Expand all Loading... |
598 ld_manifest_.get(), | 563 ld_manifest_.get(), |
599 obj_file_.get(), | 564 obj_file_.get(), |
600 nexe_file_.get(), | 565 nexe_file_.get(), |
601 pexe_wrapper_.get(), | 566 pexe_wrapper_.get(), |
602 &error_info_, | 567 &error_info_, |
603 resources_.get(), | 568 resources_.get(), |
604 plugin_); | 569 plugin_); |
605 } | 570 } |
606 | 571 |
607 } // namespace plugin | 572 } // namespace plugin |
OLD | NEW |