| 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 "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
| 6 | 6 |
| 7 #include <signal.h> | 7 #include <signal.h> |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 #include <sys/wait.h> | 9 #include <sys/wait.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 bool NotificationCallback(const CommandLine& command_line, | 35 bool NotificationCallback(const CommandLine& command_line, |
| 36 const FilePath& current_directory) { | 36 const FilePath& current_directory) { |
| 37 return true; | 37 return true; |
| 38 } | 38 } |
| 39 | 39 |
| 40 class ProcessSingletonLinuxTest : public testing::Test { | 40 class ProcessSingletonLinuxTest : public testing::Test { |
| 41 public: | 41 public: |
| 42 // A ProcessSingleton exposing some protected methods for testing. |
| 43 class TestableProcessSingleton : public ProcessSingleton { |
| 44 public: |
| 45 explicit TestableProcessSingleton(const FilePath& user_data_dir) |
| 46 : ProcessSingleton(user_data_dir) {} |
| 47 using ProcessSingleton::NotifyOtherProcessWithTimeout; |
| 48 using ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate; |
| 49 using ProcessSingleton::OverrideCurrentPidForTesting; |
| 50 using ProcessSingleton::OverrideKillCallbackForTesting; |
| 51 }; |
| 52 |
| 42 ProcessSingletonLinuxTest() | 53 ProcessSingletonLinuxTest() |
| 43 : kill_callbacks_(0), | 54 : kill_callbacks_(0), |
| 44 io_thread_(BrowserThread::IO), | 55 io_thread_(BrowserThread::IO), |
| 45 wait_event_(true, false), | 56 wait_event_(true, false), |
| 46 signal_event_(true, false), | 57 signal_event_(true, false), |
| 47 process_singleton_on_thread_(NULL) { | 58 process_singleton_on_thread_(NULL) { |
| 48 io_thread_.StartIOThread(); | 59 io_thread_.StartIOThread(); |
| 49 } | 60 } |
| 50 | 61 |
| 51 virtual void SetUp() { | 62 virtual void SetUp() { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 base::Bind(&ProcessSingletonLinuxTest:: | 103 base::Bind(&ProcessSingletonLinuxTest:: |
| 93 CreateProcessSingletonInternal, | 104 CreateProcessSingletonInternal, |
| 94 base::Unretained(this))); | 105 base::Unretained(this))); |
| 95 | 106 |
| 96 scoped_refptr<base::ThreadTestHelper> helper( | 107 scoped_refptr<base::ThreadTestHelper> helper( |
| 97 new base::ThreadTestHelper( | 108 new base::ThreadTestHelper( |
| 98 worker_thread_->message_loop_proxy())); | 109 worker_thread_->message_loop_proxy())); |
| 99 ASSERT_TRUE(helper->Run()); | 110 ASSERT_TRUE(helper->Run()); |
| 100 } | 111 } |
| 101 | 112 |
| 102 ProcessSingleton* CreateProcessSingleton() { | 113 TestableProcessSingleton* CreateProcessSingleton() { |
| 103 return new ProcessSingleton(temp_dir_.path()); | 114 return new TestableProcessSingleton(temp_dir_.path()); |
| 104 } | 115 } |
| 105 | 116 |
| 106 ProcessSingleton::NotifyResult NotifyOtherProcess( | 117 ProcessSingleton::NotifyResult NotifyOtherProcess( |
| 107 bool override_kill, | 118 bool override_kill, |
| 108 base::TimeDelta timeout) { | 119 base::TimeDelta timeout) { |
| 109 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 120 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 121 CreateProcessSingleton()); |
| 110 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); | 122 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); |
| 111 command_line.AppendArg("about:blank"); | 123 command_line.AppendArg("about:blank"); |
| 112 if (override_kill) { | 124 if (override_kill) { |
| 113 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() +
1); | 125 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() +
1); |
| 114 process_singleton->OverrideKillCallbackForTesting( | 126 process_singleton->OverrideKillCallbackForTesting( |
| 115 base::Bind(&ProcessSingletonLinuxTest::KillCallback, | 127 base::Bind(&ProcessSingletonLinuxTest::KillCallback, |
| 116 base::Unretained(this))); | 128 base::Unretained(this))); |
| 117 } | 129 } |
| 118 | 130 |
| 119 return process_singleton->NotifyOtherProcessWithTimeout( | 131 return process_singleton->NotifyOtherProcessWithTimeout( |
| 120 command_line, timeout.InSeconds(), true); | 132 command_line, timeout.InSeconds(), true); |
| 121 } | 133 } |
| 122 | 134 |
| 123 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). | 135 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). |
| 124 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( | 136 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( |
| 125 const std::string& url, | 137 const std::string& url, |
| 126 base::TimeDelta timeout) { | 138 base::TimeDelta timeout) { |
| 127 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 139 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 140 CreateProcessSingleton()); |
| 128 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); | 141 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); |
| 129 command_line.AppendArg(url); | 142 command_line.AppendArg(url); |
| 130 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( | 143 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( |
| 131 command_line, base::Bind(&NotificationCallback), timeout.InSeconds()); | 144 command_line, base::Bind(&NotificationCallback), timeout.InSeconds()); |
| 132 } | 145 } |
| 133 | 146 |
| 134 void CheckNotified() { | 147 void CheckNotified() { |
| 135 ASSERT_EQ(1u, callback_command_lines_.size()); | 148 ASSERT_EQ(1u, callback_command_lines_.size()); |
| 136 bool found = false; | 149 bool found = false; |
| 137 for (size_t i = 0; i < callback_command_lines_[0].size(); ++i) { | 150 for (size_t i = 0; i < callback_command_lines_[0].size(); ++i) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 void KillCallback(int pid) { | 203 void KillCallback(int pid) { |
| 191 kill_callbacks_++; | 204 kill_callbacks_++; |
| 192 } | 205 } |
| 193 | 206 |
| 194 content::TestBrowserThread io_thread_; | 207 content::TestBrowserThread io_thread_; |
| 195 base::ScopedTempDir temp_dir_; | 208 base::ScopedTempDir temp_dir_; |
| 196 base::WaitableEvent wait_event_; | 209 base::WaitableEvent wait_event_; |
| 197 base::WaitableEvent signal_event_; | 210 base::WaitableEvent signal_event_; |
| 198 | 211 |
| 199 scoped_ptr<base::Thread> worker_thread_; | 212 scoped_ptr<base::Thread> worker_thread_; |
| 200 ProcessSingleton* process_singleton_on_thread_; | 213 TestableProcessSingleton* process_singleton_on_thread_; |
| 201 | 214 |
| 202 std::vector<CommandLine::StringVector> callback_command_lines_; | 215 std::vector<CommandLine::StringVector> callback_command_lines_; |
| 203 }; | 216 }; |
| 204 | 217 |
| 205 } // namespace | 218 } // namespace |
| 206 | 219 |
| 207 // Test if the socket file and symbol link created by ProcessSingletonLinux | 220 // Test if the socket file and symbol link created by ProcessSingletonLinux |
| 208 // are valid. | 221 // are valid. |
| 209 // If this test flakes, use http://crbug.com/74554. | 222 // If this test flakes, use http://crbug.com/74554. |
| 210 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { | 223 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 | 338 |
| 326 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); | 339 ASSERT_EQ(0, unlink(lock_path_.value().c_str())); |
| 327 | 340 |
| 328 UnblockWorkerThread(); | 341 UnblockWorkerThread(); |
| 329 } | 342 } |
| 330 | 343 |
| 331 // Test that Create fails when another browser is using the profile directory. | 344 // Test that Create fails when another browser is using the profile directory. |
| 332 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { | 345 TEST_F(ProcessSingletonLinuxTest, CreateFailsWithExistingBrowser) { |
| 333 CreateProcessSingletonOnThread(); | 346 CreateProcessSingletonOnThread(); |
| 334 | 347 |
| 335 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 348 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 349 CreateProcessSingleton()); |
| 336 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); | 350 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); |
| 337 EXPECT_FALSE(process_singleton->Create( | 351 EXPECT_FALSE(process_singleton->Create( |
| 338 base::Bind(&NotificationCallback))); | 352 base::Bind(&NotificationCallback))); |
| 339 } | 353 } |
| 340 | 354 |
| 341 // Test that Create fails when another browser is using the profile directory | 355 // Test that Create fails when another browser is using the profile directory |
| 342 // but with the old socket location. | 356 // but with the old socket location. |
| 343 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { | 357 TEST_F(ProcessSingletonLinuxTest, CreateChecksCompatibilitySocket) { |
| 344 CreateProcessSingletonOnThread(); | 358 CreateProcessSingletonOnThread(); |
| 345 scoped_ptr<ProcessSingleton> process_singleton(CreateProcessSingleton()); | 359 scoped_ptr<TestableProcessSingleton> process_singleton( |
| 360 CreateProcessSingleton()); |
| 346 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); | 361 process_singleton->OverrideCurrentPidForTesting(base::GetCurrentProcId() + 1); |
| 347 | 362 |
| 348 // Do some surgery so as to look like the old configuration. | 363 // Do some surgery so as to look like the old configuration. |
| 349 char buf[PATH_MAX]; | 364 char buf[PATH_MAX]; |
| 350 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); | 365 ssize_t len = readlink(socket_path_.value().c_str(), buf, sizeof(buf)); |
| 351 ASSERT_GT(len, 0); | 366 ASSERT_GT(len, 0); |
| 352 FilePath socket_target_path = FilePath(std::string(buf, len)); | 367 FilePath socket_target_path = FilePath(std::string(buf, len)); |
| 353 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); | 368 ASSERT_EQ(0, unlink(socket_path_.value().c_str())); |
| 354 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), | 369 ASSERT_EQ(0, rename(socket_target_path.value().c_str(), |
| 355 socket_path_.value().c_str())); | 370 socket_path_.value().c_str())); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 368 | 383 |
| 369 // Also change the hostname, so the remote does not retry. | 384 // Also change the hostname, so the remote does not retry. |
| 370 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); | 385 EXPECT_EQ(0, unlink(lock_path_.value().c_str())); |
| 371 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); | 386 EXPECT_EQ(0, symlink("FAKEFOOHOST-1234", lock_path_.value().c_str())); |
| 372 | 387 |
| 373 std::string url("about:blank"); | 388 std::string url("about:blank"); |
| 374 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, | 389 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, |
| 375 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); | 390 NotifyOtherProcessOrCreate(url, TestTimeouts::action_timeout())); |
| 376 } | 391 } |
| 377 | 392 |
| OLD | NEW |