• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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 "components/nacl/common/nacl_service.h"
6 
7 #include <memory>
8 #include <string>
9 
10 #include "base/command_line.h"
11 #include "base/task/single_thread_task_runner.h"
12 #include "build/build_config.h"
13 #include "mojo/core/embedder/scoped_ipc_support.h"
14 #include "mojo/public/cpp/platform/platform_channel.h"
15 #include "mojo/public/cpp/platform/platform_channel_endpoint.h"
16 #include "mojo/public/cpp/platform/platform_handle.h"
17 #include "mojo/public/cpp/system/invitation.h"
18 
19 #if BUILDFLAG(IS_POSIX)
20 #include "base/files/scoped_file.h"
21 #include "base/posix/global_descriptors.h"
22 #include "content/public/common/content_descriptors.h"
23 #endif
24 
25 #if BUILDFLAG(IS_APPLE)
26 #include "base/mac/mach_port_rendezvous.h"
27 #endif
28 
29 namespace {
30 
GetMojoInvitation()31 mojo::IncomingInvitation GetMojoInvitation() {
32   mojo::PlatformChannelEndpoint endpoint;
33 #if BUILDFLAG(IS_WIN)
34   endpoint = mojo::PlatformChannel::RecoverPassedEndpointFromCommandLine(
35       *base::CommandLine::ForCurrentProcess());
36 #elif BUILDFLAG(IS_APPLE)
37   auto* client = base::MachPortRendezvousClient::GetInstance();
38   if (client) {
39     endpoint = mojo::PlatformChannelEndpoint(
40         mojo::PlatformHandle(client->TakeReceiveRight('mojo')));
41   }
42 #else
43   endpoint = mojo::PlatformChannelEndpoint(mojo::PlatformHandle(base::ScopedFD(
44       base::GlobalDescriptors::GetInstance()->Get(kMojoIPCChannel))));
45 #endif  // !BUILDFLAG(IS_WIN)
46   DCHECK(endpoint.is_valid());
47   return mojo::IncomingInvitation::Accept(std::move(endpoint));
48 }
49 
50 }  // namespace
51 
NaClService(scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner)52 NaClService::NaClService(
53     scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner)
54     : ipc_support_(std::move(ipc_task_runner),
55                    mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST) {}
56 
57 NaClService::~NaClService() = default;
58 
TakeChannelPipe()59 mojo::ScopedMessagePipeHandle NaClService::TakeChannelPipe() {
60   return GetMojoInvitation().ExtractMessagePipe(0);
61 }
62