| 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, |
| 95 AttachmentBroker* broker) |
| 90 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_CLIENT, listener), | 96 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_CLIENT, listener), |
| 91 binding_(this), | 97 binding_(this), |
| 92 weak_factory_(this) { | 98 weak_factory_(this) { |
| 93 } | 99 } |
| 94 | 100 |
| 95 ClientChannelMojo::~ClientChannelMojo() { | 101 ClientChannelMojo::~ClientChannelMojo() { |
| 96 } | 102 } |
| 97 | 103 |
| 98 void ClientChannelMojo::OnPipeAvailable( | 104 void ClientChannelMojo::OnPipeAvailable( |
| 99 mojo::embedder::ScopedPlatformHandle handle) { | 105 mojo::embedder::ScopedPlatformHandle handle) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 117 binding_.Bind(handle.Pass()); | 123 binding_.Bind(handle.Pass()); |
| 118 } | 124 } |
| 119 | 125 |
| 120 //------------------------------------------------------------------------------ | 126 //------------------------------------------------------------------------------ |
| 121 | 127 |
| 122 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { | 128 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { |
| 123 public: | 129 public: |
| 124 ServerChannelMojo(ChannelMojo::Delegate* delegate, | 130 ServerChannelMojo(ChannelMojo::Delegate* delegate, |
| 125 scoped_refptr<base::TaskRunner> io_runner, | 131 scoped_refptr<base::TaskRunner> io_runner, |
| 126 const ChannelHandle& handle, | 132 const ChannelHandle& handle, |
| 127 Listener* listener); | 133 Listener* listener, |
| 134 AttachmentBroker* broker); |
| 128 ~ServerChannelMojo() override; | 135 ~ServerChannelMojo() override; |
| 129 | 136 |
| 130 // MojoBootstrap::Delegate implementation | 137 // MojoBootstrap::Delegate implementation |
| 131 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 138 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
| 132 // mojo::ErrorHandler implementation | 139 // mojo::ErrorHandler implementation |
| 133 void OnConnectionError() override; | 140 void OnConnectionError() override; |
| 134 // Channel override | 141 // Channel override |
| 135 void Close() override; | 142 void Close() override; |
| 136 | 143 |
| 137 private: | 144 private: |
| 138 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, | 145 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, |
| 139 mojo::ScopedMessagePipeHandle handle); | 146 mojo::ScopedMessagePipeHandle handle); |
| 140 | 147 |
| 141 // ClientChannelClient implementation | 148 // ClientChannelClient implementation |
| 142 void ClientChannelWasInitialized(int32_t peer_pid); | 149 void ClientChannelWasInitialized(int32_t peer_pid); |
| 143 | 150 |
| 144 mojo::InterfacePtr<ClientChannel> client_channel_; | 151 mojo::InterfacePtr<ClientChannel> client_channel_; |
| 145 mojo::ScopedMessagePipeHandle message_pipe_; | 152 mojo::ScopedMessagePipeHandle message_pipe_; |
| 146 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; | 153 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; |
| 147 | 154 |
| 148 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); | 155 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); |
| 149 }; | 156 }; |
| 150 | 157 |
| 151 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, | 158 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, |
| 152 scoped_refptr<base::TaskRunner> io_runner, | 159 scoped_refptr<base::TaskRunner> io_runner, |
| 153 const ChannelHandle& handle, | 160 const ChannelHandle& handle, |
| 154 Listener* listener) | 161 Listener* listener, |
| 162 AttachmentBroker* broker) |
| 155 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_SERVER, listener), | 163 : ChannelMojo(delegate, io_runner, handle, Channel::MODE_SERVER, listener), |
| 156 weak_factory_(this) { | 164 weak_factory_(this) { |
| 157 } | 165 } |
| 158 | 166 |
| 159 ServerChannelMojo::~ServerChannelMojo() { | 167 ServerChannelMojo::~ServerChannelMojo() { |
| 160 Close(); | 168 Close(); |
| 161 } | 169 } |
| 162 | 170 |
| 163 void ServerChannelMojo::OnPipeAvailable( | 171 void ServerChannelMojo::OnPipeAvailable( |
| 164 mojo::embedder::ScopedPlatformHandle handle) { | 172 mojo::embedder::ScopedPlatformHandle handle) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 // this at some point. http://crbug.com/500019 | 249 // this at some point. http://crbug.com/500019 |
| 242 return false; | 250 return false; |
| 243 } | 251 } |
| 244 | 252 |
| 245 // static | 253 // static |
| 246 scoped_ptr<ChannelMojo> ChannelMojo::Create( | 254 scoped_ptr<ChannelMojo> ChannelMojo::Create( |
| 247 ChannelMojo::Delegate* delegate, | 255 ChannelMojo::Delegate* delegate, |
| 248 scoped_refptr<base::TaskRunner> io_runner, | 256 scoped_refptr<base::TaskRunner> io_runner, |
| 249 const ChannelHandle& channel_handle, | 257 const ChannelHandle& channel_handle, |
| 250 Mode mode, | 258 Mode mode, |
| 251 Listener* listener) { | 259 Listener* listener, |
| 260 AttachmentBroker* broker) { |
| 252 switch (mode) { | 261 switch (mode) { |
| 253 case Channel::MODE_CLIENT: | 262 case Channel::MODE_CLIENT: |
| 254 return make_scoped_ptr( | 263 return make_scoped_ptr(new ClientChannelMojo( |
| 255 new ClientChannelMojo(delegate, io_runner, channel_handle, listener)); | 264 delegate, io_runner, channel_handle, listener, broker)); |
| 256 case Channel::MODE_SERVER: | 265 case Channel::MODE_SERVER: |
| 257 return make_scoped_ptr( | 266 return make_scoped_ptr(new ServerChannelMojo( |
| 258 new ServerChannelMojo(delegate, io_runner, channel_handle, listener)); | 267 delegate, io_runner, channel_handle, listener, broker)); |
| 259 default: | 268 default: |
| 260 NOTREACHED(); | 269 NOTREACHED(); |
| 261 return nullptr; | 270 return nullptr; |
| 262 } | 271 } |
| 263 } | 272 } |
| 264 | 273 |
| 265 // static | 274 // static |
| 266 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 275 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
| 267 ChannelMojo::Delegate* delegate, | 276 ChannelMojo::Delegate* delegate, |
| 268 scoped_refptr<base::TaskRunner> io_runner, | 277 scoped_refptr<base::TaskRunner> io_runner, |
| 269 const ChannelHandle& channel_handle) { | 278 const ChannelHandle& channel_handle, |
| 279 AttachmentBroker* broker) { |
| 270 return make_scoped_ptr(new MojoChannelFactory( | 280 return make_scoped_ptr(new MojoChannelFactory( |
| 271 delegate, io_runner, channel_handle, Channel::MODE_SERVER)); | 281 delegate, io_runner, channel_handle, Channel::MODE_SERVER, broker)); |
| 272 } | 282 } |
| 273 | 283 |
| 274 // static | 284 // static |
| 275 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( | 285 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
| 276 ChannelMojo::Delegate* delegate, | 286 ChannelMojo::Delegate* delegate, |
| 277 scoped_refptr<base::TaskRunner> io_runner, | 287 scoped_refptr<base::TaskRunner> io_runner, |
| 278 const ChannelHandle& channel_handle) { | 288 const ChannelHandle& channel_handle, |
| 289 AttachmentBroker* broker) { |
| 279 return make_scoped_ptr(new MojoChannelFactory( | 290 return make_scoped_ptr(new MojoChannelFactory( |
| 280 delegate, io_runner, channel_handle, Channel::MODE_CLIENT)); | 291 delegate, io_runner, channel_handle, Channel::MODE_CLIENT, broker)); |
| 281 } | 292 } |
| 282 | 293 |
| 283 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, | 294 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, |
| 284 scoped_refptr<base::TaskRunner> io_runner, | 295 scoped_refptr<base::TaskRunner> io_runner, |
| 285 const ChannelHandle& handle, | 296 const ChannelHandle& handle, |
| 286 Mode mode, | 297 Mode mode, |
| 287 Listener* listener) | 298 Listener* listener) |
| 288 : mode_(mode), | 299 : mode_(mode), |
| 289 listener_(listener), | 300 listener_(listener), |
| 290 peer_pid_(base::kNullProcessId), | 301 peer_pid_(base::kNullProcessId), |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 if (!ok) { | 564 if (!ok) { |
| 554 LOG(ERROR) << "Failed to add new Mojo handle."; | 565 LOG(ERROR) << "Failed to add new Mojo handle."; |
| 555 return MOJO_RESULT_UNKNOWN; | 566 return MOJO_RESULT_UNKNOWN; |
| 556 } | 567 } |
| 557 } | 568 } |
| 558 | 569 |
| 559 return MOJO_RESULT_OK; | 570 return MOJO_RESULT_OK; |
| 560 } | 571 } |
| 561 | 572 |
| 562 } // namespace IPC | 573 } // namespace IPC |
| OLD | NEW |