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 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 | |
35 MojoChannelInit::MojoChannelInit() | 13 MojoChannelInit::MojoChannelInit() |
36 : channel_info_(NULL), | 14 : channel_info_(NULL), |
37 weak_factory_(this) { | 15 weak_factory_(this) { |
38 } | 16 } |
39 | 17 |
40 MojoChannelInit::~MojoChannelInit() { | 18 MojoChannelInit::~MojoChannelInit() { |
41 bootstrap_message_pipe_.reset(); | 19 bootstrap_message_pipe_.reset(); |
42 if (channel_info_) { | 20 if (channel_info_) { |
43 io_thread_task_runner_->PostTask( | 21 io_thread_task_runner_->PostTask( |
44 FROM_HERE, | 22 FROM_HERE, |
45 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); | 23 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); |
46 } | 24 } |
47 } | 25 } |
48 | 26 |
49 void MojoChannelInit::Init( | 27 void MojoChannelInit::Init( |
50 base::PlatformFile file, | 28 base::PlatformFile file, |
51 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 29 scoped_refptr<base::TaskRunner> io_thread_task_runner) { |
52 DCHECK(!io_thread_task_runner_.get()); // Should only init once. | 30 DCHECK(!io_thread_task_runner_.get()); // Should only init once. |
53 io_thread_task_runner_ = io_thread_task_runner; | 31 io_thread_task_runner_ = io_thread_task_runner; |
54 InitMojo(); | |
55 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( | 32 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( |
56 mojo::embedder::ScopedPlatformHandle( | 33 mojo::embedder::ScopedPlatformHandle( |
57 mojo::embedder::PlatformHandle(file)), | 34 mojo::embedder::PlatformHandle(file)), |
58 io_thread_task_runner, | 35 io_thread_task_runner, |
59 base::Bind(&MojoChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), | 36 base::Bind(&MojoChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), |
60 io_thread_task_runner), | 37 io_thread_task_runner), |
61 base::MessageLoop::current()->message_loop_proxy()).Pass(); | 38 base::MessageLoop::current()->message_loop_proxy()).Pass(); |
62 } | 39 } |
63 | 40 |
64 // static | 41 // static |
65 void MojoChannelInit::OnCreatedChannel( | 42 void MojoChannelInit::OnCreatedChannel( |
66 base::WeakPtr<MojoChannelInit> host, | 43 base::WeakPtr<MojoChannelInit> host, |
67 scoped_refptr<base::TaskRunner> io_thread, | 44 scoped_refptr<base::TaskRunner> io_thread, |
68 mojo::embedder::ChannelInfo* channel) { | 45 mojo::embedder::ChannelInfo* channel) { |
69 // 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 |
70 // channel. | 47 // channel. |
71 if (!host.get()) { | 48 if (!host.get()) { |
72 io_thread->PostTask( | 49 io_thread->PostTask( |
73 FROM_HERE, | 50 FROM_HERE, |
74 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); | 51 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); |
75 return; | 52 return; |
76 } | 53 } |
77 host->channel_info_ = channel; | 54 host->channel_info_ = channel; |
78 } | 55 } |
79 | 56 |
80 | 57 |
81 } // namespace content | 58 } // namespace content |
OLD | NEW |