Skip to content

Commit

Permalink
Inline static Create() from renderer/bindings/core
Browse files Browse the repository at this point in the history
This CL ports calls to Foo::Create() factory functions in
//third_party/blink/renderer/bindings/core to MakeGarbageCollected<Foo>.

Bug: 939691
Change-Id: Iee958f00400fd662c09e64371be97fc676341f6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1920825
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Henrique Ferreiro <hferreiro@igalia.com>
Cr-Commit-Position: refs/heads/master@{#716414}
  • Loading branch information
hferreiro authored and Commit Bot committed Nov 19, 2019
1 parent b58f7f1 commit 21db60b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 132 deletions.
72 changes: 28 additions & 44 deletions third_party/blink/renderer/bindings/core/v8/scheduled_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,39 @@

namespace blink {

ScheduledAction* ScheduledAction::Create(
ScriptState* script_state,
ExecutionContext* target,
V8Function* handler,
const HeapVector<ScriptValue>& arguments) {
if (!script_state->World().IsWorkerWorld()) {
if (!BindingSecurity::ShouldAllowAccessToFrame(
EnteredDOMWindow(script_state->GetIsolate()),
To<Document>(target)->GetFrame(),
BindingSecurity::ErrorReportOption::kDoNotReport)) {
UseCounter::Count(target, WebFeature::kScheduledActionIgnored);
return MakeGarbageCollected<ScheduledAction>(script_state);
}
}
return MakeGarbageCollected<ScheduledAction>(script_state, handler,
arguments);
}

ScheduledAction* ScheduledAction::Create(ScriptState* script_state,
ExecutionContext* target,
const String& handler) {
if (!script_state->World().IsWorkerWorld()) {
if (!BindingSecurity::ShouldAllowAccessToFrame(
EnteredDOMWindow(script_state->GetIsolate()),
To<Document>(target)->GetFrame(),
BindingSecurity::ErrorReportOption::kDoNotReport)) {
UseCounter::Count(target, WebFeature::kScheduledActionIgnored);
return MakeGarbageCollected<ScheduledAction>(script_state);
}
}
return MakeGarbageCollected<ScheduledAction>(script_state, handler);
}

ScheduledAction::ScheduledAction(ScriptState* script_state,
V8Function* function,
ExecutionContext* target,
V8Function* handler,
const HeapVector<ScriptValue>& arguments)
: script_state_(
MakeGarbageCollected<ScriptStateProtectingContext>(script_state)),
function_(function),
arguments_(arguments) {}

ScheduledAction::ScheduledAction(ScriptState* script_state, const String& code)
: script_state_(
MakeGarbageCollected<ScriptStateProtectingContext>(script_state)),
code_(code) {}
MakeGarbageCollected<ScriptStateProtectingContext>(script_state)) {
if (script_state->World().IsWorkerWorld() ||
BindingSecurity::ShouldAllowAccessToFrame(
EnteredDOMWindow(script_state->GetIsolate()),
To<Document>(target)->GetFrame(),
BindingSecurity::ErrorReportOption::kDoNotReport)) {
function_ = handler;
arguments_ = arguments;
} else {
UseCounter::Count(target, WebFeature::kScheduledActionIgnored);
}
}

ScheduledAction::ScheduledAction(ScriptState* script_state)
ScheduledAction::ScheduledAction(ScriptState* script_state,
ExecutionContext* target,
const String& handler)
: script_state_(
MakeGarbageCollected<ScriptStateProtectingContext>(script_state)) {}
MakeGarbageCollected<ScriptStateProtectingContext>(script_state)) {
if (script_state->World().IsWorkerWorld() ||
BindingSecurity::ShouldAllowAccessToFrame(
EnteredDOMWindow(script_state->GetIsolate()),
To<Document>(target)->GetFrame(),
BindingSecurity::ErrorReportOption::kDoNotReport)) {
code_ = handler;
} else {
UseCounter::Count(target, WebFeature::kScheduledActionIgnored);
}
}

