blob: cfadb50adc27bb1ac920cd8f3b47319b1cc2d888 [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;
71 void Close() override;
72 bool Send(Message* message) override;
rockot6e3d4922016-03-23 10:32:18 +090073 bool IsSendThreadSafe() const override;
dchengef7721a2014-10-22 11:29:52 +090074 base::ProcessId GetPeerPID() const override;
75 base::ProcessId GetSelfPID() const override;
rockota6edf832016-07-14 09:34:11 +090076 Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090077
amistry8b1b9e62016-06-06 12:20:49 +090078#if defined(OS_POSIX) && !defined(OS_NACL_SFI)
dchengef7721a2014-10-22 11:29:52 +090079 int GetClientFileDescriptor() const override;
80 base::ScopedFD TakeClientFileDescriptor() override;
amistry8b1b9e62016-06-06 12:20:49 +090081#endif // defined(OS_POSIX) && !defined(OS_NACL_SFI)
morrita42bda252014-09-12 04:06:29 +090082
83 // These access protected API of IPC::Message, which has ChannelMojo
84 // as a friend class.
morrita33a35902015-01-15 06:17:06 +090085 static MojoResult WriteToMessageAttachmentSet(
sammc0bcbfc72016-03-10 15:28:35 +090086 mojo::Array<mojom::SerializedHandlePtr> handle_buffer,
morrita42bda252014-09-12 04:06:29 +090087 Message* message);
morrita33a35902015-01-15 06:17:06 +090088 static MojoResult ReadFromMessageAttachmentSet(
89 Message* message,
sammc0bcbfc72016-03-10 15:28:35 +090090 mojo::Array<mojom::SerializedHandlePtr>* handles);
morrita42bda252014-09-12 04:06:29 +090091
morrita7c48ab82014-09-24 06:16:00 +090092 // MojoBootstrapDelegate implementation
rockota01dbc72016-07-23 06:18:07 +090093 void OnPipesAvailable(mojom::ChannelAssociatedPtr sender,
94 mojom::ChannelAssociatedRequest receiver) override;
95
96 // MessagePipeReader::Delegate
97 void OnPeerPidReceived() override;
98 void OnMessageReceived(const Message& message) override;
99 void OnPipeError() override;
rockota6edf832016-07-14 09:34:11 +0900100 void OnAssociatedInterfaceRequest(
101 const std::string& name,
102 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita7c48ab82014-09-24 06:16:00 +0900103
sammc6194d972016-03-08 07:38:04 +0900104 private:
rockot37e7fa42016-07-20 13:28:32 +0900105 ChannelMojo(
106 mojo::ScopedMessagePipeHandle handle,
107 Mode mode,
108 Listener* listener,
109 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita42bda252014-09-12 04:06:29 +0900110
rockota6edf832016-07-14 09:34:11 +0900111 // Channel::AssociatedInterfaceSupport:
112 mojo::AssociatedGroup* GetAssociatedGroup() override;
113 void AddGenericAssociatedInterface(
114 const std::string& name,
115 const GenericAssociatedInterfaceFactory& factory) override;
116 void GetGenericRemoteAssociatedInterface(
117 const std::string& name,
118 mojo::ScopedInterfaceEndpointHandle handle) override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900119
rockot6e3d4922016-03-23 10:32:18 +0900120 // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
121 scoped_refptr<base::TaskRunner> task_runner_;
122
123 const mojo::MessagePipeHandle pipe_;
danakjc3fb6c52016-04-23 13:21:09 +0900124 std::unique_ptr<MojoBootstrap> bootstrap_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900125 Listener* listener_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900126
rockota01dbc72016-07-23 06:18:07 +0900127 std::unique_ptr<internal::MessagePipeReader> message_reader_;
128
129 base::Lock associated_interface_lock_;
rockota6edf832016-07-14 09:34:11 +0900130 std::map<std::string, GenericAssociatedInterfaceFactory>
131 associated_interfaces_;
132
anujk.sharma8461adf2014-08-28 15:49:02 +0900133 base::WeakPtrFactory<ChannelMojo> weak_factory_;
134
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900135 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
136};
137
138} // namespace IPC
139
140#endif // IPC_IPC_CHANNEL_MOJO_H_