• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include "mojo/core/platform_handle_utils.h"
6 
7 #include "build/build_config.h"
8 
9 #if defined(OS_FUCHSIA)
10 #include <lib/zx/vmo.h>
11 #elif defined(OS_POSIX)
12 #include "base/files/scoped_file.h"
13 #elif defined(OS_WIN)
14 #include "base/win/scoped_handle.h"
15 #endif
16 
17 #if defined(OS_MACOSX) && !defined(OS_IOS)
18 #include "base/mac/scoped_mach_port.h"
19 #endif
20 
21 namespace mojo {
22 namespace core {
23 
ExtractPlatformHandlesFromSharedMemoryRegionHandle(base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle handle,PlatformHandle * extracted_handle,PlatformHandle * extracted_readonly_handle)24 void ExtractPlatformHandlesFromSharedMemoryRegionHandle(
25     base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle handle,
26     PlatformHandle* extracted_handle,
27     PlatformHandle* extracted_readonly_handle) {
28 #if defined(OS_WIN)
29   *extracted_handle = PlatformHandle(base::win::ScopedHandle(handle.Take()));
30 #elif defined(OS_FUCHSIA)
31   *extracted_handle = PlatformHandle(std::move(handle));
32 #elif defined(OS_MACOSX) && !defined(OS_IOS)
33   // This is a Mach port. Same code as above and below, but separated for
34   // clarity.
35   *extracted_handle = PlatformHandle(std::move(handle));
36 #elif defined(OS_ANDROID)
37   // This is a file descriptor. Same code as above, but separated for clarity.
38   *extracted_handle = PlatformHandle(std::move(handle));
39 #else
40   *extracted_handle = PlatformHandle(std::move(handle.fd));
41   *extracted_readonly_handle = PlatformHandle(std::move(handle.readonly_fd));
42 #endif
43 }
44 
45 base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle
CreateSharedMemoryRegionHandleFromPlatformHandles(PlatformHandle handle,PlatformHandle readonly_handle)46 CreateSharedMemoryRegionHandleFromPlatformHandles(
47     PlatformHandle handle,
48     PlatformHandle readonly_handle) {
49 #if defined(OS_WIN)
50   DCHECK(!readonly_handle.is_valid());
51   return handle.TakeHandle();
52 #elif defined(OS_FUCHSIA)
53   DCHECK(!readonly_handle.is_valid());
54   return zx::vmo(handle.TakeHandle());
55 #elif defined(OS_MACOSX) && !defined(OS_IOS)
56   DCHECK(!readonly_handle.is_valid());
57   return handle.TakeMachPort();
58 #elif defined(OS_ANDROID)
59   DCHECK(!readonly_handle.is_valid());
60   return handle.TakeFD();
61 #else
62   return base::subtle::ScopedFDPair(handle.TakeFD(), readonly_handle.TakeFD());
63 #endif
64 }
65 
66 }  // namespace core
67 }  // namespace mojo
68