OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/prefs/public/pref_member.h" | 5 #include "base/prefs/public/pref_member.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 void FetchValue() { | 50 void FetchValue() { |
51 base::WaitableEvent event(true, false); | 51 base::WaitableEvent event(true, false); |
52 ASSERT_TRUE( | 52 ASSERT_TRUE( |
53 pref_thread_.message_loop_proxy()->PostTask( | 53 pref_thread_.message_loop_proxy()->PostTask( |
54 FROM_HERE, | 54 FROM_HERE, |
55 base::Bind(&GetPrefValueHelper::GetPrefValue, this, &event))); | 55 base::Bind(&GetPrefValueHelper::GetPrefValue, this, &event))); |
56 event.Wait(); | 56 event.Wait(); |
57 } | 57 } |
58 | 58 |
| 59 // The thread must be stopped on the main thread. GetPrefValueHelper being |
| 60 // ref-counted, the destructor can be called from any thread. |
| 61 void StopThread() { |
| 62 pref_thread_.Stop(); |
| 63 } |
| 64 |
59 bool value() { return value_; } | 65 bool value() { return value_; } |
60 | 66 |
61 private: | 67 private: |
62 friend class base::RefCountedThreadSafe<GetPrefValueHelper>; | 68 friend class base::RefCountedThreadSafe<GetPrefValueHelper>; |
63 ~GetPrefValueHelper() {} | 69 ~GetPrefValueHelper() {} |
64 | 70 |
65 void GetPrefValue(base::WaitableEvent* event) { | 71 void GetPrefValue(base::WaitableEvent* event) { |
66 value_ = pref_.GetValue(); | 72 value_ = pref_.GetValue(); |
67 event->Signal(); | 73 event->Signal(); |
68 } | 74 } |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 312 |
307 prefs.SetBoolean(kBoolPref, true); | 313 prefs.SetBoolean(kBoolPref, true); |
308 | 314 |
309 helper->FetchValue(); | 315 helper->FetchValue(); |
310 EXPECT_TRUE(helper->value()); | 316 EXPECT_TRUE(helper->value()); |
311 | 317 |
312 helper->Destroy(); | 318 helper->Destroy(); |
313 | 319 |
314 helper->FetchValue(); | 320 helper->FetchValue(); |
315 EXPECT_TRUE(helper->value()); | 321 EXPECT_TRUE(helper->value()); |
| 322 |
| 323 helper->StopThread(); |
316 } | 324 } |
OLD | NEW |