extension_test_notification_observer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
  5. #define EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/callback_list.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/scoped_observation.h"
  13. #include "content/public/browser/notification_observer.h"
  14. #include "content/public/browser/notification_registrar.h"
  15. #include "extensions/browser/extension_registry.h"
  16. #include "extensions/browser/extension_registry_observer.h"
  17. #include "extensions/browser/process_manager.h"
  18. #include "extensions/browser/process_manager_observer.h"
  19. namespace content {
  20. class BrowserContext;
  21. class NotificationDetails;
  22. class WebContents;
  23. class WindowedNotificationObserver;
  24. }
  25. namespace extensions {
  26. // Test helper class for observing extension-related events.
  27. class ExtensionTestNotificationObserver : public content::NotificationObserver,
  28. ExtensionRegistryObserver {
  29. public:
  30. explicit ExtensionTestNotificationObserver(content::BrowserContext* context);
  31. ExtensionTestNotificationObserver(const ExtensionTestNotificationObserver&) =
  32. delete;
  33. ExtensionTestNotificationObserver& operator=(
  34. const ExtensionTestNotificationObserver&) = delete;
  35. ~ExtensionTestNotificationObserver() override;
  36. // Wait for the crx installer to be done. Returns true if it has finished
  37. // successfully.
  38. bool WaitForCrxInstallerDone();
  39. // Watch for the given event type from the given source.
  40. // After calling this method, call Wait() to ensure that RunMessageLoop() is
  41. // called appropriately and cleanup is performed.
  42. void Watch(int type, const content::NotificationSource& source);
  43. // After registering one or more event types with Watch(), call
  44. // this method to run the message loop and perform cleanup.
  45. void Wait();
  46. const std::string& last_loaded_extension_id() {
  47. return last_loaded_extension_id_;
  48. }
  49. void set_last_loaded_extension_id(
  50. const std::string& last_loaded_extension_id) {
  51. last_loaded_extension_id_ = last_loaded_extension_id;
  52. }
  53. // content::NotificationObserver:
  54. void Observe(int type,
  55. const content::NotificationSource& source,
  56. const content::NotificationDetails& details) override;
  57. // ExtensionRegistryObserver:
  58. void OnExtensionLoaded(content::BrowserContext* browser_context,
  59. const Extension* extension) override;
  60. void OnShutdown(ExtensionRegistry* registry) override;
  61. protected:
  62. class NotificationSet : public content::NotificationObserver,
  63. public extensions::ProcessManagerObserver {
  64. public:
  65. NotificationSet();
  66. NotificationSet(const NotificationSet&) = delete;
  67. NotificationSet& operator=(const NotificationSet&) = delete;
  68. ~NotificationSet() override;
  69. void Add(int type, const content::NotificationSource& source);
  70. void Add(int type);
  71. void AddExtensionFrameUnregistration(extensions::ProcessManager* manager);
  72. void AddWebContentsDestroyed(extensions::ProcessManager* manager);
  73. // Notified any time an Add()ed notification is received.
  74. // The details of the notification are dropped.
  75. base::RepeatingClosureList& closure_list() { return closure_list_; }
  76. private:
  77. class ForwardingWebContentsObserver;
  78. // content::NotificationObserver:
  79. void Observe(int type,
  80. const content::NotificationSource& source,
  81. const content::NotificationDetails& details) override;
  82. // extensions::ProcessManagerObserver:
  83. void OnExtensionFrameUnregistered(
  84. const std::string& extension_id,
  85. content::RenderFrameHost* render_frame_host) override;
  86. void WebContentsDestroyed(content::WebContents* web_contents);
  87. content::NotificationRegistrar notification_registrar_;
  88. base::RepeatingClosureList closure_list_;
  89. base::ScopedObservation<extensions::ProcessManager,
  90. extensions::ProcessManagerObserver>
  91. process_manager_observation_{this};
  92. std::map<content::WebContents*,
  93. std::unique_ptr<ForwardingWebContentsObserver>>
  94. web_contents_observers_;
  95. };
  96. // Wait for |condition_| to be met. |notification_set| is the set of
  97. // notifications to wait for and to check |condition| when observing. This
  98. // can be NULL if we are instead waiting for a different observer method, like
  99. // OnPageActionsUpdated().
  100. void WaitForCondition(const base::RepeatingCallback<bool(void)>& condition,
  101. NotificationSet* notification_set);
  102. void WaitForNotification(int notification_type);
  103. // Quits the message loop if |condition_| is met.
  104. void MaybeQuit();
  105. raw_ptr<content::BrowserContext> context_;
  106. private:
  107. content::NotificationRegistrar registrar_;
  108. std::unique_ptr<content::WindowedNotificationObserver> observer_;
  109. std::string last_loaded_extension_id_;
  110. int crx_installers_done_observed_;
  111. // The condition for which we are waiting. This should be checked in any
  112. // observing methods that could trigger it.
  113. base::RepeatingCallback<bool(void)> condition_;
  114. // The closure to quit the currently-running message loop.
  115. base::OnceClosure quit_closure_;
  116. // Listens to extension loaded notifications.
  117. base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
  118. registry_observation_{this};
  119. };
  120. } // namespace extensions
  121. #endif // EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_