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 "mojo/application/application_runner_chromium.h" | 8 #include "mojo/application/application_runner_chromium.h" |
9 #include "mojo/application/content_handler_factory.h" | 9 #include "mojo/application/content_handler_factory.h" |
10 #include "mojo/data_pipe_utils/data_pipe_utils.h" | 10 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
11 #include "mojo/message_pump/message_pump_mojo.h" | 11 #include "mojo/message_pump/message_pump_mojo.h" |
12 #include "mojo/nacl/irt_mojo_nonsfi.h" | 12 #include "mojo/nacl/irt_mojo_nonsfi.h" |
13 #include "mojo/public/c/system/main.h" | 13 #include "mojo/public/c/system/main.h" |
14 #include "mojo/public/cpp/application/application_impl.h" | 14 #include "mojo/public/cpp/application/application_impl.h" |
15 #include "native_client/src/public/irt_core.h" | 15 #include "native_client/src/public/irt_core.h" |
16 #include "native_client/src/public/nonsfi/elf_loader.h" | 16 #include "native_client/src/public/nonsfi/elf_loader.h" |
17 | 17 |
18 namespace nacl { | 18 namespace nacl { |
19 namespace content_handler { | 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 | 20 |
50 class NaClContentHandler : public mojo::ApplicationDelegate, | 21 class NaClContentHandler : public mojo::ApplicationDelegate, |
51 public mojo::ContentHandlerFactory::Delegate { | 22 public mojo::ContentHandlerFactory::Delegate { |
52 public: | 23 public: |
53 NaClContentHandler() : content_handler_factory_(this) {} | 24 NaClContentHandler() : content_handler_factory_(this) {} |
54 | 25 |
55 private: | 26 private: |
56 // Overridden from ApplicationDelegate: | 27 // Overridden from ApplicationDelegate: |
57 void Initialize(mojo::ApplicationImpl* app) override {} | 28 void Initialize(mojo::ApplicationImpl* app) override {} |
58 | 29 |
59 // Overridden from ApplicationDelegate: | 30 // Overridden from ApplicationDelegate: |
60 bool ConfigureIncomingConnection( | 31 bool ConfigureIncomingConnection( |
61 mojo::ApplicationConnection* connection) override { | 32 mojo::ApplicationConnection* connection) override { |
62 connection->AddService(&content_handler_factory_); | 33 connection->AddService(&content_handler_factory_); |
63 return true; | 34 return true; |
64 } | 35 } |
65 | 36 |
66 // Overridden from ContentHandlerFactory::ManagedDelegate: | 37 // Overridden from ContentHandlerFactory::ManagedDelegate: |
67 void RunApplication( | 38 void RunApplication( |
68 mojo::InterfaceRequest<mojo::Application> application_request, | 39 mojo::InterfaceRequest<mojo::Application> application_request, |
69 mojo::URLResponsePtr response) override { | 40 mojo::URLResponsePtr response) override { |
70 // Needed to use Mojo interfaces on this thread. | 41 // Needed to use Mojo interfaces on this thread. |
71 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); | 42 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); |
72 // Acquire the nexe. | 43 // Acquire the nexe. |
73 int fd; | 44 base::ScopedFILE nexe_fp = |
74 if (!URLResponseToTempFile(response, &fd)) { | 45 mojo::common::BlockingCopyToTempFile(response->body.Pass()); |
75 LOG(FATAL) << "could not redirect nexe to temp file"; | 46 if (nexe_fp == nullptr) { |
Mark Seaborn
2015/09/03 06:33:16
Ditto
Sean Klein
2015/09/03 15:51:50
Done.
| |
47 LOG(FATAL) << "Could not redirect nexe to temp file"; | |
48 } | |
49 FILE* nexe_file_stream = nexe_fp.release(); | |
50 int fd = fileno(nexe_file_stream); | |
51 if (fd == -1) { | |
52 LOG(FATAL) << "Could not open the stream pointer's file descriptor"; | |
53 } | |
54 fd = dup(fd); | |
55 if (fd == -1) { | |
56 LOG(FATAL) << "Could not dup the file descriptor"; | |
57 } | |
58 if (fclose(nexe_file_stream)) { | |
59 LOG(FATAL) << "Failed to close temp file"; | |
76 } | 60 } |
77 | 61 |
78 // Run -- also, closes the fd, removing the temp file. | 62 // Run -- also, closes the fd, removing the temp file. |
79 uintptr_t entry = NaClLoadElfFile(fd); | 63 uintptr_t entry = NaClLoadElfFile(fd); |
80 | 64 |
81 irtNonsfi::MojoSetInitialHandle( | 65 irtNonsfi::MojoSetInitialHandle( |
82 application_request.PassMessagePipe().release().value()); | 66 application_request.PassMessagePipe().release().value()); |
83 int argc = 1; | 67 int argc = 1; |
84 char* argvp = const_cast<char*>("NaClMain"); | 68 char* argvp = const_cast<char*>("NaClMain"); |
85 char* envp = nullptr; | 69 char* envp = nullptr; |
(...skipping 10 matching lines...) Expand all Loading... | |
96 }; | 80 }; |
97 | 81 |
98 } // namespace content_handler | 82 } // namespace content_handler |
99 } // namespace nacl | 83 } // namespace nacl |
100 | 84 |
101 MojoResult MojoMain(MojoHandle application_request) { | 85 MojoResult MojoMain(MojoHandle application_request) { |
102 mojo::ApplicationRunnerChromium runner( | 86 mojo::ApplicationRunnerChromium runner( |
103 new nacl::content_handler::NaClContentHandler()); | 87 new nacl::content_handler::NaClContentHandler()); |
104 return runner.Run(application_request); | 88 return runner.Run(application_request); |
105 } | 89 } |
OLD | NEW |