| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ | 5 #ifndef CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ |
| 6 #define CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ | 6 #define CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/sequenced_task_runner_helpers.h" | 11 #include "base/sequenced_task_runner_helpers.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 15 #include "content/test/notification_observer_mock.h" | 15 #include "content/public/test/mock_notification_observer.h" |
| 16 | 16 |
| 17 // Helper class to add and remove observers on a non-UI thread from | 17 // Helper class to add and remove observers on a non-UI thread from |
| 18 // the UI thread. | 18 // the UI thread. |
| 19 template <class T, typename Traits> | 19 template <class T, typename Traits> |
| 20 class ThreadObserverHelper : public base::RefCountedThreadSafe<T, Traits> { | 20 class ThreadObserverHelper : public base::RefCountedThreadSafe<T, Traits> { |
| 21 public: | 21 public: |
| 22 explicit ThreadObserverHelper(content::BrowserThread::ID id) | 22 explicit ThreadObserverHelper(content::BrowserThread::ID id) |
| 23 : id_(id), done_event_(false, false) {} | 23 : id_(id), done_event_(false, false) {} |
| 24 | 24 |
| 25 void Init() { | 25 void Init() { |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 28 BrowserThread::PostTask( | 28 BrowserThread::PostTask( |
| 29 id_, | 29 id_, |
| 30 FROM_HERE, | 30 FROM_HERE, |
| 31 base::Bind(&ThreadObserverHelper::RegisterObserversTask, this)); | 31 base::Bind(&ThreadObserverHelper::RegisterObserversTask, this)); |
| 32 done_event_.Wait(); | 32 done_event_.Wait(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 virtual ~ThreadObserverHelper() { | 35 virtual ~ThreadObserverHelper() { |
| 36 DCHECK(content::BrowserThread::CurrentlyOn(id_)); | 36 DCHECK(content::BrowserThread::CurrentlyOn(id_)); |
| 37 registrar_.RemoveAll(); | 37 registrar_.RemoveAll(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 content::NotificationObserverMock* observer() { | 40 content::MockNotificationObserver* observer() { |
| 41 return &observer_; | 41 return &observer_; |
| 42 } | 42 } |
| 43 | 43 |
| 44 protected: | 44 protected: |
| 45 friend class base::RefCountedThreadSafe<T>; | 45 friend class base::RefCountedThreadSafe<T>; |
| 46 | 46 |
| 47 virtual void RegisterObservers() = 0; | 47 virtual void RegisterObservers() = 0; |
| 48 | 48 |
| 49 content::NotificationRegistrar registrar_; | 49 content::NotificationRegistrar registrar_; |
| 50 content::NotificationObserverMock observer_; | 50 content::MockNotificationObserver observer_; |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 void RegisterObserversTask() { | 53 void RegisterObserversTask() { |
| 54 DCHECK(content::BrowserThread::CurrentlyOn(id_)); | 54 DCHECK(content::BrowserThread::CurrentlyOn(id_)); |
| 55 RegisterObservers(); | 55 RegisterObservers(); |
| 56 done_event_.Signal(); | 56 done_event_.Signal(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 content::BrowserThread::ID id_; | 59 content::BrowserThread::ID id_; |
| 60 base::WaitableEvent done_event_; | 60 base::WaitableEvent done_event_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 72 | 72 |
| 73 protected: | 73 protected: |
| 74 friend struct content::BrowserThread::DeleteOnThread< | 74 friend struct content::BrowserThread::DeleteOnThread< |
| 75 content::BrowserThread::DB>; | 75 content::BrowserThread::DB>; |
| 76 friend class base::DeleteHelper<DBThreadObserverHelper>; | 76 friend class base::DeleteHelper<DBThreadObserverHelper>; |
| 77 | 77 |
| 78 virtual ~DBThreadObserverHelper() {} | 78 virtual ~DBThreadObserverHelper() {} |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 #endif // CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ | 81 #endif // CHROME_TEST_BASE_THREAD_OBSERVER_HELPER_H_ |
| OLD | NEW |