OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <windows.h> | 5 #include <windows.h> |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/process_util.h" | 11 #include "base/process_util.h" |
12 #include "chrome/browser/extensions/app_host/binaries_installer.h" | 12 #include "chrome/browser/extensions/app_host/binaries_installer.h" |
13 #include "chrome/browser/extensions/app_host/update.h" | 13 #include "chrome/browser/extensions/app_host/update.h" |
14 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | 14 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
15 | 15 |
| 16 namespace { |
| 17 |
| 18 HRESULT LaunchChromeAndProceed(const CommandLine& cmd_line) { |
| 19 bool success = base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL); |
| 20 if (success) { |
| 21 LOG(INFO) << "Delegated to Chrome executable at " |
| 22 << cmd_line.GetProgram().value(); |
| 23 } else { |
| 24 LOG(INFO) << "Failed to launch Chrome executable at " |
| 25 << cmd_line.GetProgram().value(); |
| 26 } |
| 27 return success ? S_OK : E_FAIL; |
| 28 } |
| 29 |
| 30 HRESULT LaunchChromeAndWait(const CommandLine& cmd_line) { |
| 31 const DWORD kTimeOutInMilliseconds = 20000; |
| 32 HANDLE proc = 0; |
| 33 bool success = base::LaunchProcess(cmd_line, base::LaunchOptions(), &proc); |
| 34 if (!success) { |
| 35 LOG(INFO) << "Failed to launch Chrome executable at " |
| 36 << cmd_line.GetProgram().value(); |
| 37 return E_FAIL; |
| 38 } |
| 39 |
| 40 LOG(INFO) << "Delegated to Chrome executable at " |
| 41 << cmd_line.GetProgram().value(); |
| 42 DWORD res = WaitForSingleObject(proc, kTimeOutInMilliseconds); |
| 43 if (res != WAIT_OBJECT_0) { |
| 44 LOG(INFO) << " Failed to wailt."; |
| 45 return E_FAIL; |
| 46 } |
| 47 |
| 48 if (!GetExitCodeProcess(proc, &res)) { |
| 49 LOG(INFO) << " Failed to get exit code."; |
| 50 return E_FAIL; |
| 51 } |
| 52 // Since we know that the process has terminated, we need not worry about |
| 53 // the case where |res| should be interested as STILL_ACTIVE. |
| 54 |
| 55 LOG(INFO) << " Exit code: " << res; |
| 56 DCHECK(res != STILL_ACTIVE); |
| 57 return res; |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
16 int APIENTRY wWinMain(HINSTANCE, HINSTANCE, wchar_t*, int) { | 62 int APIENTRY wWinMain(HINSTANCE, HINSTANCE, wchar_t*, int) { |
17 base::AtExitManager exit_manager; | 63 base::AtExitManager exit_manager; |
18 | 64 |
| 65 // From chrome_switches.cc |
| 66 const char kQueryEULAAcceptance[] = "query-eula-acceptance"; |
| 67 |
19 // Initialize the commandline singleton from the environment. | 68 // Initialize the commandline singleton from the environment. |
20 CommandLine::Init(0, NULL); | 69 CommandLine::Init(0, NULL); |
21 | 70 |
22 FilePath chrome_exe(chrome_launcher_support::GetAnyChromePath()); | 71 FilePath chrome_exe(chrome_launcher_support::GetAnyChromePath()); |
23 if (chrome_exe.empty()) { | 72 if (chrome_exe.empty()) { |
24 LOG(INFO) << "No Chrome executable could be found. Let's install it."; | 73 LOG(INFO) << "No Chrome executable could be found. Let's install it."; |
25 HRESULT hr = app_host::InstallBinaries(); | 74 HRESULT hr = app_host::InstallBinaries(); |
26 if (FAILED(hr)) { | 75 if (FAILED(hr)) { |
27 LOG(ERROR) << "Failed to install the Chrome Binaries. Error: " << hr; | 76 LOG(ERROR) << "Failed to install the Chrome Binaries. Error: " << hr; |
28 return 1; | 77 return E_FAIL; |
29 } else { | 78 } else { |
30 chrome_exe = chrome_launcher_support::GetAnyChromePath(); | 79 chrome_exe = chrome_launcher_support::GetAnyChromePath(); |
31 if (chrome_exe.empty()) { | 80 if (chrome_exe.empty()) { |
32 LOG(ERROR) << "Failed to find the Chrome Binaries despite a " | 81 LOG(ERROR) << "Failed to find the Chrome Binaries despite a " |
33 << "'successful' installation."; | 82 << "'successful' installation."; |
34 return 1; | 83 return E_FAIL; |
35 } | 84 } |
36 } | 85 } |
37 } | 86 } |
38 | 87 |
| 88 CommandLine command_line(*CommandLine::ForCurrentProcess()); |
39 CommandLine chrome_exe_command_line(chrome_exe); | 89 CommandLine chrome_exe_command_line(chrome_exe); |
40 chrome_exe_command_line.AppendArguments( | 90 chrome_exe_command_line.AppendArguments(command_line, false); |
41 *CommandLine::ForCurrentProcess(), false); | |
42 // Launch Chrome before checking for update, for faster user experience. | |
43 bool launch_result = base::LaunchProcess(chrome_exe_command_line, | |
44 base::LaunchOptions(), NULL); | |
45 if (launch_result) | |
46 LOG(INFO) << "Delegated to Chrome executable at " << chrome_exe.value(); | |
47 else | |
48 LOG(INFO) << "Failed to launch Chrome executable at " << chrome_exe.value(); | |
49 | 91 |
50 app_host::EnsureAppHostUpToDate(); | 92 bool wait_for_chrome = false; |
| 93 if (command_line.HasSwitch(kQueryEULAAcceptance)) |
| 94 wait_for_chrome = true; |
51 | 95 |
52 return !launch_result; | 96 if (!wait_for_chrome) { |
| 97 // Launch Chrome before checking for update, for faster user experience. |
| 98 HRESULT ret = LaunchChromeAndProceed(chrome_exe_command_line); |
| 99 app_host::EnsureAppHostUpToDate(); |
| 100 return ret; |
| 101 |
| 102 } else { |
| 103 // We wait, so do not slow things more by trying to update app_host.exe. |
| 104 return LaunchChromeAndWait(chrome_exe_command_line); |
| 105 } |
53 } | 106 } |
OLD | NEW |