performance_manager.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "components/performance_manager/public/performance_manager.h"
  5. #include <utility>
  6. #include "components/performance_manager/graph/frame_node_impl.h"
  7. #include "components/performance_manager/graph/page_node_impl.h"
  8. #include "components/performance_manager/graph/process_node_impl.h"
  9. #include "components/performance_manager/performance_manager_impl.h"
  10. #include "components/performance_manager/performance_manager_registry_impl.h"
  11. #include "components/performance_manager/performance_manager_tab_helper.h"
  12. #include "components/performance_manager/public/performance_manager_owned.h"
  13. #include "content/public/browser/render_process_host.h"
  14. namespace performance_manager {
  15. PerformanceManager::PerformanceManager() = default;
  16. PerformanceManager::~PerformanceManager() = default;
  17. // static
  18. void PerformanceManager::CallOnGraph(const base::Location& from_here,
  19. base::OnceClosure callback) {
  20. DCHECK(callback);
  21. PerformanceManagerImpl::GetTaskRunner()->PostTask(from_here,
  22. std::move(callback));
  23. }
  24. // static
  25. void PerformanceManager::CallOnGraph(const base::Location& from_here,
  26. GraphCallback callback) {
  27. DCHECK(callback);
  28. // TODO(siggi): Unwrap this by binding the loose param.
  29. PerformanceManagerImpl::GetTaskRunner()->PostTask(
  30. from_here, base::BindOnce(&PerformanceManagerImpl::RunCallbackWithGraph,
  31. std::move(callback)));
  32. }
  33. // static
  34. void PerformanceManager::PassToGraph(const base::Location& from_here,
  35. std::unique_ptr<GraphOwned> graph_owned) {
  36. DCHECK(graph_owned);
  37. // PassToGraph() should only be called when a graph is available to take
  38. // ownership of |graph_owned|.
  39. DCHECK(IsAvailable());
  40. PerformanceManagerImpl::CallOnGraphImpl(
  41. from_here,
  42. base::BindOnce(
  43. [](std::unique_ptr<GraphOwned> graph_owned, GraphImpl* graph) {
  44. graph->PassToGraph(std::move(graph_owned));
  45. },
  46. std::move(graph_owned)));
  47. }
  48. // static
  49. base::WeakPtr<PageNode> PerformanceManager::GetPrimaryPageNodeForWebContents(
  50. content::WebContents* wc) {
  51. DCHECK(wc);
  52. PerformanceManagerTabHelper* helper =
  53. PerformanceManagerTabHelper::FromWebContents(wc);
  54. if (!helper)
  55. return nullptr;
  56. return helper->primary_page_node()->GetWeakPtrOnUIThread();
  57. }
  58. // static
  59. base::WeakPtr<PageNode> PerformanceManager::GetPageNodeForRenderFrameHost(
  60. content::RenderFrameHost* rfh) {
  61. auto* wc = content::WebContents::FromRenderFrameHost(rfh);
  62. DCHECK(wc);
  63. PerformanceManagerTabHelper* helper =
  64. PerformanceManagerTabHelper::FromWebContents(wc);
  65. if (!helper)
  66. return nullptr;
  67. auto* page_node = helper->GetPageNodeForRenderFrameHost(rfh);
  68. if (!page_node)
  69. return nullptr;
  70. return page_node->GetWeakPtrOnUIThread();
  71. }
  72. // static
  73. base::WeakPtr<FrameNode> PerformanceManager::GetFrameNodeForRenderFrameHost(
  74. content::RenderFrameHost* rfh) {
  75. DCHECK(rfh);
  76. auto* wc = content::WebContents::FromRenderFrameHost(rfh);
  77. PerformanceManagerTabHelper* helper =
  78. PerformanceManagerTabHelper::FromWebContents(wc);
  79. if (!helper)
  80. return nullptr;
  81. auto* frame_node = helper->GetFrameNode(rfh);
  82. if (!frame_node) {
  83. // This should only happen if GetFrameNodeForRenderFrameHost is called
  84. // before the RenderFrameCreated notification is dispatched.
  85. DCHECK(!rfh->IsRenderFrameLive());
  86. return nullptr;
  87. }
  88. return frame_node->GetWeakPtrOnUIThread();
  89. }
  90. // static
  91. base::WeakPtr<ProcessNode>
  92. PerformanceManager::GetProcessNodeForRenderProcessHost(
  93. content::RenderProcessHost* rph) {
  94. DCHECK(rph);
  95. auto* user_data = RenderProcessUserData::GetForRenderProcessHost(rph);
  96. // There is a window after a RenderProcessHost is created before
  97. // CreateProcessNodeAndExposeInterfacesToRendererProcess is called, during
  98. // which time the RenderProcessUserData is not attached to the RPH yet. (It's
  99. // called indirectly from RenderProcessHost::Init.)
  100. if (!user_data)
  101. return nullptr;
  102. return user_data->process_node()->GetWeakPtrOnUIThread();
  103. }
  104. // static
  105. base::WeakPtr<ProcessNode>
  106. PerformanceManager::GetProcessNodeForRenderProcessHostId(
  107. RenderProcessHostId id) {
  108. DCHECK(id);
  109. auto* rph = content::RenderProcessHost::FromID(id.value());
  110. if (!rph)
  111. return nullptr;
  112. return GetProcessNodeForRenderProcessHost(rph);
  113. }
  114. // static
  115. void PerformanceManager::AddObserver(
  116. PerformanceManagerMainThreadObserver* observer) {
  117. PerformanceManagerRegistryImpl::GetInstance()->AddObserver(observer);
  118. }
  119. // static
  120. void PerformanceManager::RemoveObserver(
  121. PerformanceManagerMainThreadObserver* observer) {
  122. PerformanceManagerRegistryImpl::GetInstance()->RemoveObserver(observer);
  123. }
  124. // static
  125. void PerformanceManager::AddMechanism(
  126. PerformanceManagerMainThreadMechanism* mechanism) {
  127. PerformanceManagerRegistryImpl::GetInstance()->AddMechanism(mechanism);
  128. }
  129. // static
  130. void PerformanceManager::RemoveMechanism(
  131. PerformanceManagerMainThreadMechanism* mechanism) {
  132. PerformanceManagerRegistryImpl::GetInstance()->RemoveMechanism(mechanism);
  133. }
  134. // static
  135. bool PerformanceManager::HasMechanism(
  136. PerformanceManagerMainThreadMechanism* mechanism) {
  137. return PerformanceManagerRegistryImpl::GetInstance()->HasMechanism(mechanism);
  138. }
  139. // static
  140. void PerformanceManager::PassToPM(
  141. std::unique_ptr<PerformanceManagerOwned> pm_owned) {
  142. return PerformanceManagerRegistryImpl::GetInstance()->PassToPM(
  143. std::move(pm_owned));
  144. }
  145. // static
  146. std::unique_ptr<PerformanceManagerOwned> PerformanceManager::TakeFromPM(
  147. PerformanceManagerOwned* pm_owned) {
  148. return PerformanceManagerRegistryImpl::GetInstance()->TakeFromPM(pm_owned);
  149. }
  150. // static
  151. void PerformanceManager::RegisterObject(
  152. PerformanceManagerRegistered* pm_object) {
  153. return PerformanceManagerRegistryImpl::GetInstance()->RegisterObject(
  154. pm_object);
  155. }
  156. // static
  157. void PerformanceManager::UnregisterObject(
  158. PerformanceManagerRegistered* pm_object) {
  159. return PerformanceManagerRegistryImpl::GetInstance()->UnregisterObject(
  160. pm_object);
  161. }
  162. // static
  163. PerformanceManagerRegistered* PerformanceManager::GetRegisteredObject(
  164. uintptr_t type_id) {
  165. return PerformanceManagerRegistryImpl::GetInstance()->GetRegisteredObject(
  166. type_id);
  167. }
  168. // static
  169. scoped_refptr<base::SequencedTaskRunner> PerformanceManager::GetTaskRunner() {
  170. return PerformanceManagerImpl::GetTaskRunner();
  171. }
  172. } // namespace performance_manager