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 "ipc/ipc_listener.h" | 10 #include "ipc/ipc_listener.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 ChannelMojo::Delegate* delegate_; | 44 ChannelMojo::Delegate* delegate_; |
45 ChannelHandle channel_handle_; | 45 ChannelHandle channel_handle_; |
46 Channel::Mode mode_; | 46 Channel::Mode mode_; |
47 }; | 47 }; |
48 | 48 |
49 //------------------------------------------------------------------------------ | 49 //------------------------------------------------------------------------------ |
50 | 50 |
51 class ClientChannelMojo | 51 class ClientChannelMojo : public ChannelMojo, |
52 : public ChannelMojo, | 52 public ClientChannel, |
53 public NON_EXPORTED_BASE(mojo::InterfaceImpl<ClientChannel>) { | 53 public mojo::ErrorHandler { |
54 public: | 54 public: |
55 ClientChannelMojo(ChannelMojo::Delegate* delegate, | 55 ClientChannelMojo(ChannelMojo::Delegate* delegate, |
56 const ChannelHandle& handle, | 56 const ChannelHandle& handle, |
57 Listener* listener); | 57 Listener* listener); |
58 ~ClientChannelMojo() override; | 58 ~ClientChannelMojo() override; |
59 // MojoBootstrap::Delegate implementation | 59 // MojoBootstrap::Delegate implementation |
60 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 60 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
61 // InterfaceImpl implementation | 61 // mojo::ErrorHandler implementation |
62 void OnConnectionError() override; | 62 void OnConnectionError() override; |
63 // ClientChannel implementation | 63 // ClientChannel implementation |
64 void Init( | 64 void Init( |
65 mojo::ScopedMessagePipeHandle pipe, | 65 mojo::ScopedMessagePipeHandle pipe, |
66 int32_t peer_pid, | 66 int32_t peer_pid, |
67 const mojo::Callback<void(int32_t)>& callback) override; | 67 const mojo::Callback<void(int32_t)>& callback) override; |
68 | 68 |
| 69 private: |
| 70 mojo::Binding<ClientChannel> binding_; |
| 71 |
69 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); | 72 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); |
70 }; | 73 }; |
71 | 74 |
72 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, | 75 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, |
73 const ChannelHandle& handle, | 76 const ChannelHandle& handle, |
74 Listener* listener) | 77 Listener* listener) |
75 : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener) { | 78 : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener), |
| 79 binding_(this) { |
76 } | 80 } |
77 | 81 |
78 ClientChannelMojo::~ClientChannelMojo() { | 82 ClientChannelMojo::~ClientChannelMojo() { |
79 } | 83 } |
80 | 84 |
81 void ClientChannelMojo::OnPipeAvailable( | 85 void ClientChannelMojo::OnPipeAvailable( |
82 mojo::embedder::ScopedPlatformHandle handle) { | 86 mojo::embedder::ScopedPlatformHandle handle) { |
83 mojo::WeakBindToPipe(this, CreateMessagingPipe(handle.Pass())); | 87 binding_.Bind(CreateMessagingPipe(handle.Pass())); |
84 } | 88 } |
85 | 89 |
86 void ClientChannelMojo::OnConnectionError() { | 90 void ClientChannelMojo::OnConnectionError() { |
87 listener()->OnChannelError(); | 91 listener()->OnChannelError(); |
88 } | 92 } |
89 | 93 |
90 void ClientChannelMojo::Init( | 94 void ClientChannelMojo::Init( |
91 mojo::ScopedMessagePipeHandle pipe, | 95 mojo::ScopedMessagePipeHandle pipe, |
92 int32_t peer_pid, | 96 int32_t peer_pid, |
93 const mojo::Callback<void(int32_t)>& callback) { | 97 const mojo::Callback<void(int32_t)>& callback) { |
94 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); | 98 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); |
95 callback.Run(GetSelfPID()); | 99 callback.Run(GetSelfPID()); |
96 } | 100 } |
97 | 101 |
98 //------------------------------------------------------------------------------ | 102 //------------------------------------------------------------------------------ |
99 | 103 |
100 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { | 104 class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { |
101 public: | 105 public: |
102 ServerChannelMojo(ChannelMojo::Delegate* delegate, | 106 ServerChannelMojo(ChannelMojo::Delegate* delegate, |
103 const ChannelHandle& handle, | 107 const ChannelHandle& handle, |
104 Listener* listener); | 108 Listener* listener); |
105 ~ServerChannelMojo() override; | 109 ~ServerChannelMojo() override; |
106 | 110 |
107 // MojoBootstrap::Delegate implementation | 111 // MojoBootstrap::Delegate implementation |
108 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | 112 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
109 // ErrorHandler implementation | 113 // mojo::ErrorHandler implementation |
110 void OnConnectionError() override; | 114 void OnConnectionError() override; |
111 // Channel override | 115 // Channel override |
112 void Close() override; | 116 void Close() override; |
113 | 117 |
114 private: | 118 private: |
115 // ClientChannelClient implementation | 119 // ClientChannelClient implementation |
116 void ClientChannelWasInitialized(int32_t peer_pid); | 120 void ClientChannelWasInitialized(int32_t peer_pid); |
117 | 121 |
118 mojo::InterfacePtr<ClientChannel> client_channel_; | 122 mojo::InterfacePtr<ClientChannel> client_channel_; |
119 mojo::ScopedMessagePipeHandle message_pipe_; | 123 mojo::ScopedMessagePipeHandle message_pipe_; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 177 } |
174 | 178 |
175 #endif | 179 #endif |
176 | 180 |
177 } // namespace | 181 } // namespace |
178 | 182 |
179 //------------------------------------------------------------------------------ | 183 //------------------------------------------------------------------------------ |
180 | 184 |
181 void ChannelMojo::ChannelInfoDeleter::operator()( | 185 void ChannelMojo::ChannelInfoDeleter::operator()( |
182 mojo::embedder::ChannelInfo* ptr) const { | 186 mojo::embedder::ChannelInfo* ptr) const { |
183 mojo::embedder::DestroyChannel(ptr); | 187 mojo::embedder::DestroyChannel(ptr, base::Bind(&base::DoNothing), nullptr); |
184 } | 188 } |
185 | 189 |
186 //------------------------------------------------------------------------------ | 190 //------------------------------------------------------------------------------ |
187 | 191 |
188 // static | 192 // static |
189 bool ChannelMojo::ShouldBeUsed() { | 193 bool ChannelMojo::ShouldBeUsed() { |
190 // TODO(morrita): Remove this if it sticks. | 194 // TODO(morrita): Remove this if it sticks. |
191 return true; | 195 return true; |
192 } | 196 } |
193 | 197 |
(...skipping 18 matching lines...) Expand all Loading... |
212 // static | 216 // static |
213 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 217 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
214 ChannelMojo::Delegate* delegate, | 218 ChannelMojo::Delegate* delegate, |
215 const ChannelHandle& channel_handle) { | 219 const ChannelHandle& channel_handle) { |
216 return make_scoped_ptr( | 220 return make_scoped_ptr( |
217 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); | 221 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); |
218 } | 222 } |
219 | 223 |
220 // static | 224 // static |
221 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( | 225 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
| 226 ChannelMojo::Delegate* delegate, |
222 const ChannelHandle& channel_handle) { | 227 const ChannelHandle& channel_handle) { |
223 return make_scoped_ptr( | 228 return make_scoped_ptr( |
224 new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT)); | 229 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_CLIENT)); |
225 } | 230 } |
226 | 231 |
227 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, | 232 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, |
228 const ChannelHandle& handle, | 233 const ChannelHandle& handle, |
229 Mode mode, | 234 Mode mode, |
230 Listener* listener) | 235 Listener* listener) |
231 : mode_(mode), | 236 : mode_(mode), |
232 listener_(listener), | 237 listener_(listener), |
233 peer_pid_(base::kNullProcessId), | 238 peer_pid_(base::kNullProcessId), |
234 weak_factory_(this) { | 239 weak_factory_(this) { |
(...skipping 11 matching lines...) Expand all Loading... |
246 &ChannelMojo::InitDelegate, base::Unretained(this), delegate)); | 251 &ChannelMojo::InitDelegate, base::Unretained(this), delegate)); |
247 } | 252 } |
248 } | 253 } |
249 } | 254 } |
250 | 255 |
251 ChannelMojo::~ChannelMojo() { | 256 ChannelMojo::~ChannelMojo() { |
252 Close(); | 257 Close(); |
253 } | 258 } |
254 | 259 |
255 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { | 260 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { |
| 261 ipc_support_.reset( |
| 262 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); |
256 delegate_ = delegate->ToWeakPtr(); | 263 delegate_ = delegate->ToWeakPtr(); |
257 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); | 264 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); |
258 } | 265 } |
259 | 266 |
260 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( | 267 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( |
261 mojo::embedder::ScopedPlatformHandle handle) { | 268 mojo::embedder::ScopedPlatformHandle handle) { |
262 DCHECK(!channel_info_.get()); | 269 DCHECK(!channel_info_.get()); |
263 mojo::embedder::ChannelInfo* channel_info; | 270 mojo::embedder::ChannelInfo* channel_info; |
264 mojo::ScopedMessagePipeHandle pipe = | 271 mojo::ScopedMessagePipeHandle pipe = |
265 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); | 272 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
266 channel_info_.reset(channel_info); | 273 channel_info_.reset(channel_info); |
267 return pipe.Pass(); | 274 return pipe.Pass(); |
268 } | 275 } |
269 | 276 |
270 bool ChannelMojo::Connect() { | 277 bool ChannelMojo::Connect() { |
271 DCHECK(!message_reader_); | 278 DCHECK(!message_reader_); |
272 return bootstrap_->Connect(); | 279 return bootstrap_->Connect(); |
273 } | 280 } |
274 | 281 |
275 void ChannelMojo::Close() { | 282 void ChannelMojo::Close() { |
276 message_reader_.reset(); | 283 message_reader_.reset(); |
277 channel_info_.reset(); | 284 channel_info_.reset(); |
| 285 ipc_support_.reset(); |
278 } | 286 } |
279 | 287 |
280 void ChannelMojo::OnBootstrapError() { | 288 void ChannelMojo::OnBootstrapError() { |
281 listener_->OnChannelError(); | 289 listener_->OnChannelError(); |
282 } | 290 } |
283 | 291 |
284 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, | 292 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, |
285 int32_t peer_pid) { | 293 int32_t peer_pid) { |
286 message_reader_ = | 294 message_reader_ = |
287 make_scoped_ptr(new internal::MessagePipeReader(pipe.Pass(), this)); | 295 make_scoped_ptr(new internal::MessagePipeReader(pipe.Pass(), this)); |
288 | 296 |
289 for (size_t i = 0; i < pending_messages_.size(); ++i) { | 297 for (size_t i = 0; i < pending_messages_.size(); ++i) { |
290 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); | 298 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); |
291 pending_messages_[i] = NULL; | 299 pending_messages_[i] = nullptr; |
292 if (!sent) { | 300 if (!sent) { |
293 pending_messages_.clear(); | 301 pending_messages_.clear(); |
294 listener_->OnChannelError(); | 302 listener_->OnChannelError(); |
295 return; | 303 return; |
296 } | 304 } |
297 } | 305 } |
298 | 306 |
299 pending_messages_.clear(); | 307 pending_messages_.clear(); |
300 | 308 |
301 set_peer_pid(peer_pid); | 309 set_peer_pid(peer_pid); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 if (!ok) { | 430 if (!ok) { |
423 DLOG(ERROR) << "Failed to add new Mojo handle."; | 431 DLOG(ERROR) << "Failed to add new Mojo handle."; |
424 return MOJO_RESULT_UNKNOWN; | 432 return MOJO_RESULT_UNKNOWN; |
425 } | 433 } |
426 } | 434 } |
427 | 435 |
428 return MOJO_RESULT_OK; | 436 return MOJO_RESULT_OK; |
429 } | 437 } |
430 | 438 |
431 } // namespace IPC | 439 } // namespace IPC |
OLD | NEW |