ipc_platform_file.h 2.8 KB

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