Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 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" |
Sean Klein | 298d994 | 2015-10-12 12:18:53 -0700 | [diff] [blame] | 12 | #include "mojo/nacl/nonsfi/nexe_launcher_nonsfi.h" |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 13 | #include "mojo/public/c/system/main.h" |
Viet-Trung Luu | 0a68a8d | 2016-05-17 16:43:16 -0700 | [diff] [blame] | 14 | #include "mojo/public/cpp/application/application_delegate.h" |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 15 | #include "mojo/public/cpp/application/application_impl.h" |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 16 | |
| 17 | namespace nacl { |
| 18 | namespace content_handler { |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 19 | |
| 20 | class NaClContentHandler : public mojo::ApplicationDelegate, |
| 21 | public mojo::ContentHandlerFactory::Delegate { |
| 22 | public: |
Viet-Trung Luu | 693c379 | 2016-05-19 13:31:07 -0700 | [diff] [blame] | 23 | NaClContentHandler() {} |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 24 | |
| 25 | private: |
| 26 | // Overridden from ApplicationDelegate: |
| 27 | void Initialize(mojo::ApplicationImpl* app) override {} |
| 28 | |
| 29 | // Overridden from ApplicationDelegate: |
| 30 | bool ConfigureIncomingConnection( |
Viet-Trung Luu | 22e78b3 | 2016-05-13 15:27:15 -0700 | [diff] [blame] | 31 | mojo::ServiceProviderImpl* service_provider_impl) override { |
| 32 | service_provider_impl->AddService<mojo::ContentHandler>( |
Viet-Trung Luu | 693c379 | 2016-05-19 13:31:07 -0700 | [diff] [blame] | 33 | mojo::ContentHandlerFactory::GetInterfaceRequestHandler(this)); |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 34 | return true; |
| 35 | } |
| 36 | |
Sean Klein | 5cdf1e7 | 2015-11-03 10:48:16 -0800 | [diff] [blame] | 37 | // Overridden from ContentHandlerFactory::Delegate: |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 38 | void RunApplication( |
| 39 | mojo::InterfaceRequest<mojo::Application> application_request, |
| 40 | mojo::URLResponsePtr response) override { |
| 41 | // Needed to use Mojo interfaces on this thread. |
| 42 | base::MessageLoop loop(mojo::common::MessagePumpMojo::Create()); |
| 43 | // Acquire the nexe. |
Sean Klein | a2b4695 | 2015-09-03 08:54:45 -0700 | [diff] [blame] | 44 | base::ScopedFILE nexe_fp = |
| 45 | mojo::common::BlockingCopyToTempFile(response->body.Pass()); |
Sean Klein | ed509d2 | 2015-11-09 13:41:26 -0800 | [diff] [blame] | 46 | CHECK(nexe_fp) << "Could not redirect nexe to temp file"; |
Sean Klein | a2b4695 | 2015-09-03 08:54:45 -0700 | [diff] [blame] | 47 | FILE* nexe_file_stream = nexe_fp.release(); |
| 48 | int fd = fileno(nexe_file_stream); |
Sean Klein | ed509d2 | 2015-11-09 13:41:26 -0800 | [diff] [blame] | 49 | CHECK_NE(fd, -1) << "Could not open the stream pointer's file descriptor"; |
Sean Klein | a2b4695 | 2015-09-03 08:54:45 -0700 | [diff] [blame] | 50 | fd = dup(fd); |
Sean Klein | ed509d2 | 2015-11-09 13:41:26 -0800 | [diff] [blame] | 51 | CHECK_NE(fd, -1) << "Could not dup the file descriptor"; |
| 52 | CHECK_EQ(fclose(nexe_file_stream), 0) << "Failed to close temp file"; |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 53 | |
Sean Klein | 9b9b2a6 | 2015-10-02 15:49:15 -0700 | [diff] [blame] | 54 | MojoHandle handle = |
| 55 | application_request.PassMessagePipe().release().value(); |
| 56 | // MojoLaunchNexeNonsfi takes ownership of the fd. |
Sean Klein | 2ba2b8d | 2015-10-28 16:38:09 -0700 | [diff] [blame] | 57 | MojoLaunchNexeNonsfi(fd, handle, false /* enable_translation_irt */); |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Sean Klein | 4f0aa60 | 2015-09-02 15:53:17 -0700 | [diff] [blame] | 60 | DISALLOW_COPY_AND_ASSIGN(NaClContentHandler); |
| 61 | }; |
| 62 | |
| 63 | } // namespace content_handler |
| 64 | } // namespace nacl |
| 65 | |
| 66 | MojoResult MojoMain(MojoHandle application_request) { |
| 67 | mojo::ApplicationRunnerChromium runner( |
| 68 | new nacl::content_handler::NaClContentHandler()); |
| 69 | return runner.Run(application_request); |
| 70 | } |