blob: de37211435867ef794b07007103ebc42c31bd680 [file] [log] [blame]
Luis Hector Chavez645501c2016-12-28 10:56:26 -08001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
6#define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
7
8#include <stddef.h>
9
10#include <vector>
11
12#include "base/macros.h"
13#include "base/memory/ref_counted.h"
14#include "build/build_config.h"
15#include "ipc/ipc_export.h"
16
Luis Hector Chavez645501c2016-12-28 10:56:26 -080017namespace IPC {
18
Luis Hector Chavez645501c2016-12-28 10:56:26 -080019class MessageAttachment;
20
21// -----------------------------------------------------------------------------
22// A MessageAttachmentSet is an ordered set of MessageAttachment objects
Jay Civellicfc1eaa2017-08-21 17:18:10 -070023// associated with an IPC message. All attachments are wrapped in a mojo handle
24// if necessary and sent over the mojo message pipe.
Luis Hector Chavez645501c2016-12-28 10:56:26 -080025//
Jay Civellicfc1eaa2017-08-21 17:18:10 -070026// For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In
27// that case, the FD is sent over socket.
Luis Hector Chavez645501c2016-12-28 10:56:26 -080028// -----------------------------------------------------------------------------
29class IPC_EXPORT MessageAttachmentSet
30 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
31 public:
32 MessageAttachmentSet();
33
34 // Return the number of attachments
35 unsigned size() const;
Luis Hector Chavez645501c2016-12-28 10:56:26 -080036
37 // Return true if no unconsumed descriptors remain
Jay Civellicfc1eaa2017-08-21 17:18:10 -070038 bool empty() const { return attachments_.empty(); }
Luis Hector Chavez645501c2016-12-28 10:56:26 -080039
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.
Luis Hector Chavez645501c2016-12-28 10:56:26 -080043 bool AddAttachment(scoped_refptr<MessageAttachment> attachment,
Jay Civellicfc1eaa2017-08-21 17:18:10 -070044 size_t* index);
Luis Hector Chavez645501c2016-12-28 10:56:26 -080045
46 // Similar to the above method, but without output variables.
47 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
48
Jay Civellicfc1eaa2017-08-21 17:18:10 -070049 // 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.
Luis Hector Chavez645501c2016-12-28 10:56:26 -080051 //
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
Jay Civellicfc1eaa2017-08-21 17:18:10 -070055 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
Luis Hector Chavez645501c2016-12-28 10:56:26 -080056
Jay Civellicfc1eaa2017-08-21 17:18:10 -070057 // Marks all the descriptors as consumed and closes those which are
58 // auto-close.
Luis Hector Chavez645501c2016-12-28 10:56:26 -080059 void CommitAllDescriptors();
60
Luis Hector Chavez645501c2016-12-28 10:56:26 -080061#if defined(OS_POSIX)
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
69 // of descriptors to a MessageAttachmentSet.
70 static const size_t kMaxDescriptorsPerMessage = 7;
Luis Hector Chavez645501c2016-12-28 10:56:26 -080071#endif // OS_POSIX
72
73 // ---------------------------------------------------------------------------
74
75 private:
76 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
77
78 ~MessageAttachmentSet();
79
Jay Civellicfc1eaa2017-08-21 17:18:10 -070080 // Return the number of file descriptors
81 unsigned num_descriptors() const;
Luis Hector Chavez645501c2016-12-28 10:56:26 -080082
Jay Civellicfc1eaa2017-08-21 17:18:10 -070083 std::vector<scoped_refptr<MessageAttachment>> attachments_;
Luis Hector Chavez21a249e2017-07-26 17:38:05 +000084
Luis Hector Chavez645501c2016-12-28 10:56:26 -080085 // 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.
Jay Civellicfc1eaa2017-08-21 17:18:10 -070089 unsigned consumed_descriptor_highwater_;
Luis Hector Chavez645501c2016-12-28 10:56:26 -080090
91 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
92};
93
94} // namespace IPC
95
96#endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_