Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: ipc/ipc_message_attachment_set.h

Issue 883093003: IPC::Message Refactoring: Move POSIX specific bits to PlatformFileAttachment (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ 5 #ifndef IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
6 #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ 6 #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
13 #include "ipc/ipc_export.h" 13 #include "ipc/ipc_export.h"
14 14
15 #if defined(OS_POSIX) 15 #if defined(OS_POSIX)
16 #include "base/files/file.h" 16 #include "base/files/file.h"
17 #endif 17 #endif
18 18
19 namespace IPC { 19 namespace IPC {
20 20
21 class MessageAttachment; 21 class MessageAttachment;
22 22
23 // ----------------------------------------------------------------------------- 23 // -----------------------------------------------------------------------------
24 // A MessageAttachmentSet is an ordered set of POSIX file descriptors. These are 24 // A MessageAttachmentSet is an ordered set of MessageAttachment objects. These
25 // associated with IPC messages so that descriptors can be transmitted over a 25 // are associated with IPC messages so that attachments, each of which is either
26 // UNIX domain socket. 26 // a platform file or a mojo handle, can be transmitted over underlying UNIX
agl 2015/01/30 19:34:02 "the" before "underlying"
Hajime Morrita 2015/01/30 21:01:29 Done.
27 // domain socket (for ChannelPosix) or Mojo MessagePipe (for ChannelMojo).
27 // ----------------------------------------------------------------------------- 28 // -----------------------------------------------------------------------------
28 class IPC_EXPORT MessageAttachmentSet 29 class IPC_EXPORT MessageAttachmentSet
29 : public base::RefCountedThreadSafe<MessageAttachmentSet> { 30 : public base::RefCountedThreadSafe<MessageAttachmentSet> {
30 public: 31 public:
31 MessageAttachmentSet(); 32 MessageAttachmentSet();
32 33
33 // Return the number of attachments 34 // Return the number of attachments
34 unsigned size() const; 35 unsigned size() const;
35 // Return the number of file descriptors 36 // Return the number of file descriptors
36 unsigned num_descriptors() const; 37 unsigned num_descriptors() const;
37 // Return true if no unconsumed descriptors remain 38 // Return true if no unconsumed descriptors remain
38 bool empty() const { return 0 == size(); } 39 bool empty() const { return 0 == size(); }
39 40
40 void AddAttachment(scoped_refptr<MessageAttachment> attachment); 41 bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
42
43 // Take the nth attachment from the beginning of the set, Code using this
44 // /must/ access the attachments in order, and must do it at most once.
45 //
46 // This interface is designed for the deserialising code as it doesn't
47 // support close flags.
48 // returns: an attachments, or nullptr on error
agl 2015/01/30 19:34:02 s/attachments/attachment/
Hajime Morrita 2015/01/30 21:01:29 Done.
41 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index); 49 scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
42 50
43 #if defined(OS_POSIX) 51 #if defined(OS_POSIX)
44 // This is the maximum number of descriptors per message. We need to know this 52 // This is the maximum number of descriptors per message. We need to know this
45 // because the control message kernel interface has to be given a buffer which 53 // because the control message kernel interface has to be given a buffer which
46 // is large enough to store all the descriptor numbers. Otherwise the kernel 54 // is large enough to store all the descriptor numbers. Otherwise the kernel
47 // tells us that it truncated the control data and the extra descriptors are 55 // tells us that it truncated the control data and the extra descriptors are
48 // lost. 56 // lost.
49 // 57 //
50 // In debugging mode, it's a fatal error to try and add more than this number 58 // In debugging mode, it's a fatal error to try and add more than this number
51 // of descriptors to a MessageAttachmentSet. 59 // of descriptors to a MessageAttachmentSet.
52 static const size_t kMaxDescriptorsPerMessage = 7; 60 static const size_t kMaxDescriptorsPerMessage = 7;
53 61
54 // --------------------------------------------------------------------------- 62 // ---------------------------------------------------------------------------
55 // Interfaces for building during message serialisation...
56
57 // Add a descriptor to the end of the set. Returns false iff the set is full.
58 bool AddToBorrow(base::PlatformFile fd);
59 // Add a descriptor to the end of the set and automatically close it after
60 // transmission. Returns false iff the set is full.
61 bool AddToOwn(base::ScopedFD fd);
62
63 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
65 // Interfaces for accessing during message deserialisation...
66
67 // Take the nth descriptor from the beginning of the set,
68 // transferring the ownership of the descriptor taken. Code using this
69 // /must/ access the descriptors in order, and must do it at most once.
70 //
71 // This interface is designed for the deserialising code as it doesn't
72 // support close flags.
73 // returns: file descriptor, or -1 on error
74 base::PlatformFile TakeDescriptorAt(unsigned n);
75
76 // ---------------------------------------------------------------------------
77
78 // ---------------------------------------------------------------------------
79 // Interfaces for transmission... 63 // Interfaces for transmission...
80 64
81 // Fill an array with file descriptors without 'consuming' them. CommitAll 65 // Fill an array with file descriptors without 'consuming' them. CommitAll
82 // must be called after these descriptors have been transmitted. 66 // must be called after these descriptors have been transmitted.
83 // buffer: (output) a buffer of, at least, size() integers. 67 // buffer: (output) a buffer of, at least, size() integers.
84 void PeekDescriptors(base::PlatformFile* buffer) const; 68 void PeekDescriptors(base::PlatformFile* buffer) const;
85 // This must be called after transmitting the descriptors returned by 69 // This must be called after transmitting the descriptors returned by
86 // PeekDescriptors. It marks all the descriptors as consumed and closes those 70 // PeekDescriptors. It marks all the descriptors as consumed and closes those
87 // which are auto-close. 71 // which are auto-close.
88 void CommitAll(); 72 void CommitAll();
(...skipping 20 matching lines...) Expand all
109 93
110 private: 94 private:
111 friend class base::RefCountedThreadSafe<MessageAttachmentSet>; 95 friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
112 96
113 ~MessageAttachmentSet(); 97 ~MessageAttachmentSet();
114 98
115 // A vector of attachments of the message, which might be |PlatformFile| or 99 // A vector of attachments of the message, which might be |PlatformFile| or
116 // |MessagePipe|. 100 // |MessagePipe|.
117 std::vector<scoped_refptr<MessageAttachment>> attachments_; 101 std::vector<scoped_refptr<MessageAttachment>> attachments_;
118 102
119 #if defined(OS_POSIX)
120 // A vector of owning descriptors. If this message is sent, then file
121 // descriptors are sent as control data. After sending, any owning descriptors
122 // are closed. If this message has been received then all received
123 // descriptors are owned by this message.
124 ScopedVector<base::ScopedFD> owned_descriptors_;
125 #endif
126
127 // This contains the index of the next descriptor which should be consumed. 103 // This contains the index of the next descriptor which should be consumed.
128 // It's used in a couple of ways. Firstly, at destruction we can check that 104 // It's used in a couple of ways. Firstly, at destruction we can check that
129 // all the descriptors have been read (with GetNthDescriptor). Secondly, we 105 // all the descriptors have been read (with GetNthDescriptor). Secondly, we
130 // can check that they are read in order. 106 // can check that they are read in order.
131 mutable unsigned consumed_descriptor_highwater_; 107 mutable unsigned consumed_descriptor_highwater_;
132 108
133 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet); 109 DISALLOW_COPY_AND_ASSIGN(MessageAttachmentSet);
134 }; 110 };
135 111
136 } // namespace IPC 112 } // namespace IPC
137 113
138 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_ 114 #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698