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