+ base::PlatformFile read_file;
+ base::PlatformFile write_file;
+ bool needs_elevation = false;
+
+#if defined(OS_WIN)
+ needs_elevation = !IsProcessElevated();
+
+ if (command_line->HasSwitch(kElevatingSwitchName)) {
+ DCHECK(!needs_elevation);
+
+ // The "elevate" switch is always accompanied by the "input" and "output"
+ // switches whose values name named pipes that should be used in place of
+ // stdin and stdout.
+ DCHECK(command_line->HasSwitch(kInputSwitchName));
+ DCHECK(command_line->HasSwitch(kOutputSwitchName));
+
+ // presubmit: allow wstring
+ std::wstring input_pipe_name =
+ command_line->GetSwitchValueNative(kInputSwitchName);
+ // presubmit: allow wstring
+ std::wstring output_pipe_name =
+ command_line->GetSwitchValueNative(kOutputSwitchName);
+
+ // A NULL SECURITY_ATTRIBUTES signifies that the handle can't be inherited
+ read_file = CreateFile(
+ input_pipe_name.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL, NULL);
+ if (read_file == INVALID_HANDLE_VALUE) {
+ LOG_GETLASTERROR(ERROR) <<
+ "CreateFile failed on '" << input_pipe_name << "'";
+ return kInitializationFailed;
+ }
+
+ write_file = CreateFile(
+ output_pipe_name.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL, NULL);
+ if (write_file == INVALID_HANDLE_VALUE) {
+ LOG_GETLASTERROR(ERROR) <<
+ "CreateFile failed on '" << output_pipe_name << "'";
+ return kInitializationFailed;
+ }
+ } else {
+ // GetStdHandle() returns pseudo-handles for stdin and stdout even if
+ // the hosting executable specifies "Windows" subsystem. However the
+ // returned handles are invalid in that case unless standard input and
+ // output are redirected to a pipe or file.
+ read_file = GetStdHandle(STD_INPUT_HANDLE);
+ write_file = GetStdHandle(STD_OUTPUT_HANDLE);
+ }
+#elif defined(OS_POSIX)
+ read_file = STDIN_FILENO;
+ write_file = STDOUT_FILENO;
+#else
+#error Not implemented.
+#endif
+