summaryrefslogtreecommitdiffstats
path: root/extensions/browser/test_extension_registry_observer.cc
blob: 4d87e9455e2a5d18f097c89c5ff691077038c5e2 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2014 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.

#include "extensions/browser/test_extension_registry_observer.h"

#include "base/run_loop.h"
#include "extensions/browser/extension_registry.h"

namespace extensions {

class TestExtensionRegistryObserver::Waiter {
 public:
  explicit Waiter(const std::string& extension_id) : observed_(false) {}

  void Wait() {
    if (observed_)
      return;
    run_loop_.Run();
  }

  void OnObserved() {
    observed_ = true;
    run_loop_.Quit();
  }

 private:
  bool observed_;
  base::RunLoop run_loop_;

  DISALLOW_COPY_AND_ASSIGN(Waiter);
};

TestExtensionRegistryObserver::TestExtensionRegistryObserver(
    ExtensionRegistry* registry,
    const std::string& extension_id)
    : will_be_installed_waiter_(new Waiter(extension_id)),
      uninstalled_waiter_(new Waiter(extension_id)),
      loaded_waiter_(new Waiter(extension_id)),
      unloaded_waiter_(new Waiter(extension_id)),
      extension_registry_observer_(this),
      extension_id_(extension_id) {
  extension_registry_observer_.Add(registry);
}

TestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
}

void TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
  uninstalled_waiter_->Wait();
}

void TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
  will_be_installed_waiter_->Wait();
}

void TestExtensionRegistryObserver::WaitForExtensionLoaded() {
  loaded_waiter_->Wait();
}

void TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
  unloaded_waiter_->Wait();
}

void TestExtensionRegistryObserver::OnExtensionWillBeInstalled(
    content::BrowserContext* browser_context,
    const Extension* extension,
    bool is_update,
    bool from_ephemeral,
    const std::string& old_name) {
  if (extension->id() == extension_id_)
    will_be_installed_waiter_->OnObserved();
}

void TestExtensionRegistryObserver::OnExtensionUninstalled(
    content::BrowserContext* browser_context,
    const Extension* extension,
    extensions::UninstallReason reason) {
  if (extension->id() == extension_id_)
    uninstalled_waiter_->OnObserved();
}

void TestExtensionRegistryObserver::OnExtensionLoaded(
    content::BrowserContext* browser_context,
    const Extension* extension) {
  if (extension->id() == extension_id_)
    loaded_waiter_->OnObserved();
}

void TestExtensionRegistryObserver::OnExtensionUnloaded(
    content::BrowserContext* browser_context,
    const Extension* extension,
    UnloadedExtensionInfo::Reason reason) {
  if (extension->id() == extension_id_)
    unloaded_waiter_->OnObserved();
}

}  // namespace extensions