Precalculate total size for safe-browsing add_prefixes.
[chromium-blink-merge.git] / chrome_frame / update_launcher.cc
blob2aff8adedd60f36fb3135a2347d3b5b9b21f3178
1 // Copyright (c) 2011 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_frame/update_launcher.h"
7 #include <windows.h>
8 #include <Shellapi.h>
10 #include "google_update_idl.h" // NOLINT
12 namespace {
14 const wchar_t kChromeFrameGuid[] = L"{8BA986DA-5100-405E-AA35-86F34A02ACBF}";
16 const DWORD kLaunchFailureExitCode = 0xFF;
18 const wchar_t kUpdateCommandFlag[] = L"--update-cmd";
20 // Waits indefinitely for the provided process to exit. Returns the process's
21 // exit code, or kLaunchFailureExitCode if an error occurs in the waiting.
22 DWORD WaitForProcessExitCode(HANDLE handle) {
23 DWORD exit_code = 0;
25 DWORD wait_result = ::WaitForSingleObject(handle, INFINITE);
27 if (wait_result == WAIT_OBJECT_0 && ::GetExitCodeProcess(handle, &exit_code))
28 return exit_code;
30 return kLaunchFailureExitCode;
33 } // namespace
35 namespace update_launcher {
37 std::wstring GetUpdateCommandFromArguments(const wchar_t* command_line) {
38 std::wstring command;
40 if (command_line != NULL) {
41 int num_args = 0;
42 wchar_t** args = NULL;
43 args = ::CommandLineToArgvW(command_line, &num_args);
45 if (args) {
46 if (num_args == 3 && _wcsicmp(args[1], kUpdateCommandFlag) == 0)
47 command = args[2];
48 ::LocalFree(args);
52 return command;
55 // Because we do not have 'base' and all of its pretty RAII helpers, please
56 // ensure that this function only ever contains a single 'return', in order
57 // to reduce the chance of introducing a leak.
58 DWORD LaunchUpdateCommand(const std::wstring& command) {
59 DWORD exit_code = kLaunchFailureExitCode;
61 HRESULT hr = ::CoInitialize(NULL);
63 if (SUCCEEDED(hr)) {
64 IProcessLauncher* ipl = NULL;
65 HANDLE process = NULL;
67 hr = ::CoCreateInstance(__uuidof(ProcessLauncherClass), NULL,
68 CLSCTX_ALL, __uuidof(IProcessLauncher),
69 reinterpret_cast<void**>(&ipl));
71 if (SUCCEEDED(hr)) {
72 ULONG_PTR phandle = NULL;
73 DWORD id = ::GetCurrentProcessId();
75 hr = ipl->LaunchCmdElevated(kChromeFrameGuid,
76 command.c_str(), id, &phandle);
77 if (SUCCEEDED(hr)) {
78 process = reinterpret_cast<HANDLE>(phandle);
79 exit_code = WaitForProcessExitCode(process);
83 if (process)
84 ::CloseHandle(process);
85 if (ipl)
86 ipl->Release();
88 ::CoUninitialize();
91 return exit_code;
94 } // namespace process_launcher