| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "content/common/mojo/mojo_channel_init.h" | 5 #include "content/common/mojo/mojo_channel_init.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/synchronization/lock.h" |
| 9 #include "mojo/embedder/embedder.h" | 11 #include "mojo/embedder/embedder.h" |
| 10 | 12 |
| 11 namespace content { | 13 namespace content { |
| 12 | 14 |
| 15 namespace { |
| 16 |
| 17 struct Initializer { |
| 18 Initializer() { |
| 19 mojo::embedder::Init(); |
| 20 } |
| 21 }; |
| 22 |
| 23 static base::LazyInstance<Initializer>::Leaky initializer = |
| 24 LAZY_INSTANCE_INITIALIZER; |
| 25 |
| 26 // Initializes mojo. Use a lazy instance to ensure we only do this once. |
| 27 // TODO(sky): this likely wants to move to a more central location, such as |
| 28 // startup. |
| 29 void InitMojo() { |
| 30 initializer.Get(); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 13 MojoChannelInit::MojoChannelInit() | 35 MojoChannelInit::MojoChannelInit() |
| 14 : channel_info_(NULL), | 36 : channel_info_(NULL), |
| 15 weak_factory_(this) { | 37 weak_factory_(this) { |
| 16 } | 38 } |
| 17 | 39 |
| 18 MojoChannelInit::~MojoChannelInit() { | 40 MojoChannelInit::~MojoChannelInit() { |
| 19 bootstrap_message_pipe_.reset(); | 41 bootstrap_message_pipe_.reset(); |
| 20 if (channel_info_) { | 42 if (channel_info_) { |
| 21 io_thread_task_runner_->PostTask( | 43 io_thread_task_runner_->PostTask( |
| 22 FROM_HERE, | 44 FROM_HERE, |
| 23 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); | 45 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); |
| 24 } | 46 } |
| 25 } | 47 } |
| 26 | 48 |
| 27 void MojoChannelInit::Init( | 49 void MojoChannelInit::Init( |
| 28 base::PlatformFile file, | 50 base::PlatformFile file, |
| 29 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 51 scoped_refptr<base::TaskRunner> io_thread_task_runner) { |
| 30 DCHECK(!io_thread_task_runner_.get()); // Should only init once. | 52 DCHECK(!io_thread_task_runner_.get()); // Should only init once. |
| 31 io_thread_task_runner_ = io_thread_task_runner; | 53 io_thread_task_runner_ = io_thread_task_runner; |
| 54 InitMojo(); |
| 32 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( | 55 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( |
| 33 mojo::embedder::ScopedPlatformHandle( | 56 mojo::embedder::ScopedPlatformHandle( |
| 34 mojo::embedder::PlatformHandle(file)), | 57 mojo::embedder::PlatformHandle(file)), |
| 35 io_thread_task_runner, | 58 io_thread_task_runner, |
| 36 base::Bind(&MojoChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), | 59 base::Bind(&MojoChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), |
| 37 io_thread_task_runner), | 60 io_thread_task_runner), |
| 38 base::MessageLoop::current()->message_loop_proxy()).Pass(); | 61 base::MessageLoop::current()->message_loop_proxy()).Pass(); |
| 39 } | 62 } |
| 40 | 63 |
| 41 // static | 64 // static |
| 42 void MojoChannelInit::OnCreatedChannel( | 65 void MojoChannelInit::OnCreatedChannel( |
| 43 base::WeakPtr<MojoChannelInit> host, | 66 base::WeakPtr<MojoChannelInit> host, |
| 44 scoped_refptr<base::TaskRunner> io_thread, | 67 scoped_refptr<base::TaskRunner> io_thread, |
| 45 mojo::embedder::ChannelInfo* channel) { | 68 mojo::embedder::ChannelInfo* channel) { |
| 46 // By the time we get here |host| may have been destroyed. If so, shutdown the | 69 // By the time we get here |host| may have been destroyed. If so, shutdown the |
| 47 // channel. | 70 // channel. |
| 48 if (!host.get()) { | 71 if (!host.get()) { |
| 49 io_thread->PostTask( | 72 io_thread->PostTask( |
| 50 FROM_HERE, | 73 FROM_HERE, |
| 51 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); | 74 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); |
| 52 return; | 75 return; |
| 53 } | 76 } |
| 54 host->channel_info_ = channel; | 77 host->channel_info_ = channel; |
| 55 } | 78 } |
| 56 | 79 |
| 57 | 80 |
| 58 } // namespace content | 81 } // namespace content |
| OLD | NEW |