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/public/c/system/main.h" | |
13 #include "mojo/public/cpp/application/application_impl.h" | |
14 #include "mojo/public/platform/nacl/mojo_irt.h" | |
15 #include "nacl_bindings/monacl_sel_main.h" | |
Mark Seaborn
2015/09/01 15:55:45
Shouldn't be needed: this refers to SFI NaCl.
Sean Klein
2015/09/01 20:24:47
Done.
| |
16 #include "native_client/src/public/irt_core.h" | |
17 #include "native_client/src/public/nacl_desc.h" | |
18 #include "native_client/src/public/nonsfi/elf_loader.h" | |
19 | |
20 namespace nacl { | |
21 namespace content_handler { | |
22 | |
23 MojoHandle g_mojo_handle = MOJO_HANDLE_INVALID; | |
Mark Seaborn
2015/09/01 15:55:45
This can go inside the anon namespace.
Sean Klein
2015/09/01 20:24:48
Done -- sort of. Moved into anon namespace of the
| |
24 | |
25 namespace { | |
26 | |
27 MojoResult _MojoGetInitialHandle(MojoHandle* handle) { | |
28 // TODO(smklein): Test this handle | |
29 *handle = g_mojo_handle; | |
30 return MOJO_RESULT_OK; | |
31 } | |
32 | |
33 const struct nacl_irt_mojo kIrtMojo = { | |
Mark Seaborn
2015/09/01 15:55:45
This is duplicated from monacl_shell_nonsfi.cc. L
Sean Klein
2015/09/01 20:24:47
Done.
| |
34 MojoCreateSharedBuffer, | |
35 MojoDuplicateBufferHandle, | |
36 MojoMapBuffer, | |
37 MojoUnmapBuffer, | |
38 MojoCreateDataPipe, | |
39 MojoWriteData, | |
40 MojoBeginWriteData, | |
41 MojoEndWriteData, | |
42 MojoReadData, | |
43 MojoBeginReadData, | |
44 MojoEndReadData, | |
45 MojoGetTimeTicksNow, | |
46 MojoClose, | |
47 MojoWait, | |
48 MojoWaitMany, | |
49 MojoCreateMessagePipe, | |
50 MojoWriteMessage, | |
51 MojoReadMessage, | |
52 _MojoGetInitialHandle, | |
53 }; | |
54 | |
55 const struct nacl_irt_interface kIrtInterfaces[] = { | |
56 { NACL_IRT_MOJO_v0_1, &kIrtMojo, sizeof(kIrtMojo), nullptr } | |
57 }; | |
58 | |
59 size_t mojo_irt_nonsfi_query(const char* interface_ident, | |
60 void* table, size_t tablesize) { | |
61 size_t result = nacl_irt_query_list(interface_ident, | |
62 table, | |
63 tablesize, | |
64 kIrtInterfaces, | |
65 sizeof(kIrtInterfaces)); | |
66 if (result != 0) | |
67 return result; | |
68 return nacl_irt_query_core(interface_ident, table, tablesize); | |
69 } | |
70 | |
71 // Copies response (input) into new temporary file at file_path (output). | |
72 bool URLResponseToTempFile(mojo::URLResponsePtr& response, | |
73 base::FilePath* file_path) { | |
74 if (!base::CreateTemporaryFile(file_path)) { | |
75 return false; | |
76 } | |
77 | |
78 if (!mojo::common::BlockingCopyToFile(response->body.Pass(), *file_path)) { | |
Mark Seaborn
2015/09/01 15:55:45
Could you make this operate on an FD rather than a
Sean Klein
2015/09/01 20:24:48
I will do it in this change for the nonsfi version
| |
79 base::DeleteFile(*file_path, false); | |
80 return false; | |
81 } | |
82 | |
83 // TODO(ncbray): can we ensure temp file deletion even if we crash? | |
84 return true; | |
85 } | |
86 | |
87 // Opens file from filepath, returns descriptor. | |
88 int FileDescFromPath(base::FilePath& path, char* pathname) { | |
89 strncpy(pathname, path.value().c_str(), PATH_MAX); | |
Mark Seaborn
2015/09/01 15:55:45
Can you avoid using fixed length buffers? Why not
Sean Klein
2015/09/01 20:24:48
Done.
| |
90 int fd = open(pathname, O_RDONLY); | |
91 if (fd < 0) { | |
92 LOG(FATAL) << "Failed to open " << pathname << ": " << strerror(errno) | |
93 << "\n"; | |
94 } | |
95 return fd; | |
96 } | |
97 } // namespace | |
98 | |
99 class NaClContentHandler : public mojo::ApplicationDelegate, | |
100 public mojo::ContentHandlerFactory::Delegate { | |
101 public: | |
102 NaClContentHandler() : content_handler_factory_(this) {} | |
103 | |
104 private: | |
105 // Overridden from ApplicationDelegate: | |
106 void Initialize(mojo::ApplicationImpl* app) override {} | |
107 | |
108 // Overridden from ApplicationDelegate: | |
109 bool ConfigureIncomingConnection( | |
110 mojo::ApplicationConnection* connection) override { | |
111 connection->AddService(&content_handler_factory_); | |
112 return true; | |
113 } | |
114 | |
115 // Overridden from ContentHandlerFactory::ManagedDelegate: | |
116 void RunApplication( | |
117 mojo::InterfaceRequest<mojo::Application> application_request, | |
118 mojo::URLResponsePtr response) override { | |
119 // Needed to use Mojo interfaces on this thread. | |
120 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); | |
121 // Aquire the nexe. | |
Mark Seaborn
2015/09/01 15:55:45
"Acquire". Same below.
Sean Klein
2015/09/01 20:24:47
Whoops. Fixed.
| |
122 base::FilePath nexe_path; | |
123 if (!URLResponseToTempFile(response, &nexe_path)) { | |
124 LOG(FATAL) << "could not redirect nexe to temp file"; | |
125 } | |
126 // Aquire the IRT. | |
127 //mojo::URLLoaderPtr url_loader = url_loader_.Pass(); | |
Mark Seaborn
2015/09/01 15:55:45
Remove commented-out code.
Sean Klein
2015/09/01 20:24:47
Done.
| |
128 //LoadIRT(url_loader); | |
129 | |
130 // Run. | |
131 char argv[PATH_MAX + 1]; | |
132 int fd = FileDescFromPath(nexe_path, argv); | |
133 uintptr_t entry = NaClLoadElfFile(fd); | |
134 | |
135 g_mojo_handle = application_request.PassMessagePipe().release().value(); | |
136 int argc = 1; | |
137 char* argvp = &argv[0]; | |
Mark Seaborn
2015/09/01 15:55:45
You're using the temp filename as the program's ar
Sean Klein
2015/09/01 20:24:47
Done.
| |
138 int exit_code = | |
139 nacl_irt_nonsfi_entry(argc, &argvp, environ, | |
Mark Seaborn
2015/09/01 15:55:45
This function is not supposed to return. You can
Sean Klein
2015/09/01 20:24:47
Done.
| |
140 reinterpret_cast<nacl_entry_func_t>(entry), | |
141 mojo_irt_nonsfi_query); | |
142 // TODO(ncbray): are the file handles actually closed at this point? | |
Mark Seaborn
2015/09/01 15:55:45
Yes, |fd| gets closed by NaClLoadElfFile(). See n
Sean Klein
2015/09/01 20:24:48
Added comment noting this.
| |
143 | |
144 // Clean up. | |
145 if (!base::DeleteFile(nexe_path, false)) { | |
Mark Seaborn
2015/09/01 15:55:45
Since control won't reach here, this cleanup is no
Sean Klein
2015/09/01 20:24:48
Done.
| |
146 LOG(FATAL) << "Failed to remove nexe temp file " << nexe_path.value(); | |
147 } | |
148 | |
149 // Exits the process cleanly, does not return. | |
150 mojo::NaClExit(exit_code); | |
151 NOTREACHED(); | |
152 } | |
153 | |
154 mojo::ContentHandlerFactory content_handler_factory_; | |
155 | |
156 DISALLOW_COPY_AND_ASSIGN(NaClContentHandler); | |
157 }; | |
158 | |
159 } // namespace content_handler | |
160 } // namespace nacl | |
161 | |
162 MojoResult MojoMain(MojoHandle application_request) { | |
163 mojo::ApplicationRunnerChromium runner( | |
164 new nacl::content_handler::NaClContentHandler()); | |
165 return runner.Run(application_request); | |
166 } | |
OLD | NEW |