[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 | // ----------------------------------------------------------------------------- |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 24 | // A MessageAttachmentSet is an ordered set of MessageAttachment objects. These |
| 25 | // are associated with IPC messages so that attachments, each of which is either |
| 26 | // a platform file or a mojo handle, can be transmitted over the underlying UNIX |
| 27 | // domain socket (for ChannelPosix) or Mojo MessagePipe (for ChannelMojo). |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 28 | // ----------------------------------------------------------------------------- |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 29 | class IPC_EXPORT MessageAttachmentSet |
| 30 | : public base::RefCountedThreadSafe<MessageAttachmentSet> { |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 31 | public: |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 32 | MessageAttachmentSet(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 33 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 34 | // Return the number of attachments |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 35 | unsigned size() const; |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 36 | // Return the number of file descriptors |
| 37 | unsigned num_descriptors() const; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 38 | // Return the number of mojo handles in the attachment set |
| 39 | unsigned num_mojo_handles() const; |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 40 | // Return the number of brokerable attachments in the attachment set. |
| 41 | unsigned num_brokerable_attachments() const; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 42 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 43 | // Return true if no unconsumed descriptors remain |
| 44 | bool empty() const { return 0 == size(); } |
| 45 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 46 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment); |
| 47 | |
| 48 | // Take the nth attachment from the beginning of the set, Code using this |
| 49 | // /must/ access the attachments in order, and must do it at most once. |
| 50 | // |
| 51 | // This interface is designed for the deserialising code as it doesn't |
| 52 | // support close flags. |
| 53 | // returns: an attachment, or nullptr on error |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 54 | scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index); |
| 55 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 56 | // This must be called after transmitting the descriptors returned by |
| 57 | // PeekDescriptors. It marks all the descriptors as consumed and closes those |
| 58 | // which are auto-close. |
| 59 | void CommitAll(); |
| 60 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 61 | // Returns a vector of all brokerable attachments. |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 62 | std::vector<BrokerableAttachment*> GetBrokerableAttachments() const; |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 63 | |
| 64 | // Replaces a placeholder brokerable attachment with |attachment|, matching |
| 65 | // them by their id. |
| 66 | void ReplacePlaceholderWithAttachment( |
| 67 | const scoped_refptr<BrokerableAttachment>& attachment); |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 68 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 69 | #if defined(OS_POSIX) |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 70 | // This is the maximum number of descriptors per message. We need to know this |
| 71 | // because the control message kernel interface has to be given a buffer which |
| 72 | // is large enough to store all the descriptor numbers. Otherwise the kernel |
| 73 | // tells us that it truncated the control data and the extra descriptors are |
| 74 | // lost. |
| 75 | // |
| 76 | // 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] | 77 | // of descriptors to a MessageAttachmentSet. |
yusukes | c986c543 | 2015-05-06 19:45:45 | [diff] [blame] | 78 | static const size_t kMaxDescriptorsPerMessage = 7; |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 79 | |
| 80 | // --------------------------------------------------------------------------- |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 81 | // Interfaces for transmission... |
| 82 | |
| 83 | // Fill an array with file descriptors without 'consuming' them. CommitAll |
| 84 | // must be called after these descriptors have been transmitted. |
| 85 | // buffer: (output) a buffer of, at least, size() integers. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 86 | void PeekDescriptors(base::PlatformFile* buffer) const; |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 87 | // Returns true if any contained file descriptors appear to be handles to a |
| 88 | // directory. |
| 89 | bool ContainsDirectoryDescriptor() const; |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 90 | // Fetch all filedescriptors with the "auto close" property. |
| 91 | // Used instead of CommitAll() when closing must be handled manually. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 92 | void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 93 | |
| 94 | // --------------------------------------------------------------------------- |
| 95 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 96 | // --------------------------------------------------------------------------- |
| 97 | // Interfaces for receiving... |
| 98 | |
| 99 | // Set the contents of the set from the given buffer. This set must be empty |
| 100 | // before calling. The auto-close flag is set on all the descriptors so that |
| 101 | // unconsumed descriptors are closed on destruction. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 102 | void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 103 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 104 | #endif // OS_POSIX |
| 105 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 106 | // --------------------------------------------------------------------------- |
| 107 | |
| 108 | private: |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 109 | friend class base::RefCountedThreadSafe<MessageAttachmentSet>; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 110 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 111 | ~MessageAttachmentSet(); |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 112 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 113 | // A vector of attachments of the message, which might be |PlatformFile| or |
| 114 | // |MessagePipe|. |
| 115 | std::vector<scoped_refptr<MessageAttachment>> attachments_; |
| 116 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 117 | // This contains the index of the next descriptor which should be consumed. |
| 118 | // It's used in a couple of ways. Firstly, at destruction we can check that |
| 119 | // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
| 120 | // can check that they are read in order. |
| 121 | mutable unsigned consumed_descriptor_highwater_; |
| 122 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 124 | }; |
| 125 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 126 | } // namespace IPC |
| 127 | |
| 128 | #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |