15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef IPC_IPC_CHANNEL_READER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define IPC_IPC_CHANNEL_READER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/basictypes.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipc/ipc_channel.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipc/ipc_export.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace IPC {
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class provides common pipe reading functionality for the
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform-specific IPC channel implementations.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It does the common input buffer management and message dispatch, while the
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform-specific parts provide the pipe management through a virtual
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// interface implemented on a per-platform basis.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that there is no "writer" corresponding to this because the code for
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// writing to the channel is much simpler and has very little common
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// functionality that would benefit from being factored out. If we add
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// something like that in the future, it would be more appropriate to add it
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// here (and rename appropriately) rather than writing a different class.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ChannelReader {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit ChannelReader(Listener* listener);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ChannelReader();
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_listener(Listener* listener) { listener_ = listener; }
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call to process messages received from the IPC connection and dispatch
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // them. Returns false on channel error. True indicates that everything
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // succeeded, although there may not have been any messages processed.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ProcessIncomingMessages();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handles asynchronously read data.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Optionally call this after returning READ_PENDING from ReadData to
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // indicate that buffer was filled with the given number of bytes of
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // data. See ReadData for more.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AsyncReadComplete(int bytes_read);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the given message is internal to the IPC implementation,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // like the "hello" message sent on channel set-up.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsInternalMessage(const Message& m);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if the given message is an Hello message
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // sent on channel set-up.
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool IsHelloMessage(const Message& m);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING };
56
57  Listener* listener() const { return listener_; }
58
59  // Populates the given buffer with data from the pipe.
60  //
61  // Returns the state of the read. On READ_SUCCESS, the number of bytes
62  // read will be placed into |*bytes_read| (which can be less than the
63  // buffer size). On READ_FAILED, the channel will be closed.
64  //
65  // If the return value is READ_PENDING, it means that there was no data
66  // ready for reading. The implementation is then responsible for either
67  // calling AsyncReadComplete with the number of bytes read into the
68  // buffer, or ProcessIncomingMessages to try the read again (depending
69  // on whether the platform's async I/O is "try again" or "write
70  // asynchronously into your buffer").
71  virtual ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) = 0;
72
73  // Loads the required file desciptors into the given message. Returns true
74  // on success. False means a fatal channel error.
75  //
76  // This will read from the input_fds_ and read more handles from the FD
77  // pipe if necessary.
78  virtual bool WillDispatchInputMessage(Message* msg) = 0;
79
80  // Performs post-dispatch checks. Called when all input buffers are empty,
81  // though there could be more data ready to be read from the OS.
82  virtual bool DidEmptyInputBuffers() = 0;
83
84  // Handles internal messages, like the hello message sent on channel startup.
85  virtual void HandleInternalMessage(const Message& msg) = 0;
86
87 private:
88  // Takes the given data received from the IPC channel and dispatches any
89  // fully completed messages.
90  //
91  // Returns true on success. False means channel error.
92  bool DispatchInputData(const char* input_data, int input_data_len);
93
94  Listener* listener_;
95
96  // We read from the pipe into this buffer. Managed by DispatchInputData, do
97  // not access directly outside that function.
98  char input_buf_[Channel::kReadBufferSize];
99
100  // Large messages that span multiple pipe buffers, get built-up using
101  // this buffer.
102  std::string input_overflow_buf_;
103
104  DISALLOW_COPY_AND_ASSIGN(ChannelReader);
105};
106
107}  // namespace internal
108}  // namespace IPC
109
110#endif  // IPC_IPC_CHANNEL_READER_H_
111