| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 const StringView& name, | 79 const StringView& name, |
| 80 v8::Local<v8::Value>& value) const { | 80 v8::Local<v8::Value>& value) const { |
| 81 v8::Isolate* isolate = script_state_->GetIsolate(); | 81 v8::Isolate* isolate = script_state_->GetIsolate(); |
| 82 v8::Local<v8::Context> context = script_state_->GetContext(); | 82 v8::Local<v8::Context> context = script_state_->GetContext(); |
| 83 v8::Local<v8::String> name_string = V8AtomicString(isolate, name); | 83 v8::Local<v8::String> name_string = V8AtomicString(isolate, name); |
| 84 v8::TryCatch try_catch(isolate); | 84 v8::TryCatch try_catch(isolate); |
| 85 if (!V8Call(object->Get(context, name_string), value, try_catch)) { | 85 if (!V8Call(object->Get(context, name_string), value, try_catch)) { |
| 86 exception_state_.RethrowV8Exception(try_catch.Exception()); | 86 exception_state_.RethrowV8Exception(try_catch.Exception()); |
| 87 return false; | 87 return false; |
| 88 } | 88 } |
| 89 return true; | 89 return script_state_->ContextIsValid(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 bool ScriptCustomElementDefinitionBuilder::CheckPrototype() { | 92 bool ScriptCustomElementDefinitionBuilder::CheckPrototype() { |
| 93 v8::Local<v8::Value> prototype_value; | 93 v8::Local<v8::Value> prototype_value; |
| 94 if (!ValueForName(constructor_, "prototype", prototype_value)) | 94 if (!ValueForName(constructor_, "prototype", prototype_value)) |
| 95 return false; | 95 return false; |
| 96 if (!prototype_value->IsObject()) { | 96 if (!prototype_value->IsObject()) { |
| 97 exception_state_.ThrowTypeError("constructor prototype is not an object"); | 97 exception_state_.ThrowTypeError("constructor prototype is not an object"); |
| 98 return false; | 98 return false; |
| 99 } | 99 } |
| 100 prototype_ = prototype_value.As<v8::Object>(); | 100 prototype_ = prototype_value.As<v8::Object>(); |
| 101 // If retrieving the prototype destroyed the context, indicate that | 101 // If retrieving the prototype destroyed the context, indicate that |
| 102 // defining the element should not proceed. | 102 // defining the element should not proceed. |
| 103 return true; | 103 return script_state_->ContextIsValid(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 bool ScriptCustomElementDefinitionBuilder::CallableForName( | 106 bool ScriptCustomElementDefinitionBuilder::CallableForName( |
| 107 const StringView& name, | 107 const StringView& name, |
| 108 v8::Local<v8::Function>& callback) const { | 108 v8::Local<v8::Function>& callback) const { |
| 109 v8::Local<v8::Value> value; | 109 v8::Local<v8::Value> value; |
| 110 if (!ValueForName(prototype_, name, value)) | 110 if (!ValueForName(prototype_, name, value)) |
| 111 return false; | 111 return false; |
| 112 // "undefined" means "omitted", so return true. | 112 // "undefined" means "omitted" which is valid. |
| 113 if (value->IsUndefined()) | 113 if (value->IsUndefined()) |
| 114 return true; | 114 return true; |
| 115 if (!value->IsFunction()) { | 115 if (!value->IsFunction()) { |
| 116 exception_state_.ThrowTypeError(String::Format( | 116 exception_state_.ThrowTypeError(String::Format( |
| 117 "\"%s\" is not a callable object", name.ToString().Ascii().Data())); | 117 "\"%s\" is not a callable object", name.ToString().Ascii().Data())); |
| 118 return false; | 118 return false; |
| 119 } | 119 } |
| 120 callback = value.As<v8::Function>(); | 120 callback = value.As<v8::Function>(); |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool ScriptCustomElementDefinitionBuilder::RetrieveObservedAttributes() { | 124 bool ScriptCustomElementDefinitionBuilder::RetrieveObservedAttributes() { |
| 125 v8::Local<v8::Value> observed_attributes_value; | 125 v8::Local<v8::Value> observed_attributes_value; |
| 126 if (!ValueForName(constructor_, "observedAttributes", | 126 if (!ValueForName(constructor_, "observedAttributes", |
| 127 observed_attributes_value)) | 127 observed_attributes_value)) |
| 128 return false; | 128 return false; |
| 129 if (observed_attributes_value->IsUndefined()) | 129 if (observed_attributes_value->IsUndefined()) |
| 130 return true; | 130 return true; |
| 131 Vector<AtomicString> list = ToImplSequence<Vector<AtomicString>>( | 131 Vector<AtomicString> list = ToImplSequence<Vector<AtomicString>>( |
| 132 script_state_->GetIsolate(), observed_attributes_value, exception_state_); | 132 script_state_->GetIsolate(), observed_attributes_value, exception_state_); |
| 133 if (exception_state_.HadException()) | 133 if (exception_state_.HadException() || !script_state_->ContextIsValid()) |
| 134 return false; | 134 return false; |
| 135 if (list.IsEmpty()) | 135 if (list.IsEmpty()) |
| 136 return true; | 136 return true; |
| 137 observed_attributes_.ReserveCapacityForSize(list.size()); | 137 observed_attributes_.ReserveCapacityForSize(list.size()); |
| 138 for (const auto& attribute : list) | 138 for (const auto& attribute : list) |
| 139 observed_attributes_.insert(attribute); | 139 observed_attributes_.insert(attribute); |
| 140 return true; | 140 return true; |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool ScriptCustomElementDefinitionBuilder::RememberOriginalProperties() { | 143 bool ScriptCustomElementDefinitionBuilder::RememberOriginalProperties() { |
| 144 // Spec requires to use values of these properties at the point | 144 // Spec requires to use values of these properties at the point |
| 145 // CustomElementDefinition is built, even if JS changes them afterwards. | 145 // CustomElementDefinition is built, even if JS changes them afterwards. |
| 146 return CallableForName("connectedCallback", connected_callback_) && | 146 return CallableForName("connectedCallback", connected_callback_) && |
| 147 CallableForName("disconnectedCallback", disconnected_callback_) && | 147 CallableForName("disconnectedCallback", disconnected_callback_) && |
| 148 CallableForName("adoptedCallback", adopted_callback_) && | 148 CallableForName("adoptedCallback", adopted_callback_) && |
| 149 CallableForName("attributeChangedCallback", | 149 CallableForName("attributeChangedCallback", |
| 150 attribute_changed_callback_) && | 150 attribute_changed_callback_) && |
| 151 (attribute_changed_callback_.IsEmpty() || | 151 (attribute_changed_callback_.IsEmpty() || |
| 152 RetrieveObservedAttributes()); | 152 RetrieveObservedAttributes()); |
| 153 } | 153 } |
| 154 | 154 |
| 155 CustomElementDefinition* ScriptCustomElementDefinitionBuilder::Build( | 155 CustomElementDefinition* ScriptCustomElementDefinitionBuilder::Build( |
| 156 const CustomElementDescriptor& descriptor) { | 156 const CustomElementDescriptor& descriptor, |
| 157 CustomElementDefinition::Id id) { |
| 157 return ScriptCustomElementDefinition::Create( | 158 return ScriptCustomElementDefinition::Create( |
| 158 script_state_.Get(), registry_, descriptor, constructor_, | 159 script_state_.Get(), registry_, descriptor, id, constructor_, |
| 159 connected_callback_, disconnected_callback_, adopted_callback_, | 160 connected_callback_, disconnected_callback_, adopted_callback_, |
| 160 attribute_changed_callback_, observed_attributes_); | 161 attribute_changed_callback_, observed_attributes_); |
| 161 } | 162 } |
| 162 | 163 |
| 163 } // namespace blink | 164 } // namespace blink |
| OLD | NEW |