1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/performance_manager/v8_memory/v8_context_tracker.h"
#include <memory>
#include "base/strings/stringprintf.h"
#include "components/performance_manager/execution_context/execution_context_registry_impl.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/test_support/performance_manager_browsertest_harness.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace performance_manager {
namespace v8_memory {
class V8ContextTrackerTest : public PerformanceManagerBrowserTestHarness {
public:
using Super = PerformanceManagerBrowserTestHarness;
V8ContextTrackerTest() = default;
~V8ContextTrackerTest() override = default;
void SetUp() override {
GetGraphFeaturesHelper().EnableV8ContextTracker();
Super::SetUp();
}
void ExpectCounts(size_t v8_context_count,
size_t execution_context_count,
size_t detached_v8_context_count,
size_t destroyed_execution_context_count) {
RunInGraph([&](Graph* graph) {
auto* v8ct = V8ContextTracker::GetFromGraph(graph);
ASSERT_TRUE(v8ct);
EXPECT_EQ(v8_context_count, v8ct->GetV8ContextCountForTesting());
EXPECT_EQ(execution_context_count,
v8ct->GetExecutionContextCountForTesting());
EXPECT_EQ(detached_v8_context_count,
v8ct->GetDetachedV8ContextCountForTesting());
EXPECT_EQ(destroyed_execution_context_count,
v8ct->GetDestroyedExecutionContextCountForTesting());
});
}
};
IN_PROC_BROWSER_TEST_F(V8ContextTrackerTest, AboutBlank) {
ExpectCounts(0, 0, 0, 0);
ASSERT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
ExpectCounts(1, 1, 0, 0);
}
IN_PROC_BROWSER_TEST_F(V8ContextTrackerTest, SameOriginIframeAttributionData) {
GURL urla(embedded_test_server()->GetURL("a.com", "/a_embeds_a.html"));
ASSERT_TRUE(NavigateToURL(shell(), urla));
// Get pointers to the RFHs for each frame.
auto* contents = shell()->web_contents();
content::RenderFrameHost* main_rfh = contents->GetMainFrame();
content::RenderFrameHost* child_rfh = nullptr;
auto frames = contents->GetAllFrames();
ASSERT_EQ(2u, frames.size());
for (auto* rfh : contents->GetAllFrames()) {
if (rfh != main_rfh)
child_rfh = rfh;
}
auto frame_node =
PerformanceManager::GetFrameNodeForRenderFrameHost(child_rfh);
RunInGraph([&frame_node](Graph* graph) {
ASSERT_TRUE(frame_node);
auto* v8_context_tracker = V8ContextTracker::GetFromGraph(graph);
ASSERT_TRUE(v8_context_tracker);
auto* ec_state = v8_context_tracker->GetExecutionContextState(
frame_node->GetFrameToken());
ASSERT_TRUE(ec_state);
ASSERT_TRUE(ec_state->iframe_attribution_data);
});
}
IN_PROC_BROWSER_TEST_F(V8ContextTrackerTest, CrossOriginIframeAttributionData) {
GURL urla(embedded_test_server()->GetURL("a.com", "/a_embeds_b.html"));
ASSERT_TRUE(NavigateToURL(shell(), urla));
// Get pointers to the RFHs for each frame.
auto* contents = shell()->web_contents();
content::RenderFrameHost* main_rfh = contents->GetMainFrame();
content::RenderFrameHost* child_rfh = nullptr;
auto frames = contents->GetAllFrames();
ASSERT_EQ(2u, frames.size());
for (auto* rfh : contents->GetAllFrames()) {
if (rfh != main_rfh)
child_rfh = rfh;
}
auto frame_node =
PerformanceManager::GetFrameNodeForRenderFrameHost(child_rfh);
RunInGraph([&frame_node](Graph* graph) {
ASSERT_TRUE(frame_node);
auto* v8_context_tracker = V8ContextTracker::GetFromGraph(graph);
ASSERT_TRUE(v8_context_tracker);
auto* ec_state = v8_context_tracker->GetExecutionContextState(
frame_node->GetFrameToken());
ASSERT_TRUE(ec_state);
ASSERT_TRUE(ec_state->iframe_attribution_data);
});
}
IN_PROC_BROWSER_TEST_F(V8ContextTrackerTest, SameDocNavigation) {
ExpectCounts(0, 0, 0, 0);
GURL urla(embedded_test_server()->GetURL("a.com", "/a_embeds_b.html"));
ASSERT_TRUE(NavigateToURL(shell(), urla));
ExpectCounts(2, 2, 0, 0);
// Get pointers to the RFHs for each frame.
auto* contents = shell()->web_contents();
content::RenderFrameHost* rfha = contents->GetMainFrame();
content::RenderFrameHost* rfhb = nullptr;
auto frames = contents->GetAllFrames();
ASSERT_EQ(2u, frames.size());
for (auto* rfh : contents->GetAllFrames()) {
if (rfh != rfha)
rfhb = rfh;
}
// Execute a same document navigation in the child frame. This causes a
// v8 context to be detached, and new context attached to the execution
// context. So there will remain 2 execution contexts, there will be 3
// v8 contexts, 1 one of which is detached.
GURL urlb(embedded_test_server()->GetURL("b.com", "/b.html?foo=bar"));
ASSERT_TRUE(ExecJs(
rfhb, base::StringPrintf("location.href = \"%s\"", urlb.spec().c_str())));
WaitForLoad(contents);
ExpectCounts(3, 2, 1, 0);
}
IN_PROC_BROWSER_TEST_F(V8ContextTrackerTest, DetachedContext) {
ExpectCounts(0, 0, 0, 0);
GURL urla(embedded_test_server()->GetURL("a.com", "/a_embeds_a.html"));
ASSERT_TRUE(NavigateToURL(shell(), urla));
ExpectCounts(2, 2, 0, 0);
// Get pointers to the RFHs for each frame.
auto* contents = shell()->web_contents();
content::RenderFrameHost* rfha = contents->GetMainFrame();
// Keep a pointer to the window associated with the child iframe, but
// unload it.
ASSERT_TRUE(ExecJs(rfha,
"let iframe = document.getElementsByTagName('iframe')[0]; "
"document.body.leakyRef = iframe.contentWindow.window; "
"iframe.parentNode.removeChild(iframe); "
"console.log('detached and leaked iframe');"));
ExpectCounts(2, 2, 1, 1);
}
} // namespace v8_memory
} // namespace performance_manager
|