OLD | NEW |
(Empty) | |
| 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 CHROME_COMMON_DESCRIPTOR_SET_POSIX_H_ |
| 6 #define CHROME_COMMON_DESCRIPTOR_SET_POSIX_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/file_descriptor_posix.h" |
| 12 |
| 13 // ----------------------------------------------------------------------------- |
| 14 // A DescriptorSet is an ordered set of POSIX file descriptors. These are |
| 15 // associated with IPC messages so that descriptors can be transmitted over a |
| 16 // UNIX domain socket. |
| 17 // ----------------------------------------------------------------------------- |
| 18 class DescriptorSet { |
| 19 public: |
| 20 DescriptorSet(); |
| 21 ~DescriptorSet(); |
| 22 |
| 23 // This is the maximum number of descriptors per message. We need to know this |
| 24 // because the control message kernel interface has to be given a buffer which |
| 25 // is large enough to store all the descriptor numbers. Otherwise the kernel |
| 26 // tells us that it truncated the control data and the extra descriptors are |
| 27 // lost. |
| 28 // |
| 29 // In debugging mode, it's a fatal error to try and add more than this number |
| 30 // of descriptors to a DescriptorSet. |
| 31 enum { |
| 32 MAX_DESCRIPTORS_PER_MESSAGE = 4, |
| 33 }; |
| 34 |
| 35 // --------------------------------------------------------------------------- |
| 36 // Interfaces for building during message serialisation... |
| 37 |
| 38 // Add a descriptor to the end of the set. Returns false iff the set is full. |
| 39 bool Add(int fd); |
| 40 // Add a descriptor to the end of the set and automatically close it after |
| 41 // transmission. Returns false iff the set is full. |
| 42 bool AddAndAutoClose(int fd); |
| 43 |
| 44 // --------------------------------------------------------------------------- |
| 45 |
| 46 |
| 47 // --------------------------------------------------------------------------- |
| 48 // Interfaces for accessing during message deserialisation... |
| 49 |
| 50 // Return the number of descriptors remaining |
| 51 unsigned size() const { return descriptors_.size() - next_descriptor_; } |
| 52 // Return true if no unconsumed descriptors remain |
| 53 bool empty() const { return descriptors_.size() == next_descriptor_; } |
| 54 // Fetch the next descriptor from the beginning of the set. This interface is |
| 55 // designed for the deserialising code as it doesn't support close flags. |
| 56 // returns: file descriptor, or -1 on error |
| 57 int NextDescriptor(); |
| 58 |
| 59 // --------------------------------------------------------------------------- |
| 60 |
| 61 |
| 62 // --------------------------------------------------------------------------- |
| 63 // Interfaces for transmission... |
| 64 |
| 65 // Fill an array with file descriptors without 'consuming' them. CommitAll |
| 66 // must be called after these descriptors have been transmitted. |
| 67 // buffer: (output) a buffer of, at least, size() integers. |
| 68 void GetDescriptors(int* buffer) const; |
| 69 // This must be called after transmitting the descriptors returned by |
| 70 // GetDescriptors. It marks all the descriptors as consumed and closes those |
| 71 // which are auto-close. |
| 72 void CommitAll(); |
| 73 |
| 74 // --------------------------------------------------------------------------- |
| 75 |
| 76 |
| 77 // --------------------------------------------------------------------------- |
| 78 // Interfaces for receiving... |
| 79 |
| 80 // Set the contents of the set from the given buffer. This set must be empty |
| 81 // before calling. The auto-close flag is set on all the descriptors so that |
| 82 // unconsumed descriptors are closed on destruction. |
| 83 void SetDescriptors(const int* buffer, unsigned count); |
| 84 |
| 85 // --------------------------------------------------------------------------- |
| 86 |
| 87 // --------------------------------------------------------------------------- |
| 88 // Interfaces for IPC::Message... |
| 89 |
| 90 // Take all the FileDescriptors from another set. Just like a copy |
| 91 // constructor, except that the source is emptied. |
| 92 void TakeFrom(DescriptorSet* other); |
| 93 |
| 94 // --------------------------------------------------------------------------- |
| 95 |
| 96 private: |
| 97 // A vector of descriptors and close flags. If this message is sent, then |
| 98 // these descriptors are sent as control data. After sending, any descriptors |
| 99 // with a true flag are closed. If this message has been received, then these |
| 100 // are the descriptors which were received and all close flags are true. |
| 101 std::vector<base::FileDescriptor> descriptors_; |
| 102 // When deserialising the message, the descriptors will be extracted |
| 103 // one-by-one. This contains the index of the next unused descriptor. |
| 104 unsigned next_descriptor_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(DescriptorSet); |
| 107 }; |
| 108 |
| 109 #endif // CHROME_COMMON_FILE_DESCRIPTOR_POSIX_H_ |
OLD | NEW |