1 // Copyright 2011 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.
5 #ifndef IPC_IPC_PLATFORM_FILE_H_
6 #define IPC_IPC_PLATFORM_FILE_H_
8 #include "base/files/file.h"
9 #include "base/process/process.h"
10 #include "build/build_config.h"
11 #include "ipc/ipc_message_support_export.h"
13 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
14 #include "base/file_descriptor_posix.h"
20 class IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit {
22 // Creates an invalid platform file.
23 PlatformFileForTransit();
25 // Creates a platform file that takes unofficial ownership of |handle|. Note
26 // that ownership is not handled by a Scoped* class due to usage patterns of
27 // this class and its POSIX counterpart [base::FileDescriptor]. When this
28 // class is used as an input to an IPC message, the IPC subsystem will close
29 // |handle|. When this class is used as the output from an IPC message, the
30 // receiver is expected to take ownership of |handle|.
31 explicit PlatformFileForTransit(HANDLE handle);
33 // Comparison operators.
34 bool operator==(const PlatformFileForTransit& platform_file) const;
35 bool operator!=(const PlatformFileForTransit& platform_file) const;
37 HANDLE GetHandle() const;
43 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
44 typedef base::FileDescriptor PlatformFileForTransit;
47 inline PlatformFileForTransit InvalidPlatformFileForTransit() {
49 return PlatformFileForTransit();
50 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
51 return base::FileDescriptor();
55 inline base::PlatformFile PlatformFileForTransitToPlatformFile(
56 const PlatformFileForTransit& transit) {
58 return transit.GetHandle();
59 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
64 inline base::File PlatformFileForTransitToFile(
65 const PlatformFileForTransit& transit) {
67 return base::File(transit.GetHandle());
68 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
69 return base::File(transit.fd);
73 // Creates a new handle that can be passed through IPC. The result must be
74 // passed to the IPC layer as part of a message, or else it will leak.
75 IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
76 GetPlatformFileForTransit(base::PlatformFile file, bool close_source_handle);
78 // Creates a new handle that can be passed through IPC. The result must be
79 // passed to the IPC layer as part of a message, or else it will leak.
80 // Note that this function takes ownership of |file|.
81 IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
82 TakePlatformFileForTransit(base::File file);
86 #endif // IPC_IPC_PLATFORM_FILE_H_