ipc_channel_reader.h 6.5 KB

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