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 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
15 #include "base/synchronization/waitable_event_watcher.h" | 15 #include "base/synchronization/waitable_event_watcher.h" |
16 #include "ipc/ipc_sender.h" | 16 #include "ipc/ipc_sender.h" |
17 #include "ipc/ipc_sync_message.h" | 17 #include "ipc/ipc_sync_message.h" |
18 #include "ipc/message_filter.h" | 18 #include "ipc/message_filter.h" |
19 #include "ipc/mojo_event.h" | 19 #include "ipc/mojo_event.h" |
| 20 #include "mojo/public/cpp/bindings/associated_group.h" |
| 21 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 22 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 23 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
20 | 24 |
21 namespace base { | 25 namespace base { |
22 class SingleThreadTaskRunner; | 26 class SingleThreadTaskRunner; |
23 class WaitableEvent; | 27 class WaitableEvent; |
24 } | 28 } |
25 | 29 |
26 namespace IPC { | 30 namespace IPC { |
27 class SyncChannel; | 31 class SyncChannel; |
28 | 32 |
29 // This MessageFilter allows sending synchronous IPC messages from a thread | 33 // This MessageFilter allows sending synchronous IPC messages from a thread |
30 // other than the listener thread associated with the SyncChannel. It does not | 34 // other than the listener thread associated with the SyncChannel. It does not |
31 // support fancy features that SyncChannel does, such as handling recursion or | 35 // support fancy features that SyncChannel does, such as handling recursion or |
32 // receiving messages while waiting for a response. Note that this object can | 36 // receiving messages while waiting for a response. Note that this object can |
33 // be used to send simultaneous synchronous messages from different threads. | 37 // be used to send simultaneous synchronous messages from different threads. |
34 class IPC_EXPORT SyncMessageFilter : public MessageFilter, public Sender { | 38 class IPC_EXPORT SyncMessageFilter : public MessageFilter, public Sender { |
35 public: | 39 public: |
36 // Sender implementation. | 40 // Sender implementation. |
37 bool Send(Message* message) override; | 41 bool Send(Message* message) override; |
38 | 42 |
39 // MessageFilter implementation. | 43 // MessageFilter implementation. |
40 void OnFilterAdded(Channel* channel) override; | 44 void OnFilterAdded(Channel* channel) override; |
41 void OnChannelError() override; | 45 void OnChannelError() override; |
42 void OnChannelClosing() override; | 46 void OnChannelClosing() override; |
43 bool OnMessageReceived(const Message& message) override; | 47 bool OnMessageReceived(const Message& message) override; |
44 | 48 |
| 49 // Binds an associated interface proxy to an interface in the browser process. |
| 50 // Interfaces acquired through this method are associated with the IPC Channel |
| 51 // and as such retain FIFO with legacy IPC messages. |
| 52 // |
| 53 // NOTE: This must ONLY be called on the Channel's thread, after |
| 54 // OnFilterAdded. |
| 55 template <typename Interface> |
| 56 void GetRemoteAssociatedInterface( |
| 57 mojo::AssociatedInterfacePtr<Interface>* proxy) { |
| 58 mojo::AssociatedInterfaceRequest<Interface> request = |
| 59 mojo::GetProxy(proxy, &channel_associated_group_); |
| 60 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); |
| 61 } |
| 62 |
45 protected: | 63 protected: |
46 explicit SyncMessageFilter(base::WaitableEvent* shutdown_event); | 64 explicit SyncMessageFilter(base::WaitableEvent* shutdown_event); |
47 ~SyncMessageFilter() override; | 65 ~SyncMessageFilter() override; |
48 | 66 |
49 private: | 67 private: |
50 class IOMessageLoopObserver; | 68 class IOMessageLoopObserver; |
51 | 69 |
52 friend class SyncChannel; | 70 friend class SyncChannel; |
53 friend class IOMessageLoopObserver; | 71 friend class IOMessageLoopObserver; |
54 | 72 |
55 void SendOnIOThread(Message* message); | 73 void SendOnIOThread(Message* message); |
56 // Signal all the pending sends as done, used in an error condition. | 74 // Signal all the pending sends as done, used in an error condition. |
57 void SignalAllEvents(); | 75 void SignalAllEvents(); |
58 | 76 |
59 void OnShutdownEventSignaled(base::WaitableEvent* event); | 77 void OnShutdownEventSignaled(base::WaitableEvent* event); |
60 void OnIOMessageLoopDestroyed(); | 78 void OnIOMessageLoopDestroyed(); |
61 | 79 |
| 80 // NOTE: This must ONLY be called on the Channel's thread. |
| 81 void GetGenericRemoteAssociatedInterface( |
| 82 const std::string& interface_name, |
| 83 mojo::ScopedInterfaceEndpointHandle handle); |
| 84 |
62 // The channel to which this filter was added. | 85 // The channel to which this filter was added. |
63 Channel* channel_; | 86 Channel* channel_; |
64 | 87 |
65 // The process's main thread. | 88 // The process's main thread. |
66 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; | 89 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
67 | 90 |
68 // The message loop where the Channel lives. | 91 // The message loop where the Channel lives. |
69 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 92 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
70 | 93 |
71 typedef std::set<PendingSyncMsg*> PendingSyncMessages; | 94 typedef std::set<PendingSyncMsg*> PendingSyncMessages; |
(...skipping 10 matching lines...) Expand all Loading... |
82 // Used to asynchronously watch |shutdown_event_| on the IO thread and forward | 105 // Used to asynchronously watch |shutdown_event_| on the IO thread and forward |
83 // to |shutdown_mojo_event_| (see below.) | 106 // to |shutdown_mojo_event_| (see below.) |
84 base::WaitableEventWatcher shutdown_watcher_; | 107 base::WaitableEventWatcher shutdown_watcher_; |
85 | 108 |
86 // A Mojo event which can be watched for shutdown. Signals are forwarded to | 109 // A Mojo event which can be watched for shutdown. Signals are forwarded to |
87 // this event asynchronously from |shutdown_event_|. | 110 // this event asynchronously from |shutdown_event_|. |
88 MojoEvent shutdown_mojo_event_; | 111 MojoEvent shutdown_mojo_event_; |
89 | 112 |
90 scoped_refptr<IOMessageLoopObserver> io_message_loop_observer_; | 113 scoped_refptr<IOMessageLoopObserver> io_message_loop_observer_; |
91 | 114 |
| 115 // The AssociatedGroup for the underlying channel, used to construct new |
| 116 // associated interface endpoints. |
| 117 mojo::AssociatedGroup channel_associated_group_; |
| 118 |
92 base::WeakPtrFactory<SyncMessageFilter> weak_factory_; | 119 base::WeakPtrFactory<SyncMessageFilter> weak_factory_; |
93 | 120 |
94 DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter); | 121 DISALLOW_COPY_AND_ASSIGN(SyncMessageFilter); |
95 }; | 122 }; |
96 | 123 |
97 } // namespace IPC | 124 } // namespace IPC |
98 | 125 |
99 #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_ | 126 #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_ |
OLD | NEW |