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_FORWARDING_MESSAGE_FILTER_H_ | 5 #ifndef IPC_IPC_FORWARDING_MESSAGE_FILTER_H_ |
6 #define IPC_IPC_FORWARDING_MESSAGE_FILTER_H_ | 6 #define IPC_IPC_FORWARDING_MESSAGE_FILTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
15 #include "ipc/ipc_channel_proxy.h" | 15 #include "ipc/message_filter.h" |
16 | 16 |
17 namespace IPC { | 17 namespace IPC { |
18 | 18 |
19 // This class can be used to intercept routed messages and | 19 // This class can be used to intercept routed messages and |
20 // deliver them to a different task runner than they would otherwise | 20 // deliver them to a different task runner than they would otherwise |
21 // be sent. Messages are filtered based on type. To route these messages, | 21 // be sent. Messages are filtered based on type. To route these messages, |
22 // add a MessageRouter to the handler. | 22 // add a MessageRouter to the handler. |
23 // | 23 // |
24 // The user of this class implements ForwardingMessageFilter::Client, | 24 // The user of this class implements ForwardingMessageFilter::Client, |
25 // which will receive the intercepted messages, on the specified target thread. | 25 // which will receive the intercepted messages, on the specified target thread. |
26 class IPC_EXPORT ForwardingMessageFilter : public ChannelProxy::MessageFilter { | 26 class IPC_EXPORT ForwardingMessageFilter : public MessageFilter { |
27 public: | 27 public: |
28 | 28 |
29 // The handler is invoked on the thread associated with | 29 // The handler is invoked on the thread associated with |
30 // |target_task_runner| with messages that were intercepted by this filter. | 30 // |target_task_runner| with messages that were intercepted by this filter. |
31 typedef base::Callback<void(const Message&)> Handler; | 31 typedef base::Callback<void(const Message&)> Handler; |
32 | 32 |
33 // This filter will intercept |message_ids_to_filter| and post | 33 // This filter will intercept |message_ids_to_filter| and post |
34 // them to the provided |target_task_runner|, where they will be given | 34 // them to the provided |target_task_runner|, where they will be given |
35 // to |handler|. | 35 // to |handler|. |
36 // | 36 // |
37 // The caller must ensure that |handler| outlives the lifetime of the filter. | 37 // The caller must ensure that |handler| outlives the lifetime of the filter. |
38 ForwardingMessageFilter( | 38 ForwardingMessageFilter( |
39 const uint32* message_ids_to_filter, | 39 const uint32* message_ids_to_filter, |
40 size_t num_message_ids_to_filter, | 40 size_t num_message_ids_to_filter, |
41 base::TaskRunner* target_task_runner); | 41 base::TaskRunner* target_task_runner); |
42 | 42 |
43 // Define the message routes to be filtered. | 43 // Define the message routes to be filtered. |
44 void AddRoute(int routing_id, const Handler& handler); | 44 void AddRoute(int routing_id, const Handler& handler); |
45 void RemoveRoute(int routing_id); | 45 void RemoveRoute(int routing_id); |
46 | 46 |
47 // ChannelProxy::MessageFilter methods: | 47 // MessageFilter methods: |
48 virtual bool OnMessageReceived(const Message& message) OVERRIDE; | 48 virtual bool OnMessageReceived(const Message& message) OVERRIDE; |
49 | 49 |
50 private: | 50 private: |
51 friend class ChannelProxy::MessageFilter; | |
52 virtual ~ForwardingMessageFilter(); | 51 virtual ~ForwardingMessageFilter(); |
53 | 52 |
54 std::set<int> message_ids_to_filter_; | 53 std::set<int> message_ids_to_filter_; |
55 | 54 |
56 // The handler_ only gets Run on the thread corresponding to | 55 // The handler_ only gets Run on the thread corresponding to |
57 // target_task_runner_. | 56 // target_task_runner_. |
58 scoped_refptr<base::TaskRunner> target_task_runner_; | 57 scoped_refptr<base::TaskRunner> target_task_runner_; |
59 | 58 |
60 // Protects access to routes_. | 59 // Protects access to routes_. |
61 base::Lock handlers_lock_; | 60 base::Lock handlers_lock_; |
62 | 61 |
63 // Indicates the routing_ids for which messages should be filtered. | 62 // Indicates the routing_ids for which messages should be filtered. |
64 std::map<int, Handler> handlers_; | 63 std::map<int, Handler> handlers_; |
65 | 64 |
66 DISALLOW_COPY_AND_ASSIGN(ForwardingMessageFilter); | 65 DISALLOW_COPY_AND_ASSIGN(ForwardingMessageFilter); |
67 }; | 66 }; |
68 | 67 |
69 } // namespace IPC | 68 } // namespace IPC |
70 | 69 |
71 #endif // IPC_IPC_FORWARDING_MESSAGE_FILTER_H_ | 70 #endif // IPC_IPC_FORWARDING_MESSAGE_FILTER_H_ |
OLD | NEW |