| 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 <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "ipc/ipc_channel.h" | 18 #include "ipc/ipc_channel.h" |
| 19 #include "ipc/ipc_channel_factory.h" | 19 #include "ipc/ipc_channel_factory.h" |
| 20 #include "ipc/ipc_export.h" | 20 #include "ipc/ipc_export.h" |
| 21 #include "ipc/mojo/ipc_message_pipe_reader.h" | 21 #include "ipc/mojo/ipc_message_pipe_reader.h" |
| 22 #include "ipc/mojo/ipc_mojo_bootstrap.h" | 22 #include "ipc/mojo/ipc_mojo_bootstrap.h" |
| 23 #include "ipc/mojo/scoped_ipc_support.h" | 23 #include "ipc/mojo/scoped_ipc_support.h" |
| 24 #include "mojo/public/cpp/system/core.h" | 24 #include "mojo/public/cpp/system/core.h" |
| 25 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h" | |
| 26 | 25 |
| 27 namespace IPC { | 26 namespace IPC { |
| 28 | 27 |
| 29 // Mojo-based IPC::Channel implementation over a platform handle. | 28 // Mojo-based IPC::Channel implementation over a platform handle. |
| 30 // | 29 // |
| 31 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by | 30 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by |
| 32 // "bootstrap" IPC::Channel which creates and owns platform pipe like | 31 // "bootstrap" IPC::Channel which creates and owns platform pipe like |
| 33 // named socket. The bootstrap Channel is used only for establishing | 32 // named socket. The bootstrap Channel is used only for establishing |
| 34 // the underlying connection. ChannelMojo takes its handle over once | 33 // the underlying connection. ChannelMojo takes its handle over once |
| 35 // the it is made and puts MessagePipe on it. | 34 // the it is made and puts MessagePipe on it. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 // separate class to clarify what ChannelMojo relies | 47 // separate class to clarify what ChannelMojo relies |
| 49 // on. | 48 // on. |
| 50 // TODO(morrita): Add APIs to create extra MessagePipes to let | 49 // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 51 // Mojo-based objects talk over this Channel. | 50 // Mojo-based objects talk over this Channel. |
| 52 // | 51 // |
| 53 class IPC_MOJO_EXPORT ChannelMojo | 52 class IPC_MOJO_EXPORT ChannelMojo |
| 54 : public Channel, | 53 : public Channel, |
| 55 public MojoBootstrap::Delegate, | 54 public MojoBootstrap::Delegate, |
| 56 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { | 55 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { |
| 57 public: | 56 public: |
| 58 using CreateMessagingPipeCallback = | |
| 59 base::Callback<void(mojo::ScopedMessagePipeHandle)>; | |
| 60 using CreateMessagingPipeOnIOThreadCallback = | |
| 61 base::Callback<void(mojo::ScopedMessagePipeHandle, | |
| 62 mojo::embedder::ChannelInfo*)>; | |
| 63 | |
| 64 // True if ChannelMojo should be used regardless of the flag. | 57 // True if ChannelMojo should be used regardless of the flag. |
| 65 static bool ShouldBeUsed(); | 58 static bool ShouldBeUsed(); |
| 66 | 59 |
| 67 // Create ChannelMojo. A bootstrap channel is created as well. | 60 // Create ChannelMojo. A bootstrap channel is created as well. |
| 68 static scoped_ptr<ChannelMojo> Create( | 61 static scoped_ptr<ChannelMojo> Create( |
| 69 scoped_refptr<base::TaskRunner> io_runner, | 62 scoped_refptr<base::TaskRunner> io_runner, |
| 70 const ChannelHandle& channel_handle, | 63 const ChannelHandle& channel_handle, |
| 71 Mode mode, | 64 Mode mode, |
| 72 Listener* listener); | 65 Listener* listener); |
| 73 | 66 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 void OnMessageReceived(Message& message) override; | 106 void OnMessageReceived(Message& message) override; |
| 114 void OnPipeClosed(internal::MessagePipeReader* reader) override; | 107 void OnPipeClosed(internal::MessagePipeReader* reader) override; |
| 115 void OnPipeError(internal::MessagePipeReader* reader) override; | 108 void OnPipeError(internal::MessagePipeReader* reader) override; |
| 116 | 109 |
| 117 protected: | 110 protected: |
| 118 ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 111 ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
| 119 const ChannelHandle& channel_handle, | 112 const ChannelHandle& channel_handle, |
| 120 Mode mode, | 113 Mode mode, |
| 121 Listener* listener); | 114 Listener* listener); |
| 122 | 115 |
| 123 void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle, | |
| 124 const CreateMessagingPipeCallback& callback); | |
| 125 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid); | 116 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid); |
| 126 | 117 |
| 127 Listener* listener() const { return listener_; } | 118 Listener* listener() const { return listener_; } |
| 128 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; } | 119 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; } |
| 129 | 120 |
| 130 private: | 121 private: |
| 131 struct ChannelInfoDeleter { | |
| 132 explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner); | |
| 133 ~ChannelInfoDeleter(); | |
| 134 | |
| 135 void operator()(mojo::embedder::ChannelInfo* ptr) const; | |
| 136 | |
| 137 scoped_refptr<base::TaskRunner> io_runner; | |
| 138 }; | |
| 139 | |
| 140 // ChannelMojo needs to kill its MessagePipeReader in delayed manner | 122 // ChannelMojo needs to kill its MessagePipeReader in delayed manner |
| 141 // because the channel wants to kill these readers during the | 123 // because the channel wants to kill these readers during the |
| 142 // notifications invoked by them. | 124 // notifications invoked by them. |
| 143 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; | 125 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; |
| 144 | 126 |
| 145 void InitOnIOThread(); | 127 void InitOnIOThread(); |
| 146 | 128 |
| 147 static void CreateMessagingPipeOnIOThread( | |
| 148 mojo::embedder::ScopedPlatformHandle handle, | |
| 149 scoped_refptr<base::TaskRunner> callback_runner, | |
| 150 const CreateMessagingPipeOnIOThreadCallback& callback); | |
| 151 void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback, | |
| 152 mojo::ScopedMessagePipeHandle handle, | |
| 153 mojo::embedder::ChannelInfo* channel_info); | |
| 154 | |
| 155 scoped_ptr<MojoBootstrap> bootstrap_; | 129 scoped_ptr<MojoBootstrap> bootstrap_; |
| 156 Listener* listener_; | 130 Listener* listener_; |
| 157 base::ProcessId peer_pid_; | 131 base::ProcessId peer_pid_; |
| 158 scoped_refptr<base::TaskRunner> io_runner_; | 132 scoped_refptr<base::TaskRunner> io_runner_; |
| 159 scoped_ptr<mojo::embedder::ChannelInfo, | |
| 160 ChannelInfoDeleter> channel_info_; | |
| 161 | 133 |
| 162 // Guards |message_reader_|, |waiting_connect_| and |pending_messages_| | 134 // Guards |message_reader_|, |waiting_connect_| and |pending_messages_| |
| 163 // | 135 // |
| 164 // * The contents of |pending_messages_| can be modified from any thread. | 136 // * The contents of |pending_messages_| can be modified from any thread. |
| 165 // * |message_reader_| is modified only from the IO thread, | 137 // * |message_reader_| is modified only from the IO thread, |
| 166 // but they can be referenced from other threads. | 138 // but they can be referenced from other threads. |
| 167 base::Lock lock_; | 139 base::Lock lock_; |
| 168 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; | 140 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; |
| 169 ScopedVector<Message> pending_messages_; | 141 ScopedVector<Message> pending_messages_; |
| 170 bool waiting_connect_; | 142 bool waiting_connect_; |
| 171 | 143 |
| 172 scoped_ptr<ScopedIPCSupport> ipc_support_; | 144 scoped_ptr<ScopedIPCSupport> ipc_support_; |
| 173 | 145 |
| 174 base::WeakPtrFactory<ChannelMojo> weak_factory_; | 146 base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 175 | 147 |
| 176 DISALLOW_COPY_AND_ASSIGN(ChannelMojo); | 148 DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 177 }; | 149 }; |
| 178 | 150 |
| 179 } // namespace IPC | 151 } // namespace IPC |
| 180 | 152 |
| 181 #endif // IPC_IPC_CHANNEL_MOJO_H_ | 153 #endif // IPC_IPC_CHANNEL_MOJO_H_ |
| OLD | NEW |