blob: 7ae2d9bf127ffd559fe86200c84860042a5a5083 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
James Robinsonb4b7af22014-12-05 11:21:01 -08005#ifndef SHELL_CHILD_PROCESS_HOST_H_
6#define SHELL_CHILD_PROCESS_HOST_H_
James Robinson646469d2014-10-03 15:33:28 -07007
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -07008#include <stdint.h>
9
Viet-Trung Luub712a8d2015-05-20 12:27:16 -070010#include <string>
11
James Robinson646469d2014-10-03 15:33:28 -070012#include "base/macros.h"
Viet-Trung Luud5e21312015-05-20 16:35:04 -070013#include "base/memory/scoped_ptr.h"
James Robinson5e66a792015-01-21 17:02:08 -080014#include "base/process/process.h"
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070015#include "mojo/edk/embedder/channel_info_forward.h"
James Robinson646469d2014-10-03 15:33:28 -070016#include "mojo/edk/embedder/scoped_platform_handle.h"
Viet-Trung Luuf09c0d92015-04-01 14:38:57 -070017#include "shell/child_controller.mojom.h"
James Robinson646469d2014-10-03 15:33:28 -070018
James Robinson646469d2014-10-03 15:33:28 -070019namespace shell {
20
21class Context;
22
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070023// Child process host: parent-process representation of a child process, which
24// hosts/runs a native Mojo application loaded from the file system. This class
25// handles launching and communicating with the child process.
James Robinson646469d2014-10-03 15:33:28 -070026//
27// This class is not thread-safe. It should be created/used/destroyed on a
28// single thread.
Viet-Trung Luu01e64cf2015-07-08 09:55:53 -070029class ChildProcessHost {
James Robinson646469d2014-10-03 15:33:28 -070030 public:
Viet-Trung Luu85fe1092015-03-31 12:10:31 -070031 explicit ChildProcessHost(Context* context);
Viet-Trung Luu01e64cf2015-07-08 09:55:53 -070032 // TODO(vtl): Virtual because |DidStart()| is, even though it shouldn't be
33 // (see |DidStart()|).
34 virtual ~ChildProcessHost();
James Robinson646469d2014-10-03 15:33:28 -070035
Viet-Trung Luu85fe1092015-03-31 12:10:31 -070036 // |Start()|s the child process; calls |DidStart()| (on the thread on which
37 // |Start()| was called) when the child has been started (or failed to start).
38 // After calling |Start()|, this object must not be destroyed until
39 // |DidStart()| has been called.
James Robinson646469d2014-10-03 15:33:28 -070040 // TODO(vtl): Consider using weak pointers and removing this requirement.
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070041 // TODO(vtl): This should probably take a callback instead.
42 // TODO(vtl): Consider merging this with |StartApp()|.
James Robinson646469d2014-10-03 15:33:28 -070043 void Start();
44
45 // Waits for the child process to terminate, and returns its exit code.
46 // Note: If |Start()| has been called, this must not be called until the
47 // callback has been called.
48 int Join();
49
Viet-Trung Luuf09c0d92015-04-01 14:38:57 -070050 // Methods relayed to the |ChildController|. These methods may be only be
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070051 // called after |Start()|, but may be called immediately (without waiting for
52 // |DidStart()|).
James Robinson646469d2014-10-03 15:33:28 -070053
Viet-Trung Luuf09c0d92015-04-01 14:38:57 -070054 // Like |ChildController::StartApp()|, but with one difference:
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070055 // |on_app_complete| will *always* get called, even on connection error (or
56 // even if the child process failed to start at all).
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070057 void StartApp(const mojo::String& app_path,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070058 mojo::InterfaceRequest<mojo::Application> application_request,
Viet-Trung Luuf09c0d92015-04-01 14:38:57 -070059 const ChildController::StartAppCallback& on_app_complete);
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070060 void ExitNow(int32_t exit_code);
Viet-Trung Luu85fe1092015-03-31 12:10:31 -070061
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070062 // TODO(vtl): This is virtual, so tests can override it, but really |Start()|
63 // should take a callback (see above) and this should be private.
Viet-Trung Luud5e21312015-05-20 16:35:04 -070064 virtual void DidStart(base::Process child_process);
James Robinson646469d2014-10-03 15:33:28 -070065
66 private:
Viet-Trung Luud5e21312015-05-20 16:35:04 -070067 struct LaunchData;
68
Viet-Trung Luud2e1b3f2015-06-09 09:55:33 -070069 // Callback for |mojo::embedder::ConnectToSlave()|.
70 void DidConnectToSlave();
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070071
Viet-Trung Luud5e21312015-05-20 16:35:04 -070072 // Note: This is probably executed on a different thread (namely, using the
73 // blocking pool).
74 base::Process DoLaunch(scoped_ptr<LaunchData> launch_data);
James Robinson646469d2014-10-03 15:33:28 -070075
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070076 void AppCompleted(int32_t result);
Viet-Trung Luu01e64cf2015-07-08 09:55:53 -070077 void OnConnectionError();
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070078
James Robinson646469d2014-10-03 15:33:28 -070079 Context* const context_;
James Robinson646469d2014-10-03 15:33:28 -070080
Viet-Trung Luuf09c0d92015-04-01 14:38:57 -070081 ChildControllerPtr controller_;
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070082 mojo::embedder::ChannelInfo* channel_info_;
Viet-Trung Luuf09c0d92015-04-01 14:38:57 -070083 ChildController::StartAppCallback on_app_complete_;
Viet-Trung Luu2cdfa552015-04-01 14:27:43 -070084
85 base::Process child_process_;
James Robinson646469d2014-10-03 15:33:28 -070086
87 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost);
88};
89
90} // namespace shell
James Robinson646469d2014-10-03 15:33:28 -070091
James Robinsonb4b7af22014-12-05 11:21:01 -080092#endif // SHELL_CHILD_PROCESS_HOST_H_