[email protected] | 5a3b914 | 2009-08-28 21:03:17 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | 5a3b914 | 2009-08-28 21:03:17 | [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); |
[email protected] | 17acbb5 | 2009-08-28 17:29:03 | [diff] [blame] | 39 | virtual ~SyncChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | // Adds information about an outgoing sync message to the context so that |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 65 | // we know how to deserialize the reply. |
| 66 | void Push(IPC::SyncMessage* sync_msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 68 | // Cleanly remove the top deserializer (and throw it away). Returns the |
| 69 | // result of the Send call for that message. |
| 70 | bool Pop(); |
| 71 | |
| 72 | // Returns an event that's set when the send is complete, timed out or the |
| 73 | // process shut down. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 74 | base::WaitableEvent* GetSendDoneEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
| 76 | // Returns an event that's set when an incoming message that's not the reply |
| 77 | // needs to get dispatched (by calling SyncContext::DispatchMessages). |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 78 | base::WaitableEvent* GetDispatchEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | |
| 80 | void DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | |
| 82 | // Checks if the given message is blocking the listener thread because of a |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 83 | // synchronous send. If it is, the thread is unblocked and true is |
| 84 | // returned. Otherwise the function returns false. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 85 | bool TryToUnblockListener(const Message* msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 87 | // Called on the IPC thread when a sync send that runs a nested message loop |
| 88 | // times out. |
| 89 | void OnSendTimeout(int message_id); |
| 90 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 91 | base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 92 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 93 | ReceivedSyncMsgQueue* received_sync_msgs() { |
| 94 | return received_sync_msgs_; |
| 95 | } |
| 96 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 98 | ~SyncContext(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 99 | // IPC::ChannelProxy methods that we override. |
| 100 | |
| 101 | // Called on the listener thread. |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 102 | virtual void Clear(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 103 | |
| 104 | // Called on the IPC thread. |
| 105 | virtual void OnMessageReceived(const Message& msg); |
| 106 | virtual void OnChannelError(); |
| 107 | virtual void OnChannelOpened(); |
| 108 | virtual void OnChannelClosed(); |
| 109 | |
| 110 | // Cancels all pending Send calls. |
| 111 | void CancelPendingSends(); |
| 112 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 113 | // WaitableEventWatcher::Delegate implementation. |
| 114 | virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 116 | // When sending a synchronous message, this structure contains an object |
| 117 | // that knows how to deserialize the response. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | struct PendingSyncMsg { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 119 | PendingSyncMsg(int id, IPC::MessageReplyDeserializer* d, |
| 120 | base::WaitableEvent* e) : |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 121 | id(id), deserializer(d), done_event(e), send_result(false) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | int id; |
| 123 | IPC::MessageReplyDeserializer* deserializer; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 124 | base::WaitableEvent* done_event; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 125 | bool send_result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | }; |
| 127 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 128 | typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | PendingSyncMessageQueue deserializers_; |
| 130 | Lock deserializers_lock_; |
| 131 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 132 | scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 134 | base::WaitableEvent* shutdown_event_; |
| 135 | base::WaitableEventWatcher shutdown_watcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | private: |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 139 | // WaitableEventWatcher::Delegate implementation. |
| 140 | virtual void OnWaitableEventSignaled(base::WaitableEvent* arg); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 141 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 142 | SyncContext* sync_context() { |
| 143 | return reinterpret_cast<SyncContext*>(context()); |
| 144 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 146 | // Both these functions wait for a reply, timeout or process shutdown. The |
| 147 | // latter one also runs a nested message loop in the meantime. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame^] | 148 | static void WaitForReply( |
| 149 | SyncContext* context, base::WaitableEvent* pump_messages_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 151 | // Runs a nested message loop until a reply arrives, times out, or the process |
| 152 | // shuts down. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame^] | 153 | static void WaitForReplyWithNestedMessageLoop(SyncContext* context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 155 | bool sync_messages_with_no_timeout_allowed_; |
| 156 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 157 | // Used to signal events between the IPC and listener threads. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 158 | base::WaitableEventWatcher dispatch_watcher_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 159 | |
[email protected] | 5a3b914 | 2009-08-28 21:03:17 | [diff] [blame] | 160 | DISALLOW_EVIL_CONSTRUCTORS(SyncChannel); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } // namespace IPC |
| 164 | |
[email protected] | 5a3b914 | 2009-08-28 21:03:17 | [diff] [blame] | 165 | #endif // IPC_IPC_SYNC_SENDER_H__ |