Commit 63353c9c authored by Rakina Zata Amni's avatar Rakina Zata Amni Committed by Commit Bot

Use WeakMember for CSSStyleSheet's adopted tree scopes

Currently we use strong references which will keep the shadow roots
from getting garbage collected.

Bug: 1002763
Change-Id: I62da84e94eaf52504efc80f1468349ee6d2f4fdc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1798222Reviewed-by: default avatarRune Lillesveen <futhark@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695953}
parent d0ff20a7
......@@ -215,6 +215,9 @@ class CORE_EXPORT CSSStyleSheet final : public StyleSheet {
void SetLoadCompleted(bool);
FRIEND_TEST_ALL_PREFIXES(
CSSStyleSheetTest,
GarbageCollectedShadowRootsRemovedFromAdoptedTreeScopes);
FRIEND_TEST_ALL_PREFIXES(CSSStyleSheetTest,
CSSStyleSheetConstructionWithEmptyCSSStyleSheetInit);
FRIEND_TEST_ALL_PREFIXES(
......@@ -252,7 +255,7 @@ class CORE_EXPORT CSSStyleSheet final : public StyleSheet {
Member<Node> owner_node_;
Member<CSSRule> owner_rule_;
HeapHashSet<Member<TreeScope>> adopted_tree_scopes_;
HeapHashSet<WeakMember<TreeScope>> adopted_tree_scopes_;
Member<Document> associated_document_;
HashSet<AtomicString> custom_element_tag_names_;
Member<ScriptPromiseResolver> resolver_;
......
......@@ -4,6 +4,7 @@
#include "third_party/blink/renderer/core/css/css_style_sheet.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_heap.h"
#include "third_party/blink/renderer/bindings/core/v8/media_list_or_string.h"
#include "third_party/blink/renderer/bindings/core/v8/script_function.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
......@@ -13,6 +14,7 @@
#include "third_party/blink/renderer/core/css/css_rule_list.h"
#include "third_party/blink/renderer/core/css/css_style_sheet_init.h"
#include "third_party/blink/renderer/core/css/media_list.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
namespace blink {
......@@ -70,4 +72,31 @@ TEST_F(CSSStyleSheetTest,
ASSERT_FALSE(exception_state.HadException());
}
TEST_F(CSSStyleSheetTest,
GarbageCollectedShadowRootsRemovedFromAdoptedTreeScopes) {
SetBodyInnerHTML("<div id='host_a'></div><div id='host_b'></div>");
auto* host_a = GetElementById("host_a");
auto& shadow_a = host_a->AttachShadowRootInternal(ShadowRootType::kOpen);
auto* host_b = GetElementById("host_b");
auto& shadow_b = host_b->AttachShadowRootInternal(ShadowRootType::kOpen);
DummyExceptionStateForTesting exception_state;
CSSStyleSheetInit* init = CSSStyleSheetInit::Create();
CSSStyleSheet* sheet =
CSSStyleSheet::Create(GetDocument(), init, exception_state);
HeapVector<Member<CSSStyleSheet>> adopted_sheets;
adopted_sheets.push_back(sheet);
shadow_a.SetAdoptedStyleSheets(adopted_sheets);
shadow_b.SetAdoptedStyleSheets(adopted_sheets);
EXPECT_EQ(sheet->adopted_tree_scopes_.size(), 2u);
EXPECT_EQ(shadow_a.AdoptedStyleSheets().size(), 1u);
EXPECT_EQ(shadow_b.AdoptedStyleSheets().size(), 1u);
host_a->remove();
WebHeap::CollectAllGarbageForTesting();
EXPECT_EQ(sheet->adopted_tree_scopes_.size(), 1u);
EXPECT_EQ(shadow_b.AdoptedStyleSheets().size(), 1u);
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment