blob: d94b6376c68fa68e96190a5eead8216dd747ff7e [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// 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
avi246998d82015-12-22 02:39:048#include <stdint.h>
9
rockot7c6bf952016-07-14 00:34:1110#include <map>
danakj03de39b22016-04-23 04:21:0911#include <memory>
rockot7c6bf952016-07-14 00:34:1112#include <string>
[email protected]64860882014-08-04 23:44:1713#include <vector>
14
Ken Rockot3044d212018-01-23 02:44:3915#include "base/component_export.h"
avi246998d82015-12-22 02:39:0416#include "base/macros.h"
rockot506f92fa22016-03-23 01:32:1817#include "base/memory/ref_counted.h"
[email protected]64860882014-08-04 23:44:1718#include "base/memory/weak_ptr.h"
rockota34707ca2016-07-20 04:28:3219#include "base/single_thread_task_runner.h"
rockot506f92fa22016-03-23 01:32:1820#include "base/synchronization/lock.h"
21#include "base/task_runner.h"
rockota34707ca2016-07-20 04:28:3222#include "base/threading/thread_task_runner_handle.h"
avi246998d82015-12-22 02:39:0423#include "build/build_config.h"
rockota628d0b2017-02-09 08:40:1524#include "ipc/ipc.mojom.h"
[email protected]64860882014-08-04 23:44:1725#include "ipc/ipc_channel.h"
26#include "ipc/ipc_channel_factory.h"
amistryd4aa70d2016-06-23 07:52:3727#include "ipc/ipc_message_pipe_reader.h"
28#include "ipc/ipc_mojo_bootstrap.h"
rockot85dce0862015-11-13 01:33:5929#include "mojo/public/cpp/system/core.h"
[email protected]64860882014-08-04 23:44:1730
[email protected]64860882014-08-04 23:44:1731namespace IPC {
32
sammce4d0abd2016-03-07 22:38:0433// Mojo-based IPC::Channel implementation over a Mojo message pipe.
[email protected]64860882014-08-04 23:44:1734//
sammc57ed9f982016-03-10 06:28:3535// ChannelMojo builds a Mojo MessagePipe using the provided message pipe
36// |handle| and builds an associated interface for each direction on the
37// channel.
[email protected]64860882014-08-04 23:44:1738//
[email protected]64860882014-08-04 23:44:1739// TODO(morrita): Add APIs to create extra MessagePipes to let
40// Mojo-based objects talk over this Channel.
41//
Ken Rockot3044d212018-01-23 02:44:3942class COMPONENT_EXPORT(IPC) ChannelMojo
43 : public Channel,
44 public Channel::AssociatedInterfaceSupport,
45 public internal::MessagePipeReader::Delegate {
[email protected]64860882014-08-04 23:44:1746 public:
sammc57ed9f982016-03-10 06:28:3547 // Creates a ChannelMojo.
Hajime Hoshia98f1102017-11-20 06:34:3548 static std::unique_ptr<ChannelMojo> Create(
49 mojo::ScopedMessagePipeHandle handle,
50 Mode mode,
51 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:1352 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2053 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner,
54 const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker);
[email protected]64860882014-08-04 23:44:1755
56 // Create a factory object for ChannelMojo.
57 // The factory is used to create Mojo-based ChannelProxy family.
morrita54f6f80c2014-09-23 21:16:0058 // |host| must not be null.
danakj03de39b22016-04-23 04:21:0959 static std::unique_ptr<ChannelFactory> CreateServerFactory(
rockota34707ca2016-07-20 04:28:3260 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshia98f1102017-11-20 06:34:3561 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
62 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
morrita54f6f80c2014-09-23 21:16:0063
danakj03de39b22016-04-23 04:21:0964 static std::unique_ptr<ChannelFactory> CreateClientFactory(
rockota34707ca2016-07-20 04:28:3265 mojo::ScopedMessagePipeHandle handle,
Hajime Hoshia98f1102017-11-20 06:34:3566 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
67 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
[email protected]64860882014-08-04 23:44:1768
dchengfe61fca2014-10-22 02:29:5269 ~ChannelMojo() override;
[email protected]64860882014-08-04 23:44:1770
71 // Channel implementation
dchengfe61fca2014-10-22 02:29:5272 bool Connect() override;
rockot10188752016-09-08 18:24:5673 void Pause() override;
rockot401fb2c2016-09-06 18:35:5774 void Unpause(bool flush) override;
75 void Flush() override;
dchengfe61fca2014-10-22 02:29:5276 void Close() override;
77 bool Send(Message* message) override;
rockot7c6bf952016-07-14 00:34:1178 Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
[email protected]64860882014-08-04 23:44:1779
morrita3b41d6c2014-09-11 19:06:2980 // These access protected API of IPC::Message, which has ChannelMojo
81 // as a friend class.
morrita4b5c28e22015-01-14 21:17:0682 static MojoResult WriteToMessageAttachmentSet(
Eve Martin-Jones475e7e62018-02-13 22:57:2583 base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
morrita3b41d6c2014-09-11 19:06:2984 Message* message);
morrita4b5c28e22015-01-14 21:17:0685 static MojoResult ReadFromMessageAttachmentSet(
86 Message* message,
Eve Martin-Jones475e7e62018-02-13 22:57:2587 base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles);
morrita3b41d6c2014-09-11 19:06:2988
rockot0e4de5f2016-07-22 21:18:0789 // MessagePipeReader::Delegate
sammcf810f07f2016-11-10 22:34:0790 void OnPeerPidReceived(int32_t peer_pid) override;
rockot0e4de5f2016-07-22 21:18:0791 void OnMessageReceived(const Message& message) override;
Roman Karaseva43d5b4e2017-12-21 03:06:0292 void OnBrokenDataReceived() override;
rockot0e4de5f2016-07-22 21:18:0793 void OnPipeError() override;
rockot7c6bf952016-07-14 00:34:1194 void OnAssociatedInterfaceRequest(
95 const std::string& name,
96 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita54f6f80c2014-09-23 21:16:0097
sammce4d0abd2016-03-07 22:38:0498 private:
rockota34707ca2016-07-20 04:28:3299 ChannelMojo(
100 mojo::ScopedMessagePipeHandle handle,
101 Mode mode,
102 Listener* listener,
Hajime Hoshia98f1102017-11-20 06:34:35103 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:20104 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner,
105 const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker);
morrita3b41d6c2014-09-11 19:06:29106
rockota628d0b2017-02-09 08:40:15107 void ForwardMessageFromThreadSafePtr(mojo::Message message);
108 void ForwardMessageWithResponderFromThreadSafePtr(
109 mojo::Message message,
110 std::unique_ptr<mojo::MessageReceiver> responder);
111
rockot7c6bf952016-07-14 00:34:11112 // Channel::AssociatedInterfaceSupport:
rockota628d0b2017-02-09 08:40:15113 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
114 CreateThreadSafeChannel() override;
rockot7c6bf952016-07-14 00:34:11115 void AddGenericAssociatedInterface(
116 const std::string& name,
117 const GenericAssociatedInterfaceFactory& factory) override;
118 void GetGenericRemoteAssociatedInterface(
119 const std::string& name,
120 mojo::ScopedInterfaceEndpointHandle handle) override;
[email protected]64860882014-08-04 23:44:17121
Wez888ef822017-10-30 16:46:06122 base::WeakPtr<ChannelMojo> weak_ptr_;
123
rockot506f92fa22016-03-23 01:32:18124 // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
rockota628d0b2017-02-09 08:40:15125 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
rockot506f92fa22016-03-23 01:32:18126
127 const mojo::MessagePipeHandle pipe_;
danakj03de39b22016-04-23 04:21:09128 std::unique_ptr<MojoBootstrap> bootstrap_;
[email protected]64860882014-08-04 23:44:17129 Listener* listener_;
[email protected]64860882014-08-04 23:44:17130
rockot0e4de5f2016-07-22 21:18:07131 std::unique_ptr<internal::MessagePipeReader> message_reader_;
132
133 base::Lock associated_interface_lock_;
rockot7c6bf952016-07-14 00:34:11134 std::map<std::string, GenericAssociatedInterfaceFactory>
135 associated_interfaces_;
136
Jeremy Roman7c5cfabd2019-08-12 15:45:27137 base::WeakPtrFactory<ChannelMojo> weak_factory_{this};
anujk.sharma0184ced2014-08-28 06:49:02138
[email protected]64860882014-08-04 23:44:17139 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
140};
141
142} // namespace IPC
143
144#endif // IPC_IPC_CHANNEL_MOJO_H_