summaryrefslogtreecommitdiffstats
path: root/mojo/common/channel_init.cc
blob: 7eef2aec705d47c8b6f07e79b521fbb43c359b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/common/channel_init.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "mojo/embedder/embedder.h"

namespace mojo {
namespace common {

ChannelInit::ChannelInit()
    : channel_info_(NULL),
      weak_factory_(this) {
}

ChannelInit::~ChannelInit() {
  if (channel_info_) {
    io_thread_task_runner_->PostTask(
        FROM_HERE,
        base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_));
  }
}

mojo::ScopedMessagePipeHandle ChannelInit::Init(
    base::PlatformFile file,
    scoped_refptr<base::TaskRunner> io_thread_task_runner) {
  DCHECK(!io_thread_task_runner_.get());  // Should only init once.
  io_thread_task_runner_ = io_thread_task_runner;
  mojo::ScopedMessagePipeHandle message_pipe = mojo::embedder::CreateChannel(
      mojo::embedder::ScopedPlatformHandle(
          mojo::embedder::PlatformHandle(file)),
      io_thread_task_runner,
      base::Bind(&ChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(),
                 io_thread_task_runner),
      base::MessageLoop::current()->message_loop_proxy()).Pass();
  return message_pipe.Pass();
}

// static
void ChannelInit::OnCreatedChannel(
    base::WeakPtr<ChannelInit> host,
    scoped_refptr<base::TaskRunner> io_thread,
    embedder::ChannelInfo* channel) {
  // By the time we get here |host| may have been destroyed. If so, shutdown the
  // channel.
  if (!host.get()) {
    io_thread->PostTask(
        FROM_HERE,
        base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel));
    return;
  }
  host->channel_info_ = channel;
}

}  // namespace common
}  // namespace mojo