extension_background_page_waiter.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2020 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_BACKGROUND_PAGE_WAITER_H_
  5. #define EXTENSIONS_TEST_EXTENSION_BACKGROUND_PAGE_WAITER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "extensions/common/extension.h"
  8. namespace content {
  9. class BrowserContext;
  10. }
  11. namespace extensions {
  12. // A class to wait for an extension's background to reach a certain state.
  13. // NOTE: See also ExtensionHostTestHelper.
  14. // * Use this class when you need to wait for a _state_ - e.g., for the
  15. // extension's background context to be initialized - and you don't
  16. // necessarily care when it happened. The state may already be active.
  17. // * Use ExtensionHostTestHelper when you need to wait for an expected
  18. // _event_ - such as host destruction.
  19. // See also
  20. // https://chromium.googlesource.com/chromium/src/+/main/docs/patterns/synchronous-runloop.md#events-vs-states
  21. // TODO(devlin): Rename this to ExtensionBackgroundContextWaiter? It supports
  22. // service workers in addition to background (and event) pages.
  23. class ExtensionBackgroundPageWaiter {
  24. public:
  25. ExtensionBackgroundPageWaiter(content::BrowserContext* browser_context,
  26. const Extension& extension);
  27. ExtensionBackgroundPageWaiter(const ExtensionBackgroundPageWaiter& other) =
  28. delete;
  29. ExtensionBackgroundPageWaiter& operator=(
  30. const ExtensionBackgroundPageWaiter& other) = delete;
  31. ~ExtensionBackgroundPageWaiter();
  32. // Returns true if this class can wait for the specified `extension`. If
  33. // false, populates `reason` with why it can't (e.g., the extension has
  34. // no background context).
  35. static bool CanWaitFor(const Extension& extension, std::string& reason);
  36. // Waits for the extension's background context to have initialized at some
  37. // point. This doesn't require the background context to be currently open,
  38. // in the case of lazy background contexts.
  39. // NOTE: The only way to determine if a lazy background context was previously
  40. // initialized is by checking for registered events. If a background context
  41. // was fully initialized and shut down without registering any event
  42. // listeners, this will spin indefinitely.
  43. void WaitForBackgroundInitialized();
  44. // Waits for the extension background context to currently be open and active.
  45. // Note: this does not (yet) support worker-based extensions.
  46. void WaitForBackgroundOpen();
  47. // Waits for the extension background context to be closed.
  48. // Note: this does not (yet) support worker-based extensions.
  49. void WaitForBackgroundClosed();
  50. private:
  51. void WaitForBackgroundWorkerInitialized();
  52. void WaitForBackgroundPageInitialized();
  53. const raw_ptr<content::BrowserContext> browser_context_;
  54. scoped_refptr<const Extension> extension_;
  55. };
  56. } // namespace extensions
  57. #endif // EXTENSIONS_TEST_EXTENSION_BACKGROUND_PAGE_WAITER_H_