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