ipc_channel_mojo.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2014 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_CHANNEL_MOJO_H_
  5. #define IPC_IPC_CHANNEL_MOJO_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/component_export.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/synchronization/lock.h"
  16. #include "base/task/single_thread_task_runner.h"
  17. #include "base/task/task_runner.h"
  18. #include "base/threading/thread_task_runner_handle.h"
  19. #include "build/build_config.h"
  20. #include "ipc/ipc.mojom.h"
  21. #include "ipc/ipc_channel.h"
  22. #include "ipc/ipc_channel_factory.h"
  23. #include "ipc/ipc_message_pipe_reader.h"
  24. #include "ipc/ipc_mojo_bootstrap.h"
  25. #include "mojo/public/cpp/system/core.h"
  26. namespace IPC {
  27. // Mojo-based IPC::Channel implementation over a Mojo message pipe.
  28. //
  29. // ChannelMojo builds a Mojo MessagePipe using the provided message pipe
  30. // |handle| and builds an associated interface for each direction on the
  31. // channel.
  32. //
  33. // TODO(morrita): Add APIs to create extra MessagePipes to let
  34. // Mojo-based objects talk over this Channel.
  35. //
  36. class COMPONENT_EXPORT(IPC) ChannelMojo
  37. : public Channel,
  38. public Channel::AssociatedInterfaceSupport,
  39. public internal::MessagePipeReader::Delegate {
  40. public:
  41. // Creates a ChannelMojo.
  42. static std::unique_ptr<ChannelMojo> Create(
  43. mojo::ScopedMessagePipeHandle handle,
  44. Mode mode,
  45. Listener* listener,
  46. const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
  47. const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner,
  48. const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker);
  49. // Create a factory object for ChannelMojo.
  50. // The factory is used to create Mojo-based ChannelProxy family.
  51. // |host| must not be null.
  52. static std::unique_ptr<ChannelFactory> CreateServerFactory(
  53. mojo::ScopedMessagePipeHandle handle,
  54. const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
  55. const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
  56. static std::unique_ptr<ChannelFactory> CreateClientFactory(
  57. mojo::ScopedMessagePipeHandle handle,
  58. const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
  59. const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
  60. ChannelMojo(const ChannelMojo&) = delete;
  61. ChannelMojo& operator=(const ChannelMojo&) = delete;
  62. ~ChannelMojo() override;
  63. // Channel implementation
  64. bool Connect() override;
  65. void Pause() override;
  66. void Unpause(bool flush) override;
  67. void Flush() override;
  68. void Close() override;
  69. bool Send(Message* message) override;
  70. Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
  71. // These access protected API of IPC::Message, which has ChannelMojo
  72. // as a friend class.
  73. static MojoResult WriteToMessageAttachmentSet(
  74. absl::optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
  75. Message* message);
  76. static MojoResult ReadFromMessageAttachmentSet(
  77. Message* message,
  78. absl::optional<std::vector<mojo::native::SerializedHandlePtr>>* handles);
  79. // MessagePipeReader::Delegate
  80. void OnPeerPidReceived(int32_t peer_pid) override;
  81. void OnMessageReceived(const Message& message) override;
  82. void OnBrokenDataReceived() override;
  83. void OnPipeError() override;
  84. void OnAssociatedInterfaceRequest(
  85. mojo::GenericPendingAssociatedReceiver receiver) override;
  86. private:
  87. ChannelMojo(
  88. mojo::ScopedMessagePipeHandle handle,
  89. Mode mode,
  90. Listener* listener,
  91. const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
  92. const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner,
  93. const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker);
  94. void ForwardMessage(mojo::Message message);
  95. // Channel::AssociatedInterfaceSupport:
  96. std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
  97. CreateThreadSafeChannel() override;
  98. void AddGenericAssociatedInterface(
  99. const std::string& name,
  100. const GenericAssociatedInterfaceFactory& factory) override;
  101. void GetRemoteAssociatedInterface(
  102. mojo::GenericPendingAssociatedReceiver receiver) override;
  103. void FinishConnectOnIOThread();
  104. base::WeakPtr<ChannelMojo> weak_ptr_;
  105. // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
  106. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  107. const mojo::MessagePipeHandle pipe_;
  108. std::unique_ptr<MojoBootstrap> bootstrap_;
  109. raw_ptr<Listener> listener_;
  110. std::unique_ptr<internal::MessagePipeReader> message_reader_;
  111. base::Lock associated_interface_lock_;
  112. std::map<std::string, GenericAssociatedInterfaceFactory>
  113. associated_interfaces_;
  114. base::WeakPtrFactory<ChannelMojo> weak_factory_{this};
  115. };
  116. } // namespace IPC
  117. #endif // IPC_IPC_CHANNEL_MOJO_H_