From 9dc629cf5fdd66d747c10c5f0f2e5d4a793eb104 Mon Sep 17 00:00:00 2001 From: iclelland Date: Tue, 24 May 2016 11:59:45 -0700 Subject: [PATCH] Add an installAttribute method for V8 objects 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} --- .../bindings/core/v8/V8DOMConfiguration.cpp | 35 +++++++++++++++---- .../bindings/core/v8/V8DOMConfiguration.h | 2 ++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.cpp b/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.cpp index a5771eb19e35f..0396898eebd7e 100644 --- a/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.cpp +++ b/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.cpp @@ -52,13 +52,31 @@ void installAttributeInternal(v8::Isolate* isolate, v8::Local data = v8::External::New(isolate, const_cast(attribute.data)); - ASSERT(attribute.propertyLocationConfiguration); + DCHECK(attribute.propertyLocationConfiguration); if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInstance) instanceTemplate->SetNativeDataProperty(name, getter, setter, data, static_cast(attribute.attribute), v8::Local(), static_cast(attribute.settings)); if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnPrototype) prototypeTemplate->SetNativeDataProperty(name, getter, setter, data, static_cast(attribute.attribute), v8::Local(), static_cast(attribute.settings)); if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInterface) - ASSERT_NOT_REACHED(); + NOTREACHED(); +} + +void installAttributeInternal(v8::Isolate* isolate, v8::Local instance, v8::Local prototype, const V8DOMConfiguration::AttributeConfiguration& attribute, const DOMWrapperWorld& world) +{ + if (attribute.exposeConfiguration == V8DOMConfiguration::OnlyExposedToPrivateScript + && !world.isPrivateScriptIsolatedWorld()) + return; + + v8::Local name = v8AtomicString(isolate, attribute.name); + v8::Local data = v8::External::New(isolate, const_cast(attribute.data)); + + DCHECK(attribute.propertyLocationConfiguration); + if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInstance) + v8CallOrCrash(instance->DefineOwnProperty(isolate->GetCurrentContext(), name, data, static_cast(attribute.attribute))); + if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnPrototype) + v8CallOrCrash(prototype->DefineOwnProperty(isolate->GetCurrentContext(), name, data, static_cast(attribute.attribute))); + if (attribute.propertyLocationConfiguration & V8DOMConfiguration::OnInterface) + NOTREACHED(); } template @@ -118,7 +136,7 @@ void installAccessorInternal(v8::Isolate* isolate, v8::Local i signature = v8::Local(); v8::Local data = v8::External::New(isolate, const_cast(accessor.data)); - ASSERT(accessor.propertyLocationConfiguration); + DCHECK(accessor.propertyLocationConfiguration); if (accessor.propertyLocationConfiguration & (V8DOMConfiguration::OnInstance | V8DOMConfiguration::OnPrototype)) { v8::Local getter = createAccessorFunctionOrTemplate(isolate, getterCallback, data, signature, 0); @@ -158,7 +176,7 @@ void installConstantInternal(v8::Isolate* isolate, v8::LocalSet(constantName, value, attributes); prototypeTemplate->Set(constantName, value, attributes); @@ -174,7 +192,7 @@ void installMethodInternal(v8::Isolate* isolate, v8::Local i v8::Local 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 functionTemplate = v8::FunctionTemplate::New(isolate, callback, v8::Local(), signature, method.length); @@ -203,7 +221,7 @@ void installMethodInternal(v8::Isolate* isolate, v8::Local instance, v8::Local 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 functionTemplate = v8::FunctionTemplate::New(isolate, callback, v8::Local(), signature, method.length); @@ -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 instance, v8::Local prototype, const AttributeConfiguration& attribute) +{ + installAttributeInternal(isolate, instance, prototype, attribute, world); +} + void V8DOMConfiguration::installAccessors(v8::Isolate* isolate, const DOMWrapperWorld& world, v8::Local instanceTemplate, v8::Local prototypeTemplate, v8::Local interfaceTemplate, v8::Local signature, const AccessorConfiguration* accessors, size_t accessorCount) { for (size_t i = 0; i < accessorCount; ++i) diff --git a/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.h b/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.h index 1636dcd447b85..ce307f514289a 100644 --- a/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.h +++ b/third_party/WebKit/Source/bindings/core/v8/V8DOMConfiguration.h @@ -84,6 +84,8 @@ class CORE_EXPORT V8DOMConfiguration final { static void installAttribute(v8::Isolate*, const DOMWrapperWorld&, v8::Local instanceTemplate, v8::Local prototypeTemplate, const AttributeConfiguration&); + static void installAttribute(v8::Isolate*, const DOMWrapperWorld&, v8::Local instance, v8::Local prototype, const AttributeConfiguration&); + // AccessorConfiguration translates into calls to SetAccessorProperty() // on prototype ObjectTemplate. struct AccessorConfiguration {