Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 <windows.h> | |
| 6 | |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/process_util.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/win/scoped_handle.h" | |
| 12 #include "chrome/browser/operation_output.h" | |
| 13 #include "chrome/common/chrome_switches.h" | |
| 14 | |
| 15 const char* kDelegateWait = "delegate-wait"; | |
|
gab
2013/03/28 03:06:06
Use char[] instead of char* for literal constants.
erikwright (departed)
2013/04/18 17:43:04
Done.
| |
| 16 const char* kWait = "wait"; | |
| 17 const char* kOutputLength = "output-length"; | |
| 18 const char* kExitCode = "exit-code"; | |
| 19 | |
| 20 int APIENTRY wWinMain(HINSTANCE, HINSTANCE, wchar_t*, int) { | |
| 21 base::AtExitManager exit_manager; | |
| 22 base::win::ScopedHandle child_ready_event( | |
| 23 ::CreateEvent(NULL, TRUE, FALSE, L"AppHostTestOperationChildReady")); | |
|
gab
2013/03/28 03:06:06
Add "Local\" prefix to the event name to make sure
erikwright (departed)
2013/04/18 17:43:04
Done.
| |
| 24 if (!child_ready_event) | |
| 25 return 1; | |
|
gab
2013/03/28 03:06:06
Define some error code constant to 1 instead of us
| |
| 26 | |
| 27 // Initialize the commandline singleton from the environment. | |
| 28 CommandLine::Init(0, NULL); | |
|
gab
2013/03/28 03:06:06
Feels a bit weird that CommandLine::Init() takes a
erikwright (departed)
2013/04/18 17:43:04
Agreed.
| |
| 29 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 30 if (command_line->HasSwitch("delegate")) { | |
| 31 CommandLine child_command_line(command_line->GetProgram()); | |
| 32 const char* forwarded_switches[] = { | |
| 33 switches::kTaskRemoteProcessId, | |
| 34 switches::kTaskOutputHandle, | |
| 35 switches::kTaskResultHandle, | |
| 36 kWait, | |
| 37 kOutputLength, | |
| 38 kExitCode}; | |
| 39 child_command_line.CopySwitchesFrom( | |
| 40 *command_line, forwarded_switches, arraysize(forwarded_switches)); | |
| 41 if (!base::LaunchProcess(child_command_line, base::LaunchOptions(), NULL)) | |
| 42 return 1; | |
| 43 if (command_line->HasSwitch(kDelegateWait)) { | |
| 44 base::win::ScopedHandle delegate_wait_event( | |
| 45 ::CreateEvent(NULL, TRUE, FALSE, | |
| 46 L"AppHostTestOperationDelegateWait")); | |
| 47 if (::WaitForSingleObject(delegate_wait_event, 10000) != WAIT_OBJECT_0) | |
| 48 return 1; | |
| 49 } | |
| 50 } else { | |
| 51 scoped_ptr<OperationOutput> operation_output = | |
| 52 OperationOutput::Create(*command_line); | |
| 53 if (!operation_output) | |
| 54 return 1; | |
| 55 if (!::SetEvent(child_ready_event)) | |
| 56 return 1; | |
| 57 if (command_line->HasSwitch(kWait)) { | |
| 58 base::win::ScopedHandle child_wait_event( | |
| 59 ::CreateEvent(NULL, TRUE, FALSE, L"AppHostTestOperationChildWait")); | |
| 60 if (::WaitForSingleObject(child_wait_event, 10000) != WAIT_OBJECT_0) | |
|
gab
2013/03/28 03:06:06
Constantify 10000 here and above into something li
| |
| 61 return 1; | |
| 62 } | |
| 63 | |
| 64 std::string output_length_string = command_line->GetSwitchValueASCII( | |
| 65 kOutputLength); | |
| 66 std::string exit_code_string = command_line->GetSwitchValueASCII( | |
| 67 kExitCode); | |
| 68 unsigned output_length = 0; | |
| 69 unsigned exit_code = 0; | |
| 70 if (!base::StringToUint(output_length_string, | |
| 71 reinterpret_cast<unsigned*>(&output_length)) || | |
| 72 !base::StringToUint(output_length_string, | |
| 73 reinterpret_cast<unsigned*>(&output_length))) { | |
| 74 return 1; | |
| 75 } | |
| 76 std::vector<char> buffer(output_length); | |
| 77 for (size_t i = 0; i < buffer.size(); ++i) { | |
| 78 buffer[i] = '0' + i % 10; | |
| 79 } | |
| 80 if (!operation_output->Write(&buffer[0], buffer.size())) | |
| 81 return 1; | |
| 82 if (!OperationOutput::Complete(operation_output.Pass(), exit_code)) | |
| 83 return 1; | |
| 84 } | |
| 85 | |
| 86 return 0; | |
| 87 } | |
| OLD | NEW |