| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 #include <sddl.h> | 6 #include <sddl.h> |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
| 29 #include "base/synchronization/waitable_event.h" | 29 #include "base/synchronization/waitable_event.h" |
| 30 #include "base/threading/platform_thread.h" | 30 #include "base/threading/platform_thread.h" |
| 31 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
| 32 #include "base/threading/thread_task_runner_handle.h" | 32 #include "base/threading/thread_task_runner_handle.h" |
| 33 #include "base/time/time.h" | 33 #include "base/time/time.h" |
| 34 #include "base/win/scoped_handle.h" | 34 #include "base/win/scoped_handle.h" |
| 35 #include "base/win/win_util.h" | 35 #include "base/win/win_util.h" |
| 36 | 36 |
| 37 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" | 37 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" |
| 38 #include "chrome/chrome_watcher/kasko_util.h" | |
| 39 #include "chrome/installer/util/util_constants.h" | 38 #include "chrome/installer/util/util_constants.h" |
| 40 #include "components/browser_watcher/endsession_watcher_window_win.h" | 39 #include "components/browser_watcher/endsession_watcher_window_win.h" |
| 41 #include "components/browser_watcher/exit_code_watcher_win.h" | 40 #include "components/browser_watcher/exit_code_watcher_win.h" |
| 42 #include "components/browser_watcher/window_hang_monitor_win.h" | 41 #include "components/browser_watcher/window_hang_monitor_win.h" |
| 43 #include "third_party/kasko/kasko_features.h" | |
| 44 | 42 |
| 45 namespace { | 43 namespace { |
| 46 | 44 |
| 47 // Use the same log facility as Chrome for convenience. | 45 // Use the same log facility as Chrome for convenience. |
| 48 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} | 46 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
| 49 const GUID kChromeWatcherTraceProviderName = { | 47 const GUID kChromeWatcherTraceProviderName = { |
| 50 0x7fe69228, 0x633e, 0x4f06, | 48 0x7fe69228, 0x633e, 0x4f06, |
| 51 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; | 49 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; |
| 52 | 50 |
| 53 // The amount of time we wait around for a WM_ENDSESSION or a process exit. | 51 // The amount of time we wait around for a WM_ENDSESSION or a process exit. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 } else { | 172 } else { |
| 175 // The browser exited abnormally, wait around for a little bit to see | 173 // The browser exited abnormally, wait around for a little bit to see |
| 176 // whether this instance will get a logoff message. | 174 // whether this instance will get a logoff message. |
| 177 main_thread_->PostDelayedTask( | 175 main_thread_->PostDelayedTask( |
| 178 FROM_HERE, | 176 FROM_HERE, |
| 179 run_loop_->QuitClosure(), | 177 run_loop_->QuitClosure(), |
| 180 base::TimeDelta::FromSeconds(kDelayTimeSeconds)); | 178 base::TimeDelta::FromSeconds(kDelayTimeSeconds)); |
| 181 } | 179 } |
| 182 } | 180 } |
| 183 | 181 |
| 184 void OnWindowEvent( | |
| 185 base::Process process, | |
| 186 const base::Callback<void(const base::Process&)>& on_hung_callback, | |
| 187 browser_watcher::WindowHangMonitor::WindowEvent window_event) { | |
| 188 if (window_event == browser_watcher::WindowHangMonitor::WINDOW_HUNG && | |
| 189 !on_hung_callback.is_null()) { | |
| 190 on_hung_callback.Run(process); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 } // namespace | 182 } // namespace |
| 195 | 183 |
| 196 // The main entry point to the watcher, declared as extern "C" to avoid name | 184 // The main entry point to the watcher, declared as extern "C" to avoid name |
| 197 // mangling. | 185 // mangling. |
| 198 extern "C" int WatcherMain(const base::char16* registry_path, | 186 extern "C" int WatcherMain(const base::char16* registry_path, |
| 199 HANDLE process_handle, | 187 HANDLE process_handle, |
| 200 DWORD main_thread_id, | 188 DWORD main_thread_id, |
| 201 HANDLE on_initialized_event_handle, | 189 HANDLE on_initialized_event_handle, |
| 202 const base::char16* browser_data_directory, | 190 const base::char16* browser_data_directory, |
| 203 const base::char16* channel_name) { | 191 const base::char16* channel_name) { |
| 204 base::Process process(process_handle); | 192 base::Process process(process_handle); |
| 205 base::win::ScopedHandle on_initialized_event(on_initialized_event_handle); | 193 base::win::ScopedHandle on_initialized_event(on_initialized_event_handle); |
| 206 | 194 |
| 207 // The exit manager is in charge of calling the dtors of singletons. | 195 // The exit manager is in charge of calling the dtors of singletons. |
| 208 base::AtExitManager exit_manager; | 196 base::AtExitManager exit_manager; |
| 209 // Initialize the commandline singleton from the environment. | 197 // Initialize the commandline singleton from the environment. |
| 210 base::CommandLine::Init(0, nullptr); | 198 base::CommandLine::Init(0, nullptr); |
| 211 | 199 |
| 212 logging::LogEventProvider::Initialize(kChromeWatcherTraceProviderName); | 200 logging::LogEventProvider::Initialize(kChromeWatcherTraceProviderName); |
| 213 | 201 |
| 214 // Arrange to be shut down as late as possible, as we want to outlive | 202 // Arrange to be shut down as late as possible, as we want to outlive |
| 215 // chrome.exe in order to report its exit status. | 203 // chrome.exe in order to report its exit status. |
| 216 ::SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY); | 204 ::SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY); |
| 217 | 205 |
| 218 base::Callback<void(const base::Process&)> on_hung_callback; | |
| 219 | |
| 220 #if BUILDFLAG(ENABLE_KASKO) | |
| 221 bool launched_kasko = InitializeKaskoReporter(GetKaskoEndpoint(process.Pid()), | |
| 222 browser_data_directory); | |
| 223 | |
| 224 #if BUILDFLAG(ENABLE_KASKO_HANG_REPORTS) | |
| 225 // Only activate hang reports for the canary channel. | |
| 226 if (launched_kasko && | |
| 227 base::StringPiece16(channel_name) == installer::kChromeChannelCanary) { | |
| 228 on_hung_callback = base::Bind(&DumpHungProcess, main_thread_id, | |
| 229 channel_name, L"hung-process"); | |
| 230 } | |
| 231 #endif // BUILDFLAG(ENABLE_KASKO_HANG_REPORTS) | |
| 232 #endif // BUILDFLAG(ENABLE_KASKO) | |
| 233 | |
| 234 // Run a UI message loop on the main thread. | 206 // Run a UI message loop on the main thread. |
| 235 base::PlatformThread::SetName("WatcherMainThread"); | 207 base::PlatformThread::SetName("WatcherMainThread"); |
| 236 base::MessageLoop msg_loop(base::MessageLoop::TYPE_UI); | 208 base::MessageLoop msg_loop(base::MessageLoop::TYPE_UI); |
| 237 | 209 |
| 238 base::RunLoop run_loop; | 210 base::RunLoop run_loop; |
| 239 BrowserMonitor monitor(registry_path, &run_loop); | 211 BrowserMonitor monitor(registry_path, &run_loop); |
| 240 if (!monitor.StartWatching(process.Duplicate(), | 212 if (!monitor.StartWatching(process.Duplicate(), |
| 241 std::move(on_initialized_event))) { | 213 std::move(on_initialized_event))) { |
| 242 return 1; | 214 return 1; |
| 243 } | 215 } |
| 244 | 216 run_loop.Run(); |
| 245 { | 217 // TODO(manzagop): hang monitoring using WindowHangMonitor. |
| 246 // Scoped to force |hang_monitor| destruction before Kasko is shut down. | |
| 247 browser_watcher::WindowHangMonitor hang_monitor( | |
| 248 base::TimeDelta::FromSeconds(60), base::TimeDelta::FromSeconds(20), | |
| 249 base::Bind(&OnWindowEvent, base::Passed(process.Duplicate()), | |
| 250 on_hung_callback)); | |
| 251 hang_monitor.Initialize(process.Duplicate()); | |
| 252 | |
| 253 run_loop.Run(); | |
| 254 } | |
| 255 | |
| 256 #if BUILDFLAG(ENABLE_KASKO) | |
| 257 if (launched_kasko) | |
| 258 ShutdownKaskoReporter(); | |
| 259 #endif // BUILDFLAG(ENABLE_KASKO) | |
| 260 | 218 |
| 261 // Wind logging down. | 219 // Wind logging down. |
| 262 logging::LogEventProvider::Uninitialize(); | 220 logging::LogEventProvider::Uninitialize(); |
| 263 | 221 |
| 264 return 0; | 222 return 0; |
| 265 } | 223 } |
| 266 | 224 |
| 267 static_assert( | 225 static_assert( |
| 268 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, | 226 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, |
| 269 "WatcherMain() has wrong type"); | 227 "WatcherMain() has wrong type"); |
| OLD | NEW |