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