Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 1 | // Copyright (c) 2011 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 IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
| 6 | #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "build/build_config.h" |
| 15 | #include "ipc/ipc_export.h" |
| 16 | |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 17 | namespace IPC { |
| 18 | |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 19 | class MessageAttachment; |
| 20 | |
| 21 | // ----------------------------------------------------------------------------- |
| 22 | // A MessageAttachmentSet is an ordered set of MessageAttachment objects |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 23 | // associated with an IPC message. All attachments are wrapped in a mojo handle |
| 24 | // if necessary and sent over the mojo message pipe. |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 25 | // |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 26 | // For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In |
| 27 | // that case, the FD is sent over socket. |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 28 | // ----------------------------------------------------------------------------- |
| 29 | class IPC_EXPORT MessageAttachmentSet |
| 30 | : public base::RefCountedThreadSafe<MessageAttachmentSet> { |
| 31 | public: |
| 32 | MessageAttachmentSet(); |
| 33 | |
| 34 | // Return the number of attachments |
| 35 | unsigned size() const; |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 36 | |
| 37 | // Return true if no unconsumed descriptors remain |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 38 | bool empty() const { return attachments_.empty(); } |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 39 | |
| 40 | // Returns whether the attachment was successfully added. |
| 41 | // |index| is an output variable. On success, it contains the index of the |
| 42 | // newly added attachment. |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 43 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment, |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 44 | size_t* index); |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 45 | |
| 46 | // Similar to the above method, but without output variables. |
| 47 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment); |
| 48 | |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 49 | // Take the nth from the beginning of the vector, Code using this /must/ |
| 50 | // access the attachments in order, and must do it at most once. |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 51 | // |
| 52 | // This interface is designed for the deserialising code as it doesn't |
| 53 | // support close flags. |
| 54 | // returns: an attachment, or nullptr on error |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 55 | scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index); |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 56 | |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 57 | // Marks all the descriptors as consumed and closes those which are |
| 58 | // auto-close. |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 59 | void CommitAllDescriptors(); |
| 60 | |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 61 | #if defined(OS_POSIX) |
| 62 | // This is the maximum number of descriptors per message. We need to know this |
| 63 | // because the control message kernel interface has to be given a buffer which |
| 64 | // is large enough to store all the descriptor numbers. Otherwise the kernel |
| 65 | // tells us that it truncated the control data and the extra descriptors are |
| 66 | // lost. |
| 67 | // |
| 68 | // In debugging mode, it's a fatal error to try and add more than this number |
| 69 | // of descriptors to a MessageAttachmentSet. |
| 70 | static const size_t kMaxDescriptorsPerMessage = 7; |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 71 | #endif // OS_POSIX |
| 72 | |
| 73 | // --------------------------------------------------------------------------- |
| 74 | |
| 75 | private: |
| 76 | friend class base::RefCountedThreadSafe<MessageAttachmentSet>; |
| 77 | |
| 78 | ~MessageAttachmentSet(); |
| 79 | |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 80 | // Return the number of file descriptors |
| 81 | unsigned num_descriptors() const; |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 82 | |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 83 | std::vector<scoped_refptr<MessageAttachment>> attachments_; |
Luis Hector Chavez | 21a249e | 2017-07-26 17:38:05 +0000 | [diff] [blame] | 84 | |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 85 | // This contains the index of the next descriptor which should be consumed. |
| 86 | // It's used in a couple of ways. Firstly, at destruction we can check that |
| 87 | // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
| 88 | // can check that they are read in order. |
Jay Civelli | cfc1eaa | 2017-08-21 17:18:10 -0700 | [diff] [blame] | 89 | unsigned consumed_descriptor_highwater_; |
Luis Hector Chavez | 645501c | 2016-12-28 10:56:26 -0800 | [diff] [blame] | 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); |
| 92 | }; |
| 93 | |
| 94 | } // namespace IPC |
| 95 | |
| 96 | #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |