1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #ifndef IPC_IPC_CHANNEL_READER_H_
6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_CHANNEL_READER_H_
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker
10*635a8641SAndroid Build Coastguard Worker #include <set>
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Worker #include "base/component_export.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
15*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel.h"
16*635a8641SAndroid Build Coastguard Worker
17*635a8641SAndroid Build Coastguard Worker namespace IPC {
18*635a8641SAndroid Build Coastguard Worker namespace internal {
19*635a8641SAndroid Build Coastguard Worker
20*635a8641SAndroid Build Coastguard Worker // This class provides common pipe reading functionality for the
21*635a8641SAndroid Build Coastguard Worker // platform-specific IPC channel implementations.
22*635a8641SAndroid Build Coastguard Worker //
23*635a8641SAndroid Build Coastguard Worker // It does the common input buffer management and message dispatch, while the
24*635a8641SAndroid Build Coastguard Worker // platform-specific parts provide the pipe management through a virtual
25*635a8641SAndroid Build Coastguard Worker // interface implemented on a per-platform basis.
26*635a8641SAndroid Build Coastguard Worker //
27*635a8641SAndroid Build Coastguard Worker // Note that there is no "writer" corresponding to this because the code for
28*635a8641SAndroid Build Coastguard Worker // writing to the channel is much simpler and has very little common
29*635a8641SAndroid Build Coastguard Worker // functionality that would benefit from being factored out. If we add
30*635a8641SAndroid Build Coastguard Worker // something like that in the future, it would be more appropriate to add it
31*635a8641SAndroid Build Coastguard Worker // here (and rename appropriately) rather than writing a different class.
COMPONENT_EXPORT(IPC)32*635a8641SAndroid Build Coastguard Worker class COMPONENT_EXPORT(IPC) ChannelReader {
33*635a8641SAndroid Build Coastguard Worker public:
34*635a8641SAndroid Build Coastguard Worker explicit ChannelReader(Listener* listener);
35*635a8641SAndroid Build Coastguard Worker virtual ~ChannelReader();
36*635a8641SAndroid Build Coastguard Worker
37*635a8641SAndroid Build Coastguard Worker void set_listener(Listener* listener) { listener_ = listener; }
38*635a8641SAndroid Build Coastguard Worker
39*635a8641SAndroid Build Coastguard Worker // This type is returned by ProcessIncomingMessages to indicate the effect of
40*635a8641SAndroid Build Coastguard Worker // the method.
41*635a8641SAndroid Build Coastguard Worker enum DispatchState {
42*635a8641SAndroid Build Coastguard Worker // All messages were successfully dispatched, or there were no messages to
43*635a8641SAndroid Build Coastguard Worker // dispatch.
44*635a8641SAndroid Build Coastguard Worker DISPATCH_FINISHED,
45*635a8641SAndroid Build Coastguard Worker // There was a channel error.
46*635a8641SAndroid Build Coastguard Worker DISPATCH_ERROR,
47*635a8641SAndroid Build Coastguard Worker // Dispatching messages is blocked on receiving more information from the
48*635a8641SAndroid Build Coastguard Worker // broker.
49*635a8641SAndroid Build Coastguard Worker DISPATCH_WAITING_ON_BROKER,
50*635a8641SAndroid Build Coastguard Worker };
51*635a8641SAndroid Build Coastguard Worker
52*635a8641SAndroid Build Coastguard Worker // Call to process messages received from the IPC connection and dispatch
53*635a8641SAndroid Build Coastguard Worker // them.
54*635a8641SAndroid Build Coastguard Worker DispatchState ProcessIncomingMessages();
55*635a8641SAndroid Build Coastguard Worker
56*635a8641SAndroid Build Coastguard Worker // Handles asynchronously read data.
57*635a8641SAndroid Build Coastguard Worker //
58*635a8641SAndroid Build Coastguard Worker // Optionally call this after returning READ_PENDING from ReadData to
59*635a8641SAndroid Build Coastguard Worker // indicate that buffer was filled with the given number of bytes of
60*635a8641SAndroid Build Coastguard Worker // data. See ReadData for more.
61*635a8641SAndroid Build Coastguard Worker DispatchState AsyncReadComplete(int bytes_read);
62*635a8641SAndroid Build Coastguard Worker
63*635a8641SAndroid Build Coastguard Worker // Returns true if the given message is internal to the IPC implementation,
64*635a8641SAndroid Build Coastguard Worker // like the "hello" message sent on channel set-up.
65*635a8641SAndroid Build Coastguard Worker bool IsInternalMessage(const Message& m);
66*635a8641SAndroid Build Coastguard Worker
67*635a8641SAndroid Build Coastguard Worker // Returns true if the given message is an Hello message
68*635a8641SAndroid Build Coastguard Worker // sent on channel set-up.
69*635a8641SAndroid Build Coastguard Worker bool IsHelloMessage(const Message& m);
70*635a8641SAndroid Build Coastguard Worker
71*635a8641SAndroid Build Coastguard Worker protected:
72*635a8641SAndroid Build Coastguard Worker enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING };
73*635a8641SAndroid Build Coastguard Worker
74*635a8641SAndroid Build Coastguard Worker Listener* listener() const { return listener_; }
75*635a8641SAndroid Build Coastguard Worker
76*635a8641SAndroid Build Coastguard Worker // Subclasses should call this method in their destructor to give this class a
77*635a8641SAndroid Build Coastguard Worker // chance to clean up state that might be dependent on subclass members.
78*635a8641SAndroid Build Coastguard Worker void CleanUp();
79*635a8641SAndroid Build Coastguard Worker
80*635a8641SAndroid Build Coastguard Worker // Populates the given buffer with data from the pipe.
81*635a8641SAndroid Build Coastguard Worker //
82*635a8641SAndroid Build Coastguard Worker // Returns the state of the read. On READ_SUCCESS, the number of bytes
83*635a8641SAndroid Build Coastguard Worker // read will be placed into |*bytes_read| (which can be less than the
84*635a8641SAndroid Build Coastguard Worker // buffer size). On READ_FAILED, the channel will be closed.
85*635a8641SAndroid Build Coastguard Worker //
86*635a8641SAndroid Build Coastguard Worker // If the return value is READ_PENDING, it means that there was no data
87*635a8641SAndroid Build Coastguard Worker // ready for reading. The implementation is then responsible for either
88*635a8641SAndroid Build Coastguard Worker // calling AsyncReadComplete with the number of bytes read into the
89*635a8641SAndroid Build Coastguard Worker // buffer, or ProcessIncomingMessages to try the read again (depending
90*635a8641SAndroid Build Coastguard Worker // on whether the platform's async I/O is "try again" or "write
91*635a8641SAndroid Build Coastguard Worker // asynchronously into your buffer").
92*635a8641SAndroid Build Coastguard Worker virtual ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) = 0;
93*635a8641SAndroid Build Coastguard Worker
94*635a8641SAndroid Build Coastguard Worker // Loads the required file desciptors into the given message. Returns true
95*635a8641SAndroid Build Coastguard Worker // on success. False means a fatal channel error.
96*635a8641SAndroid Build Coastguard Worker //
97*635a8641SAndroid Build Coastguard Worker // This will read from the input_fds_ and read more handles from the FD
98*635a8641SAndroid Build Coastguard Worker // pipe if necessary.
99*635a8641SAndroid Build Coastguard Worker virtual bool ShouldDispatchInputMessage(Message* msg) = 0;
100*635a8641SAndroid Build Coastguard Worker
101*635a8641SAndroid Build Coastguard Worker // Overridden by subclasses to get attachments that are sent alongside the IPC
102*635a8641SAndroid Build Coastguard Worker // channel.
103*635a8641SAndroid Build Coastguard Worker // Returns true on success. False means a fatal channel error.
104*635a8641SAndroid Build Coastguard Worker virtual bool GetAttachments(Message* msg) = 0;
105*635a8641SAndroid Build Coastguard Worker
106*635a8641SAndroid Build Coastguard Worker // Performs post-dispatch checks. Called when all input buffers are empty,
107*635a8641SAndroid Build Coastguard Worker // though there could be more data ready to be read from the OS.
108*635a8641SAndroid Build Coastguard Worker virtual bool DidEmptyInputBuffers() = 0;
109*635a8641SAndroid Build Coastguard Worker
110*635a8641SAndroid Build Coastguard Worker // Handles internal messages, like the hello message sent on channel startup.
111*635a8641SAndroid Build Coastguard Worker virtual void HandleInternalMessage(const Message& msg) = 0;
112*635a8641SAndroid Build Coastguard Worker
113*635a8641SAndroid Build Coastguard Worker // Exposed for testing purposes only.
114*635a8641SAndroid Build Coastguard Worker virtual void DispatchMessage(Message* m);
115*635a8641SAndroid Build Coastguard Worker
116*635a8641SAndroid Build Coastguard Worker private:
117*635a8641SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentAlreadyBrokered);
118*635a8641SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentNotYetBrokered);
119*635a8641SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, ResizeOverflowBuffer);
120*635a8641SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, InvalidMessageSize);
121*635a8641SAndroid Build Coastguard Worker FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, TrimBuffer);
122*635a8641SAndroid Build Coastguard Worker
123*635a8641SAndroid Build Coastguard Worker // Takes the data received from the IPC channel and translates it into
124*635a8641SAndroid Build Coastguard Worker // Messages. Complete messages are passed to HandleTranslatedMessage().
125*635a8641SAndroid Build Coastguard Worker // Returns |false| on unrecoverable error.
126*635a8641SAndroid Build Coastguard Worker bool TranslateInputData(const char* input_data, int input_data_len);
127*635a8641SAndroid Build Coastguard Worker
128*635a8641SAndroid Build Coastguard Worker // Internal messages and messages bound for the attachment broker are
129*635a8641SAndroid Build Coastguard Worker // immediately dispatched. Other messages are passed to
130*635a8641SAndroid Build Coastguard Worker // HandleExternalMessage().
131*635a8641SAndroid Build Coastguard Worker // Returns |false| on unrecoverable error.
132*635a8641SAndroid Build Coastguard Worker bool HandleTranslatedMessage(Message* translated_message);
133*635a8641SAndroid Build Coastguard Worker
134*635a8641SAndroid Build Coastguard Worker // Populates the message with brokered and non-brokered attachments. If
135*635a8641SAndroid Build Coastguard Worker // possible, the message is immediately dispatched. Otherwise, a deep copy of
136*635a8641SAndroid Build Coastguard Worker // the message is added to |queued_messages_|. |blocked_ids_| are updated if
137*635a8641SAndroid Build Coastguard Worker // necessary.
138*635a8641SAndroid Build Coastguard Worker bool HandleExternalMessage(Message* external_message);
139*635a8641SAndroid Build Coastguard Worker
140*635a8641SAndroid Build Coastguard Worker // If there was a dispatch error, informs |listener_|.
141*635a8641SAndroid Build Coastguard Worker void HandleDispatchError(const Message& message);
142*635a8641SAndroid Build Coastguard Worker
143*635a8641SAndroid Build Coastguard Worker // Checks that |size| is a valid message size. Has side effects if it's not.
144*635a8641SAndroid Build Coastguard Worker bool CheckMessageSize(size_t size);
145*635a8641SAndroid Build Coastguard Worker
146*635a8641SAndroid Build Coastguard Worker Listener* listener_;
147*635a8641SAndroid Build Coastguard Worker
148*635a8641SAndroid Build Coastguard Worker // We read from the pipe into this buffer. Managed by DispatchInputData, do
149*635a8641SAndroid Build Coastguard Worker // not access directly outside that function.
150*635a8641SAndroid Build Coastguard Worker char input_buf_[Channel::kReadBufferSize];
151*635a8641SAndroid Build Coastguard Worker
152*635a8641SAndroid Build Coastguard Worker // Large messages that span multiple pipe buffers, get built-up using
153*635a8641SAndroid Build Coastguard Worker // this buffer.
154*635a8641SAndroid Build Coastguard Worker std::string input_overflow_buf_;
155*635a8641SAndroid Build Coastguard Worker
156*635a8641SAndroid Build Coastguard Worker // Maximum overflow buffer size, see Channel::kMaximumReadBufferSize.
157*635a8641SAndroid Build Coastguard Worker // This is not a constant because we update it to reflect the reality
158*635a8641SAndroid Build Coastguard Worker // of std::string::reserve() implementation.
159*635a8641SAndroid Build Coastguard Worker size_t max_input_buffer_size_;
160*635a8641SAndroid Build Coastguard Worker
161*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ChannelReader);
162*635a8641SAndroid Build Coastguard Worker };
163*635a8641SAndroid Build Coastguard Worker
164*635a8641SAndroid Build Coastguard Worker } // namespace internal
165*635a8641SAndroid Build Coastguard Worker } // namespace IPC
166*635a8641SAndroid Build Coastguard Worker
167*635a8641SAndroid Build Coastguard Worker #endif // IPC_IPC_CHANNEL_READER_H_
168