OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/mojo/mojo_channel_init.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/synchronization/lock.h" | |
11 #include "mojo/system/embedder/embedder.h" | |
12 | |
13 namespace content { | |
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 | |
35 MojoChannelInit::MojoChannelInit() | |
36 : main_thread_(base::MessageLoop::current()->message_loop_proxy()), | |
37 channel_info_(NULL), | |
38 weak_factory_(this) { | |
39 } | |
40 | |
41 MojoChannelInit::~MojoChannelInit() { | |
42 bootstrap_message_pipe_.reset(); | |
43 if (channel_info_) { | |
44 io_thread_task_runner_->PostTask( | |
45 FROM_HERE, | |
46 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); | |
47 } | |
48 } | |
49 | |
50 void MojoChannelInit::Init( | |
51 base::PlatformFile file, | |
52 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | |
53 DCHECK(!io_thread_task_runner_.get()); // Should only init once. | |
54 io_thread_task_runner_ = io_thread_task_runner; | |
55 InitMojo(); | |
56 bootstrap_message_pipe_.reset( | |
57 mojo::Handle( | |
58 mojo::embedder::CreateChannel( | |
59 mojo::embedder::ScopedPlatformHandle( | |
60 mojo::embedder::PlatformHandle(file)), | |
61 io_thread_task_runner, | |
62 base::Bind(&MojoChannelInit::OnCreatedChannelOnIOThread, | |
63 weak_factory_.GetWeakPtr(), | |
64 main_thread_, | |
65 io_thread_task_runner)))); | |
66 } | |
67 | |
68 // static | |
69 void MojoChannelInit::OnCreatedChannelOnIOThread( | |
70 base::WeakPtr<MojoChannelInit> host, | |
71 scoped_refptr<base::TaskRunner> main_thread, | |
72 scoped_refptr<base::TaskRunner> io_thread, | |
73 mojo::embedder::ChannelInfo* channel) { | |
74 main_thread->PostTask( | |
75 FROM_HERE, | |
76 base::Bind(&MojoChannelInit::OnCreatedChannelOnMainThread, host, | |
77 io_thread, channel)); | |
78 } | |
79 | |
80 // static | |
81 void MojoChannelInit::OnCreatedChannelOnMainThread( | |
82 base::WeakPtr<MojoChannelInit> host, | |
83 scoped_refptr<base::TaskRunner> io_thread, | |
84 mojo::embedder::ChannelInfo* channel) { | |
85 // By the time we get here |host| may have been destroyed. If so, shutdown the | |
86 // channel. | |
87 if (!host.get()) { | |
88 io_thread->PostTask( | |
89 FROM_HERE, | |
90 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); | |
91 return; | |
92 } | |
93 host->channel_info_ = channel; | |
94 } | |
95 | |
96 | |
97 } // namespace content | |
OLD | NEW |