| 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/bindings/array.h" | 19 #include "mojo/public/cpp/bindings/array.h" |
| 20 #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h" |
| 21 #include "mojo/services/files/interfaces/directory.mojom-sync.h" |
| 20 #include "mojo/services/files/interfaces/files.mojom.h" | 22 #include "mojo/services/files/interfaces/files.mojom.h" |
| 21 #include "services/nacl/nonsfi/pnacl_compile.mojom.h" | 23 #include "services/nacl/nonsfi/pnacl_compile.mojom.h" |
| 22 #include "services/nacl/nonsfi/pnacl_link.mojom.h" | 24 #include "services/nacl/nonsfi/pnacl_link.mojom.h" |
| 23 | 25 |
| 24 namespace nacl { | 26 namespace nacl { |
| 25 namespace content_handler { | 27 namespace content_handler { |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 class CompilerUI { | 30 class CompilerUI { |
| 29 public: | 31 public: |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 PexeContentHandler() : content_handler_factory_(this) {} | 78 PexeContentHandler() : content_handler_factory_(this) {} |
| 77 | 79 |
| 78 private: | 80 private: |
| 79 // Overridden from ApplicationDelegate: | 81 // Overridden from ApplicationDelegate: |
| 80 void Initialize(mojo::ApplicationImpl* app) override { | 82 void Initialize(mojo::ApplicationImpl* app) override { |
| 81 app->ConnectToServiceDeprecated("mojo:pnacl_compile", &compiler_init_); | 83 app->ConnectToServiceDeprecated("mojo:pnacl_compile", &compiler_init_); |
| 82 app->ConnectToServiceDeprecated("mojo:pnacl_link", &linker_init_); | 84 app->ConnectToServiceDeprecated("mojo:pnacl_link", &linker_init_); |
| 83 app->ConnectToServiceDeprecated("mojo:files", &files_); | 85 app->ConnectToServiceDeprecated("mojo:files", &files_); |
| 84 mojo::files::Error error = mojo::files::Error::INTERNAL; | 86 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 85 files_->OpenFileSystem("app_persistent_cache", | 87 files_->OpenFileSystem("app_persistent_cache", |
| 86 GetProxy(&nexe_cache_directory), | 88 GetSynchronousProxy(&nexe_cache_directory_), |
| 87 [&error](mojo::files::Error e) { error = e; }); | 89 [&error](mojo::files::Error e) { error = e; }); |
| 88 CHECK(files_.WaitForIncomingResponse()); | 90 CHECK(files_.WaitForIncomingResponse()); |
| 89 CHECK_EQ(mojo::files::Error::OK, error); | 91 CHECK_EQ(mojo::files::Error::OK, error); |
| 90 } | 92 } |
| 91 | 93 |
| 92 // Overridden from ApplicationDelegate: | 94 // Overridden from ApplicationDelegate: |
| 93 bool ConfigureIncomingConnection( | 95 bool ConfigureIncomingConnection( |
| 94 mojo::ApplicationConnection* connection) override { | 96 mojo::ApplicationConnection* connection) override { |
| 95 connection->AddService(&content_handler_factory_); | 97 connection->AddService(&content_handler_factory_); |
| 96 return true; | 98 return true; |
| 97 } | 99 } |
| 98 | 100 |
| 99 int AccessFileFromCache(std::string& digest) { | 101 int AccessFileFromCache(std::string& digest) { |
| 100 mojo::files::Error error = mojo::files::Error::INTERNAL; | 102 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 101 mojo::files::FilePtr nexe_cache_file; | 103 mojo::files::FilePtr nexe_cache_file; |
| 102 nexe_cache_directory->OpenFile( | 104 CHECK(nexe_cache_directory_->OpenFile(digest, GetProxy(&nexe_cache_file), |
| 103 digest, GetProxy(&nexe_cache_file), mojo::files::kOpenFlagRead, | 105 mojo::files::kOpenFlagRead, &error)); |
| 104 [&error](mojo::files::Error e) { error = e; }); | |
| 105 CHECK(nexe_cache_directory.WaitForIncomingResponse()); | |
| 106 if (mojo::files::Error::OK == error) | 106 if (mojo::files::Error::OK == error) |
| 107 // Copy the mojo cached file into an open temporary file. | 107 // Copy the mojo cached file into an open temporary file. |
| 108 return ::nacl::MojoFileToTempFileDescriptor(nexe_cache_file.Pass()); | 108 return ::nacl::MojoFileToTempFileDescriptor(nexe_cache_file.Pass()); |
| 109 else | 109 else |
| 110 // If error != OK, The failure may have been for a variety of reasons -- | 110 // If error != OK, The failure may have been for a variety of reasons -- |
| 111 // assume that the file does not exist. | 111 // assume that the file does not exist. |
| 112 return -1; | 112 return -1; |
| 113 } | 113 } |
| 114 | 114 |
| 115 void StoreFileInCache(int nexe_fd, std::string& digest) { | 115 void StoreFileInCache(int nexe_fd, std::string& digest) { |
| 116 // First, open a "temporary" file. | 116 // First, open a "temporary" file. |
| 117 mojo::files::Error error = mojo::files::Error::INTERNAL; | 117 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 118 std::string temp_file_name; | 118 std::string temp_file_name; |
| 119 mojo::files::FilePtr nexe_cache_file = file_utils::CreateTemporaryFileInDir( | 119 auto nexe_cache_file = |
| 120 &nexe_cache_directory, &temp_file_name); | 120 mojo::files::FilePtr::Create(file_utils::CreateTemporaryFileInDir( |
| 121 &nexe_cache_directory_, &temp_file_name)); |
| 121 CHECK(nexe_cache_file); | 122 CHECK(nexe_cache_file); |
| 122 | 123 |
| 123 // Copy the contents of nexe_fd into the temporary Mojo file. | 124 // Copy the contents of nexe_fd into the temporary Mojo file. |
| 124 FileDescriptorToMojoFile(nexe_fd, nexe_cache_file.Pass()); | 125 FileDescriptorToMojoFile(nexe_fd, nexe_cache_file.Pass()); |
| 125 | 126 |
| 126 // The file is named after the hash of the requesting pexe. | 127 // The file is named after the hash of the requesting pexe. |
| 127 // This makes it usable by future requests for the same pexe under different | 128 // This makes it usable by future requests for the same pexe under different |
| 128 // names. It also atomically moves the entire temp file. | 129 // names. It also atomically moves the entire temp file. |
| 129 nexe_cache_directory->Rename(temp_file_name, digest, | 130 CHECK(nexe_cache_directory_->Rename(temp_file_name, digest, &error)); |
| 130 [&error](mojo::files::Error e) { error = e; }); | |
| 131 CHECK(nexe_cache_directory.WaitForIncomingResponse()); | |
| 132 CHECK_EQ(mojo::files::Error::OK, error); | 131 CHECK_EQ(mojo::files::Error::OK, error); |
| 133 } | 132 } |
| 134 | 133 |
| 135 int DoPexeTranslation(base::FilePath& pexe_file_path) { | 134 int DoPexeTranslation(base::FilePath& pexe_file_path) { |
| 136 // Compile the pexe into an object file | 135 // Compile the pexe into an object file |
| 137 mojo::ScopedMessagePipeHandle parent_compile_pipe; | 136 mojo::ScopedMessagePipeHandle parent_compile_pipe; |
| 138 mojo::ScopedMessagePipeHandle child_compile_pipe; | 137 mojo::ScopedMessagePipeHandle child_compile_pipe; |
| 139 CHECK_EQ(CreateMessagePipe(nullptr, &parent_compile_pipe, | 138 CHECK_EQ(CreateMessagePipe(nullptr, &parent_compile_pipe, |
| 140 &child_compile_pipe), MOJO_RESULT_OK) | 139 &child_compile_pipe), MOJO_RESULT_OK) |
| 141 << "Could not create message pipe to compiler"; | 140 << "Could not create message pipe to compiler"; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 StoreFileInCache(nexe_fd, digest); | 192 StoreFileInCache(nexe_fd, digest); |
| 194 } | 193 } |
| 195 | 194 |
| 196 // Pass the handle connecting us with mojo_shell to the nexe. | 195 // Pass the handle connecting us with mojo_shell to the nexe. |
| 197 MojoHandle handle = application_request.PassMessagePipe().release().value(); | 196 MojoHandle handle = application_request.PassMessagePipe().release().value(); |
| 198 ::nacl::MojoLaunchNexeNonsfi(nexe_fd, handle, | 197 ::nacl::MojoLaunchNexeNonsfi(nexe_fd, handle, |
| 199 false /* enable_translation_irt */); | 198 false /* enable_translation_irt */); |
| 200 } | 199 } |
| 201 | 200 |
| 202 private: | 201 private: |
| 203 mojo::files::DirectoryPtr nexe_cache_directory; | 202 mojo::SynchronousInterfacePtr<mojo::files::Directory> nexe_cache_directory_; |
| 204 mojo::files::FilesPtr files_; | 203 mojo::files::FilesPtr files_; |
| 205 mojo::ContentHandlerFactory content_handler_factory_; | 204 mojo::ContentHandlerFactory content_handler_factory_; |
| 206 mojo::nacl::PexeCompilerInitPtr compiler_init_; | 205 mojo::nacl::PexeCompilerInitPtr compiler_init_; |
| 207 mojo::nacl::PexeLinkerInitPtr linker_init_; | 206 mojo::nacl::PexeLinkerInitPtr linker_init_; |
| 208 | 207 |
| 209 DISALLOW_COPY_AND_ASSIGN(PexeContentHandler); | 208 DISALLOW_COPY_AND_ASSIGN(PexeContentHandler); |
| 210 }; | 209 }; |
| 211 | 210 |
| 212 } // namespace content_handler | 211 } // namespace content_handler |
| 213 } // namespace nacl | 212 } // namespace nacl |
| 214 | 213 |
| 215 MojoResult MojoMain(MojoHandle application_request) { | 214 MojoResult MojoMain(MojoHandle application_request) { |
| 216 mojo::ApplicationRunnerChromium runner( | 215 mojo::ApplicationRunnerChromium runner( |
| 217 new nacl::content_handler::PexeContentHandler()); | 216 new nacl::content_handler::PexeContentHandler()); |
| 218 return runner.Run(application_request); | 217 return runner.Run(application_request); |
| 219 } | 218 } |
| OLD | NEW |