1 // 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.
5 #include "components/browser_watcher/watcher_client_win.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/process/launch.h"
12 #include "base/win/windows_version.h"
14 namespace browser_watcher
{
18 base::Process
OpenOwnProcessInheritable() {
20 ::OpenProcess(SYNCHRONIZE
| PROCESS_QUERY_INFORMATION
,
21 TRUE
, // Ineritable handle.
22 base::GetCurrentProcId()));
27 WatcherClient::WatcherClient(const CommandLineGenerator
& command_line_generator
)
28 : use_legacy_launch_(base::win::GetVersion() < base::win::VERSION_VISTA
),
29 command_line_generator_(command_line_generator
),
30 process_(base::kNullProcessHandle
) {
33 void WatcherClient::LaunchWatcher() {
34 DCHECK(!process_
.IsValid());
36 // Build the command line for the watcher process.
37 base::Process self
= OpenOwnProcessInheritable();
38 DCHECK(self
.IsValid());
39 base::CommandLine
cmd_line(command_line_generator_
.Run(self
.Handle()));
41 base::HandlesToInheritVector to_inherit
;
42 base::LaunchOptions options
;
43 options
.start_hidden
= true;
44 if (use_legacy_launch_
) {
45 // Launch the child process inheriting all handles on XP.
46 options
.inherit_handles
= true;
48 // Launch the child process inheriting only |self| on
50 to_inherit
.push_back(self
.Handle());
51 to_inherit
.insert(to_inherit
.end(), inherited_handles_
.begin(),
52 inherited_handles_
.end());
53 options
.handles_to_inherit
= &to_inherit
;
56 process_
= base::LaunchProcess(cmd_line
, options
);
57 if (!process_
.IsValid())
58 LOG(ERROR
) << "Failed to launch browser watcher.";
61 void WatcherClient::AddInheritedHandle(HANDLE handle
) {
62 inherited_handles_
.push_back(handle
);
65 } // namespace browser_watcher