Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Unified Diff: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp

Issue 1952893003: Implement custom element construction and some 'define' checks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Do not expose CustomElementsRegistry to isolated worlds. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
diff --git a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
index f22190cea9e0545388421215b5afaafbf2006b51..81340f62ee63df57985d1e2ee60b8ea5a5224641 100644
--- a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
+++ b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
@@ -4,26 +4,160 @@
#include "core/dom/custom/CustomElementsRegistry.h"
+// TODO(dominicc): Stop including Document.h when
+// v0CustomElementIsDefined has been removed.
+#include "bindings/core/v8/DOMWrapperWorld.h"
#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/ScriptState.h"
+#include "bindings/core/v8/ScriptValue.h"
+#include "bindings/core/v8/V8Binding.h"
+#include "bindings/core/v8/V8BindingMacros.h"
+#include "core/dom/Document.h"
#include "core/dom/ElementRegistrationOptions.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/dom/custom/CustomElement.h"
+#include "core/dom/custom/CustomElementDefinition.h"
+#include "core/dom/custom/V0CustomElementRegistrationContext.h"
+#include "core/dom/custom/V0CustomElementRegistry.h"
namespace blink {
-CustomElementsRegistry::CustomElementsRegistry()
+CustomElementsRegistry* CustomElementsRegistry::create(
+ ScriptState* scriptState)
{
+ if (!scriptState->contextIsValid())
+ return nullptr;
+ DCHECK(scriptState->world().isMainWorld());
+ return new CustomElementsRegistry(scriptState->isolate());
}
+CustomElementsRegistry::CustomElementsRegistry(v8::Isolate* isolate)
+ : m_constructorIdMap(isolate, v8::Map::New(isolate))
+{
+ m_constructorIdMap.setWeak();
+}
+
+static bool v0CustomElementIsDefined(
+ ScriptState* scriptState,
+ const AtomicString& name)
+{
+ Document* doc = toDocument(scriptState->getExecutionContext());
+ V0CustomElementRegistrationContext* v0Context = doc->registrationContext();
+ return v0Context && v0Context->registry().nameIsDefined(name);
+}
+
+// http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition
void CustomElementsRegistry::define(ScriptState* scriptState,
const AtomicString& name, const ScriptValue& constructor,
const ElementRegistrationOptions& options, ExceptionState& exceptionState)
{
- // TODO(kojii): Element definition process not implemented yet.
- // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition
+ DCHECK(scriptState->world().isMainWorld());
+ v8::Isolate* isolate = scriptState->isolate();
+ v8::Local<v8::Context> context = scriptState->context();
+
+ // TODO(dominicc): Make this check for constructors and not just
+ // functions when
+ // https://bugs.chromium.org/p/v8/issues/detail?id=4993 is fixed.
+ v8::Local<v8::Value> constructorValue = constructor.v8Value();
+ if (!constructorValue->IsFunction()) {
+ exceptionState.throwTypeError(
+ "constructor argument is not a constructor");
+ return;
+ }
+ v8::Local<v8::Object> constructorObject =
+ constructorValue.As<v8::Object>();
+
+ // Raise an exception if the name is not valid.
+ if (!CustomElement::isValidName(name)) {
+ exceptionState.throwSyntaxError(
+ "\"" + name + "\" is not a valid custom element name");
+ return;
+ }
+
+ // Raise an exception if the name is already in use.
+ if (nameIsDefined(name) || v0CustomElementIsDefined(scriptState, name)) {
+ exceptionState.throwDOMException(
+ NotSupportedError,
+ "this name has already been used with this registry");
+ return;
+ }
+
+ // Raise an exception if the constructor is already registered.
+ if (definitionForConstructor(scriptState, constructorObject)) {
+ exceptionState.throwDOMException(
+ NotSupportedError,
+ "this constructor has already been used with this registry");
+ return;
+ }
+
+ // TODO(dominicc): Implement steps:
+ // 5: localName
+ // 6-7: extends processing
+ // 8-9: observed attributes caching
+
+ v8::TryCatch tryCatch(isolate);
+ v8::Local<v8::String> prototypeString =
+ v8AtomicString(isolate, "prototype");
+ v8::Local<v8::Value> prototypeValue;
+ if (!v8Call(
+ constructorObject->Get(context, prototypeString), prototypeValue)) {
+ DCHECK(tryCatch.HasCaught());
+ tryCatch.ReThrow();
+ return;
+ }
+ if (!prototypeValue->IsObject()) {
+ DCHECK(!tryCatch.HasCaught());
+ exceptionState.throwTypeError("constructor prototype is not an object");
+ return;
+ }
+ v8::Local<v8::Object> prototype = prototypeValue.As<v8::Object>();
+
+ // TODO(dominicc): Implement steps:
+ // 12-13: connected callback
+ // 14-15: disconnected callback
+ // 16-17: attribute changed callback
+
+ size_t id = m_definitions.size();
+ m_definitions.append(new CustomElementDefinition(name, isolate, prototype));
+ v8CallOrCrash(m_constructorIdMap.newLocal(isolate)->Set(
haraken 2016/05/11 11:30:09 It would be nicer to use a hidden value (i.e., V8H
+ context,
+ constructorObject,
+ v8::Integer::NewFromUnsigned(isolate, id)));
+ m_names.add(name);
+
+ // TODO(dominicc): Implement steps:
+ // 20: when-defined promise processing
+ DCHECK(!tryCatch.HasCaught() || tryCatch.HasTerminated());
+}
+
+CustomElementDefinition* CustomElementsRegistry::definitionForConstructor(
+ ScriptState* scriptState,
+ v8::Handle<v8::Value> constructor)
+{
+ v8::Local<v8::Value> entry = v8CallOrCrash(
+ m_constructorIdMap.newLocal(scriptState->isolate())->Get(
+ scriptState->context(), constructor));
+ if (!entry->IsUint32())
+ return nullptr;
+ uint32_t id = v8CallOrCrash(entry->Uint32Value(scriptState->context()));
+ return m_definitions[id];
haraken 2016/05/11 11:30:09 I'd add m_id to CustomElementDefinition and add DC
+}
+
+bool CustomElementsRegistry::nameIsDefined(const AtomicString& name) const
+{
+ return m_names.contains(name);
+}
+
+void CustomElementsRegistry::setWrapperReferences(
+ v8::Isolate* isolate,
+ const v8::Persistent<v8::Object>& wrapper) const
+{
+ m_constructorIdMap.setReference(wrapper, isolate);
}
DEFINE_TRACE(CustomElementsRegistry)
{
+ visitor->trace(m_definitions);
}
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698