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. |
| 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 // As an optimization, after a message is translated, the message is |
| 126 // immediately dispatched if able. This prevents an otherwise unnecessary deep |
| 127 // copy of the message which is needed to store the message in the message |
| 128 // queue. |
| 129 bool TranslateInputData(const char* input_data, int input_data_len); |
| 130 |
| 131 // Dispatches messages from queued_messages_ to listeners. Successfully |
| 132 // dispatched messages are removed from queued_messages_. |
| 133 DispatchState DispatchMessages(); |
| 134 |
| 135 // Attempts to fill in the brokerable attachments of |msg| with information |
| 136 // from the Attachment Broker. |
| 137 // Returns the set of ids that are still waiting to be brokered. |
| 138 AttachmentIdSet GetBrokeredAttachments(Message* msg); |
| 139 |
| 140 // AttachmentBroker::Observer overrides. |
| 141 void ReceivedBrokerableAttachmentWithId( |
| 142 const BrokerableAttachment::AttachmentId& id) override; |
| 143 |
| 144 // This class should observe the attachment broker if and only if blocked_ids_ |
| 145 // is not empty. |
| 146 void StartObservingAttachmentBroker(); |
| 147 void StopObservingAttachmentBroker(); |
94 | 148 |
95 Listener* listener_; | 149 Listener* listener_; |
96 | 150 |
97 // We read from the pipe into this buffer. Managed by DispatchInputData, do | 151 // We read from the pipe into this buffer. Managed by DispatchInputData, do |
98 // not access directly outside that function. | 152 // not access directly outside that function. |
99 char input_buf_[Channel::kReadBufferSize]; | 153 char input_buf_[Channel::kReadBufferSize]; |
100 | 154 |
101 // Large messages that span multiple pipe buffers, get built-up using | 155 // Large messages that span multiple pipe buffers, get built-up using |
102 // this buffer. | 156 // this buffer. |
103 std::string input_overflow_buf_; | 157 std::string input_overflow_buf_; |
104 | 158 |
| 159 // These messages are waiting to be dispatched. If this vector is non-empty, |
| 160 // then the front Message must be blocked on receiving an attachment from the |
| 161 // AttachmentBroker. |
| 162 ScopedVector<Message> queued_messages_; |
| 163 |
| 164 // If the next message to be processed is blocked by the broker, then this |
| 165 // set contains the AttachmentIds that are needed to unblock the message. |
| 166 AttachmentIdSet blocked_ids_; |
| 167 |
105 DISALLOW_COPY_AND_ASSIGN(ChannelReader); | 168 DISALLOW_COPY_AND_ASSIGN(ChannelReader); |
106 }; | 169 }; |
107 | 170 |
108 } // namespace internal | 171 } // namespace internal |
109 } // namespace IPC | 172 } // namespace IPC |
110 | 173 |
111 #endif // IPC_IPC_CHANNEL_READER_H_ | 174 #endif // IPC_IPC_CHANNEL_READER_H_ |
OLD | NEW |