ScheduledAction::~ScheduledAction() {
// Verify that owning DOMTimer has eagerly disposed.
Expand Down
21 changes: 7 additions & 14 deletions third_party/blink/renderer/bindings/core/v8/scheduled_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,13 @@ class ScheduledAction final : public GarbageCollected<ScheduledAction>,
DISALLOW_COPY_AND_ASSIGN(ScheduledAction);

public:
static ScheduledAction* Create(ScriptState*,
ExecutionContext* target,
V8Function* handler,
const HeapVector<ScriptValue>& arguments);
static ScheduledAction* Create(ScriptState*,
ExecutionContext* target,
const String& handler);

explicit ScheduledAction(ScriptState*,
V8Function* handler,
const HeapVector<ScriptValue>& arguments);
explicit ScheduledAction(ScriptState*, const String& handler);
// Creates an empty ScheduledAction.
explicit ScheduledAction(ScriptState*);
ScheduledAction(ScriptState*,
ExecutionContext* target,
V8Function* handler,
const HeapVector<ScriptValue>& arguments);
ScheduledAction(ScriptState*,
ExecutionContext* target,
const String& handler);

~ScheduledAction();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,10 @@ ScriptCustomElementDefinition* ScriptCustomElementDefinition::ForConstructor(
return static_cast<ScriptCustomElementDefinition*>(definition);
}

ScriptCustomElementDefinition* ScriptCustomElementDefinition::Create(
const ScriptCustomElementDefinitionData& data,
const CustomElementDescriptor& descriptor,
CustomElementDefinition::Id id) {
auto* definition =
MakeGarbageCollected<ScriptCustomElementDefinition>(data, descriptor);

// Tag the JavaScript constructor object with its ID.
ScriptState* script_state = data.script_state_;
v8::Local<v8::Value> id_value =
v8::Integer::NewFromUnsigned(script_state->GetIsolate(), id);
auto private_id =
script_state->PerContextData()->GetPrivateCustomElementDefinitionId();
CHECK(data.constructor_->CallbackObject()
->SetPrivate(script_state->GetContext(), private_id, id_value)
.ToChecked());

return definition;
}

ScriptCustomElementDefinition::ScriptCustomElementDefinition(
const ScriptCustomElementDefinitionData& data,
const CustomElementDescriptor& descriptor)
const CustomElementDescriptor& descriptor,
CustomElementDefinition::Id id)
: CustomElementDefinition(descriptor,
std::move(data.observed_attributes_),
data.disabled_features_,
Expand All @@ -113,7 +94,17 @@ ScriptCustomElementDefinition::ScriptCustomElementDefinition(
form_associated_callback_(data.form_associated_callback_),
form_reset_callback_(data.form_reset_callback_),
form_disabled_callback_(data.form_disabled_callback_),
form_state_restore_callback_(data.form_state_restore_callback_) {}
form_state_restore_callback_(data.form_state_restore_callback_) {
// Tag the JavaScript constructor object with its ID.
ScriptState* script_state = data.script_state_;
v8::Local<v8::Value> id_value =
v8::Integer::NewFromUnsigned(script_state->GetIsolate(), id);
auto private_id =
script_state->PerContextData()->GetPrivateCustomElementDefinitionId();
CHECK(data.constructor_->CallbackObject()
->SetPrivate(script_state->GetContext(), private_id, id_value)
.ToChecked());
}

void ScriptCustomElementDefinition::Trace(Visitor* visitor) {
visitor->Trace(script_state_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ class CORE_EXPORT ScriptCustomElementDefinition final
CustomElementRegistry*,
v8::Local<v8::Value> constructor);

static ScriptCustomElementDefinition* Create(
const ScriptCustomElementDefinitionData& data,
const CustomElementDescriptor&,
CustomElementDefinition::Id);

ScriptCustomElementDefinition(const ScriptCustomElementDefinitionData& data,
const CustomElementDescriptor&);
const CustomElementDescriptor&,
CustomElementDefinition::Id);
~ScriptCustomElementDefinition() override = default;

void Trace(Visitor*) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding_macros.h"
#include "third_party/blink/renderer/platform/heap/heap.h"

namespace blink {

Expand Down Expand Up @@ -221,7 +222,8 @@ bool ScriptCustomElementDefinitionBuilder::RememberOriginalProperties() {
CustomElementDefinition* ScriptCustomElementDefinitionBuilder::Build(
const CustomElementDescriptor& descriptor,
CustomElementDefinition::Id id) {
return ScriptCustomElementDefinition::Create(data_, descriptor, id);
return MakeGarbageCollected<ScriptCustomElementDefinition>(data_, descriptor,
id);
}

v8::Isolate* ScriptCustomElementDefinitionBuilder::Isolate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "third_party/blink/renderer/platform/bindings/v0_custom_element_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_context_data.h"
#include "third_party/blink/renderer/platform/bindings/v8_private_property.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"

Expand Down Expand Up @@ -163,7 +164,7 @@ V0CustomElementConstructorBuilder::CreateCallbacks() {
v8::MaybeLocal<v8::Function> attribute_changed =
RetrieveCallback("attributeChangedCallback");

callbacks_ = V8V0CustomElementLifecycleCallbacks::Create(
callbacks_ = MakeGarbageCollected<V8V0CustomElementLifecycleCallbacks>(
script_state_, prototype_, created, attached, detached,
attribute_changed);
return callbacks_.Get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,6 @@ namespace blink {
V(detached, DetachedCallback) \
V(attribute_changed, AttributeChangedCallback)

V8V0CustomElementLifecycleCallbacks*
V8V0CustomElementLifecycleCallbacks::Create(
ScriptState* script_state,
v8::Local<v8::Object> prototype,
v8::MaybeLocal<v8::Function> created,
v8::MaybeLocal<v8::Function> attached,
v8::MaybeLocal<v8::Function> detached,
v8::MaybeLocal<v8::Function> attribute_changed) {
v8::Isolate* isolate = script_state->GetIsolate();

// A given object can only be used as a Custom Element prototype
// once; see customElementIsInterfacePrototypeObject
#define SET_PRIVATE_PROPERTY(Maybe, Name) \
static const V8PrivateProperty::SymbolKey kPrivateProperty##Name; \
V8PrivateProperty::Symbol symbol##Name = \
V8PrivateProperty::GetSymbol(isolate, kPrivateProperty##Name); \
DCHECK(!symbol##Name.HasValue(prototype)); \
{ \
v8::Local<v8::Function> function; \
if (Maybe.ToLocal(&function)) \
symbol##Name.Set(prototype, function); \
}

CALLBACK_LIST(SET_PRIVATE_PROPERTY)
#undef SET_PRIVATE_PROPERTY

return MakeGarbageCollected<V8V0CustomElementLifecycleCallbacks>(
script_state, prototype, created, attached, detached, attribute_changed);
}

static V0CustomElementLifecycleCallbacks::CallbackType FlagSet(
v8::MaybeLocal<v8::Function> attached,
v8::MaybeLocal<v8::Function> detached,
Expand Down Expand Up @@ -110,6 +80,22 @@ V8V0CustomElementLifecycleCallbacks::V8V0CustomElementLifecycleCallbacks(
prototype_(script_state->GetIsolate(), prototype){
v8::Isolate* isolate = script_state->GetIsolate();
v8::Local<v8::Function> function;

// A given object can only be used as a Custom Element prototype
// once; see customElementIsInterfacePrototypeObject
#define SET_PRIVATE_PROPERTY(Maybe, Name) \
static const V8PrivateProperty::SymbolKey kPrivateProperty##Name; \
V8PrivateProperty::Symbol symbol##Name = \
V8PrivateProperty::GetSymbol(isolate, kPrivateProperty##Name); \
DCHECK(!symbol##Name.HasValue(prototype)); \
{ \
if (Maybe.ToLocal(&function)) \
symbol##Name.Set(prototype, function); \
}

CALLBACK_LIST(SET_PRIVATE_PROPERTY)
#undef SET_PRIVATE_PROPERTY

#define SET_FIELD(maybe, ignored) \
if (maybe.ToLocal(&function)) \
maybe##_.Set(isolate, function);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ class V8PerContextData;
class V8V0CustomElementLifecycleCallbacks final
: public V0CustomElementLifecycleCallbacks {
public:
static V8V0CustomElementLifecycleCallbacks* Create(
ScriptState*,
v8::Local<v8::Object> prototype,
v8::MaybeLocal<v8::Function> created,
v8::MaybeLocal<v8::Function> attached,
v8::MaybeLocal<v8::Function> detached,
v8::MaybeLocal<v8::Function> attribute_changed);

V8V0CustomElementLifecycleCallbacks(
ScriptState*,
v8::Local<v8::Object> prototype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "third_party/blink/renderer/core/trustedtypes/trusted_types_util.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/weborigin/security_violation_reporting_policy.h"
#include "third_party/blink/renderer/platform/wtf/text/base64.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
Expand Down Expand Up @@ -142,7 +143,7 @@ int WindowOrWorkerGlobalScope::setTimeout(
// be done using the scheduler instead.
V8GCForContextDispose::Instance().NotifyIdle();
}
ScheduledAction* action = ScheduledAction::Create(
auto* action = MakeGarbageCollected<ScheduledAction>(
script_state, execution_context, handler, arguments);
return DOMTimer::Install(execution_context, action,
base::TimeDelta::FromMilliseconds(timeout), true);
Expand Down Expand Up @@ -185,8 +186,8 @@ int WindowOrWorkerGlobalScope::setTimeoutFromString(
// be done using the scheduler instead.
V8GCForContextDispose::Instance().NotifyIdle();
}
ScheduledAction* action =
ScheduledAction::Create(script_state, execution_context, handler);
auto* action = MakeGarbageCollected<ScheduledAction>(
script_state, execution_context, handler);
return DOMTimer::Install(execution_context, action,
base::TimeDelta::FromMilliseconds(timeout), true);
}
Expand All @@ -200,7 +201,7 @@ int WindowOrWorkerGlobalScope::setInterval(
ExecutionContext* execution_context = event_target.GetExecutionContext();
if (!IsAllowed(execution_context, false, g_empty_string))
return 0;
ScheduledAction* action = ScheduledAction::Create(
auto* action = MakeGarbageCollected<ScheduledAction>(
script_state, execution_context, handler, arguments);
return DOMTimer::Install(execution_context, action,
base::TimeDelta::FromMilliseconds(timeout), false);
Expand Down Expand Up @@ -238,8 +239,8 @@ int WindowOrWorkerGlobalScope::setIntervalFromString(
// performance issue.
if (handler.IsEmpty())
return 0;
ScheduledAction* action =
ScheduledAction::Create(script_state, execution_context, handler);
auto* action = MakeGarbageCollected<ScheduledAction>(
script_state, execution_context, handler);
return DOMTimer::Install(execution_context, action,
base::TimeDelta::FromMilliseconds(timeout), false);
}
Expand Down

0 comments on commit 21db60b

Please sign in to comment.