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