forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension_test_notification_observer.h
155 lines (120 loc) · 5.47 KB
/
extension_test_notification_observer.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2016 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 EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
#define EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
#include <map>
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/callback_list.h"
#include "base/macros.h"
#include "base/scoped_observation.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
namespace content {
class BrowserContext;
class NotificationDetails;
class WebContents;
class WindowedNotificationObserver;
}
namespace extensions {
// Test helper class for observing extension-related events.
class ExtensionTestNotificationObserver : public content::NotificationObserver,
ExtensionRegistryObserver {
public:
explicit ExtensionTestNotificationObserver(content::BrowserContext* context);
~ExtensionTestNotificationObserver() override;
// Wait for the specified extension to crash. Returns true if it really
// crashed.
bool WaitForExtensionCrash(const std::string& extension_id);
// Wait for the crx installer to be done. Returns true if it has finished
// successfully.
bool WaitForCrxInstallerDone();
// Watch for the given event type from the given source.
// After calling this method, call Wait() to ensure that RunMessageLoop() is
// called appropriately and cleanup is performed.
void Watch(int type, const content::NotificationSource& source);
// After registering one or more event types with Watch(), call
// this method to run the message loop and perform cleanup.
void Wait();
const std::string& last_loaded_extension_id() {
return last_loaded_extension_id_;
}
void set_last_loaded_extension_id(
const std::string& last_loaded_extension_id) {
last_loaded_extension_id_ = last_loaded_extension_id;
}
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// ExtensionRegistryObserver:
void OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) override;
void OnShutdown(ExtensionRegistry* registry) override;
protected:
class NotificationSet : public content::NotificationObserver,
public extensions::ProcessManagerObserver {
public:
NotificationSet();
~NotificationSet() override;
void Add(int type, const content::NotificationSource& source);
void Add(int type);
void AddExtensionFrameUnregistration(extensions::ProcessManager* manager);
void AddWebContentsDestroyed(extensions::ProcessManager* manager);
// Notified any time an Add()ed notification is received.
// The details of the notification are dropped.
base::RepeatingClosureList& closure_list() { return closure_list_; }
private:
class ForwardingWebContentsObserver;
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// extensions::ProcessManagerObserver:
void OnExtensionFrameUnregistered(
const std::string& extension_id,
content::RenderFrameHost* render_frame_host) override;
void WebContentsDestroyed(content::WebContents* web_contents);
content::NotificationRegistrar notification_registrar_;
base::RepeatingClosureList closure_list_;
base::ScopedObservation<extensions::ProcessManager,
extensions::ProcessManagerObserver>
process_manager_observation_{this};
std::map<content::WebContents*,
std::unique_ptr<ForwardingWebContentsObserver>>
web_contents_observers_;
DISALLOW_COPY_AND_ASSIGN(NotificationSet);
};
// Wait for |condition_| to be met. |notification_set| is the set of
// notifications to wait for and to check |condition| when observing. This
// can be NULL if we are instead waiting for a different observer method, like
// OnPageActionsUpdated().
void WaitForCondition(const base::RepeatingCallback<bool(void)>& condition,
NotificationSet* notification_set);
void WaitForNotification(int notification_type);
// Quits the message loop if |condition_| is met.
void MaybeQuit();
content::BrowserContext* context_;
private:
content::NotificationRegistrar registrar_;
std::unique_ptr<content::WindowedNotificationObserver> observer_;
std::string last_loaded_extension_id_;
int crx_installers_done_observed_;
// The condition for which we are waiting. This should be checked in any
// observing methods that could trigger it.
base::RepeatingCallback<bool(void)> condition_;
// The closure to quit the currently-running message loop.
base::OnceClosure quit_closure_;
// Listens to extension loaded notifications.
base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
registry_observation_{this};
DISALLOW_COPY_AND_ASSIGN(ExtensionTestNotificationObserver);
};
} // namespace extensions
#endif // EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_