levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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 | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
| 6 | #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 7 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
tfarina | 2d565d3 | 2015-09-16 18:56:21 +0900 | [diff] [blame] | 12 | #include "base/macros.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 14 | #include "build/build_config.h" |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 15 | #include "ipc/ipc_message_support_export.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 16 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 17 | namespace IPC { |
| 18 | |
morrita | 6308f32 | 2015-01-27 07:42:54 +0900 | [diff] [blame] | 19 | class MessageAttachment; |
| 20 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 21 | // ----------------------------------------------------------------------------- |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 22 | // A MessageAttachmentSet is an ordered set of MessageAttachment objects |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [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. |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 25 | // |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 26 | // For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In |
| 27 | // that case, the FD is sent over socket. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 28 | // ----------------------------------------------------------------------------- |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 29 | class IPC_MESSAGE_SUPPORT_EXPORT MessageAttachmentSet |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 30 | : public base::RefCountedThreadSafe<MessageAttachmentSet> { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 31 | public: |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 32 | MessageAttachmentSet(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 33 | |
morrita | 6308f32 | 2015-01-27 07:42:54 +0900 | [diff] [blame] | 34 | // Return the number of attachments |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 35 | unsigned size() const; |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 36 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 37 | // Return true if no unconsumed descriptors remain |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 38 | bool empty() const { return attachments_.empty(); } |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 39 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 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. |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 43 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment, |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 44 | size_t* index); |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 45 | |
| 46 | // Similar to the above method, but without output variables. |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 47 | bool AddAttachment(scoped_refptr<MessageAttachment> attachment); |
| 48 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [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. |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [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 |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 55 | scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index); |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 56 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 57 | // Marks all the descriptors as consumed and closes those which are |
| 58 | // auto-close. |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 59 | void CommitAllDescriptors(); |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 60 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 61 | #if defined(OS_POSIX) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 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 |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 69 | // of descriptors to a MessageAttachmentSet. |
yusukes | df3387d | 2015-05-07 04:45:45 +0900 | [diff] [blame] | 70 | static const size_t kMaxDescriptorsPerMessage = 7; |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 71 | #endif // OS_POSIX |
| 72 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 73 | // --------------------------------------------------------------------------- |
| 74 | |
| 75 | private: |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 76 | friend class base::RefCountedThreadSafe<MessageAttachmentSet>; |
jam@chromium.org | b1f47b2 | 2009-11-06 06:53:08 +0900 | [diff] [blame] | 77 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 78 | ~MessageAttachmentSet(); |
jam@chromium.org | b1f47b2 | 2009-11-06 06:53:08 +0900 | [diff] [blame] | 79 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 80 | // Return the number of file descriptors |
| 81 | unsigned num_descriptors() const; |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 82 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 83 | std::vector<scoped_refptr<MessageAttachment>> attachments_; |
horo | e816a0c | 2016-11-16 10:52:54 +0900 | [diff] [blame] | 84 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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. |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 89 | unsigned consumed_descriptor_highwater_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 90 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 91 | DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 92 | }; |
| 93 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 94 | } // namespace IPC |
| 95 | |
| 96 | #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |