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 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
34 | |
35 V8PrivateProperty::Symbol symbol = | |
36 V8PrivateProperty::getCustomElementRegistryMap(isolate); | |
35 v8::Local<v8::Object> wrapper = ToV8(registry, scriptState).As<v8::Object>(); | 37 v8::Local<v8::Object> wrapper = ToV8(registry, scriptState).As<v8::Object>(); |
36 v8::Local<v8::Value> map = | 38 v8::Local<v8::Value> map = symbol.get(context, wrapper); |
37 V8HiddenValue::getHiddenValue(scriptState, wrapper, name); | |
38 if (map.IsEmpty()) { | 39 if (map.IsEmpty()) { |
39 map = v8::Map::New(scriptState->isolate()); | 40 map = v8::Map::New(isolate); |
40 V8HiddenValue::setHiddenValue(scriptState, wrapper, name, map); | 41 symbol.set(context, wrapper, map); |
41 } | 42 } |
42 return map.As<v8::Map>(); | 43 return map.As<v8::Map>(); |
43 } | 44 } |
44 | 45 |
45 ScriptCustomElementDefinition* ScriptCustomElementDefinition::forConstructor( | 46 ScriptCustomElementDefinition* ScriptCustomElementDefinition::forConstructor( |
46 ScriptState* scriptState, | 47 ScriptState* scriptState, |
47 CustomElementRegistry* registry, | 48 CustomElementRegistry* registry, |
48 const v8::Local<v8::Value>& constructor) { | 49 const v8::Local<v8::Value>& constructor) { |
49 v8::Local<v8::Map> map = | 50 v8::Local<v8::Map> map = |
50 ensureCustomElementRegistryMap(scriptState, registry); | 51 ensureCustomElementRegistryMap(scriptState, registry); |
51 v8::Local<v8::Value> nameValue = | 52 v8::Local<v8::Value> nameValue = |
52 map->Get(scriptState->context(), constructor).ToLocalChecked(); | 53 map->Get(scriptState->context(), constructor).ToLocalChecked(); |
53 if (!nameValue->IsString()) | 54 if (!nameValue->IsString()) |
54 return nullptr; | 55 return nullptr; |
55 AtomicString name = toCoreAtomicString(nameValue.As<v8::String>()); | 56 AtomicString name = toCoreAtomicString(nameValue.As<v8::String>()); |
56 | 57 |
57 // This downcast is safe because only | 58 // This downcast is safe because only |
58 // ScriptCustomElementDefinitions have a name associated with a V8 | 59 // ScriptCustomElementDefinitions have a name associated with a V8 |
59 // constructor in the map; see | 60 // constructor in the map; see |
60 // ScriptCustomElementDefinition::create. This relies on three | 61 // ScriptCustomElementDefinition::create. This relies on three |
61 // things: | 62 // things: |
62 // | 63 // |
63 // 1. Only ScriptCustomElementDefinition adds entries to the map. | 64 // 1. Only ScriptCustomElementDefinition adds entries to the map. |
64 // Audit the use of V8HiddenValue/hidden values in general and | 65 // Audit the use of hidden values in general and how the map is |
Yuki
2017/03/29 12:36:05
nit: s/hidden values/private properties/
peria
2017/03/30 09:30:16
Done.
| |
65 // how the map is handled--it should never be leaked to script. | 66 // handled--it should never be leaked to script. |
66 // | 67 // |
67 // 2. CustomElementRegistry does not overwrite definitions with a | 68 // 2. CustomElementRegistry does not overwrite definitions with a |
68 // given name--see the CHECK in CustomElementRegistry::define | 69 // given name--see the CHECK in CustomElementRegistry::define |
69 // --and adds ScriptCustomElementDefinitions to the map without | 70 // --and adds ScriptCustomElementDefinitions to the map without |
70 // fail. | 71 // fail. |
71 // | 72 // |
72 // 3. The relationship between the CustomElementRegistry and its | 73 // 3. The relationship between the CustomElementRegistry and its |
73 // map is never mixed up; this is guaranteed by the bindings | 74 // map is never mixed up; this is guaranteed by the bindings |
74 // system which provides a stable wrapper, and the map hangs | 75 // system which provides a stable wrapper, and the map hangs |
75 // off the wrapper. | 76 // off the wrapper. |
(...skipping 287 matching lines...) Loading... | |
363 v8::Local<v8::Value> argv[] = { | 364 v8::Local<v8::Value> argv[] = { |
364 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), | 365 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), |
365 v8StringOrNull(isolate, newValue), | 366 v8StringOrNull(isolate, newValue), |
366 v8StringOrNull(isolate, name.namespaceURI()), | 367 v8StringOrNull(isolate, name.namespaceURI()), |
367 }; | 368 }; |
368 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 369 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
369 WTF_ARRAY_LENGTH(argv), argv); | 370 WTF_ARRAY_LENGTH(argv), argv); |
370 } | 371 } |
371 | 372 |
372 } // namespace blink | 373 } // namespace blink |
OLD | NEW |