Chromium Code Reviews| Index: apps/app_host/test_operation.cc |
| diff --git a/apps/app_host/test_operation.cc b/apps/app_host/test_operation.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85e00e2081141e6233f918f40b6fd5ce2bf58a38 |
| --- /dev/null |
| +++ b/apps/app_host/test_operation.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <windows.h> |
| + |
| +#include "base/at_exit.h" |
| +#include "base/command_line.h" |
| +#include "base/process_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/win/scoped_handle.h" |
| +#include "chrome/browser/operation_output.h" |
| +#include "chrome/common/chrome_switches.h" |
| + |
| +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.
|
| +const char* kWait = "wait"; |
| +const char* kOutputLength = "output-length"; |
| +const char* kExitCode = "exit-code"; |
| + |
| +int APIENTRY wWinMain(HINSTANCE, HINSTANCE, wchar_t*, int) { |
| + base::AtExitManager exit_manager; |
| + base::win::ScopedHandle child_ready_event( |
| + ::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.
|
| + if (!child_ready_event) |
| + return 1; |
|
gab
2013/03/28 03:06:06
Define some error code constant to 1 instead of us
|
| + |
| + // Initialize the commandline singleton from the environment. |
| + 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.
|
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + if (command_line->HasSwitch("delegate")) { |
| + CommandLine child_command_line(command_line->GetProgram()); |
| + const char* forwarded_switches[] = { |
| + switches::kTaskRemoteProcessId, |
| + switches::kTaskOutputHandle, |
| + switches::kTaskResultHandle, |
| + kWait, |
| + kOutputLength, |
| + kExitCode}; |
| + child_command_line.CopySwitchesFrom( |
| + *command_line, forwarded_switches, arraysize(forwarded_switches)); |
| + if (!base::LaunchProcess(child_command_line, base::LaunchOptions(), NULL)) |
| + return 1; |
| + if (command_line->HasSwitch(kDelegateWait)) { |
| + base::win::ScopedHandle delegate_wait_event( |
| + ::CreateEvent(NULL, TRUE, FALSE, |
| + L"AppHostTestOperationDelegateWait")); |
| + if (::WaitForSingleObject(delegate_wait_event, 10000) != WAIT_OBJECT_0) |
| + return 1; |
| + } |
| + } else { |
| + scoped_ptr<OperationOutput> operation_output = |
| + OperationOutput::Create(*command_line); |
| + if (!operation_output) |
| + return 1; |
| + if (!::SetEvent(child_ready_event)) |
| + return 1; |
| + if (command_line->HasSwitch(kWait)) { |
| + base::win::ScopedHandle child_wait_event( |
| + ::CreateEvent(NULL, TRUE, FALSE, L"AppHostTestOperationChildWait")); |
| + if (::WaitForSingleObject(child_wait_event, 10000) != WAIT_OBJECT_0) |
|
gab
2013/03/28 03:06:06
Constantify 10000 here and above into something li
|
| + return 1; |
| + } |
| + |
| + std::string output_length_string = command_line->GetSwitchValueASCII( |
| + kOutputLength); |
| + std::string exit_code_string = command_line->GetSwitchValueASCII( |
| + kExitCode); |
| + unsigned output_length = 0; |
| + unsigned exit_code = 0; |
| + if (!base::StringToUint(output_length_string, |
| + reinterpret_cast<unsigned*>(&output_length)) || |
| + !base::StringToUint(output_length_string, |
| + reinterpret_cast<unsigned*>(&output_length))) { |
| + return 1; |
| + } |
| + std::vector<char> buffer(output_length); |
| + for (size_t i = 0; i < buffer.size(); ++i) { |
| + buffer[i] = '0' + i % 10; |
| + } |
| + if (!operation_output->Write(&buffer[0], buffer.size())) |
| + return 1; |
| + if (!OperationOutput::Complete(operation_output.Pass(), exit_code)) |
| + return 1; |
| + } |
| + |
| + return 0; |
| +} |