| 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/ScriptCustomElementDefinition.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8BindingMacros.h" | 9 #include "bindings/core/v8/V8BindingMacros.h" |
| 10 #include "bindings/core/v8/V8CustomElementRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementRegistry.h" |
| 11 #include "bindings/core/v8/V8Element.h" | 11 #include "bindings/core/v8/V8Element.h" |
| 12 #include "bindings/core/v8/V8ErrorHandler.h" | 12 #include "bindings/core/v8/V8ErrorHandler.h" |
| 13 #include "bindings/core/v8/V8HiddenValue.h" | |
| 14 #include "bindings/core/v8/V8PrivateProperty.h" | 13 #include "bindings/core/v8/V8PrivateProperty.h" |
| 15 #include "bindings/core/v8/V8ScriptRunner.h" | 14 #include "bindings/core/v8/V8ScriptRunner.h" |
| 16 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
| 17 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
| 18 #include "core/dom/custom/CustomElement.h" | 17 #include "core/dom/custom/CustomElement.h" |
| 19 #include "core/events/ErrorEvent.h" | 18 #include "core/events/ErrorEvent.h" |
| 20 #include "core/html/HTMLElement.h" | 19 #include "core/html/HTMLElement.h" |
| 21 #include "core/html/imports/HTMLImportsController.h" | 20 #include "core/html/imports/HTMLImportsController.h" |
| 22 #include "v8.h" | 21 #include "v8.h" |
| 23 #include "wtf/Allocator.h" | 22 #include "wtf/Allocator.h" |
| 24 | 23 |
| 25 namespace blink { | 24 namespace blink { |
| 26 | 25 |
| 27 // Retrieves the custom elements constructor -> name map, creating it | 26 // Retrieves the custom elements constructor -> name map, creating it |
| 28 // if necessary. | 27 // if necessary. |
| 29 static v8::Local<v8::Map> ensureCustomElementRegistryMap( | 28 static v8::Local<v8::Map> ensureCustomElementRegistryMap( |
| 30 ScriptState* scriptState, | 29 ScriptState* scriptState, |
| 31 CustomElementRegistry* registry) { | 30 CustomElementRegistry* registry) { |
| 32 CHECK(scriptState->world().isMainWorld()); | 31 CHECK(scriptState->world().isMainWorld()); |
| 33 v8::Local<v8::String> name = | 32 v8::Isolate* isolate = scriptState->isolate(); |
| 34 V8HiddenValue::customElementsRegistryMap(scriptState->isolate()); | 33 |
| 34 V8PrivateProperty::Symbol symbol = |
| 35 V8PrivateProperty::getCustomElementRegistryMap(isolate); |
| 35 v8::Local<v8::Object> wrapper = ToV8(registry, scriptState).As<v8::Object>(); | 36 v8::Local<v8::Object> wrapper = ToV8(registry, scriptState).As<v8::Object>(); |
| 36 v8::Local<v8::Value> map = | 37 v8::Local<v8::Value> map = symbol.getOrUndefined(wrapper); |
| 37 V8HiddenValue::getHiddenValue(scriptState, wrapper, name); | 38 if (map->IsUndefined()) { |
| 38 if (map.IsEmpty()) { | 39 map = v8::Map::New(isolate); |
| 39 map = v8::Map::New(scriptState->isolate()); | 40 symbol.set(wrapper, map); |
| 40 V8HiddenValue::setHiddenValue(scriptState, wrapper, name, map); | |
| 41 } | 41 } |
| 42 return map.As<v8::Map>(); | 42 return map.As<v8::Map>(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 ScriptCustomElementDefinition* ScriptCustomElementDefinition::forConstructor( | 45 ScriptCustomElementDefinition* ScriptCustomElementDefinition::forConstructor( |
| 46 ScriptState* scriptState, | 46 ScriptState* scriptState, |
| 47 CustomElementRegistry* registry, | 47 CustomElementRegistry* registry, |
| 48 const v8::Local<v8::Value>& constructor) { | 48 const v8::Local<v8::Value>& constructor) { |
| 49 v8::Local<v8::Map> map = | 49 v8::Local<v8::Map> map = |
| 50 ensureCustomElementRegistryMap(scriptState, registry); | 50 ensureCustomElementRegistryMap(scriptState, registry); |
| 51 v8::Local<v8::Value> nameValue = | 51 v8::Local<v8::Value> nameValue = |
| 52 map->Get(scriptState->context(), constructor).ToLocalChecked(); | 52 map->Get(scriptState->context(), constructor).ToLocalChecked(); |
| 53 if (!nameValue->IsString()) | 53 if (!nameValue->IsString()) |
| 54 return nullptr; | 54 return nullptr; |
| 55 AtomicString name = toCoreAtomicString(nameValue.As<v8::String>()); | 55 AtomicString name = toCoreAtomicString(nameValue.As<v8::String>()); |
| 56 | 56 |
| 57 // This downcast is safe because only | 57 // This downcast is safe because only |
| 58 // ScriptCustomElementDefinitions have a name associated with a V8 | 58 // ScriptCustomElementDefinitions have a name associated with a V8 |
| 59 // constructor in the map; see | 59 // constructor in the map; see |
| 60 // ScriptCustomElementDefinition::create. This relies on three | 60 // ScriptCustomElementDefinition::create. This relies on three |
| 61 // things: | 61 // things: |
| 62 // | 62 // |
| 63 // 1. Only ScriptCustomElementDefinition adds entries to the map. | 63 // 1. Only ScriptCustomElementDefinition adds entries to the map. |
| 64 // Audit the use of V8HiddenValue/hidden values in general and | 64 // Audit the use of private properties in general and how the |
| 65 // how the map is handled--it should never be leaked to script. | 65 // map is handled--it should never be leaked to script. |
| 66 // | 66 // |
| 67 // 2. CustomElementRegistry does not overwrite definitions with a | 67 // 2. CustomElementRegistry does not overwrite definitions with a |
| 68 // given name--see the CHECK in CustomElementRegistry::define | 68 // given name--see the CHECK in CustomElementRegistry::define |
| 69 // --and adds ScriptCustomElementDefinitions to the map without | 69 // --and adds ScriptCustomElementDefinitions to the map without |
| 70 // fail. | 70 // fail. |
| 71 // | 71 // |
| 72 // 3. The relationship between the CustomElementRegistry and its | 72 // 3. The relationship between the CustomElementRegistry and its |
| 73 // map is never mixed up; this is guaranteed by the bindings | 73 // map is never mixed up; this is guaranteed by the bindings |
| 74 // system which provides a stable wrapper, and the map hangs | 74 // system which provides a stable wrapper, and the map hangs |
| 75 // off the wrapper. | 75 // off the wrapper. |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 v8::Local<v8::Value> argv[] = { | 363 v8::Local<v8::Value> argv[] = { |
| 364 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), | 364 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), |
| 365 v8StringOrNull(isolate, newValue), | 365 v8StringOrNull(isolate, newValue), |
| 366 v8StringOrNull(isolate, name.namespaceURI()), | 366 v8StringOrNull(isolate, name.namespaceURI()), |
| 367 }; | 367 }; |
| 368 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 368 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
| 369 WTF_ARRAY_LENGTH(argv), argv); | 369 WTF_ARRAY_LENGTH(argv), argv); |
| 370 } | 370 } |
| 371 | 371 |
| 372 } // namespace blink | 372 } // namespace blink |
| OLD | NEW |