OLD | NEW |
| (Empty) |
1 // Copyright 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 "mojo/common/mojo_channel_init.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "mojo/embedder/embedder.h" | |
10 | |
11 namespace mojo { | |
12 namespace common { | |
13 | |
14 MojoChannelInit::MojoChannelInit() | |
15 : channel_info_(NULL), | |
16 weak_factory_(this) { | |
17 } | |
18 | |
19 MojoChannelInit::~MojoChannelInit() { | |
20 bootstrap_message_pipe_.reset(); | |
21 if (channel_info_) { | |
22 io_thread_task_runner_->PostTask( | |
23 FROM_HERE, | |
24 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); | |
25 } | |
26 } | |
27 | |
28 void MojoChannelInit::Init( | |
29 base::PlatformFile file, | |
30 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | |
31 DCHECK(!io_thread_task_runner_.get()); // Should only init once. | |
32 io_thread_task_runner_ = io_thread_task_runner; | |
33 bootstrap_message_pipe_ = mojo::embedder::CreateChannel( | |
34 mojo::embedder::ScopedPlatformHandle( | |
35 mojo::embedder::PlatformHandle(file)), | |
36 io_thread_task_runner, | |
37 base::Bind(&MojoChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), | |
38 io_thread_task_runner), | |
39 base::MessageLoop::current()->message_loop_proxy()).Pass(); | |
40 } | |
41 | |
42 // static | |
43 void MojoChannelInit::OnCreatedChannel( | |
44 base::WeakPtr<MojoChannelInit> host, | |
45 scoped_refptr<base::TaskRunner> io_thread, | |
46 embedder::ChannelInfo* channel) { | |
47 // By the time we get here |host| may have been destroyed. If so, shutdown the | |
48 // channel. | |
49 if (!host.get()) { | |
50 io_thread->PostTask( | |
51 FROM_HERE, | |
52 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel)); | |
53 return; | |
54 } | |
55 host->channel_info_ = channel; | |
56 } | |
57 | |
58 } // namespace common | |
59 } // namespace mojo | |
OLD | NEW |