| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ | |
| 6 #define CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/sequenced_task_runner_helpers.h" | |
| 11 #include "base/synchronization/waitable_event.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/notification_registrar.h" | |
| 14 #include "content/public/test/mock_notification_observer.h" | |
| 15 | |
| 16 // Helper class to add and remove observers on a non-UI thread from | |
| 17 // the UI thread. | |
| 18 template <class T, typename Traits> | |
| 19 class ThreadObserverHelper : public base::RefCountedThreadSafe<T, Traits> { | |
| 20 public: | |
| 21 explicit ThreadObserverHelper(content::BrowserThread::ID id) | |
| 22 : id_(id), done_event_(false, false) {} | |
| 23 | |
| 24 void Init() { | |
| 25 using content::BrowserThread; | |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 27 BrowserThread::PostTask( | |
| 28 id_, | |
| 29 FROM_HERE, | |
| 30 base::Bind(&ThreadObserverHelper::RegisterObserversTask, this)); | |
| 31 done_event_.Wait(); | |
| 32 } | |
| 33 | |
| 34 virtual ~ThreadObserverHelper() { | |
| 35 DCHECK(content::BrowserThread::CurrentlyOn(id_)); | |
| 36 registrar_.RemoveAll(); | |
| 37 } | |
| 38 | |
| 39 content::MockNotificationObserver* observer() { | |
| 40 return &observer_; | |
| 41 } | |
| 42 | |
| 43 protected: | |
| 44 friend class base::RefCountedThreadSafe<T>; | |
| 45 | |
| 46 virtual void RegisterObservers() = 0; | |
| 47 | |
| 48 content::NotificationRegistrar registrar_; | |
| 49 content::MockNotificationObserver observer_; | |
| 50 | |
| 51 private: | |
| 52 void RegisterObserversTask() { | |
| 53 DCHECK(content::BrowserThread::CurrentlyOn(id_)); | |
| 54 RegisterObservers(); | |
| 55 done_event_.Signal(); | |
| 56 } | |
| 57 | |
| 58 content::BrowserThread::ID id_; | |
| 59 base::WaitableEvent done_event_; | |
| 60 }; | |
| 61 | |
| 62 class DBThreadObserverHelper; | |
| 63 typedef ThreadObserverHelper< | |
| 64 DBThreadObserverHelper, | |
| 65 content::BrowserThread::DeleteOnDBThread> DBThreadObserverHelperBase; | |
| 66 | |
| 67 class DBThreadObserverHelper : public DBThreadObserverHelperBase { | |
| 68 public: | |
| 69 DBThreadObserverHelper() : | |
| 70 DBThreadObserverHelperBase(content::BrowserThread::DB) {} | |
| 71 | |
| 72 protected: | |
| 73 friend struct content::BrowserThread::DeleteOnThread< | |
| 74 content::BrowserThread::DB>; | |
| 75 friend class base::DeleteHelper<DBThreadObserverHelper>; | |
| 76 | |
| 77 virtual ~DBThreadObserverHelper() {} | |
| 78 }; | |
| 79 | |
| 80 #endif // CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ | |
| OLD | NEW |