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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 | 53 |
54 // Constructor is already registered. | 54 // Constructor is already registered. |
55 m_exceptionState.throwDOMException( | 55 m_exceptionState.throwDOMException( |
56 NotSupportedError, | 56 NotSupportedError, |
57 "this constructor has already been used with this registry"); | 57 "this constructor has already been used with this registry"); |
58 return false; | 58 return false; |
59 } | 59 } |
60 return true; | 60 return true; |
61 } | 61 } |
62 | 62 |
63 bool ScriptCustomElementDefinitionBuilder::getValue( | |
64 const v8::Local<v8::Object>& object, const char* name, | |
65 v8::Local<v8::Value>& value) const | |
66 { | |
67 v8::Isolate* isolate = m_scriptState->isolate(); | |
68 v8::Local<v8::Context> context = m_scriptState->context(); | |
69 v8::Local<v8::String> nameString = v8AtomicString(isolate, name); | |
70 return v8Call(object->Get(context, nameString), value); | |
71 } | |
72 | |
63 bool ScriptCustomElementDefinitionBuilder::checkPrototype() | 73 bool ScriptCustomElementDefinitionBuilder::checkPrototype() |
64 { | 74 { |
65 v8::Isolate* isolate = m_scriptState->isolate(); | |
66 v8::Local<v8::Context> context = m_scriptState->context(); | |
67 v8::Local<v8::String> prototypeString = | |
68 v8AtomicString(isolate, "prototype"); | |
69 v8::Local<v8::Value> prototypeValue; | 75 v8::Local<v8::Value> prototypeValue; |
70 if (!v8Call( | 76 if (!getValue(m_constructor, "prototype", prototypeValue)) |
71 m_constructor->Get(context, prototypeString), prototypeValue)) { | |
72 return false; | 77 return false; |
73 } | |
74 if (!prototypeValue->IsObject()) { | 78 if (!prototypeValue->IsObject()) { |
75 m_exceptionState.throwTypeError( | 79 m_exceptionState.throwTypeError( |
76 "constructor prototype is not an object"); | 80 "constructor prototype is not an object"); |
77 return false; | 81 return false; |
78 } | 82 } |
79 m_prototype = prototypeValue.As<v8::Object>(); | 83 m_prototype = prototypeValue.As<v8::Object>(); |
80 // If retrieving the prototype destroyed the context, indicate that | 84 // If retrieving the prototype destroyed the context, indicate that |
81 // defining the element should not proceed. | 85 // defining the element should not proceed. |
82 return true; | 86 return true; |
83 } | 87 } |
84 | 88 |
89 bool ScriptCustomElementDefinitionBuilder::getCallable(const char* name, | |
yosin_UTC9
2016/06/02 01:33:38
It is better to return Optional, or v8::Maybe<v8::
| |
90 v8::Local<v8::Object>& callback) const | |
91 { | |
92 v8::Local<v8::Value> value; | |
93 if (!getValue(m_prototype, name, value)) | |
94 return false; | |
95 if (value->IsUndefined()) | |
96 return true; | |
97 if (!value->IsObject()) { | |
98 m_exceptionState.throwTypeError( | |
99 String::format("\"%s\" is not an object", name)); | |
100 return false; | |
101 } | |
102 callback = value.As<v8::Object>(); | |
103 if (!callback->IsCallable()) { | |
104 m_exceptionState.throwTypeError( | |
105 String::format("\"%s\" is not callable", name)); | |
106 return false; | |
107 } | |
108 return true; | |
109 } | |
110 | |
111 bool ScriptCustomElementDefinitionBuilder::cacheProperties() | |
112 { | |
113 const char* observedAttributes = "observedAttributes"; | |
114 v8::Local<v8::Value> observedAttributesValue; | |
115 if (!getValue(m_constructor, observedAttributes, observedAttributesValue)) | |
116 return false; | |
117 if (!observedAttributesValue->IsUndefined()) { | |
118 m_observedAttributes = toImplArray<Vector<AtomicString>>( | |
119 observedAttributesValue, 0, m_scriptState->isolate(), | |
120 m_exceptionState); | |
121 if (m_exceptionState.hadException()) | |
122 return false; | |
123 } | |
124 | |
125 const char* connectedCallback = "connectedCallback"; | |
126 const char* disconnectedCallback = "disconnectedCallback"; | |
127 const char* attributeChangedCallback = "attributeChangedCallback"; | |
128 return getCallable(connectedCallback, m_connectedCallback) | |
dominicc (has gone to gerrit)
2016/06/02 00:31:27
Please add unit tests which detach the window in c
| |
129 && getCallable(disconnectedCallback, m_disconnectedCallback) | |
130 && getCallable(attributeChangedCallback, m_attributeChangedCallback); | |
131 } | |
132 | |
85 CustomElementDefinition* ScriptCustomElementDefinitionBuilder::build( | 133 CustomElementDefinition* ScriptCustomElementDefinitionBuilder::build( |
86 const CustomElementDescriptor& descriptor) | 134 const CustomElementDescriptor& descriptor) |
87 { | 135 { |
88 return ScriptCustomElementDefinition::create( | 136 return ScriptCustomElementDefinition::create(descriptor, *this); |
89 m_scriptState.get(), | |
90 m_registry, | |
91 descriptor, | |
92 m_constructor, | |
93 m_prototype); | |
94 } | 137 } |
95 | 138 |
96 } // namespace blink | 139 } // namespace blink |
OLD | NEW |