Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(521)

Side by Side Diff: chrome/test/base/thread_observer_helper.h

Issue 12476031: Refactor notifications of chrome/browser/webdata (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_
OLDNEW
« no previous file with comments | « chrome/common/chrome_notification_types.h ('k') | components/autofill/browser/autofill_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698