1 // Copyright 2014 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.
5 #include "extensions/browser/test_extension_registry_observer.h"
7 #include "base/run_loop.h"
8 #include "extensions/browser/extension_registry.h"
10 namespace extensions
{
12 class TestExtensionRegistryObserver::Waiter
{
14 Waiter() : observed_(false), extension_(nullptr) {}
21 void OnObserved(const Extension
* extension
) {
24 extension_
= extension
;
27 const Extension
* extension() const { return extension_
; }
31 base::RunLoop run_loop_
;
32 const Extension
* extension_
;
34 DISALLOW_COPY_AND_ASSIGN(Waiter
);
37 TestExtensionRegistryObserver::TestExtensionRegistryObserver(
38 ExtensionRegistry
* registry
)
39 : TestExtensionRegistryObserver(registry
, std::string()) {
42 TestExtensionRegistryObserver::TestExtensionRegistryObserver(
43 ExtensionRegistry
* registry
,
44 const std::string
& extension_id
)
45 : will_be_installed_waiter_(new Waiter()),
46 uninstalled_waiter_(new Waiter()),
47 loaded_waiter_(new Waiter()),
48 unloaded_waiter_(new Waiter()),
49 extension_registry_observer_(this),
50 extension_id_(extension_id
) {
51 extension_registry_observer_
.Add(registry
);
54 TestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
57 const Extension
* TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
58 return Wait(&uninstalled_waiter_
);
62 TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
63 return Wait(&will_be_installed_waiter_
);
66 const Extension
* TestExtensionRegistryObserver::WaitForExtensionLoaded() {
67 return Wait(&loaded_waiter_
);
70 const Extension
* TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
71 return Wait(&unloaded_waiter_
);
74 void TestExtensionRegistryObserver::OnExtensionWillBeInstalled(
75 content::BrowserContext
* browser_context
,
76 const Extension
* extension
,
79 const std::string
& old_name
) {
80 if (extension_id_
.empty() || extension
->id() == extension_id_
)
81 will_be_installed_waiter_
->OnObserved(extension
);
84 void TestExtensionRegistryObserver::OnExtensionUninstalled(
85 content::BrowserContext
* browser_context
,
86 const Extension
* extension
,
87 extensions::UninstallReason reason
) {
88 if (extension_id_
.empty() || extension
->id() == extension_id_
)
89 uninstalled_waiter_
->OnObserved(extension
);
92 void TestExtensionRegistryObserver::OnExtensionLoaded(
93 content::BrowserContext
* browser_context
,
94 const Extension
* extension
) {
95 if (extension_id_
.empty() || extension
->id() == extension_id_
)
96 loaded_waiter_
->OnObserved(extension
);
99 void TestExtensionRegistryObserver::OnExtensionUnloaded(
100 content::BrowserContext
* browser_context
,
101 const Extension
* extension
,
102 UnloadedExtensionInfo::Reason reason
) {
103 if (extension_id_
.empty() || extension
->id() == extension_id_
)
104 unloaded_waiter_
->OnObserved(extension
);
107 const Extension
* TestExtensionRegistryObserver::Wait(
108 scoped_ptr
<Waiter
>* waiter
) {
109 waiter
->get()->Wait();
110 const Extension
* extension
= waiter
->get()->extension();
111 // Reset the waiter for future uses.
112 // We could have a Waiter::Reset method, but it would reset every field in the
113 // class, so let's just reset the pointer.
114 waiter
->reset(new Waiter());
118 } // namespace extensions