OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "ipc/ipc_channel_reader.h" |
| 6 |
| 7 namespace IPC { |
| 8 namespace internal { |
| 9 |
| 10 ChannelReader::ChannelReader(Channel::Listener* listener) |
| 11 : listener_(listener) { |
| 12 } |
| 13 |
| 14 ChannelReader::~ChannelReader() { |
| 15 } |
| 16 |
| 17 bool ChannelReader::ProcessIncomingMessages() { |
| 18 while (true) { |
| 19 int bytes_read = 0; |
| 20 ReadState read_state = ReadData(input_buf_, Channel::kReadBufferSize, |
| 21 &bytes_read); |
| 22 if (read_state == READ_FAILED) |
| 23 return false; |
| 24 if (read_state == READ_PENDING) |
| 25 return true; |
| 26 |
| 27 DCHECK(bytes_read > 0); |
| 28 if (!DispatchInputData(input_buf_, bytes_read)) |
| 29 return false; |
| 30 } |
| 31 } |
| 32 |
| 33 bool ChannelReader::AsyncReadComplete(int bytes_read) { |
| 34 return DispatchInputData(input_buf_, bytes_read); |
| 35 } |
| 36 |
| 37 bool ChannelReader::IsHelloMessage(const Message& m) const { |
| 38 return m.routing_id() == MSG_ROUTING_NONE && |
| 39 m.type() == Channel::HELLO_MESSAGE_TYPE; |
| 40 } |
| 41 |
| 42 bool ChannelReader::DispatchInputData(const char* input_data, |
| 43 int input_data_len) { |
| 44 const char* p; |
| 45 const char* end; |
| 46 |
| 47 // Possibly combine with the overflow buffer to make a larger buffer. |
| 48 if (input_overflow_buf_.empty()) { |
| 49 p = input_data; |
| 50 end = input_data + input_data_len; |
| 51 } else { |
| 52 if (input_overflow_buf_.size() > |
| 53 Channel::kMaximumMessageSize - input_data_len) { |
| 54 input_overflow_buf_.clear(); |
| 55 LOG(ERROR) << "IPC message is too big"; |
| 56 return false; |
| 57 } |
| 58 input_overflow_buf_.append(input_data, input_data_len); |
| 59 p = input_overflow_buf_.data(); |
| 60 end = p + input_overflow_buf_.size(); |
| 61 } |
| 62 |
| 63 // Dispatch all complete messages in the data buffer. |
| 64 while (p < end) { |
| 65 const char* message_tail = Message::FindNext(p, end); |
| 66 if (message_tail) { |
| 67 int len = static_cast<int>(message_tail - p); |
| 68 Message m(p, len); |
| 69 if (!WillDispatchInputMessage(&m)) |
| 70 return false; |
| 71 |
| 72 if (IsHelloMessage(m)) |
| 73 HandleHelloMessage(m); |
| 74 else |
| 75 listener_->OnMessageReceived(m); |
| 76 p = message_tail; |
| 77 } else { |
| 78 // Last message is partial. |
| 79 break; |
| 80 } |
| 81 } |
| 82 |
| 83 // Save any partial data in the overflow buffer. |
| 84 input_overflow_buf_.assign(p, end - p); |
| 85 |
| 86 if (input_overflow_buf_.empty() && !DidEmptyInputBuffers()) |
| 87 return false; |
| 88 return true; |
| 89 } |
| 90 |
| 91 |
| 92 } // namespace internal |
| 93 } // namespace IPC |
OLD | NEW |