Upstream changes for NetworkQualityProvider
[chromium-blink-merge.git] / mojo / runner / child_process_host.h
blob1875ede4bd751a8cb5fb32f093dd343258e91310
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 #ifndef MOJO_RUNNER_CHILD_PROCESS_HOST_H_
6 #define MOJO_RUNNER_CHILD_PROCESS_HOST_H_
8 #include "base/files/file_path.h"
9 #include "base/macros.h"
10 #include "base/process/process.h"
11 #include "mojo/edk/embedder/channel_info_forward.h"
12 #include "mojo/edk/embedder/platform_channel_pair.h"
13 #include "mojo/edk/embedder/scoped_platform_handle.h"
14 #include "mojo/runner/child_process.mojom.h"
15 #include "mojo/runner/child_process_host.h"
17 namespace mojo {
18 namespace runner {
20 class Context;
22 // This class represents a "child process host". Handles launching and
23 // connecting a platform-specific "pipe" to the child, and supports joining the
24 // child process. Currently runs a single app (loaded from the file system).
26 // This class is not thread-safe. It should be created/used/destroyed on a
27 // single thread.
29 // Note: Does not currently work on Windows before Vista.
30 // Note: After |Start()|, |StartApp| must be called and this object must
31 // remained alive until the |on_app_complete| callback is called.
32 class ChildProcessHost {
33 public:
34 // |name| is just for debugging ease. We will spawn off a process so that it
35 // can be sandboxed if |start_sandboxed| is true. |app_path| is a path to the
36 // mojo application we wish to start. |clean_app_path| cleans up transient
37 // applications after execution.
38 ChildProcessHost(Context* context,
39 bool start_sandboxed,
40 const base::FilePath& app_path,
41 bool clean_app_path);
42 virtual ~ChildProcessHost();
44 // |Start()|s the child process; calls |DidStart()| (on the thread on which
45 // |Start()| was called) when the child has been started (or failed to start).
46 // After calling |Start()|, this object must not be destroyed until
47 // |DidStart()| has been called.
48 // TODO(vtl): Consider using weak pointers and removing this requirement.
49 void Start();
51 // Waits for the child process to terminate, and returns its exit code.
52 // Note: If |Start()| has been called, this must not be called until the
53 // callback has been called.
54 int Join();
56 // See |ChildController|:
57 void StartApp(InterfaceRequest<Application> application_request,
58 const ChildController::StartAppCallback& on_app_complete);
59 void ExitNow(int32_t exit_code);
61 protected:
62 // virtual for testing.
63 virtual void DidStart(bool success);
65 private:
66 bool DoLaunch();
68 void AppCompleted(int32_t result);
70 // Callback for |embedder::CreateChannel()|.
71 void DidCreateChannel(embedder::ChannelInfo* channel_info);
73 Context* const context_;
74 bool start_sandboxed_;
75 const base::FilePath app_path_;
76 bool clean_app_path_;
77 base::Process child_process_;
78 embedder::PlatformChannelPair platform_channel_pair_;
79 ChildControllerPtr controller_;
80 embedder::ChannelInfo* channel_info_;
81 ChildController::StartAppCallback on_app_complete_;
83 // Platform-specific "pipe" to the child process. Valid immediately after
84 // creation.
85 embedder::ScopedPlatformHandle platform_channel_;
87 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost);
90 } // namespace runner
91 } // namespace mojo
93 #endif // MOJO_RUNNER_CHILD_PROCESS_HOST_H_