render_process_host_proxy_browsertest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/render_process_host_proxy.h"
  5. #include "base/callback_helpers.h"
  6. #include "base/memory/weak_ptr.h"
  7. #include "base/process/process.h"
  8. #include "base/run_loop.h"
  9. #include "base/task/task_traits.h"
  10. #include "base/test/bind.h"
  11. #include "components/performance_manager/graph/process_node_impl.h"
  12. #include "components/performance_manager/performance_manager_impl.h"
  13. #include "components/performance_manager/render_process_user_data.h"
  14. #include "components/performance_manager/test_support/performance_manager_browsertest_harness.h"
  15. #include "content/public/browser/browser_task_traits.h"
  16. #include "content/public/browser/browser_thread.h"
  17. #include "content/public/test/browser_test.h"
  18. #include "content/public/test/content_browser_test_utils.h"
  19. #include "content/shell/browser/shell.h"
  20. #include "testing/gmock/include/gmock/gmock.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. namespace performance_manager {
  23. using RenderProcessHostProxyTest = PerformanceManagerBrowserTestHarness;
  24. IN_PROC_BROWSER_TEST_F(RenderProcessHostProxyTest,
  25. RPHDeletionInvalidatesProxy) {
  26. // Navigate, and block until the navigation has completed. RPH, RFH, etc, will
  27. // all exist when this is finished.
  28. ASSERT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
  29. // Helper for dereferencing a RenderProcessHostProxy on the main thread,
  30. // and saving its value in |proxy_host|.
  31. content::RenderProcessHost* proxy_host = nullptr;
  32. auto deref_proxy = base::BindLambdaForTesting(
  33. [&proxy_host](const RenderProcessHostProxy& proxy,
  34. base::OnceClosure quit_loop) {
  35. proxy_host = proxy.Get();
  36. std::move(quit_loop).Run();
  37. });
  38. // Get the RPH associated with the main frame.
  39. content::RenderProcessHost* host =
  40. shell()->web_contents()->GetPrimaryMainFrame()->GetProcess();
  41. // And its associated ProcessNode.
  42. auto* render_process_user_data =
  43. RenderProcessUserData::GetForRenderProcessHost(host);
  44. ASSERT_NE(render_process_user_data, nullptr);
  45. ProcessNode* process_node = render_process_user_data->process_node();
  46. ASSERT_NE(process_node, nullptr);
  47. // Bounce over to the PM sequence, retrieve the proxy, bounce back to the UI
  48. // thread, dereference it if possible, and save the returned host. To be
  49. // fair, it's entirely valid to grab the weak pointer directly on the UI
  50. // thread, as the lifetime of the process node is managed there and the
  51. // property being accessed is thread safe. However, this test aims to simulate
  52. // what would happen with a policy message being posted from the graph.
  53. {
  54. base::RunLoop run_loop;
  55. PerformanceManager::CallOnGraph(
  56. FROM_HERE,
  57. base::BindLambdaForTesting(
  58. [&deref_proxy, process_node, quit_loop = run_loop.QuitClosure()]() {
  59. content::GetUIThreadTaskRunner({})->PostTask(
  60. FROM_HERE,
  61. base::BindOnce(deref_proxy,
  62. process_node->GetRenderProcessHostProxy(),
  63. std::move(quit_loop)));
  64. }));
  65. run_loop.Run();
  66. // We should see the RPH via the proxy.
  67. EXPECT_EQ(host, proxy_host);
  68. }
  69. // Run the same test but make sure the RPH is gone first.
  70. {
  71. base::RunLoop run_loop;
  72. PerformanceManagerImpl::CallOnGraphImpl(
  73. FROM_HERE,
  74. base::BindLambdaForTesting([&deref_proxy, process_node,
  75. shell = this->shell(),
  76. quit_loop = run_loop.QuitClosure()]() {
  77. content::GetUIThreadTaskRunner({})->PostTask(
  78. FROM_HERE,
  79. base::BindLambdaForTesting([shell]() { shell->Close(); }));
  80. content::GetUIThreadTaskRunner({})->PostTask(
  81. FROM_HERE,
  82. base::BindOnce(deref_proxy,
  83. process_node->GetRenderProcessHostProxy(),
  84. std::move(quit_loop)));
  85. }));
  86. run_loop.Run();
  87. // The process was destroyed on the UI thread prior to dereferencing the
  88. // proxy, so it should return nullptr.
  89. EXPECT_EQ(proxy_host, nullptr);
  90. }
  91. }
  92. } // namespace performance_manager