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_
8 #include "base/basictypes.h"
9 #include "ipc/attachment_broker.h"
10 #include "ipc/ipc_channel.h"
11 #include "ipc/ipc_export.h"
16 // This class provides common pipe reading functionality for the
17 // platform-specific IPC channel implementations.
19 // It does the common input buffer management and message dispatch, while the
20 // platform-specific parts provide the pipe management through a virtual
21 // interface implemented on a per-platform basis.
23 // Note that there is no "writer" corresponding to this because the code for
24 // writing to the channel is much simpler and has very little common
25 // functionality that would benefit from being factored out. If we add
26 // something like that in the future, it would be more appropriate to add it
27 // here (and rename appropriately) rather than writing a different class.
28 class ChannelReader
: public SupportsAttachmentBrokering
{
30 explicit ChannelReader(Listener
* listener
);
31 virtual ~ChannelReader();
33 void set_listener(Listener
* listener
) { listener_
= listener
; }
35 // Call to process messages received from the IPC connection and dispatch
36 // them. Returns false on channel error. True indicates that everything
37 // succeeded, although there may not have been any messages processed.
38 bool ProcessIncomingMessages();
40 // Handles asynchronously read data.
42 // Optionally call this after returning READ_PENDING from ReadData to
43 // indicate that buffer was filled with the given number of bytes of
44 // data. See ReadData for more.
45 bool AsyncReadComplete(int bytes_read
);
47 // Returns true if the given message is internal to the IPC implementation,
48 // like the "hello" message sent on channel set-up.
49 bool IsInternalMessage(const Message
& m
);
51 // Returns true if the given message is an Hello message
52 // sent on channel set-up.
53 bool IsHelloMessage(const Message
& m
);
56 enum ReadState
{ READ_SUCCEEDED
, READ_FAILED
, READ_PENDING
};
58 Listener
* listener() const { return listener_
; }
60 // Populates the given buffer with data from the pipe.
62 // Returns the state of the read. On READ_SUCCESS, the number of bytes
63 // read will be placed into |*bytes_read| (which can be less than the
64 // buffer size). On READ_FAILED, the channel will be closed.
66 // If the return value is READ_PENDING, it means that there was no data
67 // ready for reading. The implementation is then responsible for either
68 // calling AsyncReadComplete with the number of bytes read into the
69 // buffer, or ProcessIncomingMessages to try the read again (depending
70 // on whether the platform's async I/O is "try again" or "write
71 // asynchronously into your buffer").
72 virtual ReadState
ReadData(char* buffer
, int buffer_len
, int* bytes_read
) = 0;
74 // Loads the required file desciptors into the given message. Returns true
75 // on success. False means a fatal channel error.
77 // This will read from the input_fds_ and read more handles from the FD
79 virtual bool WillDispatchInputMessage(Message
* msg
) = 0;
81 // Performs post-dispatch checks. Called when all input buffers are empty,
82 // though there could be more data ready to be read from the OS.
83 virtual bool DidEmptyInputBuffers() = 0;
85 // Handles internal messages, like the hello message sent on channel startup.
86 virtual void HandleInternalMessage(const Message
& msg
) = 0;
89 // Takes the given data received from the IPC channel and dispatches any
90 // fully completed messages.
92 // Returns true on success. False means channel error.
93 bool DispatchInputData(const char* input_data
, int input_data_len
);
97 // We read from the pipe into this buffer. Managed by DispatchInputData, do
98 // not access directly outside that function.
99 char input_buf_
[Channel::kReadBufferSize
];
101 // Large messages that span multiple pipe buffers, get built-up using
103 std::string input_overflow_buf_
;
105 DISALLOW_COPY_AND_ASSIGN(ChannelReader
);
108 } // namespace internal
111 #endif // IPC_IPC_CHANNEL_READER_H_