morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 1 | // 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 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 10 | #include <map> |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 11 | #include <memory> |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 12 | #include <string> |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 15 | #include "base/macros.h" |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 17 | #include "base/memory/scoped_vector.h" |
| 18 | #include "base/memory/weak_ptr.h" |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 19 | #include "base/single_thread_task_runner.h" |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 20 | #include "base/synchronization/lock.h" |
| 21 | #include "base/task_runner.h" |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 23 | #include "build/build_config.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 24 | #include "ipc/ipc_channel.h" |
| 25 | #include "ipc/ipc_channel_factory.h" |
| 26 | #include "ipc/ipc_export.h" |
amistry | 6555e69 | 2016-06-23 16:52:37 +0900 | [diff] [blame] | 27 | #include "ipc/ipc_message_pipe_reader.h" |
| 28 | #include "ipc/ipc_mojo_bootstrap.h" |
rockot | af32acb | 2015-11-13 10:33:59 +0900 | [diff] [blame] | 29 | #include "mojo/public/cpp/system/core.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 30 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 31 | namespace IPC { |
| 32 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 33 | // Mojo-based IPC::Channel implementation over a Mojo message pipe. |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 34 | // |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 35 | // 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.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 38 | // |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 39 | // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 40 | // Mojo-based objects talk over this Channel. |
| 41 | // |
amistry | 6555e69 | 2016-06-23 16:52:37 +0900 | [diff] [blame] | 42 | class IPC_EXPORT ChannelMojo |
morrita | 1f42dc6 | 2014-11-26 08:35:57 +0900 | [diff] [blame] | 43 | : public Channel, |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 44 | public Channel::AssociatedInterfaceSupport, |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame^] | 45 | public NON_EXPORTED_BASE(MojoBootstrap::Delegate), |
morrita | 1f42dc6 | 2014-11-26 08:35:57 +0900 | [diff] [blame] | 46 | public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 47 | public: |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 48 | // Creates a ChannelMojo. |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 49 | static std::unique_ptr<ChannelMojo> |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 50 | 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.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 55 | |
| 56 | // Create a factory object for ChannelMojo. |
| 57 | // The factory is used to create Mojo-based ChannelProxy family. |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 58 | // |host| must not be null. |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 59 | static std::unique_ptr<ChannelFactory> CreateServerFactory( |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 60 | mojo::ScopedMessagePipeHandle handle, |
| 61 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 62 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 63 | static std::unique_ptr<ChannelFactory> CreateClientFactory( |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 64 | mojo::ScopedMessagePipeHandle handle, |
| 65 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 66 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 67 | ~ChannelMojo() override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 68 | |
| 69 | // Channel implementation |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 70 | bool Connect() override; |
| 71 | void Close() override; |
| 72 | bool Send(Message* message) override; |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 73 | bool IsSendThreadSafe() const override; |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 74 | base::ProcessId GetPeerPID() const override; |
| 75 | base::ProcessId GetSelfPID() const override; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 76 | Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 77 | |
amistry | 8b1b9e6 | 2016-06-06 12:20:49 +0900 | [diff] [blame] | 78 | #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 79 | int GetClientFileDescriptor() const override; |
| 80 | base::ScopedFD TakeClientFileDescriptor() override; |
amistry | 8b1b9e6 | 2016-06-06 12:20:49 +0900 | [diff] [blame] | 81 | #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 82 | |
| 83 | // These access protected API of IPC::Message, which has ChannelMojo |
| 84 | // as a friend class. |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 85 | static MojoResult WriteToMessageAttachmentSet( |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 86 | mojo::Array<mojom::SerializedHandlePtr> handle_buffer, |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 87 | Message* message); |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 88 | static MojoResult ReadFromMessageAttachmentSet( |
| 89 | Message* message, |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 90 | mojo::Array<mojom::SerializedHandlePtr>* handles); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 91 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 92 | // MojoBootstrapDelegate implementation |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame^] | 93 | 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; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 100 | void OnAssociatedInterfaceRequest( |
| 101 | const std::string& name, |
| 102 | mojo::ScopedInterfaceEndpointHandle handle) override; |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 103 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 104 | private: |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 105 | ChannelMojo( |
| 106 | mojo::ScopedMessagePipeHandle handle, |
| 107 | Mode mode, |
| 108 | Listener* listener, |
| 109 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 110 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 111 | // 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.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 119 | |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 120 | // A TaskRunner which runs tasks on the ChannelMojo's owning thread. |
| 121 | scoped_refptr<base::TaskRunner> task_runner_; |
| 122 | |
| 123 | const mojo::MessagePipeHandle pipe_; |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 124 | std::unique_ptr<MojoBootstrap> bootstrap_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 125 | Listener* listener_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 126 | |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame^] | 127 | std::unique_ptr<internal::MessagePipeReader> message_reader_; |
| 128 | |
| 129 | base::Lock associated_interface_lock_; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 130 | std::map<std::string, GenericAssociatedInterfaceFactory> |
| 131 | associated_interfaces_; |
| 132 | |
anujk.sharma | 8461adf | 2014-08-28 15:49:02 +0900 | [diff] [blame] | 133 | base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 134 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 136 | }; |
| 137 | |
| 138 | } // namespace IPC |
| 139 | |
| 140 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |