rsleevi@chromium.org | 997c1d4 | 2012-04-28 11:12:00 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef IPC_IPC_SYNC_MESSAGE_FILTER_H_ |
| 6 | #define IPC_IPC_SYNC_MESSAGE_FILTER_H_ |
| 7 | |
thestig@chromium.org | 519c9ac | 2011-11-15 09:29:48 +0900 | [diff] [blame] | 8 | #include <set> |
| 9 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 10 | #include "base/basictypes.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 13 | #include "ipc/ipc_sender.h" |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 14 | #include "ipc/ipc_sync_message.h" |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 15 | #include "ipc/message_filter.h" |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 16 | |
thestig@chromium.org | 613caba | 2010-08-06 15:22:15 +0900 | [diff] [blame] | 17 | namespace base { |
sievers@chromium.org | 7ac134c | 2011-09-16 18:38:38 +0900 | [diff] [blame] | 18 | class MessageLoopProxy; |
thestig@chromium.org | 613caba | 2010-08-06 15:22:15 +0900 | [diff] [blame] | 19 | class WaitableEvent; |
| 20 | } |
| 21 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 22 | namespace IPC { |
| 23 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 24 | // This MessageFilter allows sending synchronous IPC messages from a thread |
| 25 | // other than the listener thread associated with the SyncChannel. It does not |
| 26 | // support fancy features that SyncChannel does, such as handling recursion or |
| 27 | // receiving messages while waiting for a response. Note that this object can |
| 28 | // be used to send simultaneous synchronous messages from different threads. |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 29 | class IPC_EXPORT SyncMessageFilter : public MessageFilter, public Sender { |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 30 | public: |
| 31 | explicit SyncMessageFilter(base::WaitableEvent* shutdown_event); |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 32 | |
brettw@chromium.org | f947ed0 | 2012-06-12 07:35:26 +0900 | [diff] [blame] | 33 | // MessageSender implementation. |
avi@chromium.org | 362c8a8 | 2011-11-18 01:09:44 +0900 | [diff] [blame] | 34 | virtual bool Send(Message* message) OVERRIDE; |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 35 | |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 36 | // MessageFilter implementation. |
morrita@chromium.org | 631fb8e | 2014-06-13 15:07:14 +0900 | [diff] [blame] | 37 | virtual void OnFilterAdded(Sender* sender) OVERRIDE; |
avi@chromium.org | 362c8a8 | 2011-11-18 01:09:44 +0900 | [diff] [blame] | 38 | virtual void OnChannelError() OVERRIDE; |
| 39 | virtual void OnChannelClosing() OVERRIDE; |
| 40 | virtual bool OnMessageReceived(const Message& message) OVERRIDE; |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 41 | |
rsleevi@chromium.org | 997c1d4 | 2012-04-28 11:12:00 +0900 | [diff] [blame] | 42 | protected: |
| 43 | virtual ~SyncMessageFilter(); |
| 44 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 45 | private: |
| 46 | void SendOnIOThread(Message* message); |
| 47 | // Signal all the pending sends as done, used in an error condition. |
| 48 | void SignalAllEvents(); |
| 49 | |
| 50 | // The channel to which this filter was added. |
morrita@chromium.org | 631fb8e | 2014-06-13 15:07:14 +0900 | [diff] [blame] | 51 | Sender* sender_; |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 52 | |
sievers@chromium.org | 7ac134c | 2011-09-16 18:38:38 +0900 | [diff] [blame] | 53 | // The process's main thread. |
| 54 | scoped_refptr<base::MessageLoopProxy> listener_loop_; |
| 55 | |
| 56 | // The message loop where the Channel lives. |
| 57 | scoped_refptr<base::MessageLoopProxy> io_loop_; |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 58 | |
dumi@chromium.org | 229db06 | 2010-05-29 16:00:47 +0900 | [diff] [blame] | 59 | typedef std::set<PendingSyncMsg*> PendingSyncMessages; |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 60 | PendingSyncMessages pending_sync_messages_; |
| 61 | |
| 62 | // Locks data members above. |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 63 | base::Lock lock_; |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 64 | |
| 65 | base::WaitableEvent* shutdown_event_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter); |
| 68 | }; |
| 69 | |
| 70 | } // namespace IPC |
| 71 | |
| 72 | #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_ |