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 // Initializes mojo. Use a lock to ensure we only do this once. | |
18 // TODO(sky): this likely wants to move to a more central location, such as | |
19 // startup. | |
20 void InitMojo() { | |
21 static base::LazyInstance<base::Lock> lock = LAZY_INSTANCE_INITIALIZER; | |
darin (slow to review)
2014/03/14 22:03:36
hmm, to be we don't have an equivalent of pthread_
sky
2014/03/14 22:45:12
Done.
| |
22 static bool inited = false; | |
23 base::AutoLock auto_lock(lock.Get()); | |
24 if (!inited) { | |
25 inited = true; | |
26 mojo::embedder::Init(); | |
27 } | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 MojoChannelInit::MojoChannelInit() | |
33 : main_thread_(base::MessageLoop::current()->message_loop_proxy()), | |
34 channel_info_(NULL), | |
35 weak_factory_(this) { | |
36 } | |
37 | |
38 MojoChannelInit::~MojoChannelInit() { | |
39 bootstrap_message_pipe_.reset(); | |
40 if (channel_info_) { | |
41 io_thread_task_runner_->PostTask( | |
42 FROM_HERE, | |
43 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); | |
44 } | |
45 } | |
46 | |
47 void MojoChannelInit::Init( | |
48 base::PlatformFile file, | |
49 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | |
50 DCHECK(!io_thread_task_runner_.get()); // Should only init once. | |
51 io_thread_task_runner_ = io_thread_task_runner; | |
52 InitMojo(); | |
53 bootstrap_message_pipe_.reset( | |
54 mojo::Handle( | |
55 mojo::embedder::CreateChannel( | |
56 mojo::embedder::ScopedPlatformHandle( | |
57 mojo::embedder::PlatformHandle(file)), | |
58 io_thread_task_runner, | |
59 base::Bind(&MojoChannelInit::OnCreatedChannelOnIOThread, | |
60 weak_factory_.GetWeakPtr(), | |
61 main_thread_, | |
62 io_thread_task_runner)))); | |
63 } | |
64 | |
65 // static | |
66 void MojoChannelInit::OnCreatedChannelOnIOThread( | |
67 base::WeakPtr<MojoChannelInit> host, | |
68 scoped_refptr<base::TaskRunner> main_thread, | |
69 scoped_refptr<base::TaskRunner> io_thread, | |
70 mojo::embedder::ChannelInfo* channel) { | |
71 main_thread->PostTask( | |
72 FROM_HERE, | |
73 base::Bind(&MojoChannelInit::OnCreatedChannelOnMainThread, host, | |
74 io_thread, channel)); | |
75 } | |
76 | |
77 // static | |
78 void MojoChannelInit::OnCreatedChannelOnMainThread( | |
79 base::WeakPtr<MojoChannelInit> host, | |
80 scoped_refptr<base::TaskRunner> io_thread, | |
81 mojo::embedder::ChannelInfo* channel) { | |
82 // By the time we get here |host| may have been destroyed. If so, shutdown the | |
83 // channel. | |
84 if (!host.get()) { | |
85 io_thread->PostTask( | |
86 FROM_HERE, | |
87 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); | |
88 return; | |
89 } | |
90 host->channel_info_ = channel; | |
91 } | |
92 | |
93 | |
94 } // namespace content | |
OLD | NEW |