blob: 0a6cee0a88bed9758178e92b0b3f1c83cc7eee13 [file] [log] [blame]
siggi420541c2014-11-18 23:16:321// Copyright (c) 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
5#ifndef COMPONENTS_BROWSER_WATCHER_WATCHER_CLIENT_WIN_H_
6#define COMPONENTS_BROWSER_WATCHER_WATCHER_CLIENT_WIN_H_
7
erikwright7c4a4262015-01-09 18:32:338#include "base/callback.h"
siggi420541c2014-11-18 23:16:329#include "base/macros.h"
rvargas42ba4692014-12-10 18:40:2110#include "base/process/process.h"
siggi420541c2014-11-18 23:16:3211
erikwright7c4a4262015-01-09 18:32:3312namespace base {
13class CommandLine;
14} // namespace base
15
siggi420541c2014-11-18 23:16:3216namespace browser_watcher {
17
18// An interface class to take care of the details in launching a browser
19// watch process.
20class WatcherClient {
21 public:
erikwright7c4a4262015-01-09 18:32:3322 // A CommandLineGenerator generates command lines that will launch a separate
23 // process and pass the supplied HANDLE to ExitCodeWatcher in that process.
24 typedef base::Callback<base::CommandLine(HANDLE)> CommandLineGenerator;
siggi420541c2014-11-18 23:16:3225
erikwright7c4a4262015-01-09 18:32:3326 // Constructs a watcher client that launches its watcher process using the
27 // command line generated by |command_line_generator|.
28 explicit WatcherClient(const CommandLineGenerator& command_line_generator);
29
thakis56e8586a2015-05-01 18:55:2130 ~WatcherClient();
31
erikwright7c4a4262015-01-09 18:32:3332 // Launches the watcher process such that the child process is able to inherit
33 // a handle to the current process. If use_legacy_launch() is true, this uses
34 // a non-threadsafe legacy launch mode that's compatible with Windows XP.
siggi420541c2014-11-18 23:16:3235 void LaunchWatcher();
36
erikwrightc57091e2015-02-04 15:19:1037 // Ensures that |handle| may be inherited by the watcher process. |handle|
38 // must still be inheritable, and it's the client's responsibility to
39 // communicate the value of |handle| to the launched process.
40 void AddInheritedHandle(HANDLE handle);
41
42 // Returns the launched process.
43 const base::Process& process() const { return process_; }
44
siggi420541c2014-11-18 23:16:3245 // Accessors, exposed only for testing.
46 bool use_legacy_launch() const { return use_legacy_launch_; }
47 void set_use_legacy_launch(bool use_legacy_launch) {
48 use_legacy_launch_ = use_legacy_launch;
49 }
siggi420541c2014-11-18 23:16:3250
51 private:
siggi420541c2014-11-18 23:16:3252 // If true, the watcher process will be launched with XP legacy handle
53 // inheritance. This is not thread safe and can leak random handles into the
54 // child process, but it's the best we can do on XP.
55 bool use_legacy_launch_;
56
erikwright7c4a4262015-01-09 18:32:3357 // The CommandLineGenerator passed to the constructor.
58 CommandLineGenerator command_line_generator_;
siggi420541c2014-11-18 23:16:3259
60 // A handle to the launched watcher process. Valid after a successful
61 // LaunchWatcher() call.
rvargas42ba4692014-12-10 18:40:2162 base::Process process_;
siggi420541c2014-11-18 23:16:3263
erikwrightc57091e2015-02-04 15:19:1064 std::vector<HANDLE> inherited_handles_;
65
siggi420541c2014-11-18 23:16:3266 DISALLOW_COPY_AND_ASSIGN(WatcherClient);
67};
68
69} // namespace browser_watcher
70
71#endif // COMPONENTS_BROWSER_WATCHER_WATCHER_CLIENT_WIN_H_