blob: 8dbc23e3125008bdbce172be70d7fd9a2d46b3c2 [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/scoped_vector.h"
18#include "base/memory/weak_ptr.h"
rockot37e7fa42016-07-20 13:28:32 +090019#include "base/single_thread_task_runner.h"
rockot6e3d4922016-03-23 10:32:18 +090020#include "base/synchronization/lock.h"
21#include "base/task_runner.h"
rockot37e7fa42016-07-20 13:28:32 +090022#include "base/threading/thread_task_runner_handle.h"
avi42ebda42015-12-22 11:39:04 +090023#include "build/build_config.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"
rockotaf32acb2015-11-13 10:33:59 +090029#include "mojo/public/cpp/system/core.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090030
morrita@chromium.org15996aa2014-08-05 08:44:17 +090031namespace IPC {
32
sammc6194d972016-03-08 07:38:04 +090033// Mojo-based IPC::Channel implementation over a Mojo message pipe.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090034//
sammc0bcbfc72016-03-10 15:28:35 +090035// ChannelMojo builds a Mojo MessagePipe using the provided message pipe
36// |handle| and builds an associated interface for each direction on the
37// channel.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090038//
morrita@chromium.org15996aa2014-08-05 08:44:17 +090039// TODO(morrita): Add APIs to create extra MessagePipes to let
40// Mojo-based objects talk over this Channel.
41//
amistry6555e692016-06-23 16:52:37 +090042class IPC_EXPORT ChannelMojo
morrita1f42dc62014-11-26 08:35:57 +090043 : public Channel,
rockota6edf832016-07-14 09:34:11 +090044 public Channel::AssociatedInterfaceSupport,
rockota01dbc72016-07-23 06:18:07 +090045 public NON_EXPORTED_BASE(MojoBootstrap::Delegate),
morrita1f42dc62014-11-26 08:35:57 +090046 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090047 public:
sammc0bcbfc72016-03-10 15:28:35 +090048 // Creates a ChannelMojo.
danakjc3fb6c52016-04-23 13:21:09 +090049 static std::unique_ptr<ChannelMojo>
rockot37e7fa42016-07-20 13:28:32 +090050 Create(mojo::ScopedMessagePipeHandle handle,
51 Mode mode,
52 Listener* listener,
53 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner =
54 base::ThreadTaskRunnerHandle::Get());
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,
61 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita7c48ab82014-09-24 06:16:00 +090062
danakjc3fb6c52016-04-23 13:21:09 +090063 static std::unique_ptr<ChannelFactory> CreateClientFactory(
rockot37e7fa42016-07-20 13:28:32 +090064 mojo::ScopedMessagePipeHandle handle,
65 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090066
dchengef7721a2014-10-22 11:29:52 +090067 ~ChannelMojo() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090068
69 // Channel implementation
dchengef7721a2014-10-22 11:29:52 +090070 bool Connect() override;
rockote7c47902016-09-09 03:24:56 +090071 void Pause() override;
rockot685505b2016-09-07 03:35:57 +090072 void Unpause(bool flush) override;
73 void Flush() override;
dchengef7721a2014-10-22 11:29:52 +090074 void Close() override;
75 bool Send(Message* message) override;
rockot6e3d4922016-03-23 10:32:18 +090076 bool IsSendThreadSafe() const override;
dchengef7721a2014-10-22 11:29:52 +090077 base::ProcessId GetPeerPID() const override;
78 base::ProcessId GetSelfPID() const override;
rockota6edf832016-07-14 09:34:11 +090079 Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090080
amistry8b1b9e62016-06-06 12:20:49 +090081#if defined(OS_POSIX) && !defined(OS_NACL_SFI)
dchengef7721a2014-10-22 11:29:52 +090082 int GetClientFileDescriptor() const override;
83 base::ScopedFD TakeClientFileDescriptor() override;
amistry8b1b9e62016-06-06 12:20:49 +090084#endif // defined(OS_POSIX) && !defined(OS_NACL_SFI)
morrita42bda252014-09-12 04:06:29 +090085
86 // These access protected API of IPC::Message, which has ChannelMojo
87 // as a friend class.
morrita33a35902015-01-15 06:17:06 +090088 static MojoResult WriteToMessageAttachmentSet(
yzshen8af798c2016-08-24 10:10:13 +090089 base::Optional<std::vector<mojom::SerializedHandlePtr>> handle_buffer,
morrita42bda252014-09-12 04:06:29 +090090 Message* message);
morrita33a35902015-01-15 06:17:06 +090091 static MojoResult ReadFromMessageAttachmentSet(
92 Message* message,
yzshen8af798c2016-08-24 10:10:13 +090093 base::Optional<std::vector<mojom::SerializedHandlePtr>>* handles);
morrita42bda252014-09-12 04:06:29 +090094
morrita7c48ab82014-09-24 06:16:00 +090095 // MojoBootstrapDelegate implementation
rockota01dbc72016-07-23 06:18:07 +090096 void OnPipesAvailable(mojom::ChannelAssociatedPtr sender,
97 mojom::ChannelAssociatedRequest receiver) override;
98
99 // MessagePipeReader::Delegate
100 void OnPeerPidReceived() override;
101 void OnMessageReceived(const Message& message) override;
102 void OnPipeError() override;
rockota6edf832016-07-14 09:34:11 +0900103 void OnAssociatedInterfaceRequest(
104 const std::string& name,
105 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita7c48ab82014-09-24 06:16:00 +0900106
sammc6194d972016-03-08 07:38:04 +0900107 private:
rockot37e7fa42016-07-20 13:28:32 +0900108 ChannelMojo(
109 mojo::ScopedMessagePipeHandle handle,
110 Mode mode,
111 Listener* listener,
112 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita42bda252014-09-12 04:06:29 +0900113
rockota6edf832016-07-14 09:34:11 +0900114 // Channel::AssociatedInterfaceSupport:
115 mojo::AssociatedGroup* GetAssociatedGroup() override;
116 void AddGenericAssociatedInterface(
117 const std::string& name,
118 const GenericAssociatedInterfaceFactory& factory) override;
119 void GetGenericRemoteAssociatedInterface(
120 const std::string& name,
121 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900122
rockot6e3d4922016-03-23 10:32:18 +0900123 // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
124 scoped_refptr<base::TaskRunner> task_runner_;
125
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_