Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/process_singleton.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/scoped_temp_dir.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/process/launch.h" | |
| 16 #include "base/process/process.h" | |
| 17 #include "base/process/process_handle.h" | |
| 18 #include "base/strings/string16.h" | |
| 19 #include "base/strings/stringprintf.h" | |
| 20 #include "base/test/multiprocess_test.h" | |
| 21 #include "base/win/scoped_handle.h" | |
| 22 #include "base/win/wrapped_window_proc.h" | |
| 23 #include "chrome/browser/chrome_process_finder_win.h" | |
| 24 #include "chrome/common/chrome_constants.h" | |
| 25 #include "chrome/common/chrome_switches.h" | |
| 26 #include "content/public/common/result_codes.h" | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | |
| 28 #include "testing/multiprocess_func_list.h" | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char kReadyEventNameFlag[] = "ready_event_name"; | |
| 33 const char kContinueEventNameFlag[] = "continue_event_name"; | |
| 34 const char kCreateWindowFlag[] = "create_window"; | |
| 35 const int kErrorResultCode = 0x345; | |
| 36 | |
| 37 bool NotificationCallback(const base::CommandLine& command_line, | |
| 38 const base::FilePath& current_directory) { | |
| 39 // This is never called in this test, but would signal that the singleton | |
| 40 // notification was successfully handled. | |
|
gab
2015/03/11 15:19:43
Add a NOTREACHED() to confirm that this is never c
Sigurður Ásgeirsson
2015/03/11 16:36:11
Done.
| |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 HWND CreateVisibleWindow() { | |
| 45 WNDCLASSEX wnd_cls = {}; | |
| 46 base::win::InitializeWindowClass( | |
| 47 L"ProcessSingletonTest", base::win::WrappedWindowProc<::DefWindowProc>, | |
| 48 0, // style | |
| 49 0, // class_extra | |
| 50 0, // window_extra | |
| 51 NULL, // cursor | |
| 52 NULL, // background | |
| 53 L"", // menu_name | |
| 54 NULL, // large_icon | |
| 55 NULL, // small_icon | |
| 56 &wnd_cls); | |
| 57 | |
| 58 ATOM clazz = ::RegisterClassEx(&wnd_cls); | |
| 59 | |
| 60 // Create a visible popup window to trigger user interaction in the | |
| 61 // process singleton. | |
| 62 HWND window = ::CreateWindow(reinterpret_cast<LPCWSTR>(clazz), 0, WS_POPUP, 0, | |
| 63 0, 0, 0, 0, 0, NULL, 0); | |
| 64 if (window) | |
| 65 ::ShowWindow(window, SW_SHOW); | |
| 66 | |
| 67 return window; | |
| 68 } | |
| 69 | |
| 70 MULTIPROCESS_TEST_MAIN(ProcessSingletonTestProcessMain) { | |
| 71 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 72 base::FilePath user_data_dir = | |
| 73 cmd_line->GetSwitchValuePath(switches::kUserDataDir); | |
| 74 if (user_data_dir.empty()) | |
| 75 return kErrorResultCode; | |
| 76 | |
| 77 base::string16 ready_event_name = | |
| 78 cmd_line->GetSwitchValueNative(kReadyEventNameFlag); | |
| 79 | |
| 80 base::win::ScopedHandle ready_event( | |
| 81 ::OpenEvent(EVENT_MODIFY_STATE, FALSE, ready_event_name.c_str())); | |
| 82 if (!ready_event.IsValid()) | |
| 83 return kErrorResultCode; | |
| 84 | |
| 85 base::string16 continue_event_name = | |
| 86 cmd_line->GetSwitchValueNative(kContinueEventNameFlag); | |
| 87 | |
| 88 base::win::ScopedHandle continue_event( | |
| 89 ::OpenEvent(SYNCHRONIZE, FALSE, continue_event_name.c_str())); | |
| 90 if (!continue_event.IsValid()) | |
| 91 return kErrorResultCode; | |
| 92 | |
| 93 if (cmd_line->HasSwitch(kCreateWindowFlag)) { | |
| 94 HWND window = CreateVisibleWindow(); | |
| 95 if (!window) | |
| 96 return kErrorResultCode; | |
| 97 } | |
| 98 | |
| 99 // Instantiate the process singleton. | |
| 100 ProcessSingleton process_singleton(user_data_dir, | |
| 101 base::Bind(&NotificationCallback)); | |
| 102 | |
| 103 if (!process_singleton.Create()) | |
| 104 return kErrorResultCode; | |
| 105 | |
| 106 // Signal ready and block for the continue event. | |
| 107 if (!::SetEvent(ready_event.Get())) | |
| 108 return kErrorResultCode; | |
| 109 | |
| 110 if (::WaitForSingleObject(continue_event.Get(), INFINITE) != WAIT_OBJECT_0) | |
| 111 return kErrorResultCode; | |
| 112 | |
| 113 return 0; | |
| 114 } | |
| 115 | |
| 116 class ProcessSingletonTest : public base::MultiProcessTest { | |
| 117 protected: | |
| 118 enum WindowOption { WITH_WINDOW, NO_WINDOW }; | |
| 119 | |
| 120 ProcessSingletonTest() : window_option_(NO_WINDOW), msg_box_called_(false) {} | |
| 121 | |
| 122 void SetUp() override { | |
| 123 ASSERT_NO_FATAL_FAILURE(base::MultiProcessTest::SetUp()); | |
| 124 | |
| 125 // Drop the process finder notification timeout to one second for testing. | |
| 126 old_notification_timeout_ = | |
| 127 chrome::SetNotificationTimeoutInSecondsForTesting(1); | |
| 128 } | |
| 129 | |
| 130 void TearDown() override { | |
| 131 chrome::SetNotificationTimeoutInSecondsForTesting( | |
| 132 old_notification_timeout_); | |
| 133 | |
| 134 if (browser_victim_.IsValid()) { | |
| 135 EXPECT_TRUE(::SetEvent(continue_event_.Get())); | |
| 136 int exit_code = 0; | |
| 137 EXPECT_TRUE(browser_victim_.WaitForExit(&exit_code)); | |
| 138 } | |
| 139 | |
| 140 base::MultiProcessTest::TearDown(); | |
| 141 } | |
| 142 | |
| 143 void LaunchHungBrowserProcess(WindowOption window_option) { | |
| 144 // Create a unique user data dir to rendezvous on. | |
| 145 ASSERT_TRUE(user_data_dir_.CreateUniqueTempDir()); | |
| 146 | |
| 147 // Create the named "ready" event, this is unique to our process. | |
| 148 ready_event_name_ = | |
| 149 base::StringPrintf(L"ready-event-%d", base::GetCurrentProcId()); | |
| 150 base::win::ScopedHandle ready_event( | |
| 151 ::CreateEvent(NULL, TRUE, FALSE, ready_event_name_.c_str())); | |
| 152 ASSERT_TRUE(ready_event.IsValid()); | |
| 153 | |
| 154 // Create the named "continue" event, this is unique to our process. | |
| 155 continue_event_name_ = | |
| 156 base::StringPrintf(L"continue-event-%d", base::GetCurrentProcId()); | |
| 157 continue_event_.Set( | |
| 158 ::CreateEvent(NULL, TRUE, FALSE, continue_event_name_.c_str())); | |
| 159 ASSERT_TRUE(continue_event_.IsValid()); | |
| 160 | |
| 161 window_option_ = window_option; | |
| 162 | |
| 163 base::LaunchOptions options; | |
| 164 options.start_hidden = true; | |
| 165 browser_victim_ = | |
| 166 SpawnChildWithOptions("ProcessSingletonTestProcessMain", options); | |
| 167 | |
| 168 // Wait for the ready event (or process exit). | |
| 169 HANDLE handles[] = {ready_event.Get(), browser_victim_.Handle()}; | |
| 170 DWORD result = | |
| 171 ::WaitForMultipleObjects(arraysize(handles), handles, FALSE, INFINITE); | |
| 172 ASSERT_EQ(result, WAIT_OBJECT_0); | |
| 173 } | |
| 174 | |
| 175 base::CommandLine MakeCmdLine(const std::string& procname) override { | |
| 176 base::CommandLine cmd_line = base::MultiProcessTest::MakeCmdLine(procname); | |
| 177 | |
| 178 cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_.path()); | |
| 179 cmd_line.AppendSwitchNative(kReadyEventNameFlag, ready_event_name_); | |
| 180 cmd_line.AppendSwitchNative(kContinueEventNameFlag, continue_event_name_); | |
| 181 if (window_option_ == WITH_WINDOW) | |
| 182 cmd_line.AppendSwitch(kCreateWindowFlag); | |
| 183 | |
| 184 return cmd_line; | |
| 185 } | |
| 186 | |
| 187 void PrepareTest(WindowOption window_option, bool allow_kill) { | |
| 188 ASSERT_NO_FATAL_FAILURE(LaunchHungBrowserProcess(window_option)); | |
| 189 | |
| 190 // The ready event has been signalled - the process singleton is held by | |
| 191 // the hung sub process. | |
| 192 test_singleton_.reset(new ProcessSingleton( | |
| 193 user_data_dir(), base::Bind(&NotificationCallback))); | |
| 194 | |
| 195 test_singleton_->OverrideKillMessageBoxCallbackForTesting( | |
| 196 base::Bind(&ProcessSingletonTest::MockDisplayKillMessageBox, | |
| 197 base::Unretained(this), allow_kill)); | |
| 198 } | |
| 199 | |
| 200 base::Process* browser_victim() { return &browser_victim_; } | |
| 201 const base::FilePath& user_data_dir() const { return user_data_dir_.path(); } | |
| 202 ProcessSingleton* test_singleton() const { return test_singleton_.get(); } | |
| 203 bool msg_box_called() const { return msg_box_called_; } | |
| 204 | |
| 205 private: | |
| 206 bool MockDisplayKillMessageBox(bool allow_kill) { | |
|
gab
2015/03/11 15:19:43
Update method name to match new callback name.
Sigurður Ásgeirsson
2015/03/11 16:36:11
Done.
| |
| 207 msg_box_called_ = true; | |
| 208 return allow_kill; | |
| 209 } | |
| 210 | |
| 211 base::string16 ready_event_name_; | |
| 212 base::string16 continue_event_name_; | |
| 213 | |
| 214 WindowOption window_option_; | |
| 215 base::ScopedTempDir user_data_dir_; | |
| 216 base::Process browser_victim_; | |
| 217 base::win::ScopedHandle continue_event_; | |
| 218 | |
| 219 scoped_ptr<ProcessSingleton> test_singleton_; | |
| 220 | |
| 221 int old_notification_timeout_; | |
| 222 int msg_box_called_; | |
| 223 | |
| 224 DISALLOW_COPY_AND_ASSIGN(ProcessSingletonTest); | |
| 225 }; | |
| 226 | |
| 227 } // namespace | |
| 228 | |
| 229 TEST_F(ProcessSingletonTest, KillsHungBrowserWithNoWindows) { | |
| 230 ASSERT_NO_FATAL_FAILURE(PrepareTest(NO_WINDOW, false)); | |
| 231 | |
| 232 // As the hung browser has no visible window, it'll be killed without | |
| 233 // user interaction. | |
| 234 ProcessSingleton::NotifyResult notify_result = | |
| 235 test_singleton()->NotifyOtherProcessOrCreate(); | |
| 236 ASSERT_EQ(notify_result, ProcessSingleton::PROFILE_IN_USE); | |
| 237 | |
| 238 // The message box should not have been invoked, as the "browser" had no | |
| 239 // windows. | |
| 240 EXPECT_FALSE(msg_box_called()); | |
|
gab
2015/03/11 15:19:43
Update msg_box_called() naming (and above comment)
Sigurður Ásgeirsson
2015/03/11 16:36:11
Done.
| |
| 241 | |
| 242 // Verify that the hung browser has beem terminated with the | |
| 243 // RESULT_CODE_HUNG exit code. | |
| 244 int exit_code = 0; | |
| 245 EXPECT_TRUE( | |
| 246 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); | |
| 247 EXPECT_EQ(exit_code, content::RESULT_CODE_HUNG); | |
| 248 } | |
| 249 | |
| 250 TEST_F(ProcessSingletonTest, DoesntKillWithoutUserPermission) { | |
| 251 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, false)); | |
| 252 | |
| 253 // As the hung browser has a visible window, this should query the user | |
| 254 // before killing the hung process. | |
| 255 ProcessSingleton::NotifyResult notify_result = | |
| 256 test_singleton()->NotifyOtherProcessOrCreate(); | |
| 257 ASSERT_EQ(notify_result, ProcessSingleton::PROCESS_NOTIFIED); | |
| 258 | |
| 259 // The message box should have been invoked, as the "browser" has a | |
| 260 // visible window. | |
| 261 EXPECT_TRUE(msg_box_called()); | |
| 262 | |
| 263 // Make sure the process hasn't been killed. | |
| 264 int exit_code = 0; | |
| 265 EXPECT_FALSE( | |
| 266 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); | |
| 267 } | |
| 268 | |
| 269 TEST_F(ProcessSingletonTest, KillWithUserPermission) { | |
| 270 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, true)); | |
| 271 | |
| 272 // As the hung browser has a visible window, this should query the user | |
| 273 // before killing the hung process. | |
| 274 ProcessSingleton::NotifyResult notify_result = | |
| 275 test_singleton()->NotifyOtherProcessOrCreate(); | |
| 276 ASSERT_EQ(notify_result, ProcessSingleton::PROFILE_IN_USE); | |
| 277 | |
| 278 // The message box should have been invoked, as the "browser" has a | |
| 279 // visible window. | |
| 280 EXPECT_TRUE(msg_box_called()); | |
| 281 | |
| 282 // Verify that the hung browser has beem terminated with the | |
| 283 // RESULT_CODE_HUNG exit code. | |
| 284 int exit_code = 0; | |
| 285 EXPECT_TRUE( | |
| 286 browser_victim()->WaitForExitWithTimeout(base::TimeDelta(), &exit_code)); | |
| 287 EXPECT_EQ(exit_code, content::RESULT_CODE_HUNG); | |
| 288 } | |
| OLD | NEW |