extension_test_notification_observer.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2016 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/test/extension_test_notification_observer.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include <memory>
  7. #include "base/bind.h"
  8. #include "base/containers/contains.h"
  9. #include "content/public/browser/browser_context.h"
  10. #include "content/public/browser/notification_details.h"
  11. #include "content/public/browser/notification_registrar.h"
  12. #include "content/public/browser/notification_service.h"
  13. #include "content/public/browser/render_frame_host.h"
  14. #include "content/public/browser/web_contents.h"
  15. #include "content/public/browser/web_contents_observer.h"
  16. #include "content/public/test/test_utils.h"
  17. #include "extensions/browser/notification_types.h"
  18. #include "extensions/common/extension.h"
  19. namespace extensions {
  20. // A callback that returns true if the condition has been met and takes no
  21. // arguments.
  22. using ConditionCallback = base::RepeatingCallback<bool(void)>;
  23. ////////////////////////////////////////////////////////////////////////////////
  24. // NotificationSet::ForwardingWebContentsObserver
  25. class ExtensionTestNotificationObserver::NotificationSet::
  26. ForwardingWebContentsObserver : public content::WebContentsObserver {
  27. public:
  28. ForwardingWebContentsObserver(
  29. content::WebContents* contents,
  30. ExtensionTestNotificationObserver::NotificationSet* owner)
  31. : WebContentsObserver(contents), owner_(owner) {}
  32. private:
  33. // content::WebContentsObserver
  34. void WebContentsDestroyed() override {
  35. // Do not add code after this line, deletes `this`.
  36. owner_->WebContentsDestroyed(web_contents());
  37. }
  38. raw_ptr<ExtensionTestNotificationObserver::NotificationSet> owner_;
  39. };
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // ExtensionTestNotificationObserver::NotificationSet
  42. ExtensionTestNotificationObserver::NotificationSet::NotificationSet() = default;
  43. ExtensionTestNotificationObserver::NotificationSet::~NotificationSet() =
  44. default;
  45. void ExtensionTestNotificationObserver::NotificationSet::Add(
  46. int type,
  47. const content::NotificationSource& source) {
  48. notification_registrar_.Add(this, type, source);
  49. }
  50. void ExtensionTestNotificationObserver::NotificationSet::Add(int type) {
  51. Add(type, content::NotificationService::AllSources());
  52. }
  53. void ExtensionTestNotificationObserver::NotificationSet::
  54. AddExtensionFrameUnregistration(ProcessManager* manager) {
  55. process_manager_observation_.Observe(manager);
  56. }
  57. void ExtensionTestNotificationObserver::NotificationSet::Observe(
  58. int type,
  59. const content::NotificationSource& source,
  60. const content::NotificationDetails& details) {
  61. closure_list_.Notify();
  62. }
  63. void ExtensionTestNotificationObserver::NotificationSet::
  64. AddWebContentsDestroyed(extensions::ProcessManager* manager) {
  65. for (content::RenderFrameHost* render_frame_host : manager->GetAllFrames()) {
  66. content::WebContents* contents =
  67. content::WebContents::FromRenderFrameHost(render_frame_host);
  68. if (!base::Contains(web_contents_observers_, contents)) {
  69. web_contents_observers_[contents] =
  70. std::make_unique<ForwardingWebContentsObserver>(contents, this);
  71. }
  72. }
  73. }
  74. void ExtensionTestNotificationObserver::NotificationSet::
  75. OnExtensionFrameUnregistered(const std::string& extension_id,
  76. content::RenderFrameHost* render_frame_host) {
  77. closure_list_.Notify();
  78. }
  79. void ExtensionTestNotificationObserver::NotificationSet::WebContentsDestroyed(
  80. content::WebContents* web_contents) {
  81. web_contents_observers_.erase(web_contents);
  82. closure_list_.Notify();
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. // ExtensionTestNotificationObserver
  86. ExtensionTestNotificationObserver::ExtensionTestNotificationObserver(
  87. content::BrowserContext* context)
  88. : context_(context),
  89. crx_installers_done_observed_(0) {
  90. if (context_)
  91. registry_observation_.Observe(ExtensionRegistry::Get(context_));
  92. }
  93. ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {}
  94. void ExtensionTestNotificationObserver::WaitForNotification(
  95. int notification_type) {
  96. // TODO(bauerb): Using a WindowedNotificationObserver like this can break
  97. // easily, if the notification we're waiting for is sent before this method.
  98. // Change it so that the WindowedNotificationObserver is constructed earlier.
  99. content::NotificationRegistrar registrar;
  100. registrar.Add(this, notification_type,
  101. content::NotificationService::AllSources());
  102. content::WindowedNotificationObserver(
  103. notification_type, content::NotificationService::AllSources())
  104. .Wait();
  105. }
  106. bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() {
  107. int before = crx_installers_done_observed_;
  108. WaitForNotification(NOTIFICATION_CRX_INSTALLER_DONE);
  109. return crx_installers_done_observed_ == before + 1 &&
  110. !last_loaded_extension_id_.empty();
  111. }
  112. void ExtensionTestNotificationObserver::Watch(
  113. int type,
  114. const content::NotificationSource& source) {
  115. CHECK(!observer_);
  116. observer_ =
  117. std::make_unique<content::WindowedNotificationObserver>(type, source);
  118. registrar_.Add(this, type, source);
  119. }
  120. void ExtensionTestNotificationObserver::Wait() {
  121. observer_->Wait();
  122. registrar_.RemoveAll();
  123. observer_.reset();
  124. }
  125. void ExtensionTestNotificationObserver::Observe(
  126. int type,
  127. const content::NotificationSource& source,
  128. const content::NotificationDetails& details) {
  129. switch (type) {
  130. case NOTIFICATION_CRX_INSTALLER_DONE:
  131. VLOG(1) << "Got CRX_INSTALLER_DONE notification.";
  132. {
  133. const Extension* extension =
  134. content::Details<const Extension>(details).ptr();
  135. if (extension)
  136. last_loaded_extension_id_ = extension->id();
  137. else
  138. last_loaded_extension_id_.clear();
  139. }
  140. ++crx_installers_done_observed_;
  141. break;
  142. default:
  143. NOTREACHED();
  144. break;
  145. }
  146. }
  147. void ExtensionTestNotificationObserver::OnExtensionLoaded(
  148. content::BrowserContext* browser_context,
  149. const Extension* extension) {
  150. last_loaded_extension_id_ = extension->id();
  151. VLOG(1) << "Got EXTENSION_LOADED notification.";
  152. }
  153. void ExtensionTestNotificationObserver::OnShutdown(
  154. ExtensionRegistry* registry) {
  155. registry_observation_.Reset();
  156. }
  157. void ExtensionTestNotificationObserver::WaitForCondition(
  158. const ConditionCallback& condition,
  159. NotificationSet* notification_set) {
  160. if (condition.Run())
  161. return;
  162. condition_ = condition;
  163. base::RunLoop run_loop;
  164. quit_closure_ = run_loop.QuitClosure();
  165. base::CallbackListSubscription subscription;
  166. if (notification_set) {
  167. subscription = notification_set->closure_list().Add(base::BindRepeating(
  168. &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this)));
  169. }
  170. run_loop.Run();
  171. condition_.Reset();
  172. quit_closure_.Reset();
  173. }
  174. void ExtensionTestNotificationObserver::MaybeQuit() {
  175. // We can be called synchronously from any of the events being observed,
  176. // so return immediately if the closure has already been run.
  177. if (quit_closure_.is_null())
  178. return;
  179. if (condition_.Run())
  180. std::move(quit_closure_).Run();
  181. }
  182. } // namespace extensions