blob: 4276fcd9cdc14d899a1863c5467dc0c5895c4479 [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
8#include <vector>
9
tfarina4da82752015-09-16 09:56:2110#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
[email protected]7c854372011-08-15 20:41:4612#include "ipc/ipc_export.h"
[email protected]a17b7ca62009-02-12 18:09:1113
morrita4b5c28e22015-01-14 21:17:0614#if defined(OS_POSIX)
15#include "base/files/file.h"
16#endif
17
18namespace IPC {
19
erikcheneece6c32015-07-07 22:13:1120class BrokerableAttachment;
morrita98b7aaa2015-01-26 22:42:5421class MessageAttachment;
22
[email protected]a17b7ca62009-02-12 18:09:1123// -----------------------------------------------------------------------------
erikchenae6d3212015-10-10 02:43:4924// A MessageAttachmentSet is an ordered set of MessageAttachment objects
25// associated with an IPC message. There are three types of MessageAttachments:
26// 1) TYPE_PLATFORM_FILE is transmitted over the Channel's underlying
27// UNIX domain socket
28// 2) TYPE_MOJO_HANDLE is transmitted over the Mojo MessagePipe.
29// 3) TYPE_BROKERABLE_ATTACHMENT is transmitted by the Attachment Broker.
30// Any given IPC Message can have attachments of type (1) or (2), but not both.
31// These are stored in |attachments_|. Attachments of type (3) are stored in
32// |brokerable_attachments_|.
33//
34// To produce a deterministic ordering, all attachments in |attachments_| are
35// considered to come before those in |brokerable_attachments_|. These
36// attachments are transmitted across different communication channels, and
37// multiplexed by the receiver, so ordering between them cannot be guaranteed.
38//
[email protected]a17b7ca62009-02-12 18:09:1139// -----------------------------------------------------------------------------
morrita4b5c28e22015-01-14 21:17:0640class IPC_EXPORT MessageAttachmentSet
41 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
[email protected]a17b7ca62009-02-12 18:09:1142 public:
morrita4b5c28e22015-01-14 21:17:0643 MessageAttachmentSet();
[email protected]a17b7ca62009-02-12 18:09:1144
morrita98b7aaa2015-01-26 22:42:5445 // Return the number of attachments
morrita4b5c28e22015-01-14 21:17:0646 unsigned size() const;
morrita98b7aaa2015-01-26 22:42:5447 // Return the number of file descriptors
48 unsigned num_descriptors() const;
morrita81b17e02015-02-06 00:58:3049 // Return the number of mojo handles in the attachment set
50 unsigned num_mojo_handles() const;
erikcheneece6c32015-07-07 22:13:1151 // Return the number of brokerable attachments in the attachment set.
52 unsigned num_brokerable_attachments() const;
erikchenae6d3212015-10-10 02:43:4953 // Return the number of non-brokerable attachments in the attachment set.
54 unsigned num_non_brokerable_attachments() const;
morrita81b17e02015-02-06 00:58:3055
morrita4b5c28e22015-01-14 21:17:0656 // Return true if no unconsumed descriptors remain
57 bool empty() const { return 0 == size(); }
58
erikchenae6d3212015-10-10 02:43:4959 // Returns whether the attachment was successfully added.
60 // |index| is an output variable. On success, it contains the index of the
61 // newly added attachment.
62 // |brokerable| is an output variable. On success, it describes which vector
63 // the attachment was added to.
64 bool AddAttachment(scoped_refptr<MessageAttachment> attachment,
65 size_t* index,
66 bool* brokerable);
67
68 // Similar to the above method, but without output variables.
morrita1aa788c2015-01-31 05:45:4269 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
70
erikchenae6d3212015-10-10 02:43:4971 // Take the nth non-brokerable attachment from the beginning of the vector,
72 // Code using this /must/ access the attachments in order, and must do it at
73 // most once.
morrita1aa788c2015-01-31 05:45:4274 //
75 // This interface is designed for the deserialising code as it doesn't
76 // support close flags.
77 // returns: an attachment, or nullptr on error
erikchenae6d3212015-10-10 02:43:4978 scoped_refptr<MessageAttachment> GetNonBrokerableAttachmentAt(unsigned index);
79
80 // Similar to GetNonBrokerableAttachmentAt, but there are no ordering
81 // requirements.
82 scoped_refptr<MessageAttachment> GetBrokerableAttachmentAt(unsigned index);
morrita98b7aaa2015-01-26 22:42:5483
morrita81b17e02015-02-06 00:58:3084 // This must be called after transmitting the descriptors returned by
erikchenae6d3212015-10-10 02:43:4985 // PeekDescriptors. It marks all the non-brokerable descriptors as consumed
86 // and closes those which are auto-close.
87 void CommitAllDescriptors();
morrita81b17e02015-02-06 00:58:3088
erikcheneece6c32015-07-07 22:13:1189 // Returns a vector of all brokerable attachments.
erikchen3722a322015-10-07 20:51:5590 std::vector<BrokerableAttachment*> GetBrokerableAttachments() const;
erikchen87351da2015-09-15 19:11:0991
92 // Replaces a placeholder brokerable attachment with |attachment|, matching
93 // them by their id.
94 void ReplacePlaceholderWithAttachment(
95 const scoped_refptr<BrokerableAttachment>& attachment);
erikcheneece6c32015-07-07 22:13:1196
morrita4b5c28e22015-01-14 21:17:0697#if defined(OS_POSIX)
[email protected]a17b7ca62009-02-12 18:09:1198 // This is the maximum number of descriptors per message. We need to know this
99 // because the control message kernel interface has to be given a buffer which
100 // is large enough to store all the descriptor numbers. Otherwise the kernel
101 // tells us that it truncated the control data and the extra descriptors are
102 // lost.
103 //
104 // In debugging mode, it's a fatal error to try and add more than this number
morrita4b5c28e22015-01-14 21:17:06105 // of descriptors to a MessageAttachmentSet.
yusukesc986c5432015-05-06 19:45:45106 static const size_t kMaxDescriptorsPerMessage = 7;
[email protected]a17b7ca62009-02-12 18:09:11107
108 // ---------------------------------------------------------------------------
[email protected]a17b7ca62009-02-12 18:09:11109 // Interfaces for transmission...
110
erikchenae6d3212015-10-10 02:43:49111 // Fill an array with file descriptors without 'consuming' them.
112 // CommitAllDescriptors must be called after these descriptors have been
113 // transmitted.
[email protected]a17b7ca62009-02-12 18:09:11114 // buffer: (output) a buffer of, at least, size() integers.
morrita96693852014-09-24 20:11:45115 void PeekDescriptors(base::PlatformFile* buffer) const;
[email protected]aac449e2010-06-10 21:39:04116 // Returns true if any contained file descriptors appear to be handles to a
117 // directory.
118 bool ContainsDirectoryDescriptor() const;
erikchenae6d3212015-10-10 02:43:49119 // Fetch all filedescriptors with the "auto close" property. Used instead of
120 // CommitAllDescriptors() when closing must be handled manually.
morrita96693852014-09-24 20:11:45121 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds);
[email protected]a17b7ca62009-02-12 18:09:11122
123 // ---------------------------------------------------------------------------
124
[email protected]a17b7ca62009-02-12 18:09:11125 // ---------------------------------------------------------------------------
126 // Interfaces for receiving...
127
128 // Set the contents of the set from the given buffer. This set must be empty
129 // before calling. The auto-close flag is set on all the descriptors so that
130 // unconsumed descriptors are closed on destruction.
morrita96693852014-09-24 20:11:45131 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count);
[email protected]a17b7ca62009-02-12 18:09:11132
morrita4b5c28e22015-01-14 21:17:06133#endif // OS_POSIX
134
[email protected]a17b7ca62009-02-12 18:09:11135 // ---------------------------------------------------------------------------
136
137 private:
morrita4b5c28e22015-01-14 21:17:06138 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
[email protected]877d55d2009-11-05 21:53:08139
morrita4b5c28e22015-01-14 21:17:06140 ~MessageAttachmentSet();
[email protected]877d55d2009-11-05 21:53:08141
erikchenae6d3212015-10-10 02:43:49142 // All elements either have type TYPE_PLATFORM_FILE or TYPE_MOJO_HANDLE.
morrita98b7aaa2015-01-26 22:42:54143 std::vector<scoped_refptr<MessageAttachment>> attachments_;
144
erikchenae6d3212015-10-10 02:43:49145 // All elements have type TYPE_BROKERABLE_ATTACHMENT.
146 std::vector<scoped_refptr<BrokerableAttachment>> brokerable_attachments_;
147
[email protected]a17b7ca62009-02-12 18:09:11148 // This contains the index of the next descriptor which should be consumed.
149 // It's used in a couple of ways. Firstly, at destruction we can check that
150 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
151 // can check that they are read in order.
152 mutable unsigned consumed_descriptor_highwater_;
153
morrita4b5c28e22015-01-14 21:17:06154 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
[email protected]a17b7ca62009-02-12 18:09:11155};
156
morrita4b5c28e22015-01-14 21:17:06157} // namespace IPC
158
159#endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_