1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IPC_IPC_CHANNEL_MOJO_H_
6 #define IPC_IPC_CHANNEL_MOJO_H_
15 #include "base/component_export.h"
16 #include "base/memory/raw_ptr.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/synchronization/lock.h"
20 #include "base/task/single_thread_task_runner.h"
21 #include "base/task/task_runner.h"
22 #include "build/build_config.h"
23 #include "ipc/ipc.mojom.h"
24 #include "ipc/ipc_channel.h"
25 #include "ipc/ipc_channel_factory.h"
26 #include "ipc/ipc_message_pipe_reader.h"
27 #include "ipc/ipc_mojo_bootstrap.h"
31 // Mojo-based IPC::Channel implementation over a Mojo message pipe.
33 // ChannelMojo builds a Mojo MessagePipe using the provided message pipe
34 // |handle| and builds an associated interface for each direction on the
37 // TODO(morrita): Add APIs to create extra MessagePipes to let
38 // Mojo-based objects talk over this Channel.
40 class COMPONENT_EXPORT(IPC) ChannelMojo
42 public Channel::AssociatedInterfaceSupport,
43 public internal::MessagePipeReader::Delegate {
45 // Creates a ChannelMojo.
46 static std::unique_ptr<ChannelMojo> Create(
47 mojo::ScopedMessagePipeHandle handle,
50 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
51 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
53 // Create a factory object for ChannelMojo.
54 // The factory is used to create Mojo-based ChannelProxy family.
55 // |host| must not be null.
56 static std::unique_ptr<ChannelFactory> CreateServerFactory(
57 mojo::ScopedMessagePipeHandle handle,
58 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
59 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
61 static std::unique_ptr<ChannelFactory> CreateClientFactory(
62 mojo::ScopedMessagePipeHandle handle,
63 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
64 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
66 ChannelMojo(const ChannelMojo&) = delete;
67 ChannelMojo& operator=(const ChannelMojo&) = delete;
69 ~ChannelMojo() override;
71 // Channel implementation
72 bool Connect() override;
73 void Pause() override;
74 void Unpause(bool flush) override;
75 void Flush() override;
76 void Close() override;
77 bool Send(Message* message) override;
78 Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
80 // These access protected API of IPC::Message, which has ChannelMojo
82 static MojoResult WriteToMessageAttachmentSet(
83 absl::optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
85 static MojoResult ReadFromMessageAttachmentSet(
87 absl::optional<std::vector<mojo::native::SerializedHandlePtr>>* handles);
89 // MessagePipeReader::Delegate
90 void OnPeerPidReceived(int32_t peer_pid) override;
91 void OnMessageReceived(const Message& message) override;
92 void OnBrokenDataReceived() override;
93 void OnPipeError() override;
94 void OnAssociatedInterfaceRequest(
95 mojo::GenericPendingAssociatedReceiver receiver) override;
99 mojo::ScopedMessagePipeHandle handle,
102 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
103 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
105 void ForwardMessage(mojo::Message message);
107 // Channel::AssociatedInterfaceSupport:
108 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
109 CreateThreadSafeChannel() override;
110 void AddGenericAssociatedInterface(
111 const std::string& name,
112 const GenericAssociatedInterfaceFactory& factory) override;
113 void GetRemoteAssociatedInterface(
114 mojo::GenericPendingAssociatedReceiver receiver) override;
116 void FinishConnectOnIOThread();
118 base::WeakPtr<ChannelMojo> weak_ptr_;
120 // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
121 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
123 const mojo::MessagePipeHandle pipe_;
124 std::unique_ptr<MojoBootstrap> bootstrap_;
125 raw_ptr<Listener, DanglingUntriaged> listener_;
127 std::unique_ptr<internal::MessagePipeReader> message_reader_;
129 base::Lock associated_interface_lock_;
130 std::map<std::string, GenericAssociatedInterfaceFactory>
131 associated_interfaces_;
133 base::WeakPtrFactory<ChannelMojo> weak_factory_{this};
138 #endif // IPC_IPC_CHANNEL_MOJO_H_