File: test_extension_registry_observer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 5,064 bytes parent folder | download | duplicates (6)
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
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2014 The Chromium Authors
// 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 <memory>

#include "base/run_loop.h"
#include "extensions/common/extension_id.h"

namespace extensions {

class TestExtensionRegistryObserver::Waiter {
 public:
  Waiter() = default;
  Waiter(const Waiter&) = delete;
  Waiter& operator=(const Waiter&) = delete;

  scoped_refptr<const Extension> Wait() {
    if (!observed_) {
      run_loop_.Run();
    }
    return extension_;
  }

  void OnObserved(const Extension* extension) {
    observed_ = true;
    run_loop_.Quit();
    extension_ = extension;
  }

 private:
  bool observed_ = false;
  base::RunLoop run_loop_{base::RunLoop::Type::kNestableTasksAllowed};
  scoped_refptr<const Extension> extension_;
};

TestExtensionRegistryObserver::TestExtensionRegistryObserver(
    ExtensionRegistry* registry)
    : TestExtensionRegistryObserver(registry, std::string()) {
}

TestExtensionRegistryObserver::TestExtensionRegistryObserver(
    ExtensionRegistry* registry,
    const ExtensionId& extension_id)
    : will_be_installed_waiter_(std::make_unique<Waiter>()),
      installed_waiter_(std::make_unique<Waiter>()),
      uninstalled_waiter_(std::make_unique<Waiter>()),
      uninstallation_denied_waiter_(std::make_unique<Waiter>()),
      loaded_waiter_(std::make_unique<Waiter>()),
      ready_waiter_(std::make_unique<Waiter>()),
      unloaded_waiter_(std::make_unique<Waiter>()),
      extension_id_(extension_id) {
  extension_registry_observation_.Observe(registry);
}

TestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
  return Wait(&uninstalled_waiter_);
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionUninstallationDenied() {
  return Wait(&uninstallation_denied_waiter_);
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
  return Wait(&will_be_installed_waiter_);
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionInstalled() {
  return Wait(&installed_waiter_);
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionLoaded() {
  return Wait(&loaded_waiter_);
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
  return Wait(&unloaded_waiter_);
}

scoped_refptr<const Extension>
TestExtensionRegistryObserver::WaitForExtensionReady() {
  return Wait(&ready_waiter_);
}

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

void TestExtensionRegistryObserver::OnExtensionInstalled(
    content::BrowserContext* browser_context,
    const Extension* extension,
    bool is_update) {
  if (extension_id_.empty() || extension->id() == extension_id_) {
    installed_waiter_->OnObserved(extension);
  }
}

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

void TestExtensionRegistryObserver::OnExtensionUninstallationDenied(
    content::BrowserContext* browser_context,
    const Extension* extension) {
  if (extension_id_.empty() || extension->id() == extension_id_) {
    uninstallation_denied_waiter_->OnObserved(extension);
  }
}

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

void TestExtensionRegistryObserver::OnExtensionReady(
    content::BrowserContext* browser_context,
    const Extension* extension) {
  if (extension_id_.empty() || extension->id() == extension_id_) {
    ready_waiter_->OnObserved(extension);
  }
}

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

scoped_refptr<const Extension> TestExtensionRegistryObserver::Wait(
    std::unique_ptr<Waiter>* waiter) {
  scoped_refptr<const Extension> extension = (*waiter)->Wait();
  // Reset the waiter for future uses.
  // We could have a Waiter::Reset method, but it would reset every field in the
  // class, so let's just reset the pointer.
  *waiter = std::make_unique<Waiter>();
  return extension;
}

}  // namespace extensions