summaryrefslogtreecommitdiffstats
path: root/mojo/shell/runner/host/child_process_host.h
blob: 6c6411dc7bcab384caebcf41435ec022710f18dc (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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_RUNNER_HOST_CHILD_PROCESS_HOST_H_
#define MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_

#include <stdint.h>

#include <string>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/shell/public/cpp/identity.h"
#include "mojo/shell/public/interfaces/shell_client_factory.mojom.h"
#include "mojo/shell/runner/host/child_process_host.h"

namespace base {
class TaskRunner;
}

namespace mojo {
class Identity;
namespace shell {

class NativeRunnerDelegate;

// This class represents a "child process host". Handles launching and
// connecting a platform-specific "pipe" to the child, and supports joining the
// child process. Currently runs a single app (loaded from the file system).
//
// 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.
// Note: After |Start()|, |StartApp| must be called and this object must
// remained alive until the |on_app_complete| callback is called.
class ChildProcessHost {
 public:
  using ProcessReadyCallback = base::Callback<void(base::ProcessId)>;

  // |name| is just for debugging ease. We will spawn off a process so that it
  // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the
  // mojo application we wish to start.
  ChildProcessHost(base::TaskRunner* launch_process_runner,
                   NativeRunnerDelegate* delegate,
                   bool start_sandboxed,
                   const Identity& target,
                   const base::FilePath& app_path);
  virtual ~ChildProcessHost();

  // |Start()|s the child process; calls |DidStart()| (on the thread on which
  // |Start()| was called) when the child has been started (or failed to start).
  void Start(mojom::ShellClientRequest request,
             const String& name,
             const ProcessReadyCallback& callback,
             const base::Closure& quit_closure);

  // Waits for the child process to terminate.
  void Join();

 protected:
  void DidStart(const ProcessReadyCallback& callback);

 private:
  void DoLaunch();

  scoped_refptr<base::TaskRunner> launch_process_runner_;
  NativeRunnerDelegate* delegate_ = nullptr;
  bool start_sandboxed_ = false;
  Identity target_;
  const base::FilePath app_path_;
  base::Process child_process_;
  // Used for the ShellClientFactory binding.
  edk::PlatformChannelPair platform_channel_pair_;
  mojom::ShellClientFactoryPtr factory_;
  edk::HandlePassingInformation handle_passing_info_;

  // Used to back the NodeChannel between the parent and child node.
  scoped_ptr<edk::PlatformChannelPair> node_channel_;

  // Since Start() calls a method on another thread, we use an event to block
  // the main thread if it tries to destruct |this| while launching the process.
  base::WaitableEvent start_child_process_event_;

  // A token the child can use to connect a primordial pipe to the host.
  std::string primordial_pipe_token_;

  base::WeakPtrFactory<ChildProcessHost> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ChildProcessHost);
};

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_RUNNER_HOST_CHILD_PROCESS_HOST_H_