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 "chrome/app/chrome_watcher_command_line_win.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/common/chrome_switches.h"
17 const char kParentHandleSwitch
[] = "parent-handle";
20 base::CommandLine
GenerateChromeWatcherCommandLine(
21 const base::FilePath
& chrome_exe
,
22 HANDLE parent_process
) {
23 base::CommandLine
command_line(chrome_exe
);
24 command_line
.AppendSwitchASCII(switches::kProcessType
, "watcher");
25 command_line
.AppendSwitchASCII(
27 base::UintToString(reinterpret_cast<unsigned int>(parent_process
)));
31 base::win::ScopedHandle
InterpretChromeWatcherCommandLine(
32 const base::CommandLine
& command_line
) {
33 std::string parent_handle_str
=
34 command_line
.GetSwitchValueASCII(kParentHandleSwitch
);
35 unsigned parent_handle_uint
= 0;
36 if (parent_handle_str
.empty() ||
37 !base::StringToUint(parent_handle_str
, &parent_handle_uint
)) {
38 LOG(ERROR
) << "Missing or invalid " << kParentHandleSwitch
<< " argument.";
39 return base::win::ScopedHandle();
42 HANDLE parent_process
= reinterpret_cast<HANDLE
>(parent_handle_uint
);
43 // Initial test of the handle, a zero PID indicates invalid handle, or not
44 // a process handle. In this case, parsing fails and we avoid closing the
46 DWORD process_pid
= ::GetProcessId(parent_process
);
47 if (process_pid
== 0) {
48 LOG(ERROR
) << "Invalid " << kParentHandleSwitch
49 << " argument. Can't get parent PID.";
50 return base::win::ScopedHandle();
53 return base::win::ScopedHandle(parent_process
);