xref: /aosp_15_r20/external/libchrome/ipc/ipc_platform_file.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef IPC_IPC_PLATFORM_FILE_H_
6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_PLATFORM_FILE_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include "base/files/file.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h"
10*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
11*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_message_support_export.h"
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
14*635a8641SAndroid Build Coastguard Worker #include "base/file_descriptor_posix.h"
15*635a8641SAndroid Build Coastguard Worker #endif
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace IPC {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
20*635a8641SAndroid Build Coastguard Worker class IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit {
21*635a8641SAndroid Build Coastguard Worker  public:
22*635a8641SAndroid Build Coastguard Worker   // Creates an invalid platform file.
23*635a8641SAndroid Build Coastguard Worker   PlatformFileForTransit();
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker   // Creates a platform file that takes unofficial ownership of |handle|. Note
26*635a8641SAndroid Build Coastguard Worker   // that ownership is not handled by a Scoped* class due to usage patterns of
27*635a8641SAndroid Build Coastguard Worker   // this class and its POSIX counterpart [base::FileDescriptor]. When this
28*635a8641SAndroid Build Coastguard Worker   // class is used as an input to an IPC message, the IPC subsystem will close
29*635a8641SAndroid Build Coastguard Worker   // |handle|. When this class is used as the output from an IPC message, the
30*635a8641SAndroid Build Coastguard Worker   // receiver is expected to take ownership of |handle|.
31*635a8641SAndroid Build Coastguard Worker   explicit PlatformFileForTransit(HANDLE handle);
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker   // Comparison operators.
34*635a8641SAndroid Build Coastguard Worker   bool operator==(const PlatformFileForTransit& platform_file) const;
35*635a8641SAndroid Build Coastguard Worker   bool operator!=(const PlatformFileForTransit& platform_file) const;
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker   HANDLE GetHandle() const;
38*635a8641SAndroid Build Coastguard Worker   bool IsValid() const;
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker  private:
41*635a8641SAndroid Build Coastguard Worker   HANDLE handle_;
42*635a8641SAndroid Build Coastguard Worker };
43*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
44*635a8641SAndroid Build Coastguard Worker typedef base::FileDescriptor PlatformFileForTransit;
45*635a8641SAndroid Build Coastguard Worker #endif
46*635a8641SAndroid Build Coastguard Worker 
InvalidPlatformFileForTransit()47*635a8641SAndroid Build Coastguard Worker inline PlatformFileForTransit InvalidPlatformFileForTransit() {
48*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
49*635a8641SAndroid Build Coastguard Worker   return PlatformFileForTransit();
50*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
51*635a8641SAndroid Build Coastguard Worker   return base::FileDescriptor();
52*635a8641SAndroid Build Coastguard Worker #endif
53*635a8641SAndroid Build Coastguard Worker }
54*635a8641SAndroid Build Coastguard Worker 
PlatformFileForTransitToPlatformFile(const PlatformFileForTransit & transit)55*635a8641SAndroid Build Coastguard Worker inline base::PlatformFile PlatformFileForTransitToPlatformFile(
56*635a8641SAndroid Build Coastguard Worker     const PlatformFileForTransit& transit) {
57*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
58*635a8641SAndroid Build Coastguard Worker   return transit.GetHandle();
59*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
60*635a8641SAndroid Build Coastguard Worker   return transit.fd;
61*635a8641SAndroid Build Coastguard Worker #endif
62*635a8641SAndroid Build Coastguard Worker }
63*635a8641SAndroid Build Coastguard Worker 
PlatformFileForTransitToFile(const PlatformFileForTransit & transit)64*635a8641SAndroid Build Coastguard Worker inline base::File PlatformFileForTransitToFile(
65*635a8641SAndroid Build Coastguard Worker     const PlatformFileForTransit& transit) {
66*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
67*635a8641SAndroid Build Coastguard Worker   return base::File(transit.GetHandle());
68*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
69*635a8641SAndroid Build Coastguard Worker   return base::File(transit.fd);
70*635a8641SAndroid Build Coastguard Worker #endif
71*635a8641SAndroid Build Coastguard Worker }
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker // Creates a new handle that can be passed through IPC. The result must be
74*635a8641SAndroid Build Coastguard Worker // passed to the IPC layer as part of a message, or else it will leak.
75*635a8641SAndroid Build Coastguard Worker IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
76*635a8641SAndroid Build Coastguard Worker GetPlatformFileForTransit(base::PlatformFile file, bool close_source_handle);
77*635a8641SAndroid Build Coastguard Worker 
78*635a8641SAndroid Build Coastguard Worker // Creates a new handle that can be passed through IPC. The result must be
79*635a8641SAndroid Build Coastguard Worker // passed to the IPC layer as part of a message, or else it will leak.
80*635a8641SAndroid Build Coastguard Worker // Note that this function takes ownership of |file|.
81*635a8641SAndroid Build Coastguard Worker IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
82*635a8641SAndroid Build Coastguard Worker TakePlatformFileForTransit(base::File file);
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker }  // namespace IPC
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker #endif  // IPC_IPC_PLATFORM_FILE_H_
87