license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame^] | 5 | #ifndef IPC_IPC_SYNC_SENDER_H__ |
| 6 | #define IPC_IPC_SYNC_SENDER_H__ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <string> |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 9 | #include <deque> |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 10 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/basictypes.h" |
| 12 | #include "base/lock.h" |
| 13 | #include "base/ref_counted.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 14 | #include "base/waitable_event_watcher.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame^] | 15 | #include "ipc/ipc_channel_proxy.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 17 | namespace base { |
| 18 | class WaitableEvent; |
| 19 | }; |
| 20 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | namespace IPC { |
| 22 | |
| 23 | class SyncMessage; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 24 | class MessageReplyDeserializer; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | |
| 26 | // This is similar to IPC::ChannelProxy, with the added feature of supporting |
| 27 | // sending synchronous messages. |
| 28 | // 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 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. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 32 | class SyncChannel : public ChannelProxy, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 33 | public base::WaitableEventWatcher::Delegate { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | public: |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 35 | SyncChannel(const std::string& channel_id, Channel::Mode mode, |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 36 | Channel::Listener* listener, MessageFilter* filter, |
| 37 | MessageLoop* ipc_message_loop, bool create_pipe_now, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 38 | base::WaitableEvent* shutdown_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | ~SyncChannel(); |
| 40 | |
| 41 | virtual bool Send(Message* message); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 42 | virtual bool SendWithTimeout(Message* message, int timeout_ms); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 44 | // Whether we allow sending messages with no time-out. |
| 45 | void set_sync_messages_with_no_timeout_allowed(bool value) { |
| 46 | sync_messages_with_no_timeout_allowed_ = value; |
| 47 | } |
| 48 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | protected: |
| 50 | class ReceivedSyncMsgQueue; |
| 51 | friend class ReceivedSyncMsgQueue; |
| 52 | |
| 53 | // SyncContext holds the per object data for SyncChannel, so that SyncChannel |
| 54 | // can be deleted while it's being used in a different thread. See |
| 55 | // ChannelProxy::Context for more information. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 56 | class SyncContext : public Context, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 57 | public base::WaitableEventWatcher::Delegate { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | public: |
| 59 | SyncContext(Channel::Listener* listener, |
| 60 | MessageFilter* filter, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 61 | MessageLoop* ipc_thread, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 62 | base::WaitableEvent* shutdown_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
| 64 | ~SyncContext(); |
| 65 | |
| 66 | // Adds information about an outgoing sync message to the context so that |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 67 | // we know how to deserialize the reply. |
| 68 | void Push(IPC::SyncMessage* sync_msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 70 | // Cleanly remove the top deserializer (and throw it away). Returns the |
| 71 | // result of the Send call for that message. |
| 72 | bool Pop(); |
| 73 | |
| 74 | // Returns an event that's set when the send is complete, timed out or the |
| 75 | // process shut down. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 76 | base::WaitableEvent* GetSendDoneEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | |
| 78 | // Returns an event that's set when an incoming message that's not the reply |
| 79 | // needs to get dispatched (by calling SyncContext::DispatchMessages). |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 80 | base::WaitableEvent* GetDispatchEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | |
| 82 | void DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
| 84 | // Checks if the given message is blocking the listener thread because of a |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 85 | // synchronous send. If it is, the thread is unblocked and true is |
| 86 | // returned. Otherwise the function returns false. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 87 | bool TryToUnblockListener(const Message* msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 89 | // Called on the IPC thread when a sync send that runs a nested message loop |
| 90 | // times out. |
| 91 | void OnSendTimeout(int message_id); |
| 92 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 93 | base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 94 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | private: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 96 | // IPC::ChannelProxy methods that we override. |
| 97 | |
| 98 | // Called on the listener thread. |
| 99 | virtual void Clear(); |
| 100 | |
| 101 | // Called on the IPC thread. |
| 102 | virtual void OnMessageReceived(const Message& msg); |
| 103 | virtual void OnChannelError(); |
| 104 | virtual void OnChannelOpened(); |
| 105 | virtual void OnChannelClosed(); |
| 106 | |
| 107 | // Cancels all pending Send calls. |
| 108 | void CancelPendingSends(); |
| 109 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 110 | // WaitableEventWatcher::Delegate implementation. |
| 111 | virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 113 | // When sending a synchronous message, this structure contains an object |
| 114 | // that knows how to deserialize the response. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | struct PendingSyncMsg { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 116 | PendingSyncMsg(int id, IPC::MessageReplyDeserializer* d, |
| 117 | base::WaitableEvent* e) : |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 118 | id(id), deserializer(d), done_event(e), send_result(false) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | int id; |
| 120 | IPC::MessageReplyDeserializer* deserializer; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 121 | base::WaitableEvent* done_event; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 122 | bool send_result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | }; |
| 124 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 125 | typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | PendingSyncMessageQueue deserializers_; |
| 127 | Lock deserializers_lock_; |
| 128 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 129 | scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 131 | base::WaitableEvent* shutdown_event_; |
| 132 | base::WaitableEventWatcher shutdown_watcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | private: |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 136 | // WaitableEventWatcher::Delegate implementation. |
| 137 | virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 138 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 139 | SyncContext* sync_context() { |
| 140 | return reinterpret_cast<SyncContext*>(context()); |
| 141 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 143 | // Both these functions wait for a reply, timeout or process shutdown. The |
| 144 | // latter one also runs a nested message loop in the meantime. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 145 | void WaitForReply(base::WaitableEvent* pump_messages_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 147 | // Runs a nested message loop until a reply arrives, times out, or the process |
| 148 | // shuts down. |
| 149 | void WaitForReplyWithNestedMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 151 | bool sync_messages_with_no_timeout_allowed_; |
| 152 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 153 | // Used to signal events between the IPC and listener threads. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 154 | base::WaitableEventWatcher send_done_watcher_; |
| 155 | base::WaitableEventWatcher dispatch_watcher_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 156 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | DISALLOW_EVIL_CONSTRUCTORS(SyncChannel); |
| 158 | }; |
| 159 | |
| 160 | } // namespace IPC |
| 161 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame^] | 162 | #endif // IPC_IPC_SYNC_SENDER_H__ |