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