worker_watcher.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2019 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_WORKER_WATCHER_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_WORKER_WATCHER_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/memory/raw_ptr.h"
  12. #include "base/scoped_observation.h"
  13. #include "base/sequence_checker.h"
  14. #include "components/performance_manager/service_worker_client.h"
  15. #include "content/public/browser/dedicated_worker_service.h"
  16. #include "content/public/browser/global_routing_id.h"
  17. #include "content/public/browser/service_worker_context.h"
  18. #include "content/public/browser/service_worker_context_observer.h"
  19. #include "content/public/browser/shared_worker_service.h"
  20. #include "third_party/blink/public/common/tokens/tokens.h"
  21. namespace performance_manager {
  22. class FrameNodeImpl;
  23. class FrameNodeSource;
  24. class ProcessNodeSource;
  25. class WorkerNodeImpl;
  26. // This class keeps track of running workers of all types for a single browser
  27. // context and handles the ownership of the worker nodes.
  28. //
  29. // Most of the complexity of this class is tracking every worker's clients. Each
  30. // type of worker handles them a bit differently.
  31. //
  32. // The simplest case is dedicated workers, where each worker always has exactly
  33. // one frame client. Technically, it is possible to create a nested dedicated
  34. // worker, but for now they are treated as child of the ancestor frame.
  35. // TODO(1128645): Expose nested dedicated workers correctly.
  36. //
  37. // Shared workers are quite similar to dedicated workers but they can have any
  38. // number of clients. Also, a shared worker can temporarily appear to have no
  39. // clients shortly after being created and just before being destroyed.
  40. //
  41. // Service workers are more complicated to handle. They also can have any number
  42. // of clients, but they aren't only frames. They could also be dedicated worker
  43. // and shared worker clients. These different types of clients are tracked using
  44. // the ServiceWorkerClient class. Also, because of the important role the
  45. // service worker plays with frame navigations, the service worker can be
  46. // created before its first client's navigation has committed to a
  47. // RenderFrameHost. So when a OnControlleeAdded() notification is received for
  48. // a client frame, it is necessary to wait until the render frame host was
  49. // determined.
  50. class WorkerWatcher : public content::DedicatedWorkerService::Observer,
  51. public content::SharedWorkerService::Observer,
  52. public content::ServiceWorkerContextObserver {
  53. public:
  54. WorkerWatcher(const std::string& browser_context_id,
  55. content::DedicatedWorkerService* dedicated_worker_service,
  56. content::SharedWorkerService* shared_worker_service,
  57. content::ServiceWorkerContext* service_worker_context,
  58. ProcessNodeSource* process_node_source,
  59. FrameNodeSource* frame_node_source);
  60. WorkerWatcher(const WorkerWatcher&) = delete;
  61. WorkerWatcher& operator=(const WorkerWatcher&) = delete;
  62. ~WorkerWatcher() override;
  63. // Cleans up this instance and ensures shared worker nodes are correctly
  64. // destroyed on the PM graph.
  65. void TearDown();
  66. // content::DedicatedWorkerService::Observer:
  67. void OnWorkerCreated(
  68. const blink::DedicatedWorkerToken& dedicated_worker_token,
  69. int worker_process_id,
  70. content::GlobalRenderFrameHostId ancestor_render_frame_host_id) override;
  71. void OnBeforeWorkerDestroyed(
  72. const blink::DedicatedWorkerToken& dedicated_worker_token,
  73. content::GlobalRenderFrameHostId ancestor_render_frame_host_id) override;
  74. void OnFinalResponseURLDetermined(
  75. const blink::DedicatedWorkerToken& dedicated_worker_token,
  76. const GURL& url) override;
  77. // content::SharedWorkerService::Observer:
  78. void OnWorkerCreated(const blink::SharedWorkerToken& shared_worker_token,
  79. int worker_process_id,
  80. const base::UnguessableToken& dev_tools_token) override;
  81. void OnBeforeWorkerDestroyed(
  82. const blink::SharedWorkerToken& shared_worker_token) override;
  83. void OnFinalResponseURLDetermined(
  84. const blink::SharedWorkerToken& shared_worker_token,
  85. const GURL& url) override;
  86. void OnClientAdded(
  87. const blink::SharedWorkerToken& shared_worker_token,
  88. content::GlobalRenderFrameHostId render_frame_host_id) override;
  89. void OnClientRemoved(
  90. const blink::SharedWorkerToken& shared_worker_token,
  91. content::GlobalRenderFrameHostId render_frame_host_id) override;
  92. // content::ServiceWorkerContextObserver:
  93. // Note: If you add a new function here, make sure it is also added to
  94. // ServiceWorkerContextAdapter.
  95. void OnVersionStartedRunning(
  96. int64_t version_id,
  97. const content::ServiceWorkerRunningInfo& running_info) override;
  98. void OnVersionStoppedRunning(int64_t version_id) override;
  99. void OnControlleeAdded(
  100. int64_t version_id,
  101. const std::string& client_uuid,
  102. const content::ServiceWorkerClientInfo& client_info) override;
  103. void OnControlleeRemoved(int64_t version_id,
  104. const std::string& client_uuid) override;
  105. void OnControlleeNavigationCommitted(
  106. int64_t version_id,
  107. const std::string& client_uuid,
  108. content::GlobalRenderFrameHostId render_frame_host_id) override;
  109. private:
  110. friend class WorkerWatcherTest;
  111. // Adds a connection between |worker_node| and the frame node represented by
  112. // |client_render_frame_host_id|. Connects them in the graph when the first
  113. // connection is added.
  114. void AddFrameClientConnection(
  115. WorkerNodeImpl* worker_node,
  116. content::GlobalRenderFrameHostId client_render_frame_host_id);
  117. // Removes a connection between |worker_node| and the frame node represented
  118. // by |client_render_frame_host_id|. Disconnects them in the graph when the
  119. // last connection is removed.
  120. void RemoveFrameClientConnection(
  121. WorkerNodeImpl* worker_node,
  122. content::GlobalRenderFrameHostId client_render_frame_host_id);
  123. // If a node with |client_dedicated_worker_token| exists, posts a task to
  124. // the PM graph to connect/disconnect |worker_node| with the
  125. // dedicated worker node associated to |client_dedicated_worker_token|.
  126. void ConnectDedicatedWorkerClient(
  127. WorkerNodeImpl* worker_node,
  128. blink::DedicatedWorkerToken client_dedicated_worker_token);
  129. void DisconnectDedicatedWorkerClient(
  130. WorkerNodeImpl* worker_node,
  131. blink::DedicatedWorkerToken client_dedicated_worker_token);
  132. // If a node with |client_shared_worker_token| exists, posts a task to
  133. // the PM graph to connect/disconnect |worker_node| with the
  134. // dedicated worker node associated to |client_shared_worker_token|.
  135. void ConnectSharedWorkerClient(
  136. WorkerNodeImpl* worker_node,
  137. blink::SharedWorkerToken client_shared_worker_token);
  138. void DisconnectSharedWorkerClient(
  139. WorkerNodeImpl* worker_node,
  140. blink::SharedWorkerToken client_shared_worker_token);
  141. // Posts a task to the PM graph to connect/disconnect |service_worker_node|
  142. // with all of its existing clients. Called when a service worker starts/stops
  143. // running.
  144. void ConnectAllServiceWorkerClients(WorkerNodeImpl* service_worker_node,
  145. int64_t version_id);
  146. void DisconnectAllServiceWorkerClients(WorkerNodeImpl* service_worker_node,
  147. int64_t version_id);
  148. void OnBeforeFrameNodeRemoved(
  149. content::GlobalRenderFrameHostId render_frame_host_id,
  150. FrameNodeImpl* frame_node);
  151. // Adds/removes a connection to |child_worker_node| in the set of child
  152. // workers of a frame.
  153. // On exit |is_first_child_worker| is true if this is the first child worker
  154. // added to the frame and |is_first_child_worker_connection| is true if
  155. // this was the first connection from the frame and |child_worker_node|.
  156. // Conversely |was_last_child_worker| is true if this was the last client
  157. // worker removed, and |was_last_child_worker_connection| is true if this
  158. // removed the last connection between the frame and |child_worker_node|.
  159. void AddChildWorkerConnection(
  160. content::GlobalRenderFrameHostId render_frame_host_id,
  161. WorkerNodeImpl* child_worker_node,
  162. bool* is_first_child_worker,
  163. bool* is_first_child_worker_connection);
  164. void RemoveChildWorkerConnection(
  165. content::GlobalRenderFrameHostId render_frame_host_id,
  166. WorkerNodeImpl* child_worker_node,
  167. bool* was_last_child_worker,
  168. bool* was_last_child_worker_connection);
  169. // Helper functions to retrieve an existing worker node.
  170. // Return the requested node, or nullptr if no such node registered.
  171. WorkerNodeImpl* GetDedicatedWorkerNode(
  172. const blink::DedicatedWorkerToken& dedicated_worker_token);
  173. WorkerNodeImpl* GetSharedWorkerNode(
  174. const blink::SharedWorkerToken& shared_worker_token);
  175. WorkerNodeImpl* GetServiceWorkerNode(int64_t version_id);
  176. #if DCHECK_IS_ON()
  177. bool IsServiceWorkerNode(WorkerNodeImpl* worker_node);
  178. #endif
  179. SEQUENCE_CHECKER(sequence_checker_);
  180. // The ID of the BrowserContext who owns the shared worker service.
  181. const std::string browser_context_id_;
  182. // Observes the DedicatedWorkerService for this browser context.
  183. base::ScopedObservation<content::DedicatedWorkerService,
  184. content::DedicatedWorkerService::Observer>
  185. dedicated_worker_service_observation_{this};
  186. // Observes the SharedWorkerService for this browser context.
  187. base::ScopedObservation<content::SharedWorkerService,
  188. content::SharedWorkerService::Observer>
  189. shared_worker_service_observation_{this};
  190. base::ScopedObservation<content::ServiceWorkerContext,
  191. content::ServiceWorkerContextObserver>
  192. service_worker_context_observation_{this};
  193. // Used to retrieve an existing process node from its render process ID.
  194. const raw_ptr<ProcessNodeSource> process_node_source_;
  195. // Used to retrieve an existing frame node from its render process ID and
  196. // frame ID. Also allows to subscribe to a frame's deletion notification.
  197. const raw_ptr<FrameNodeSource> frame_node_source_;
  198. // Maps each dedicated worker ID to its worker node.
  199. base::flat_map<blink::DedicatedWorkerToken, std::unique_ptr<WorkerNodeImpl>>
  200. dedicated_worker_nodes_;
  201. // Maps each shared worker ID to its worker node.
  202. base::flat_map<blink::SharedWorkerToken, std::unique_ptr<WorkerNodeImpl>>
  203. shared_worker_nodes_;
  204. // Maps each service worker version ID to its worker node.
  205. base::flat_map<int64_t /*version_id*/, std::unique_ptr<WorkerNodeImpl>>
  206. service_worker_nodes_;
  207. // Keeps track of frame clients that are awaiting the navigation commit
  208. // notification. Used for service workers only.
  209. using AwaitingKey =
  210. std::pair<int64_t /*version_id*/, std::string /*client_uuid*/>;
  211. base::flat_set<AwaitingKey> client_frames_awaiting_commit_;
  212. // Maps each service worker to its clients.
  213. base::flat_map<
  214. int64_t /*version_id*/,
  215. base::flat_map<std::string /*client_uuid*/, ServiceWorkerClient>>
  216. service_worker_clients_;
  217. // Maps each frame to the number of connections to each worker that this frame
  218. // is a client of in the graph. Note that normally there's a single connection
  219. // from a frame to a worker, except in rare circumstances where it appears
  220. // that a single frame can have multiple "controllee" relationships to the
  221. // same service worker. This is represented as a single edge in the PM graph.
  222. using WorkerNodeConnections = base::flat_map<WorkerNodeImpl*, size_t>;
  223. base::flat_map<content::GlobalRenderFrameHostId, WorkerNodeConnections>
  224. frame_node_child_worker_connections_;
  225. // Maps each dedicated worker to all its child workers.
  226. using WorkerNodeSet = base::flat_set<WorkerNodeImpl*>;
  227. base::flat_map<blink::DedicatedWorkerToken, WorkerNodeSet>
  228. dedicated_worker_child_workers_;
  229. // Maps each shared worker to all its child workers.
  230. base::flat_map<blink::SharedWorkerToken, WorkerNodeSet>
  231. shared_worker_child_workers_;
  232. #if DCHECK_IS_ON()
  233. // Keeps track of how many OnClientRemoved() calls are expected for an
  234. // existing worker. This happens when OnBeforeFrameNodeRemoved() is invoked
  235. // before OnClientRemoved(), or when it wasn't possible to initially attach
  236. // a client frame node to a worker.
  237. base::flat_map<WorkerNodeImpl*, int> detached_frame_count_per_worker_;
  238. // Keeps track of shared and dedicated workers that were not present when
  239. // a service worker tried to add a client relationship for them.
  240. base::flat_map<WorkerNodeImpl*, base::flat_set<ServiceWorkerClient>>
  241. missing_service_worker_clients_;
  242. #endif // DCHECK_IS_ON()
  243. };
  244. } // namespace performance_manager
  245. #endif // COMPONENTS_PERFORMANCE_MANAGER_WORKER_WATCHER_H_