OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_SYNC_SENDER_H__ | 5 #ifndef IPC_IPC_SYNC_SENDER_H__ |
6 #define IPC_IPC_SYNC_SENDER_H__ | 6 #define IPC_IPC_SYNC_SENDER_H__ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <deque> | 9 #include <deque> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/lock.h" | 12 #include "base/lock.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "base/waitable_event_watcher.h" | 14 #include "base/waitable_event_watcher.h" |
15 #include "ipc/ipc_channel_proxy.h" | 15 #include "ipc/ipc_channel_proxy.h" |
| 16 #include "ipc/ipc_sync_message.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 class WaitableEvent; | 19 class WaitableEvent; |
19 }; | 20 }; |
20 | 21 |
21 namespace IPC { | 22 namespace IPC { |
22 | 23 |
23 class SyncMessage; | 24 class SyncMessage; |
24 class MessageReplyDeserializer; | 25 class MessageReplyDeserializer; |
25 | 26 |
26 // This is similar to IPC::ChannelProxy, with the added feature of supporting | 27 // This is similar to ChannelProxy, with the added feature of supporting sending |
27 // sending synchronous messages. | 28 // synchronous messages. |
28 // Note that care must be taken that the lifetime of the ipc_thread argument | 29 // Note that care must be taken that the lifetime of the ipc_thread argument |
29 // is more than this object. If the message loop goes away while this object | 30 // is more than this object. If the message loop goes away while this object |
30 // is running and it's used to send a message, then it will use the invalid | 31 // is running and it's used to send a message, then it will use the invalid |
31 // message loop pointer to proxy it to the ipc thread. | 32 // message loop pointer to proxy it to the ipc thread. |
32 class SyncChannel : public ChannelProxy, | 33 class SyncChannel : public ChannelProxy, |
33 public base::WaitableEventWatcher::Delegate { | 34 public base::WaitableEventWatcher::Delegate { |
34 public: | 35 public: |
35 SyncChannel(const std::string& channel_id, Channel::Mode mode, | 36 SyncChannel(const std::string& channel_id, Channel::Mode mode, |
36 Channel::Listener* listener, MessageFilter* filter, | 37 Channel::Listener* listener, MessageFilter* filter, |
37 MessageLoop* ipc_message_loop, bool create_pipe_now, | 38 MessageLoop* ipc_message_loop, bool create_pipe_now, |
(...skipping 18 matching lines...) Expand all Loading... |
56 class SyncContext : public Context, | 57 class SyncContext : public Context, |
57 public base::WaitableEventWatcher::Delegate { | 58 public base::WaitableEventWatcher::Delegate { |
58 public: | 59 public: |
59 SyncContext(Channel::Listener* listener, | 60 SyncContext(Channel::Listener* listener, |
60 MessageFilter* filter, | 61 MessageFilter* filter, |
61 MessageLoop* ipc_thread, | 62 MessageLoop* ipc_thread, |
62 base::WaitableEvent* shutdown_event); | 63 base::WaitableEvent* shutdown_event); |
63 | 64 |
64 // Adds information about an outgoing sync message to the context so that | 65 // Adds information about an outgoing sync message to the context so that |
65 // we know how to deserialize the reply. | 66 // we know how to deserialize the reply. |
66 void Push(IPC::SyncMessage* sync_msg); | 67 void Push(SyncMessage* sync_msg); |
67 | 68 |
68 // Cleanly remove the top deserializer (and throw it away). Returns the | 69 // Cleanly remove the top deserializer (and throw it away). Returns the |
69 // result of the Send call for that message. | 70 // result of the Send call for that message. |
70 bool Pop(); | 71 bool Pop(); |
71 | 72 |
72 // Returns an event that's set when the send is complete, timed out or the | 73 // Returns an event that's set when the send is complete, timed out or the |
73 // process shut down. | 74 // process shut down. |
74 base::WaitableEvent* GetSendDoneEvent(); | 75 base::WaitableEvent* GetSendDoneEvent(); |
75 | 76 |
76 // Returns an event that's set when an incoming message that's not the reply | 77 // Returns an event that's set when an incoming message that's not the reply |
(...skipping 12 matching lines...) Expand all Loading... |
89 void OnSendTimeout(int message_id); | 90 void OnSendTimeout(int message_id); |
90 | 91 |
91 base::WaitableEvent* shutdown_event() { return shutdown_event_; } | 92 base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
92 | 93 |
93 ReceivedSyncMsgQueue* received_sync_msgs() { | 94 ReceivedSyncMsgQueue* received_sync_msgs() { |
94 return received_sync_msgs_; | 95 return received_sync_msgs_; |
95 } | 96 } |
96 | 97 |
97 private: | 98 private: |
98 ~SyncContext(); | 99 ~SyncContext(); |
99 // IPC::ChannelProxy methods that we override. | 100 // ChannelProxy methods that we override. |
100 | 101 |
101 // Called on the listener thread. | 102 // Called on the listener thread. |
102 virtual void Clear(); | 103 virtual void Clear(); |
103 | 104 |
104 // Called on the IPC thread. | 105 // Called on the IPC thread. |
105 virtual void OnMessageReceived(const Message& msg); | 106 virtual void OnMessageReceived(const Message& msg); |
106 virtual void OnChannelError(); | 107 virtual void OnChannelError(); |
107 virtual void OnChannelOpened(); | 108 virtual void OnChannelOpened(); |
108 virtual void OnChannelClosed(); | 109 virtual void OnChannelClosed(); |
109 | 110 |
110 // Cancels all pending Send calls. | 111 // Cancels all pending Send calls. |
111 void CancelPendingSends(); | 112 void CancelPendingSends(); |
112 | 113 |
113 // WaitableEventWatcher::Delegate implementation. | 114 // WaitableEventWatcher::Delegate implementation. |
114 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); | 115 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
115 | 116 |
116 // When sending a synchronous message, this structure contains an object | |
117 // that knows how to deserialize the response. | |
118 struct PendingSyncMsg { | |
119 PendingSyncMsg(int id, IPC::MessageReplyDeserializer* d, | |
120 base::WaitableEvent* e) : | |
121 id(id), deserializer(d), done_event(e), send_result(false) { } | |
122 int id; | |
123 IPC::MessageReplyDeserializer* deserializer; | |
124 base::WaitableEvent* done_event; | |
125 bool send_result; | |
126 }; | |
127 | |
128 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; | 117 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
129 PendingSyncMessageQueue deserializers_; | 118 PendingSyncMessageQueue deserializers_; |
130 Lock deserializers_lock_; | 119 Lock deserializers_lock_; |
131 | 120 |
132 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; | 121 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
133 | 122 |
134 base::WaitableEvent* shutdown_event_; | 123 base::WaitableEvent* shutdown_event_; |
135 base::WaitableEventWatcher shutdown_watcher_; | 124 base::WaitableEventWatcher shutdown_watcher_; |
136 }; | 125 }; |
137 | 126 |
(...skipping 18 matching lines...) Expand all Loading... |
156 | 145 |
157 // Used to signal events between the IPC and listener threads. | 146 // Used to signal events between the IPC and listener threads. |
158 base::WaitableEventWatcher dispatch_watcher_; | 147 base::WaitableEventWatcher dispatch_watcher_; |
159 | 148 |
160 DISALLOW_EVIL_CONSTRUCTORS(SyncChannel); | 149 DISALLOW_EVIL_CONSTRUCTORS(SyncChannel); |
161 }; | 150 }; |
162 | 151 |
163 } // namespace IPC | 152 } // namespace IPC |
164 | 153 |
165 #endif // IPC_IPC_SYNC_SENDER_H__ | 154 #endif // IPC_IPC_SYNC_SENDER_H__ |
OLD | NEW |