Skip to content

Commit

Permalink
Add an installAttribute method for V8 objects
Browse files Browse the repository at this point in the history
The existing DOM configuration code for installAttribute only applies to object
templates, and is intended for use when setting up the DOM template. This patch
adds a separate method which can be used to add attributes into a specific V8
context, by providing an object instance rather than a template (this parallels
the existing installAccessor code).

This is intended to be used by origin trials, to configure attributes in a
context-dependent manner.

BUG=584367

Review-Url: https://codereview.chromium.org/2002613002
Cr-Commit-Position: refs/heads/master@{#395659}
(cherry picked from commit 9dc629c)

Review URL: https://codereview.chromium.org/2037493002 .

Cr-Commit-Position: refs/branch-heads/2743@{crosswalk-project#183}
Cr-Branched-From: 2b3ae3b-refs/heads/master@{#394939}
  • Loading branch information
clelland committed Jun 2, 2016
1 parent a4720b8 commit 1ff02c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 29 additions & 6 deletions third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,31 @@ void installAttributeInternal(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate
}
v8::Local<v8::Value> data = v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data));

ASSERT(attribute.propertyLocationConfiguration);
DCHECK(attribute.propertyLocationConfiguration);
if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInstance)
instanceTemplate->SetNativeDataProperty(name, getter, setter, data, static_cast<v8::PropertyAttribute>(attribute.attribute), v8::Local<v8::AccessorSignature>(), static_cast<v8::AccessControl>(attribute.settings));
if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnPrototype)
prototypeTemplate->SetNativeDataProperty(name, getter, setter, data, static_cast<v8::PropertyAttribute>(attribute.attribute), v8::Local<v8::AccessorSignature>(), static_cast<v8::AccessControl>(attribute.settings));
if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInterface)
ASSERT_NOT_REACHED();
NOTREACHED();
}

void installAttributeInternal(v8::Isolate* isolate, v8::Local<v8::Object> instance, v8::Local<v8::Object> prototype, const V8DOMConfiguration::AttributeConfiguration& attribute, const DOMWrapperWorld& world)
{
if (attribute.exposeConfiguration == V8DOMConfiguration::OnlyExposedToPrivateScript
&& !world.isPrivateScriptIsolatedWorld())
return;

v8::Local<v8::Name> name = v8AtomicString(isolate, attribute.name);
v8::Local<v8::Value> data = v8::External::New(isolate, const_cast<WrapperTypeInfo*>(attribute.data));

DCHECK(attribute.propertyLocationConfiguration);
if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInstance)
v8CallOrCrash(instance->DefineOwnProperty(isolate->GetCurrentContext(), name, data, static_cast<v8::PropertyAttribute>(attribute.attribute)));
if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnPrototype)
v8CallOrCrash(prototype->DefineOwnProperty(isolate->GetCurrentContext(), name, data, static_cast<v8::PropertyAttribute>(attribute.attribute)));
if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInterface)
NOTREACHED();
}

template<class FunctionOrTemplate>
Expand Down Expand Up @@ -118,7 +136,7 @@ void installAccessorInternal(v8::Isolate* isolate, v8::Local<ObjectOrTemplate> i
signature = v8::Local<v8::Signature>();
v8::Local<v8::Value> data = v8::External::New(isolate, const_cast<WrapperTypeInfo*>(accessor.data));

ASSERT(accessor.propertyLocationConfiguration);
DCHECK(accessor.propertyLocationConfiguration);
if (accessor.propertyLocationConfiguration &
(V8DOMConfiguration::OnInstance | V8DOMConfiguration::OnPrototype)) {
v8::Local<FunctionOrTemplate> getter = createAccessorFunctionOrTemplate<FunctionOrTemplate>(isolate, getterCallback, data, signature, 0);
Expand Down Expand Up @@ -158,7 +176,7 @@ void installConstantInternal(v8::Isolate* isolate, v8::Local<v8::FunctionTemplat
value = v8::Number::New(isolate, constant.dvalue);
break;
default:
ASSERT_NOT_REACHED();
NOTREACHED();
}
interfaceTemplate->Set(constantName, value, attributes);
prototypeTemplate->Set(constantName, value, attributes);
Expand All @@ -174,7 +192,7 @@ void installMethodInternal(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> i
v8::Local<v8::Name> name = method.methodName(isolate);
v8::FunctionCallback callback = method.callbackForWorld(world);

ASSERT(method.propertyLocationConfiguration);
DCHECK(method.propertyLocationConfiguration);
if (method.propertyLocationConfiguration &
(V8DOMConfiguration::OnInstance | V8DOMConfiguration::OnPrototype)) {
v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), signature, method.length);
Expand Down Expand Up @@ -203,7 +221,7 @@ void installMethodInternal(v8::Isolate* isolate, v8::Local<v8::Object> instance,
v8::Local<v8::Name> name = method.methodName(isolate);
v8::FunctionCallback callback = method.callbackForWorld(world);

ASSERT(method.propertyLocationConfiguration);
DCHECK(method.propertyLocationConfiguration);
if (method.propertyLocationConfiguration &
(V8DOMConfiguration::OnInstance | V8DOMConfiguration::OnPrototype)) {
v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), signature, method.length);
Expand Down Expand Up @@ -238,6 +256,11 @@ void V8DOMConfiguration::installAttribute(v8::Isolate* isolate, const DOMWrapper
installAttributeInternal(isolate, instanceTemplate, prototypeTemplate, attribute, world);
}

void V8DOMConfiguration::installAttribute(v8::Isolate* isolate, const DOMWrapperWorld& world, v8::Local<v8::Object> instance, v8::Local<v8::Object> prototype, const AttributeConfiguration& attribute)
{
installAttributeInternal(isolate, instance, prototype, attribute, world);
}

void V8DOMConfiguration::installAccessors(v8::Isolate* isolate, const DOMWrapperWorld& world, v8::Local<v8::ObjectTemplate> instanceTemplate, v8::Local<v8::ObjectTemplate> prototypeTemplate, v8::Local<v8::FunctionTemplate> interfaceTemplate, v8::Local<v8::Signature> signature, const AccessorConfiguration* accessors, size_t accessorCount)
{
for (size_t i = 0; i < accessorCount; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class CORE_EXPORT V8DOMConfiguration final {

static void installAttribute(v8::Isolate*, const DOMWrapperWorld&, v8::Local<v8::ObjectTemplate> instanceTemplate, v8::Local<v8::ObjectTemplate> prototypeTemplate, const AttributeConfiguration&);

static void installAttribute(v8::Isolate*, const DOMWrapperWorld&, v8::Local<v8::Object> instance, v8::Local<v8::Object> prototype, const AttributeConfiguration&);

// AccessorConfiguration translates into calls to SetAccessorProperty()
// on prototype ObjectTemplate.
struct AccessorConfiguration {
Expand Down

0 comments on commit 1ff02c3

Please sign in to comment.