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