Chromium Code Reviews| 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/observer_list.h" | 5 #include "base/observer_list.h" |
| 6 #include "base/observer_list_threadsafe.h" | 6 #include "base/observer_list_threadsafe.h" |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using base::PlatformThread; | 16 using base::PlatformThread; |
| 17 using base::Time; | 17 using base::Time; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class Foo { | 21 class Foo { |
| 22 public: | 22 public: |
| 23 virtual void Observe(int x) = 0; | 23 virtual void Observe(int x) = 0; |
| 24 virtual ~Foo() {} | 24 virtual ~Foo() {} |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 class Adder : public Foo { | 27 class Adder : public Foo { |
| 28 public: | 28 public: |
| 29 explicit Adder(int scaler) : total(0), scaler_(scaler) {} | 29 explicit Adder(int scaler) : total(0), scaler_(scaler) {} |
| 30 virtual void Observe(int x) { | 30 virtual void Observe(int x) OVERRIDE { |
| 31 total += x * scaler_; | 31 total += x * scaler_; |
| 32 } | 32 } |
| 33 virtual ~Adder() { } | 33 virtual ~Adder() { } |
| 34 int total; | 34 int total; |
| 35 private: | 35 private: |
| 36 int scaler_; | 36 int scaler_; |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 class Disrupter : public Foo { | 39 class Disrupter : public Foo { |
| 40 public: | 40 public: |
| 41 Disrupter(ObserverList<Foo>* list, Foo* doomed) | 41 Disrupter(ObserverList<Foo>* list, Foo* doomed) |
| 42 : list_(list), doomed_(doomed) { } | 42 : list_(list), doomed_(doomed) { } |
|
jar (doing other things)
2012/04/05 21:11:30
nit: one initializer per line.
Also the occaisona
| |
| 43 virtual ~Disrupter() { } | 43 virtual ~Disrupter() { } |
| 44 virtual void Observe(int x) { | 44 virtual void Observe(int x) OVERRIDE { |
| 45 list_->RemoveObserver(doomed_); | 45 list_->RemoveObserver(doomed_); |
| 46 } | 46 } |
| 47 private: | 47 private: |
| 48 ObserverList<Foo>* list_; | 48 ObserverList<Foo>* list_; |
| 49 Foo* doomed_; | 49 Foo* doomed_; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class ThreadSafeDisrupter : public Foo { | 52 class ThreadSafeDisrupter : public Foo { |
| 53 public: | 53 public: |
| 54 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) | 54 ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) |
| 55 : list_(list), doomed_(doomed) { } | 55 : list_(list), doomed_(doomed) { } |
| 56 virtual ~ThreadSafeDisrupter() { } | 56 virtual ~ThreadSafeDisrupter() { } |
| 57 virtual void Observe(int x) { | 57 virtual void Observe(int x) OVERRIDE { |
| 58 list_->RemoveObserver(doomed_); | 58 list_->RemoveObserver(doomed_); |
| 59 } | 59 } |
| 60 private: | 60 private: |
| 61 ObserverListThreadSafe<Foo>* list_; | 61 ObserverListThreadSafe<Foo>* list_; |
| 62 Foo* doomed_; | 62 Foo* doomed_; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 template <typename ObserverListType> | 65 template <typename ObserverListType> |
| 66 class AddInObserve : public Foo { | 66 class AddInObserve : public Foo { |
| 67 public: | 67 public: |
| 68 explicit AddInObserve(ObserverListType* observer_list) | 68 explicit AddInObserve(ObserverListType* observer_list) |
| 69 : added(false), | 69 : added(false), |
| 70 observer_list(observer_list), | 70 observer_list(observer_list), |
| 71 adder(1) { | 71 adder(1) { |
| 72 } | 72 } |
| 73 virtual void Observe(int x) { | 73 virtual void Observe(int x) OVERRIDE { |
| 74 if (!added) { | 74 if (!added) { |
| 75 added = true; | 75 added = true; |
| 76 observer_list->AddObserver(&adder); | 76 observer_list->AddObserver(&adder); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool added; | 80 bool added; |
| 81 ObserverListType* observer_list; | 81 ObserverListType* observer_list; |
| 82 Adder adder; | 82 Adder adder; |
| 83 }; | 83 }; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 97 start_(Time::Now()), | 97 start_(Time::Now()), |
| 98 count_observes_(0), | 98 count_observes_(0), |
| 99 count_addtask_(0), | 99 count_addtask_(0), |
| 100 do_notifies_(notify), | 100 do_notifies_(notify), |
| 101 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 101 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 102 } | 102 } |
| 103 | 103 |
| 104 virtual ~AddRemoveThread() { | 104 virtual ~AddRemoveThread() { |
| 105 } | 105 } |
| 106 | 106 |
| 107 void ThreadMain() { | 107 virtual void ThreadMain() OVERRIDE { |
| 108 loop_ = new MessageLoop(); // Fire up a message loop. | 108 loop_ = new MessageLoop(); // Fire up a message loop. |
| 109 loop_->PostTask( | 109 loop_->PostTask( |
| 110 FROM_HERE, | 110 FROM_HERE, |
| 111 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); | 111 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); |
| 112 loop_->Run(); | 112 loop_->Run(); |
| 113 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << | 113 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << |
| 114 // count_observes_ << ", " << count_addtask_; | 114 // count_observes_ << ", " << count_addtask_; |
| 115 delete loop_; | 115 delete loop_; |
| 116 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); | 116 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); |
| 117 delete this; | 117 delete this; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 138 | 138 |
| 139 loop_->PostTask( | 139 loop_->PostTask( |
| 140 FROM_HERE, | 140 FROM_HERE, |
| 141 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); | 141 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); |
| 142 } | 142 } |
| 143 | 143 |
| 144 void Quit() { | 144 void Quit() { |
| 145 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 145 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 146 } | 146 } |
| 147 | 147 |
| 148 virtual void Observe(int x) { | 148 virtual void Observe(int x) OVERRIDE { |
| 149 count_observes_++; | 149 count_observes_++; |
| 150 | 150 |
| 151 // If we're getting called after we removed ourselves from | 151 // If we're getting called after we removed ourselves from |
| 152 // the list, that is very bad! | 152 // the list, that is very bad! |
| 153 DCHECK(in_list_); | 153 DCHECK(in_list_); |
| 154 | 154 |
| 155 // This callback should fire on the appropriate thread | 155 // This callback should fire on the appropriate thread |
| 156 EXPECT_EQ(loop_, MessageLoop::current()); | 156 EXPECT_EQ(loop_, MessageLoop::current()); |
| 157 | 157 |
| 158 list_->RemoveObserver(this); | 158 list_->RemoveObserver(this); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 | 311 |
| 312 class FooRemover : public Foo { | 312 class FooRemover : public Foo { |
| 313 public: | 313 public: |
| 314 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} | 314 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} |
| 315 virtual ~FooRemover() {} | 315 virtual ~FooRemover() {} |
| 316 | 316 |
| 317 void AddFooToRemove(Foo* foo) { | 317 void AddFooToRemove(Foo* foo) { |
| 318 foos_.push_back(foo); | 318 foos_.push_back(foo); |
| 319 } | 319 } |
| 320 | 320 |
| 321 virtual void Observe(int x) { | 321 virtual void Observe(int x) OVERRIDE { |
| 322 std::vector<Foo*> tmp; | 322 std::vector<Foo*> tmp; |
| 323 tmp.swap(foos_); | 323 tmp.swap(foos_); |
| 324 for (std::vector<Foo*>::iterator it = tmp.begin(); | 324 for (std::vector<Foo*>::iterator it = tmp.begin(); |
| 325 it != tmp.end(); ++it) { | 325 it != tmp.end(); ++it) { |
| 326 list_->RemoveObserver(*it); | 326 list_->RemoveObserver(*it); |
| 327 } | 327 } |
| 328 } | 328 } |
| 329 | 329 |
| 330 private: | 330 private: |
| 331 const scoped_refptr<ObserverListThreadSafe<Foo> > list_; | 331 const scoped_refptr<ObserverListThreadSafe<Foo> > list_; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 463 observer_list->Notify(&Foo::Observe, 1); | 463 observer_list->Notify(&Foo::Observe, 1); |
| 464 loop.RunAllPending(); | 464 loop.RunAllPending(); |
| 465 EXPECT_EQ(1, b.adder.total); | 465 EXPECT_EQ(1, b.adder.total); |
| 466 } | 466 } |
| 467 | 467 |
| 468 class AddInClearObserve : public Foo { | 468 class AddInClearObserve : public Foo { |
| 469 public: | 469 public: |
| 470 explicit AddInClearObserve(ObserverList<Foo>* list) | 470 explicit AddInClearObserve(ObserverList<Foo>* list) |
| 471 : list_(list), added_(false), adder_(1) {} | 471 : list_(list), added_(false), adder_(1) {} |
| 472 | 472 |
| 473 virtual void Observe(int /* x */) { | 473 virtual void Observe(int /* x */) OVERRIDE { |
| 474 list_->Clear(); | 474 list_->Clear(); |
| 475 list_->AddObserver(&adder_); | 475 list_->AddObserver(&adder_); |
| 476 added_ = true; | 476 added_ = true; |
| 477 } | 477 } |
| 478 | 478 |
| 479 bool added() const { return added_; } | 479 bool added() const { return added_; } |
| 480 const Adder& adder() const { return adder_; } | 480 const Adder& adder() const { return adder_; } |
| 481 | 481 |
| 482 private: | 482 private: |
| 483 ObserverList<Foo>* const list_; | 483 ObserverList<Foo>* const list_; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 506 | 506 |
| 507 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); | 507 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 508 EXPECT_TRUE(a.added()); | 508 EXPECT_TRUE(a.added()); |
| 509 EXPECT_EQ(0, a.adder().total) | 509 EXPECT_EQ(0, a.adder().total) |
| 510 << "Adder should not observe, so sum should still be 0."; | 510 << "Adder should not observe, so sum should still be 0."; |
| 511 } | 511 } |
| 512 | 512 |
| 513 class ListDestructor : public Foo { | 513 class ListDestructor : public Foo { |
| 514 public: | 514 public: |
| 515 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} | 515 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} |
| 516 virtual void Observe(int x) { | 516 virtual ~ListDestructor() {} |
| 517 | |
| 518 virtual void Observe(int x) OVERRIDE { | |
| 517 delete list_; | 519 delete list_; |
| 518 } | 520 } |
| 519 virtual ~ListDestructor() { } | |
| 520 int total; | 521 int total; |
| 521 private: | 522 private: |
| 522 ObserverList<Foo>* list_; | 523 ObserverList<Foo>* list_; |
| 523 }; | 524 }; |
| 524 | 525 |
| 525 | 526 |
| 526 TEST(ObserverListTest, IteratorOutlivesList) { | 527 TEST(ObserverListTest, IteratorOutlivesList) { |
| 527 ObserverList<Foo>* observer_list = new ObserverList<Foo>; | 528 ObserverList<Foo>* observer_list = new ObserverList<Foo>; |
| 528 ListDestructor a(observer_list); | 529 ListDestructor a(observer_list); |
| 529 observer_list->AddObserver(&a); | 530 observer_list->AddObserver(&a); |
| 530 | 531 |
| 531 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); | 532 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
| 532 // If this test fails, there'll be Valgrind errors when this function goes out | 533 // If this test fails, there'll be Valgrind errors when this function goes out |
| 533 // of scope. | 534 // of scope. |
| 534 } | 535 } |
| 535 | 536 |
| 536 } // namespace | 537 } // namespace |
| OLD | NEW |