blob: 58a6937b0de3010249d8bea6dfd33ec129b29fd1 [file] [log] [blame]
Avi Drissmanea1be232022-09-14 23:29:061// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]92bf9062011-05-02 18:00:495#ifndef IPC_IPC_CHANNEL_PROXY_H_
6#define IPC_IPC_CHANNEL_PROXY_H_
initial.commit09911bf2008-07-26 23:55:297
avi246998d82015-12-22 02:39:048#include <stdint.h>
9
rockot8d890f62016-07-14 16:37:1410#include <map>
danakj03de39b22016-04-23 04:21:0911#include <memory>
rockot8d890f62016-07-14 16:37:1412#include <string>
initial.commit09911bf2008-07-26 23:55:2913#include <vector>
[email protected]7bf730952009-05-29 09:31:1514
Sebastien Marchand6d0558fd2019-01-25 16:49:3715#include "base/bind.h"
rockot8d890f62016-07-14 16:37:1416#include "base/callback.h"
Ken Rockot3044d212018-01-23 02:44:3917#include "base/component_export.h"
Keishi Hattori0e45c022021-11-27 09:25:5218#include "base/memory/raw_ptr.h"
[email protected]3b63f8f42011-03-28 01:54:1519#include "base/memory/ref_counted.h"
gab21edbfa2017-05-31 16:49:1220#include "base/sequence_checker.h"
[email protected]20305ec2011-01-21 04:55:5221#include "base/synchronization/lock.h"
avi246998d82015-12-22 02:39:0422#include "build/build_config.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:5623#include "ipc/ipc.mojom.h"
[email protected]946d1b22009-07-22 23:57:2124#include "ipc/ipc_channel.h"
[email protected]42ce94e2010-12-08 19:28:0925#include "ipc/ipc_channel_handle.h"
[email protected]57319ce2012-06-11 22:35:2626#include "ipc/ipc_listener.h"
27#include "ipc/ipc_sender.h"
Miyoung Shin998cfcf2019-09-05 07:31:5128#include "mojo/public/cpp/bindings/associated_remote.h"
Ken Rockot493a59f32021-06-04 22:16:5029#include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h"
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2030#include "mojo/public/cpp/bindings/lib/message_quota_checker.h"
Julie Jeongeun Kima6c8e672019-12-07 03:27:0531#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
32#include "mojo/public/cpp/bindings/pending_associated_remote.h"
rockot8d890f62016-07-14 16:37:1433#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
Julie Jeongeun Kima6c8e672019-12-07 03:27:0534#include "mojo/public/cpp/bindings/shared_associated_remote.h"
initial.commit09911bf2008-07-26 23:55:2935
[email protected]b2432302012-07-02 21:15:5236namespace base {
37class SingleThreadTaskRunner;
38}
39
initial.commit09911bf2008-07-26 23:55:2940namespace IPC {
41
[email protected]64860882014-08-04 23:44:1742class ChannelFactory;
[email protected]74122042014-04-25 00:07:3043class MessageFilter;
44class MessageFilterRouter;
[email protected]163951422009-07-28 22:09:4545
initial.commit09911bf2008-07-26 23:55:2946//-----------------------------------------------------------------------------
47// IPC::ChannelProxy
48//
49// This class is a helper class that is useful when you wish to run an IPC
50// channel on a background thread. It provides you with the option of either
51// handling IPC messages on that background thread or having them dispatched to
52// your main thread (the thread on which the IPC::ChannelProxy is created).
53//
54// The API for an IPC::ChannelProxy is very similar to that of an IPC::Channel.
55// When you send a message to an IPC::ChannelProxy, the message is routed to
56// the background thread, where it is then passed to the IPC::Channel's Send
57// method. This means that you can send a message from your thread and your
58// message will be sent over the IPC channel when possible instead of being
59// delayed until your thread returns to its message loop. (Often IPC messages
60// will queue up on the IPC::Channel when there is a lot of traffic, and the
61// channel will not get cycles to flush its message queue until the thread, on
62// which it is running, returns to its message loop.)
63//
64// An IPC::ChannelProxy can have a MessageFilter associated with it, which will
65// be notified of incoming messages on the IPC::Channel's thread. This gives
66// the consumer of IPC::ChannelProxy the ability to respond to incoming
67// messages on this background thread instead of on their own thread, which may
68// be bogged down with other processing. The result can be greatly improved
69// latency for messages that can be handled on a background thread.
70//
71// The consumer of IPC::ChannelProxy is responsible for allocating the Thread
72// instance where the IPC::Channel will be created and operated.
73//
morritab4472142015-04-20 21:20:1274// Thread-safe send
75//
76// If a particular |Channel| implementation has a thread-safe |Send()| operation
77// then ChannelProxy skips the inter-thread hop and calls |Send()| directly. In
78// this case the |channel_| variable is touched by multiple threads so
79// |channel_lifetime_lock_| is used to protect it. The locking overhead is only
80// paid if the underlying channel supports thread-safe |Send|.
81//
Ken Rockot3044d212018-01-23 02:44:3982class COMPONENT_EXPORT(IPC) ChannelProxy : public Sender {
initial.commit09911bf2008-07-26 23:55:2983 public:
mbarbellae97877bc2015-03-06 21:51:1484#if defined(ENABLE_IPC_FUZZER)
85 // Interface for a filter to be imposed on outgoing messages which can
86 // re-write the message. Used for testing.
87 class OutgoingMessageFilter {
88 public:
89 virtual Message* Rewrite(Message* message) = 0;
90 };
91#endif
92
[email protected]42ce94e2010-12-08 19:28:0993 // Initializes a channel proxy. The channel_handle and mode parameters are
initial.commit09911bf2008-07-26 23:55:2994 // passed directly to the underlying IPC::Channel. The listener is called on
95 // the thread that creates the ChannelProxy. The filter's OnMessageReceived
96 // method is called on the thread where the IPC::Channel is running. The
97 // filter may be null if the consumer is not interested in handling messages
98 // on the background thread. Any message not handled by the filter will be
[email protected]b2432302012-07-02 21:15:5299 // dispatched to the listener. The given task runner correspond to a thread
100 // on which IPC::Channel is created and used (e.g. IO thread).
danakj03de39b22016-04-23 04:21:09101 static std::unique_ptr<ChannelProxy> Create(
[email protected]fca876a12014-06-05 16:15:38102 const IPC::ChannelHandle& channel_handle,
103 Channel::Mode mode,
104 Listener* listener,
Hajime Hoshiff15e972017-11-09 06:37:09105 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
106 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner);
initial.commit09911bf2008-07-26 23:55:29107
danakj03de39b22016-04-23 04:21:09108 static std::unique_ptr<ChannelProxy> Create(
109 std::unique_ptr<ChannelFactory> factory,
[email protected]64860882014-08-04 23:44:17110 Listener* listener,
Hajime Hoshiff15e972017-11-09 06:37:09111 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
112 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner);
[email protected]64860882014-08-04 23:44:17113
erikchen90971902016-04-25 23:45:31114 // Constructs a ChannelProxy without initializing it.
115 ChannelProxy(
116 Listener* listener,
Hajime Hoshiff15e972017-11-09 06:37:09117 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
118 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner);
erikchen90971902016-04-25 23:45:31119
dchengfe61fca2014-10-22 02:29:52120 ~ChannelProxy() override;
initial.commit09911bf2008-07-26 23:55:29121
[email protected]952394af2011-11-16 01:06:46122 // Initializes the channel proxy. Only call this once to initialize a channel
rockot401fb2c2016-09-06 18:35:57123 // proxy that was not initialized in its constructor. If |create_pipe_now| is
[email protected]952394af2011-11-16 01:06:46124 // true, the pipe is created synchronously. Otherwise it's created on the IO
rockot10188752016-09-08 18:24:56125 // thread.
erikchen27aa7d82015-06-16 21:21:04126 void Init(const IPC::ChannelHandle& channel_handle,
127 Channel::Mode mode,
rockot10188752016-09-08 18:24:56128 bool create_pipe_now);
rockot401fb2c2016-09-06 18:35:57129 void Init(std::unique_ptr<ChannelFactory> factory,
rockot10188752016-09-08 18:24:56130 bool create_pipe_now);
rockot401fb2c2016-09-06 18:35:57131
rockot10188752016-09-08 18:24:56132 // Pause the channel. Subsequent calls to Send() will be internally queued
133 // until Unpause() is called. Queued messages will not be sent until the
134 // channel is flushed.
135 void Pause();
136
137 // Unpause the channel. If |flush| is true the channel will be flushed as soon
138 // as it's unpaused (see Flush() below.) Otherwise you must explicitly call
139 // Flush() to flush messages which were queued while the channel was paused.
rockot401fb2c2016-09-06 18:35:57140 void Unpause(bool flush);
141
142 // Flush the channel. This sends any messages which were queued before calling
143 // Connect. Only useful if Unpause(false) was called previously.
144 void Flush();
[email protected]952394af2011-11-16 01:06:46145
initial.commit09911bf2008-07-26 23:55:29146 // Close the IPC::Channel. This operation completes asynchronously, once the
147 // background thread processes the command to close the channel. It is ok to
148 // call this method multiple times. Redundant calls are ignored.
149 //
[email protected]74122042014-04-25 00:07:30150 // WARNING: MessageFilter objects held by the ChannelProxy is also
initial.commit09911bf2008-07-26 23:55:29151 // released asynchronously, and it may in fact have its final reference
152 // released on the background thread. The caller should be careful to deal
153 // with / allow for this possibility.
154 void Close();
155
rockotf14a8ae2016-06-16 19:28:41156 // Send a message asynchronously. The message is routed to the background
157 // thread where it is passed to the IPC::Channel's Send method.
dchengfe61fca2014-10-22 02:29:52158 bool Send(Message* message) override;
initial.commit09911bf2008-07-26 23:55:29159
160 // Used to intercept messages as they are received on the background thread.
161 //
162 // Ordinarily, messages sent to the ChannelProxy are routed to the matching
163 // listener on the worker thread. This API allows code to intercept messages
164 // before they are sent to the worker thread.
[email protected]4b580bf2010-12-02 19:16:07165 // If you call this before the target process is launched, then you're
166 // guaranteed to not miss any messages. But if you call this anytime after,
167 // then some messages might be missed since the filter is added internally on
168 // the IO thread.
initial.commit09911bf2008-07-26 23:55:29169 void AddFilter(MessageFilter* filter);
170 void RemoveFilter(MessageFilter* filter);
171
rockot8d890f62016-07-14 16:37:14172 using GenericAssociatedInterfaceFactory =
Matt Falkenhagen6140bb12019-11-19 22:52:36173 base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>;
rockot8d890f62016-07-14 16:37:14174
175 // Adds a generic associated interface factory to bind incoming interface
rockotfd3d90842016-09-15 23:57:29176 // requests directly on the IO thread. MUST be called either before Init() or
177 // before the remote end of the Channel is able to send messages (e.g. before
178 // its process is launched.)
rockot8d890f62016-07-14 16:37:14179 void AddGenericAssociatedInterfaceForIOThread(
180 const std::string& name,
181 const GenericAssociatedInterfaceFactory& factory);
182
rockot8d890f62016-07-14 16:37:14183 template <typename Interface>
Julie Jeongeun Kima6c8e672019-12-07 03:27:05184 using AssociatedInterfaceFactory =
185 base::RepeatingCallback<void(mojo::PendingAssociatedReceiver<Interface>)>;
rockot8d890f62016-07-14 16:37:14186
187 // Helper to bind an IO-thread associated interface factory, inferring the
188 // interface name from the callback argument's type. MUST be called before
189 // Init().
190 template <typename Interface>
191 void AddAssociatedInterfaceForIOThread(
192 const AssociatedInterfaceFactory<Interface>& factory) {
193 AddGenericAssociatedInterfaceForIOThread(
194 Interface::Name_,
Matt Falkenhagen6140bb12019-11-19 22:52:36195 base::BindRepeating(
Julie Jeongeun Kima6c8e672019-12-07 03:27:05196 &ChannelProxy::BindPendingAssociatedReceiver<Interface>, factory));
rockot8d890f62016-07-14 16:37:14197 }
198
rockot8d890f62016-07-14 16:37:14199 // Requests an associated interface from the remote endpoint.
Ken Rockot493a59f32021-06-04 22:16:50200 void GetRemoteAssociatedInterface(
201 mojo::GenericPendingAssociatedReceiver receiver);
rockot8d890f62016-07-14 16:37:14202
Miyoung Shin998cfcf2019-09-05 07:31:51203 // Template helper to receive associated interfaces from the remote endpoint.
204 template <typename Interface>
205 void GetRemoteAssociatedInterface(mojo::AssociatedRemote<Interface>* proxy) {
Ken Rockot493a59f32021-06-04 22:16:50206 GetRemoteAssociatedInterface(proxy->BindNewEndpointAndPassReceiver());
Miyoung Shin998cfcf2019-09-05 07:31:51207 }
208
mbarbellae97877bc2015-03-06 21:51:14209#if defined(ENABLE_IPC_FUZZER)
210 void set_outgoing_message_filter(OutgoingMessageFilter* filter) {
211 outgoing_message_filter_ = filter;
212 }
213#endif
214
Julie Jeongeun Kima6c8e672019-12-07 03:27:05215 // Creates a SharedAssociatedRemote for |Interface|. This object may be used
216 // to send messages on the interface from any thread and those messages will
217 // remain ordered with respect to other messages sent on the same thread over
218 // other SharedAssociatedRemotes associated with the same Channel.
jcivelli315d17f2016-11-29 16:15:05219 template <typename Interface>
rockota628d0b2017-02-09 08:40:15220 void GetThreadSafeRemoteAssociatedInterface(
Julie Jeongeun Kima6c8e672019-12-07 03:27:05221 scoped_refptr<mojo::SharedAssociatedRemote<Interface>>* out_remote) {
222 mojo::PendingAssociatedRemote<Interface> pending_remote;
223 auto receiver = pending_remote.InitWithNewEndpointAndPassReceiver();
224 GetGenericRemoteAssociatedInterface(Interface::Name_,
225 receiver.PassHandle());
226 *out_remote = mojo::SharedAssociatedRemote<Interface>::Create(
227 std::move(pending_remote), ipc_task_runner());
jcivelli315d17f2016-11-29 16:15:05228 }
229
jcivelli35e5ff52017-01-06 18:23:01230 base::SingleThreadTaskRunner* ipc_task_runner() const {
231 return context_->ipc_task_runner();
232 }
233
xlai11ac7802017-04-07 20:13:18234 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner_refptr()
235 const {
236 return context_->ipc_task_runner_refptr();
237 }
238
[email protected]b2432302012-07-02 21:15:52239 // Called to clear the pointer to the IPC task runner when it's going away.
240 void ClearIPCTaskRunner();
[email protected]42f1d7822009-07-23 18:17:55241
initial.commit09911bf2008-07-26 23:55:29242 protected:
initial.commit09911bf2008-07-26 23:55:29243 class Context;
244 // A subclass uses this constructor if it needs to add more information
[email protected]952394af2011-11-16 01:06:46245 // to the internal state.
rockot0e4de5f2016-07-22 21:18:07246 explicit ChannelProxy(Context* context);
initial.commit09911bf2008-07-26 23:55:29247
248 // Used internally to hold state that is referenced on the IPC thread.
249 class Context : public base::RefCountedThreadSafe<Context>,
[email protected]57319ce2012-06-11 22:35:26250 public Listener {
initial.commit09911bf2008-07-26 23:55:29251 public:
dchengfd033702014-08-28 16:59:29252 Context(Listener* listener,
Hajime Hoshiff15e972017-11-09 06:37:09253 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
254 const scoped_refptr<base::SingleThreadTaskRunner>&
255 listener_task_runner);
[email protected]b2432302012-07-02 21:15:52256 void ClearIPCTaskRunner();
257 base::SingleThreadTaskRunner* ipc_task_runner() const {
[email protected]17571642013-06-01 04:11:27258 return ipc_task_runner_.get();
[email protected]92bf9062011-05-02 18:00:49259 }
xlai11ac7802017-04-07 20:13:18260 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner_refptr()
261 const {
262 return ipc_task_runner_;
263 }
264
Hajime Hoshi138652c92018-01-12 15:11:44265 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner() {
Yuzu Saijo8866b1d2019-05-24 03:23:28266 return default_listener_task_runner_;
Hajime Hoshi138652c92018-01-12 15:11:44267 }
268
[email protected]827ab812009-03-12 07:17:17269 // Dispatches a message on the listener thread.
270 void OnDispatchMessage(const Message& message);
271
morritab4472142015-04-20 21:20:12272 // Sends |message| from appropriate thread.
Etienne Bergeron0a07d73d2019-06-27 16:40:47273 void Send(Message* message);
morritab4472142015-04-20 21:20:12274
Yuzu Saijo8866b1d2019-05-24 03:23:28275 // Adds |task_runner| for the task to be executed later.
276 void AddListenerTaskRunner(
277 int32_t routing_id,
278 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
279
280 // Removes task runner for |routing_id|.
281 void RemoveListenerTaskRunner(int32_t routing_id);
282
283 // Called on the IPC::Channel thread.
284 // Returns the task runner associated with |routing_id|.
Yuzu Saijo3b6330012019-07-11 03:51:27285 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(
286 int32_t routing_id);
Yuzu Saijo8866b1d2019-05-24 03:23:28287
[email protected]d3ae7a072008-12-05 20:27:20288 protected:
[email protected]877d55d2009-11-05 21:53:08289 friend class base::RefCountedThreadSafe<Context>;
dchengfe61fca2014-10-22 02:29:52290 ~Context() override;
[email protected]877d55d2009-11-05 21:53:08291
[email protected]57319ce2012-06-11 22:35:26292 // IPC::Listener methods:
dchengfe61fca2014-10-22 02:29:52293 bool OnMessageReceived(const Message& message) override;
tfarina10a5c062015-09-04 18:47:57294 void OnChannelConnected(int32_t peer_pid) override;
dchengfe61fca2014-10-22 02:29:52295 void OnChannelError() override;
rockotf62002a2016-09-15 00:08:59296 void OnAssociatedInterfaceRequest(
297 const std::string& interface_name,
298 mojo::ScopedInterfaceEndpointHandle handle) override;
initial.commit09911bf2008-07-26 23:55:29299
[email protected]3cdb7af812008-10-24 19:21:13300 // Like OnMessageReceived but doesn't try the filters.
[email protected]a95986a82010-12-24 06:19:28301 bool OnMessageReceivedNoFilter(const Message& message);
[email protected]3cdb7af812008-10-24 19:21:13302
[email protected]d65cab7a2008-08-12 01:25:41303 // Gives the filters a chance at processing |message|.
304 // Returns true if the message was processed, false otherwise.
305 bool TryFilters(const Message& message);
306
rockot10188752016-09-08 18:24:56307 void PauseChannel();
rockot401fb2c2016-09-06 18:35:57308 void UnpauseChannel(bool flush);
309 void FlushChannel();
310
[email protected]3cdb7af812008-10-24 19:21:13311 // Like Open and Close, but called on the IPC thread.
rockot10188752016-09-08 18:24:56312 virtual void OnChannelOpened();
[email protected]3cdb7af812008-10-24 19:21:13313 virtual void OnChannelClosed();
314
315 // Called on the consumers thread when the ChannelProxy is closed. At that
316 // point the consumer is telling us that they don't want to receive any
317 // more messages, so we honor that wish by forgetting them!
[email protected]772a5b22012-08-09 20:39:12318 virtual void Clear();
[email protected]3cdb7af812008-10-24 19:21:13319
initial.commit09911bf2008-07-26 23:55:29320 private:
321 friend class ChannelProxy;
nick4c8dfd42014-11-14 04:11:49322 friend class IpcSecurityTestUtil;
[email protected]877d55d2009-11-05 21:53:08323
initial.commit09911bf2008-07-26 23:55:29324 // Create the Channel
danakj03de39b22016-04-23 04:21:09325 void CreateChannel(std::unique_ptr<ChannelFactory> factory);
initial.commit09911bf2008-07-26 23:55:29326
[email protected]4b580bf2010-12-02 19:16:07327 // Methods called on the IO thread.
Etienne Bergeron0a07d73d2019-06-27 16:40:47328 void OnSendMessage(std::unique_ptr<Message> message_ptr);
[email protected]4b580bf2010-12-02 19:16:07329 void OnAddFilter();
initial.commit09911bf2008-07-26 23:55:29330 void OnRemoveFilter(MessageFilter* filter);
[email protected]4b580bf2010-12-02 19:16:07331
332 // Methods called on the listener thread.
333 void AddFilter(MessageFilter* filter);
[email protected]827ab812009-03-12 07:17:17334 void OnDispatchConnected();
initial.commit09911bf2008-07-26 23:55:29335 void OnDispatchError();
[email protected]ef2f6ba2014-05-15 23:06:07336 void OnDispatchBadMessage(const Message& message);
rockotf62002a2016-09-15 00:08:59337 void OnDispatchAssociatedInterfaceRequest(
338 const std::string& interface_name,
339 mojo::ScopedInterfaceEndpointHandle handle);
initial.commit09911bf2008-07-26 23:55:29340
morritab4472142015-04-20 21:20:12341 void ClearChannel();
342
rockota628d0b2017-02-09 08:40:15343 mojom::Channel& thread_safe_channel() {
344 return thread_safe_channel_->proxy();
345 }
rockot0e4de5f2016-07-22 21:18:07346
rockot0e4de5f2016-07-22 21:18:07347 void AddGenericAssociatedInterfaceForIOThread(
348 const std::string& name,
349 const GenericAssociatedInterfaceFactory& factory);
350
Yuzu Saijo8866b1d2019-05-24 03:23:28351 base::Lock listener_thread_task_runners_lock_;
352 // Map of routing_id and listener's thread task runner.
353 std::map<int32_t, scoped_refptr<base::SingleThreadTaskRunner>>
354 listener_thread_task_runners_
355 GUARDED_BY(listener_thread_task_runners_lock_);
356
357 scoped_refptr<base::SingleThreadTaskRunner> default_listener_task_runner_;
Keishi Hattori0e45c022021-11-27 09:25:52358 raw_ptr<Listener> listener_;
initial.commit09911bf2008-07-26 23:55:29359
360 // List of filters. This is only accessed on the IPC thread.
[email protected]d4651ff2008-12-02 16:51:58361 std::vector<scoped_refptr<MessageFilter> > filters_;
[email protected]b2432302012-07-02 21:15:52362 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
[email protected]7e3d7522014-03-20 21:00:50363
364 // Note, channel_ may be set on the Listener thread or the IPC thread.
365 // But once it has been set, it must only be read or cleared on the IPC
366 // thread.
morritab4472142015-04-20 21:20:12367 // One exception is the thread-safe send. See the class comment.
danakj03de39b22016-04-23 04:21:09368 std::unique_ptr<Channel> channel_;
[email protected]827ab812009-03-12 07:17:17369 bool channel_connected_called_;
[email protected]4b580bf2010-12-02 19:16:07370
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:20371 // The quota checker associated with this channel, if any.
372 scoped_refptr<mojo::internal::MessageQuotaChecker> quota_checker_;
373
morritab4472142015-04-20 21:20:12374 // Lock for |channel_| value. This is only relevant in the context of
375 // thread-safe send.
376 base::Lock channel_lifetime_lock_;
morritab4472142015-04-20 21:20:12377
[email protected]b51352a2014-02-26 05:18:04378 // Routes a given message to a proper subset of |filters_|, depending
379 // on which message classes a filter might support.
danakj03de39b22016-04-23 04:21:09380 std::unique_ptr<MessageFilterRouter> message_filter_router_;
[email protected]b51352a2014-02-26 05:18:04381
[email protected]4b580bf2010-12-02 19:16:07382 // Holds filters between the AddFilter call on the listerner thread and the
383 // IPC thread when they're added to filters_.
384 std::vector<scoped_refptr<MessageFilter> > pending_filters_;
385 // Lock for pending_filters_.
[email protected]20305ec2011-01-21 04:55:52386 base::Lock pending_filters_lock_;
[email protected]0a6fc4b2012-04-05 02:38:34387
388 // Cached copy of the peer process ID. Set on IPC but read on both IPC and
389 // listener threads.
390 base::ProcessId peer_pid_;
amistry78ef5f1b2016-07-19 02:52:31391 base::Lock peer_pid_lock_;
erikchenbcc79612015-07-31 00:04:42392
rockota628d0b2017-02-09 08:40:15393 // A thread-safe mojom::Channel interface we use to make remote interface
394 // requests from the proxy thread.
395 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
396 thread_safe_channel_;
397
rockot70bbb59492017-01-25 00:56:51398 // Holds associated interface binders added by
399 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has
400 // been initialized.
401 base::Lock pending_io_thread_interfaces_lock_;
rockot0e4de5f2016-07-22 21:18:07402 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>>
rockot70bbb59492017-01-25 00:56:51403 pending_io_thread_interfaces_;
initial.commit09911bf2008-07-26 23:55:29404 };
405
[email protected]17571642013-06-01 04:11:27406 Context* context() { return context_.get(); }
initial.commit09911bf2008-07-26 23:55:29407
mbarbellae97877bc2015-03-06 21:51:14408#if defined(ENABLE_IPC_FUZZER)
409 OutgoingMessageFilter* outgoing_message_filter() const {
410 return outgoing_message_filter_;
411 }
412#endif
413
rockot29ade1b2015-08-07 06:23:59414 bool did_init() const { return did_init_; }
415
John Abd-El-Malekd1d6f6a2017-07-18 20:01:00416 // A Send() which doesn't DCHECK if the message is synchronous.
Etienne Bergeron0a07d73d2019-06-27 16:40:47417 void SendInternal(Message* message);
John Abd-El-Malekd1d6f6a2017-07-18 20:01:00418
initial.commit09911bf2008-07-26 23:55:29419 private:
nick4c8dfd42014-11-14 04:11:49420 friend class IpcSecurityTestUtil;
[email protected]163951422009-07-28 22:09:45421
rockot8d890f62016-07-14 16:37:14422 template <typename Interface>
Julie Jeongeun Kima6c8e672019-12-07 03:27:05423 static void BindPendingAssociatedReceiver(
rockot8d890f62016-07-14 16:37:14424 const AssociatedInterfaceFactory<Interface>& factory,
425 mojo::ScopedInterfaceEndpointHandle handle) {
Julie Jeongeun Kima6c8e672019-12-07 03:27:05426 factory.Run(mojo::PendingAssociatedReceiver<Interface>(std::move(handle)));
rockot8d890f62016-07-14 16:37:14427 }
428
rockot29ade1b2015-08-07 06:23:59429 // Always called once immediately after Init.
430 virtual void OnChannelInit();
431
initial.commit09911bf2008-07-26 23:55:29432 // By maintaining this indirection (ref-counted) to our internal state, we
433 // can safely be destroyed while the background thread continues to do stuff
434 // that involves this data.
435 scoped_refptr<Context> context_;
[email protected]89480312011-05-04 17:14:16436
[email protected]952394af2011-11-16 01:06:46437 // Whether the channel has been initialized.
438 bool did_init_;
mbarbellae97877bc2015-03-06 21:51:14439
440#if defined(ENABLE_IPC_FUZZER)
441 OutgoingMessageFilter* outgoing_message_filter_;
442#endif
gab21edbfa2017-05-31 16:49:12443
444 SEQUENCE_CHECKER(sequence_checker_);
initial.commit09911bf2008-07-26 23:55:29445};
446
447} // namespace IPC
448
[email protected]92bf9062011-05-02 18:00:49449#endif // IPC_IPC_CHANNEL_PROXY_H_