OLD | NEW |
---|---|
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_FILE_DESCRIPTOR_POSIX_H_ | 5 #ifndef BASE_FILE_DESCRIPTOR_POSIX_H_ |
6 #define BASE_FILE_DESCRIPTOR_POSIX_H_ | 6 #define BASE_FILE_DESCRIPTOR_POSIX_H_ |
7 | 7 |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/files/scoped_file.h" | 9 #include "base/files/scoped_file.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 // ----------------------------------------------------------------------------- | 13 // ----------------------------------------------------------------------------- |
14 // We introduct a special structure for file descriptors in order that we are | 14 // We introduct a special structure for file descriptors in order that we are |
15 // able to use template specialisation to special-case their handling. | 15 // able to use template specialisation to special-case their handling. |
16 // | 16 // |
17 // WARNING: (Chromium only) There are subtleties to consider if serialising | 17 // IMPORTANT: This is primarily intended for use when sending file descriptors |
18 // these objects over IPC. See comments in ipc/ipc_message_utils.h | 18 // over IPC. Even if |auto_close| is true, base::FileDescriptor does NOT close() |
19 // above the template specialisation for this structure. | 19 // |fd| when going out of scope. Instead, a consumer of a base::FileDescriptor |
20 // must invoke close() on |fd| if |auto_close| is true. | |
21 // | |
22 // In the case of IPC, the the IPC subsystem knows to close() |fd| after sending | |
23 // a message that contains a base::FileDescriptor if auto_close == true. On the | |
24 // other end, the receiver must make sure to close() |fd| after its finished | |
Nico
2015/08/18 22:32:30
s/its/it has/
dcheng
2015/08/18 23:30:14
Done.
| |
25 // processing the IPC message. See the IPC::ParamTraits<> specialization in | |
26 // ipc/ipc_message_utils.h for all the details. | |
27 // | |
28 // TL;DR: I'm a sign, not a cop. Don't use this class unless you absolutely have | |
Nico
2015/08/18 22:32:30
remove this paragraph
dcheng
2015/08/18 23:30:14
Aww, I really wanted to find a legitimate way to u
| |
29 // to. | |
20 // ----------------------------------------------------------------------------- | 30 // ----------------------------------------------------------------------------- |
21 struct FileDescriptor { | 31 struct FileDescriptor { |
22 FileDescriptor() : fd(-1), auto_close(false) {} | 32 FileDescriptor() : fd(-1), auto_close(false) {} |
23 | 33 |
24 FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) { | 34 FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) { |
25 } | 35 } |
26 | 36 |
27 FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {} | 37 FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {} |
28 explicit FileDescriptor(ScopedFD fd) : fd(fd.release()), auto_close(true) {} | 38 explicit FileDescriptor(ScopedFD fd) : fd(fd.release()), auto_close(true) {} |
29 | 39 |
(...skipping 13 matching lines...) Expand all Loading... | |
43 int fd; | 53 int fd; |
44 // If true, this file descriptor should be closed after it has been used. For | 54 // If true, this file descriptor should be closed after it has been used. For |
45 // example an IPC system might interpret this flag as indicating that the | 55 // example an IPC system might interpret this flag as indicating that the |
46 // file descriptor it has been given should be closed after use. | 56 // file descriptor it has been given should be closed after use. |
47 bool auto_close; | 57 bool auto_close; |
48 }; | 58 }; |
49 | 59 |
50 } // namespace base | 60 } // namespace base |
51 | 61 |
52 #endif // BASE_FILE_DESCRIPTOR_POSIX_H_ | 62 #endif // BASE_FILE_DESCRIPTOR_POSIX_H_ |
OLD | NEW |