worker_node.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_PUBLIC_GRAPH_WORKER_NODE_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_WORKER_NODE_H_
  6. #include <string>
  7. #include "base/callback_forward.h"
  8. #include "base/containers/flat_set.h"
  9. #include "base/types/token_type.h"
  10. #include "components/performance_manager/public/execution_context_priority/execution_context_priority.h"
  11. #include "components/performance_manager/public/graph/node.h"
  12. #include "third_party/blink/public/common/tokens/tokens.h"
  13. class GURL;
  14. namespace performance_manager {
  15. class WorkerNodeObserver;
  16. class FrameNode;
  17. class ProcessNode;
  18. using execution_context_priority::PriorityAndReason;
  19. // Represents a running instance of a WorkerGlobalScope.
  20. // See https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope.
  21. //
  22. // A worker is the equivalent of a thread on the web platform. To do
  23. // asynchronous work, a frame can create a dedicated worker or a shared worker
  24. // with Javascript. Those workers can now be used by sending an asynchronous
  25. // message using the postMessage() method, and replies can be received by
  26. // registering a message handler on the worker object.
  27. //
  28. // One notable special case is that dedicated workers can be nested. That means
  29. // that a dedicated worker can create another dedicated worker which is then
  30. // only accessible from the parent worker.
  31. //
  32. // Service workers are different. Instead of being created by the javascript
  33. // when needed, a service worker is registered once and affects all frames and
  34. // dedicated/shared workers whose URL matches the scope of the service worker.
  35. // A service worker is mainly used to intercept network requests and optionally
  36. // serve the responses from a cache, making the web site work offline.
  37. //
  38. // A client, from the point of view of the worker, is the frame or worker that
  39. // caused the worker to start running, either because it explicitly created it,
  40. // or a service worker is registered to handle their network requests.
  41. class WorkerNode : public Node {
  42. public:
  43. using WorkerNodeVisitor = base::RepeatingCallback<bool(const WorkerNode*)>;
  44. // The different possible worker types.
  45. enum class WorkerType {
  46. kDedicated,
  47. kShared,
  48. kService,
  49. };
  50. using Observer = WorkerNodeObserver;
  51. class ObserverDefaultImpl;
  52. WorkerNode();
  53. WorkerNode(const WorkerNode&) = delete;
  54. WorkerNode& operator=(const WorkerNode&) = delete;
  55. ~WorkerNode() override;
  56. // Returns the worker type. Note that this is different from the NodeTypeEnum.
  57. virtual WorkerType GetWorkerType() const = 0;
  58. // Returns the unique ID of the browser context that this worker belongs to.
  59. virtual const std::string& GetBrowserContextID() const = 0;
  60. // Returns the process node to which this worker belongs. This is a constant
  61. // over the lifetime of the frame.
  62. virtual const ProcessNode* GetProcessNode() const = 0;
  63. // Returns the unique token identifying this worker.
  64. virtual const blink::WorkerToken& GetWorkerToken() const = 0;
  65. // Returns the URL of the worker script. This is the final response URL which
  66. // takes into account redirections.
  67. virtual const GURL& GetURL() const = 0;
  68. // Returns the frames that are clients of this worker.
  69. virtual const base::flat_set<const FrameNode*> GetClientFrames() const = 0;
  70. // Returns the workers that are clients of this worker.
  71. // There are 2 cases where this is possible:
  72. // - A dedicated worker can create nested workers. The parent worker becomes
  73. // one of its client worker.
  74. // - A dedicated worker or a shared worker will become a client of the service
  75. // worker that handles their network requests.
  76. virtual const base::flat_set<const WorkerNode*> GetClientWorkers() const = 0;
  77. // Returns the child workers of this worker.
  78. // There are 2 cases where a worker can be the child of another worker:
  79. // - A dedicated worker can create nested workers. The nested worker becomes
  80. // a child worker of the parent.
  81. // - A service worker will become a child worker of every worker for which
  82. // it handles network requests.
  83. virtual const base::flat_set<const WorkerNode*> GetChildWorkers() const = 0;
  84. // Visits the child dedicated workers of this frame. The iteration is halted
  85. // if the visitor returns false. Returns true if every call to the visitor
  86. // returned true, false otherwise.
  87. //
  88. // The reason why we don't have a generic VisitChildWorkers method is that
  89. // a service/shared worker may appear as a child of multiple other nodes
  90. // and thus may be visited multiple times.
  91. virtual bool VisitChildDedicatedWorkers(
  92. const WorkerNodeVisitor& visitor) const = 0;
  93. // Returns the current priority of the worker, and the reason for the worker
  94. // having that particular priority.
  95. virtual const PriorityAndReason& GetPriorityAndReason() const = 0;
  96. };
  97. // Pure virtual observer interface. Derive from this if you want to be forced to
  98. // implement the entire interface.
  99. class WorkerNodeObserver {
  100. public:
  101. WorkerNodeObserver();
  102. WorkerNodeObserver(const WorkerNodeObserver&) = delete;
  103. WorkerNodeObserver& operator=(const WorkerNodeObserver&) = delete;
  104. virtual ~WorkerNodeObserver();
  105. // Node lifetime notifications.
  106. // Called when a |worker_node| is added to the graph. Observers must not make
  107. // any property changes or cause re-entrant notifications during the scope of
  108. // this call. Instead, make property changes via a separate posted task.
  109. virtual void OnWorkerNodeAdded(const WorkerNode* worker_node) = 0;
  110. // Called before a |worker_node| is removed from the graph. Observers must not
  111. // make any property changes or cause re-entrant notifications during the
  112. // scope of this call.
  113. virtual void OnBeforeWorkerNodeRemoved(const WorkerNode* worker_node) = 0;
  114. // Notifications of property changes.
  115. // Invoked when the final url of the worker script has been determined, which
  116. // happens when the script has finished loading.
  117. virtual void OnFinalResponseURLDetermined(const WorkerNode* worker_node) = 0;
  118. // Invoked when |client_frame_node| becomes a client of |worker_node|.
  119. virtual void OnClientFrameAdded(const WorkerNode* worker_node,
  120. const FrameNode* client_frame_node) = 0;
  121. // Invoked when |client_frame_node| is no longer a client of |worker_node|.
  122. virtual void OnBeforeClientFrameRemoved(
  123. const WorkerNode* worker_node,
  124. const FrameNode* client_frame_node) = 0;
  125. // Invoked when |client_worker_node| becomes a client of |worker_node|.
  126. virtual void OnClientWorkerAdded(const WorkerNode* worker_node,
  127. const WorkerNode* client_worker_node) = 0;
  128. // Invoked when |client_worker_node| is no longer a client of |worker_node|.
  129. virtual void OnBeforeClientWorkerRemoved(
  130. const WorkerNode* worker_node,
  131. const WorkerNode* client_worker_node) = 0;
  132. // Invoked when the worker priority and reason changes.
  133. virtual void OnPriorityAndReasonChanged(
  134. const WorkerNode* worker_node,
  135. const PriorityAndReason& previous_value) = 0;
  136. };
  137. // Default implementation of observer that provides dummy versions of each
  138. // function. Derive from this if you only need to implement a few of the
  139. // functions.
  140. class WorkerNode::ObserverDefaultImpl : public WorkerNodeObserver {
  141. public:
  142. ObserverDefaultImpl();
  143. ObserverDefaultImpl(const ObserverDefaultImpl&) = delete;
  144. ObserverDefaultImpl& operator=(const ObserverDefaultImpl&) = delete;
  145. ~ObserverDefaultImpl() override;
  146. // WorkerNodeObserver implementation:
  147. // Called when a |worker_node| is added to the graph.
  148. void OnWorkerNodeAdded(const WorkerNode* worker_node) override {}
  149. void OnBeforeWorkerNodeRemoved(const WorkerNode* worker_node) override {}
  150. void OnFinalResponseURLDetermined(const WorkerNode* worker_node) override {}
  151. void OnClientFrameAdded(const WorkerNode* worker_node,
  152. const FrameNode* client_frame_node) override {}
  153. void OnBeforeClientFrameRemoved(const WorkerNode* worker_node,
  154. const FrameNode* client_frame_node) override {
  155. }
  156. void OnClientWorkerAdded(const WorkerNode* worker_node,
  157. const WorkerNode* client_worker_node) override {}
  158. void OnBeforeClientWorkerRemoved(
  159. const WorkerNode* worker_node,
  160. const WorkerNode* client_worker_node) override {}
  161. void OnPriorityAndReasonChanged(
  162. const WorkerNode* worker_node,
  163. const PriorityAndReason& previous_value) override {}
  164. };
  165. } // namespace performance_manager
  166. #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_WORKER_NODE_H_