worker_node_impl.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2017 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_GRAPH_WORKER_NODE_IMPL_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_GRAPH_WORKER_NODE_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/containers/flat_set.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/types/pass_key.h"
  12. #include "components/performance_manager/graph/node_base.h"
  13. #include "components/performance_manager/public/graph/worker_node.h"
  14. #include "third_party/blink/public/common/tokens/tokens.h"
  15. #include "url/gurl.h"
  16. namespace performance_manager {
  17. class FrameNodeImpl;
  18. class ProcessNodeImpl;
  19. namespace execution_context {
  20. class ExecutionContextAccess;
  21. } // namespace execution_context
  22. class WorkerNodeImpl
  23. : public PublicNodeImpl<WorkerNodeImpl, WorkerNode>,
  24. public TypedNodeBase<WorkerNodeImpl, WorkerNode, WorkerNodeObserver> {
  25. public:
  26. static const char kDefaultPriorityReason[];
  27. static constexpr NodeTypeEnum Type() { return NodeTypeEnum::kWorker; }
  28. WorkerNodeImpl(const std::string& browser_context_id,
  29. WorkerType worker_type,
  30. ProcessNodeImpl* process_node,
  31. const blink::WorkerToken& worker_token);
  32. WorkerNodeImpl(const WorkerNodeImpl&) = delete;
  33. WorkerNodeImpl& operator=(const WorkerNodeImpl&) = delete;
  34. ~WorkerNodeImpl() override;
  35. // Invoked when a frame starts/stops being a client of this worker.
  36. void AddClientFrame(FrameNodeImpl* frame_node);
  37. void RemoveClientFrame(FrameNodeImpl* frame_node);
  38. // Invoked when a worker starts/stops being a client of this worker.
  39. void AddClientWorker(WorkerNodeImpl* worker_node);
  40. void RemoveClientWorker(WorkerNodeImpl* worker_node);
  41. // Sets the worker priority, and the reason behind it.
  42. void SetPriorityAndReason(const PriorityAndReason& priority_and_reason);
  43. // Invoked when the worker script was fetched and the final response URL is
  44. // available.
  45. void OnFinalResponseURLDetermined(const GURL& url);
  46. // Getters for const properties.
  47. const std::string& browser_context_id() const;
  48. WorkerType worker_type() const;
  49. ProcessNodeImpl* process_node() const;
  50. const blink::WorkerToken& worker_token() const;
  51. // Getters for non-const properties. These are not thread safe.
  52. const GURL& url() const;
  53. const base::flat_set<FrameNodeImpl*>& client_frames() const;
  54. const base::flat_set<WorkerNodeImpl*>& client_workers() const;
  55. const base::flat_set<WorkerNodeImpl*>& child_workers() const;
  56. const PriorityAndReason& priority_and_reason() const;
  57. base::WeakPtr<WorkerNodeImpl> GetWeakPtr() {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. return weak_factory_.GetWeakPtr();
  60. }
  61. // Implementation details below this point.
  62. // Used by the ExecutionContextRegistry mechanism.
  63. std::unique_ptr<NodeAttachedData>* GetExecutionContextStorage(
  64. base::PassKey<execution_context::ExecutionContextAccess> key) {
  65. return &execution_context_;
  66. }
  67. private:
  68. friend class ExecutionContextPriorityAccess;
  69. friend class WorkerNodeImplDescriber;
  70. void OnJoiningGraph() override;
  71. void OnBeforeLeavingGraph() override;
  72. void RemoveNodeAttachedData() override;
  73. // WorkerNode: These are private so that users of the
  74. // impl use the private getters rather than the public interface.
  75. WorkerType GetWorkerType() const override;
  76. const std::string& GetBrowserContextID() const override;
  77. const ProcessNode* GetProcessNode() const override;
  78. const blink::WorkerToken& GetWorkerToken() const override;
  79. const GURL& GetURL() const override;
  80. const base::flat_set<const FrameNode*> GetClientFrames() const override;
  81. const base::flat_set<const WorkerNode*> GetClientWorkers() const override;
  82. const base::flat_set<const WorkerNode*> GetChildWorkers() const override;
  83. bool VisitChildDedicatedWorkers(const WorkerNodeVisitor&) const override;
  84. const PriorityAndReason& GetPriorityAndReason() const override;
  85. // Invoked when |worker_node| becomes a child of this worker.
  86. void AddChildWorker(WorkerNodeImpl* worker_node);
  87. void RemoveChildWorker(WorkerNodeImpl* worker_node);
  88. // The unique ID of the browser context that this worker belongs to.
  89. const std::string browser_context_id_;
  90. // The type of this worker.
  91. const WorkerType worker_type_;
  92. // The process in which this worker lives.
  93. const raw_ptr<ProcessNodeImpl> process_node_;
  94. // A unique identifier shared with all representations of this worker across
  95. // content and blink. This token should only ever be sent between the browser
  96. // and the renderer hosting the worker. It should not be used to identify a
  97. // worker in browser-to-renderer control messages, but may be used to identify
  98. // a worker in informational messages going in either direction.
  99. const blink::WorkerToken worker_token_;
  100. // The URL of the worker script. This is the final response URL which takes
  101. // into account redirections. This is initially empty and it is set when
  102. // OnFinalResponseURLDetermined() is invoked.
  103. GURL url_ GUARDED_BY_CONTEXT(sequence_checker_);
  104. // Frames that are clients of this worker.
  105. base::flat_set<FrameNodeImpl*> client_frames_
  106. GUARDED_BY_CONTEXT(sequence_checker_);
  107. // Other workers that are clients of this worker. See the declaration of
  108. // WorkerNode for a distinction between client workers and child workers.
  109. base::flat_set<WorkerNodeImpl*> client_workers_
  110. GUARDED_BY_CONTEXT(sequence_checker_);
  111. // The child workers of this worker. See the declaration of WorkerNode for a
  112. // distinction between client workers and child workers.
  113. base::flat_set<WorkerNodeImpl*> child_workers_
  114. GUARDED_BY_CONTEXT(sequence_checker_);
  115. // Worker priority information. Set via ExecutionContextPriorityDecorator.
  116. ObservedProperty::NotifiesOnlyOnChangesWithPreviousValue<
  117. PriorityAndReason,
  118. const PriorityAndReason&,
  119. &WorkerNodeObserver::OnPriorityAndReasonChanged>
  120. priority_and_reason_ GUARDED_BY_CONTEXT(sequence_checker_){
  121. PriorityAndReason(base::TaskPriority::LOWEST,
  122. kDefaultPriorityReason)};
  123. // Used by ExecutionContextRegistry mechanism.
  124. std::unique_ptr<NodeAttachedData> execution_context_
  125. GUARDED_BY_CONTEXT(sequence_checker_);
  126. base::WeakPtrFactory<WorkerNodeImpl> weak_factory_
  127. GUARDED_BY_CONTEXT(sequence_checker_){this};
  128. };
  129. } // namespace performance_manager
  130. #endif // COMPONENTS_PERFORMANCE_MANAGER_GRAPH_WORKER_NODE_IMPL_H_