test_extension_registry_observer.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.
  4. #include "extensions/browser/test_extension_registry_observer.h"
  5. #include <memory>
  6. #include "base/run_loop.h"
  7. namespace extensions {
  8. class TestExtensionRegistryObserver::Waiter {
  9. public:
  10. Waiter() : observed_(false), extension_(nullptr) {}
  11. Waiter(const Waiter&) = delete;
  12. Waiter& operator=(const Waiter&) = delete;
  13. scoped_refptr<const Extension> Wait() {
  14. if (!observed_)
  15. run_loop_.Run();
  16. return extension_;
  17. }
  18. void OnObserved(const Extension* extension) {
  19. observed_ = true;
  20. run_loop_.Quit();
  21. extension_ = extension;
  22. }
  23. private:
  24. bool observed_;
  25. base::RunLoop run_loop_;
  26. scoped_refptr<const Extension> extension_;
  27. };
  28. TestExtensionRegistryObserver::TestExtensionRegistryObserver(
  29. ExtensionRegistry* registry)
  30. : TestExtensionRegistryObserver(registry, std::string()) {
  31. }
  32. TestExtensionRegistryObserver::TestExtensionRegistryObserver(
  33. ExtensionRegistry* registry,
  34. const std::string& extension_id)
  35. : will_be_installed_waiter_(std::make_unique<Waiter>()),
  36. installed_waiter_(std::make_unique<Waiter>()),
  37. uninstalled_waiter_(std::make_unique<Waiter>()),
  38. uninstallation_denied_waiter_(std::make_unique<Waiter>()),
  39. loaded_waiter_(std::make_unique<Waiter>()),
  40. ready_waiter_(std::make_unique<Waiter>()),
  41. unloaded_waiter_(std::make_unique<Waiter>()),
  42. extension_id_(extension_id) {
  43. extension_registry_observation_.Observe(registry);
  44. }
  45. TestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
  46. }
  47. scoped_refptr<const Extension>
  48. TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
  49. return Wait(&uninstalled_waiter_);
  50. }
  51. scoped_refptr<const Extension>
  52. TestExtensionRegistryObserver::WaitForExtensionUninstallationDenied() {
  53. return Wait(&uninstallation_denied_waiter_);
  54. }
  55. scoped_refptr<const Extension>
  56. TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
  57. return Wait(&will_be_installed_waiter_);
  58. }
  59. scoped_refptr<const Extension>
  60. TestExtensionRegistryObserver::WaitForExtensionInstalled() {
  61. return Wait(&installed_waiter_);
  62. }
  63. scoped_refptr<const Extension>
  64. TestExtensionRegistryObserver::WaitForExtensionLoaded() {
  65. return Wait(&loaded_waiter_);
  66. }
  67. scoped_refptr<const Extension>
  68. TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
  69. return Wait(&unloaded_waiter_);
  70. }
  71. scoped_refptr<const Extension>
  72. TestExtensionRegistryObserver::WaitForExtensionReady() {
  73. return Wait(&ready_waiter_);
  74. }
  75. void TestExtensionRegistryObserver::OnExtensionWillBeInstalled(
  76. content::BrowserContext* browser_context,
  77. const Extension* extension,
  78. bool is_update,
  79. const std::string& old_name) {
  80. if (extension_id_.empty() || extension->id() == extension_id_)
  81. will_be_installed_waiter_->OnObserved(extension);
  82. }
  83. void TestExtensionRegistryObserver::OnExtensionInstalled(
  84. content::BrowserContext* browser_context,
  85. const Extension* extension,
  86. bool is_update) {
  87. if (extension_id_.empty() || extension->id() == extension_id_)
  88. installed_waiter_->OnObserved(extension);
  89. }
  90. void TestExtensionRegistryObserver::OnExtensionUninstalled(
  91. content::BrowserContext* browser_context,
  92. const Extension* extension,
  93. extensions::UninstallReason reason) {
  94. if (extension_id_.empty() || extension->id() == extension_id_)
  95. uninstalled_waiter_->OnObserved(extension);
  96. }
  97. void TestExtensionRegistryObserver::OnExtensionUninstallationDenied(
  98. content::BrowserContext* browser_context,
  99. const Extension* extension) {
  100. if (extension_id_.empty() || extension->id() == extension_id_)
  101. uninstallation_denied_waiter_->OnObserved(extension);
  102. }
  103. void TestExtensionRegistryObserver::OnExtensionLoaded(
  104. content::BrowserContext* browser_context,
  105. const Extension* extension) {
  106. if (extension_id_.empty() || extension->id() == extension_id_)
  107. loaded_waiter_->OnObserved(extension);
  108. }
  109. void TestExtensionRegistryObserver::OnExtensionReady(
  110. content::BrowserContext* browser_context,
  111. const Extension* extension) {
  112. if (extension_id_.empty() || extension->id() == extension_id_)
  113. ready_waiter_->OnObserved(extension);
  114. }
  115. void TestExtensionRegistryObserver::OnExtensionUnloaded(
  116. content::BrowserContext* browser_context,
  117. const Extension* extension,
  118. UnloadedExtensionReason reason) {
  119. if (extension_id_.empty() || extension->id() == extension_id_)
  120. unloaded_waiter_->OnObserved(extension);
  121. }
  122. scoped_refptr<const Extension> TestExtensionRegistryObserver::Wait(
  123. std::unique_ptr<Waiter>* waiter) {
  124. scoped_refptr<const Extension> extension = (*waiter)->Wait();
  125. // Reset the waiter for future uses.
  126. // We could have a Waiter::Reset method, but it would reset every field in the
  127. // class, so let's just reset the pointer.
  128. *waiter = std::make_unique<Waiter>();
  129. return extension;
  130. }
  131. } // namespace extensions