blob: 764c818c9f07b9014240a25bf2f306d58bc16c57 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a17b7ca62009-02-12 18:09:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
morrita4b5c28e22015-01-14 21:17:065#ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
6#define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
[email protected]a17b7ca62009-02-12 18:09:117
avi246998d82015-12-22 02:39:048#include <stddef.h>
9
[email protected]a17b7ca62009-02-12 18:09:1110#include <vector>
11
tfarina4da82752015-09-16 09:56:2112#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
avi246998d82015-12-22 02:39:0414#include "build/build_config.h"
[email protected]7c854372011-08-15 20:41:4615#include "ipc/ipc_export.h"
[email protected]a17b7ca62009-02-12 18:09:1116
morrita4b5c28e22015-01-14 21:17:0617#if defined(OS_POSIX)
18#include "base/files/file.h"
19#endif
20
21namespace IPC {
22
erikcheneece6c32015-07-07 22:13:1123class BrokerableAttachment;
morrita98b7aaa2015-01-26 22:42:5424class MessageAttachment;
25
[email protected]a17b7ca62009-02-12 18:09:1126// -----------------------------------------------------------------------------
erikchenae6d3212015-10-10 02:43:4927// A MessageAttachmentSet is an ordered set of MessageAttachment objects
28// associated with an IPC message. There are three types of MessageAttachments:
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_|.
36//
37// To produce a deterministic ordering, all attachments in |attachments_| are
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//
[email protected]a17b7ca62009-02-12 18:09:1142// -----------------------------------------------------------------------------
morrita4b5c28e22015-01-14 21:17:0643class IPC_EXPORT MessageAttachmentSet
44 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
[email protected]a17b7ca62009-02-12 18:09:1145 public:
morrita4b5c28e22015-01-14 21:17:0646 MessageAttachmentSet();
[email protected]a17b7ca62009-02-12 18:09:1147
morrita98b7aaa2015-01-26 22:42:5448 // Return the number of attachments
morrita4b5c28e22015-01-14 21:17:0649 unsigned size() const;
morrita98b7aaa2015-01-26 22:42:5450 // Return the number of file descriptors
51 unsigned num_descriptors() const;
morrita81b17e02015-02-06 00:58:3052 // Return the number of mojo handles in the attachment set
53 unsigned num_mojo_handles() const;
erikcheneece6c32015-07-07 22:13:1154 // Return the number of brokerable attachments in the attachment set.
55 unsigned num_brokerable_attachments() const;
erikchenae6d3212015-10-10 02:43:4956 // Return the number of non-brokerable attachments in the attachment set.
57 unsigned num_non_brokerable_attachments() const;
morrita81b17e02015-02-06 00:58:3058
morrita4b5c28e22015-01-14 21:17:0659 // Return true if no unconsumed descriptors remain
60 bool empty() const { return 0 == size(); }
61
erikchenae6d3212015-10-10 02:43:4962 // Returns whether the attachment was successfully added.
63 // |index| is an output variable. On success, it contains the index of the
64 // 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,
68 size_t* index,
69 bool* brokerable);
70
71 // Similar to the above method, but without output variables.
morrita1aa788c2015-01-31 05:45:4272 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
73
erikchenae6d3212015-10-10 02:43:4974 // Take the nth non-brokerable attachment from the beginning of the vector,
75 // Code using this /must/ access the attachments in order, and must do it at
76 // most once.
morrita1aa788c2015-01-31 05:45:4277 //
78 // This interface is designed for the deserialising code as it doesn't
79 // support close flags.
80 // returns: an attachment, or nullptr on error
erikchenae6d3212015-10-10 02:43:4981 scoped_refptr<MessageAttachment> GetNonBrokerableAttachmentAt(unsigned index);
82
83 // Similar to GetNonBrokerableAttachmentAt, but there are no ordering
84 // requirements.
85 scoped_refptr<MessageAttachment> GetBrokerableAttachmentAt(unsigned index);
morrita98b7aaa2015-01-26 22:42:5486
morrita81b17e02015-02-06 00:58:3087 // This must be called after transmitting the descriptors returned by
erikchenae6d3212015-10-10 02:43:4988 // PeekDescriptors. It marks all the non-brokerable descriptors as consumed
89 // and closes those which are auto-close.
90 void CommitAllDescriptors();
morrita81b17e02015-02-06 00:58:3091
erikcheneece6c32015-07-07 22:13:1192 // Returns a vector of all brokerable attachments.
erikchena03dde6f2015-10-29 22:37:0493 std::vector<scoped_refptr<IPC::BrokerableAttachment>>
94 GetBrokerableAttachments() const;
erikchen87351da2015-09-15 19:11:0995
96 // Replaces a placeholder brokerable attachment with |attachment|, matching
97 // them by their id.
98 void ReplacePlaceholderWithAttachment(
99 const scoped_refptr<BrokerableAttachment>& attachment);
erikcheneece6c32015-07-07 22:13:11100
morrita4b5c28e22015-01-14 21:17:06101#if defined(OS_POSIX)
[email protected]a17b7ca62009-02-12 18:09:11102 // This is the maximum number of descriptors per message. We need to know this
103 // because the control message kernel interface has to be given a buffer which
104 // is large enough to store all the descriptor numbers. Otherwise the kernel
105 // tells us that it truncated the control data and the extra descriptors are
106 // lost.
107 //
108 // In debugging mode, it's a fatal error to try and add more than this number
morrita4b5c28e22015-01-14 21:17:06109 // of descriptors to a MessageAttachmentSet.
yusukesc986c5432015-05-06 19:45:45110 static const size_t kMaxDescriptorsPerMessage = 7;
[email protected]a17b7ca62009-02-12 18:09:11111
112 // ---------------------------------------------------------------------------
[email protected]a17b7ca62009-02-12 18:09:11113 // Interfaces for transmission...
114
erikchenae6d3212015-10-10 02:43:49115 // Fill an array with file descriptors without 'consuming' them.
116 // CommitAllDescriptors must be called after these descriptors have been
117 // transmitted.
[email protected]a17b7ca62009-02-12 18:09:11118 // buffer: (output) a buffer of, at least, size() integers.
morrita96693852014-09-24 20:11:45119 void PeekDescriptors(base::PlatformFile* buffer) const;
[email protected]aac449e2010-06-10 21:39:04120 // Returns true if any contained file descriptors appear to be handles to a
121 // directory.
122 bool ContainsDirectoryDescriptor() const;
erikchenae6d3212015-10-10 02:43:49123 // Fetch all filedescriptors with the "auto close" property. Used instead of
124 // CommitAllDescriptors() when closing must be handled manually.
morrita96693852014-09-24 20:11:45125 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds);
[email protected]a17b7ca62009-02-12 18:09:11126
127 // ---------------------------------------------------------------------------
128
[email protected]a17b7ca62009-02-12 18:09:11129 // ---------------------------------------------------------------------------
130 // Interfaces for receiving...
131
132 // Set the contents of the set from the given buffer. This set must be empty
133 // before calling. The auto-close flag is set on all the descriptors so that
134 // unconsumed descriptors are closed on destruction.
morrita96693852014-09-24 20:11:45135 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count);
[email protected]a17b7ca62009-02-12 18:09:11136
morrita4b5c28e22015-01-14 21:17:06137#endif // OS_POSIX
138
[email protected]a17b7ca62009-02-12 18:09:11139 // ---------------------------------------------------------------------------
140
141 private:
morrita4b5c28e22015-01-14 21:17:06142 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
[email protected]877d55d2009-11-05 21:53:08143
morrita4b5c28e22015-01-14 21:17:06144 ~MessageAttachmentSet();
[email protected]877d55d2009-11-05 21:53:08145
erikchenae6d3212015-10-10 02:43:49146 // All elements either have type TYPE_PLATFORM_FILE or TYPE_MOJO_HANDLE.
morrita98b7aaa2015-01-26 22:42:54147 std::vector<scoped_refptr<MessageAttachment>> attachments_;
148
erikchenae6d3212015-10-10 02:43:49149 // All elements have type TYPE_BROKERABLE_ATTACHMENT.
150 std::vector<scoped_refptr<BrokerableAttachment>> brokerable_attachments_;
151
[email protected]a17b7ca62009-02-12 18:09:11152 // This contains the index of the next descriptor which should be consumed.
153 // It's used in a couple of ways. Firstly, at destruction we can check that
154 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
155 // can check that they are read in order.
156 mutable unsigned consumed_descriptor_highwater_;
157
morrita4b5c28e22015-01-14 21:17:06158 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
[email protected]a17b7ca62009-02-12 18:09:11159};
160
morrita4b5c28e22015-01-14 21:17:06161} // namespace IPC
162
163#endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_