OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <fcntl.h> |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "mojo/application/application_runner_chromium.h" |
| 9 #include "mojo/application/content_handler_factory.h" |
| 10 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
| 11 #include "mojo/message_pump/message_pump_mojo.h" |
| 12 #include "mojo/nacl/irt_mojo_nonsfi.h" |
| 13 #include "mojo/public/c/system/main.h" |
| 14 #include "mojo/public/cpp/application/application_impl.h" |
| 15 #include "native_client/src/public/irt_core.h" |
| 16 #include "native_client/src/public/nonsfi/elf_loader.h" |
| 17 |
| 18 namespace nacl { |
| 19 namespace content_handler { |
| 20 namespace { |
| 21 |
| 22 // Copies response (input) into new temporary file open by fd (output). |
| 23 bool URLResponseToTempFile(mojo::URLResponsePtr& response, int* fd) { |
| 24 base::FilePath path; |
| 25 if (!base::CreateTemporaryFile(&path)) { |
| 26 return false; |
| 27 } |
| 28 |
| 29 if (!mojo::common::BlockingCopyToFile(response->body.Pass(), path)) { |
| 30 base::DeleteFile(path, false); |
| 31 return false; |
| 32 } |
| 33 *fd = open(path.value().c_str(), O_RDONLY); |
| 34 if (*fd < 0) { |
| 35 LOG(FATAL) << "Failed to open " << path.value().c_str() << ": " |
| 36 << strerror(errno) << "\n"; |
| 37 } |
| 38 |
| 39 // Since we unlink an open file, the temp file will be removed as soon as the |
| 40 // fd is closed. |
| 41 if (unlink(path.value().c_str())) { |
| 42 LOG(FATAL) << "Failed to unlink " << path.value().c_str() << ": " |
| 43 << strerror(errno) << "\n"; |
| 44 } |
| 45 return true; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class NaClContentHandler : public mojo::ApplicationDelegate, |
| 51 public mojo::ContentHandlerFactory::Delegate { |
| 52 public: |
| 53 NaClContentHandler() : content_handler_factory_(this) {} |
| 54 |
| 55 private: |
| 56 // Overridden from ApplicationDelegate: |
| 57 void Initialize(mojo::ApplicationImpl* app) override {} |
| 58 |
| 59 // Overridden from ApplicationDelegate: |
| 60 bool ConfigureIncomingConnection( |
| 61 mojo::ApplicationConnection* connection) override { |
| 62 connection->AddService(&content_handler_factory_); |
| 63 return true; |
| 64 } |
| 65 |
| 66 // Overridden from ContentHandlerFactory::ManagedDelegate: |
| 67 void RunApplication( |
| 68 mojo::InterfaceRequest<mojo::Application> application_request, |
| 69 mojo::URLResponsePtr response) override { |
| 70 // Needed to use Mojo interfaces on this thread. |
| 71 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); |
| 72 // Acquire the nexe. |
| 73 int fd; |
| 74 if (!URLResponseToTempFile(response, &fd)) { |
| 75 LOG(FATAL) << "could not redirect nexe to temp file"; |
| 76 } |
| 77 |
| 78 // Run -- also, closes the fd, removing the temp file. |
| 79 uintptr_t entry = NaClLoadElfFile(fd); |
| 80 |
| 81 irtNonsfi::MojoSetInitialHandle( |
| 82 application_request.PassMessagePipe().release().value()); |
| 83 int argc = 1; |
| 84 char* argvp = const_cast<char*>("NaClMain"); |
| 85 char* envp = nullptr; |
| 86 nacl_irt_nonsfi_entry(argc, &argvp, &envp, |
| 87 reinterpret_cast<nacl_entry_func_t>(entry), |
| 88 irtNonsfi::MojoIrtNonsfiQuery); |
| 89 abort(); |
| 90 NOTREACHED(); |
| 91 } |
| 92 |
| 93 mojo::ContentHandlerFactory content_handler_factory_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(NaClContentHandler); |
| 96 }; |
| 97 |
| 98 } // namespace content_handler |
| 99 } // namespace nacl |
| 100 |
| 101 MojoResult MojoMain(MojoHandle application_request) { |
| 102 mojo::ApplicationRunnerChromium runner( |
| 103 new nacl::content_handler::NaClContentHandler()); |
| 104 return runner.Run(application_request); |
| 105 } |
OLD | NEW |