lazy_background_task_queue.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2013 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_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_
  5. #define EXTENSIONS_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_
  6. #include <stddef.h>
  7. #include <algorithm>
  8. #include <map>
  9. #include <string>
  10. #include "base/compiler_specific.h"
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/scoped_observation.h"
  14. #include "components/keyed_service/core/keyed_service.h"
  15. #include "extensions/browser/extension_host_registry.h"
  16. #include "extensions/browser/extension_registry.h"
  17. #include "extensions/browser/extension_registry_observer.h"
  18. #include "extensions/browser/lazy_context_id.h"
  19. #include "extensions/browser/lazy_context_task_queue.h"
  20. #include "extensions/common/extension_id.h"
  21. namespace content {
  22. class BrowserContext;
  23. }
  24. namespace extensions {
  25. class Extension;
  26. class ExtensionHost;
  27. // This class maintains a queue of tasks that should execute when an
  28. // extension's lazy background page is loaded. It is also in charge of loading
  29. // the page when the first task is queued.
  30. //
  31. // It is the consumer's responsibility to use this class when appropriate, i.e.
  32. // only with extensions that have not-yet-loaded lazy background pages.
  33. class LazyBackgroundTaskQueue : public KeyedService,
  34. public LazyContextTaskQueue,
  35. public ExtensionRegistryObserver,
  36. public ExtensionHostRegistry::Observer {
  37. public:
  38. explicit LazyBackgroundTaskQueue(content::BrowserContext* browser_context);
  39. LazyBackgroundTaskQueue(const LazyBackgroundTaskQueue&) = delete;
  40. LazyBackgroundTaskQueue& operator=(const LazyBackgroundTaskQueue&) = delete;
  41. ~LazyBackgroundTaskQueue() override;
  42. // Convenience method to return the LazyBackgroundTaskQueue for a given
  43. // |context|.
  44. static LazyBackgroundTaskQueue* Get(content::BrowserContext* context);
  45. // Returns true if the task should be added to the queue (that is, if the
  46. // extension has a lazy background page that isn't ready yet). If the
  47. // extension has a lazy background page that is being suspended this method
  48. // cancels that suspension.
  49. bool ShouldEnqueueTask(content::BrowserContext* context,
  50. const Extension* extension) override;
  51. // Adds a task to the queue for a given extension. If this is the first
  52. // task added for the extension, its lazy background page will be loaded.
  53. // The task will be called either when the page is loaded, or when the
  54. // page fails to load for some reason (e.g. a crash or browser
  55. // shutdown). In the latter case, |task| will be called with an empty
  56. // std::unique_ptr<ContextItem> parameter.
  57. void AddPendingTask(const LazyContextId& context_id,
  58. PendingTask task) override;
  59. private:
  60. FRIEND_TEST_ALL_PREFIXES(LazyBackgroundTaskQueueTest, AddPendingTask);
  61. FRIEND_TEST_ALL_PREFIXES(LazyBackgroundTaskQueueTest, ProcessPendingTasks);
  62. FRIEND_TEST_ALL_PREFIXES(LazyBackgroundTaskQueueTest,
  63. CreateLazyBackgroundPageOnExtensionLoaded);
  64. // A map between a LazyContextId and the queue of tasks pending the load of
  65. // its background page.
  66. using PendingTasksKey = LazyContextId;
  67. using PendingTasksList = std::vector<PendingTask>;
  68. using PendingTasksMap =
  69. std::map<PendingTasksKey, std::unique_ptr<PendingTasksList>>;
  70. // ExtensionHostRegistry::Observer:
  71. void OnExtensionHostCompletedFirstLoad(
  72. content::BrowserContext* browser_context,
  73. ExtensionHost* host) override;
  74. void OnExtensionHostDestroyed(content::BrowserContext* browser_context,
  75. ExtensionHost* host) override;
  76. // ExtensionRegistryObserver interface.
  77. void OnExtensionLoaded(content::BrowserContext* browser_context,
  78. const Extension* extension) override;
  79. void OnExtensionUnloaded(content::BrowserContext* browser_context,
  80. const Extension* extension,
  81. UnloadedExtensionReason reason) override;
  82. // If there are pending tasks for |extension| in |browser_context|, try to
  83. // create the background host. If the background host cannot be created, the
  84. // pending tasks are invoked with nullptr.
  85. void CreateLazyBackgroundHostOnExtensionLoaded(
  86. content::BrowserContext* browser_context,
  87. const Extension* extension);
  88. // Called when a lazy background page has finished loading, or has failed to
  89. // load (host is nullptr in that case). All enqueued tasks are run in order.
  90. void ProcessPendingTasks(
  91. ExtensionHost* host,
  92. content::BrowserContext* context,
  93. const Extension* extension);
  94. // Notifies queued tasks that a lazy background page has failed to load.
  95. void NotifyTasksExtensionFailedToLoad(
  96. content::BrowserContext* browser_context,
  97. const Extension* extension);
  98. raw_ptr<content::BrowserContext> browser_context_;
  99. PendingTasksMap pending_tasks_;
  100. base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
  101. extension_registry_observation_{this};
  102. base::ScopedObservation<ExtensionHostRegistry,
  103. ExtensionHostRegistry::Observer>
  104. extension_host_registry_observation_{this};
  105. };
  106. } // namespace extensions
  107. #endif // EXTENSIONS_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_