summaryrefslogtreecommitdiffstats
path: root/mojo/shell/child_process_host.h
blob: b815c3b79142aaaa5014ec531db7f840ee5d0d06 (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
// Copyright 2014 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.

#ifndef MOJO_SHELL_CHILD_PROCESS_HOST_H_
#define MOJO_SHELL_CHILD_PROCESS_HOST_H_

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/process/process_handle.h"
#include "mojo/shell/child_process.h"  // For |ChildProcess::Type|.
#include "mojo/system/embedder/scoped_platform_handle.h"

namespace base {
class TaskRunner;
}

namespace mojo {
namespace shell {

class Context;

// (Base) class for a "child process host". Handles launching and connecting a
// platform-specific "pipe" to the child, and supports joining the child
// process. Intended for use as a base class, but may be used on its own in
// simple cases.
//
// This class is not thread-safe. It should be created/used/destroyed on a
// single thread.
//
// Note: Does not currently work on Windows before Vista.
class ChildProcessHost {
 public:
  ChildProcessHost(Context* context, ChildProcess::Type type);
  ~ChildProcessHost();

  // |Start()|s a
  typedef base::Callback<void(bool success)> DidStartCallback;
  void Start(DidStartCallback callback);

  // Waits for the child process to terminate, and returns its exit code.
  // Note: If |Start()| has been called, this must not be called until the
  // callback has been called.
  int Join();

  embedder::ScopedPlatformHandle* platform_channel() {
    return &platform_channel_;
  }

 private:
  bool DoLaunch();

  scoped_refptr<base::TaskRunner> process_launch_task_runner_;
  const ChildProcess::Type type_;

  base::ProcessHandle child_process_handle_;

  // Platform-specific "pipe" to the child process.
  // Valid after the |Start()| callback has been completed (successfully).
  embedder::ScopedPlatformHandle platform_channel_;

  DISALLOW_COPY_AND_ASSIGN(ChildProcessHost);
};

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_CHILD_PROCESS_HOST_H_