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> |
11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
19 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
20 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
21 #include "base/files/scoped_temp_dir.h" | 21 #include "base/files/scoped_temp_dir.h" |
22 #include "base/message_loop/message_loop.h" | 22 #include "base/location.h" |
23 #include "base/posix/eintr_wrapper.h" | 23 #include "base/posix/eintr_wrapper.h" |
| 24 #include "base/single_thread_task_runner.h" |
24 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
25 #include "base/synchronization/waitable_event.h" | 26 #include "base/synchronization/waitable_event.h" |
26 #include "base/test/test_timeouts.h" | 27 #include "base/test/test_timeouts.h" |
27 #include "base/test/thread_test_helper.h" | 28 #include "base/test/thread_test_helper.h" |
28 #include "base/threading/thread.h" | 29 #include "base/threading/thread.h" |
29 #include "chrome/common/chrome_constants.h" | 30 #include "chrome/common/chrome_constants.h" |
30 #include "content/public/test/test_browser_thread.h" | 31 #include "content/public/test/test_browser_thread.h" |
31 #include "net/base/net_util.h" | 32 #include "net/base/net_util.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
33 | 34 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 } | 90 } |
90 | 91 |
91 void TearDown() override { | 92 void TearDown() override { |
92 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper( | 93 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper( |
93 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get())); | 94 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get())); |
94 ASSERT_TRUE(io_helper->Run()); | 95 ASSERT_TRUE(io_helper->Run()); |
95 | 96 |
96 // Destruct the ProcessSingleton object before the IO thread so that its | 97 // Destruct the ProcessSingleton object before the IO thread so that its |
97 // internals are destructed properly. | 98 // internals are destructed properly. |
98 if (process_singleton_on_thread_) { | 99 if (process_singleton_on_thread_) { |
99 worker_thread_->message_loop()->PostTask( | 100 worker_thread_->task_runner()->PostTask( |
100 FROM_HERE, | 101 FROM_HERE, |
101 base::Bind(&ProcessSingletonPosixTest::DestructProcessSingleton, | 102 base::Bind(&ProcessSingletonPosixTest::DestructProcessSingleton, |
102 base::Unretained(this))); | 103 base::Unretained(this))); |
103 | 104 |
104 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper( | 105 scoped_refptr<base::ThreadTestHelper> helper( |
105 worker_thread_->message_loop_proxy().get())); | 106 new base::ThreadTestHelper(worker_thread_->task_runner().get())); |
106 ASSERT_TRUE(helper->Run()); | 107 ASSERT_TRUE(helper->Run()); |
107 } | 108 } |
108 | 109 |
109 io_thread_.Stop(); | 110 io_thread_.Stop(); |
110 testing::Test::TearDown(); | 111 testing::Test::TearDown(); |
111 } | 112 } |
112 | 113 |
113 void CreateProcessSingletonOnThread() { | 114 void CreateProcessSingletonOnThread() { |
114 ASSERT_EQ(NULL, worker_thread_.get()); | 115 ASSERT_EQ(NULL, worker_thread_.get()); |
115 worker_thread_.reset(new base::Thread("BlockingThread")); | 116 worker_thread_.reset(new base::Thread("BlockingThread")); |
116 worker_thread_->Start(); | 117 worker_thread_->Start(); |
117 | 118 |
118 worker_thread_->message_loop()->PostTask( | 119 worker_thread_->task_runner()->PostTask( |
119 FROM_HERE, | 120 FROM_HERE, |
120 base::Bind(&ProcessSingletonPosixTest:: | 121 base::Bind(&ProcessSingletonPosixTest::CreateProcessSingletonInternal, |
121 CreateProcessSingletonInternal, | 122 base::Unretained(this))); |
122 base::Unretained(this))); | |
123 | 123 |
124 scoped_refptr<base::ThreadTestHelper> helper( | 124 scoped_refptr<base::ThreadTestHelper> helper( |
125 new base::ThreadTestHelper(worker_thread_->message_loop_proxy().get())); | 125 new base::ThreadTestHelper(worker_thread_->task_runner().get())); |
126 ASSERT_TRUE(helper->Run()); | 126 ASSERT_TRUE(helper->Run()); |
127 } | 127 } |
128 | 128 |
129 TestableProcessSingleton* CreateProcessSingleton() { | 129 TestableProcessSingleton* CreateProcessSingleton() { |
130 return new TestableProcessSingleton(user_data_path_); | 130 return new TestableProcessSingleton(user_data_path_); |
131 } | 131 } |
132 | 132 |
133 void VerifyFiles() { | 133 void VerifyFiles() { |
134 struct stat statbuf; | 134 struct stat statbuf; |
135 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); | 135 ASSERT_EQ(0, lstat(lock_path_.value().c_str(), &statbuf)); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 "about:blank") { | 200 "about:blank") { |
201 found = true; | 201 found = true; |
202 break; | 202 break; |
203 } | 203 } |
204 } | 204 } |
205 ASSERT_TRUE(found); | 205 ASSERT_TRUE(found); |
206 ASSERT_EQ(0, kill_callbacks_); | 206 ASSERT_EQ(0, kill_callbacks_); |
207 } | 207 } |
208 | 208 |
209 void BlockWorkerThread() { | 209 void BlockWorkerThread() { |
210 worker_thread_->message_loop()->PostTask( | 210 worker_thread_->task_runner()->PostTask( |
211 FROM_HERE, | 211 FROM_HERE, base::Bind(&ProcessSingletonPosixTest::BlockThread, |
212 base::Bind(&ProcessSingletonPosixTest::BlockThread, | 212 base::Unretained(this))); |
213 base::Unretained(this))); | |
214 } | 213 } |
215 | 214 |
216 void UnblockWorkerThread() { | 215 void UnblockWorkerThread() { |
217 wait_event_.Signal(); // Unblock the worker thread for shutdown. | 216 wait_event_.Signal(); // Unblock the worker thread for shutdown. |
218 signal_event_.Wait(); // Ensure thread unblocks before continuing. | 217 signal_event_.Wait(); // Ensure thread unblocks before continuing. |
219 } | 218 } |
220 | 219 |
221 void BlockThread() { | 220 void BlockThread() { |
222 wait_event_.Wait(); | 221 wait_event_.Wait(); |
223 signal_event_.Signal(); | 222 signal_event_.Signal(); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 // 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 |
423 // it. | 422 // it. |
424 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { | 423 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { |
425 scoped_ptr<TestableProcessSingleton> process_singleton( | 424 scoped_ptr<TestableProcessSingleton> process_singleton( |
426 CreateProcessSingleton()); | 425 CreateProcessSingleton()); |
427 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); | 426 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); |
428 EXPECT_TRUE(process_singleton->Create()); | 427 EXPECT_TRUE(process_singleton->Create()); |
429 VerifyFiles(); | 428 VerifyFiles(); |
430 } | 429 } |
431 #endif // defined(OS_MACOSX) | 430 #endif // defined(OS_MACOSX) |
OLD | NEW |