blob: 4d87e9455e2a5d18f097c89c5ff691077038c5e2 [file] [log] [blame]
[email protected]eccbbb62014-06-27 21:39:451// 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.
4
5#include "extensions/browser/test_extension_registry_observer.h"
6
rdevlin.cronin267be092014-10-07 21:18:537#include "base/run_loop.h"
[email protected]eccbbb62014-06-27 21:39:458#include "extensions/browser/extension_registry.h"
9
10namespace extensions {
11
12class TestExtensionRegistryObserver::Waiter {
13 public:
rdevlin.cronin267be092014-10-07 21:18:5314 explicit Waiter(const std::string& extension_id) : observed_(false) {}
[email protected]eccbbb62014-06-27 21:39:4515
16 void Wait() {
17 if (observed_)
18 return;
rdevlin.cronin267be092014-10-07 21:18:5319 run_loop_.Run();
[email protected]eccbbb62014-06-27 21:39:4520 }
21
22 void OnObserved() {
23 observed_ = true;
rdevlin.cronin267be092014-10-07 21:18:5324 run_loop_.Quit();
[email protected]eccbbb62014-06-27 21:39:4525 }
26
27 private:
28 bool observed_;
rdevlin.cronin267be092014-10-07 21:18:5329 base::RunLoop run_loop_;
[email protected]eccbbb62014-06-27 21:39:4530
31 DISALLOW_COPY_AND_ASSIGN(Waiter);
32};
33
34TestExtensionRegistryObserver::TestExtensionRegistryObserver(
35 ExtensionRegistry* registry,
36 const std::string& extension_id)
37 : will_be_installed_waiter_(new Waiter(extension_id)),
38 uninstalled_waiter_(new Waiter(extension_id)),
39 loaded_waiter_(new Waiter(extension_id)),
40 unloaded_waiter_(new Waiter(extension_id)),
41 extension_registry_observer_(this),
42 extension_id_(extension_id) {
43 extension_registry_observer_.Add(registry);
44}
45
46TestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
47}
48
49void TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
50 uninstalled_waiter_->Wait();
51}
52
53void TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
54 will_be_installed_waiter_->Wait();
55}
56
57void TestExtensionRegistryObserver::WaitForExtensionLoaded() {
58 loaded_waiter_->Wait();
59}
60
61void TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
62 unloaded_waiter_->Wait();
63}
64
65void TestExtensionRegistryObserver::OnExtensionWillBeInstalled(
66 content::BrowserContext* browser_context,
67 const Extension* extension,
68 bool is_update,
69 bool from_ephemeral,
70 const std::string& old_name) {
71 if (extension->id() == extension_id_)
72 will_be_installed_waiter_->OnObserved();
73}
74
75void TestExtensionRegistryObserver::OnExtensionUninstalled(
76 content::BrowserContext* browser_context,
[email protected]e43c61f2014-07-20 21:46:3477 const Extension* extension,
78 extensions::UninstallReason reason) {
[email protected]eccbbb62014-06-27 21:39:4579 if (extension->id() == extension_id_)
80 uninstalled_waiter_->OnObserved();
81}
82
83void TestExtensionRegistryObserver::OnExtensionLoaded(
84 content::BrowserContext* browser_context,
85 const Extension* extension) {
86 if (extension->id() == extension_id_)
87 loaded_waiter_->OnObserved();
88}
89
90void TestExtensionRegistryObserver::OnExtensionUnloaded(
91 content::BrowserContext* browser_context,
92 const Extension* extension,
93 UnloadedExtensionInfo::Reason reason) {
94 if (extension->id() == extension_id_)
95 unloaded_waiter_->OnObserved();
96}
97
98} // namespace extensions