Revert of Add logging to profile downloader to diagnose Android FR issue. (patchset...
[chromium-blink-merge.git] / ipc / ipc_message_attachment_set.h
blob5e4b195ea3f83197e55a8e2863de59ca457ed24b
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
3 // found in the LICENSE file.
5 #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
6 #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "ipc/ipc_export.h"
14 #if defined(OS_POSIX)
15 #include "base/files/file.h"
16 #endif
18 namespace IPC {
20 class BrokerableAttachment;
21 class MessageAttachment;
23 // -----------------------------------------------------------------------------
24 // 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).
28 // -----------------------------------------------------------------------------
29 class IPC_EXPORT MessageAttachmentSet
30 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
31 public:
32 MessageAttachmentSet();
34 // Return the number of attachments
35 unsigned size() const;
36 // Return the number of file descriptors
37 unsigned num_descriptors() const;
38 // Return the number of mojo handles in the attachment set
39 unsigned num_mojo_handles() const;
40 // Return the number of brokerable attachments in the attachment set.
41 unsigned num_brokerable_attachments() const;
43 // Return true if no unconsumed descriptors remain
44 bool empty() const { return 0 == size(); }
46 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
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.
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
54 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
56 // 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();
61 // Returns a vector of all brokerable attachments.
62 std::vector<const BrokerableAttachment*> PeekBrokerableAttachments() const;
63 std::vector<scoped_refptr<BrokerableAttachment>>
64 GetBrokerableAttachmentsForUpdating();
66 #if defined(OS_POSIX)
67 // This is the maximum number of descriptors per message. We need to know this
68 // because the control message kernel interface has to be given a buffer which
69 // is large enough to store all the descriptor numbers. Otherwise the kernel
70 // tells us that it truncated the control data and the extra descriptors are
71 // lost.
73 // In debugging mode, it's a fatal error to try and add more than this number
74 // of descriptors to a MessageAttachmentSet.
75 static const size_t kMaxDescriptorsPerMessage = 7;
77 // ---------------------------------------------------------------------------
78 // Interfaces for transmission...
80 // Fill an array with file descriptors without 'consuming' them. CommitAll
81 // must be called after these descriptors have been transmitted.
82 // buffer: (output) a buffer of, at least, size() integers.
83 void PeekDescriptors(base::PlatformFile* buffer) const;
84 // Returns true if any contained file descriptors appear to be handles to a
85 // directory.
86 bool ContainsDirectoryDescriptor() const;
87 // Fetch all filedescriptors with the "auto close" property.
88 // Used instead of CommitAll() when closing must be handled manually.
89 void ReleaseFDsToClose(std::vector<base::PlatformFile>* fds);
91 // ---------------------------------------------------------------------------
93 // ---------------------------------------------------------------------------
94 // Interfaces for receiving...
96 // Set the contents of the set from the given buffer. This set must be empty
97 // before calling. The auto-close flag is set on all the descriptors so that
98 // unconsumed descriptors are closed on destruction.
99 void AddDescriptorsToOwn(const base::PlatformFile* buffer, unsigned count);
101 #endif // OS_POSIX
103 // ---------------------------------------------------------------------------
105 private:
106 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
108 ~MessageAttachmentSet();
110 // A vector of attachments of the message, which might be |PlatformFile| or
111 // |MessagePipe|.
112 std::vector<scoped_refptr<MessageAttachment>> attachments_;
114 // This contains the index of the next descriptor which should be consumed.
115 // It's used in a couple of ways. Firstly, at destruction we can check that
116 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
117 // can check that they are read in order.
118 mutable unsigned consumed_descriptor_highwater_;
120 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
123 } // namespace IPC
125 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_