Commit e7df4948 authored by Xida Chen's avatar Xida Chen Committed by Commit Bot

Create a CrossThreadKeywordValue

This CL creates a CrossThreadKeywordValue, and use it in the
CrossThreadStyleValueFromComputedStyle. Unit tests are added to ensure
the correctness.

Bug: 895579
Change-Id: Id5972634732489473dcb49dd54e8856e93d8752c
Reviewed-on: https://chromium-review.googlesource.com/c/1427780
Commit-Queue: Xida Chen <xidachen@chromium.org>
Reviewed-by: default avatarAnders Hartvoll Ruud <andruud@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626101}
parent 654dd89d
......@@ -10,11 +10,13 @@
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PROPERTY_H_
#include "third_party/blink/renderer/core/css/css_property_name.h"
#include "third_party/blink/renderer/core/css/css_property_names.h"
#include "third_party/blink/renderer/core/css/css_value.h"
#include "third_party/blink/renderer/core/css/properties/css_unresolved_property.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_keyword_value.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_style_value.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_unsupported_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_keyword_value.h"
#include "third_party/blink/renderer/core/css/cssom/style_value_factory.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
#include "third_party/blink/renderer/platform/text/text_direction.h"
#include "third_party/blink/renderer/platform/text/writing_mode.h"
......@@ -111,9 +113,19 @@ class CSSProperty : public CSSUnresolvedProperty {
computed_style, layout_object, node, allow_visited_style);
if (!css_value)
return std::make_unique<CrossThreadUnsupportedValue>("");
// Make an isolated copy to ensure that it is safe to pass cross thread.
return std::make_unique<CrossThreadUnsupportedValue>(
css_value->CssText().IsolatedCopy());
CSSStyleValue* style_value = StyleValueFactory::CssValueToStyleValue(
GetCSSPropertyName(), *css_value);
if (!style_value)
return std::make_unique<CrossThreadUnsupportedValue>("");
switch (style_value->GetType()) {
case CSSStyleValue::StyleValueType::kKeywordType:
return std::make_unique<CrossThreadKeywordValue>(
static_cast<CSSKeywordValue*>(style_value)->value().IsolatedCopy());
default:
// Make an isolated copy to ensure that it is safe to pass cross thread.
return std::make_unique<CrossThreadUnsupportedValue>(
css_value->CssText().IsolatedCopy());
}
}
virtual const CSSProperty& ResolveDirectionAwareProperty(
TextDirection,
......
......@@ -195,6 +195,8 @@ blink_core_sources("css") {
"css_viewport_rule.h",
"cssom/computed_style_property_map.cc",
"cssom/computed_style_property_map.h",
"cssom/cross_thread_keyword_value.cc",
"cssom/cross_thread_keyword_value.h",
"cssom/cross_thread_style_value.h",
"cssom/cross_thread_unsupported_value.cc",
"cssom/cross_thread_unsupported_value.h",
......
// Copyright 2019 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 "third_party/blink/renderer/core/css/cssom/cross_thread_keyword_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_keyword_value.h"
namespace blink {
CSSStyleValue* CrossThreadKeywordValue::ToCSSStyleValue() {
return CSSKeywordValue::Create(keyword_value_);
}
} // namespace blink
// Copyright 2019 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSSOM_CROSS_THREAD_KEYWORD_VALUE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSSOM_CROSS_THREAD_KEYWORD_VALUE_H_
#include "base/macros.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_style_value.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class CSSStyleValue;
class CORE_EXPORT CrossThreadKeywordValue final : public CrossThreadStyleValue {
public:
explicit CrossThreadKeywordValue(const String& keyword)
: keyword_value_(keyword) {}
~CrossThreadKeywordValue() override = default;
CSSStyleValue* ToCSSStyleValue() override;
private:
friend class CrossThreadStyleValueTest;
String keyword_value_;
DISALLOW_COPY_AND_ASSIGN(CrossThreadKeywordValue);
};
} // namespace blink
#endif
......@@ -7,7 +7,9 @@
#include <memory>
#include "base/single_thread_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_keyword_value.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_unsupported_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_keyword_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_style_value.h"
#include "third_party/blink/renderer/platform/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
......@@ -43,6 +45,15 @@ class CrossThreadStyleValueTest : public testing::Test {
waitable_event->Signal();
}
void CheckKeywordValue(WaitableEvent* waitable_event,
std::unique_ptr<CrossThreadKeywordValue> value) {
DCHECK(!IsMainThread());
thread_->InitializeOnThread();
EXPECT_EQ(value->keyword_value_, "Keyword");
waitable_event->Signal();
}
protected:
std::unique_ptr<WebThreadSupportingGC> thread_;
};
......@@ -80,4 +91,34 @@ TEST_F(CrossThreadStyleValueTest, CrossThreadUnsupportedValueToCSSStyleValue) {
EXPECT_EQ(style_value->CSSText(), "Unsupported");
}
TEST_F(CrossThreadStyleValueTest, PassKeywordValueCrossThread) {
std::unique_ptr<CrossThreadKeywordValue> value =
std::make_unique<CrossThreadKeywordValue>("Keyword");
DCHECK(value);
// Use a WebThreadSupportingGC to emulate worklet thread.
thread_ = WebThreadSupportingGC::Create(
ThreadCreationParams(WebThreadType::kTestThread));
WaitableEvent waitable_event;
thread_->PostTask(
FROM_HERE, CrossThreadBind(&CrossThreadStyleValueTest::CheckKeywordValue,
CrossThreadUnretained(this),
CrossThreadUnretained(&waitable_event),
WTF::Passed(std::move(value))));
waitable_event.Wait();
ShutDownThread();
}
TEST_F(CrossThreadStyleValueTest, CrossThreadKeywordValueToCSSStyleValue) {
std::unique_ptr<CrossThreadKeywordValue> value =
std::make_unique<CrossThreadKeywordValue>("Keyword");
DCHECK(value);
CSSStyleValue* style_value = value->ToCSSStyleValue();
EXPECT_EQ(style_value->GetType(),
CSSStyleValue::StyleValueType::kKeywordType);
EXPECT_EQ(static_cast<CSSKeywordValue*>(style_value)->value(), "Keyword");
}
} // namespace blink
......@@ -7,7 +7,6 @@
#include "base/macros.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_value.h"
#include "third_party/blink/renderer/core/css/cssom/cross_thread_style_value.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
......
......@@ -8,6 +8,7 @@
#include "base/single_thread_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/css_computed_style_declaration.h"
#include "third_party/blink/renderer/core/css/css_test_helpers.h"
#include "third_party/blink/renderer/core/css/cssom/paint_worklet_input.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
......@@ -107,7 +108,7 @@ class PaintWorkletStylePropertyMapTest : public PageTestBase {
const HashMap<String, std::unique_ptr<CrossThreadStyleValue>>& values =
map->ValuesForTest();
EXPECT_EQ(values.size(), 4u);
EXPECT_EQ(values.size(), 6u);
EXPECT_EQ(values.at("color")->ToCSSStyleValue()->CSSText(),
"rgb(0, 255, 0)");
EXPECT_EQ(values.at("color")->ToCSSStyleValue()->GetType(),
......@@ -115,12 +116,24 @@ class PaintWorkletStylePropertyMapTest : public PageTestBase {
EXPECT_EQ(values.at("align-items")->ToCSSStyleValue()->CSSText(), "normal");
EXPECT_EQ(values.at("align-items")->ToCSSStyleValue()->GetType(),
CSSStyleValue::StyleValueType::kUnknownType);
EXPECT_EQ(
static_cast<CSSKeywordValue*>(values.at("display")->ToCSSStyleValue())
->value(),
"block");
EXPECT_EQ(values.at("display")->ToCSSStyleValue()->GetType(),
CSSStyleValue::StyleValueType::kKeywordType);
EXPECT_EQ(values.at("--foo")->ToCSSStyleValue()->CSSText(), "PaintWorklet");
EXPECT_EQ(values.at("--foo")->ToCSSStyleValue()->GetType(),
CSSStyleValue::StyleValueType::kUnknownType);
EXPECT_EQ(values.at("--bar")->ToCSSStyleValue()->CSSText(), "");
EXPECT_EQ(values.at("--bar")->ToCSSStyleValue()->GetType(),
CSSStyleValue::StyleValueType::kUnknownType);
EXPECT_EQ(values.at("--keyword")->ToCSSStyleValue()->GetType(),
CSSStyleValue::StyleValueType::kKeywordType);
EXPECT_EQ(
static_cast<CSSKeywordValue*>(values.at("--keyword")->ToCSSStyleValue())
->value(),
"test");
waitable_event->Signal();
}
......@@ -190,12 +203,18 @@ TEST_F(PaintWorkletStylePropertyMapTest, CustomPropertyAccessors) {
// threads and no information is lost.
TEST_F(PaintWorkletStylePropertyMapTest, PassValuesCrossThread) {
Vector<CSSPropertyID> native_properties(
{CSSPropertyColor, CSSPropertyAlignItems});
{CSSPropertyColor, CSSPropertyAlignItems, CSSPropertyDisplay});
GetDocument().documentElement()->style()->setProperty(
&GetDocument(), "color", "rgb(0, 255, 0)", "", ASSERT_NO_EXCEPTION);
Vector<AtomicString> custom_properties({"--foo", "--bar"});
GetDocument().documentElement()->style()->setProperty(
&GetDocument(), "display", "block", "", ASSERT_NO_EXCEPTION);
Vector<AtomicString> custom_properties({"--foo", "--bar", "--keyword"});
css_test_helpers::RegisterProperty(GetDocument(), "--keyword", "test", "test",
false);
GetDocument().documentElement()->style()->setProperty(
&GetDocument(), "--foo", "PaintWorklet", "", ASSERT_NO_EXCEPTION);
GetDocument().documentElement()->style()->setProperty(
&GetDocument(), "--keyword", "test", "", ASSERT_NO_EXCEPTION);
UpdateAllLifecyclePhasesForTest();
Node* node = PageNode();
......
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