[M73 Dev][Tizen] Fix compilation errors for TV profile
[platform/framework/web/chromium-efl.git] / base / file_descriptor_posix.h
1 // Copyright (c) 2006-2009 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
5 #ifndef BASE_FILE_DESCRIPTOR_POSIX_H_
6 #define BASE_FILE_DESCRIPTOR_POSIX_H_
7
8 #include "base/files/file.h"
9 #include "base/files/scoped_file.h"
10
11 namespace base {
12
13 constexpr int kInvalidFd = -1;
14
15 // -----------------------------------------------------------------------------
16 // We introduct a special structure for file descriptors in order that we are
17 // able to use template specialisation to special-case their handling.
18 //
19 // IMPORTANT: This is primarily intended for use when sending file descriptors
20 // over IPC. Even if |auto_close| is true, base::FileDescriptor does NOT close()
21 // |fd| when going out of scope. Instead, a consumer of a base::FileDescriptor
22 // must invoke close() on |fd| if |auto_close| is true.
23 //
24 // In the case of IPC, the the IPC subsystem knows to close() |fd| after sending
25 // a message that contains a base::FileDescriptor if auto_close == true. On the
26 // other end, the receiver must make sure to close() |fd| after it has finished
27 // processing the IPC message. See the IPC::ParamTraits<> specialization in
28 // ipc/ipc_message_utils.h for all the details.
29 // -----------------------------------------------------------------------------
30 struct FileDescriptor {
31   FileDescriptor() : fd(kInvalidFd), auto_close(false) {}
32
33   FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) {
34   }
35
36   FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {}
37   explicit FileDescriptor(ScopedFD fd) : fd(fd.release()), auto_close(true) {}
38
39   bool operator==(const FileDescriptor& other) const {
40     return (fd == other.fd && auto_close == other.auto_close);
41   }
42
43   bool operator!=(const FileDescriptor& other) const {
44     return !operator==(other);
45   }
46
47   // A comparison operator so that we can use these as keys in a std::map.
48   bool operator<(const FileDescriptor& other) const {
49     return other.fd < fd;
50   }
51
52   int fd;
53   // If true, this file descriptor should be closed after it has been used. For
54   // example an IPC system might interpret this flag as indicating that the
55   // file descriptor it has been given should be closed after use.
56   bool auto_close;
57 };
58
59 }  // namespace base
60
61 #endif  // BASE_FILE_DESCRIPTOR_POSIX_H_