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/lazy_instance.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "mojo/embedder/embedder.h" | 11 #include "mojo/embedder/embedder.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 struct Initializer { | 17 struct Initializer { |
18 Initializer() { | 18 Initializer() { |
19 mojo::embedder::Init(); | 19 mojo::embedder::Init(); |
20 } | 20 } |
21 }; | 21 }; |
22 | 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 } |
23 | 32 |
24 } // namespace | 33 } // namespace |
25 | 34 |
26 MojoChannelInit::MojoChannelInit() | 35 MojoChannelInit::MojoChannelInit() |
27 : channel_info_(NULL), | 36 : channel_info_(NULL), |
28 weak_factory_(this) { | 37 weak_factory_(this) { |
29 } | 38 } |
30 | 39 |
31 MojoChannelInit::~MojoChannelInit() { | 40 MojoChannelInit::~MojoChannelInit() { |
32 bootstrap_message_pipe_.reset(); | 41 bootstrap_message_pipe_.reset(); |
33 if (channel_info_) { | 42 if (channel_info_) { |
34 io_thread_task_runner_->PostTask( | 43 io_thread_task_runner_->PostTask( |
35 FROM_HERE, | 44 FROM_HERE, |
36 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); | 45 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); |
37 } | 46 } |
38 } | 47 } |
39 | 48 |
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( | 49 void MojoChannelInit::Init( |
51 base::PlatformFile file, | 50 base::PlatformFile file, |
52 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 51 scoped_refptr<base::TaskRunner> io_thread_task_runner) { |
53 DCHECK(!io_thread_task_runner_.get()); // Should only init once. | 52 DCHECK(!io_thread_task_runner_.get()); // Should only init once. |
54 io_thread_task_runner_ = io_thread_task_runner; | 53 io_thread_task_runner_ = io_thread_task_runner; |
55 InitMojo(); | 54 InitMojo(); |
56 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( | 55 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( |
57 mojo::embedder::ScopedPlatformHandle( | 56 mojo::embedder::ScopedPlatformHandle( |
58 mojo::embedder::PlatformHandle(file)), | 57 mojo::embedder::PlatformHandle(file)), |
59 io_thread_task_runner, | 58 io_thread_task_runner, |
(...skipping 13 matching lines...) Expand all Loading... |
73 io_thread->PostTask( | 72 io_thread->PostTask( |
74 FROM_HERE, | 73 FROM_HERE, |
75 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); | 74 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); |
76 return; | 75 return; |
77 } | 76 } |
78 host->channel_info_ = channel; | 77 host->channel_info_ = channel; |
79 } | 78 } |
80 | 79 |
81 | 80 |
82 } // namespace content | 81 } // namespace content |
OLD | NEW |