| 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> // NOLINT | 5 #include <windows.h> // NOLINT |
| 6 #include <shlwapi.h> // NOLINT | 6 #include <shlwapi.h> // NOLINT |
| 7 | 7 |
| 8 #include "chrome/app/main_dll_loader_win.h" | 8 #include "chrome/app/main_dll_loader_win.h" |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/environment.h" | 14 #include "base/environment.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
| 24 #include "base/win/metro.h" |
| 24 #include "base/win/scoped_handle.h" | 25 #include "base/win/scoped_handle.h" |
| 25 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
| 26 #include "chrome/app/chrome_crash_reporter_client.h" | 27 #include "chrome/app/chrome_crash_reporter_client.h" |
| 27 #include "chrome/app/chrome_watcher_client_win.h" | 28 #include "chrome/app/chrome_watcher_client_win.h" |
| 28 #include "chrome/app/chrome_watcher_command_line_win.h" | 29 #include "chrome/app/chrome_watcher_command_line_win.h" |
| 29 #include "chrome/app/file_pre_reader_win.h" | 30 #include "chrome/app/file_pre_reader_win.h" |
| 30 #include "chrome/app/kasko_client.h" | 31 #include "chrome/app/kasko_client.h" |
| 31 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" | 32 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" |
| 32 #include "chrome/common/chrome_constants.h" | 33 #include "chrome/common/chrome_constants.h" |
| 33 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
| 34 #include "chrome/common/chrome_result_codes.h" | 35 #include "chrome/common/chrome_result_codes.h" |
| 35 #include "chrome/common/chrome_switches.h" | 36 #include "chrome/common/chrome_switches.h" |
| 36 #include "chrome/common/env_vars.h" | 37 #include "chrome/common/env_vars.h" |
| 37 #include "chrome/installer/util/google_update_constants.h" | 38 #include "chrome/installer/util/google_update_constants.h" |
| 38 #include "chrome/installer/util/google_update_settings.h" | 39 #include "chrome/installer/util/google_update_settings.h" |
| 39 #include "chrome/installer/util/install_util.h" | 40 #include "chrome/installer/util/install_util.h" |
| 40 #include "chrome/installer/util/module_util_win.h" | 41 #include "chrome/installer/util/module_util_win.h" |
| 41 #include "chrome/installer/util/util_constants.h" | 42 #include "chrome/installer/util/util_constants.h" |
| 42 #include "components/crash/content/app/breakpad_win.h" | |
| 43 #include "components/crash/content/app/crash_reporter_client.h" | 43 #include "components/crash/content/app/crash_reporter_client.h" |
| 44 #include "components/crash/content/app/crashpad.h" |
| 44 #include "content/public/app/sandbox_helper_win.h" | 45 #include "content/public/app/sandbox_helper_win.h" |
| 45 #include "content/public/common/content_switches.h" | 46 #include "content/public/common/content_switches.h" |
| 46 #include "sandbox/win/src/sandbox.h" | 47 #include "sandbox/win/src/sandbox.h" |
| 47 | 48 |
| 48 namespace { | 49 namespace { |
| 49 // The entry point signature of chrome.dll. | 50 // The entry point signature of chrome.dll. |
| 50 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); | 51 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); |
| 51 | 52 |
| 52 typedef void (*RelaunchChromeBrowserWithNewCommandLineIfNeededFunc)(); | 53 typedef void (*RelaunchChromeBrowserWithNewCommandLineIfNeededFunc)(); |
| 53 | 54 |
| 54 base::LazyInstance<ChromeCrashReporterClient>::Leaky g_chrome_crash_client = | |
| 55 LAZY_INSTANCE_INITIALIZER; | |
| 56 | |
| 57 // Loads |module| after setting the CWD to |module|'s directory. Returns a | 55 // Loads |module| after setting the CWD to |module|'s directory. Returns a |
| 58 // reference to the loaded module on success, or null on error. | 56 // reference to the loaded module on success, or null on error. |
| 59 HMODULE LoadModuleWithDirectory(const base::FilePath& module, bool pre_read) { | 57 HMODULE LoadModuleWithDirectory(const base::FilePath& module, bool pre_read) { |
| 60 ::SetCurrentDirectoryW(module.DirName().value().c_str()); | 58 ::SetCurrentDirectoryW(module.DirName().value().c_str()); |
| 61 | 59 |
| 62 if (pre_read) { | 60 if (pre_read) { |
| 63 // We pre-read the binary to warm the memory caches (fewer hard faults to | 61 // We pre-read the binary to warm the memory caches (fewer hard faults to |
| 64 // page parts of the binary in). | 62 // page parts of the binary in). |
| 65 const size_t kStepSize = 1024 * 1024; | 63 const size_t kStepSize = 1024 * 1024; |
| 66 PreReadFile(module, kStepSize); | 64 PreReadFile(module, kStepSize); |
| 67 } | 65 } |
| 68 | 66 |
| 69 return ::LoadLibraryExW(module.value().c_str(), nullptr, | 67 return ::LoadLibraryExW(module.value().c_str(), nullptr, |
| 70 LOAD_WITH_ALTERED_SEARCH_PATH); | 68 LOAD_WITH_ALTERED_SEARCH_PATH); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void RecordDidRun(const base::FilePath& dll_path) { | 71 void RecordDidRun(const base::FilePath& dll_path) { |
| 74 bool system_level = !InstallUtil::IsPerUserInstall(dll_path); | 72 bool system_level = !InstallUtil::IsPerUserInstall(dll_path); |
| 75 GoogleUpdateSettings::UpdateDidRunState(true, system_level); | 73 GoogleUpdateSettings::UpdateDidRunState(true, system_level); |
| 76 } | 74 } |
| 77 | 75 |
| 78 void ClearDidRun(const base::FilePath& dll_path) { | 76 void ClearDidRun(const base::FilePath& dll_path) { |
| 79 bool system_level = !InstallUtil::IsPerUserInstall(dll_path); | 77 bool system_level = !InstallUtil::IsPerUserInstall(dll_path); |
| 80 GoogleUpdateSettings::UpdateDidRunState(false, system_level); | 78 GoogleUpdateSettings::UpdateDidRunState(false, system_level); |
| 81 } | 79 } |
| 82 | 80 |
| 83 bool InMetroMode() { | |
| 84 return (wcsstr( | |
| 85 ::GetCommandLineW(), L" -ServerName:DefaultBrowserServer") != nullptr); | |
| 86 } | |
| 87 | |
| 88 typedef int (*InitMetro)(); | 81 typedef int (*InitMetro)(); |
| 89 | 82 |
| 90 } // namespace | 83 } // namespace |
| 91 | 84 |
| 92 //============================================================================= | 85 //============================================================================= |
| 93 | 86 |
| 94 MainDllLoader::MainDllLoader() | 87 MainDllLoader::MainDllLoader() |
| 95 : dll_(nullptr), metro_mode_(InMetroMode()) { | 88 : dll_(nullptr), metro_mode_(base::win::IsMetroProcess()) { |
| 96 } | 89 } |
| 97 | 90 |
| 98 MainDllLoader::~MainDllLoader() { | 91 MainDllLoader::~MainDllLoader() { |
| 99 } | 92 } |
| 100 | 93 |
| 101 // Loading chrome is an interesting affair. First we try loading from the | 94 // Loading chrome is an interesting affair. First we try loading from the |
| 102 // current directory to support run-what-you-compile and other development | 95 // current directory to support run-what-you-compile and other development |
| 103 // scenarios. | 96 // scenarios. |
| 104 // If that fails then we look at the version resource in the current | 97 // If that fails then we look at the version resource in the current |
| 105 // module. This is the expected path for chrome.exe browser instances in an | 98 // module. This is the expected path for chrome.exe browser instances in an |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 parent_process.Take(), main_thread_id, | 190 parent_process.Take(), main_thread_id, |
| 198 on_initialized_event.Take(), | 191 on_initialized_event.Take(), |
| 199 watcher_data_directory.value().c_str(), | 192 watcher_data_directory.value().c_str(), |
| 200 message_window_name.c_str(), channel_name.c_str()); | 193 message_window_name.c_str(), channel_name.c_str()); |
| 201 } | 194 } |
| 202 | 195 |
| 203 // Initialize the sandbox services. | 196 // Initialize the sandbox services. |
| 204 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | 197 sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
| 205 content::InitializeSandboxInfo(&sandbox_info); | 198 content::InitializeSandboxInfo(&sandbox_info); |
| 206 | 199 |
| 207 crash_reporter::SetCrashReporterClient(g_chrome_crash_client.Pointer()); | |
| 208 bool exit_now = true; | |
| 209 if (process_type_.empty()) { | |
| 210 if (breakpad::ShowRestartDialogIfCrashed(&exit_now)) { | |
| 211 // We restarted because of a previous crash. Ask user if we should | |
| 212 // Relaunch. Only for the browser process. See crbug.com/132119. | |
| 213 if (exit_now) | |
| 214 return content::RESULT_CODE_NORMAL_EXIT; | |
| 215 } | |
| 216 } | |
| 217 breakpad::InitCrashReporter(process_type_); | |
| 218 | |
| 219 dll_ = Load(&version, &file); | 200 dll_ = Load(&version, &file); |
| 220 if (!dll_) | 201 if (!dll_) |
| 221 return chrome::RESULT_CODE_MISSING_DATA; | 202 return chrome::RESULT_CODE_MISSING_DATA; |
| 222 | 203 |
| 223 scoped_ptr<base::Environment> env(base::Environment::Create()); | 204 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 224 env->SetVar(chrome::kChromeVersionEnvVar, base::WideToUTF8(version)); | 205 env->SetVar(chrome::kChromeVersionEnvVar, base::WideToUTF8(version)); |
| 225 | 206 |
| 226 OnBeforeLaunch(process_type_, file); | 207 OnBeforeLaunch(process_type_, file); |
| 227 DLL_MAIN chrome_main = | 208 DLL_MAIN chrome_main = |
| 228 reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll_, "ChromeMain")); | 209 reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll_, "ChromeMain")); |
| 229 int rc = chrome_main(instance, &sandbox_info); | 210 int rc = chrome_main(instance, &sandbox_info); |
| 230 rc = OnBeforeExit(rc, file); | 211 rc = OnBeforeExit(rc, file); |
| 231 // Sandboxed processes close some system DLL handles after lockdown so ignore | |
| 232 // EXCEPTION_INVALID_HANDLE generated on Windows 10 during shutdown of these | |
| 233 // processes. | |
| 234 // TODO(wfh): Check whether MS have fixed this in Win10 RTM. crbug.com/456193 | |
| 235 if (base::win::GetVersion() >= base::win::VERSION_WIN10) | |
| 236 breakpad::ConsumeInvalidHandleExceptions(); | |
| 237 return rc; | 212 return rc; |
| 238 } | 213 } |
| 239 | 214 |
| 240 void MainDllLoader::RelaunchChromeBrowserWithNewCommandLineIfNeeded() { | 215 void MainDllLoader::RelaunchChromeBrowserWithNewCommandLineIfNeeded() { |
| 241 if (!dll_) | 216 if (!dll_) |
| 242 return; | 217 return; |
| 243 | 218 |
| 244 RelaunchChromeBrowserWithNewCommandLineIfNeededFunc relaunch_function = | 219 RelaunchChromeBrowserWithNewCommandLineIfNeededFunc relaunch_function = |
| 245 reinterpret_cast<RelaunchChromeBrowserWithNewCommandLineIfNeededFunc>( | 220 reinterpret_cast<RelaunchChromeBrowserWithNewCommandLineIfNeededFunc>( |
| 246 ::GetProcAddress(dll_, | 221 ::GetProcAddress(dll_, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 268 scoped_ptr<KaskoClient> kasko_client_; | 243 scoped_ptr<KaskoClient> kasko_client_; |
| 269 #endif // KASKO | 244 #endif // KASKO |
| 270 }; | 245 }; |
| 271 | 246 |
| 272 void ChromeDllLoader::OnBeforeLaunch(const std::string& process_type, | 247 void ChromeDllLoader::OnBeforeLaunch(const std::string& process_type, |
| 273 const base::FilePath& dll_path) { | 248 const base::FilePath& dll_path) { |
| 274 if (process_type.empty()) { | 249 if (process_type.empty()) { |
| 275 RecordDidRun(dll_path); | 250 RecordDidRun(dll_path); |
| 276 | 251 |
| 277 // Launch the watcher process if stats collection consent has been granted. | 252 // Launch the watcher process if stats collection consent has been granted. |
| 278 if (g_chrome_crash_client.Get().GetCollectStatsConsent()) { | 253 #if defined(GOOGLE_CHROME_BUILD) |
| 254 const bool stats_collection_consent = |
| 255 GoogleUpdateSettings::GetCollectStatsConsent(); |
| 256 #else |
| 257 const bool stats_collection_consent = false; |
| 258 #endif |
| 259 if (stats_collection_consent) { |
| 279 base::FilePath exe_path; | 260 base::FilePath exe_path; |
| 280 if (PathService::Get(base::FILE_EXE, &exe_path)) { | 261 if (PathService::Get(base::FILE_EXE, &exe_path)) { |
| 281 chrome_watcher_client_.reset(new ChromeWatcherClient( | 262 chrome_watcher_client_.reset(new ChromeWatcherClient( |
| 282 base::Bind(&GenerateChromeWatcherCommandLine, exe_path))); | 263 base::Bind(&GenerateChromeWatcherCommandLine, exe_path))); |
| 283 if (chrome_watcher_client_->LaunchWatcher()) { | 264 if (chrome_watcher_client_->LaunchWatcher()) { |
| 284 #if defined(KASKO) | 265 #if defined(KASKO) |
| 285 kasko::api::MinidumpType minidump_type = kasko::api::SMALL_DUMP_TYPE; | 266 kasko::api::MinidumpType minidump_type = kasko::api::SMALL_DUMP_TYPE; |
| 286 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 267 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 287 switches::kFullMemoryCrashReport)) { | 268 switches::kFullMemoryCrashReport)) { |
| 288 minidump_type = kasko::api::FULL_DUMP_TYPE; | 269 minidump_type = kasko::api::FULL_DUMP_TYPE; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 } | 321 } |
| 341 }; | 322 }; |
| 342 | 323 |
| 343 MainDllLoader* MakeMainDllLoader() { | 324 MainDllLoader* MakeMainDllLoader() { |
| 344 #if defined(GOOGLE_CHROME_BUILD) | 325 #if defined(GOOGLE_CHROME_BUILD) |
| 345 return new ChromeDllLoader(); | 326 return new ChromeDllLoader(); |
| 346 #else | 327 #else |
| 347 return new ChromiumDllLoader(); | 328 return new ChromiumDllLoader(); |
| 348 #endif | 329 #endif |
| 349 } | 330 } |
| OLD | NEW |