blob: 0ed62accab23de502b35837f730c5a082ed4c75f [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// -----------------------------------------------------------------------------
morrita1aa788c2015-01-31 05:45:4224// A MessageAttachmentSet is an ordered set of MessageAttachment objects. These
25// are associated with IPC messages so that attachments, each of which is either
26// a platform file or a mojo handle, can be transmitted over the underlying UNIX
27// domain socket (for ChannelPosix) or Mojo MessagePipe (for ChannelMojo).
[email protected]a17b7ca62009-02-12 18:09:1128// -----------------------------------------------------------------------------
morrita4b5c28e22015-01-14 21:17:0629class IPC_EXPORT MessageAttachmentSet
30 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
[email protected]a17b7ca62009-02-12 18:09:1131 public:
morrita4b5c28e22015-01-14 21:17:0632 MessageAttachmentSet();
[email protected]a17b7ca62009-02-12 18:09:1133
morrita98b7aaa2015-01-26 22:42:5434 // Return the number of attachments
morrita4b5c28e22015-01-14 21:17:0635 unsigned size() const;
morrita98b7aaa2015-01-26 22:42:5436 // Return the number of file descriptors
37 unsigned num_descriptors() const;
morrita81b17e02015-02-06 00:58:3038 // Return the number of mojo handles in the attachment set
39 unsigned num_mojo_handles() const;
erikcheneece6c32015-07-07 22:13:1140 // Return the number of brokerable attachments in the attachment set.
41 unsigned num_brokerable_attachments() const;
morrita81b17e02015-02-06 00:58:3042
morrita4b5c28e22015-01-14 21:17:0643 // Return true if no unconsumed descriptors remain
44 bool empty() const { return 0 == size(); }
45
morrita1aa788c2015-01-31 05:45:4246 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
47
48 // Take the nth attachment from the beginning of the set, Code using this
49 // /must/ access the attachments in order, and must do it at most once.
50 //
51 // This interface is designed for the deserialising code as it doesn't
52 // support close flags.
53 // returns: an attachment, or nullptr on error
morrita98b7aaa2015-01-26 22:42:5454 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
55
morrita81b17e02015-02-06 00:58:3056 // This must be called after transmitting the descriptors returned by
57 // PeekDescriptors. It marks all the descriptors as consumed and closes those
58 // which are auto-close.
59 void CommitAll();
60
erikcheneece6c32015-07-07 22:13:1161 // Returns a vector of all brokerable attachments.
erikchen3722a322015-10-07 20:51:5562 std::vector<BrokerableAttachment*> GetBrokerableAttachments() const;
erikchen87351da2015-09-15 19:11:0963
64 // Replaces a placeholder brokerable attachment with |attachment|, matching
65 // them by their id.
66 void ReplacePlaceholderWithAttachment(
67 const scoped_refptr<BrokerableAttachment>& attachment);
erikcheneece6c32015-07-07 22:13:1168
morrita4b5c28e22015-01-14 21:17:0669#if defined(OS_POSIX)
[email protected]a17b7ca62009-02-12 18:09:1170 // This is the maximum number of descriptors per message. We need to know this
71 // because the control message kernel interface has to be given a buffer which
72 // is large enough to store all the descriptor numbers. Otherwise the kernel
73 // tells us that it truncated the control data and the extra descriptors are
74 // lost.
75 //
76 // In debugging mode, it's a fatal error to try and add more than this number
morrita4b5c28e22015-01-14 21:17:0677 // of descriptors to a MessageAttachmentSet.
yusukesc986c5432015-05-06 19:45:4578 static const size_t kMaxDescriptorsPerMessage = 7;
[email protected]a17b7ca62009-02-12 18:09:1179
80 // ---------------------------------------------------------------------------
[email protected]a17b7ca62009-02-12 18:09:1181 // Interfaces for transmission...
82
83 // Fill an array with file descriptors without 'consuming' them. CommitAll
84 // must be called after these descriptors have been transmitted.
85 // buffer: (output) a buffer of, at least, size() integers.
morrita96693852014-09-24 20:11:4586 void PeekDescriptors(base::PlatformFile* buffer) const;
[email protected]aac449e2010-06-10 21:39:0487 // Returns true if any contained file descriptors appear to be handles to a
88 // directory.
89 bool ContainsDirectoryDescriptor() const;
[email protected]dc875dc2013-10-15 00:07:0090 // Fetch all filedescriptors with the "auto close" property.
91 // Used instead of CommitAll() when closing must be handled manually.
morrita96693852014-09-24 20:11:4592 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds);
[email protected]a17b7ca62009-02-12 18:09:1193
94 // ---------------------------------------------------------------------------
95
[email protected]a17b7ca62009-02-12 18:09:1196 // ---------------------------------------------------------------------------
97 // Interfaces for receiving...
98
99 // Set the contents of the set from the given buffer. This set must be empty
100 // before calling. The auto-close flag is set on all the descriptors so that
101 // unconsumed descriptors are closed on destruction.
morrita96693852014-09-24 20:11:45102 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count);
[email protected]a17b7ca62009-02-12 18:09:11103
morrita4b5c28e22015-01-14 21:17:06104#endif // OS_POSIX
105
[email protected]a17b7ca62009-02-12 18:09:11106 // ---------------------------------------------------------------------------
107
108 private:
morrita4b5c28e22015-01-14 21:17:06109 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
[email protected]877d55d2009-11-05 21:53:08110
morrita4b5c28e22015-01-14 21:17:06111 ~MessageAttachmentSet();
[email protected]877d55d2009-11-05 21:53:08112
morrita98b7aaa2015-01-26 22:42:54113 // A vector of attachments of the message, which might be |PlatformFile| or
114 // |MessagePipe|.
115 std::vector<scoped_refptr<MessageAttachment>> attachments_;
116
[email protected]a17b7ca62009-02-12 18:09:11117 // This contains the index of the next descriptor which should be consumed.
118 // It's used in a couple of ways. Firstly, at destruction we can check that
119 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
120 // can check that they are read in order.
121 mutable unsigned consumed_descriptor_highwater_;
122
morrita4b5c28e22015-01-14 21:17:06123 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
[email protected]a17b7ca62009-02-12 18:09:11124};
125
morrita4b5c28e22015-01-14 21:17:06126} // namespace IPC
127
128#endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_