GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo_host.cc
blob7e1bff9427561110c763cec46747b853d83f645b
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;
33 scoped_refptr<base::TaskRunner> GetIOTaskRunner() override;
35 // Returns an weak ptr of ChannelDelegate instead of Delegate
36 base::WeakPtr<ChannelDelegate> GetWeakPtr();
37 void OnClientLaunched(base::ProcessHandle process);
38 void DeleteThisSoon() const;
40 private:
41 friend class base::DeleteHelper<ChannelDelegate>;
43 ~ChannelDelegate() override;
45 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
46 base::WeakPtr<ChannelMojo> channel_;
47 base::WeakPtrFactory<ChannelDelegate> weak_factory_;
49 DISALLOW_COPY_AND_ASSIGN(ChannelDelegate);
52 ChannelMojoHost::ChannelDelegate::ChannelDelegate(
53 scoped_refptr<base::SequencedTaskRunner> io_task_runner)
54 : io_task_runner_(io_task_runner), weak_factory_(this) {
57 ChannelMojoHost::ChannelDelegate::~ChannelDelegate() {
60 base::WeakPtr<ChannelMojo::Delegate>
61 ChannelMojoHost::ChannelDelegate::ToWeakPtr() {
62 return weak_factory_.GetWeakPtr();
65 base::WeakPtr<ChannelMojoHost::ChannelDelegate>
66 ChannelMojoHost::ChannelDelegate::GetWeakPtr() {
67 return weak_factory_.GetWeakPtr();
70 void ChannelMojoHost::ChannelDelegate::OnChannelCreated(
71 base::WeakPtr<ChannelMojo> channel) {
72 DCHECK(!channel_);
73 channel_ = channel;
76 scoped_refptr<base::TaskRunner>
77 ChannelMojoHost::ChannelDelegate::GetIOTaskRunner() {
78 return io_task_runner_;
81 void ChannelMojoHost::ChannelDelegate::OnClientLaunched(
82 base::ProcessHandle process) {
83 if (channel_)
84 channel_->OnClientLaunched(process);
87 void ChannelMojoHost::ChannelDelegate::DeleteThisSoon() const {
88 io_task_runner_->DeleteSoon(FROM_HERE, this);
92 // ChannelMojoHost
95 ChannelMojoHost::ChannelMojoHost(
96 scoped_refptr<base::SequencedTaskRunner> io_task_runner)
97 : io_task_runner_(io_task_runner),
98 channel_delegate_(new ChannelDelegate(io_task_runner)),
99 weak_factory_(this) {
102 ChannelMojoHost::~ChannelMojoHost() {
105 void ChannelMojoHost::OnClientLaunched(base::ProcessHandle process) {
106 if (io_task_runner_ == base::MessageLoop::current()->message_loop_proxy()) {
107 channel_delegate_->OnClientLaunched(process);
108 } else {
109 io_task_runner_->PostTask(FROM_HERE,
110 base::Bind(&ChannelDelegate::OnClientLaunched,
111 channel_delegate_, process));
115 ChannelMojo::Delegate* ChannelMojoHost::channel_delegate() const {
116 return channel_delegate_.get();
119 // static
120 void ChannelMojoHost::ChannelDelegateTraits::Destruct(
121 const ChannelMojoHost::ChannelDelegate* ptr) {
122 ptr->DeleteThisSoon();
125 } // namespace IPC