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 WatcherClient::~WatcherClient() {
36 void WatcherClient::LaunchWatcher() {
37 DCHECK(!process_
.IsValid());
39 // Build the command line for the watcher process.
40 base::Process self
= OpenOwnProcessInheritable();
41 DCHECK(self
.IsValid());
42 base::CommandLine
cmd_line(command_line_generator_
.Run(self
.Handle()));
44 base::HandlesToInheritVector to_inherit
;
45 base::LaunchOptions options
;
46 options
.start_hidden
= true;
47 if (use_legacy_launch_
) {
48 // Launch the child process inheriting all handles on XP.
49 options
.inherit_handles
= true;
51 // Launch the child process inheriting only |self| on
53 to_inherit
.push_back(self
.Handle());
54 to_inherit
.insert(to_inherit
.end(), inherited_handles_
.begin(),
55 inherited_handles_
.end());
56 options
.handles_to_inherit
= &to_inherit
;
59 process_
= base::LaunchProcess(cmd_line
, options
);
60 if (!process_
.IsValid())
61 LOG(ERROR
) << "Failed to launch browser watcher.";
64 void WatcherClient::AddInheritedHandle(HANDLE handle
) {
65 inherited_handles_
.push_back(handle
);
68 } // namespace browser_watcher