blob: aa6ba4355689c5f47a85450ed717ace7061e9422 [file] [log] [blame]
morrita@chromium.org15996aa2014-08-05 08:44:17 +09001// 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
5#ifndef IPC_IPC_CHANNEL_MOJO_H_
6#define IPC_IPC_CHANNEL_MOJO_H_
7
avi42ebda42015-12-22 11:39:04 +09008#include <stdint.h>
9
rockota6edf832016-07-14 09:34:11 +090010#include <map>
danakjc3fb6c52016-04-23 13:21:09 +090011#include <memory>
rockota6edf832016-07-14 09:34:11 +090012#include <string>
morrita@chromium.org15996aa2014-08-05 08:44:17 +090013#include <vector>
14
avi42ebda42015-12-22 11:39:04 +090015#include "base/macros.h"
rockot6e3d4922016-03-23 10:32:18 +090016#include "base/memory/ref_counted.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090017#include "base/memory/weak_ptr.h"
rockot37e7fa42016-07-20 13:28:32 +090018#include "base/single_thread_task_runner.h"
rockot6e3d4922016-03-23 10:32:18 +090019#include "base/synchronization/lock.h"
20#include "base/task_runner.h"
rockot37e7fa42016-07-20 13:28:32 +090021#include "base/threading/thread_task_runner_handle.h"
avi42ebda42015-12-22 11:39:04 +090022#include "build/build_config.h"
rockot7900d342017-02-09 17:40:15 +090023#include "ipc/ipc.mojom.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090024#include "ipc/ipc_channel.h"
25#include "ipc/ipc_channel_factory.h"
26#include "ipc/ipc_export.h"
amistry6555e692016-06-23 16:52:37 +090027#include "ipc/ipc_message_pipe_reader.h"
28#include "ipc/ipc_mojo_bootstrap.h"
rockot7900d342017-02-09 17:40:15 +090029#include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
rockotaf32acb2015-11-13 10:33:59 +090030#include "mojo/public/cpp/system/core.h"
Ken Rockotb3a77c92017-09-14 13:23:41 +090031#include "mojo/public/interfaces/bindings/native_struct.mojom.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090032
morrita@chromium.org15996aa2014-08-05 08:44:17 +090033namespace IPC {
34
sammc6194d972016-03-08 07:38:04 +090035// Mojo-based IPC::Channel implementation over a Mojo message pipe.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090036//
sammc0bcbfc72016-03-10 15:28:35 +090037// ChannelMojo builds a Mojo MessagePipe using the provided message pipe
38// |handle| and builds an associated interface for each direction on the
39// channel.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090040//
morrita@chromium.org15996aa2014-08-05 08:44:17 +090041// TODO(morrita): Add APIs to create extra MessagePipes to let
42// Mojo-based objects talk over this Channel.
43//
Nico Weber1615c312017-08-16 04:19:27 +090044class IPC_EXPORT ChannelMojo : public Channel,
45 public Channel::AssociatedInterfaceSupport,
46 public internal::MessagePipeReader::Delegate {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090047 public:
sammc0bcbfc72016-03-10 15:28:35 +090048 // Creates a ChannelMojo.
Hajime Hoshi595c8562017-11-20 15:34:35 +090049 static std::unique_ptr<ChannelMojo> Create(
50 mojo::ScopedMessagePipeHandle handle,
51 Mode mode,
52 Listener* listener,
Hajime Hoshi38161442017-11-28 12:34:13 +090053 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
54 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090055
56 // Create a factory object for ChannelMojo.
57 // The factory is used to create Mojo-based ChannelProxy family.
morrita7c48ab82014-09-24 06:16:00 +090058 // |host| must not be null.
danakjc3fb6c52016-04-23 13:21:09 +090059 static std::unique_ptr<ChannelFactory> CreateServerFactory(
rockot37e7fa42016-07-20 13:28:32 +090060 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshi595c8562017-11-20 15:34:35 +090061 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
62 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
morrita7c48ab82014-09-24 06:16:00 +090063
danakjc3fb6c52016-04-23 13:21:09 +090064 static std::unique_ptr<ChannelFactory> CreateClientFactory(
rockot37e7fa42016-07-20 13:28:32 +090065 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshi595c8562017-11-20 15:34:35 +090066 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
67 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090068
dchengef7721a2014-10-22 11:29:52 +090069 ~ChannelMojo() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090070
71 // Channel implementation
dchengef7721a2014-10-22 11:29:52 +090072 bool Connect() override;
rockote7c47902016-09-09 03:24:56 +090073 void Pause() override;
rockot685505b2016-09-07 03:35:57 +090074 void Unpause(bool flush) override;
75 void Flush() override;
dchengef7721a2014-10-22 11:29:52 +090076 void Close() override;
77 bool Send(Message* message) override;
rockota6edf832016-07-14 09:34:11 +090078 Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090079
morrita42bda252014-09-12 04:06:29 +090080 // These access protected API of IPC::Message, which has ChannelMojo
81 // as a friend class.
morrita33a35902015-01-15 06:17:06 +090082 static MojoResult WriteToMessageAttachmentSet(
Ken Rockotb3a77c92017-09-14 13:23:41 +090083 base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
morrita42bda252014-09-12 04:06:29 +090084 Message* message);
morrita33a35902015-01-15 06:17:06 +090085 static MojoResult ReadFromMessageAttachmentSet(
86 Message* message,
Ken Rockotb3a77c92017-09-14 13:23:41 +090087 base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles);
morrita42bda252014-09-12 04:06:29 +090088
rockota01dbc72016-07-23 06:18:07 +090089 // MessagePipeReader::Delegate
sammcb83485d2016-11-11 07:34:07 +090090 void OnPeerPidReceived(int32_t peer_pid) override;
rockota01dbc72016-07-23 06:18:07 +090091 void OnMessageReceived(const Message& message) override;
Roman Karasev02dec602017-12-21 12:06:02 +090092 void OnBrokenDataReceived() override;
rockota01dbc72016-07-23 06:18:07 +090093 void OnPipeError() override;
rockota6edf832016-07-14 09:34:11 +090094 void OnAssociatedInterfaceRequest(
95 const std::string& name,
96 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita7c48ab82014-09-24 06:16:00 +090097
sammc6194d972016-03-08 07:38:04 +090098 private:
rockot37e7fa42016-07-20 13:28:32 +090099 ChannelMojo(
100 mojo::ScopedMessagePipeHandle handle,
101 Mode mode,
102 Listener* listener,
Hajime Hoshi595c8562017-11-20 15:34:35 +0900103 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
104 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
morrita42bda252014-09-12 04:06:29 +0900105
rockot7900d342017-02-09 17:40:15 +0900106 void ForwardMessageFromThreadSafePtr(mojo::Message message);
107 void ForwardMessageWithResponderFromThreadSafePtr(
108 mojo::Message message,
109 std::unique_ptr<mojo::MessageReceiver> responder);
110
rockota6edf832016-07-14 09:34:11 +0900111 // Channel::AssociatedInterfaceSupport:
rockot7900d342017-02-09 17:40:15 +0900112 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
113 CreateThreadSafeChannel() override;
rockota6edf832016-07-14 09:34:11 +0900114 void AddGenericAssociatedInterface(
115 const std::string& name,
116 const GenericAssociatedInterfaceFactory& factory) override;
117 void GetGenericRemoteAssociatedInterface(
118 const std::string& name,
119 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900120
Wezd7f09fa2017-10-31 01:46:06 +0900121 base::WeakPtr<ChannelMojo> weak_ptr_;
122
rockot6e3d4922016-03-23 10:32:18 +0900123 // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
rockot7900d342017-02-09 17:40:15 +0900124 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
rockot6e3d4922016-03-23 10:32:18 +0900125
126 const mojo::MessagePipeHandle pipe_;
danakjc3fb6c52016-04-23 13:21:09 +0900127 std::unique_ptr<MojoBootstrap> bootstrap_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900128 Listener* listener_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900129
rockota01dbc72016-07-23 06:18:07 +0900130 std::unique_ptr<internal::MessagePipeReader> message_reader_;
131
132 base::Lock associated_interface_lock_;
rockota6edf832016-07-14 09:34:11 +0900133 std::map<std::string, GenericAssociatedInterfaceFactory>
134 associated_interfaces_;
135
anujk.sharma8461adf2014-08-28 15:49:02 +0900136 base::WeakPtrFactory<ChannelMojo> weak_factory_;
137
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900138 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
139};
140
141} // namespace IPC
142
143#endif // IPC_IPC_CHANNEL_MOJO_H_