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 <set> | |
9 | |
8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/scoped_vector.h" | |
9 #include "ipc/attachment_broker.h" | 13 #include "ipc/attachment_broker.h" |
14 #include "ipc/brokerable_attachment.h" | |
10 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
11 #include "ipc/ipc_export.h" | 16 #include "ipc/ipc_export.h" |
12 | 17 |
13 namespace IPC { | 18 namespace IPC { |
14 namespace internal { | 19 namespace internal { |
15 | 20 |
16 // This class provides common pipe reading functionality for the | 21 // This class provides common pipe reading functionality for the |
17 // platform-specific IPC channel implementations. | 22 // platform-specific IPC channel implementations. |
18 // | 23 // |
19 // It does the common input buffer management and message dispatch, while the | 24 // It does the common input buffer management and message dispatch, while the |
20 // platform-specific parts provide the pipe management through a virtual | 25 // platform-specific parts provide the pipe management through a virtual |
21 // interface implemented on a per-platform basis. | 26 // interface implemented on a per-platform basis. |
22 // | 27 // |
23 // Note that there is no "writer" corresponding to this because the code for | 28 // Note that there is no "writer" corresponding to this because the code for |
24 // writing to the channel is much simpler and has very little common | 29 // writing to the channel is much simpler and has very little common |
25 // functionality that would benefit from being factored out. If we add | 30 // functionality that would benefit from being factored out. If we add |
26 // something like that in the future, it would be more appropriate to add it | 31 // something like that in the future, it would be more appropriate to add it |
27 // here (and rename appropriately) rather than writing a different class. | 32 // here (and rename appropriately) rather than writing a different class. |
28 class ChannelReader : public SupportsAttachmentBrokering { | 33 class IPC_EXPORT ChannelReader : public SupportsAttachmentBrokering, |
34 public AttachmentBroker::Observer { | |
29 public: | 35 public: |
30 explicit ChannelReader(Listener* listener); | 36 explicit ChannelReader(Listener* listener); |
31 virtual ~ChannelReader(); | 37 virtual ~ChannelReader(); |
32 | 38 |
33 void set_listener(Listener* listener) { listener_ = listener; } | 39 void set_listener(Listener* listener) { listener_ = listener; } |
34 | 40 |
41 // This type is returned by ProcessIncomingMessages to indicate the effect of | |
42 // the method. | |
43 enum DispatchState { | |
44 // All messages were successfully dispatched, or there were no messages to | |
45 // dispatch. | |
46 DISPATCH_FINISHED, | |
47 // There was a channel error. | |
48 DISPATCH_ERROR, | |
49 // Dispatching messages is blocked on receiving more information from the | |
50 // broker. | |
51 DISPATCH_WAITING_ON_BROKER, | |
52 }; | |
53 | |
35 // Call to process messages received from the IPC connection and dispatch | 54 // Call to process messages received from the IPC connection and dispatch |
36 // them. Returns false on channel error. True indicates that everything | 55 // them. |
37 // succeeded, although there may not have been any messages processed. | 56 DispatchState ProcessIncomingMessages(); |
38 bool ProcessIncomingMessages(); | |
39 | 57 |
40 // Handles asynchronously read data. | 58 // Handles asynchronously read data. |
41 // | 59 // |
42 // Optionally call this after returning READ_PENDING from ReadData to | 60 // Optionally call this after returning READ_PENDING from ReadData to |
43 // indicate that buffer was filled with the given number of bytes of | 61 // indicate that buffer was filled with the given number of bytes of |
44 // data. See ReadData for more. | 62 // data. See ReadData for more. |
45 bool AsyncReadComplete(int bytes_read); | 63 DispatchState AsyncReadComplete(int bytes_read); |
46 | 64 |
47 // Returns true if the given message is internal to the IPC implementation, | 65 // Returns true if the given message is internal to the IPC implementation, |
48 // like the "hello" message sent on channel set-up. | 66 // like the "hello" message sent on channel set-up. |
49 bool IsInternalMessage(const Message& m); | 67 bool IsInternalMessage(const Message& m); |
50 | 68 |
51 // Returns true if the given message is an Hello message | 69 // Returns true if the given message is an Hello message |
52 // sent on channel set-up. | 70 // sent on channel set-up. |
53 bool IsHelloMessage(const Message& m); | 71 bool IsHelloMessage(const Message& m); |
54 | 72 |
55 protected: | 73 protected: |
(...skipping 13 matching lines...) Expand all Loading... | |
69 // buffer, or ProcessIncomingMessages to try the read again (depending | 87 // buffer, or ProcessIncomingMessages to try the read again (depending |
70 // on whether the platform's async I/O is "try again" or "write | 88 // on whether the platform's async I/O is "try again" or "write |
71 // asynchronously into your buffer"). | 89 // asynchronously into your buffer"). |
72 virtual ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) = 0; | 90 virtual ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) = 0; |
73 | 91 |
74 // Loads the required file desciptors into the given message. Returns true | 92 // Loads the required file desciptors into the given message. Returns true |
75 // on success. False means a fatal channel error. | 93 // on success. False means a fatal channel error. |
76 // | 94 // |
77 // This will read from the input_fds_ and read more handles from the FD | 95 // This will read from the input_fds_ and read more handles from the FD |
78 // pipe if necessary. | 96 // pipe if necessary. |
79 virtual bool WillDispatchInputMessage(Message* msg) = 0; | 97 virtual bool ShouldDispatchInputMessage(Message* msg) = 0; |
98 | |
99 // Overridden by subclasses to get attachments that are sent alongside the IPC | |
100 // channel (as opposed to through a broker). | |
101 // Returns true on success. False means a fatal channel error. | |
102 virtual bool GetNonBrokeredAttachments(Message* msg) = 0; | |
80 | 103 |
81 // Performs post-dispatch checks. Called when all input buffers are empty, | 104 // Performs post-dispatch checks. Called when all input buffers are empty, |
82 // though there could be more data ready to be read from the OS. | 105 // though there could be more data ready to be read from the OS. |
83 virtual bool DidEmptyInputBuffers() = 0; | 106 virtual bool DidEmptyInputBuffers() = 0; |
84 | 107 |
85 // Handles internal messages, like the hello message sent on channel startup. | 108 // Handles internal messages, like the hello message sent on channel startup. |
86 virtual void HandleInternalMessage(const Message& msg) = 0; | 109 virtual void HandleInternalMessage(const Message& msg) = 0; |
87 | 110 |
111 // Exposed for testing purposes only. | |
Tom Sepez
2015/07/13 18:28:47
nit: maybe these go into a subclass in the test fi
erikchen
2015/07/17 18:44:01
The Google C++ style guide indicates that data mem
| |
112 ScopedVector<Message>* get_queued_messages() { return &queued_messages_; } | |
113 | |
114 // Exposed for testing purposes only. | |
115 virtual void DispatchMessage(Message* m); | |
116 | |
88 private: | 117 private: |
89 // Takes the given data received from the IPC channel and dispatches any | 118 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentAlreadyBrokered); |
90 // fully completed messages. | 119 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentNotYetBrokered); |
91 // | 120 |
92 // Returns true on success. False means channel error. | 121 typedef std::set<BrokerableAttachment::AttachmentId> AttachmentIdSet; |
93 bool DispatchInputData(const char* input_data, int input_data_len); | 122 |
123 // Takes the given data received from the IPC channel, translates it into | |
124 // Messages, and puts them in queued_messages_. | |
125 bool TranslateInputData(const char* input_data, int input_data_len); | |
126 | |
127 // Dispatches messages from queued_messages_ to listeners. Successfully | |
128 // dispatched messages are removed from queued_messages_. | |
129 DispatchState DispatchMessages(); | |
130 | |
131 // Attempts to fill in the brokerable attachments of |msg| with information | |
132 // from the Attachment Broker. | |
133 // Returns the set of ids that are still waiting to be brokered. | |
134 AttachmentIdSet GetBrokeredAttachments(Message* msg); | |
135 | |
136 // AttachmentBroker::Observer overrides. | |
137 void ReceivedBrokerableAttachmentWithId( | |
138 const BrokerableAttachment::AttachmentId& id) override; | |
139 | |
140 // This class should observe the attachment broker if and only if blocked_ids_ | |
141 // is not empty. | |
142 void StartObservingAttachmentBroker(); | |
143 void StopObservingAttachmentBroker(); | |
94 | 144 |
95 Listener* listener_; | 145 Listener* listener_; |
96 | 146 |
97 // We read from the pipe into this buffer. Managed by DispatchInputData, do | 147 // We read from the pipe into this buffer. Managed by DispatchInputData, do |
98 // not access directly outside that function. | 148 // not access directly outside that function. |
99 char input_buf_[Channel::kReadBufferSize]; | 149 char input_buf_[Channel::kReadBufferSize]; |
100 | 150 |
101 // Large messages that span multiple pipe buffers, get built-up using | 151 // Large messages that span multiple pipe buffers, get built-up using |
102 // this buffer. | 152 // this buffer. |
103 std::string input_overflow_buf_; | 153 std::string input_overflow_buf_; |
104 | 154 |
155 // These messages are waiting to be dispatched. If this vector is non-empty, | |
156 // then the front Message must be blocked on receiving an attachment from the | |
157 // AttachmentBroker. | |
158 ScopedVector<Message> queued_messages_; | |
159 | |
160 // If the next message to be processed is blocked by the broker, then this | |
161 // set contains the AttachmentIds that are needed to unblock the message. | |
162 AttachmentIdSet blocked_ids_; | |
163 | |
105 DISALLOW_COPY_AND_ASSIGN(ChannelReader); | 164 DISALLOW_COPY_AND_ASSIGN(ChannelReader); |
106 }; | 165 }; |
107 | 166 |
108 } // namespace internal | 167 } // namespace internal |
109 } // namespace IPC | 168 } // namespace IPC |
110 | 169 |
111 #endif // IPC_IPC_CHANNEL_READER_H_ | 170 #endif // IPC_IPC_CHANNEL_READER_H_ |
OLD | NEW |