| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <fcntl.h> | 5 #include <fcntl.h> |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/sha1.h" | 8 #include "base/sha1.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "mojo/application/application_runner_chromium.h" | 10 #include "mojo/application/application_runner_chromium.h" |
| 11 #include "mojo/application/content_handler_factory.h" | 11 #include "mojo/application/content_handler_factory.h" |
| 12 #include "mojo/data_pipe_utils/data_pipe_utils.h" | 12 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
| 13 #include "mojo/file_utils/file_util.h" | 13 #include "mojo/file_utils/file_util.h" |
| 14 #include "mojo/message_pump/message_pump_mojo.h" | 14 #include "mojo/message_pump/message_pump_mojo.h" |
| 15 #include "mojo/nacl/nonsfi/file_util.h" | 15 #include "mojo/nacl/nonsfi/file_util.h" |
| 16 #include "mojo/nacl/nonsfi/nexe_launcher_nonsfi.h" | 16 #include "mojo/nacl/nonsfi/nexe_launcher_nonsfi.h" |
| 17 #include "mojo/public/c/system/main.h" | 17 #include "mojo/public/c/system/main.h" |
| 18 #include "mojo/public/cpp/application/application_impl.h" | 18 #include "mojo/public/cpp/application/application_impl.h" |
| 19 #include "mojo/public/cpp/application/connect.h" | 19 #include "mojo/public/cpp/application/connect.h" |
| 20 #include "mojo/public/cpp/bindings/array.h" | 20 #include "mojo/public/cpp/bindings/array.h" |
| 21 #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h" | 21 #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h" |
| 22 #include "mojo/services/files/interfaces/directory.mojom-sync.h" | 22 #include "mojo/services/files/interfaces/directory.mojom-sync.h" |
| 23 #include "mojo/services/files/interfaces/files.mojom.h" | 23 #include "mojo/services/files/interfaces/files.mojom.h" |
| 24 #include "services/nacl/nonsfi/pnacl_compile.mojom.h" | 24 #include "services/nacl/nonsfi/pnacl_compile.mojom-sync.h" |
| 25 #include "services/nacl/nonsfi/pnacl_link.mojom.h" | 25 #include "services/nacl/nonsfi/pnacl_link.mojom-sync.h" |
| 26 | 26 |
| 27 namespace nacl { | 27 namespace nacl { |
| 28 namespace content_handler { | 28 namespace content_handler { |
| 29 namespace { | |
| 30 | |
| 31 class CompilerUI { | |
| 32 public: | |
| 33 explicit CompilerUI(mojo::ScopedMessagePipeHandle handle) { | |
| 34 compiler_.Bind( | |
| 35 mojo::InterfaceHandle<mojo::nacl::PexeCompiler>(handle.Pass(), 0u)); | |
| 36 } | |
| 37 | |
| 38 // Synchronous method to compile pexe into object file. | |
| 39 mojo::Array<mojo::String> CompilePexe(mojo::String pexe_file_path) { | |
| 40 mojo::Array<mojo::String> output; | |
| 41 compiler_->PexeCompile( | |
| 42 pexe_file_path, | |
| 43 [&output](mojo::Array<mojo::String> o) { output = o.Pass(); }); | |
| 44 CHECK(compiler_.WaitForIncomingResponse()) | |
| 45 << "Waiting for pexe compiler failed"; | |
| 46 return output; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 mojo::nacl::PexeCompilerPtr compiler_; | |
| 51 }; | |
| 52 | |
| 53 class LinkerUI { | |
| 54 public: | |
| 55 explicit LinkerUI(mojo::ScopedMessagePipeHandle handle) { | |
| 56 linker_.Bind( | |
| 57 mojo::InterfaceHandle<mojo::nacl::PexeLinker>(handle.Pass(), 0u)); | |
| 58 } | |
| 59 | |
| 60 // Synchronous method to link object file into nexe. | |
| 61 mojo::String LinkPexe(mojo::Array<mojo::String> object_file_paths) { | |
| 62 mojo::String output; | |
| 63 linker_->PexeLink(std::move(object_file_paths), | |
| 64 [&output](mojo::String o) { output = o; }); | |
| 65 CHECK(linker_.WaitForIncomingResponse()) | |
| 66 << "Waiting for pexe linker failed"; | |
| 67 return output; | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 mojo::nacl::PexeLinkerPtr linker_; | |
| 72 }; | |
| 73 | |
| 74 } // namespace anonymous | |
| 75 | 29 |
| 76 class PexeContentHandler : public mojo::ApplicationDelegate, | 30 class PexeContentHandler : public mojo::ApplicationDelegate, |
| 77 public mojo::ContentHandlerFactory::Delegate { | 31 public mojo::ContentHandlerFactory::Delegate { |
| 78 public: | 32 public: |
| 79 PexeContentHandler() : content_handler_factory_(this) {} | 33 PexeContentHandler() : content_handler_factory_(this) {} |
| 80 | 34 |
| 81 private: | 35 private: |
| 82 // Overridden from ApplicationDelegate: | 36 // Overridden from ApplicationDelegate: |
| 83 void Initialize(mojo::ApplicationImpl* app) override { | 37 void Initialize(mojo::ApplicationImpl* app) override { |
| 84 mojo::ConnectToService(app->shell(), "mojo:pnacl_compile", | 38 mojo::ConnectToService(app->shell(), "mojo:pnacl_compile", |
| 85 GetProxy(&compiler_init_)); | 39 GetSynchronousProxy(&compiler_init_)); |
| 86 mojo::ConnectToService(app->shell(), "mojo:pnacl_link", | 40 mojo::ConnectToService(app->shell(), "mojo:pnacl_link", |
| 87 GetProxy(&linker_init_)); | 41 GetSynchronousProxy(&linker_init_)); |
| 88 mojo::ConnectToService(app->shell(), "mojo:files", GetProxy(&files_)); | 42 mojo::ConnectToService(app->shell(), "mojo:files", GetProxy(&files_)); |
| 89 mojo::files::Error error = mojo::files::Error::INTERNAL; | 43 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 90 files_->OpenFileSystem("app_persistent_cache", | 44 files_->OpenFileSystem("app_persistent_cache", |
| 91 GetSynchronousProxy(&nexe_cache_directory_), | 45 GetSynchronousProxy(&nexe_cache_directory_), |
| 92 [&error](mojo::files::Error e) { error = e; }); | 46 [&error](mojo::files::Error e) { error = e; }); |
| 93 CHECK(files_.WaitForIncomingResponse()); | 47 CHECK(files_.WaitForIncomingResponse()); |
| 94 CHECK_EQ(mojo::files::Error::OK, error); | 48 CHECK_EQ(mojo::files::Error::OK, error); |
| 95 } | 49 } |
| 96 | 50 |
| 97 // Overridden from ApplicationDelegate: | 51 // Overridden from ApplicationDelegate: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 83 |
| 130 // The file is named after the hash of the requesting pexe. | 84 // The file is named after the hash of the requesting pexe. |
| 131 // This makes it usable by future requests for the same pexe under different | 85 // This makes it usable by future requests for the same pexe under different |
| 132 // names. It also atomically moves the entire temp file. | 86 // names. It also atomically moves the entire temp file. |
| 133 CHECK(nexe_cache_directory_->Rename(temp_file_name, digest, &error)); | 87 CHECK(nexe_cache_directory_->Rename(temp_file_name, digest, &error)); |
| 134 CHECK_EQ(mojo::files::Error::OK, error); | 88 CHECK_EQ(mojo::files::Error::OK, error); |
| 135 } | 89 } |
| 136 | 90 |
| 137 int DoPexeTranslation(base::FilePath& pexe_file_path) { | 91 int DoPexeTranslation(base::FilePath& pexe_file_path) { |
| 138 // Compile the pexe into an object file | 92 // Compile the pexe into an object file |
| 139 mojo::ScopedMessagePipeHandle parent_compile_pipe; | 93 mojo::SynchronousInterfacePtr<mojo::nacl::PexeCompiler> compiler; |
| 140 mojo::ScopedMessagePipeHandle child_compile_pipe; | 94 CHECK(compiler_init_->PexeCompilerStart(GetSynchronousProxy(&compiler))); |
| 141 CHECK_EQ(CreateMessagePipe(nullptr, &parent_compile_pipe, | |
| 142 &child_compile_pipe), MOJO_RESULT_OK) | |
| 143 << "Could not create message pipe to compiler"; | |
| 144 compiler_init_->PexeCompilerStart(child_compile_pipe.Pass()); | |
| 145 | 95 |
| 146 // Communicate with the compiler using a mojom interface. | 96 // Communicate with the compiler using a mojom interface. |
| 147 CompilerUI compiler_ui(parent_compile_pipe.Pass()); | 97 mojo::Array<mojo::String> object_files; |
| 148 mojo::Array<mojo::String> object_files = | 98 CHECK(compiler->PexeCompile(pexe_file_path.value(), &object_files)); |
| 149 compiler_ui.CompilePexe(pexe_file_path.value()); | |
| 150 | 99 |
| 151 // Link the object file into a nexe | 100 // Link the object file into a nexe |
| 152 mojo::ScopedMessagePipeHandle parent_link_pipe; | 101 mojo::SynchronousInterfacePtr<mojo::nacl::PexeLinker> linker; |
| 153 mojo::ScopedMessagePipeHandle child_link_pipe; | 102 CHECK(linker_init_->PexeLinkerStart(GetSynchronousProxy(&linker))); |
| 154 CHECK_EQ(CreateMessagePipe(nullptr, &parent_link_pipe, &child_link_pipe), | |
| 155 MOJO_RESULT_OK) << "Could not create message pipe to linker"; | |
| 156 linker_init_->PexeLinkerStart(child_link_pipe.Pass()); | |
| 157 | 103 |
| 158 // Communicate with the linker using a mojom interface. | 104 // Communicate with the linker using a mojom interface. |
| 159 LinkerUI linker_ui(parent_link_pipe.Pass()); | 105 mojo::String nexe_file; |
| 160 mojo::String nexe_file = linker_ui.LinkPexe(std::move(object_files)); | 106 CHECK(linker->PexeLink(std::move(object_files), &nexe_file)); |
| 161 | 107 |
| 162 // Open the nexe file and launch it (with our mojo handle) | 108 // Open the nexe file and launch it (with our mojo handle) |
| 163 int nexe_fd = open(nexe_file.get().c_str(), O_RDONLY); | 109 int nexe_fd = open(nexe_file.get().c_str(), O_RDONLY); |
| 164 CHECK(!unlink(nexe_file.get().c_str())) | 110 CHECK(!unlink(nexe_file.get().c_str())) |
| 165 << "Could not unlink temporary nexe file"; | 111 << "Could not unlink temporary nexe file"; |
| 166 CHECK_GE(nexe_fd, 0) << "Could not open nexe object file"; | 112 CHECK_GE(nexe_fd, 0) << "Could not open nexe object file"; |
| 167 return nexe_fd; | 113 return nexe_fd; |
| 168 } | 114 } |
| 169 | 115 |
| 170 // Overridden from ContentHandlerFactory::Delegate: | 116 // Overridden from ContentHandlerFactory::Delegate: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 198 // Pass the handle connecting us with mojo_shell to the nexe. | 144 // Pass the handle connecting us with mojo_shell to the nexe. |
| 199 MojoHandle handle = application_request.PassMessagePipe().release().value(); | 145 MojoHandle handle = application_request.PassMessagePipe().release().value(); |
| 200 ::nacl::MojoLaunchNexeNonsfi(nexe_fd, handle, | 146 ::nacl::MojoLaunchNexeNonsfi(nexe_fd, handle, |
| 201 false /* enable_translation_irt */); | 147 false /* enable_translation_irt */); |
| 202 } | 148 } |
| 203 | 149 |
| 204 private: | 150 private: |
| 205 mojo::SynchronousInterfacePtr<mojo::files::Directory> nexe_cache_directory_; | 151 mojo::SynchronousInterfacePtr<mojo::files::Directory> nexe_cache_directory_; |
| 206 mojo::files::FilesPtr files_; | 152 mojo::files::FilesPtr files_; |
| 207 mojo::ContentHandlerFactory content_handler_factory_; | 153 mojo::ContentHandlerFactory content_handler_factory_; |
| 208 mojo::nacl::PexeCompilerInitPtr compiler_init_; | 154 mojo::SynchronousInterfacePtr<mojo::nacl::PexeCompilerInit> compiler_init_; |
| 209 mojo::nacl::PexeLinkerInitPtr linker_init_; | 155 mojo::SynchronousInterfacePtr<mojo::nacl::PexeLinkerInit> linker_init_; |
| 210 | 156 |
| 211 DISALLOW_COPY_AND_ASSIGN(PexeContentHandler); | 157 DISALLOW_COPY_AND_ASSIGN(PexeContentHandler); |
| 212 }; | 158 }; |
| 213 | 159 |
| 214 } // namespace content_handler | 160 } // namespace content_handler |
| 215 } // namespace nacl | 161 } // namespace nacl |
| 216 | 162 |
| 217 MojoResult MojoMain(MojoHandle application_request) { | 163 MojoResult MojoMain(MojoHandle application_request) { |
| 218 mojo::ApplicationRunnerChromium runner( | 164 mojo::ApplicationRunnerChromium runner( |
| 219 new nacl::content_handler::PexeContentHandler()); | 165 new nacl::content_handler::PexeContentHandler()); |
| 220 return runner.Run(application_request); | 166 return runner.Run(application_request); |
| 221 } | 167 } |
| OLD | NEW |