service_worker_context_adapter.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 COMPONENTS_PERFORMANCE_MANAGER_SERVICE_WORKER_CONTEXT_ADAPTER_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_SERVICE_WORKER_CONTEXT_ADAPTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/check_op.h"
  9. #include "base/containers/flat_map.h"
  10. #include "base/containers/flat_set.h"
  11. #include "base/observer_list.h"
  12. #include "base/scoped_observation.h"
  13. #include "content/public/browser/service_worker_context.h"
  14. #include "content/public/browser/service_worker_context_observer.h"
  15. namespace blink {
  16. class StorageKey;
  17. } // namespace blink
  18. namespace performance_manager {
  19. // This class adapts an existing ServiceWorkerContext to ensure that the
  20. // OnVersionStoppedRunning() notifications are sent as soon as the render
  21. // process of a running service worker exits.
  22. //
  23. // It implements ServiceWorkerContext so it can be used interchangeably where a
  24. // ServiceWorkerContext* is needed, and it also observes the underlying context
  25. // so that it can receive the original notifications and control when they are
  26. // sent to the observers.
  27. //
  28. // Lives on the UI thread. Must outlive |underlying_context|.
  29. //
  30. // Note: This is a temporary class that can be removed when the representation
  31. // of a worker in the content/ layer (ServiceWorkerVersion) is moved to
  32. // the UI thread. At that point, it'll be able to observe its associated
  33. // RenderProcessHost itself. See https://crbug.com/824858.
  34. class ServiceWorkerContextAdapter
  35. : public content::ServiceWorkerContext,
  36. public content::ServiceWorkerContextObserver {
  37. public:
  38. explicit ServiceWorkerContextAdapter(
  39. content::ServiceWorkerContext* underlying_context);
  40. ~ServiceWorkerContextAdapter() override;
  41. // content::ServiceWorkerContext:
  42. // Note that this is a minimal implementation for the use case of the
  43. // PerformanceManager. Only AddObserver/RemoveObserver are implemented.
  44. void AddObserver(content::ServiceWorkerContextObserver* observer) override;
  45. void RemoveObserver(content::ServiceWorkerContextObserver* observer) override;
  46. void RegisterServiceWorker(
  47. const GURL& script_url,
  48. const blink::StorageKey& key,
  49. const blink::mojom::ServiceWorkerRegistrationOptions& options,
  50. StatusCodeCallback callback) override;
  51. void UnregisterServiceWorker(const GURL& scope,
  52. const blink::StorageKey& key,
  53. ResultCallback callback) override;
  54. content::ServiceWorkerExternalRequestResult StartingExternalRequest(
  55. int64_t service_worker_version_id,
  56. content::ServiceWorkerExternalRequestTimeoutType timeout_type,
  57. const std::string& request_uuid) override;
  58. content::ServiceWorkerExternalRequestResult FinishedExternalRequest(
  59. int64_t service_worker_version_id,
  60. const std::string& request_uuid) override;
  61. size_t CountExternalRequestsForTest(const blink::StorageKey& key) override;
  62. bool ExecuteScriptForTest(
  63. const std::string& script,
  64. int64_t service_worker_version_id,
  65. content::ServiceWorkerScriptExecutionCallback callback) override;
  66. bool MaybeHasRegistrationForStorageKey(const blink::StorageKey& key) override;
  67. void GetAllOriginsInfo(GetUsageInfoCallback callback) override;
  68. void DeleteForStorageKey(const blink::StorageKey& key,
  69. ResultCallback callback) override;
  70. void CheckHasServiceWorker(const GURL& url,
  71. const blink::StorageKey& key,
  72. CheckHasServiceWorkerCallback callback) override;
  73. void CheckOfflineCapability(const GURL& url,
  74. const blink::StorageKey& key,
  75. CheckOfflineCapabilityCallback callback) override;
  76. void ClearAllServiceWorkersForTest(base::OnceClosure callback) override;
  77. void StartWorkerForScope(const GURL& scope,
  78. const blink::StorageKey& key,
  79. StartWorkerCallback info_callback,
  80. StatusCodeCallback failure_callback) override;
  81. void StartServiceWorkerAndDispatchMessage(
  82. const GURL& scope,
  83. const blink::StorageKey& key,
  84. blink::TransferableMessage message,
  85. ResultCallback result_callback) override;
  86. void StartServiceWorkerForNavigationHint(
  87. const GURL& document_url,
  88. const blink::StorageKey& key,
  89. StartServiceWorkerForNavigationHintCallback callback) override;
  90. void StopAllServiceWorkersForStorageKey(
  91. const blink::StorageKey& key) override;
  92. void StopAllServiceWorkers(base::OnceClosure callback) override;
  93. const base::flat_map<int64_t /* version_id */,
  94. content::ServiceWorkerRunningInfo>&
  95. GetRunningServiceWorkerInfos() override;
  96. bool IsLiveRunningServiceWorker(int64_t service_worker_version_id) override;
  97. service_manager::InterfaceProvider& GetRemoteInterfaces(
  98. int64_t service_worker_version_id) override;
  99. // content::ServiceWorkerContextObserver:
  100. void OnRegistrationCompleted(const GURL& scope) override;
  101. void OnRegistrationStored(int64_t registration_id,
  102. const GURL& scope) override;
  103. void OnVersionActivated(int64_t version_id, const GURL& scope) override;
  104. void OnVersionRedundant(int64_t version_id, const GURL& scope) override;
  105. void OnVersionStartedRunning(
  106. int64_t version_id,
  107. const content::ServiceWorkerRunningInfo& running_info) override;
  108. void OnVersionStoppedRunning(int64_t version_id) override;
  109. void OnControlleeAdded(
  110. int64_t version_id,
  111. const std::string& client_uuid,
  112. const content::ServiceWorkerClientInfo& client_info) override;
  113. void OnControlleeRemoved(int64_t version_id,
  114. const std::string& client_uuid) override;
  115. void OnNoControllees(int64_t version_id, const GURL& scope) override;
  116. void OnControlleeNavigationCommitted(
  117. int64_t version_id,
  118. const std::string& uuid,
  119. content::GlobalRenderFrameHostId render_frame_host_id) override;
  120. void OnReportConsoleMessage(int64_t version_id,
  121. const GURL& scope,
  122. const content::ConsoleMessage& message) override;
  123. void OnDestruct(ServiceWorkerContext* context) override;
  124. private:
  125. class RunningServiceWorker;
  126. // Invoked by a RunningServiceWorker when it observes that the render process
  127. // has exited.
  128. void OnRenderProcessExited(int64_t version_id);
  129. // Adds a registration to |worker_process_host| that will result in
  130. // |OnRenderProcessExited| with |version_id| when it exits.
  131. void AddRunningServiceWorker(int64_t version_id,
  132. content::RenderProcessHost* worker_process_host);
  133. // Removes a registration made by |AddRunningServiceWorker| if one exists,
  134. // returns true if a registration existed, false otherwise.
  135. bool MaybeRemoveRunningServiceWorker(int64_t version_id);
  136. base::ScopedObservation<content::ServiceWorkerContext,
  137. content::ServiceWorkerContextObserver>
  138. scoped_underlying_context_observation_{this};
  139. base::ObserverList<content::ServiceWorkerContextObserver, true, false>::
  140. Unchecked observer_list_;
  141. // For each running service worker, tracks when their render process exits.
  142. base::flat_map<int64_t /*version_id*/, std::unique_ptr<RunningServiceWorker>>
  143. running_service_workers_;
  144. // Tracks the OnControlleeAdded and OnControlleeRemoved notification for each
  145. // service worker, with the goal of cleaning up duplicate notifications for
  146. // observers of this class.
  147. // TODO(1015692): Fix the underlying code in content/browser/service_worker so
  148. // that duplicate notifications are no longer sent.
  149. base::flat_map<int64_t /*version_id*/,
  150. base::flat_set<std::string /*client_uuid*/>>
  151. service_worker_clients_;
  152. #if DCHECK_IS_ON()
  153. // Keeps track of service worker whose render process exited early.
  154. base::flat_set<int64_t> stopped_service_workers_;
  155. #endif // DCHECK_IS_ON()
  156. };
  157. } // namespace performance_manager
  158. #endif // COMPONENTS_PERFORMANCE_MANAGER_SERVICE_WORKER_CONTEXT_ADAPTER_H_