ipc_sync_message_filter.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  3. // found in the LICENSE file.
  4. #ifndef IPC_IPC_SYNC_MESSAGE_FILTER_H_
  5. #define IPC_IPC_SYNC_MESSAGE_FILTER_H_
  6. #include <set>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/synchronization/lock.h"
  12. #include "ipc/ipc_sender.h"
  13. #include "ipc/ipc_sync_message.h"
  14. #include "ipc/message_filter.h"
  15. #include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h"
  16. #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
  17. namespace base {
  18. class SingleThreadTaskRunner;
  19. class WaitableEvent;
  20. }
  21. namespace IPC {
  22. class SyncChannel;
  23. // This MessageFilter allows sending synchronous IPC messages from a thread
  24. // other than the listener thread associated with the SyncChannel. It does not
  25. // support fancy features that SyncChannel does, such as handling recursion or
  26. // receiving messages while waiting for a response. Note that this object can
  27. // be used to send simultaneous synchronous messages from different threads.
  28. class COMPONENT_EXPORT(IPC) SyncMessageFilter : public MessageFilter,
  29. public Sender {
  30. public:
  31. SyncMessageFilter(const SyncMessageFilter&) = delete;
  32. SyncMessageFilter& operator=(const SyncMessageFilter&) = delete;
  33. // Sender implementation.
  34. bool Send(Message* message) override;
  35. // MessageFilter implementation.
  36. void OnFilterAdded(Channel* channel) override;
  37. void OnChannelError() override;
  38. void OnChannelClosing() override;
  39. bool OnMessageReceived(const Message& message) override;
  40. // Binds an associated interface proxy to an interface in the browser process.
  41. // Interfaces acquired through this method are associated with the IPC Channel
  42. // and as such retain FIFO with legacy IPC messages.
  43. //
  44. // NOTE: This must ONLY be called on the Channel's thread, after
  45. // OnFilterAdded.
  46. void GetRemoteAssociatedInterface(
  47. mojo::GenericPendingAssociatedReceiver receiver);
  48. template <typename Interface>
  49. void GetRemoteAssociatedInterface(
  50. mojo::PendingAssociatedRemote<Interface>* proxy) {
  51. GetRemoteAssociatedInterface(proxy->InitWithNewEndpointAndPassReceiver());
  52. }
  53. protected:
  54. explicit SyncMessageFilter(base::WaitableEvent* shutdown_event);
  55. ~SyncMessageFilter() override;
  56. private:
  57. friend class SyncChannel;
  58. void SendOnIOThread(Message* message);
  59. // Signal all the pending sends as done, used in an error condition.
  60. void SignalAllEvents();
  61. // The channel to which this filter was added.
  62. raw_ptr<Channel> channel_;
  63. // The process's main thread.
  64. scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
  65. // The message loop where the Channel lives.
  66. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  67. typedef std::set<PendingSyncMsg*> PendingSyncMessages;
  68. PendingSyncMessages pending_sync_messages_;
  69. // Messages waiting to be delivered after IO initialization.
  70. std::vector<std::unique_ptr<Message>> pending_messages_;
  71. // Locks data members above.
  72. base::Lock lock_;
  73. const raw_ptr<base::WaitableEvent> shutdown_event_;
  74. };
  75. } // namespace IPC
  76. #endif // IPC_IPC_SYNC_MESSAGE_FILTER_H_