[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
| 6 | #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | |
tfarina | 4da8275 | 2015-09-16 09:56:21 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 12 | #include "ipc/ipc_export.h" |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 13 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 14 | #if defined(OS_POSIX) |
| 15 | #include "base/files/file.h" |
| 16 | #endif |
| 17 | |
| 18 | namespace IPC { |
| 19 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 20 | class BrokerableAttachment; |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 21 | class MessageAttachment; |
| 22 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 23 | // ----------------------------------------------------------------------------- |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 24 | // A MessageAttachmentSet is an ordered set of MessageAttachment objects |
| 25 | // associated with an IPC message. There are three types of MessageAttachments: |
| 26 | // 1) TYPE_PLATFORM_FILE is transmitted over the Channel's underlying |
| 27 | // UNIX domain socket |
| 28 | // 2) TYPE_MOJO_HANDLE is transmitted over the Mojo MessagePipe. |
| 29 | // 3) TYPE_BROKERABLE_ATTACHMENT is transmitted by the Attachment Broker. |
| 30 | // Any given IPC Message can have attachments of type (1) or (2), but not both. |
| 31 | // These are stored in |attachments_|. Attachments of type (3) are stored in |
| 32 | // |brokerable_attachments_|. |
| 33 | // |
| 34 | // To produce a deterministic ordering, all attachments in |attachments_| are |
| 35 | // considered to come before those in |brokerable_attachments_|. These |
| 36 | // attachments are transmitted across different communication channels, and |
| 37 | // multiplexed by the receiver, so ordering between them cannot be guaranteed. |
| 38 | // |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 39 | // ----------------------------------------------------------------------------- |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 40 | class IPC_EXPORT MessageAttachmentSet |
| 41 | : public base::RefCountedThreadSafe<MessageAttachmentSet> { |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 42 | public: |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 43 | MessageAttachmentSet(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 44 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 45 | // Return the number of attachments |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 46 | unsigned size() const; |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 47 | // Return the number of file descriptors |
| 48 | unsigned num_descriptors() const; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 49 | // Return the number of mojo handles in the attachment set |
| 50 | unsigned num_mojo_handles() const; |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 51 | // Return the number of brokerable attachments in the attachment set. |
| 52 | unsigned num_brokerable_attachments() const; |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 53 | // Return the number of non-brokerable attachments in the attachment set. |
| 54 | unsigned num_non_brokerable_attachments() const; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 55 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 56 | // Return true if no unconsumed descriptors remain |
| 57 | bool empty() const { return 0 == size(); } |
| 58 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 59 | // Returns whether the attachment was successfully added. |
| 60 | // |index| is an output variable. On success, it contains the index of the |
| 61 | // newly added attachment. |
| 62 | // |brokerable| is an output variable. On success, it describes which vector |
| 63 | // the attachment was added to. |
| 64 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment, |
| 65 | size_t* index, |
| 66 | bool* brokerable); |
| 67 | |
| 68 | // Similar to the above method, but without output variables. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 69 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment); |
| 70 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 71 | // Take the nth non-brokerable attachment from the beginning of the vector, |
| 72 | // Code using this /must/ access the attachments in order, and must do it at |
| 73 | // most once. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 74 | // |
| 75 | // This interface is designed for the deserialising code as it doesn't |
| 76 | // support close flags. |
| 77 | // returns: an attachment, or nullptr on error |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 78 | scoped_refptr<MessageAttachment> GetNonBrokerableAttachmentAt(unsigned index); |
| 79 | |
| 80 | // Similar to GetNonBrokerableAttachmentAt, but there are no ordering |
| 81 | // requirements. |
| 82 | scoped_refptr<MessageAttachment> GetBrokerableAttachmentAt(unsigned index); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 83 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 84 | // This must be called after transmitting the descriptors returned by |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 85 | // PeekDescriptors. It marks all the non-brokerable descriptors as consumed |
| 86 | // and closes those which are auto-close. |
| 87 | void CommitAllDescriptors(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 88 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 89 | // Returns a vector of all brokerable attachments. |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 90 | std::vector<BrokerableAttachment*> GetBrokerableAttachments() const; |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 91 | |
| 92 | // Replaces a placeholder brokerable attachment with |attachment|, matching |
| 93 | // them by their id. |
| 94 | void ReplacePlaceholderWithAttachment( |
| 95 | const scoped_refptr<BrokerableAttachment>& attachment); |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 96 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 97 | #if defined(OS_POSIX) |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 98 | // This is the maximum number of descriptors per message. We need to know this |
| 99 | // because the control message kernel interface has to be given a buffer which |
| 100 | // is large enough to store all the descriptor numbers. Otherwise the kernel |
| 101 | // tells us that it truncated the control data and the extra descriptors are |
| 102 | // lost. |
| 103 | // |
| 104 | // In debugging mode, it's a fatal error to try and add more than this number |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 105 | // of descriptors to a MessageAttachmentSet. |
yusukes | c986c543 | 2015-05-06 19:45:45 | [diff] [blame] | 106 | static const size_t kMaxDescriptorsPerMessage = 7; |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 107 | |
| 108 | // --------------------------------------------------------------------------- |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 109 | // Interfaces for transmission... |
| 110 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 111 | // Fill an array with file descriptors without 'consuming' them. |
| 112 | // CommitAllDescriptors must be called after these descriptors have been |
| 113 | // transmitted. |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 114 | // buffer: (output) a buffer of, at least, size() integers. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 115 | void PeekDescriptors(base::PlatformFile* buffer) const; |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 116 | // Returns true if any contained file descriptors appear to be handles to a |
| 117 | // directory. |
| 118 | bool ContainsDirectoryDescriptor() const; |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 119 | // Fetch all filedescriptors with the "auto close" property. Used instead of |
| 120 | // CommitAllDescriptors() when closing must be handled manually. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 121 | void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 122 | |
| 123 | // --------------------------------------------------------------------------- |
| 124 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 125 | // --------------------------------------------------------------------------- |
| 126 | // Interfaces for receiving... |
| 127 | |
| 128 | // Set the contents of the set from the given buffer. This set must be empty |
| 129 | // before calling. The auto-close flag is set on all the descriptors so that |
| 130 | // unconsumed descriptors are closed on destruction. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 131 | void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 132 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 133 | #endif // OS_POSIX |
| 134 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 135 | // --------------------------------------------------------------------------- |
| 136 | |
| 137 | private: |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 138 | friend class base::RefCountedThreadSafe<MessageAttachmentSet>; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 139 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 140 | ~MessageAttachmentSet(); |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 141 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 142 | // All elements either have type TYPE_PLATFORM_FILE or TYPE_MOJO_HANDLE. |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 143 | std::vector<scoped_refptr<MessageAttachment>> attachments_; |
| 144 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame^] | 145 | // All elements have type TYPE_BROKERABLE_ATTACHMENT. |
| 146 | std::vector<scoped_refptr<BrokerableAttachment>> brokerable_attachments_; |
| 147 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 148 | // This contains the index of the next descriptor which should be consumed. |
| 149 | // It's used in a couple of ways. Firstly, at destruction we can check that |
| 150 | // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
| 151 | // can check that they are read in order. |
| 152 | mutable unsigned consumed_descriptor_highwater_; |
| 153 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 154 | DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 155 | }; |
| 156 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 157 | } // namespace IPC |
| 158 | |
| 159 | #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |