1 // Copyright (c) 2012 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_CHANNEL_READER_H_
6 #define IPC_IPC_CHANNEL_READER_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_vector.h"
13 #include "ipc/attachment_broker.h"
14 #include "ipc/brokerable_attachment.h"
15 #include "ipc/ipc_channel.h"
16 #include "ipc/ipc_export.h"
21 // This class provides common pipe reading functionality for the
22 // platform-specific IPC channel implementations.
24 // It does the common input buffer management and message dispatch, while the
25 // platform-specific parts provide the pipe management through a virtual
26 // interface implemented on a per-platform basis.
28 // Note that there is no "writer" corresponding to this because the code for
29 // writing to the channel is much simpler and has very little common
30 // functionality that would benefit from being factored out. If we add
31 // something like that in the future, it would be more appropriate to add it
32 // here (and rename appropriately) rather than writing a different class.
33 class IPC_EXPORT ChannelReader
: public SupportsAttachmentBrokering
,
34 public AttachmentBroker::Observer
{
36 explicit ChannelReader(Listener
* listener
);
37 virtual ~ChannelReader();
39 void set_listener(Listener
* listener
) { listener_
= listener
; }
41 // This type is returned by ProcessIncomingMessages to indicate the effect of
44 // All messages were successfully dispatched, or there were no messages to
47 // There was a channel error.
49 // Dispatching messages is blocked on receiving more information from the
51 DISPATCH_WAITING_ON_BROKER
,
54 // Call to process messages received from the IPC connection and dispatch
56 DispatchState
ProcessIncomingMessages();
58 // Handles asynchronously read data.
60 // Optionally call this after returning READ_PENDING from ReadData to
61 // indicate that buffer was filled with the given number of bytes of
62 // data. See ReadData for more.
63 DispatchState
AsyncReadComplete(int bytes_read
);
65 // Returns true if the given message is internal to the IPC implementation,
66 // like the "hello" message sent on channel set-up.
67 bool IsInternalMessage(const Message
& m
);
69 // Returns true if the given message is an Hello message
70 // sent on channel set-up.
71 bool IsHelloMessage(const Message
& m
);
74 enum ReadState
{ READ_SUCCEEDED
, READ_FAILED
, READ_PENDING
};
76 Listener
* listener() const { return listener_
; }
78 // Subclasses should call this method in their destructor to give this class a
79 // chance to clean up state that might be dependent on subclass members.
82 // Populates the given buffer with data from the pipe.
84 // Returns the state of the read. On READ_SUCCESS, the number of bytes
85 // read will be placed into |*bytes_read| (which can be less than the
86 // buffer size). On READ_FAILED, the channel will be closed.
88 // If the return value is READ_PENDING, it means that there was no data
89 // ready for reading. The implementation is then responsible for either
90 // calling AsyncReadComplete with the number of bytes read into the
91 // buffer, or ProcessIncomingMessages to try the read again (depending
92 // on whether the platform's async I/O is "try again" or "write
93 // asynchronously into your buffer").
94 virtual ReadState
ReadData(char* buffer
, int buffer_len
, int* bytes_read
) = 0;
96 // Loads the required file desciptors into the given message. Returns true
97 // on success. False means a fatal channel error.
99 // This will read from the input_fds_ and read more handles from the FD
100 // pipe if necessary.
101 virtual bool ShouldDispatchInputMessage(Message
* msg
) = 0;
103 // Overridden by subclasses to get attachments that are sent alongside the IPC
104 // channel (as opposed to through a broker).
105 // Returns true on success. False means a fatal channel error.
106 virtual bool GetNonBrokeredAttachments(Message
* msg
) = 0;
108 // Performs post-dispatch checks. Called when all input buffers are empty,
109 // though there could be more data ready to be read from the OS.
110 virtual bool DidEmptyInputBuffers() = 0;
112 // Handles internal messages, like the hello message sent on channel startup.
113 virtual void HandleInternalMessage(const Message
& msg
) = 0;
115 // Exposed for testing purposes only.
116 ScopedVector
<Message
>* get_queued_messages() { return &queued_messages_
; }
118 // Exposed for testing purposes only.
119 virtual void DispatchMessage(Message
* m
);
121 // Get the process ID for the sender of the message.
122 virtual base::ProcessId
GetSenderPID() = 0;
124 // Whether the channel is an endpoint of attachment brokering.
125 virtual bool IsAttachmentBrokerEndpoint() = 0;
128 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest
, AttachmentAlreadyBrokered
);
129 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest
, AttachmentNotYetBrokered
);
131 typedef std::set
<BrokerableAttachment::AttachmentId
> AttachmentIdSet
;
133 // Takes the given data received from the IPC channel, translates it into
134 // Messages, and puts them in queued_messages_.
135 // As an optimization, after a message is translated, the message is
136 // immediately dispatched if able. This prevents an otherwise unnecessary deep
137 // copy of the message which is needed to store the message in the message
139 bool TranslateInputData(const char* input_data
, int input_data_len
);
141 // Dispatches messages from queued_messages_ to listeners. Successfully
142 // dispatched messages are removed from queued_messages_.
143 DispatchState
DispatchMessages();
145 // Attempts to fill in the brokerable attachments of |msg| with information
146 // from the Attachment Broker.
147 // Returns the set of ids that are still waiting to be brokered.
148 AttachmentIdSet
GetBrokeredAttachments(Message
* msg
);
150 // AttachmentBroker::Observer overrides.
151 void ReceivedBrokerableAttachmentWithId(
152 const BrokerableAttachment::AttachmentId
& id
) override
;
154 // This class should observe the attachment broker if and only if blocked_ids_
156 void StartObservingAttachmentBroker();
157 void StopObservingAttachmentBroker();
161 // We read from the pipe into this buffer. Managed by DispatchInputData, do
162 // not access directly outside that function.
163 char input_buf_
[Channel::kReadBufferSize
];
165 // Large messages that span multiple pipe buffers, get built-up using
167 std::string input_overflow_buf_
;
169 // These messages are waiting to be dispatched. If this vector is non-empty,
170 // then the front Message must be blocked on receiving an attachment from the
172 ScopedVector
<Message
> queued_messages_
;
174 // If the next message to be processed is blocked by the broker, then this
175 // set contains the AttachmentIds that are needed to unblock the message.
176 AttachmentIdSet blocked_ids_
;
178 DISALLOW_COPY_AND_ASSIGN(ChannelReader
);
181 } // namespace internal
184 #endif // IPC_IPC_CHANNEL_READER_H_