nacl_service.cc 1.9 KB

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