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_CHANNEL_PROXY_H_ | 5 #ifndef IPC_IPC_CHANNEL_PROXY_H_ |
6 #define IPC_IPC_CHANNEL_PROXY_H_ | 6 #define IPC_IPC_CHANNEL_PROXY_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "ipc/ipc_channel.h" | 20 #include "ipc/ipc_channel.h" |
21 #include "ipc/ipc_channel_handle.h" | 21 #include "ipc/ipc_channel_handle.h" |
22 #include "ipc/ipc_endpoint.h" | |
23 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
24 #include "ipc/ipc_sender.h" | 23 #include "ipc/ipc_sender.h" |
25 #include "mojo/public/cpp/bindings/associated_group.h" | 24 #include "mojo/public/cpp/bindings/associated_group.h" |
26 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 25 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
27 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
28 | 27 |
29 namespace base { | 28 namespace base { |
30 class SingleThreadTaskRunner; | 29 class SingleThreadTaskRunner; |
31 } | 30 } |
32 | 31 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 // instance where the IPC::Channel will be created and operated. | 65 // instance where the IPC::Channel will be created and operated. |
67 // | 66 // |
68 // Thread-safe send | 67 // Thread-safe send |
69 // | 68 // |
70 // If a particular |Channel| implementation has a thread-safe |Send()| operation | 69 // If a particular |Channel| implementation has a thread-safe |Send()| operation |
71 // then ChannelProxy skips the inter-thread hop and calls |Send()| directly. In | 70 // then ChannelProxy skips the inter-thread hop and calls |Send()| directly. In |
72 // this case the |channel_| variable is touched by multiple threads so | 71 // this case the |channel_| variable is touched by multiple threads so |
73 // |channel_lifetime_lock_| is used to protect it. The locking overhead is only | 72 // |channel_lifetime_lock_| is used to protect it. The locking overhead is only |
74 // paid if the underlying channel supports thread-safe |Send|. | 73 // paid if the underlying channel supports thread-safe |Send|. |
75 // | 74 // |
76 class IPC_EXPORT ChannelProxy : public Endpoint, public base::NonThreadSafe { | 75 class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe { |
77 public: | 76 public: |
78 #if defined(ENABLE_IPC_FUZZER) | 77 #if defined(ENABLE_IPC_FUZZER) |
79 // Interface for a filter to be imposed on outgoing messages which can | 78 // Interface for a filter to be imposed on outgoing messages which can |
80 // re-write the message. Used for testing. | 79 // re-write the message. Used for testing. |
81 class OutgoingMessageFilter { | 80 class OutgoingMessageFilter { |
82 public: | 81 public: |
83 virtual Message* Rewrite(Message* message) = 0; | 82 virtual Message* Rewrite(Message* message) = 0; |
84 }; | 83 }; |
85 #endif | 84 #endif |
86 | 85 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 226 |
228 #if defined(ENABLE_IPC_FUZZER) | 227 #if defined(ENABLE_IPC_FUZZER) |
229 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { | 228 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { |
230 outgoing_message_filter_ = filter; | 229 outgoing_message_filter_ = filter; |
231 } | 230 } |
232 #endif | 231 #endif |
233 | 232 |
234 // Called to clear the pointer to the IPC task runner when it's going away. | 233 // Called to clear the pointer to the IPC task runner when it's going away. |
235 void ClearIPCTaskRunner(); | 234 void ClearIPCTaskRunner(); |
236 | 235 |
237 // Endpoint overrides. | |
238 base::ProcessId GetPeerPID() const override; | |
239 void OnSetAttachmentBrokerEndpoint() override; | |
240 | |
241 protected: | 236 protected: |
242 class Context; | 237 class Context; |
243 // A subclass uses this constructor if it needs to add more information | 238 // A subclass uses this constructor if it needs to add more information |
244 // to the internal state. | 239 // to the internal state. |
245 explicit ChannelProxy(Context* context); | 240 explicit ChannelProxy(Context* context); |
246 | 241 |
247 | 242 |
248 // Used internally to hold state that is referenced on the IPC thread. | 243 // Used internally to hold state that is referenced on the IPC thread. |
249 class Context : public base::RefCountedThreadSafe<Context>, | 244 class Context : public base::RefCountedThreadSafe<Context>, |
250 public Listener { | 245 public Listener { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 // more messages, so we honor that wish by forgetting them! | 295 // more messages, so we honor that wish by forgetting them! |
301 virtual void Clear(); | 296 virtual void Clear(); |
302 | 297 |
303 private: | 298 private: |
304 friend class ChannelProxy; | 299 friend class ChannelProxy; |
305 friend class IpcSecurityTestUtil; | 300 friend class IpcSecurityTestUtil; |
306 | 301 |
307 // Create the Channel | 302 // Create the Channel |
308 void CreateChannel(std::unique_ptr<ChannelFactory> factory); | 303 void CreateChannel(std::unique_ptr<ChannelFactory> factory); |
309 | 304 |
310 void set_attachment_broker_endpoint(bool is_endpoint) { | |
311 attachment_broker_endpoint_ = is_endpoint; | |
312 if (channel_) | |
313 channel_->SetAttachmentBrokerEndpoint(is_endpoint); | |
314 } | |
315 | |
316 // Methods called on the IO thread. | 305 // Methods called on the IO thread. |
317 void OnSendMessage(std::unique_ptr<Message> message_ptr); | 306 void OnSendMessage(std::unique_ptr<Message> message_ptr); |
318 void OnAddFilter(); | 307 void OnAddFilter(); |
319 void OnRemoveFilter(MessageFilter* filter); | 308 void OnRemoveFilter(MessageFilter* filter); |
320 | 309 |
321 // Methods called on the listener thread. | 310 // Methods called on the listener thread. |
322 void AddFilter(MessageFilter* filter); | 311 void AddFilter(MessageFilter* filter); |
323 void OnDispatchConnected(); | 312 void OnDispatchConnected(); |
324 void OnDispatchError(); | 313 void OnDispatchError(); |
325 void OnDispatchBadMessage(const Message& message); | 314 void OnDispatchBadMessage(const Message& message); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 // IPC thread when they're added to filters_. | 354 // IPC thread when they're added to filters_. |
366 std::vector<scoped_refptr<MessageFilter> > pending_filters_; | 355 std::vector<scoped_refptr<MessageFilter> > pending_filters_; |
367 // Lock for pending_filters_. | 356 // Lock for pending_filters_. |
368 base::Lock pending_filters_lock_; | 357 base::Lock pending_filters_lock_; |
369 | 358 |
370 // Cached copy of the peer process ID. Set on IPC but read on both IPC and | 359 // Cached copy of the peer process ID. Set on IPC but read on both IPC and |
371 // listener threads. | 360 // listener threads. |
372 base::ProcessId peer_pid_; | 361 base::ProcessId peer_pid_; |
373 base::Lock peer_pid_lock_; | 362 base::Lock peer_pid_lock_; |
374 | 363 |
375 // Whether this channel is used as an endpoint for sending and receiving | |
376 // brokerable attachment messages to/from the broker process. | |
377 bool attachment_broker_endpoint_; | |
378 | |
379 mojo::AssociatedGroup associated_group_; | 364 mojo::AssociatedGroup associated_group_; |
380 | 365 |
381 // Holds associated interface binders added by AddGenericAssociatedInterface | 366 // Holds associated interface binders added by AddGenericAssociatedInterface |
382 // or AddGenericAssociatedInterfaceForIOThread until the underlying channel | 367 // or AddGenericAssociatedInterfaceForIOThread until the underlying channel |
383 // has been initialized. | 368 // has been initialized. |
384 base::Lock pending_interfaces_lock_; | 369 base::Lock pending_interfaces_lock_; |
385 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> | 370 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> |
386 pending_interfaces_; | 371 pending_interfaces_; |
387 }; | 372 }; |
388 | 373 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 bool did_init_; | 406 bool did_init_; |
422 | 407 |
423 #if defined(ENABLE_IPC_FUZZER) | 408 #if defined(ENABLE_IPC_FUZZER) |
424 OutgoingMessageFilter* outgoing_message_filter_; | 409 OutgoingMessageFilter* outgoing_message_filter_; |
425 #endif | 410 #endif |
426 }; | 411 }; |
427 | 412 |
428 } // namespace IPC | 413 } // namespace IPC |
429 | 414 |
430 #endif // IPC_IPC_CHANNEL_PROXY_H_ | 415 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
OLD | NEW |