Use null rather than empty frame to lock Surface resources
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo_host.cc
blob28067cf74679dd48db8f713ff1584f580bc6524d
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 class ChannelMojoHost::ChannelDelegateTraits {
14 public:
15 static void Destruct(const ChannelMojoHost::ChannelDelegate* ptr);
18 // The delete class lives on the IO thread to talk to ChannelMojo on
19 // behalf of ChannelMojoHost.
21 // The object must be touched only on the IO thread.
22 class ChannelMojoHost::ChannelDelegate
23 : public base::RefCountedThreadSafe<ChannelMojoHost::ChannelDelegate,
24 ChannelMojoHost::ChannelDelegateTraits>,
25 public ChannelMojo::Delegate {
26 public:
27 explicit ChannelDelegate(
28 scoped_refptr<base::SequencedTaskRunner> io_task_runner);
30 // ChannelMojo::Delegate
31 base::WeakPtr<Delegate> ToWeakPtr() override;
32 void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) override;
34 // Returns an weak ptr of ChannelDelegate instead of Delegate
35 base::WeakPtr<ChannelDelegate> GetWeakPtr();
36 void OnClientLaunched(base::ProcessHandle process);
37 void DeleteThisSoon() const;
39 private:
40 friend class base::DeleteHelper<ChannelDelegate>;
42 ~ChannelDelegate() override;
44 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
45 base::WeakPtr<ChannelMojo> channel_;
46 base::WeakPtrFactory<ChannelDelegate> weak_factory_;
48 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate);
51 ChannelMojoHost::ChannelDelegate::ChannelDelegate(
52 scoped_refptr<base::SequencedTaskRunner> io_task_runner)
53 : io_task_runner_(io_task_runner), weak_factory_(this) {
56 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() {
59 base::WeakPtr<ChannelMojo::Delegate>
60 ChannelMojoHost::ChannelDelegate::ToWeakPtr() {
61 return weak_factory_.GetWeakPtr();
64 base::WeakPtr<ChannelMojoHost::ChannelDelegate>
65 ChannelMojoHost::ChannelDelegate::GetWeakPtr() {
66 return weak_factory_.GetWeakPtr();
69 void ChannelMojoHost::ChannelDelegate::OnChannelCreated(
70 base::WeakPtr<ChannelMojo> channel) {
71 DCHECK(!channel_);
72 channel_ = channel;
75 void ChannelMojoHost::ChannelDelegate::OnClientLaunched(
76 base::ProcessHandle process) {
77 if (channel_)
78 channel_->OnClientLaunched(process);
81 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() const {
82 io_task_runner_->DeleteSoon(FROM_HERE, this);
86 // ChannelMojoHost
89 ChannelMojoHost::ChannelMojoHost(
90 scoped_refptr<base::SequencedTaskRunner> io_task_runner)
91 : io_task_runner_(io_task_runner),
92 channel_delegate_(new ChannelDelegate(io_task_runner)),
93 weak_factory_(this) {
96 ChannelMojoHost::~ChannelMojoHost() {
99 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) {
100 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) {
101 channel_delegate_->OnClientLaunched(process);
102 } else {
103 io_task_runner_->PostTask(FROM_HERE,
104 base::Bind(&ChannelDelegate::OnClientLaunched,
105 channel_delegate_, process));
109 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const {
110 return channel_delegate_.get();
113 // static
114 void ChannelMojoHost::ChannelDelegateTraits::Destruct(
115 const ChannelMojoHost::ChannelDelegate* ptr) {
116 ptr->DeleteThisSoon();
119 } // namespace IPC