blob: 1704b943c3cba14e22ae140add0f94e13e0ffaaa [file] [log] [blame]
jcivelli@chromium.org3bab1032011-06-15 04:35:10 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
estade@chromium.org3ac32062009-11-17 07:55:17 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef IPC_IPC_PLATFORM_FILE_H_
6#define IPC_IPC_PLATFORM_FILE_H_
7
rvargas@chromium.org9e469f62014-01-28 06:36:00 +09008#include "base/files/file.h"
rsesek@chromium.org19319712013-07-24 14:15:24 +09009#include "base/process/process.h"
avi42ebda42015-12-22 11:39:04 +090010#include "build/build_config.h"
Ken Rockotb3a77c92017-09-14 13:23:41 +090011#include "ipc/ipc_message_support_export.h"
estade@chromium.org3ac32062009-11-17 07:55:17 +090012
13#if defined(OS_POSIX)
14#include "base/file_descriptor_posix.h"
15#endif
16
17namespace IPC {
18
19#if defined(OS_WIN)
Ken Rockotb3a77c92017-09-14 13:23:41 +090020class IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit {
erikchen9a454c12017-04-29 11:24:36 +090021 public:
22 // Creates an invalid platform file.
23 PlatformFileForTransit();
24
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);
32
33 // Comparison operators.
34 bool operator==(const PlatformFileForTransit& platform_file) const;
35 bool operator!=(const PlatformFileForTransit& platform_file) const;
36
37 HANDLE GetHandle() const;
38 bool IsValid() const;
39
40 private:
41 HANDLE handle_;
42};
estade@chromium.org3ac32062009-11-17 07:55:17 +090043#elif defined(OS_POSIX)
44typedef base::FileDescriptor PlatformFileForTransit;
45#endif
46
estade@chromium.orgd9963452009-12-18 09:23:05 +090047inline PlatformFileForTransit InvalidPlatformFileForTransit() {
48#if defined(OS_WIN)
erikchenb23e3a42016-03-30 07:26:42 +090049 return PlatformFileForTransit();
estade@chromium.orgd9963452009-12-18 09:23:05 +090050#elif defined(OS_POSIX)
51 return base::FileDescriptor();
52#endif
53}
54
estade@chromium.org3ac32062009-11-17 07:55:17 +090055inline base::PlatformFile PlatformFileForTransitToPlatformFile(
56 const PlatformFileForTransit& transit) {
57#if defined(OS_WIN)
erikchenb23e3a42016-03-30 07:26:42 +090058 return transit.GetHandle();
estade@chromium.org3ac32062009-11-17 07:55:17 +090059#elif defined(OS_POSIX)
60 return transit.fd;
61#endif
62}
63
rvargas@chromium.org5e1b33d2014-03-25 15:01:48 +090064inline base::File PlatformFileForTransitToFile(
65 const PlatformFileForTransit& transit) {
66#if defined(OS_WIN)
erikchenb23e3a42016-03-30 07:26:42 +090067 return base::File(transit.GetHandle());
rvargas@chromium.org5e1b33d2014-03-25 15:01:48 +090068#elif defined(OS_POSIX)
69 return base::File(transit.fd);
70#endif
71}
72
erikchen556548b2016-04-07 03:51:55 +090073// 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.
Ken Rockotb3a77c92017-09-14 13:23:41 +090075IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
76GetPlatformFileForTransit(base::PlatformFile file, bool close_source_handle);
jcivelli@chromium.org3bab1032011-06-15 04:35:10 +090077
erikchen33f45fa2016-04-08 01:35:11 +090078// 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.
rvargas@chromium.orgf40fe382014-03-06 05:13:49 +090080// Note that this function takes ownership of |file|.
Ken Rockotb3a77c92017-09-14 13:23:41 +090081IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
82TakePlatformFileForTransit(base::File file);
rvargas@chromium.orgf40fe382014-03-06 05:13:49 +090083
estade@chromium.org3ac32062009-11-17 07:55:17 +090084} // namespace IPC
85
86#endif // IPC_IPC_PLATFORM_FILE_H_