| 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 "ppapi/native_client/src/trusted/plugin/pnacl_coordinator.h" | 5 #include "ppapi/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/checked_cast.h" | 10 #include "native_client/src/include/checked_cast.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // before any scoped_* fields hanging off of PnaclCoordinator | 188 // before any scoped_* fields hanging off of PnaclCoordinator |
| 189 // since the thread may be accessing those fields. | 189 // since the thread may be accessing those fields. |
| 190 // It will also be accessing obj_files_. | 190 // It will also be accessing obj_files_. |
| 191 translate_thread_.reset(NULL); | 191 translate_thread_.reset(NULL); |
| 192 // TODO(jvoung): use base/memory/scoped_vector.h to hold obj_files_. | 192 // TODO(jvoung): use base/memory/scoped_vector.h to hold obj_files_. |
| 193 for (int i = 0; i < num_object_files_opened_; i++) { | 193 for (int i = 0; i < num_object_files_opened_; i++) { |
| 194 delete obj_files_[i]; | 194 delete obj_files_[i]; |
| 195 } | 195 } |
| 196 } | 196 } |
| 197 | 197 |
| 198 nacl::DescWrapper* PnaclCoordinator::ReleaseTranslatedFD() { | 198 PP_FileHandle PnaclCoordinator::TakeTranslatedFileHandle() { |
| 199 DCHECK(temp_nexe_file_ != NULL); | 199 DCHECK(temp_nexe_file_ != NULL); |
| 200 return temp_nexe_file_->release_read_wrapper(); | 200 return temp_nexe_file_->TakeFileHandle(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void PnaclCoordinator::ReportNonPpapiError(PP_NaClError err_code, | 203 void PnaclCoordinator::ReportNonPpapiError(PP_NaClError err_code, |
| 204 const nacl::string& message) { | 204 const nacl::string& message) { |
| 205 ErrorInfo error_info; | 205 ErrorInfo error_info; |
| 206 error_info.SetReport(err_code, message); | 206 error_info.SetReport(err_code, message); |
| 207 plugin_->ReportLoadError(error_info); | 207 plugin_->ReportLoadError(error_info); |
| 208 ExitWithError(); | 208 ExitWithError(); |
| 209 } | 209 } |
| 210 | 210 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 plugin_->nacl_interface()->GetNexeFd( | 419 plugin_->nacl_interface()->GetNexeFd( |
| 420 plugin_->pp_instance(), | 420 plugin_->pp_instance(), |
| 421 streaming_downloader_->full_url().c_str(), | 421 streaming_downloader_->full_url().c_str(), |
| 422 // TODO(dschuff): Get this value from the pnacl json file after it | 422 // TODO(dschuff): Get this value from the pnacl json file after it |
| 423 // rolls in from NaCl. | 423 // rolls in from NaCl. |
| 424 1, | 424 1, |
| 425 pnacl_options_.opt_level, | 425 pnacl_options_.opt_level, |
| 426 headers.c_str(), | 426 headers.c_str(), |
| 427 architecture_attributes_.c_str(), // Extra compile flags. | 427 architecture_attributes_.c_str(), // Extra compile flags. |
| 428 &is_cache_hit_, | 428 &is_cache_hit_, |
| 429 temp_nexe_file_->existing_handle(), | 429 temp_nexe_file_->internal_handle(), |
| 430 cb.pp_completion_callback()); | 430 cb.pp_completion_callback()); |
| 431 if (nexe_fd_err < PP_OK_COMPLETIONPENDING) { | 431 if (nexe_fd_err < PP_OK_COMPLETIONPENDING) { |
| 432 ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, nexe_fd_err, | 432 ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, nexe_fd_err, |
| 433 nacl::string("Call to GetNexeFd failed")); | 433 nacl::string("Call to GetNexeFd failed")); |
| 434 } | 434 } |
| 435 } | 435 } |
| 436 | 436 |
| 437 void PnaclCoordinator::NexeFdDidOpen(int32_t pp_error) { | 437 void PnaclCoordinator::NexeFdDidOpen(int32_t pp_error) { |
| 438 PLUGIN_PRINTF(("PnaclCoordinator::NexeFdDidOpen (pp_error=%" | 438 PLUGIN_PRINTF(("PnaclCoordinator::NexeFdDidOpen (pp_error=%" |
| 439 NACL_PRId32 ", hit=%d, handle=%d)\n", pp_error, | 439 NACL_PRId32 ", hit=%d)\n", pp_error, |
| 440 is_cache_hit_ == PP_TRUE, | 440 is_cache_hit_ == PP_TRUE)); |
| 441 *temp_nexe_file_->existing_handle())); | |
| 442 if (pp_error < PP_OK) { | 441 if (pp_error < PP_OK) { |
| 443 ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, pp_error, | 442 ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, pp_error, |
| 444 nacl::string("GetNexeFd failed")); | 443 nacl::string("GetNexeFd failed")); |
| 445 return; | 444 return; |
| 446 } | 445 } |
| 447 | 446 |
| 448 if (*temp_nexe_file_->existing_handle() == PP_kInvalidFileHandle) { | 447 if (*temp_nexe_file_->internal_handle() == PP_kInvalidFileHandle) { |
| 449 ReportNonPpapiError( | 448 ReportNonPpapiError( |
| 450 PP_NACL_ERROR_PNACL_CREATE_TEMP, | 449 PP_NACL_ERROR_PNACL_CREATE_TEMP, |
| 451 nacl::string( | 450 nacl::string( |
| 452 "PnaclCoordinator: Got bad temp file handle from GetNexeFd")); | 451 "PnaclCoordinator: Got bad temp file handle from GetNexeFd")); |
| 453 return; | 452 return; |
| 454 } | 453 } |
| 455 HistogramEnumerateTranslationCache(plugin_->uma_interface(), is_cache_hit_); | 454 HistogramEnumerateTranslationCache(plugin_->uma_interface(), is_cache_hit_); |
| 456 | 455 |
| 457 if (is_cache_hit_ == PP_TRUE) { | 456 if (is_cache_hit_ == PP_TRUE) { |
| 458 // Cache hit -- no need to stream the rest of the file. | 457 // Cache hit -- no need to stream the rest of the file. |
| 459 streaming_downloader_.reset(NULL); | 458 streaming_downloader_.reset(NULL); |
| 460 // Open it for reading as the cached nexe file. | 459 // Open it for reading as the cached nexe file. |
| 461 pp::CompletionCallback cb = | 460 NexeReadDidOpen(temp_nexe_file_->Open(false)); |
| 462 callback_factory_.NewCallback(&PnaclCoordinator::NexeReadDidOpen); | |
| 463 temp_nexe_file_->Open(cb, false); | |
| 464 } else { | 461 } else { |
| 465 // Open an object file first so the translator can start writing to it | 462 // Open an object file first so the translator can start writing to it |
| 466 // during streaming translation. | 463 // during streaming translation. |
| 467 for (int i = 0; i < split_module_count_; i++) { | 464 for (int i = 0; i < split_module_count_; i++) { |
| 468 obj_files_.push_back(new TempFile(plugin_)); | 465 obj_files_.push_back(new TempFile(plugin_)); |
| 469 | 466 int32_t pp_error = obj_files_[i]->Open(true); |
| 470 pp::CompletionCallback obj_cb = | 467 if (pp_error != PP_OK) { |
| 471 callback_factory_.NewCallback(&PnaclCoordinator::ObjectFileDidOpen); | 468 ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, |
| 472 obj_files_[i]->Open(obj_cb, true); | 469 pp_error, |
| 470 "Failed to open scratch object file."); |
| 471 } else { |
| 472 num_object_files_opened_++; |
| 473 } |
| 473 } | 474 } |
| 474 invalid_desc_wrapper_.reset(plugin_->wrapper_factory()->MakeInvalid()); | 475 invalid_desc_wrapper_.reset(plugin_->wrapper_factory()->MakeInvalid()); |
| 475 | 476 |
| 476 // Meanwhile, a miss means we know we need to stream the bitcode, so stream | 477 // Meanwhile, a miss means we know we need to stream the bitcode, so stream |
| 477 // the rest of it now. (Calling FinishStreaming means that the downloader | 478 // the rest of it now. (Calling FinishStreaming means that the downloader |
| 478 // will begin handing data to the coordinator, which is safe any time after | 479 // will begin handing data to the coordinator, which is safe any time after |
| 479 // the translate_thread_ object has been initialized). | 480 // the translate_thread_ object has been initialized). |
| 480 pp::CompletionCallback finish_cb = callback_factory_.NewCallback( | 481 pp::CompletionCallback finish_cb = callback_factory_.NewCallback( |
| 481 &PnaclCoordinator::BitcodeStreamDidFinish); | 482 &PnaclCoordinator::BitcodeStreamDidFinish); |
| 482 streaming_downloader_->FinishStreaming(finish_cb); | 483 streaming_downloader_->FinishStreaming(finish_cb); |
| 484 |
| 485 if (num_object_files_opened_ == split_module_count_) { |
| 486 // Open the nexe file for connecting ld and sel_ldr. |
| 487 // Start translation when done with this last step of setup! |
| 488 RunTranslate(temp_nexe_file_->Open(true)); |
| 489 } |
| 483 } | 490 } |
| 484 } | 491 } |
| 485 | 492 |
| 486 void PnaclCoordinator::BitcodeStreamDidFinish(int32_t pp_error) { | 493 void PnaclCoordinator::BitcodeStreamDidFinish(int32_t pp_error) { |
| 487 PLUGIN_PRINTF(("PnaclCoordinator::BitcodeStreamDidFinish (pp_error=%" | 494 PLUGIN_PRINTF(("PnaclCoordinator::BitcodeStreamDidFinish (pp_error=%" |
| 488 NACL_PRId32 ")\n", pp_error)); | 495 NACL_PRId32 ")\n", pp_error)); |
| 489 if (pp_error != PP_OK) { | 496 if (pp_error != PP_OK) { |
| 490 // Defer reporting the error and cleanup until after the translation | 497 // Defer reporting the error and cleanup until after the translation |
| 491 // thread returns, because it may be accessing the coordinator's | 498 // thread returns, because it may be accessing the coordinator's |
| 492 // objects or writing to the files. | 499 // objects or writing to the files. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 event_name, | 586 event_name, |
| 580 microsecs); | 587 microsecs); |
| 581 } | 588 } |
| 582 | 589 |
| 583 void PnaclCoordinator::GetCurrentProgress(int64_t* bytes_loaded, | 590 void PnaclCoordinator::GetCurrentProgress(int64_t* bytes_loaded, |
| 584 int64_t* bytes_total) { | 591 int64_t* bytes_total) { |
| 585 *bytes_loaded = pexe_bytes_compiled_; | 592 *bytes_loaded = pexe_bytes_compiled_; |
| 586 *bytes_total = expected_pexe_size_; | 593 *bytes_total = expected_pexe_size_; |
| 587 } | 594 } |
| 588 | 595 |
| 589 void PnaclCoordinator::ObjectFileDidOpen(int32_t pp_error) { | |
| 590 PLUGIN_PRINTF(("PnaclCoordinator::ObjectFileDidOpen (pp_error=%" | |
| 591 NACL_PRId32 ")\n", pp_error)); | |
| 592 if (pp_error != PP_OK) { | |
| 593 ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, | |
| 594 pp_error, | |
| 595 "Failed to open scratch object file."); | |
| 596 return; | |
| 597 } | |
| 598 num_object_files_opened_++; | |
| 599 if (num_object_files_opened_ == split_module_count_) { | |
| 600 // Open the nexe file for connecting ld and sel_ldr. | |
| 601 // Start translation when done with this last step of setup! | |
| 602 pp::CompletionCallback cb = | |
| 603 callback_factory_.NewCallback(&PnaclCoordinator::RunTranslate); | |
| 604 temp_nexe_file_->Open(cb, true); | |
| 605 } | |
| 606 } | |
| 607 | |
| 608 void PnaclCoordinator::RunTranslate(int32_t pp_error) { | 596 void PnaclCoordinator::RunTranslate(int32_t pp_error) { |
| 609 PLUGIN_PRINTF(("PnaclCoordinator::RunTranslate (pp_error=%" | 597 PLUGIN_PRINTF(("PnaclCoordinator::RunTranslate (pp_error=%" |
| 610 NACL_PRId32 ")\n", pp_error)); | 598 NACL_PRId32 ")\n", pp_error)); |
| 611 // Invoke llc followed by ld off the main thread. This allows use of | 599 // Invoke llc followed by ld off the main thread. This allows use of |
| 612 // blocking RPCs that would otherwise block the JavaScript main thread. | 600 // blocking RPCs that would otherwise block the JavaScript main thread. |
| 613 pp::CompletionCallback report_translate_finished = | 601 pp::CompletionCallback report_translate_finished = |
| 614 callback_factory_.NewCallback(&PnaclCoordinator::TranslateFinished); | 602 callback_factory_.NewCallback(&PnaclCoordinator::TranslateFinished); |
| 615 | 603 |
| 616 CHECK(translate_thread_ != NULL); | 604 CHECK(translate_thread_ != NULL); |
| 617 translate_thread_->RunTranslate(report_translate_finished, | 605 translate_thread_->RunTranslate(report_translate_finished, |
| 618 &obj_files_, | 606 &obj_files_, |
| 619 temp_nexe_file_.get(), | 607 temp_nexe_file_.get(), |
| 620 invalid_desc_wrapper_.get(), | 608 invalid_desc_wrapper_.get(), |
| 621 &error_info_, | 609 &error_info_, |
| 622 resources_.get(), | 610 resources_.get(), |
| 623 &pnacl_options_, | 611 &pnacl_options_, |
| 624 architecture_attributes_, | 612 architecture_attributes_, |
| 625 this, | 613 this, |
| 626 plugin_); | 614 plugin_); |
| 627 } | 615 } |
| 628 | 616 |
| 629 } // namespace plugin | 617 } // namespace plugin |
| OLD | NEW |