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/embedder/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 embedder { 13 14 ChannelInit::ChannelInit() 15 : channel_info_(NULL), 16 weak_factory_(this) { 17 } 18 19 ChannelInit::~ChannelInit() { 20 if (channel_info_) { 21 io_thread_task_runner_->PostTask( 22 FROM_HERE, 23 base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_)); 24 } 25 } 26 27 mojo::ScopedMessagePipeHandle ChannelInit::Init( 28 base::PlatformFile file, 29 scoped_refptr<base::TaskRunner> io_thread_task_runner) { 30 DCHECK(!io_thread_task_runner_.get()); // Should only init once. 31 io_thread_task_runner_ = io_thread_task_runner; 32 mojo::ScopedMessagePipeHandle message_pipe = mojo::embedder::CreateChannel( 33 mojo::embedder::ScopedPlatformHandle( 34 mojo::embedder::PlatformHandle(file)), 35 io_thread_task_runner, 36 base::Bind(&ChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(), 37 io_thread_task_runner), 38 base::MessageLoop::current()->message_loop_proxy()).Pass(); 39 return message_pipe.Pass(); 40 } 41 42 // static 43 void ChannelInit::OnCreatedChannel( 44 base::WeakPtr<ChannelInit> 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 embedder 59 } // namespace mojo 60