OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/un.h> | 10 #include <sys/un.h> |
(...skipping 28 matching lines...) Expand all Loading... |
39 public: | 39 public: |
40 // A ProcessSingleton exposing some protected methods for testing. | 40 // A ProcessSingleton exposing some protected methods for testing. |
41 class TestableProcessSingleton : public ProcessSingleton { | 41 class TestableProcessSingleton : public ProcessSingleton { |
42 public: | 42 public: |
43 explicit TestableProcessSingleton(const base::FilePath& user_data_dir) | 43 explicit TestableProcessSingleton(const base::FilePath& user_data_dir) |
44 : ProcessSingleton( | 44 : ProcessSingleton( |
45 user_data_dir, | 45 user_data_dir, |
46 base::Bind(&TestableProcessSingleton::NotificationCallback, | 46 base::Bind(&TestableProcessSingleton::NotificationCallback, |
47 base::Unretained(this))) {} | 47 base::Unretained(this))) {} |
48 | 48 |
49 | 49 std::vector<base::CommandLine::StringVector> callback_command_lines_; |
50 std::vector<CommandLine::StringVector> callback_command_lines_; | |
51 | 50 |
52 using ProcessSingleton::NotifyOtherProcessWithTimeout; | 51 using ProcessSingleton::NotifyOtherProcessWithTimeout; |
53 using ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate; | 52 using ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate; |
54 using ProcessSingleton::OverrideCurrentPidForTesting; | 53 using ProcessSingleton::OverrideCurrentPidForTesting; |
55 using ProcessSingleton::OverrideKillCallbackForTesting; | 54 using ProcessSingleton::OverrideKillCallbackForTesting; |
56 | 55 |
57 private: | 56 private: |
58 bool NotificationCallback(const CommandLine& command_line, | 57 bool NotificationCallback(const base::CommandLine& command_line, |
59 const base::FilePath& current_directory) { | 58 const base::FilePath& current_directory) { |
60 callback_command_lines_.push_back(command_line.argv()); | 59 callback_command_lines_.push_back(command_line.argv()); |
61 return true; | 60 return true; |
62 } | 61 } |
63 }; | 62 }; |
64 | 63 |
65 ProcessSingletonPosixTest() | 64 ProcessSingletonPosixTest() |
66 : kill_callbacks_(0), | 65 : kill_callbacks_(0), |
67 io_thread_(BrowserThread::IO), | 66 io_thread_(BrowserThread::IO), |
68 wait_event_(true, false), | 67 wait_event_(true, false), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 base::FilePath remote_cookie_path = socket_target_path.DirName(). | 155 base::FilePath remote_cookie_path = socket_target_path.DirName(). |
157 Append(chrome::kSingletonCookieFilename); | 156 Append(chrome::kSingletonCookieFilename); |
158 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); | 157 len = readlink(remote_cookie_path.value().c_str(), buf, PATH_MAX); |
159 ASSERT_GT(len, 0); | 158 ASSERT_GT(len, 0); |
160 EXPECT_EQ(cookie, std::string(buf, len)); | 159 EXPECT_EQ(cookie, std::string(buf, len)); |
161 } | 160 } |
162 | 161 |
163 ProcessSingleton::NotifyResult NotifyOtherProcess(bool override_kill) { | 162 ProcessSingleton::NotifyResult NotifyOtherProcess(bool override_kill) { |
164 scoped_ptr<TestableProcessSingleton> process_singleton( | 163 scoped_ptr<TestableProcessSingleton> process_singleton( |
165 CreateProcessSingleton()); | 164 CreateProcessSingleton()); |
166 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); | 165 base::CommandLine command_line( |
| 166 base::CommandLine::ForCurrentProcess()->GetProgram()); |
167 command_line.AppendArg("about:blank"); | 167 command_line.AppendArg("about:blank"); |
168 if (override_kill) { | 168 if (override_kill) { |
169 process_singleton->OverrideCurrentPidForTesting( | 169 process_singleton->OverrideCurrentPidForTesting( |
170 base::GetCurrentProcId() + 1); | 170 base::GetCurrentProcId() + 1); |
171 process_singleton->OverrideKillCallbackForTesting( | 171 process_singleton->OverrideKillCallbackForTesting( |
172 base::Bind(&ProcessSingletonPosixTest::KillCallback, | 172 base::Bind(&ProcessSingletonPosixTest::KillCallback, |
173 base::Unretained(this))); | 173 base::Unretained(this))); |
174 } | 174 } |
175 | 175 |
176 return process_singleton->NotifyOtherProcessWithTimeout( | 176 return process_singleton->NotifyOtherProcessWithTimeout( |
177 command_line, kRetryAttempts, timeout(), true); | 177 command_line, kRetryAttempts, timeout(), true); |
178 } | 178 } |
179 | 179 |
180 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). | 180 // A helper method to call ProcessSingleton::NotifyOtherProcessOrCreate(). |
181 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( | 181 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate( |
182 const std::string& url) { | 182 const std::string& url) { |
183 scoped_ptr<TestableProcessSingleton> process_singleton( | 183 scoped_ptr<TestableProcessSingleton> process_singleton( |
184 CreateProcessSingleton()); | 184 CreateProcessSingleton()); |
185 CommandLine command_line(CommandLine::ForCurrentProcess()->GetProgram()); | 185 base::CommandLine command_line( |
| 186 base::CommandLine::ForCurrentProcess()->GetProgram()); |
186 command_line.AppendArg(url); | 187 command_line.AppendArg(url); |
187 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( | 188 return process_singleton->NotifyOtherProcessWithTimeoutOrCreate( |
188 command_line, kRetryAttempts, timeout()); | 189 command_line, kRetryAttempts, timeout()); |
189 } | 190 } |
190 | 191 |
191 void CheckNotified() { | 192 void CheckNotified() { |
192 ASSERT_TRUE(process_singleton_on_thread_ != NULL); | 193 ASSERT_TRUE(process_singleton_on_thread_ != NULL); |
193 ASSERT_EQ(1u, process_singleton_on_thread_->callback_command_lines_.size()); | 194 ASSERT_EQ(1u, process_singleton_on_thread_->callback_command_lines_.size()); |
194 bool found = false; | 195 bool found = false; |
195 for (size_t i = 0; | 196 for (size_t i = 0; |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 // Test that if there is an existing lock file, and it's not locked, we replace | 421 // Test that if there is an existing lock file, and it's not locked, we replace |
421 // it. | 422 // it. |
422 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { | 423 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { |
423 scoped_ptr<TestableProcessSingleton> process_singleton( | 424 scoped_ptr<TestableProcessSingleton> process_singleton( |
424 CreateProcessSingleton()); | 425 CreateProcessSingleton()); |
425 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); | 426 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); |
426 EXPECT_TRUE(process_singleton->Create()); | 427 EXPECT_TRUE(process_singleton->Create()); |
427 VerifyFiles(); | 428 VerifyFiles(); |
428 } | 429 } |
429 #endif // defined(OS_MACOSX) | 430 #endif // defined(OS_MACOSX) |
OLD | NEW |