OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" |
6 | 6 |
7 #include "bindings/core/v8/DOMWrapperWorld.h" | 7 #include "bindings/core/v8/DOMWrapperWorld.h" |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptCustomElementDefinition.h" | 9 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 continue; | 74 continue; |
75 } | 75 } |
76 m_exceptionState.throwDOMException( | 76 m_exceptionState.throwDOMException( |
77 NotSupportedError, | 77 NotSupportedError, |
78 "this constructor is already being defined in this registry"); | 78 "this constructor is already being defined in this registry"); |
79 return false; | 79 return false; |
80 } | 80 } |
81 return true; | 81 return true; |
82 } | 82 } |
83 | 83 |
84 bool ScriptCustomElementDefinitionBuilder::valueForName( | |
85 const v8::Local<v8::Object>& object, const String& name, | |
86 v8::Local<v8::Value>& value) const | |
87 { | |
88 v8::Isolate* isolate = m_scriptState->isolate(); | |
89 v8::Local<v8::Context> context = m_scriptState->context(); | |
90 v8::Local<v8::String> nameString = v8String(isolate, name); | |
91 return v8Call(object->Get(context, nameString), value); | |
haraken
2016/06/06 11:45:52
Use v8CallOrCrash.
kojii
2016/06/06 13:10:40
Done.
kojii
2016/06/06 14:43:33
Oh, no, I had to revert this in PS11. The spec def
| |
92 } | |
93 | |
84 bool ScriptCustomElementDefinitionBuilder::checkPrototype() | 94 bool ScriptCustomElementDefinitionBuilder::checkPrototype() |
85 { | 95 { |
86 v8::Isolate* isolate = m_scriptState->isolate(); | |
87 v8::Local<v8::Context> context = m_scriptState->context(); | |
88 v8::Local<v8::String> prototypeString = | |
89 v8AtomicString(isolate, "prototype"); | |
90 v8::Local<v8::Value> prototypeValue; | 96 v8::Local<v8::Value> prototypeValue; |
91 if (!v8Call( | 97 if (!valueForName(m_constructor, "prototype", prototypeValue)) |
92 m_constructor->Get(context, prototypeString), prototypeValue)) { | |
93 return false; | 98 return false; |
94 } | |
95 if (!prototypeValue->IsObject()) { | 99 if (!prototypeValue->IsObject()) { |
96 m_exceptionState.throwTypeError( | 100 m_exceptionState.throwTypeError( |
97 "constructor prototype is not an object"); | 101 "constructor prototype is not an object"); |
98 return false; | 102 return false; |
99 } | 103 } |
100 m_prototype = prototypeValue.As<v8::Object>(); | 104 m_prototype = prototypeValue.As<v8::Object>(); |
101 // If retrieving the prototype destroyed the context, indicate that | 105 // If retrieving the prototype destroyed the context, indicate that |
102 // defining the element should not proceed. | 106 // defining the element should not proceed. |
103 return true; | 107 return true; |
104 } | 108 } |
105 | 109 |
110 bool ScriptCustomElementDefinitionBuilder::callableForName(const String& name, | |
111 v8::Local<v8::Object>& callback) const | |
112 { | |
113 v8::Local<v8::Value> value; | |
114 if (!valueForName(m_prototype, name, value)) | |
115 return false; | |
116 // "undefined" means "omitted", so return true. | |
117 if (value->IsUndefined()) | |
118 return true; | |
119 if (!value->IsObject()) { | |
120 m_exceptionState.throwTypeError( | |
121 String::format("\"%s\" is not an object", name.ascii().data())); | |
122 return false; | |
123 } | |
124 callback = value.As<v8::Object>(); | |
125 if (!callback->IsCallable()) { | |
126 m_exceptionState.throwTypeError( | |
127 String::format("\"%s\" is not callable", name.ascii().data())); | |
128 return false; | |
129 } | |
130 return true; | |
131 } | |
132 | |
133 bool ScriptCustomElementDefinitionBuilder::retrieveObservedAttributes() | |
134 { | |
135 const String kObservedAttributes = "observedAttributes"; | |
136 v8::Local<v8::Value> observedAttributesValue; | |
137 if (!valueForName(m_constructor, kObservedAttributes, observedAttributesValu e)) | |
138 return false; | |
139 if (observedAttributesValue->IsUndefined()) | |
140 return true; | |
141 Vector<AtomicString> list = toImplArray<Vector<AtomicString>>( | |
142 observedAttributesValue, 0, m_scriptState->isolate(), m_exceptionState); | |
143 if (m_exceptionState.hadException()) | |
144 return false; | |
145 if (list.isEmpty()) | |
146 return true; | |
147 m_observedAttributes.reserveCapacityForSize(list.size()); | |
148 for (const auto& attribute : list) | |
149 m_observedAttributes.add(attribute); | |
150 return true; | |
151 } | |
152 | |
153 bool ScriptCustomElementDefinitionBuilder::retrieveProperties() | |
154 { | |
155 | |
156 const String kConnectedCallback = "connectedCallback"; | |
157 const String kDisconnectedCallback = "disconnectedCallback"; | |
158 const String kAttributeChangedCallback = "attributeChangedCallback"; | |
159 return retrieveObservedAttributes() | |
160 && callableForName(kConnectedCallback, m_connectedCallback) | |
161 && callableForName(kDisconnectedCallback, m_disconnectedCallback) | |
162 && callableForName(kAttributeChangedCallback, m_attributeChangedCallback ); | |
163 } | |
164 | |
106 CustomElementDefinition* ScriptCustomElementDefinitionBuilder::build( | 165 CustomElementDefinition* ScriptCustomElementDefinitionBuilder::build( |
107 const CustomElementDescriptor& descriptor) | 166 const CustomElementDescriptor& descriptor) |
108 { | 167 { |
109 return ScriptCustomElementDefinition::create( | 168 return ScriptCustomElementDefinition::create( |
110 m_scriptState.get(), | 169 m_scriptState.get(), |
111 m_registry, | 170 m_registry, |
112 descriptor, | 171 descriptor, |
113 m_constructor, | 172 m_constructor, |
114 m_prototype); | 173 m_prototype, |
174 m_connectedCallback, | |
175 m_disconnectedCallback, | |
176 m_attributeChangedCallback, | |
177 m_observedAttributes); | |
115 } | 178 } |
116 | 179 |
117 } // namespace blink | 180 } // namespace blink |
OLD | NEW |