summaryrefslogtreecommitdiffstats
path: root/mojo/shell/runner/child/runner_connection.cc
blob: f1478d599edd37450ad31cb75dc46519eb19b229 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2015 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/runner/child/runner_connection.h"

#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/shell/public/interfaces/shell_client_factory.mojom.h"
#include "mojo/shell/runner/common/switches.h"

namespace mojo {
namespace shell {
namespace {

class RunnerConnectionImpl : public RunnerConnection,
                             public mojom::ShellClientFactory {
 public:
  RunnerConnectionImpl(mojo::ShellConnection* shell_connetion,
                       mojom::ShellClientFactoryRequest client_factory_request,
                       bool exit_on_error)
      : shell_connection_(shell_connetion),
        binding_(this, std::move(client_factory_request)),
        exit_on_error_(exit_on_error) {
    binding_.set_connection_error_handler([this] { OnConnectionError(); });
  }

  ~RunnerConnectionImpl() override {}

 private:
  // mojom::ShellClientFactory:
  void CreateShellClient(mojom::ShellClientRequest client_request,
                         const String& name) override {
    shell_connection_->BindToRequest(std::move(client_request));
  }

  void OnConnectionError() {
    // A connection error means the connection to the shell is lost. This is not
    // recoverable.
    DLOG(ERROR) << "Connection error to the shell.";
    if (exit_on_error_)
      _exit(1);
  }

  mojo::ShellConnection* const shell_connection_;
  Binding<mojom::ShellClientFactory> binding_;
  const bool exit_on_error_;

  DISALLOW_COPY_AND_ASSIGN(RunnerConnectionImpl);
};

}  // namespace

RunnerConnection::~RunnerConnection() {}

// static
scoped_ptr<RunnerConnection> RunnerConnection::Create(
    mojo::ShellConnection* shell_connection,
    bool exit_on_error) {
  std::string primordial_pipe_token =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kPrimordialPipeToken);
  mojom::ShellClientFactoryRequest client_factory_request;
  client_factory_request.Bind(
      edk::CreateChildMessagePipe(primordial_pipe_token));

  return make_scoped_ptr(new RunnerConnectionImpl(
      shell_connection, std::move(client_factory_request), exit_on_error));
}

RunnerConnection::RunnerConnection() {}

}  // namespace shell
}  // namespace mojo