blob: b3cc607bdbd628cee5e4f7e47d531c6089ee1af2 [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
10#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
morrita96693852014-09-24 20:11:4512#include "base/memory/scoped_vector.h"
[email protected]7c854372011-08-15 20:41:4613#include "ipc/ipc_export.h"
[email protected]a17b7ca62009-02-12 18:09:1114
morrita4b5c28e22015-01-14 21:17:0615#if defined(OS_POSIX)
16#include "base/files/file.h"
17#endif
18
19namespace IPC {
20
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;
40
morrita4b5c28e22015-01-14 21:17:0641 // Return true if no unconsumed descriptors remain
42 bool empty() const { return 0 == size(); }
43
morrita1aa788c2015-01-31 05:45:4244 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
45
46 // Take the nth attachment from the beginning of the set, Code using this
47 // /must/ access the attachments in order, and must do it at most once.
48 //
49 // This interface is designed for the deserialising code as it doesn't
50 // support close flags.
51 // returns: an attachment, or nullptr on error
morrita98b7aaa2015-01-26 22:42:5452 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
53
morrita81b17e02015-02-06 00:58:3054 // This must be called after transmitting the descriptors returned by
55 // PeekDescriptors. It marks all the descriptors as consumed and closes those
56 // which are auto-close.
57 void CommitAll();
58
morrita4b5c28e22015-01-14 21:17:0659#if defined(OS_POSIX)
[email protected]a17b7ca62009-02-12 18:09:1160 // This is the maximum number of descriptors per message. We need to know this
61 // because the control message kernel interface has to be given a buffer which
62 // is large enough to store all the descriptor numbers. Otherwise the kernel
63 // tells us that it truncated the control data and the extra descriptors are
64 // lost.
65 //
66 // In debugging mode, it's a fatal error to try and add more than this number
morrita4b5c28e22015-01-14 21:17:0667 // of descriptors to a MessageAttachmentSet.
yusukeseaa3c5bf2015-03-17 00:34:1368 static const size_t kMaxDescriptorsPerMessage = 128;
[email protected]a17b7ca62009-02-12 18:09:1169
70 // ---------------------------------------------------------------------------
[email protected]a17b7ca62009-02-12 18:09:1171 // Interfaces for transmission...
72
73 // Fill an array with file descriptors without 'consuming' them. CommitAll
74 // must be called after these descriptors have been transmitted.
75 // buffer: (output) a buffer of, at least, size() integers.
morrita96693852014-09-24 20:11:4576 void PeekDescriptors(base::PlatformFile* buffer) const;
[email protected]aac449e2010-06-10 21:39:0477 // Returns true if any contained file descriptors appear to be handles to a
78 // directory.
79 bool ContainsDirectoryDescriptor() const;
[email protected]dc875dc2013-10-15 00:07:0080 // Fetch all filedescriptors with the "auto close" property.
81 // Used instead of CommitAll() when closing must be handled manually.
morrita96693852014-09-24 20:11:4582 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds);
[email protected]a17b7ca62009-02-12 18:09:1183
84 // ---------------------------------------------------------------------------
85
[email protected]a17b7ca62009-02-12 18:09:1186 // ---------------------------------------------------------------------------
87 // Interfaces for receiving...
88
89 // Set the contents of the set from the given buffer. This set must be empty
90 // before calling. The auto-close flag is set on all the descriptors so that
91 // unconsumed descriptors are closed on destruction.
morrita96693852014-09-24 20:11:4592 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count);
[email protected]a17b7ca62009-02-12 18:09:1193
morrita4b5c28e22015-01-14 21:17:0694#endif // OS_POSIX
95
[email protected]a17b7ca62009-02-12 18:09:1196 // ---------------------------------------------------------------------------
97
98 private:
morrita4b5c28e22015-01-14 21:17:0699 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
[email protected]877d55d2009-11-05 21:53:08100
morrita4b5c28e22015-01-14 21:17:06101 ~MessageAttachmentSet();
[email protected]877d55d2009-11-05 21:53:08102
morrita98b7aaa2015-01-26 22:42:54103 // A vector of attachments of the message, which might be |PlatformFile| or
104 // |MessagePipe|.
105 std::vector<scoped_refptr<MessageAttachment>> attachments_;
106
[email protected]a17b7ca62009-02-12 18:09:11107 // This contains the index of the next descriptor which should be consumed.
108 // It's used in a couple of ways. Firstly, at destruction we can check that
109 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
110 // can check that they are read in order.
111 mutable unsigned consumed_descriptor_highwater_;
112
morrita4b5c28e22015-01-14 21:17:06113 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
[email protected]a17b7ca62009-02-12 18:09:11114};
115
morrita4b5c28e22015-01-14 21:17:06116} // namespace IPC
117
118#endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_