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