Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(577)

Side by Side Diff: ipc/ipc_channel_mojo.cc

Issue 2668153003: Mojo C++ Bindings: Eliminate unbound ThreadSafeInterfacePtr (Closed)
Patch Set: . Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ipc_channel_mojo.h" 5 #include "ipc/ipc_channel_mojo.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { 267 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
268 return base::MakeUnique<MojoChannelFactory>( 268 return base::MakeUnique<MojoChannelFactory>(
269 std::move(handle), Channel::MODE_CLIENT, ipc_task_runner); 269 std::move(handle), Channel::MODE_CLIENT, ipc_task_runner);
270 } 270 }
271 271
272 ChannelMojo::ChannelMojo( 272 ChannelMojo::ChannelMojo(
273 mojo::ScopedMessagePipeHandle handle, 273 mojo::ScopedMessagePipeHandle handle,
274 Mode mode, 274 Mode mode,
275 Listener* listener, 275 Listener* listener,
276 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) 276 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner)
277 : pipe_(handle.get()), listener_(listener), weak_factory_(this) { 277 : task_runner_(ipc_task_runner),
278 // Create MojoBootstrap after all members are set as it touches 278 pipe_(handle.get()),
279 // ChannelMojo from a different thread. 279 listener_(listener),
280 bootstrap_ = 280 weak_factory_(this) {
281 MojoBootstrap::Create(std::move(handle), mode, this, ipc_task_runner); 281 bootstrap_ = MojoBootstrap::Create(
282 std::move(handle), mode, ipc_task_runner);
283 }
284
285 void ChannelMojo::ForwardMessageFromThreadSafePtr(mojo::Message message) {
286 DCHECK(task_runner_->RunsTasksOnCurrentThread());
287 if (!message_reader_)
288 return;
289 message_reader_->sender().internal_state()->ForwardMessage(
290 std::move(message));
291 }
292
293 void ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr(
294 mojo::Message message,
295 std::unique_ptr<mojo::MessageReceiver> responder) {
296 DCHECK(task_runner_->RunsTasksOnCurrentThread());
297 if (!message_reader_)
298 return;
299 message_reader_->sender().internal_state()->ForwardMessageWithResponder(
300 std::move(message), std::move(responder));
282 } 301 }
283 302
284 ChannelMojo::~ChannelMojo() { 303 ChannelMojo::~ChannelMojo() {
304 DCHECK(task_runner_->RunsTasksOnCurrentThread());
285 Close(); 305 Close();
286 } 306 }
287 307
288 bool ChannelMojo::Connect() { 308 bool ChannelMojo::Connect() {
309 DCHECK(task_runner_->RunsTasksOnCurrentThread());
310
289 WillConnect(); 311 WillConnect();
290 312
291 DCHECK(!task_runner_); 313 mojom::ChannelAssociatedPtr sender;
292 task_runner_ = base::ThreadTaskRunnerHandle::Get(); 314 mojom::ChannelAssociatedRequest receiver;
315 bootstrap_->Connect(&sender, &receiver);
316
293 DCHECK(!message_reader_); 317 DCHECK(!message_reader_);
294 318 message_reader_.reset(new internal::MessagePipeReader(
295 bootstrap_->Connect(); 319 pipe_, std::move(sender), std::move(receiver), this));
320 message_reader_->sender()->SetPeerPid(GetSelfPID());
296 return true; 321 return true;
297 } 322 }
298 323
299 void ChannelMojo::Pause() { 324 void ChannelMojo::Pause() {
300 bootstrap_->Pause(); 325 bootstrap_->Pause();
301 } 326 }
302 327
303 void ChannelMojo::Unpause(bool flush) { 328 void ChannelMojo::Unpause(bool flush) {
304 bootstrap_->Unpause(); 329 bootstrap_->Unpause();
305 if (flush) 330 if (flush)
306 Flush(); 331 Flush();
307 } 332 }
308 333
309 void ChannelMojo::Flush() { 334 void ChannelMojo::Flush() {
310 bootstrap_->Flush(); 335 bootstrap_->Flush();
311 } 336 }
312 337
313 void ChannelMojo::Close() { 338 void ChannelMojo::Close() {
314 // NOTE: The MessagePipeReader's destructor may re-enter this function. Use 339 // NOTE: The MessagePipeReader's destructor may re-enter this function. Use
315 // caution when changing this method. 340 // caution when changing this method.
316 std::unique_ptr<internal::MessagePipeReader> reader = 341 std::unique_ptr<internal::MessagePipeReader> reader =
317 std::move(message_reader_); 342 std::move(message_reader_);
318 reader.reset(); 343 reader.reset();
319 344
320 base::AutoLock lock(associated_interface_lock_); 345 base::AutoLock lock(associated_interface_lock_);
321 associated_interfaces_.clear(); 346 associated_interfaces_.clear();
322 } 347 }
323 348
324 // MojoBootstrap::Delegate implementation
325 void ChannelMojo::OnPipesAvailable(mojom::ChannelAssociatedPtr sender,
326 mojom::ChannelAssociatedRequest receiver) {
327 sender->SetPeerPid(GetSelfPID());
328 message_reader_.reset(new internal::MessagePipeReader(
329 pipe_, std::move(sender), std::move(receiver), this));
330 }
331
332 void ChannelMojo::OnPipeError() { 349 void ChannelMojo::OnPipeError() {
333 DCHECK(task_runner_); 350 DCHECK(task_runner_);
334 if (task_runner_->RunsTasksOnCurrentThread()) { 351 if (task_runner_->RunsTasksOnCurrentThread()) {
335 listener_->OnChannelError(); 352 listener_->OnChannelError();
336 } else { 353 } else {
337 task_runner_->PostTask( 354 task_runner_->PostTask(
338 FROM_HERE, 355 FROM_HERE,
339 base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr())); 356 base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr()));
340 } 357 }
341 } 358 }
(...skipping 28 matching lines...) Expand all
370 // called. 387 // called.
371 // 388 //
372 // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the 389 // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the
373 // pipe's connection error handler will be invoked in its place. 390 // pipe's connection error handler will be invoked in its place.
374 return message_reader_->Send(std::move(scoped_message)); 391 return message_reader_->Send(std::move(scoped_message));
375 } 392 }
376 393
377 Channel::AssociatedInterfaceSupport* 394 Channel::AssociatedInterfaceSupport*
378 ChannelMojo::GetAssociatedInterfaceSupport() { return this; } 395 ChannelMojo::GetAssociatedInterfaceSupport() { return this; }
379 396
397 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
398 ChannelMojo::CreateThreadSafeChannel() {
399 return base::MakeUnique<mojo::ThreadSafeForwarder<mojom::Channel>>(
400 task_runner_,
401 base::Bind(&ChannelMojo::ForwardMessageFromThreadSafePtr,
402 weak_factory_.GetWeakPtr()),
403 base::Bind(&ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr,
404 weak_factory_.GetWeakPtr()));
405 }
406
380 void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) { 407 void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) {
381 listener_->OnChannelConnected(peer_pid); 408 listener_->OnChannelConnected(peer_pid);
382 } 409 }
383 410
384 void ChannelMojo::OnMessageReceived(const Message& message) { 411 void ChannelMojo::OnMessageReceived(const Message& message) {
385 TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", 412 TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived",
386 "class", IPC_MESSAGE_ID_CLASS(message.type()), 413 "class", IPC_MESSAGE_ID_CLASS(message.type()),
387 "line", IPC_MESSAGE_ID_LINE(message.type())); 414 "line", IPC_MESSAGE_ID_LINE(message.type()));
388 listener_->OnMessageReceived(message); 415 listener_->OnMessageReceived(message);
389 if (message.dispatch_error()) 416 if (message.dispatch_error())
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 483 }
457 484
458 void ChannelMojo::GetGenericRemoteAssociatedInterface( 485 void ChannelMojo::GetGenericRemoteAssociatedInterface(
459 const std::string& name, 486 const std::string& name,
460 mojo::ScopedInterfaceEndpointHandle handle) { 487 mojo::ScopedInterfaceEndpointHandle handle) {
461 if (message_reader_) 488 if (message_reader_)
462 message_reader_->GetRemoteInterface(name, std::move(handle)); 489 message_reader_->GetRemoteInterface(name, std::move(handle));
463 } 490 }
464 491
465 } // namespace IPC 492 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698