platform_handle_utils.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2018 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 "mojo/core/platform_handle_utils.h"
  5. #include "build/build_config.h"
  6. #if BUILDFLAG(IS_FUCHSIA)
  7. #include <lib/zx/process.h>
  8. #include <lib/zx/vmo.h>
  9. #elif BUILDFLAG(IS_POSIX)
  10. #include "base/files/scoped_file.h"
  11. #elif BUILDFLAG(IS_WIN)
  12. #include <windows.h>
  13. #include "base/win/scoped_handle.h"
  14. #endif
  15. #if BUILDFLAG(IS_APPLE)
  16. #include "base/mac/scoped_mach_port.h"
  17. #endif
  18. namespace mojo {
  19. namespace core {
  20. void ExtractPlatformHandlesFromSharedMemoryRegionHandle(
  21. base::subtle::ScopedPlatformSharedMemoryHandle handle,
  22. PlatformHandle* extracted_handle,
  23. PlatformHandle* extracted_readonly_handle) {
  24. #if BUILDFLAG(IS_WIN)
  25. *extracted_handle = PlatformHandle(base::win::ScopedHandle(handle.Take()));
  26. #elif BUILDFLAG(IS_FUCHSIA)
  27. *extracted_handle = PlatformHandle(std::move(handle));
  28. #elif BUILDFLAG(IS_APPLE)
  29. // This is a Mach port. Same code as above and below, but separated for
  30. // clarity.
  31. *extracted_handle = PlatformHandle(std::move(handle));
  32. #elif BUILDFLAG(IS_ANDROID)
  33. // This is a file descriptor. Same code as above, but separated for clarity.
  34. *extracted_handle = PlatformHandle(std::move(handle));
  35. #else
  36. *extracted_handle = PlatformHandle(std::move(handle.fd));
  37. *extracted_readonly_handle = PlatformHandle(std::move(handle.readonly_fd));
  38. #endif
  39. }
  40. base::subtle::ScopedPlatformSharedMemoryHandle
  41. CreateSharedMemoryRegionHandleFromPlatformHandles(
  42. PlatformHandle handle,
  43. PlatformHandle readonly_handle) {
  44. #if BUILDFLAG(IS_WIN)
  45. DCHECK(!readonly_handle.is_valid());
  46. return handle.TakeHandle();
  47. #elif BUILDFLAG(IS_FUCHSIA)
  48. DCHECK(!readonly_handle.is_valid());
  49. return zx::vmo(handle.TakeHandle());
  50. #elif BUILDFLAG(IS_APPLE)
  51. DCHECK(!readonly_handle.is_valid());
  52. return handle.TakeMachSendRight();
  53. #elif BUILDFLAG(IS_ANDROID)
  54. DCHECK(!readonly_handle.is_valid());
  55. return handle.TakeFD();
  56. #else
  57. return base::subtle::ScopedFDPair(handle.TakeFD(), readonly_handle.TakeFD());
  58. #endif
  59. }
  60. MojoResult UnwrapAndClonePlatformProcessHandle(
  61. const MojoPlatformProcessHandle* process_handle,
  62. base::Process& process) {
  63. if (process_handle->struct_size < sizeof(*process_handle))
  64. return MOJO_RESULT_INVALID_ARGUMENT;
  65. #if BUILDFLAG(IS_WIN)
  66. base::ProcessHandle in_handle = reinterpret_cast<base::ProcessHandle>(
  67. static_cast<uintptr_t>(process_handle->value));
  68. #else
  69. base::ProcessHandle in_handle =
  70. static_cast<base::ProcessHandle>(process_handle->value);
  71. #endif
  72. if (in_handle == base::kNullProcessHandle) {
  73. process = base::Process();
  74. return MOJO_RESULT_OK;
  75. }
  76. #if BUILDFLAG(IS_WIN)
  77. base::ProcessHandle out_handle;
  78. if (!::DuplicateHandle(::GetCurrentProcess(), in_handle,
  79. ::GetCurrentProcess(), &out_handle, 0, FALSE,
  80. DUPLICATE_SAME_ACCESS)) {
  81. return MOJO_RESULT_INVALID_ARGUMENT;
  82. }
  83. process = base::Process(out_handle);
  84. #elif BUILDFLAG(IS_FUCHSIA)
  85. zx::process out;
  86. if (zx::unowned_process(in_handle)->duplicate(ZX_RIGHT_SAME_RIGHTS, &out) !=
  87. ZX_OK) {
  88. return MOJO_RESULT_INVALID_ARGUMENT;
  89. }
  90. process = base::Process(out.release());
  91. #else
  92. process = base::Process(in_handle);
  93. #endif
  94. return MOJO_RESULT_OK;
  95. }
  96. } // namespace core
  97. } // namespace mojo