| 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 CHROME_COMMON_DESCRIPTOR_SET_POSIX_H_ | 5 #ifndef CHROME_COMMON_DESCRIPTOR_SET_POSIX_H_ |
| 6 #define CHROME_COMMON_DESCRIPTOR_SET_POSIX_H_ | 6 #define CHROME_COMMON_DESCRIPTOR_SET_POSIX_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_descriptor_posix.h" | 11 #include "base/file_descriptor_posix.h" |
| 12 #include "base/ref_counted.h" |
| 12 | 13 |
| 13 // ----------------------------------------------------------------------------- | 14 // ----------------------------------------------------------------------------- |
| 14 // A DescriptorSet is an ordered set of POSIX file descriptors. These are | 15 // 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 // associated with IPC messages so that descriptors can be transmitted over a |
| 16 // UNIX domain socket. | 17 // UNIX domain socket. |
| 17 // ----------------------------------------------------------------------------- | 18 // ----------------------------------------------------------------------------- |
| 18 class DescriptorSet { | 19 class DescriptorSet : public base::RefCountedThreadSafe<DescriptorSet> { |
| 19 public: | 20 public: |
| 20 DescriptorSet(); | 21 DescriptorSet(); |
| 21 ~DescriptorSet(); | 22 ~DescriptorSet(); |
| 22 | 23 |
| 23 // This is the maximum number of descriptors per message. We need to know this | 24 // 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 // 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 // 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 // tells us that it truncated the control data and the extra descriptors are |
| 27 // lost. | 28 // lost. |
| 28 // | 29 // |
| (...skipping 11 matching lines...) Expand all Loading... |
| 40 // Add a descriptor to the end of the set and automatically close it after | 41 // Add a descriptor to the end of the set and automatically close it after |
| 41 // transmission. Returns false iff the set is full. | 42 // transmission. Returns false iff the set is full. |
| 42 bool AddAndAutoClose(int fd); | 43 bool AddAndAutoClose(int fd); |
| 43 | 44 |
| 44 // --------------------------------------------------------------------------- | 45 // --------------------------------------------------------------------------- |
| 45 | 46 |
| 46 | 47 |
| 47 // --------------------------------------------------------------------------- | 48 // --------------------------------------------------------------------------- |
| 48 // Interfaces for accessing during message deserialisation... | 49 // Interfaces for accessing during message deserialisation... |
| 49 | 50 |
| 50 // Return the number of descriptors remaining | 51 // Return the number of descriptors |
| 51 unsigned size() const { return descriptors_.size() - next_descriptor_; } | 52 unsigned size() const { return descriptors_.size(); } |
| 52 // Return true if no unconsumed descriptors remain | 53 // Return true if no unconsumed descriptors remain |
| 53 bool empty() const { return descriptors_.size() == next_descriptor_; } | 54 bool empty() const { return descriptors_.empty(); } |
| 54 // Fetch the next descriptor from the beginning of the set. This interface is | 55 // Fetch the nth descriptor from the beginning of the set. Code using this |
| 55 // designed for the deserialising code as it doesn't support close flags. | 56 // /must/ access the descriptors in order, except that it may wrap from the |
| 57 // end to index 0 again. |
| 58 // |
| 59 // This interface is designed for the deserialising code as it doesn't |
| 60 // support close flags. |
| 56 // returns: file descriptor, or -1 on error | 61 // returns: file descriptor, or -1 on error |
| 57 int NextDescriptor(); | 62 int GetDescriptorAt(unsigned n) const; |
| 58 | 63 |
| 59 // --------------------------------------------------------------------------- | 64 // --------------------------------------------------------------------------- |
| 60 | 65 |
| 61 | 66 |
| 62 // --------------------------------------------------------------------------- | 67 // --------------------------------------------------------------------------- |
| 63 // Interfaces for transmission... | 68 // Interfaces for transmission... |
| 64 | 69 |
| 65 // Fill an array with file descriptors without 'consuming' them. CommitAll | 70 // Fill an array with file descriptors without 'consuming' them. CommitAll |
| 66 // must be called after these descriptors have been transmitted. | 71 // must be called after these descriptors have been transmitted. |
| 67 // buffer: (output) a buffer of, at least, size() integers. | 72 // buffer: (output) a buffer of, at least, size() integers. |
| 68 void GetDescriptors(int* buffer) const; | 73 void GetDescriptors(int* buffer) const; |
| 69 // This must be called after transmitting the descriptors returned by | 74 // This must be called after transmitting the descriptors returned by |
| 70 // GetDescriptors. It marks all the descriptors as consumed and closes those | 75 // GetDescriptors. It marks all the descriptors as consumed and closes those |
| 71 // which are auto-close. | 76 // which are auto-close. |
| 72 void CommitAll(); | 77 void CommitAll(); |
| 73 | 78 |
| 74 // --------------------------------------------------------------------------- | 79 // --------------------------------------------------------------------------- |
| 75 | 80 |
| 76 | 81 |
| 77 // --------------------------------------------------------------------------- | 82 // --------------------------------------------------------------------------- |
| 78 // Interfaces for receiving... | 83 // Interfaces for receiving... |
| 79 | 84 |
| 80 // Set the contents of the set from the given buffer. This set must be empty | 85 // 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 | 86 // before calling. The auto-close flag is set on all the descriptors so that |
| 82 // unconsumed descriptors are closed on destruction. | 87 // unconsumed descriptors are closed on destruction. |
| 83 void SetDescriptors(const int* buffer, unsigned count); | 88 void SetDescriptors(const int* buffer, unsigned count); |
| 84 | 89 |
| 85 // --------------------------------------------------------------------------- | 90 // --------------------------------------------------------------------------- |
| 86 | 91 |
| 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: | 92 private: |
| 97 // A vector of descriptors and close flags. If this message is sent, then | 93 // 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 | 94 // 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 | 95 // 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. | 96 // are the descriptors which were received and all close flags are true. |
| 101 std::vector<base::FileDescriptor> descriptors_; | 97 std::vector<base::FileDescriptor> descriptors_; |
| 102 // When deserialising the message, the descriptors will be extracted | 98 |
| 103 // one-by-one. This contains the index of the next unused descriptor. | 99 // This contains the index of the next descriptor which should be consumed. |
| 104 unsigned next_descriptor_; | 100 // It's used in a couple of ways. Firstly, at destruction we can check that |
| 101 // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
| 102 // can check that they are read in order. |
| 103 mutable unsigned consumed_descriptor_highwater_; |
| 105 | 104 |
| 106 DISALLOW_COPY_AND_ASSIGN(DescriptorSet); | 105 DISALLOW_COPY_AND_ASSIGN(DescriptorSet); |
| 107 }; | 106 }; |
| 108 | 107 |
| 109 #endif // CHROME_COMMON_FILE_DESCRIPTOR_POSIX_H_ | 108 #endif // CHROME_COMMON_FILE_DESCRIPTOR_POSIX_H_ |
| OLD | NEW |