blob: de37211435867ef794b07007103ebc42c31bd680 [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
morrita33a35902015-01-15 06:17:06 +09005#ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
6#define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09007
avi42ebda42015-12-22 11:39:04 +09008#include <stddef.h>
9
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090010#include <vector>
11
tfarina2d565d32015-09-16 18:56:21 +090012#include "base/macros.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090013#include "base/memory/ref_counted.h"
avi42ebda42015-12-22 11:39:04 +090014#include "build/build_config.h"
darin@chromium.org80e4c5e2011-08-16 05:41:46 +090015#include "ipc/ipc_export.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090016
morrita33a35902015-01-15 06:17:06 +090017namespace IPC {
18
morrita6308f322015-01-27 07:42:54 +090019class MessageAttachment;
20
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090021// -----------------------------------------------------------------------------
erikchenc8fa4212015-10-10 11:43:49 +090022// A MessageAttachmentSet is an ordered set of MessageAttachment objects
sammc14583362016-11-23 12:17:35 +090023// associated with an IPC message. All attachments are wrapped in a mojo handle
24// if necessary and sent over the mojo message pipe.
erikchenc8fa4212015-10-10 11:43:49 +090025//
sammc14583362016-11-23 12:17:35 +090026// For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In
27// that case, the FD is sent over socket.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090028// -----------------------------------------------------------------------------
morrita33a35902015-01-15 06:17:06 +090029class IPC_EXPORT MessageAttachmentSet
30 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090031 public:
morrita33a35902015-01-15 06:17:06 +090032 MessageAttachmentSet();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090033
morrita6308f322015-01-27 07:42:54 +090034 // Return the number of attachments
morrita33a35902015-01-15 06:17:06 +090035 unsigned size() const;
morrita1b52e4c2015-02-06 09:58:30 +090036
morrita33a35902015-01-15 06:17:06 +090037 // Return true if no unconsumed descriptors remain
sammc14583362016-11-23 12:17:35 +090038 bool empty() const { return attachments_.empty(); }
morrita33a35902015-01-15 06:17:06 +090039
erikchenc8fa4212015-10-10 11:43:49 +090040 // 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.
erikchenc8fa4212015-10-10 11:43:49 +090043 bool AddAttachment(scoped_refptr<MessageAttachment> attachment,
sammc14583362016-11-23 12:17:35 +090044 size_t* index);
erikchenc8fa4212015-10-10 11:43:49 +090045
46 // Similar to the above method, but without output variables.
morrita7d1bfcc2015-01-31 14:45:42 +090047 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
48
sammc14583362016-11-23 12:17:35 +090049 // 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.
morrita7d1bfcc2015-01-31 14:45:42 +090051 //
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
sammc14583362016-11-23 12:17:35 +090055 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
erikchenc8fa4212015-10-10 11:43:49 +090056
sammc14583362016-11-23 12:17:35 +090057 // Marks all the descriptors as consumed and closes those which are
58 // auto-close.
erikchenc8fa4212015-10-10 11:43:49 +090059 void CommitAllDescriptors();
morrita1b52e4c2015-02-06 09:58:30 +090060
morrita33a35902015-01-15 06:17:06 +090061#if defined(OS_POSIX)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090062 // 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
morrita33a35902015-01-15 06:17:06 +090069 // of descriptors to a MessageAttachmentSet.
yusukesdf3387d2015-05-07 04:45:45 +090070 static const size_t kMaxDescriptorsPerMessage = 7;
morrita33a35902015-01-15 06:17:06 +090071#endif // OS_POSIX
72
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090073 // ---------------------------------------------------------------------------
74
75 private:
morrita33a35902015-01-15 06:17:06 +090076 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
jam@chromium.orgb1f47b22009-11-06 06:53:08 +090077
morrita33a35902015-01-15 06:17:06 +090078 ~MessageAttachmentSet();
jam@chromium.orgb1f47b22009-11-06 06:53:08 +090079
sammc14583362016-11-23 12:17:35 +090080 // Return the number of file descriptors
81 unsigned num_descriptors() const;
erikchenc8fa4212015-10-10 11:43:49 +090082
sammc14583362016-11-23 12:17:35 +090083 std::vector<scoped_refptr<MessageAttachment>> attachments_;
horoe816a0c2016-11-16 10:52:54 +090084
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090085 // 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.
sammc14583362016-11-23 12:17:35 +090089 unsigned consumed_descriptor_highwater_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090090
morrita33a35902015-01-15 06:17:06 +090091 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090092};
93
morrita33a35902015-01-15 06:17:06 +090094} // namespace IPC
95
96#endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_