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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "ipc/attachment_broker.h" | |
16 #include "ipc/brokerable_attachment.h" | 15 #include "ipc/brokerable_attachment.h" |
17 #include "ipc/ipc_channel.h" | 16 #include "ipc/ipc_channel.h" |
18 #include "ipc/ipc_export.h" | 17 #include "ipc/ipc_export.h" |
19 | 18 |
20 namespace IPC { | 19 namespace IPC { |
21 namespace internal { | 20 namespace internal { |
22 | 21 |
23 // This class provides common pipe reading functionality for the | 22 // This class provides common pipe reading functionality for the |
24 // platform-specific IPC channel implementations. | 23 // platform-specific IPC channel implementations. |
25 // | 24 // |
26 // It does the common input buffer management and message dispatch, while the | 25 // It does the common input buffer management and message dispatch, while the |
27 // platform-specific parts provide the pipe management through a virtual | 26 // platform-specific parts provide the pipe management through a virtual |
28 // interface implemented on a per-platform basis. | 27 // interface implemented on a per-platform basis. |
29 // | 28 // |
30 // Note that there is no "writer" corresponding to this because the code for | 29 // Note that there is no "writer" corresponding to this because the code for |
31 // writing to the channel is much simpler and has very little common | 30 // writing to the channel is much simpler and has very little common |
32 // functionality that would benefit from being factored out. If we add | 31 // functionality that would benefit from being factored out. If we add |
33 // something like that in the future, it would be more appropriate to add it | 32 // something like that in the future, it would be more appropriate to add it |
34 // here (and rename appropriately) rather than writing a different class. | 33 // here (and rename appropriately) rather than writing a different class. |
35 class IPC_EXPORT ChannelReader : public SupportsAttachmentBrokering, | 34 class IPC_EXPORT ChannelReader { |
36 public AttachmentBroker::Observer { | |
37 public: | 35 public: |
38 explicit ChannelReader(Listener* listener); | 36 explicit ChannelReader(Listener* listener); |
39 virtual ~ChannelReader(); | 37 virtual ~ChannelReader(); |
40 | 38 |
41 void set_listener(Listener* listener) { listener_ = listener; } | 39 void set_listener(Listener* listener) { listener_ = listener; } |
42 | 40 |
43 // This type is returned by ProcessIncomingMessages to indicate the effect of | 41 // This type is returned by ProcessIncomingMessages to indicate the effect of |
44 // the method. | 42 // the method. |
45 enum DispatchState { | 43 enum DispatchState { |
46 // All messages were successfully dispatched, or there were no messages to | 44 // All messages were successfully dispatched, or there were no messages to |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 virtual bool GetNonBrokeredAttachments(Message* msg) = 0; | 106 virtual bool GetNonBrokeredAttachments(Message* msg) = 0; |
109 | 107 |
110 // Performs post-dispatch checks. Called when all input buffers are empty, | 108 // Performs post-dispatch checks. Called when all input buffers are empty, |
111 // though there could be more data ready to be read from the OS. | 109 // though there could be more data ready to be read from the OS. |
112 virtual bool DidEmptyInputBuffers() = 0; | 110 virtual bool DidEmptyInputBuffers() = 0; |
113 | 111 |
114 // Handles internal messages, like the hello message sent on channel startup. | 112 // Handles internal messages, like the hello message sent on channel startup. |
115 virtual void HandleInternalMessage(const Message& msg) = 0; | 113 virtual void HandleInternalMessage(const Message& msg) = 0; |
116 | 114 |
117 // Exposed for testing purposes only. | 115 // Exposed for testing purposes only. |
118 ScopedVector<Message>* get_queued_messages() { return &queued_messages_; } | |
119 | |
120 // Exposed for testing purposes only. | |
121 virtual void DispatchMessage(Message* m); | 116 virtual void DispatchMessage(Message* m); |
122 | 117 |
123 // Get the process ID for the sender of the message. | 118 // Get the process ID for the sender of the message. |
124 virtual base::ProcessId GetSenderPID() = 0; | 119 virtual base::ProcessId GetSenderPID() = 0; |
125 | 120 |
126 // Whether the channel is an endpoint of attachment brokering. | |
127 virtual bool IsAttachmentBrokerEndpoint() = 0; | |
128 | |
129 private: | 121 private: |
130 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentAlreadyBrokered); | 122 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentAlreadyBrokered); |
131 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentNotYetBrokered); | 123 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentNotYetBrokered); |
132 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, ResizeOverflowBuffer); | 124 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, ResizeOverflowBuffer); |
133 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, InvalidMessageSize); | 125 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, InvalidMessageSize); |
134 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, TrimBuffer); | 126 FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, TrimBuffer); |
135 | 127 |
136 using AttachmentIdSet = std::set<BrokerableAttachment::AttachmentId>; | |
137 using AttachmentIdVector = std::vector<BrokerableAttachment::AttachmentId>; | |
138 | |
139 // Takes the data received from the IPC channel and translates it into | 128 // Takes the data received from the IPC channel and translates it into |
140 // Messages. Complete messages are passed to HandleTranslatedMessage(). | 129 // Messages. Complete messages are passed to HandleTranslatedMessage(). |
141 // Returns |false| on unrecoverable error. | 130 // Returns |false| on unrecoverable error. |
142 bool TranslateInputData(const char* input_data, int input_data_len); | 131 bool TranslateInputData(const char* input_data, int input_data_len); |
143 | 132 |
144 // Internal messages and messages bound for the attachment broker are | 133 // Internal messages and messages bound for the attachment broker are |
145 // immediately dispatched. Other messages are passed to | 134 // immediately dispatched. Other messages are passed to |
146 // HandleExternalMessage(). | 135 // HandleExternalMessage(). |
147 // Returns |false| on unrecoverable error. | 136 // Returns |false| on unrecoverable error. |
148 bool HandleTranslatedMessage(Message* translated_message, | 137 bool HandleTranslatedMessage(Message* translated_message); |
149 const AttachmentIdVector& attachment_ids); | |
150 | 138 |
151 // Populates the message with brokered and non-brokered attachments. If | 139 // Populates the message with brokered and non-brokered attachments. If |
152 // possible, the message is immediately dispatched. Otherwise, a deep copy of | 140 // possible, the message is immediately dispatched. Otherwise, a deep copy of |
153 // the message is added to |queued_messages_|. |blocked_ids_| are updated if | 141 // the message is added to |queued_messages_|. |blocked_ids_| are updated if |
154 // necessary. | 142 // necessary. |
155 bool HandleExternalMessage(Message* external_message, | 143 bool HandleExternalMessage(Message* external_message); |
156 const AttachmentIdVector& attachment_ids); | |
157 | 144 |
158 // If there was a dispatch error, informs |listener_|. | 145 // If there was a dispatch error, informs |listener_|. |
159 void HandleDispatchError(const Message& message); | 146 void HandleDispatchError(const Message& message); |
160 | 147 |
161 // Attachment broker messages should be dispatched out of band, since there | |
162 // are no ordering restrictions on them, and they may be required to dispatch | |
163 // the messages waiting in |queued_messages_|. | |
164 // Returns true if the attachment broker handled |message|. | |
165 bool DispatchAttachmentBrokerMessage(const Message& message); | |
166 | |
167 // Dispatches messages from queued_messages_ to listeners. Successfully | |
168 // dispatched messages are removed from queued_messages_. | |
169 DispatchState DispatchMessages(); | |
170 | |
171 // Attempts to fill in the brokerable attachments of |msg| with information | |
172 // from the Attachment Broker. | |
173 // Returns the set of ids that are still waiting to be brokered. | |
174 AttachmentIdSet GetBrokeredAttachments(Message* msg); | |
175 | |
176 // AttachmentBroker::Observer overrides. | |
177 void ReceivedBrokerableAttachmentWithId( | |
178 const BrokerableAttachment::AttachmentId& id) override; | |
179 | |
180 // This class should observe the attachment broker if and only if blocked_ids_ | |
181 // is not empty. | |
182 void StartObservingAttachmentBroker(); | |
183 void StopObservingAttachmentBroker(); | |
184 | |
185 // Checks that |size| is a valid message size. Has side effects if it's not. | 148 // Checks that |size| is a valid message size. Has side effects if it's not. |
186 bool CheckMessageSize(size_t size); | 149 bool CheckMessageSize(size_t size); |
187 | 150 |
188 Listener* listener_; | 151 Listener* listener_; |
189 | 152 |
190 // We read from the pipe into this buffer. Managed by DispatchInputData, do | 153 // We read from the pipe into this buffer. Managed by DispatchInputData, do |
191 // not access directly outside that function. | 154 // not access directly outside that function. |
192 char input_buf_[Channel::kReadBufferSize]; | 155 char input_buf_[Channel::kReadBufferSize]; |
193 | 156 |
194 // Large messages that span multiple pipe buffers, get built-up using | 157 // Large messages that span multiple pipe buffers, get built-up using |
195 // this buffer. | 158 // this buffer. |
196 std::string input_overflow_buf_; | 159 std::string input_overflow_buf_; |
197 | 160 |
198 // Maximum overflow buffer size, see Channel::kMaximumReadBufferSize. | 161 // Maximum overflow buffer size, see Channel::kMaximumReadBufferSize. |
199 // This is not a constant because we update it to reflect the reality | 162 // This is not a constant because we update it to reflect the reality |
200 // of std::string::reserve() implementation. | 163 // of std::string::reserve() implementation. |
201 size_t max_input_buffer_size_; | 164 size_t max_input_buffer_size_; |
202 | 165 |
203 // These messages are waiting to be dispatched. If this vector is non-empty, | |
204 // then the front Message must be blocked on receiving an attachment from the | |
205 // AttachmentBroker. | |
206 ScopedVector<Message> queued_messages_; | |
207 | |
208 // If the next message to be processed is blocked by the broker, then this | |
209 // set contains the AttachmentIds that are needed to unblock the message. | |
210 AttachmentIdSet blocked_ids_; | |
211 | |
212 DISALLOW_COPY_AND_ASSIGN(ChannelReader); | 166 DISALLOW_COPY_AND_ASSIGN(ChannelReader); |
213 }; | 167 }; |
214 | 168 |
215 } // namespace internal | 169 } // namespace internal |
216 } // namespace IPC | 170 } // namespace IPC |
217 | 171 |
218 #endif // IPC_IPC_CHANNEL_READER_H_ | 172 #endif // IPC_IPC_CHANNEL_READER_H_ |
OLD | NEW |