OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ | 5 #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
6 #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ | 6 #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "ipc/ipc_export.h" | 15 #include "ipc/ipc_export.h" |
16 | 16 |
17 #if defined(OS_POSIX) | |
18 #include "base/files/file.h" | |
19 #endif | |
20 | |
21 namespace IPC { | 17 namespace IPC { |
22 | 18 |
23 class BrokerableAttachment; | |
24 class MessageAttachment; | 19 class MessageAttachment; |
25 | 20 |
26 // ----------------------------------------------------------------------------- | 21 // ----------------------------------------------------------------------------- |
27 // A MessageAttachmentSet is an ordered set of MessageAttachment objects | 22 // A MessageAttachmentSet is an ordered set of MessageAttachment objects |
28 // associated with an IPC message. There are three types of MessageAttachments: | 23 // associated with an IPC message. All attachments are wrapped in a mojo handle |
29 // 1) TYPE_PLATFORM_FILE is transmitted over the Channel's underlying | 24 // if necessary and sent over the mojo message pipe. |
30 // UNIX domain socket | |
31 // 2) TYPE_MOJO_HANDLE is transmitted over the Mojo MessagePipe. | |
32 // 3) TYPE_BROKERABLE_ATTACHMENT is transmitted by the Attachment Broker. | |
33 // Any given IPC Message can have attachments of type (1) or (2), but not both. | |
34 // These are stored in |attachments_|. Attachments of type (3) are stored in | |
35 // |brokerable_attachments_|. | |
36 // | 25 // |
37 // To produce a deterministic ordering, all attachments in |attachments_| are | 26 // For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In |
38 // considered to come before those in |brokerable_attachments_|. These | 27 // that case, the FD is sent over socket. |
39 // attachments are transmitted across different communication channels, and | |
40 // multiplexed by the receiver, so ordering between them cannot be guaranteed. | |
41 // | |
42 // ----------------------------------------------------------------------------- | 28 // ----------------------------------------------------------------------------- |
43 class IPC_EXPORT MessageAttachmentSet | 29 class IPC_EXPORT MessageAttachmentSet |
44 : public base::RefCountedThreadSafe<MessageAttachmentSet> { | 30 : public base::RefCountedThreadSafe<MessageAttachmentSet> { |
45 public: | 31 public: |
46 MessageAttachmentSet(); | 32 MessageAttachmentSet(); |
47 | 33 |
48 // Return the number of attachments | 34 // Return the number of attachments |
49 unsigned size() const; | 35 unsigned size() const; |
50 // Return the number of file descriptors | |
51 unsigned num_descriptors() const; | |
52 // Return the number of mojo handles in the attachment set | |
53 unsigned num_mojo_handles() const; | |
54 // Return the number of brokerable attachments in the attachment set. | |
55 unsigned num_brokerable_attachments() const; | |
56 // Return the number of non-brokerable attachments in the attachment set. | |
57 unsigned num_non_brokerable_attachments() const; | |
58 | 36 |
59 // Return true if no unconsumed descriptors remain | 37 // Return true if no unconsumed descriptors remain |
60 bool empty() const { return 0 == size(); } | 38 bool empty() const { return attachments_.empty(); } |
61 | 39 |
62 // Returns whether the attachment was successfully added. | 40 // Returns whether the attachment was successfully added. |
63 // |index| is an output variable. On success, it contains the index of the | 41 // |index| is an output variable. On success, it contains the index of the |
64 // newly added attachment. | 42 // newly added attachment. |
65 // |brokerable| is an output variable. On success, it describes which vector | |
66 // the attachment was added to. | |
67 bool AddAttachment(scoped_refptr<MessageAttachment> attachment, | 43 bool AddAttachment(scoped_refptr<MessageAttachment> attachment, |
68 size_t* index, | 44 size_t* index); |
69 bool* brokerable); | |
70 | 45 |
71 // Similar to the above method, but without output variables. | 46 // Similar to the above method, but without output variables. |
72 bool AddAttachment(scoped_refptr<MessageAttachment> attachment); | 47 bool AddAttachment(scoped_refptr<MessageAttachment> attachment); |
73 | 48 |
74 // Take the nth non-brokerable attachment from the beginning of the vector, | 49 // Take the nth from the beginning of the vector, Code using this /must/ |
75 // Code using this /must/ access the attachments in order, and must do it at | 50 // access the attachments in order, and must do it at most once. |
76 // most once. | |
77 // | 51 // |
78 // This interface is designed for the deserialising code as it doesn't | 52 // This interface is designed for the deserialising code as it doesn't |
79 // support close flags. | 53 // support close flags. |
80 // returns: an attachment, or nullptr on error | 54 // returns: an attachment, or nullptr on error |
81 scoped_refptr<MessageAttachment> GetNonBrokerableAttachmentAt(unsigned index); | 55 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index); |
82 | 56 |
83 // Similar to GetNonBrokerableAttachmentAt, but there are no ordering | 57 // Marks all the descriptors as consumed and closes those which are |
84 // requirements. | 58 // auto-close. |
85 scoped_refptr<MessageAttachment> GetBrokerableAttachmentAt(unsigned index); | |
86 | |
87 // This must be called after transmitting the descriptors returned by | |
88 // PeekDescriptors. It marks all the non-brokerable descriptors as consumed | |
89 // and closes those which are auto-close. | |
90 void CommitAllDescriptors(); | 59 void CommitAllDescriptors(); |
91 | 60 |
92 // Returns a vector of all brokerable attachments. | |
93 std::vector<scoped_refptr<IPC::BrokerableAttachment>> | |
94 GetBrokerableAttachments() const; | |
95 | |
96 #if defined(OS_POSIX) | 61 #if defined(OS_POSIX) |
97 // This is the maximum number of descriptors per message. We need to know this | 62 // This is the maximum number of descriptors per message. We need to know this |
98 // because the control message kernel interface has to be given a buffer which | 63 // because the control message kernel interface has to be given a buffer which |
99 // is large enough to store all the descriptor numbers. Otherwise the kernel | 64 // is large enough to store all the descriptor numbers. Otherwise the kernel |
100 // tells us that it truncated the control data and the extra descriptors are | 65 // tells us that it truncated the control data and the extra descriptors are |
101 // lost. | 66 // lost. |
102 // | 67 // |
103 // In debugging mode, it's a fatal error to try and add more than this number | 68 // In debugging mode, it's a fatal error to try and add more than this number |
104 // of descriptors to a MessageAttachmentSet. | 69 // of descriptors to a MessageAttachmentSet. |
105 static const size_t kMaxDescriptorsPerMessage = 7; | 70 static const size_t kMaxDescriptorsPerMessage = 7; |
106 | |
107 // --------------------------------------------------------------------------- | |
108 // Interfaces for transmission... | |
109 | |
110 // Fill an array with file descriptors without 'consuming' them. | |
111 // CommitAllDescriptors must be called after these descriptors have been | |
112 // transmitted. | |
113 // buffer: (output) a buffer of, at least, size() integers. | |
114 void PeekDescriptors(base::PlatformFile* buffer) const; | |
115 // Returns true if any contained file descriptors appear to be handles to a | |
116 // directory. | |
117 bool ContainsDirectoryDescriptor() const; | |
118 // Fetch all filedescriptors with the "auto close" property. Used instead of | |
119 // CommitAllDescriptors() when closing must be handled manually. | |
120 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds); | |
121 | |
122 // --------------------------------------------------------------------------- | |
123 | |
124 // --------------------------------------------------------------------------- | |
125 // Interfaces for receiving... | |
126 | |
127 // Set the contents of the set from the given buffer. This set must be empty | |
128 // before calling. The auto-close flag is set on all the descriptors so that | |
129 // unconsumed descriptors are closed on destruction. | |
130 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count); | |
131 | |
132 #endif // OS_POSIX | 71 #endif // OS_POSIX |
133 | 72 |
134 // --------------------------------------------------------------------------- | 73 // --------------------------------------------------------------------------- |
135 | 74 |
136 private: | 75 private: |
137 friend class base::RefCountedThreadSafe<MessageAttachmentSet>; | 76 friend class base::RefCountedThreadSafe<MessageAttachmentSet>; |
138 | 77 |
139 ~MessageAttachmentSet(); | 78 ~MessageAttachmentSet(); |
140 | 79 |
141 // All elements either have type TYPE_PLATFORM_FILE or TYPE_MOJO_HANDLE. | 80 // Return the number of file descriptors |
| 81 unsigned num_descriptors() const; |
| 82 |
142 std::vector<scoped_refptr<MessageAttachment>> attachments_; | 83 std::vector<scoped_refptr<MessageAttachment>> attachments_; |
143 | 84 |
144 // All elements have type TYPE_BROKERABLE_ATTACHMENT. | |
145 std::vector<scoped_refptr<BrokerableAttachment>> brokerable_attachments_; | |
146 | |
147 // This contains the index of the next descriptor which should be consumed. | 85 // This contains the index of the next descriptor which should be consumed. |
148 // It's used in a couple of ways. Firstly, at destruction we can check that | 86 // It's used in a couple of ways. Firstly, at destruction we can check that |
149 // all the descriptors have been read (with GetNthDescriptor). Secondly, we | 87 // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
150 // can check that they are read in order. | 88 // can check that they are read in order. |
151 mutable unsigned consumed_descriptor_highwater_; | 89 unsigned consumed_descriptor_highwater_; |
152 | 90 |
153 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); | 91 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); |
154 }; | 92 }; |
155 | 93 |
156 } // namespace IPC | 94 } // namespace IPC |
157 | 95 |
158 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ | 96 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
OLD | NEW |