blob: 158816a7506abe524baf329fa955bae0ebd3e91c [file] [log] [blame]
Sean Klein4f0aa602015-09-02 15:53:17 -07001// 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 Klein298d9942015-10-12 12:18:53 -070012#include "mojo/nacl/nonsfi/nexe_launcher_nonsfi.h"
Sean Klein4f0aa602015-09-02 15:53:17 -070013#include "mojo/public/c/system/main.h"
Viet-Trung Luu0a68a8d2016-05-17 16:43:16 -070014#include "mojo/public/cpp/application/application_delegate.h"
Sean Klein4f0aa602015-09-02 15:53:17 -070015#include "mojo/public/cpp/application/application_impl.h"
Sean Klein4f0aa602015-09-02 15:53:17 -070016
17namespace nacl {
18namespace content_handler {
Sean Klein4f0aa602015-09-02 15:53:17 -070019
20class NaClContentHandler : public mojo::ApplicationDelegate,
21 public mojo::ContentHandlerFactory::Delegate {
22 public:
Viet-Trung Luu693c3792016-05-19 13:31:07 -070023 NaClContentHandler() {}
Sean Klein4f0aa602015-09-02 15:53:17 -070024
25 private:
26 // Overridden from ApplicationDelegate:
27 void Initialize(mojo::ApplicationImpl* app) override {}
28
29 // Overridden from ApplicationDelegate:
30 bool ConfigureIncomingConnection(
Viet-Trung Luu22e78b32016-05-13 15:27:15 -070031 mojo::ServiceProviderImpl* service_provider_impl) override {
32 service_provider_impl->AddService<mojo::ContentHandler>(
Viet-Trung Luu693c3792016-05-19 13:31:07 -070033 mojo::ContentHandlerFactory::GetInterfaceRequestHandler(this));
Sean Klein4f0aa602015-09-02 15:53:17 -070034 return true;
35 }
36
Sean Klein5cdf1e72015-11-03 10:48:16 -080037 // Overridden from ContentHandlerFactory::Delegate:
Sean Klein4f0aa602015-09-02 15:53:17 -070038 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 Kleina2b46952015-09-03 08:54:45 -070044 base::ScopedFILE nexe_fp =
45 mojo::common::BlockingCopyToTempFile(response->body.Pass());
Sean Kleined509d22015-11-09 13:41:26 -080046 CHECK(nexe_fp) << "Could not redirect nexe to temp file";
Sean Kleina2b46952015-09-03 08:54:45 -070047 FILE* nexe_file_stream = nexe_fp.release();
48 int fd = fileno(nexe_file_stream);
Sean Kleined509d22015-11-09 13:41:26 -080049 CHECK_NE(fd, -1) << "Could not open the stream pointer's file descriptor";
Sean Kleina2b46952015-09-03 08:54:45 -070050 fd = dup(fd);
Sean Kleined509d22015-11-09 13:41:26 -080051 CHECK_NE(fd, -1) << "Could not dup the file descriptor";
52 CHECK_EQ(fclose(nexe_file_stream), 0) << "Failed to close temp file";
Sean Klein4f0aa602015-09-02 15:53:17 -070053
Sean Klein9b9b2a62015-10-02 15:49:15 -070054 MojoHandle handle =
55 application_request.PassMessagePipe().release().value();
56 // MojoLaunchNexeNonsfi takes ownership of the fd.
Sean Klein2ba2b8d2015-10-28 16:38:09 -070057 MojoLaunchNexeNonsfi(fd, handle, false /* enable_translation_irt */);
Sean Klein4f0aa602015-09-02 15:53:17 -070058 }
59
Sean Klein4f0aa602015-09-02 15:53:17 -070060 DISALLOW_COPY_AND_ASSIGN(NaClContentHandler);
61};
62
63} // namespace content_handler
64} // namespace nacl
65
66MojoResult MojoMain(MojoHandle application_request) {
67 mojo::ApplicationRunnerChromium runner(
68 new nacl::content_handler::NaClContentHandler());
69 return runner.Run(application_request);
70}