blob: ba769f8afe7ba0bf822bd1b25483a163988a2983 [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"
rockota628d0b2017-02-09 08:40:1529#include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
rockot85dce0862015-11-13 01:33:5930#include "mojo/public/cpp/system/core.h"
[email protected]64860882014-08-04 23:44:1731
[email protected]64860882014-08-04 23:44:1732namespace IPC {
33
sammce4d0abd2016-03-07 22:38:0434// Mojo-based IPC::Channel implementation over a Mojo message pipe.
[email protected]64860882014-08-04 23:44:1735//
sammc57ed9f982016-03-10 06:28:3536// ChannelMojo builds a Mojo MessagePipe using the provided message pipe
37// |handle| and builds an associated interface for each direction on the
38// channel.
[email protected]64860882014-08-04 23:44:1739//
[email protected]64860882014-08-04 23:44:1740// TODO(morrita): Add APIs to create extra MessagePipes to let
41// Mojo-based objects talk over this Channel.
42//
Ken Rockot3044d212018-01-23 02:44:3943class COMPONENT_EXPORT(IPC) ChannelMojo
44 : public Channel,
45 public Channel::AssociatedInterfaceSupport,
46 public internal::MessagePipeReader::Delegate {
[email protected]64860882014-08-04 23:44:1747 public:
sammc57ed9f982016-03-10 06:28:3548 // Creates a ChannelMojo.
Hajime Hoshia98f1102017-11-20 06:34:3549 static std::unique_ptr<ChannelMojo> Create(
50 mojo::ScopedMessagePipeHandle handle,
51 Mode mode,
52 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:1353 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
54 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
[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,
104 const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
morrita3b41d6c2014-09-11 19:06:29105
rockota628d0b2017-02-09 08:40:15106 void ForwardMessageFromThreadSafePtr(mojo::Message message);
107 void ForwardMessageWithResponderFromThreadSafePtr(
108 mojo::Message message,
109 std::unique_ptr<mojo::MessageReceiver> responder);
110
rockot7c6bf952016-07-14 00:34:11111 // Channel::AssociatedInterfaceSupport:
rockota628d0b2017-02-09 08:40:15112 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
113 CreateThreadSafeChannel() override;
rockot7c6bf952016-07-14 00:34:11114 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;
[email protected]64860882014-08-04 23:44:17120
Wez888ef822017-10-30 16:46:06121 base::WeakPtr<ChannelMojo> weak_ptr_;
122
rockot506f92fa22016-03-23 01:32:18123 // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
rockota628d0b2017-02-09 08:40:15124 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
rockot506f92fa22016-03-23 01:32:18125
126 const mojo::MessagePipeHandle pipe_;
danakj03de39b22016-04-23 04:21:09127 std::unique_ptr<MojoBootstrap> bootstrap_;
[email protected]64860882014-08-04 23:44:17128 Listener* listener_;
[email protected]64860882014-08-04 23:44:17129
rockot0e4de5f2016-07-22 21:18:07130 std::unique_ptr<internal::MessagePipeReader> message_reader_;
131
132 base::Lock associated_interface_lock_;
rockot7c6bf952016-07-14 00:34:11133 std::map<std::string, GenericAssociatedInterfaceFactory>
134 associated_interfaces_;
135
anujk.sharma0184ced2014-08-28 06:49:02136 base::WeakPtrFactory<ChannelMojo> weak_factory_;
137
[email protected]64860882014-08-04 23:44:17138 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
139};
140
141} // namespace IPC
142
143#endif // IPC_IPC_CHANNEL_MOJO_H_