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; |
rockot | 685505b | 2016-09-07 03:35:57 +0900 | [diff] [blame^] | 71 | bool ConnectPaused() override; |
| 72 | void Unpause(bool flush) override; |
| 73 | void Flush() override; |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 74 | void Close() override; |
| 75 | bool Send(Message* message) override; |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 76 | bool IsSendThreadSafe() const override; |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 77 | base::ProcessId GetPeerPID() const override; |
| 78 | base::ProcessId GetSelfPID() const override; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 79 | Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 80 | |
amistry | 8b1b9e6 | 2016-06-06 12:20:49 +0900 | [diff] [blame] | 81 | #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 82 | int GetClientFileDescriptor() const override; |
| 83 | base::ScopedFD TakeClientFileDescriptor() override; |
amistry | 8b1b9e6 | 2016-06-06 12:20:49 +0900 | [diff] [blame] | 84 | #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 85 | |
| 86 | // These access protected API of IPC::Message, which has ChannelMojo |
| 87 | // as a friend class. |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 88 | static MojoResult WriteToMessageAttachmentSet( |
yzshen | 8af798c | 2016-08-24 10:10:13 +0900 | [diff] [blame] | 89 | base::Optional<std::vector<mojom::SerializedHandlePtr>> handle_buffer, |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 90 | Message* message); |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 91 | static MojoResult ReadFromMessageAttachmentSet( |
| 92 | Message* message, |
yzshen | 8af798c | 2016-08-24 10:10:13 +0900 | [diff] [blame] | 93 | base::Optional<std::vector<mojom::SerializedHandlePtr>>* handles); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 94 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 95 | // MojoBootstrapDelegate implementation |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 96 | 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; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 103 | void OnAssociatedInterfaceRequest( |
| 104 | const std::string& name, |
| 105 | mojo::ScopedInterfaceEndpointHandle handle) override; |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 106 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 107 | private: |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 108 | ChannelMojo( |
| 109 | mojo::ScopedMessagePipeHandle handle, |
| 110 | Mode mode, |
| 111 | Listener* listener, |
| 112 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 113 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 114 | // 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.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 122 | |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 123 | // A TaskRunner which runs tasks on the ChannelMojo's owning thread. |
| 124 | scoped_refptr<base::TaskRunner> task_runner_; |
| 125 | |
| 126 | const mojo::MessagePipeHandle pipe_; |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 127 | std::unique_ptr<MojoBootstrap> bootstrap_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 128 | Listener* listener_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 129 | |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 130 | std::unique_ptr<internal::MessagePipeReader> message_reader_; |
| 131 | |
| 132 | base::Lock associated_interface_lock_; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 133 | std::map<std::string, GenericAssociatedInterfaceFactory> |
| 134 | associated_interfaces_; |
| 135 | |
anujk.sharma | 8461adf | 2014-08-28 15:49:02 +0900 | [diff] [blame] | 136 | base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 137 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 139 | }; |
| 140 | |
| 141 | } // namespace IPC |
| 142 | |
| 143 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |