| 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 #include "ipc/mojo/ipc_channel_mojo.h" | 5 #include "ipc/mojo/ipc_channel_mojo.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 namespace IPC { | 25 namespace IPC { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 class MojoChannelFactory : public ChannelFactory { | 29 class MojoChannelFactory : public ChannelFactory { |
| 30 public: | 30 public: |
| 31 MojoChannelFactory(ChannelMojo::Delegate* delegate, | 31 MojoChannelFactory(ChannelMojo::Delegate* delegate, |
| 32 scoped_refptr<base::TaskRunner> io_runner, | 32 scoped_refptr<base::TaskRunner> io_runner, |
| 33 ChannelHandle channel_handle, | 33 ChannelHandle channel_handle, |
| 34 Channel::Mode mode) | 34 Channel::Mode mode, |
| 35 AttachmentBroker* broker) |
| 35 : delegate_(delegate), | 36 : delegate_(delegate), |
| 36 io_runner_(io_runner), | 37 io_runner_(io_runner), |
| 37 channel_handle_(channel_handle), | 38 channel_handle_(channel_handle), |
| 38 mode_(mode) {} | 39 mode_(mode), |
| 40 broker_(broker) {} |
| 39 | 41 |
| 40 std::string GetName() const override { | 42 std::string GetName() const override { |
| 41 return channel_handle_.name; | 43 return channel_handle_.name; |
| 42 } | 44 } |
| 43 | 45 |
| 44 scoped_ptr<Channel> BuildChannel(Listener* listener) override { | 46 scoped_ptr<Channel> BuildChannel(Listener* listener) override { |
| 45 return ChannelMojo::Create(delegate_, io_runner_, channel_handle_, mode_, | 47 return ChannelMojo::Create(delegate_, io_runner_, channel_handle_, mode_, |
| 46 listener); | 48 listener, broker_); |
| 47 } | 49 } |
| 48 | 50 |
| 49 private: | 51 private: |
| 50 ChannelMojo::Delegate* delegate_; | 52 ChannelMojo::Delegate* delegate_; |
| 51 scoped_refptr<base::TaskRunner> io_runner_; | 53 scoped_refptr<base::TaskRunner> io_runner_; |
| 52 ChannelHandle channel_handle_; | 54 ChannelHandle channel_handle_; |
| 53 Channel::Mode mode_; | 55 Channel::Mode mode_; |
| 56 AttachmentBroker* broker_; |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 //------------------------------------------------------------------------------ | 59 //------------------------------------------------------------------------------ |
| 57 | 60 |
| 58 class ClientChannelMojo : public ChannelMojo, | 61 class ClientChannelMojo : public ChannelMojo, |
| 59 public ClientChannel, | 62 public ClientChannel, |
| 60 public mojo::ErrorHandler { | 63 public mojo::ErrorHandler { |
| 61 public: | 64 public: |
| 62 ClientChannelMojo(ChannelMojo::Delegate* delegate, | 65 ClientChannelMojo(ChannelMojo::Delegate* delegate, |
| 63 scoped_refptr<base::TaskRunner> io_runner, | 66 scoped_refptr<base::TaskRunner> io_runner, |
| 64 const ChannelHandle& handle, | 67 const ChannelHandle& handle, |
| 65 Listener* listener); | 68 Listener* listener, |
| 69 AttachmentBroker* broker); |
| 66 ~ClientChannelMojo() override; | 70 ~ClientChannelMojo() override; |
| 67 // MojoBootstrap::Delegate implementation | 71 // MojoBootstrap::Delegate implementation |
| 68 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 72 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
| 69 // mojo::ErrorHandler implementation | 73 // mojo::ErrorHandler implementation |
| 70 void OnConnectionError() override; | 74 void OnConnectionError() override; |
| 75 |
| 71 // ClientChannel implementation | 76 // ClientChannel implementation |
| 72 void Init( | 77 void Init( |
| 73 mojo::ScopedMessagePipeHandle pipe, | 78 mojo::ScopedMessagePipeHandle pipe, |
| 74 int32_t peer_pid, | 79 int32_t peer_pid, |
| 75 const mojo::Callback<void(int32_t)>& callback) override; | 80 const mojo::Callback<void(int32_t)>& callback) override; |
| 76 | 81 |
| 77 private: | 82 private: |
| 78 void BindPipe(mojo::ScopedMessagePipeHandle handle); | 83 void BindPipe(mojo::ScopedMessagePipeHandle handle); |
| 79 | 84 |
| 80 mojo::Binding<ClientChannel> binding_; | 85 mojo::Binding<ClientChannel> binding_; |
| 81 base::WeakPtrFactory<ClientChannelMojo> weak_factory_; | 86 base::WeakPtrFactory<ClientChannelMojo> weak_factory_; |
| 82 | 87 |
| 83 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); | 88 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); |
| 84 }; | 89 }; |
| 85 | 90 |
| 86 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, | 91 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, |
| 87 scoped_refptr<base::TaskRunner> io_runner, | 92 scoped_refptr<base::TaskRunner> io_runner, |
| 88 const ChannelHandle& handle, | 93 const ChannelHandle& handle, |
| 89 Listener* listener) | 94 Listener* listener, |
| 90 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_CLIENT, listener), | 95 AttachmentBroker* broker) |
| 96 : ChannelMojo(delegate, |
| 97 io_runner, |
| 98 handle, |
| 99 Channel::MODE_CLIENT, |
| 100 listener, |
| 101 broker), |
| 91 binding_(this), | 102 binding_(this), |
| 92 weak_factory_(this) { | 103 weak_factory_(this) { |
| 93 } | 104 } |
| 94 | 105 |
| 95 ClientChannelMojo::~ClientChannelMojo() { | 106 ClientChannelMojo::~ClientChannelMojo() { |
| 96 } | 107 } |
| 97 | 108 |
| 98 void ClientChannelMojo::OnPipeAvailable( | 109 void ClientChannelMojo::OnPipeAvailable( |
| 99 mojo::embedder::ScopedPlatformHandle handle) { | 110 mojo::embedder::ScopedPlatformHandle handle) { |
| 100 CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe, | 111 CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 117 binding_.Bind(handle.Pass()); | 128 binding_.Bind(handle.Pass()); |
| 118 } | 129 } |
| 119 | 130 |
| 120 //------------------------------------------------------------------------------ | 131 //------------------------------------------------------------------------------ |
| 121 | 132 |
| 122 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { | 133 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { |
| 123 public: | 134 public: |
| 124 ServerChannelMojo(ChannelMojo::Delegate* delegate, | 135 ServerChannelMojo(ChannelMojo::Delegate* delegate, |
| 125 scoped_refptr<base::TaskRunner> io_runner, | 136 scoped_refptr<base::TaskRunner> io_runner, |
| 126 const ChannelHandle& handle, | 137 const ChannelHandle& handle, |
| 127 Listener* listener); | 138 Listener* listener, |
| 139 AttachmentBroker* broker); |
| 128 ~ServerChannelMojo() override; | 140 ~ServerChannelMojo() override; |
| 129 | 141 |
| 130 // MojoBootstrap::Delegate implementation | 142 // MojoBootstrap::Delegate implementation |
| 131 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 143 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
| 132 // mojo::ErrorHandler implementation | 144 // mojo::ErrorHandler implementation |
| 133 void OnConnectionError() override; | 145 void OnConnectionError() override; |
| 134 // Channel override | 146 // Channel override |
| 135 void Close() override; | 147 void Close() override; |
| 136 | 148 |
| 137 private: | 149 private: |
| 138 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, | 150 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, |
| 139 mojo::ScopedMessagePipeHandle handle); | 151 mojo::ScopedMessagePipeHandle handle); |
| 140 | 152 |
| 141 // ClientChannelClient implementation | 153 // ClientChannelClient implementation |
| 142 void ClientChannelWasInitialized(int32_t peer_pid); | 154 void ClientChannelWasInitialized(int32_t peer_pid); |
| 143 | 155 |
| 144 mojo::InterfacePtr<ClientChannel> client_channel_; | 156 mojo::InterfacePtr<ClientChannel> client_channel_; |
| 145 mojo::ScopedMessagePipeHandle message_pipe_; | 157 mojo::ScopedMessagePipeHandle message_pipe_; |
| 146 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; | 158 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; |
| 147 | 159 |
| 148 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); | 160 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); |
| 149 }; | 161 }; |
| 150 | 162 |
| 151 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, | 163 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, |
| 152 scoped_refptr<base::TaskRunner> io_runner, | 164 scoped_refptr<base::TaskRunner> io_runner, |
| 153 const ChannelHandle& handle, | 165 const ChannelHandle& handle, |
| 154 Listener* listener) | 166 Listener* listener, |
| 155 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_SERVER, listener), | 167 AttachmentBroker* broker) |
| 168 : ChannelMojo(delegate, |
| 169 io_runner, |
| 170 handle, |
| 171 Channel::MODE_SERVER, |
| 172 listener, |
| 173 broker), |
| 156 weak_factory_(this) { | 174 weak_factory_(this) { |
| 157 } | 175 } |
| 158 | 176 |
| 159 ServerChannelMojo::~ServerChannelMojo() { | 177 ServerChannelMojo::~ServerChannelMojo() { |
| 160 Close(); | 178 Close(); |
| 161 } | 179 } |
| 162 | 180 |
| 163 void ServerChannelMojo::OnPipeAvailable( | 181 void ServerChannelMojo::OnPipeAvailable( |
| 164 mojo::embedder::ScopedPlatformHandle handle) { | 182 mojo::embedder::ScopedPlatformHandle handle) { |
| 165 mojo::ScopedMessagePipeHandle peer; | 183 mojo::ScopedMessagePipeHandle peer; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 // this at some point. http://crbug.com/500019 | 259 // this at some point. http://crbug.com/500019 |
| 242 return false; | 260 return false; |
| 243 } | 261 } |
| 244 | 262 |
| 245 // static | 263 // static |
| 246 scoped_ptr<ChannelMojo> ChannelMojo::Create( | 264 scoped_ptr<ChannelMojo> ChannelMojo::Create( |
| 247 ChannelMojo::Delegate* delegate, | 265 ChannelMojo::Delegate* delegate, |
| 248 scoped_refptr<base::TaskRunner> io_runner, | 266 scoped_refptr<base::TaskRunner> io_runner, |
| 249 const ChannelHandle& channel_handle, | 267 const ChannelHandle& channel_handle, |
| 250 Mode mode, | 268 Mode mode, |
| 251 Listener* listener) { | 269 Listener* listener, |
| 270 AttachmentBroker* broker) { |
| 252 switch (mode) { | 271 switch (mode) { |
| 253 case Channel::MODE_CLIENT: | 272 case Channel::MODE_CLIENT: |
| 254 return make_scoped_ptr( | 273 return make_scoped_ptr(new ClientChannelMojo( |
| 255 new ClientChannelMojo(delegate, io_runner, channel_handle, listener)); | 274 delegate, io_runner, channel_handle, listener, broker)); |
| 256 case Channel::MODE_SERVER: | 275 case Channel::MODE_SERVER: |
| 257 return make_scoped_ptr( | 276 return make_scoped_ptr(new ServerChannelMojo( |
| 258 new ServerChannelMojo(delegate, io_runner, channel_handle, listener)); | 277 delegate, io_runner, channel_handle, listener, broker)); |
| 259 default: | 278 default: |
| 260 NOTREACHED(); | 279 NOTREACHED(); |
| 261 return nullptr; | 280 return nullptr; |
| 262 } | 281 } |
| 263 } | 282 } |
| 264 | 283 |
| 265 // static | 284 // static |
| 266 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 285 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
| 267 ChannelMojo::Delegate* delegate, | 286 ChannelMojo::Delegate* delegate, |
| 268 scoped_refptr<base::TaskRunner> io_runner, | 287 scoped_refptr<base::TaskRunner> io_runner, |
| 269 const ChannelHandle& channel_handle) { | 288 const ChannelHandle& channel_handle, |
| 289 AttachmentBroker* broker) { |
| 270 return make_scoped_ptr(new MojoChannelFactory( | 290 return make_scoped_ptr(new MojoChannelFactory( |
| 271 delegate, io_runner, channel_handle, Channel::MODE_SERVER)); | 291 delegate, io_runner, channel_handle, Channel::MODE_SERVER, broker)); |
| 272 } | 292 } |
| 273 | 293 |
| 274 // static | 294 // static |
| 275 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( | 295 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
| 276 ChannelMojo::Delegate* delegate, | 296 ChannelMojo::Delegate* delegate, |
| 277 scoped_refptr<base::TaskRunner> io_runner, | 297 scoped_refptr<base::TaskRunner> io_runner, |
| 278 const ChannelHandle& channel_handle) { | 298 const ChannelHandle& channel_handle, |
| 299 AttachmentBroker* broker) { |
| 279 return make_scoped_ptr(new MojoChannelFactory( | 300 return make_scoped_ptr(new MojoChannelFactory( |
| 280 delegate, io_runner, channel_handle, Channel::MODE_CLIENT)); | 301 delegate, io_runner, channel_handle, Channel::MODE_CLIENT, broker)); |
| 281 } | 302 } |
| 282 | 303 |
| 283 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, | 304 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, |
| 284 scoped_refptr<base::TaskRunner> io_runner, | 305 scoped_refptr<base::TaskRunner> io_runner, |
| 285 const ChannelHandle& handle, | 306 const ChannelHandle& handle, |
| 286 Mode mode, | 307 Mode mode, |
| 287 Listener* listener) | 308 Listener* listener, |
| 309 AttachmentBroker* broker) |
| 288 : mode_(mode), | 310 : mode_(mode), |
| 289 listener_(listener), | 311 listener_(listener), |
| 290 peer_pid_(base::kNullProcessId), | 312 peer_pid_(base::kNullProcessId), |
| 291 io_runner_(io_runner), | 313 io_runner_(io_runner), |
| 292 channel_info_(nullptr, ChannelInfoDeleter(nullptr)), | 314 channel_info_(nullptr, ChannelInfoDeleter(nullptr)), |
| 293 weak_factory_(this) { | 315 weak_factory_(this) { |
| 294 // Create MojoBootstrap after all members are set as it touches | 316 // Create MojoBootstrap after all members are set as it touches |
| 295 // ChannelMojo from a different thread. | 317 // ChannelMojo from a different thread. |
| 296 bootstrap_ = MojoBootstrap::Create(handle, mode, this); | 318 bootstrap_ = MojoBootstrap::Create(handle, mode, this, broker); |
| 297 if (io_runner == base::MessageLoop::current()->task_runner()) { | 319 if (io_runner == base::MessageLoop::current()->task_runner()) { |
| 298 InitOnIOThread(delegate); | 320 InitOnIOThread(delegate); |
| 299 } else { | 321 } else { |
| 300 io_runner->PostTask(FROM_HERE, | 322 io_runner->PostTask(FROM_HERE, |
| 301 base::Bind(&ChannelMojo::InitOnIOThread, | 323 base::Bind(&ChannelMojo::InitOnIOThread, |
| 302 base::Unretained(this), delegate)); | 324 base::Unretained(this), delegate)); |
| 303 } | 325 } |
| 304 } | 326 } |
| 305 | 327 |
| 306 ChannelMojo::~ChannelMojo() { | 328 ChannelMojo::~ChannelMojo() { |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 if (!ok) { | 578 if (!ok) { |
| 557 LOG(ERROR) << "Failed to add new Mojo handle."; | 579 LOG(ERROR) << "Failed to add new Mojo handle."; |
| 558 return MOJO_RESULT_UNKNOWN; | 580 return MOJO_RESULT_UNKNOWN; |
| 559 } | 581 } |
| 560 } | 582 } |
| 561 | 583 |
| 562 return MOJO_RESULT_OK; | 584 return MOJO_RESULT_OK; |
| 563 } | 585 } |
| 564 | 586 |
| 565 } // namespace IPC | 587 } // namespace IPC |
| OLD | NEW |