1 // Copyright (c) 2012 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.
5 #ifndef IPC_IPC_SYNC_MESSAGE_FILTER_H_
6 #define IPC_IPC_SYNC_MESSAGE_FILTER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/synchronization/lock.h"
14 #include "ipc/ipc_sender.h"
15 #include "ipc/ipc_sync_message.h"
16 #include "ipc/message_filter.h"
19 class SingleThreadTaskRunner
;
26 // This MessageFilter allows sending synchronous IPC messages from a thread
27 // other than the listener thread associated with the SyncChannel. It does not
28 // support fancy features that SyncChannel does, such as handling recursion or
29 // receiving messages while waiting for a response. Note that this object can
30 // be used to send simultaneous synchronous messages from different threads.
31 class IPC_EXPORT SyncMessageFilter
: public MessageFilter
, public Sender
{
33 // MessageSender implementation.
34 bool Send(Message
* message
) override
;
36 // MessageFilter implementation.
37 void OnFilterAdded(Sender
* sender
) override
;
38 void OnChannelError() override
;
39 void OnChannelClosing() override
;
40 bool OnMessageReceived(const Message
& message
) override
;
43 SyncMessageFilter(base::WaitableEvent
* shutdown_event
,
44 bool is_channel_send_thread_safe
);
46 ~SyncMessageFilter() override
;
49 friend class SyncChannel
;
51 void set_is_channel_send_thread_safe(bool is_channel_send_thread_safe
) {
52 is_channel_send_thread_safe_
= is_channel_send_thread_safe
;
55 void SendOnIOThread(Message
* message
);
56 // Signal all the pending sends as done, used in an error condition.
57 void SignalAllEvents();
59 // The channel to which this filter was added.
62 // Indicates if |sender_|'s Send method is thread-safe.
63 bool is_channel_send_thread_safe_
;
65 // The process's main thread.
66 scoped_refptr
<base::SingleThreadTaskRunner
> listener_task_runner_
;
68 // The message loop where the Channel lives.
69 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
71 typedef std::set
<PendingSyncMsg
*> PendingSyncMessages
;
72 PendingSyncMessages pending_sync_messages_
;
74 // Messages waiting to be delivered after IO initialization.
75 ScopedVector
<Message
> pending_messages_
;
77 // Locks data members above.
80 base::WaitableEvent
* shutdown_event_
;
82 DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter
);
87 #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_