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/mojo_interface_nacl_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/nonsfi/elf_loader.h" | |
16 | |
17 namespace nacl { | |
18 namespace content_handler { | |
19 namespace { | |
20 | |
21 // Copies response (input) into new temporary file open by fd (output). | |
22 bool URLResponseToTempFile(mojo::URLResponsePtr& response, int* fd) { | |
23 base::FilePath path; | |
24 if (!base::CreateTemporaryFile(&path)) { | |
25 return false; | |
26 } | |
27 | |
28 if (!mojo::common::BlockingCopyToFile(response->body.Pass(), path)) { | |
Mark Seaborn
2015/09/01 21:05:47
This still has the issue that if the process is ki
Sean Klein
2015/09/01 22:55:27
Made a separate CL for this issue (I'll post it as
Mark Seaborn
2015/09/02 20:27:15
Yes, that's still a possibility, but the window is
| |
29 base::DeleteFile(path, false); | |
30 return false; | |
31 } | |
32 *fd = open(path.value().c_str(), O_RDONLY); | |
33 if (fd < 0) { | |
Mark Seaborn
2015/09/01 21:05:47
fd is a pointer. This is "fd < NULL". :-)
Sean Klein
2015/09/01 22:55:27
Whoops! Changed to (*fd < 0)
| |
34 LOG(FATAL) << "Failed to open " << path.value().c_str() << ": " | |
35 << strerror(errno) << "\n"; | |
36 } | |
37 | |
38 // Since we unlink an open file, the temp file will be removed as soon as the | |
39 // fd is closed. | |
40 if (unlink(path.value().c_str())) { | |
41 LOG(FATAL) << "Failed to unlink " << path.value().c_str() << ": " | |
42 << strerror(errno) << "\n"; | |
43 } | |
44 return true; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 class NaClContentHandler : public mojo::ApplicationDelegate, | |
50 public mojo::ContentHandlerFactory::Delegate { | |
51 public: | |
52 NaClContentHandler() : content_handler_factory_(this) {} | |
53 | |
54 private: | |
55 // Overridden from ApplicationDelegate: | |
56 void Initialize(mojo::ApplicationImpl* app) override {} | |
57 | |
58 // Overridden from ApplicationDelegate: | |
59 bool ConfigureIncomingConnection( | |
60 mojo::ApplicationConnection* connection) override { | |
61 connection->AddService(&content_handler_factory_); | |
62 return true; | |
63 } | |
64 | |
65 // Overridden from ContentHandlerFactory::ManagedDelegate: | |
66 void RunApplication( | |
67 mojo::InterfaceRequest<mojo::Application> application_request, | |
68 mojo::URLResponsePtr response) override { | |
69 // Needed to use Mojo interfaces on this thread. | |
70 base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); | |
71 // Acquire the nexe. | |
72 int fd; | |
73 if (!URLResponseToTempFile(response, &fd)) { | |
74 LOG(FATAL) << "could not redirect nexe to temp file"; | |
75 } | |
76 | |
77 // Run -- Also, closes the fd, removing the temp file. | |
78 uintptr_t entry = NaClLoadElfFile(fd); | |
79 | |
80 mojo_set_initial_handle( | |
81 application_request.PassMessagePipe().release().value()); | |
82 int argc = 1; | |
83 char* argvp = (char *) "NaClMain"; | |
Mark Seaborn
2015/09/01 21:05:47
Nit: use C++ style cast
Sean Klein
2015/09/01 22:55:27
Using const_cast.
| |
84 nacl_irt_nonsfi_entry(argc, &argvp, environ, | |
Mark Seaborn
2015/09/01 21:05:47
Let's use an empty set of env vars instead of pass
Sean Klein
2015/09/01 22:55:27
Done.
| |
85 reinterpret_cast<nacl_entry_func_t>(entry), | |
86 mojo_irt_nonsfi_query); | |
87 abort(); | |
88 NOTREACHED(); | |
89 } | |
90 | |
91 mojo::ContentHandlerFactory content_handler_factory_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(NaClContentHandler); | |
94 }; | |
95 | |
96 } // namespace content_handler | |
97 } // namespace nacl | |
98 | |
99 MojoResult MojoMain(MojoHandle application_request) { | |
100 mojo::ApplicationRunnerChromium runner( | |
101 new nacl::content_handler::NaClContentHandler()); | |
102 return runner.Run(application_request); | |
103 } | |
OLD | NEW |