Revert of Enable Surfaces on Android (try #2) (patchset #1 id:1 of https://codereview...
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo_host.cc
blobbeb18ee5e99ffc53d7159d1b6cb4c68b8cdcd398
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.
5 #include "ipc/mojo/ipc_channel_mojo_host.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "ipc/mojo/ipc_channel_mojo.h"
11 namespace IPC {
13 // The delete class lives on the IO thread to talk to ChannelMojo on
14 // behalf of ChannelMojoHost.
16 // The object must be touched only on the IO thread.
17 class ChannelMojoHost::ChannelDelegate : public ChannelMojo::Delegate {
18 public:
19 explicit ChannelDelegate(scoped_refptr<base::TaskRunner> io_task_runner);
20 ~ChannelDelegate() override;
22 // ChannelMojo::Delegate
23 base::WeakPtr<Delegate> ToWeakPtr() override;
24 void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) override;
25 scoped_refptr<base::TaskRunner> GetIOTaskRunner() override;
27 // Returns an weak ptr of ChannelDelegate instead of Delegate
28 base::WeakPtr<ChannelDelegate> GetWeakPtr();
29 void OnClientLaunched(base::ProcessHandle process);
30 void DeleteThisSoon();
32 private:
33 scoped_refptr<base::TaskRunner> io_task_runner_;
34 base::WeakPtr<ChannelMojo> channel_;
35 base::WeakPtrFactory<ChannelDelegate> weak_factory_;
37 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate);
40 ChannelMojoHost::ChannelDelegate::ChannelDelegate(
41 scoped_refptr<base::TaskRunner> io_task_runner)
42 : io_task_runner_(io_task_runner), weak_factory_(this) {
45 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() {
48 base::WeakPtr<ChannelMojo::Delegate>
49 ChannelMojoHost::ChannelDelegate::ToWeakPtr() {
50 return weak_factory_.GetWeakPtr();
53 base::WeakPtr<ChannelMojoHost::ChannelDelegate>
54 ChannelMojoHost::ChannelDelegate::GetWeakPtr() {
55 return weak_factory_.GetWeakPtr();
58 void ChannelMojoHost::ChannelDelegate::OnChannelCreated(
59 base::WeakPtr<ChannelMojo> channel) {
60 DCHECK(!channel_);
61 channel_ = channel;
64 scoped_refptr<base::TaskRunner>
65 ChannelMojoHost::ChannelDelegate::GetIOTaskRunner() {
66 return io_task_runner_;
69 void ChannelMojoHost::ChannelDelegate::OnClientLaunched(
70 base::ProcessHandle process) {
71 if (channel_)
72 channel_->OnClientLaunched(process);
75 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() {
76 io_task_runner_->PostTask(
77 FROM_HERE,
78 base::Bind(&base::DeletePointer<ChannelMojoHost::ChannelDelegate>,
79 base::Unretained(this)));
83 // ChannelMojoHost
86 ChannelMojoHost::ChannelMojoHost(scoped_refptr<base::TaskRunner> io_task_runner)
87 : io_task_runner_(io_task_runner),
88 channel_delegate_(new ChannelDelegate(io_task_runner)),
89 weak_factory_(this) {
92 ChannelMojoHost::~ChannelMojoHost() {
95 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) {
96 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) {
97 channel_delegate_->OnClientLaunched(process);
98 } else {
99 io_task_runner_->PostTask(FROM_HERE,
100 base::Bind(&ChannelDelegate::OnClientLaunched,
101 channel_delegate_->GetWeakPtr(),
102 process));
106 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const {
107 return channel_delegate_.get();
110 void ChannelMojoHost::DelegateDeleter::operator()(
111 ChannelMojoHost::ChannelDelegate* ptr) const {
112 ptr->DeleteThisSoon();
115 } // namespace IPC