summaryrefslogtreecommitdiffstats
path: root/mojo/shell/tests/lifecycle/app_client.cc
blob: a2400ec9a3a33d43e49760fa08b80572309c10b4 (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
60
61
62
63
64
// Copyright 2016 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/shell/tests/lifecycle/app_client.h"

#include "mojo/shell/public/cpp/shell_connection.h"

namespace mojo {
namespace shell {
namespace test {

AppClient::AppClient() {}
AppClient::AppClient(shell::mojom::ShellClientRequest request)
    : connection_(new ShellConnection(this, std::move(request))) {}
AppClient::~AppClient() {}

bool AppClient::AcceptConnection(mojo::Connection* connection) {
  connection->AddInterface<LifecycleControl>(this);
  return true;
}

void AppClient::ShellConnectionLost() {
  GracefulQuit();
}

void AppClient::Create(mojo::Connection* connection,
                       LifecycleControlRequest request) {
  bindings_.AddBinding(this, std::move(request));
}

void AppClient::Ping(const PingCallback& callback) {
  callback.Run();
}

void AppClient::GracefulQuit() {
  base::MessageLoop::current()->QuitWhenIdle();
}

void AppClient::Crash() {
  // Rather than actually crash, which causes a bunch of console spray and
  // maybe UI clutter on some platforms, just exit without shutting anything
  // down properly.
  exit(1);
}

void AppClient::CloseShellConnection() {
  DCHECK(runner_);
  runner_->DestroyShellConnection();
  // Quit the app once the caller goes away.
  bindings_.set_connection_error_handler(
      base::Bind(&AppClient::BindingLost, base::Unretained(this)));
}

void AppClient::BindingLost() {
  if (bindings_.empty())
    GracefulQuit();
}

}  // namespace test
}  // namespace shell
}  // namespace mojo