OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_SYNC_MESSAGE_FILTER_H_ | 5 #ifndef IPC_IPC_SYNC_MESSAGE_FILTER_H_ |
6 #define IPC_IPC_SYNC_MESSAGE_FILTER_H_ | 6 #define IPC_IPC_SYNC_MESSAGE_FILTER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "ipc/ipc_sender.h" | 14 #include "ipc/ipc_sender.h" |
15 #include "ipc/ipc_sync_message.h" | 15 #include "ipc/ipc_sync_message.h" |
16 #include "ipc/message_filter.h" | 16 #include "ipc/message_filter.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
20 class WaitableEvent; | 20 class WaitableEvent; |
21 } | 21 } |
22 | 22 |
23 namespace IPC { | 23 namespace IPC { |
| 24 class SyncChannel; |
24 | 25 |
25 // This MessageFilter allows sending synchronous IPC messages from a thread | 26 // This MessageFilter allows sending synchronous IPC messages from a thread |
26 // other than the listener thread associated with the SyncChannel. It does not | 27 // other than the listener thread associated with the SyncChannel. It does not |
27 // support fancy features that SyncChannel does, such as handling recursion or | 28 // support fancy features that SyncChannel does, such as handling recursion or |
28 // receiving messages while waiting for a response. Note that this object can | 29 // receiving messages while waiting for a response. Note that this object can |
29 // be used to send simultaneous synchronous messages from different threads. | 30 // be used to send simultaneous synchronous messages from different threads. |
30 class IPC_EXPORT SyncMessageFilter : public MessageFilter, public Sender { | 31 class IPC_EXPORT SyncMessageFilter : public MessageFilter, public Sender { |
31 public: | 32 public: |
32 explicit SyncMessageFilter(base::WaitableEvent* shutdown_event); | |
33 | |
34 // MessageSender implementation. | 33 // MessageSender implementation. |
35 bool Send(Message* message) override; | 34 bool Send(Message* message) override; |
36 | 35 |
37 // MessageFilter implementation. | 36 // MessageFilter implementation. |
38 void OnFilterAdded(Sender* sender) override; | 37 void OnFilterAdded(Sender* sender) override; |
39 void OnChannelError() override; | 38 void OnChannelError() override; |
40 void OnChannelClosing() override; | 39 void OnChannelClosing() override; |
41 bool OnMessageReceived(const Message& message) override; | 40 bool OnMessageReceived(const Message& message) override; |
42 | 41 |
43 protected: | 42 protected: |
| 43 SyncMessageFilter(base::WaitableEvent* shutdown_event, |
| 44 bool is_channel_send_thread_safe); |
| 45 |
44 ~SyncMessageFilter() override; | 46 ~SyncMessageFilter() override; |
45 | 47 |
46 private: | 48 private: |
| 49 friend class SyncChannel; |
| 50 |
47 void SendOnIOThread(Message* message); | 51 void SendOnIOThread(Message* message); |
48 // Signal all the pending sends as done, used in an error condition. | 52 // Signal all the pending sends as done, used in an error condition. |
49 void SignalAllEvents(); | 53 void SignalAllEvents(); |
50 | 54 |
51 // The channel to which this filter was added. | 55 // The channel to which this filter was added. |
52 Sender* sender_; | 56 Sender* sender_; |
53 | 57 |
| 58 // Indicates if |sender_|'s Send method is thread-safe. |
| 59 bool is_channel_send_thread_safe_; |
| 60 |
54 // The process's main thread. | 61 // The process's main thread. |
55 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; | 62 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
56 | 63 |
57 // The message loop where the Channel lives. | 64 // The message loop where the Channel lives. |
58 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 65 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
59 | 66 |
60 typedef std::set<PendingSyncMsg*> PendingSyncMessages; | 67 typedef std::set<PendingSyncMsg*> PendingSyncMessages; |
61 PendingSyncMessages pending_sync_messages_; | 68 PendingSyncMessages pending_sync_messages_; |
62 | 69 |
63 // Messages waiting to be delivered after IO initialization. | 70 // Messages waiting to be delivered after IO initialization. |
64 ScopedVector<Message> pending_messages_; | 71 ScopedVector<Message> pending_messages_; |
65 | 72 |
66 // Locks data members above. | 73 // Locks data members above. |
67 base::Lock lock_; | 74 base::Lock lock_; |
68 | 75 |
69 base::WaitableEvent* shutdown_event_; | 76 base::WaitableEvent* shutdown_event_; |
70 | 77 |
71 DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter); | 78 DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter); |
72 }; | 79 }; |
73 | 80 |
74 } // namespace IPC | 81 } // namespace IPC |
75 | 82 |
76 #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_ | 83 #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_ |
OLD | NEW |