| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IPC_IPC_CHANNEL_MOJO_H_ | 5 #ifndef IPC_IPC_CHANNEL_MOJO_H_ |
| 6 #define IPC_IPC_CHANNEL_MOJO_H_ | 6 #define IPC_IPC_CHANNEL_MOJO_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | |
| 11 #include <memory> | 10 #include <memory> |
| 12 #include <string> | |
| 13 #include <vector> | 11 #include <vector> |
| 14 | 12 |
| 15 #include "base/macros.h" | 13 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 18 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 19 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 20 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
| 21 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 22 #include "ipc/ipc_channel.h" | 20 #include "ipc/ipc_channel.h" |
| 23 #include "ipc/ipc_channel_factory.h" | 21 #include "ipc/ipc_channel_factory.h" |
| 24 #include "ipc/ipc_export.h" | 22 #include "ipc/ipc_export.h" |
| 25 #include "ipc/ipc_message_pipe_reader.h" | 23 #include "ipc/ipc_message_pipe_reader.h" |
| 26 #include "ipc/ipc_mojo_bootstrap.h" | 24 #include "ipc/ipc_mojo_bootstrap.h" |
| 27 #include "mojo/public/cpp/system/core.h" | 25 #include "mojo/public/cpp/system/core.h" |
| 28 | 26 |
| 29 namespace IPC { | 27 namespace IPC { |
| 30 | 28 |
| 31 // Mojo-based IPC::Channel implementation over a Mojo message pipe. | 29 // Mojo-based IPC::Channel implementation over a Mojo message pipe. |
| 32 // | 30 // |
| 33 // ChannelMojo builds a Mojo MessagePipe using the provided message pipe | 31 // ChannelMojo builds a Mojo MessagePipe using the provided message pipe |
| 34 // |handle| and builds an associated interface for each direction on the | 32 // |handle| and builds an associated interface for each direction on the |
| 35 // channel. | 33 // channel. |
| 36 // | 34 // |
| 37 // TODO(morrita): Add APIs to create extra MessagePipes to let | 35 // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 38 // Mojo-based objects talk over this Channel. | 36 // Mojo-based objects talk over this Channel. |
| 39 // | 37 // |
| 40 class IPC_EXPORT ChannelMojo | 38 class IPC_EXPORT ChannelMojo |
| 41 : public Channel, | 39 : public Channel, |
| 42 public Channel::AssociatedInterfaceSupport, | |
| 43 public MojoBootstrap::Delegate, | 40 public MojoBootstrap::Delegate, |
| 44 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { | 41 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { |
| 45 public: | 42 public: |
| 46 // Creates a ChannelMojo. | 43 // Creates a ChannelMojo. |
| 47 static std::unique_ptr<ChannelMojo> | 44 static std::unique_ptr<ChannelMojo> |
| 48 Create(mojo::ScopedMessagePipeHandle handle, Mode mode, Listener* listener); | 45 Create(mojo::ScopedMessagePipeHandle handle, Mode mode, Listener* listener); |
| 49 | 46 |
| 50 // Create a factory object for ChannelMojo. | 47 // Create a factory object for ChannelMojo. |
| 51 // The factory is used to create Mojo-based ChannelProxy family. | 48 // The factory is used to create Mojo-based ChannelProxy family. |
| 52 // |host| must not be null. | 49 // |host| must not be null. |
| 53 static std::unique_ptr<ChannelFactory> CreateServerFactory( | 50 static std::unique_ptr<ChannelFactory> CreateServerFactory( |
| 54 mojo::ScopedMessagePipeHandle handle); | 51 mojo::ScopedMessagePipeHandle handle); |
| 55 | 52 |
| 56 static std::unique_ptr<ChannelFactory> CreateClientFactory( | 53 static std::unique_ptr<ChannelFactory> CreateClientFactory( |
| 57 mojo::ScopedMessagePipeHandle handle); | 54 mojo::ScopedMessagePipeHandle handle); |
| 58 | 55 |
| 59 ~ChannelMojo() override; | 56 ~ChannelMojo() override; |
| 60 | 57 |
| 61 // Channel implementation | 58 // Channel implementation |
| 62 bool Connect() override; | 59 bool Connect() override; |
| 63 void Close() override; | 60 void Close() override; |
| 64 bool Send(Message* message) override; | 61 bool Send(Message* message) override; |
| 65 bool IsSendThreadSafe() const override; | 62 bool IsSendThreadSafe() const override; |
| 66 base::ProcessId GetPeerPID() const override; | 63 base::ProcessId GetPeerPID() const override; |
| 67 base::ProcessId GetSelfPID() const override; | 64 base::ProcessId GetSelfPID() const override; |
| 68 Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override; | |
| 69 | 65 |
| 70 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 66 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 71 int GetClientFileDescriptor() const override; | 67 int GetClientFileDescriptor() const override; |
| 72 base::ScopedFD TakeClientFileDescriptor() override; | 68 base::ScopedFD TakeClientFileDescriptor() override; |
| 73 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) | 69 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 74 | 70 |
| 75 // These access protected API of IPC::Message, which has ChannelMojo | 71 // These access protected API of IPC::Message, which has ChannelMojo |
| 76 // as a friend class. | 72 // as a friend class. |
| 77 static MojoResult WriteToMessageAttachmentSet( | 73 static MojoResult WriteToMessageAttachmentSet( |
| 78 mojo::Array<mojom::SerializedHandlePtr> handle_buffer, | 74 mojo::Array<mojom::SerializedHandlePtr> handle_buffer, |
| 79 Message* message); | 75 Message* message); |
| 80 static MojoResult ReadFromMessageAttachmentSet( | 76 static MojoResult ReadFromMessageAttachmentSet( |
| 81 Message* message, | 77 Message* message, |
| 82 mojo::Array<mojom::SerializedHandlePtr>* handles); | 78 mojo::Array<mojom::SerializedHandlePtr>* handles); |
| 83 | 79 |
| 84 // MojoBootstrapDelegate implementation | 80 // MojoBootstrapDelegate implementation |
| 85 void OnPipesAvailable(mojom::ChannelAssociatedPtrInfo send_channel, | 81 void OnPipesAvailable(mojom::ChannelAssociatedPtrInfo send_channel, |
| 86 mojom::ChannelAssociatedRequest receive_channel, | 82 mojom::ChannelAssociatedRequest receive_channel, |
| 87 int32_t peer_pid) override; | 83 int32_t peer_pid) override; |
| 88 void OnBootstrapError() override; | 84 void OnBootstrapError() override; |
| 89 void OnAssociatedInterfaceRequest( | |
| 90 const std::string& name, | |
| 91 mojo::ScopedInterfaceEndpointHandle handle) override; | |
| 92 | 85 |
| 93 // MessagePipeReader::Delegate | 86 // MessagePipeReader::Delegate |
| 94 void OnMessageReceived(const Message& message) override; | 87 void OnMessageReceived(const Message& message) override; |
| 95 void OnPipeError() override; | 88 void OnPipeError() override; |
| 96 | 89 |
| 97 private: | 90 private: |
| 98 ChannelMojo(mojo::ScopedMessagePipeHandle handle, | 91 ChannelMojo(mojo::ScopedMessagePipeHandle handle, |
| 99 Mode mode, | 92 Mode mode, |
| 100 Listener* listener); | 93 Listener* listener); |
| 101 | 94 |
| 102 void InitMessageReader(mojom::ChannelAssociatedPtrInfo sender, | 95 void InitMessageReader(mojom::ChannelAssociatedPtrInfo sender, |
| 103 mojom::ChannelAssociatedRequest receiver, | 96 mojom::ChannelAssociatedRequest receiver, |
| 104 base::ProcessId peer_pid); | 97 base::ProcessId peer_pid); |
| 105 | 98 |
| 106 // Channel::AssociatedInterfaceSupport: | |
| 107 mojo::AssociatedGroup* GetAssociatedGroup() override; | |
| 108 void AddGenericAssociatedInterface( | |
| 109 const std::string& name, | |
| 110 const GenericAssociatedInterfaceFactory& factory) override; | |
| 111 void GetGenericRemoteAssociatedInterface( | |
| 112 const std::string& name, | |
| 113 mojo::ScopedInterfaceEndpointHandle handle) override; | |
| 114 | |
| 115 // ChannelMojo needs to kill its MessagePipeReader in delayed manner | 99 // ChannelMojo needs to kill its MessagePipeReader in delayed manner |
| 116 // because the channel wants to kill these readers during the | 100 // because the channel wants to kill these readers during the |
| 117 // notifications invoked by them. | 101 // notifications invoked by them. |
| 118 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; | 102 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; |
| 119 | 103 |
| 120 // A TaskRunner which runs tasks on the ChannelMojo's owning thread. | 104 // A TaskRunner which runs tasks on the ChannelMojo's owning thread. |
| 121 scoped_refptr<base::TaskRunner> task_runner_; | 105 scoped_refptr<base::TaskRunner> task_runner_; |
| 122 | 106 |
| 123 const mojo::MessagePipeHandle pipe_; | 107 const mojo::MessagePipeHandle pipe_; |
| 124 std::unique_ptr<MojoBootstrap> bootstrap_; | 108 std::unique_ptr<MojoBootstrap> bootstrap_; |
| 125 Listener* listener_; | 109 Listener* listener_; |
| 126 | 110 |
| 127 std::map<std::string, GenericAssociatedInterfaceFactory> | |
| 128 associated_interfaces_; | |
| 129 | |
| 130 // Guards access to the fields below. | 111 // Guards access to the fields below. |
| 131 mutable base::Lock lock_; | 112 mutable base::Lock lock_; |
| 132 std::unique_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; | 113 std::unique_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; |
| 133 std::vector<std::unique_ptr<Message>> pending_messages_; | 114 std::vector<std::unique_ptr<Message>> pending_messages_; |
| 134 bool waiting_connect_; | 115 bool waiting_connect_; |
| 135 | 116 |
| 136 base::WeakPtrFactory<ChannelMojo> weak_factory_; | 117 base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 137 | 118 |
| 138 DISALLOW_COPY_AND_ASSIGN(ChannelMojo); | 119 DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 139 }; | 120 }; |
| 140 | 121 |
| 141 } // namespace IPC | 122 } // namespace IPC |
| 142 | 123 |
| 143 #endif // IPC_IPC_CHANNEL_MOJO_H_ | 124 #endif // IPC_IPC_CHANNEL_MOJO_H_ |
| OLD | NEW |