OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_CHANNEL_READER_H_ | 5 #ifndef IPC_IPC_CHANNEL_READER_H_ |
6 #define IPC_IPC_CHANNEL_READER_H_ | 6 #define IPC_IPC_CHANNEL_READER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "ipc/ipc_channel.h" | 9 #include "ipc/ipc_channel.h" |
10 | 10 |
11 namespace IPC { | 11 namespace IPC { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 // This class provides common pipe reading functionality for the | 14 // This class provides common pipe reading functionality for the |
15 // platform-specific IPC channel implementations. | 15 // platform-specific IPC channel implementations. |
16 // | 16 // |
17 // It does the common input buffer management and message dispatch, while the | 17 // It does the common input buffer management and message dispatch, while the |
18 // platform-specific parts provide the pipe management through a virtual | 18 // platform-specific parts provide the pipe management through a virtual |
19 // interface implemented on a per-platform basis. | 19 // interface implemented on a per-platform basis. |
20 // | 20 // |
21 // Note that there is no "writer" corresponding to this because the code for | 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 | 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 | 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 | 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. | 25 // here (and rename appropriately) rather than writing a different class. |
26 class ChannelReader { | 26 class ChannelReader { |
27 public: | 27 public: |
28 explicit ChannelReader(Channel::Listener* listener); | 28 explicit ChannelReader(Listener* listener); |
29 virtual ~ChannelReader(); | 29 virtual ~ChannelReader(); |
30 | 30 |
31 void set_listener(Channel::Listener* listener) { listener_ = listener; } | 31 void set_listener(Listener* listener) { listener_ = listener; } |
32 | 32 |
33 // Call to process messages received from the IPC connection and dispatch | 33 // Call to process messages received from the IPC connection and dispatch |
34 // them. Returns false on channel error. True indicates that everything | 34 // them. Returns false on channel error. True indicates that everything |
35 // succeeded, although there may not have been any messages processed. | 35 // succeeded, although there may not have been any messages processed. |
36 bool ProcessIncomingMessages(); | 36 bool ProcessIncomingMessages(); |
37 | 37 |
38 // Handles asynchronously read data. | 38 // Handles asynchronously read data. |
39 // | 39 // |
40 // Optionally call this after returning READ_PENDING from ReadData to | 40 // Optionally call this after returning READ_PENDING from ReadData to |
41 // indicate that buffer was filled with the given number of bytes of | 41 // indicate that buffer was filled with the given number of bytes of |
42 // data. See ReadData for more. | 42 // data. See ReadData for more. |
43 bool AsyncReadComplete(int bytes_read); | 43 bool AsyncReadComplete(int bytes_read); |
44 | 44 |
45 // Returns true if the given message is the "hello" message sent on channel | 45 // Returns true if the given message is the "hello" message sent on channel |
46 // set-up. | 46 // set-up. |
47 bool IsHelloMessage(const Message& m) const; | 47 bool IsHelloMessage(const Message& m) const; |
48 | 48 |
49 protected: | 49 protected: |
50 enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING }; | 50 enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING }; |
51 | 51 |
52 Channel::Listener* listener() const { return listener_; } | 52 Listener* listener() const { return listener_; } |
53 | 53 |
54 // Populates the given buffer with data from the pipe. | 54 // Populates the given buffer with data from the pipe. |
55 // | 55 // |
56 // Returns the state of the read. On READ_SUCCESS, the number of bytes | 56 // Returns the state of the read. On READ_SUCCESS, the number of bytes |
57 // read will be placed into |*bytes_read| (which can be less than the | 57 // read will be placed into |*bytes_read| (which can be less than the |
58 // buffer size). On READ_FAILED, the channel will be closed. | 58 // buffer size). On READ_FAILED, the channel will be closed. |
59 // | 59 // |
60 // If the return value is READ_PENDING, it means that there was no data | 60 // If the return value is READ_PENDING, it means that there was no data |
61 // ready for reading. The implementation is then responsible for either | 61 // ready for reading. The implementation is then responsible for either |
62 // calling AsyncReadComplete with the number of bytes read into the | 62 // calling AsyncReadComplete with the number of bytes read into the |
(...skipping 16 matching lines...) Expand all Loading... |
79 // Handles the first message sent over the pipe which contains setup info. | 79 // Handles the first message sent over the pipe which contains setup info. |
80 virtual void HandleHelloMessage(const Message& msg) = 0; | 80 virtual void HandleHelloMessage(const Message& msg) = 0; |
81 | 81 |
82 private: | 82 private: |
83 // Takes the given data received from the IPC channel and dispatches any | 83 // Takes the given data received from the IPC channel and dispatches any |
84 // fully completed messages. | 84 // fully completed messages. |
85 // | 85 // |
86 // Returns true on success. False means channel error. | 86 // Returns true on success. False means channel error. |
87 bool DispatchInputData(const char* input_data, int input_data_len); | 87 bool DispatchInputData(const char* input_data, int input_data_len); |
88 | 88 |
89 Channel::Listener* listener_; | 89 Listener* listener_; |
90 | 90 |
91 // We read from the pipe into this buffer. Managed by DispatchInputData, do | 91 // We read from the pipe into this buffer. Managed by DispatchInputData, do |
92 // not access directly outside that function. | 92 // not access directly outside that function. |
93 char input_buf_[Channel::kReadBufferSize]; | 93 char input_buf_[Channel::kReadBufferSize]; |
94 | 94 |
95 // Large messages that span multiple pipe buffers, get built-up using | 95 // Large messages that span multiple pipe buffers, get built-up using |
96 // this buffer. | 96 // this buffer. |
97 std::string input_overflow_buf_; | 97 std::string input_overflow_buf_; |
98 | 98 |
99 DISALLOW_COPY_AND_ASSIGN(ChannelReader); | 99 DISALLOW_COPY_AND_ASSIGN(ChannelReader); |
100 }; | 100 }; |
101 | 101 |
102 } // namespace internal | 102 } // namespace internal |
103 } // namespace IPC | 103 } // namespace IPC |
104 | 104 |
105 #endif // IPC_IPC_CHANNEL_READER_H_ | 105 #endif // IPC_IPC_CHANNEL_READER_H_ |
OLD | NEW |