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 |
17 namespace IPC { | 21 namespace IPC { |
18 | 22 |
| 23 class BrokerableAttachment; |
19 class MessageAttachment; | 24 class MessageAttachment; |
20 | 25 |
21 // ----------------------------------------------------------------------------- | 26 // ----------------------------------------------------------------------------- |
22 // A MessageAttachmentSet is an ordered set of MessageAttachment objects | 27 // A MessageAttachmentSet is an ordered set of MessageAttachment objects |
23 // associated with an IPC message. All attachments are wrapped in a mojo handle | 28 // associated with an IPC message. There are three types of MessageAttachments: |
24 // if necessary and sent over the mojo message pipe. | 29 // 1) TYPE_PLATFORM_FILE is transmitted over the Channel's underlying |
| 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_|. |
25 // | 36 // |
26 // For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In | 37 // To produce a deterministic ordering, all attachments in |attachments_| are |
27 // that case, the FD is sent over socket. | 38 // considered to come before those in |brokerable_attachments_|. These |
| 39 // attachments are transmitted across different communication channels, and |
| 40 // multiplexed by the receiver, so ordering between them cannot be guaranteed. |
| 41 // |
28 // ----------------------------------------------------------------------------- | 42 // ----------------------------------------------------------------------------- |
29 class IPC_EXPORT MessageAttachmentSet | 43 class IPC_EXPORT MessageAttachmentSet |
30 : public base::RefCountedThreadSafe<MessageAttachmentSet> { | 44 : public base::RefCountedThreadSafe<MessageAttachmentSet> { |
31 public: | 45 public: |
32 MessageAttachmentSet(); | 46 MessageAttachmentSet(); |
33 | 47 |
34 // Return the number of attachments | 48 // Return the number of attachments |
35 unsigned size() const; | 49 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; |
36 | 58 |
37 // Return true if no unconsumed descriptors remain | 59 // Return true if no unconsumed descriptors remain |
38 bool empty() const { return attachments_.empty(); } | 60 bool empty() const { return 0 == size(); } |
39 | 61 |
40 // Returns whether the attachment was successfully added. | 62 // Returns whether the attachment was successfully added. |
41 // |index| is an output variable. On success, it contains the index of the | 63 // |index| is an output variable. On success, it contains the index of the |
42 // newly added attachment. | 64 // newly added attachment. |
| 65 // |brokerable| is an output variable. On success, it describes which vector |
| 66 // the attachment was added to. |
43 bool AddAttachment(scoped_refptr<MessageAttachment> attachment, | 67 bool AddAttachment(scoped_refptr<MessageAttachment> attachment, |
44 size_t* index); | 68 size_t* index, |
| 69 bool* brokerable); |
45 | 70 |
46 // Similar to the above method, but without output variables. | 71 // Similar to the above method, but without output variables. |
47 bool AddAttachment(scoped_refptr<MessageAttachment> attachment); | 72 bool AddAttachment(scoped_refptr<MessageAttachment> attachment); |
48 | 73 |
49 // Take the nth from the beginning of the vector, Code using this /must/ | 74 // Take the nth non-brokerable attachment from the beginning of the vector, |
50 // access the attachments in order, and must do it at most once. | 75 // Code using this /must/ access the attachments in order, and must do it at |
| 76 // most once. |
51 // | 77 // |
52 // This interface is designed for the deserialising code as it doesn't | 78 // This interface is designed for the deserialising code as it doesn't |
53 // support close flags. | 79 // support close flags. |
54 // returns: an attachment, or nullptr on error | 80 // returns: an attachment, or nullptr on error |
55 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index); | 81 scoped_refptr<MessageAttachment> GetNonBrokerableAttachmentAt(unsigned index); |
56 | 82 |
57 // Marks all the descriptors as consumed and closes those which are | 83 // Similar to GetNonBrokerableAttachmentAt, but there are no ordering |
58 // auto-close. | 84 // requirements. |
| 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. |
59 void CommitAllDescriptors(); | 90 void CommitAllDescriptors(); |
60 | 91 |
| 92 // Returns a vector of all brokerable attachments. |
| 93 std::vector<scoped_refptr<IPC::BrokerableAttachment>> |
| 94 GetBrokerableAttachments() const; |
| 95 |
61 #if defined(OS_POSIX) | 96 #if defined(OS_POSIX) |
62 // This is the maximum number of descriptors per message. We need to know this | 97 // 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 | 98 // 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 | 99 // 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 | 100 // tells us that it truncated the control data and the extra descriptors are |
66 // lost. | 101 // lost. |
67 // | 102 // |
68 // In debugging mode, it's a fatal error to try and add more than this number | 103 // In debugging mode, it's a fatal error to try and add more than this number |
69 // of descriptors to a MessageAttachmentSet. | 104 // of descriptors to a MessageAttachmentSet. |
70 static const size_t kMaxDescriptorsPerMessage = 7; | 105 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 |
71 #endif // OS_POSIX | 132 #endif // OS_POSIX |
72 | 133 |
73 // --------------------------------------------------------------------------- | 134 // --------------------------------------------------------------------------- |
74 | 135 |
75 private: | 136 private: |
76 friend class base::RefCountedThreadSafe<MessageAttachmentSet>; | 137 friend class base::RefCountedThreadSafe<MessageAttachmentSet>; |
77 | 138 |
78 ~MessageAttachmentSet(); | 139 ~MessageAttachmentSet(); |
79 | 140 |
80 // Return the number of file descriptors | 141 // All elements either have type TYPE_PLATFORM_FILE or TYPE_MOJO_HANDLE. |
81 unsigned num_descriptors() const; | 142 std::vector<scoped_refptr<MessageAttachment>> attachments_; |
82 | 143 |
83 std::vector<scoped_refptr<MessageAttachment>> attachments_; | 144 // All elements have type TYPE_BROKERABLE_ATTACHMENT. |
| 145 std::vector<scoped_refptr<BrokerableAttachment>> brokerable_attachments_; |
84 | 146 |
85 // This contains the index of the next descriptor which should be consumed. | 147 // 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 | 148 // 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 | 149 // all the descriptors have been read (with GetNthDescriptor). Secondly, we |
88 // can check that they are read in order. | 150 // can check that they are read in order. |
89 unsigned consumed_descriptor_highwater_; | 151 mutable unsigned consumed_descriptor_highwater_; |
90 | 152 |
91 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); | 153 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); |
92 }; | 154 }; |
93 | 155 |
94 } // namespace IPC | 156 } // namespace IPC |
95 | 157 |
96 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ | 158 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ |
OLD | NEW |