OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/browser_watcher/watcher_client_win.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/process/launch.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/win/windows_version.h" | |
13 #include "components/browser_watcher/exit_code_watcher_win.h" | |
14 | |
15 namespace browser_watcher { | |
16 | |
17 namespace { | |
18 | |
19 base::win::ScopedHandle OpenOwnProcessInheritable() { | |
20 return base::win::ScopedHandle( | |
21 ::OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, | |
22 TRUE, // Ineritable handle. | |
23 base::GetCurrentProcId())); | |
24 } | |
25 | |
26 void AddHandleArgument(base::ProcessHandle handle, | |
27 base::CommandLine* cmd_line) { | |
28 cmd_line->AppendSwitchASCII(ExitCodeWatcher::kParenthHandleSwitch, | |
29 base::StringPrintf("%d", handle)); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 WatcherClient::WatcherClient(const base::CommandLine& base_command_line) : | |
35 use_legacy_launch_(base::win::GetVersion() < base::win::VERSION_VISTA), | |
36 base_command_line_(base_command_line), | |
37 process_(base::kNullProcessHandle) { | |
38 } | |
39 | |
40 base::win::ScopedHandle WatcherClient::LaunchWatcherProcess( | |
41 const base::CommandLine& cmd_line, base::ProcessHandle handle) { | |
42 base::HandlesToInheritVector to_inherit; | |
43 base::LaunchOptions options; | |
44 options.start_hidden = true; | |
45 if (use_legacy_launch_) { | |
46 // Launch the child process inheriting all handles on XP. | |
47 options.inherit_handles = true; | |
48 } else { | |
49 // Launch the child process inheriting only |handle| on | |
50 // Vista and better. | |
51 to_inherit.push_back(handle); | |
52 options.handles_to_inherit = &to_inherit; | |
53 } | |
54 | |
55 base::ProcessHandle process = base::kNullProcessHandle; | |
56 if (!base::LaunchProcess(cmd_line, options, &process)) { | |
erikwright (departed)
2014/11/18 21:25:32
nit: braces unnecessary for single-line block
Sigurður Ásgeirsson
2014/11/18 21:33:20
Done.
| |
57 LOG(ERROR) << "Failed to launch browser watcher."; | |
58 } | |
59 | |
60 return base::win::ScopedHandle(process); | |
61 } | |
62 | |
63 void WatcherClient::LaunchWatcher() { | |
64 DCHECK(!process_.IsValid()); | |
65 | |
66 // Build the command line for the watcher process. | |
67 base::win::ScopedHandle self(OpenOwnProcessInheritable()); | |
68 DCHECK(self.IsValid()); | |
69 base::CommandLine cmd_line(base_command_line_); | |
70 AddHandleArgument(self.Get(), &cmd_line); | |
71 | |
72 // Launch it. | |
73 process_ = LaunchWatcherProcess(cmd_line, self.Get()); | |
74 } | |
75 | |
76 } // namespace browser_watcher | |
OLD | NEW |