| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.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/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/process/launch.h" | 18 #include "base/process/launch.h" |
| 19 #include "base/process/process.h" | 19 #include "base/process/process.h" |
| 20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
| 21 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
| 22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 23 #include "base/test/histogram_tester.h" |
| 23 #include "base/test/multiprocess_test.h" | 24 #include "base/test/multiprocess_test.h" |
| 24 #include "base/win/scoped_handle.h" | 25 #include "base/win/scoped_handle.h" |
| 25 #include "base/win/wrapped_window_proc.h" | 26 #include "base/win/wrapped_window_proc.h" |
| 26 #include "chrome/browser/win/chrome_process_finder.h" | 27 #include "chrome/browser/win/chrome_process_finder.h" |
| 27 #include "chrome/common/chrome_constants.h" | 28 #include "chrome/common/chrome_constants.h" |
| 28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
| 29 #include "content/public/common/result_codes.h" | 30 #include "content/public/common/result_codes.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "testing/multiprocess_func_list.h" | 32 #include "testing/multiprocess_func_list.h" |
| 32 | 33 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 base::Unretained(this), allow_kill)); | 229 base::Unretained(this), allow_kill)); |
| 229 } | 230 } |
| 230 | 231 |
| 231 base::Process* browser_victim() { return &(browser_victim_.process); } | 232 base::Process* browser_victim() { return &(browser_victim_.process); } |
| 232 const base::FilePath& user_data_dir() const { | 233 const base::FilePath& user_data_dir() const { |
| 233 return user_data_dir_.GetPath(); | 234 return user_data_dir_.GetPath(); |
| 234 } | 235 } |
| 235 ProcessSingleton* test_singleton() const { return test_singleton_.get(); } | 236 ProcessSingleton* test_singleton() const { return test_singleton_.get(); } |
| 236 bool should_kill_called() const { return should_kill_called_; } | 237 bool should_kill_called() const { return should_kill_called_; } |
| 237 | 238 |
| 239 const base::HistogramTester& histogram_tester() const { |
| 240 return histogram_tester_; |
| 241 } |
| 242 |
| 238 private: | 243 private: |
| 239 bool MockShouldKillRemoteProcess(bool allow_kill) { | 244 bool MockShouldKillRemoteProcess(bool allow_kill) { |
| 240 should_kill_called_ = true; | 245 should_kill_called_ = true; |
| 241 return allow_kill; | 246 return allow_kill; |
| 242 } | 247 } |
| 243 | 248 |
| 244 base::string16 ready_event_name_; | 249 base::string16 ready_event_name_; |
| 245 base::string16 continue_event_name_; | 250 base::string16 continue_event_name_; |
| 246 | 251 |
| 247 WindowOption window_option_; | 252 WindowOption window_option_; |
| 248 base::ScopedTempDir user_data_dir_; | 253 base::ScopedTempDir user_data_dir_; |
| 249 base::SpawnChildResult browser_victim_; | 254 base::SpawnChildResult browser_victim_; |
| 250 base::win::ScopedHandle continue_event_; | 255 base::win::ScopedHandle continue_event_; |
| 251 | 256 |
| 252 std::unique_ptr<ProcessSingleton> test_singleton_; | 257 std::unique_ptr<ProcessSingleton> test_singleton_; |
| 253 | 258 |
| 254 base::TimeDelta old_notification_timeout_; | 259 base::TimeDelta old_notification_timeout_; |
| 255 bool should_kill_called_; | 260 bool should_kill_called_; |
| 261 base::HistogramTester histogram_tester_; |
| 256 | 262 |
| 257 DISALLOW_COPY_AND_ASSIGN(ProcessSingletonTest); | 263 DISALLOW_COPY_AND_ASSIGN(ProcessSingletonTest); |
| 258 }; | 264 }; |
| 259 | 265 |
| 260 } // namespace | 266 } // namespace |
| 261 | 267 |
| 262 TEST_F(ProcessSingletonTest, KillsHungBrowserWithNoWindows) { | 268 TEST_F(ProcessSingletonTest, KillsHungBrowserWithNoWindows) { |
| 263 ASSERT_NO_FATAL_FAILURE(PrepareTest(NO_WINDOW, false)); | 269 ASSERT_NO_FATAL_FAILURE(PrepareTest(NO_WINDOW, false)); |
| 264 | 270 |
| 265 // As the hung browser has no visible window, it'll be killed without | 271 // As the hung browser has no visible window, it'll be killed without |
| 266 // user interaction. | 272 // user interaction. |
| 267 ProcessSingleton::NotifyResult notify_result = | 273 ProcessSingleton::NotifyResult notify_result = |
| 268 test_singleton()->NotifyOtherProcessOrCreate(); | 274 test_singleton()->NotifyOtherProcessOrCreate(); |
| 269 | 275 |
| 270 // The hung process was killed and the notification is equivalent to | 276 // The hung process was killed and the notification is equivalent to |
| 271 // a non existent process. | 277 // a non existent process. |
| 272 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, notify_result); | 278 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, notify_result); |
| 273 | 279 |
| 274 // The should-kill callback should not have been called, as the "browser" does | 280 // The should-kill callback should not have been called, as the "browser" does |
| 275 // not have visible window. | 281 // not have visible window. |
| 276 EXPECT_FALSE(should_kill_called()); | 282 EXPECT_FALSE(should_kill_called()); |
| 277 | 283 |
| 284 histogram_tester().ExpectUniqueSample( |
| 285 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", |
| 286 ProcessSingleton::TERMINATE_SUCCEEDED, 1u); |
| 287 histogram_tester().ExpectTotalCount( |
| 288 "Chrome.ProcessSingleton.TerminateProcessTime", 1u); |
| 289 histogram_tester().ExpectUniqueSample( |
| 290 "Chrome.ProcessSingleton.ProcessTerminateErrorCode.Windows", 0, 1u); |
| 291 histogram_tester().ExpectUniqueSample( |
| 292 "Chrome.ProcessSingleton.TerminationWaitErrorCode.Windows", 0, 1u); |
| 293 histogram_tester().ExpectUniqueSample( |
| 294 "Chrome.ProcessSingleton.RemoteHungProcessTerminateReason", |
| 295 ProcessSingleton::NO_VISIBLE_WINDOW_FOUND, 1u); |
| 296 |
| 278 // Verify that the hung browser has been terminated with the | 297 // Verify that the hung browser has been terminated with the |
| 279 // RESULT_CODE_HUNG exit code. | 298 // RESULT_CODE_HUNG exit code. |
| 280 int exit_code = 0; | 299 int exit_code = 0; |
| 281 EXPECT_TRUE( | 300 EXPECT_TRUE( |
| 282 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); | 301 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); |
| 283 EXPECT_EQ(content::RESULT_CODE_HUNG, exit_code); | 302 EXPECT_EQ(content::RESULT_CODE_HUNG, exit_code); |
| 284 } | 303 } |
| 285 | 304 |
| 286 TEST_F(ProcessSingletonTest, DoesntKillWithoutUserPermission) { | 305 TEST_F(ProcessSingletonTest, DoesntKillWithoutUserPermission) { |
| 287 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, false)); | 306 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, false)); |
| 288 | 307 |
| 289 // As the hung browser has a visible window, this should query the user | 308 // As the hung browser has a visible window, this should query the user |
| 290 // before killing the hung process. | 309 // before killing the hung process. |
| 291 ProcessSingleton::NotifyResult notify_result = | 310 ProcessSingleton::NotifyResult notify_result = |
| 292 test_singleton()->NotifyOtherProcessOrCreate(); | 311 test_singleton()->NotifyOtherProcessOrCreate(); |
| 293 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, notify_result); | 312 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, notify_result); |
| 294 | 313 |
| 295 // The should-kill callback should have been called, as the "browser" has a | 314 // The should-kill callback should have been called, as the "browser" has a |
| 296 // visible window. | 315 // visible window. |
| 297 EXPECT_TRUE(should_kill_called()); | 316 EXPECT_TRUE(should_kill_called()); |
| 298 | 317 |
| 318 histogram_tester().ExpectTotalCount( |
| 319 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", 0); |
| 320 |
| 299 // Make sure the process hasn't been killed. | 321 // Make sure the process hasn't been killed. |
| 300 int exit_code = 0; | 322 int exit_code = 0; |
| 301 EXPECT_FALSE( | 323 EXPECT_FALSE( |
| 302 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); | 324 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); |
| 303 } | 325 } |
| 304 | 326 |
| 305 TEST_F(ProcessSingletonTest, KillWithUserPermission) { | 327 TEST_F(ProcessSingletonTest, KillWithUserPermission) { |
| 306 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, true)); | 328 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, true)); |
| 307 | 329 |
| 308 // As the hung browser has a visible window, this should query the user | 330 // As the hung browser has a visible window, this should query the user |
| 309 // before killing the hung process. | 331 // before killing the hung process. |
| 310 ProcessSingleton::NotifyResult notify_result = | 332 ProcessSingleton::NotifyResult notify_result = |
| 311 test_singleton()->NotifyOtherProcessOrCreate(); | 333 test_singleton()->NotifyOtherProcessOrCreate(); |
| 312 | 334 |
| 313 // The hung process was killed and the notification is equivalent to | 335 // The hung process was killed and the notification is equivalent to |
| 314 // a non existent process. | 336 // a non existent process. |
| 315 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, notify_result); | 337 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, notify_result); |
| 316 | 338 |
| 317 // The should-kill callback should have been called, as the "browser" has a | 339 // The should-kill callback should have been called, as the "browser" has a |
| 318 // visible window. | 340 // visible window. |
| 319 EXPECT_TRUE(should_kill_called()); | 341 EXPECT_TRUE(should_kill_called()); |
| 320 | 342 |
| 343 histogram_tester().ExpectUniqueSample( |
| 344 "Chrome.ProcessSingleton.RemoteProcessInteractionResult", |
| 345 ProcessSingleton::TERMINATE_SUCCEEDED, 1u); |
| 346 histogram_tester().ExpectTotalCount( |
| 347 "Chrome.ProcessSingleton.TerminateProcessTime", 1u); |
| 348 histogram_tester().ExpectUniqueSample( |
| 349 "Chrome.ProcessSingleton.ProcessTerminateErrorCode.Windows", 0, 1u); |
| 350 histogram_tester().ExpectUniqueSample( |
| 351 "Chrome.ProcessSingleton.TerminationWaitErrorCode.Windows", 0, 1u); |
| 352 histogram_tester().ExpectUniqueSample( |
| 353 "Chrome.ProcessSingleton.RemoteHungProcessTerminateReason", |
| 354 ProcessSingleton::USER_ACCEPTED_TERMINATION, 1u); |
| 355 |
| 321 // Verify that the hung browser has been terminated with the | 356 // Verify that the hung browser has been terminated with the |
| 322 // RESULT_CODE_HUNG exit code. | 357 // RESULT_CODE_HUNG exit code. |
| 323 int exit_code = 0; | 358 int exit_code = 0; |
| 324 EXPECT_TRUE( | 359 EXPECT_TRUE( |
| 325 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); | 360 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); |
| 326 EXPECT_EQ(content::RESULT_CODE_HUNG, exit_code); | 361 EXPECT_EQ(content::RESULT_CODE_HUNG, exit_code); |
| 327 } | 362 } |
| OLD | NEW |