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