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 CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_ |
6 #define CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_ | 6 #define CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <utility> | 10 #include <utility> |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 class TaskRunner; | 24 class TaskRunner; |
25 } | 25 } |
26 | 26 |
27 namespace IPC { | 27 namespace IPC { |
28 class MessageFilter; | 28 class MessageFilter; |
29 } | 29 } |
30 | 30 |
31 namespace content { | 31 namespace content { |
| 32 class BrowserAssociatedInterfaceTest; |
32 struct BrowserMessageFilterTraits; | 33 struct BrowserMessageFilterTraits; |
33 | 34 |
34 // Base class for message filters in the browser process. You can receive and | 35 // Base class for message filters in the browser process. You can receive and |
35 // send messages on any thread. | 36 // send messages on any thread. |
36 class CONTENT_EXPORT BrowserMessageFilter | 37 class CONTENT_EXPORT BrowserMessageFilter |
37 : public base::RefCountedThreadSafe< | 38 : public base::RefCountedThreadSafe< |
38 BrowserMessageFilter, BrowserMessageFilterTraits>, | 39 BrowserMessageFilter, BrowserMessageFilterTraits>, |
39 public IPC::Sender { | 40 public IPC::Sender { |
40 public: | 41 public: |
41 explicit BrowserMessageFilter(uint32_t message_class_to_filter); | 42 explicit BrowserMessageFilter(uint32_t message_class_to_filter); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // since that has extra checks to avoid deadlocks. | 76 // since that has extra checks to avoid deadlocks. |
76 virtual base::TaskRunner* OverrideTaskRunnerForMessage( | 77 virtual base::TaskRunner* OverrideTaskRunnerForMessage( |
77 const IPC::Message& message); | 78 const IPC::Message& message); |
78 | 79 |
79 // Override this to receive messages. | 80 // Override this to receive messages. |
80 // Your function will normally be called on the IO thread. However, if your | 81 // Your function will normally be called on the IO thread. However, if your |
81 // OverrideXForMessage modifies the thread used to dispatch the message, | 82 // OverrideXForMessage modifies the thread used to dispatch the message, |
82 // your function will be called on the requested thread. | 83 // your function will be called on the requested thread. |
83 virtual bool OnMessageReceived(const IPC::Message& message) = 0; | 84 virtual bool OnMessageReceived(const IPC::Message& message) = 0; |
84 | 85 |
| 86 // Adds an associated interface factory to this filter. Must be called before |
| 87 // RegisterAssociatedInterfaces(). |
| 88 void AddAssociatedInterface( |
| 89 const std::string& name, |
| 90 const IPC::ChannelProxy::GenericAssociatedInterfaceFactory& factory); |
| 91 |
85 // Can be called on any thread, after OnChannelConnected is called. | 92 // Can be called on any thread, after OnChannelConnected is called. |
86 base::ProcessHandle PeerHandle(); | 93 base::ProcessHandle PeerHandle(); |
87 | 94 |
88 // Can be called on any thread, after OnChannelConnected is called. | 95 // Can be called on any thread, after OnChannelConnected is called. |
89 base::ProcessId peer_pid() const { return peer_process_.Pid(); } | 96 base::ProcessId peer_pid() const { return peer_process_.Pid(); } |
90 | 97 |
91 void set_peer_process_for_testing(base::Process peer_process) { | 98 void set_peer_process_for_testing(base::Process peer_process) { |
92 peer_process_ = std::move(peer_process); | 99 peer_process_ = std::move(peer_process); |
93 } | 100 } |
94 | 101 |
95 // Called by bad_message.h helpers if a message couldn't be deserialized. This | 102 // Called by bad_message.h helpers if a message couldn't be deserialized. This |
96 // kills the renderer. Can be called on any thread. This doesn't log the | 103 // kills the renderer. Can be called on any thread. This doesn't log the |
97 // error details to UMA, so use the bad_message.h for your module instead. | 104 // error details to UMA, so use the bad_message.h for your module instead. |
98 virtual void ShutdownForBadMessage(); | 105 virtual void ShutdownForBadMessage(); |
99 | 106 |
100 const std::vector<uint32_t>& message_classes_to_filter() const { | 107 const std::vector<uint32_t>& message_classes_to_filter() const { |
101 return message_classes_to_filter_; | 108 return message_classes_to_filter_; |
102 } | 109 } |
103 | 110 |
104 protected: | 111 protected: |
105 ~BrowserMessageFilter() override; | 112 ~BrowserMessageFilter() override; |
106 | 113 |
107 private: | 114 private: |
108 friend class base::RefCountedThreadSafe<BrowserMessageFilter, | 115 friend class base::RefCountedThreadSafe<BrowserMessageFilter, |
109 BrowserMessageFilterTraits>; | 116 BrowserMessageFilterTraits>; |
110 | 117 |
111 class Internal; | 118 class Internal; |
| 119 friend class BrowserAssociatedInterfaceTest; |
112 friend class BrowserChildProcessHostImpl; | 120 friend class BrowserChildProcessHostImpl; |
113 friend class BrowserPpapiHost; | 121 friend class BrowserPpapiHost; |
114 friend class RenderProcessHostImpl; | 122 friend class RenderProcessHostImpl; |
115 | 123 |
116 // This is private because the only classes that need access to it are made | 124 // These are private because the only classes that need access to them are |
117 // friends above. This is only guaranteed to be valid on creation, after that | 125 // made friends above. These are only guaranteed to be valid to call on |
118 // this class could outlive the filter. | 126 // creation. After that this class could outlive the filter and new interface |
| 127 // registrations could race with incoming requests. |
119 IPC::MessageFilter* GetFilter(); | 128 IPC::MessageFilter* GetFilter(); |
| 129 void RegisterAssociatedInterfaces(IPC::ChannelProxy* proxy); |
120 | 130 |
121 // This implements IPC::MessageFilter so that we can hide that from child | 131 // This implements IPC::MessageFilter so that we can hide that from child |
122 // classes. Internal keeps a reference to this class, which is why there's a | 132 // classes. Internal keeps a reference to this class, which is why there's a |
123 // weak pointer back. This class could outlive Internal based on what the | 133 // weak pointer back. This class could outlive Internal based on what the |
124 // child class does in its OnDestruct method. | 134 // child class does in its OnDestruct method. |
125 Internal* internal_; | 135 Internal* internal_; |
126 | 136 |
127 IPC::Sender* sender_; | 137 IPC::Sender* sender_; |
128 base::Process peer_process_; | 138 base::Process peer_process_; |
129 | 139 |
130 std::vector<uint32_t> message_classes_to_filter_; | 140 std::vector<uint32_t> message_classes_to_filter_; |
| 141 |
| 142 std::vector<std::pair<std::string, |
| 143 IPC::ChannelProxy::GenericAssociatedInterfaceFactory>> |
| 144 associated_interfaces_; |
131 }; | 145 }; |
132 | 146 |
133 struct BrowserMessageFilterTraits { | 147 struct BrowserMessageFilterTraits { |
134 static void Destruct(const BrowserMessageFilter* filter) { | 148 static void Destruct(const BrowserMessageFilter* filter) { |
135 filter->OnDestruct(); | 149 filter->OnDestruct(); |
136 } | 150 } |
137 }; | 151 }; |
138 | 152 |
139 } // namespace content | 153 } // namespace content |
140 | 154 |
141 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_ | 155 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_ |
OLD | NEW |