blob: e174ee83d85adc79dab8e485647b19a2f2061826 [file] [log] [blame]
[email protected]e7f009d2011-06-14 19:35:101// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]cb6037d2009-11-16 22:55:172// 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
[email protected]141bcc52014-01-27 21:36:008#include "base/files/file.h"
[email protected]e66ef602013-07-24 05:15:249#include "base/process/process.h"
avi246998d82015-12-22 02:39:0410#include "build/build_config.h"
Ken Rockotfd907632017-09-14 04:23:4111#include "ipc/ipc_message_support_export.h"
[email protected]cb6037d2009-11-16 22:55:1712
Fabrice de Gans-Ribericbce4342018-05-07 20:02:0913#if defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]cb6037d2009-11-16 22:55:1714#include "base/file_descriptor_posix.h"
15#endif
16
17namespace IPC {
18
19#if defined(OS_WIN)
Ken Rockotfd907632017-09-14 04:23:4120class IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit {
erikchend804e1052017-04-29 02:24:3621 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};
Fabrice de Gans-Ribericbce4342018-05-07 20:02:0943#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]cb6037d2009-11-16 22:55:1744typedef base::FileDescriptor PlatformFileForTransit;
45#endif
46
[email protected]a784b842009-12-18 00:23:0547inline PlatformFileForTransit InvalidPlatformFileForTransit() {
48#if defined(OS_WIN)
erikchen19e5f692016-03-29 22:26:4249 return PlatformFileForTransit();
Fabrice de Gans-Ribericbce4342018-05-07 20:02:0950#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]a784b842009-12-18 00:23:0551 return base::FileDescriptor();
52#endif
53}
54
[email protected]cb6037d2009-11-16 22:55:1755inline base::PlatformFile PlatformFileForTransitToPlatformFile(
56 const PlatformFileForTransit& transit) {
57#if defined(OS_WIN)
erikchen19e5f692016-03-29 22:26:4258 return transit.GetHandle();
Fabrice de Gans-Ribericbce4342018-05-07 20:02:0959#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]cb6037d2009-11-16 22:55:1760 return transit.fd;
61#endif
62}
63
[email protected]008e3b32014-03-25 06:01:4864inline base::File PlatformFileForTransitToFile(
65 const PlatformFileForTransit& transit) {
66#if defined(OS_WIN)
erikchen19e5f692016-03-29 22:26:4267 return base::File(transit.GetHandle());
Fabrice de Gans-Ribericbce4342018-05-07 20:02:0968#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]008e3b32014-03-25 06:01:4869 return base::File(transit.fd);
70#endif
71}
72
erikchen09b40032016-04-06 18:51:5573// 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 Rockotfd907632017-09-14 04:23:4175IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
76GetPlatformFileForTransit(base::PlatformFile file, bool close_source_handle);
[email protected]e7f009d2011-06-14 19:35:1077
erikchen41e60832016-04-07 16:35:1178// 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.
[email protected]8880e702014-03-05 20:13:4980// Note that this function takes ownership of |file|.
Ken Rockotfd907632017-09-14 04:23:4181IPC_MESSAGE_SUPPORT_EXPORT PlatformFileForTransit
82TakePlatformFileForTransit(base::File file);
[email protected]8880e702014-03-05 20:13:4983
[email protected]cb6037d2009-11-16 22:55:1784} // namespace IPC
85
86#endif // IPC_IPC_PLATFORM_FILE_H_