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