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"
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
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
{
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
,
40 const base::FilePath
& 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.
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.
56 // See |ChildController|:
57 void StartApp(InterfaceRequest
<Application
> application_request
,
58 const ChildController::StartAppCallback
& on_app_complete
);
59 void ExitNow(int32_t exit_code
);
62 // virtual for testing.
63 virtual void DidStart(bool success
);
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_
;
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
85 embedder::ScopedPlatformHandle platform_channel_
;
87 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost
);
93 #endif // MOJO_RUNNER_CHILD_PROCESS_HOST_H_