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/basictypes.h"
11 #include "base/gtest_prod_util.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 // Populates the given buffer with data from the pipe.
80 // Returns the state of the read. On READ_SUCCESS, the number of bytes
81 // read will be placed into |*bytes_read| (which can be less than the
82 // buffer size). On READ_FAILED, the channel will be closed.
84 // If the return value is READ_PENDING, it means that there was no data
85 // ready for reading. The implementation is then responsible for either
86 // calling AsyncReadComplete with the number of bytes read into the
87 // buffer, or ProcessIncomingMessages to try the read again (depending
88 // on whether the platform's async I/O is "try again" or "write
89 // asynchronously into your buffer").
90 virtual ReadState
ReadData(char* buffer
, int buffer_len
, int* bytes_read
) = 0;
92 // Loads the required file desciptors into the given message. Returns true
93 // on success. False means a fatal channel error.
95 // This will read from the input_fds_ and read more handles from the FD
97 virtual bool ShouldDispatchInputMessage(Message
* msg
) = 0;
99 // Overridden by subclasses to get attachments that are sent alongside the IPC
100 // channel (as opposed to through a broker).
101 // Returns true on success. False means a fatal channel error.
102 virtual bool GetNonBrokeredAttachments(Message
* msg
) = 0;
104 // Performs post-dispatch checks. Called when all input buffers are empty,
105 // though there could be more data ready to be read from the OS.
106 virtual bool DidEmptyInputBuffers() = 0;
108 // Handles internal messages, like the hello message sent on channel startup.
109 virtual void HandleInternalMessage(const Message
& msg
) = 0;
111 // Exposed for testing purposes only.
112 ScopedVector
<Message
>* get_queued_messages() { return &queued_messages_
; }
114 // Exposed for testing purposes only.
115 virtual void DispatchMessage(Message
* m
);
117 // Get the process ID for the sender of the message.
118 virtual base::ProcessId
GetSenderPID() = 0;
121 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest
, AttachmentAlreadyBrokered
);
122 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest
, AttachmentNotYetBrokered
);
124 typedef std::set
<BrokerableAttachment::AttachmentId
> AttachmentIdSet
;
126 // Takes the given data received from the IPC channel, translates it into
127 // Messages, and puts them in queued_messages_.
128 // As an optimization, after a message is translated, the message is
129 // immediately dispatched if able. This prevents an otherwise unnecessary deep
130 // copy of the message which is needed to store the message in the message
132 bool TranslateInputData(const char* input_data
, int input_data_len
);
134 // Dispatches messages from queued_messages_ to listeners. Successfully
135 // dispatched messages are removed from queued_messages_.
136 DispatchState
DispatchMessages();
138 // Attempts to fill in the brokerable attachments of |msg| with information
139 // from the Attachment Broker.
140 // Returns the set of ids that are still waiting to be brokered.
141 AttachmentIdSet
GetBrokeredAttachments(Message
* msg
);
143 // AttachmentBroker::Observer overrides.
144 void ReceivedBrokerableAttachmentWithId(
145 const BrokerableAttachment::AttachmentId
& id
) override
;
147 // This class should observe the attachment broker if and only if blocked_ids_
149 void StartObservingAttachmentBroker();
150 void StopObservingAttachmentBroker();
154 // We read from the pipe into this buffer. Managed by DispatchInputData, do
155 // not access directly outside that function.
156 char input_buf_
[Channel::kReadBufferSize
];
158 // Large messages that span multiple pipe buffers, get built-up using
160 std::string input_overflow_buf_
;
162 // These messages are waiting to be dispatched. If this vector is non-empty,
163 // then the front Message must be blocked on receiving an attachment from the
165 ScopedVector
<Message
> queued_messages_
;
167 // If the next message to be processed is blocked by the broker, then this
168 // set contains the AttachmentIds that are needed to unblock the message.
169 AttachmentIdSet blocked_ids_
;
171 DISALLOW_COPY_AND_ASSIGN(ChannelReader
);
174 } // namespace internal
177 #endif // IPC_IPC_CHANNEL_READER_H_