blob: ffd3b6e629f6bb92b5dbbfcb2930ea3d007337e1 [file] [log] [blame]
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +09001// 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.
4
5#ifndef IPC_IPC_CHANNEL_READER_H_
6#define IPC_IPC_CHANNEL_READER_H_
7
avi42ebda42015-12-22 11:39:04 +09008#include <stddef.h>
9
erikchenf295bbc2015-07-28 03:26:14 +090010#include <set>
11
erikchenf295bbc2015-07-28 03:26:14 +090012#include "base/gtest_prod_util.h"
tfarina2d565d32015-09-16 18:56:21 +090013#include "base/macros.h"
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090014#include "ipc/ipc_channel.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090015#include "ipc/ipc_export.h"
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090016
17namespace IPC {
18namespace internal {
19
20// This class provides common pipe reading functionality for the
21// platform-specific IPC channel implementations.
22//
23// It does the common input buffer management and message dispatch, while the
24// platform-specific parts provide the pipe management through a virtual
25// interface implemented on a per-platform basis.
26//
27// Note that there is no "writer" corresponding to this because the code for
28// writing to the channel is much simpler and has very little common
29// functionality that would benefit from being factored out. If we add
30// something like that in the future, it would be more appropriate to add it
31// here (and rename appropriately) rather than writing a different class.
sammcc54b4e92016-11-09 08:24:35 +090032class IPC_EXPORT ChannelReader {
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090033 public:
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090034 explicit ChannelReader(Listener* listener);
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090035 virtual ~ChannelReader();
36
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090037 void set_listener(Listener* listener) { listener_ = listener; }
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090038
erikchenf295bbc2015-07-28 03:26:14 +090039 // This type is returned by ProcessIncomingMessages to indicate the effect of
40 // the method.
41 enum DispatchState {
42 // All messages were successfully dispatched, or there were no messages to
43 // dispatch.
44 DISPATCH_FINISHED,
45 // There was a channel error.
46 DISPATCH_ERROR,
47 // Dispatching messages is blocked on receiving more information from the
48 // broker.
49 DISPATCH_WAITING_ON_BROKER,
50 };
51
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090052 // Call to process messages received from the IPC connection and dispatch
erikchenf295bbc2015-07-28 03:26:14 +090053 // them.
54 DispatchState ProcessIncomingMessages();
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090055
56 // Handles asynchronously read data.
57 //
58 // Optionally call this after returning READ_PENDING from ReadData to
59 // indicate that buffer was filled with the given number of bytes of
60 // data. See ReadData for more.
erikchenf295bbc2015-07-28 03:26:14 +090061 DispatchState AsyncReadComplete(int bytes_read);
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090062
hubbe@chromium.org683920d2013-10-15 09:07:00 +090063 // Returns true if the given message is internal to the IPC implementation,
64 // like the "hello" message sent on channel set-up.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090065 bool IsInternalMessage(const Message& m);
hubbe@chromium.org683920d2013-10-15 09:07:00 +090066
67 // Returns true if the given message is an Hello message
68 // sent on channel set-up.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090069 bool IsHelloMessage(const Message& m);
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090070
71 protected:
72 enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING };
73
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090074 Listener* listener() const { return listener_; }
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090075
erikchen133c2892015-09-12 02:33:34 +090076 // Subclasses should call this method in their destructor to give this class a
77 // chance to clean up state that might be dependent on subclass members.
78 void CleanUp();
79
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090080 // Populates the given buffer with data from the pipe.
81 //
82 // Returns the state of the read. On READ_SUCCESS, the number of bytes
83 // read will be placed into |*bytes_read| (which can be less than the
84 // buffer size). On READ_FAILED, the channel will be closed.
85 //
86 // If the return value is READ_PENDING, it means that there was no data
87 // ready for reading. The implementation is then responsible for either
88 // calling AsyncReadComplete with the number of bytes read into the
89 // buffer, or ProcessIncomingMessages to try the read again (depending
90 // on whether the platform's async I/O is "try again" or "write
91 // asynchronously into your buffer").
92 virtual ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) = 0;
93
94 // Loads the required file desciptors into the given message. Returns true
95 // on success. False means a fatal channel error.
96 //
97 // This will read from the input_fds_ and read more handles from the FD
98 // pipe if necessary.
erikchenf295bbc2015-07-28 03:26:14 +090099 virtual bool ShouldDispatchInputMessage(Message* msg) = 0;
100
101 // Overridden by subclasses to get attachments that are sent alongside the IPC
sammc14583362016-11-23 12:17:35 +0900102 // channel.
erikchenf295bbc2015-07-28 03:26:14 +0900103 // Returns true on success. False means a fatal channel error.
sammc14583362016-11-23 12:17:35 +0900104 virtual bool GetAttachments(Message* msg) = 0;
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +0900105
106 // Performs post-dispatch checks. Called when all input buffers are empty,
107 // though there could be more data ready to be read from the OS.
108 virtual bool DidEmptyInputBuffers() = 0;
109
hubbe@chromium.org683920d2013-10-15 09:07:00 +0900110 // Handles internal messages, like the hello message sent on channel startup.
111 virtual void HandleInternalMessage(const Message& msg) = 0;
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +0900112
erikchenf295bbc2015-07-28 03:26:14 +0900113 // Exposed for testing purposes only.
erikchenf295bbc2015-07-28 03:26:14 +0900114 virtual void DispatchMessage(Message* m);
115
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +0900116 private:
erikchenf295bbc2015-07-28 03:26:14 +0900117 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentAlreadyBrokered);
118 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentNotYetBrokered);
dskibaaa08dcb2015-10-01 02:24:30 +0900119 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, ResizeOverflowBuffer);
120 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, InvalidMessageSize);
dskiba905b9b92015-11-04 10:24:59 +0900121 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, TrimBuffer);
erikchenf295bbc2015-07-28 03:26:14 +0900122
erikchen2c0e5dc2015-10-22 07:28:54 +0900123 // Takes the data received from the IPC channel and translates it into
124 // Messages. Complete messages are passed to HandleTranslatedMessage().
125 // Returns |false| on unrecoverable error.
erikchenf295bbc2015-07-28 03:26:14 +0900126 bool TranslateInputData(const char* input_data, int input_data_len);
127
erikchen2c0e5dc2015-10-22 07:28:54 +0900128 // Internal messages and messages bound for the attachment broker are
129 // immediately dispatched. Other messages are passed to
130 // HandleExternalMessage().
131 // Returns |false| on unrecoverable error.
sammcc54b4e92016-11-09 08:24:35 +0900132 bool HandleTranslatedMessage(Message* translated_message);
erikchen2c0e5dc2015-10-22 07:28:54 +0900133
134 // Populates the message with brokered and non-brokered attachments. If
135 // possible, the message is immediately dispatched. Otherwise, a deep copy of
136 // the message is added to |queued_messages_|. |blocked_ids_| are updated if
137 // necessary.
sammcc54b4e92016-11-09 08:24:35 +0900138 bool HandleExternalMessage(Message* external_message);
erikchen2c0e5dc2015-10-22 07:28:54 +0900139
140 // If there was a dispatch error, informs |listener_|.
141 void HandleDispatchError(const Message& message);
142
dskibaaa08dcb2015-10-01 02:24:30 +0900143 // Checks that |size| is a valid message size. Has side effects if it's not.
144 bool CheckMessageSize(size_t size);
145
brettw@chromium.orgf947ed02012-06-12 07:35:26 +0900146 Listener* listener_;
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +0900147
148 // We read from the pipe into this buffer. Managed by DispatchInputData, do
149 // not access directly outside that function.
150 char input_buf_[Channel::kReadBufferSize];
151
152 // Large messages that span multiple pipe buffers, get built-up using
153 // this buffer.
154 std::string input_overflow_buf_;
155
dskiba905b9b92015-11-04 10:24:59 +0900156 // Maximum overflow buffer size, see Channel::kMaximumReadBufferSize.
157 // This is not a constant because we update it to reflect the reality
158 // of std::string::reserve() implementation.
159 size_t max_input_buffer_size_;
160
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +0900161 DISALLOW_COPY_AND_ASSIGN(ChannelReader);
162};
163
164} // namespace internal
165} // namespace IPC
166
167#endif // IPC_IPC_CHANNEL_READER_H_