xref: /aosp_15_r20/external/libchrome/ipc/ipc_message_attachment_set.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <vector>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
14*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
15*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_message_support_export.h"
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace IPC {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker class MessageAttachment;
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
22*635a8641SAndroid Build Coastguard Worker // A MessageAttachmentSet is an ordered set of MessageAttachment objects
23*635a8641SAndroid Build Coastguard Worker // associated with an IPC message. All attachments are wrapped in a mojo handle
24*635a8641SAndroid Build Coastguard Worker // if necessary and sent over the mojo message pipe.
25*635a8641SAndroid Build Coastguard Worker //
26*635a8641SAndroid Build Coastguard Worker // For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In
27*635a8641SAndroid Build Coastguard Worker // that case, the FD is sent over socket.
28*635a8641SAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
29*635a8641SAndroid Build Coastguard Worker class IPC_MESSAGE_SUPPORT_EXPORT MessageAttachmentSet
30*635a8641SAndroid Build Coastguard Worker     : public base::RefCountedThreadSafe<MessageAttachmentSet> {
31*635a8641SAndroid Build Coastguard Worker  public:
32*635a8641SAndroid Build Coastguard Worker   MessageAttachmentSet();
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker   // Return the number of attachments
35*635a8641SAndroid Build Coastguard Worker   unsigned size() const;
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker   // Return true if no unconsumed descriptors remain
empty()38*635a8641SAndroid Build Coastguard Worker   bool empty() const { return attachments_.empty(); }
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker   // Returns whether the attachment was successfully added.
41*635a8641SAndroid Build Coastguard Worker   // |index| is an output variable. On success, it contains the index of the
42*635a8641SAndroid Build Coastguard Worker   // newly added attachment.
43*635a8641SAndroid Build Coastguard Worker   bool AddAttachment(scoped_refptr<MessageAttachment> attachment,
44*635a8641SAndroid Build Coastguard Worker                      size_t* index);
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker   // Similar to the above method, but without output variables.
47*635a8641SAndroid Build Coastguard Worker   bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker   // Take the nth from the beginning of the vector, Code using this /must/
50*635a8641SAndroid Build Coastguard Worker   // access the attachments in order, and must do it at most once.
51*635a8641SAndroid Build Coastguard Worker   //
52*635a8641SAndroid Build Coastguard Worker   // This interface is designed for the deserialising code as it doesn't
53*635a8641SAndroid Build Coastguard Worker   // support close flags.
54*635a8641SAndroid Build Coastguard Worker   //   returns: an attachment, or nullptr on error
55*635a8641SAndroid Build Coastguard Worker   scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker   // Marks all the descriptors as consumed and closes those which are
58*635a8641SAndroid Build Coastguard Worker   // auto-close.
59*635a8641SAndroid Build Coastguard Worker   void CommitAllDescriptors();
60*635a8641SAndroid Build Coastguard Worker 
61*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
62*635a8641SAndroid Build Coastguard Worker   // This is the maximum number of descriptors per message. We need to know this
63*635a8641SAndroid Build Coastguard Worker   // because the control message kernel interface has to be given a buffer which
64*635a8641SAndroid Build Coastguard Worker   // is large enough to store all the descriptor numbers. Otherwise the kernel
65*635a8641SAndroid Build Coastguard Worker   // tells us that it truncated the control data and the extra descriptors are
66*635a8641SAndroid Build Coastguard Worker   // lost.
67*635a8641SAndroid Build Coastguard Worker   //
68*635a8641SAndroid Build Coastguard Worker   // In debugging mode, it's a fatal error to try and add more than this number
69*635a8641SAndroid Build Coastguard Worker   // of descriptors to a MessageAttachmentSet.
70*635a8641SAndroid Build Coastguard Worker   static const size_t kMaxDescriptorsPerMessage = 7;
71*635a8641SAndroid Build Coastguard Worker #endif  // OS_POSIX || OS_FUCHSIA
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker   // ---------------------------------------------------------------------------
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker  private:
76*635a8641SAndroid Build Coastguard Worker   friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
77*635a8641SAndroid Build Coastguard Worker 
78*635a8641SAndroid Build Coastguard Worker   ~MessageAttachmentSet();
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   // Return the number of file descriptors
81*635a8641SAndroid Build Coastguard Worker   unsigned num_descriptors() const;
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker   std::vector<scoped_refptr<MessageAttachment>> attachments_;
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker   // This contains the index of the next descriptor which should be consumed.
86*635a8641SAndroid Build Coastguard Worker   // It's used in a couple of ways. Firstly, at destruction we can check that
87*635a8641SAndroid Build Coastguard Worker   // all the descriptors have been read (with GetNthDescriptor). Secondly, we
88*635a8641SAndroid Build Coastguard Worker   // can check that they are read in order.
89*635a8641SAndroid Build Coastguard Worker   unsigned consumed_descriptor_highwater_;
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
92*635a8641SAndroid Build Coastguard Worker };
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker }  // namespace IPC
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker #endif  // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
97