Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp

Issue 1985623002: [WIP] Custom element upgrades (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a C++-only test.' Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/V8CustomElementsRegistry.h" 10 #include "bindings/core/v8/V8CustomElementsRegistry.h"
11 #include "bindings/core/v8/V8Element.h"
10 #include "bindings/core/v8/V8HiddenValue.h" 12 #include "bindings/core/v8/V8HiddenValue.h"
13 #include "bindings/core/v8/V8ScriptRunner.h"
14 #include "v8.h"
11 #include "wtf/Allocator.h" 15 #include "wtf/Allocator.h"
12 16
13 namespace blink { 17 namespace blink {
14 18
15 // Custom elements stores state off the registry's wrapper. To avoid leaking 19 // Custom elements stores state off the registry's wrapper. To avoid leaking
16 // contexts, that state should be manipulated in the registry's wrapper 20 // contexts, that state should be manipulated in the registry's wrapper
17 // context. This helper retrieves the wrapper and enters its context. 21 // context. This helper retrieves the wrapper and enters its context.
18 // 22 //
19 // For example if there are two same-origin frames and script in frame 23 // For example if there are two same-origin frames and script in frame
20 // A does frameB.contentWindow.customElements.define the map should be 24 // A does frameB.contentWindow.customElements.define the map should be
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 150
147 return definition; 151 return definition;
148 } 152 }
149 153
150 ScriptCustomElementDefinition::ScriptCustomElementDefinition( 154 ScriptCustomElementDefinition::ScriptCustomElementDefinition(
151 ScriptState* scriptState, 155 ScriptState* scriptState,
152 const CustomElementDescriptor& descriptor, 156 const CustomElementDescriptor& descriptor,
153 const v8::Local<v8::Object>& constructor, 157 const v8::Local<v8::Object>& constructor,
154 const v8::Local<v8::Object>& prototype) 158 const v8::Local<v8::Object>& prototype)
155 : CustomElementDefinition(descriptor) 159 : CustomElementDefinition(descriptor)
160 , m_scriptState(scriptState)
156 , m_constructor(scriptState->isolate(), constructor) 161 , m_constructor(scriptState->isolate(), constructor)
157 , m_prototype(scriptState->isolate(), prototype) 162 , m_prototype(scriptState->isolate(), prototype)
158 { 163 {
159 // These objects are kept alive by references from the 164 // These objects are kept alive by references from the
160 // CustomElementsRegistry wrapper set up by 165 // CustomElementsRegistry wrapper set up by
161 // ScriptCustomElementDefinition::create. 166 // ScriptCustomElementDefinition::create.
162 m_constructor.setPhantom(); 167 m_constructor.setPhantom();
163 m_prototype.setPhantom(); 168 m_prototype.setPhantom();
164 } 169 }
165 170
171 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades
172 bool ScriptCustomElementDefinition::upgrade(Element* element)
173 {
174 if (!m_scriptState->contextIsValid())
175 return false;
176 ScriptState::Scope scope(m_scriptState.get());
177 v8::Isolate* isolate = m_scriptState->isolate();
178
179 // Step 5 says to rethrow the exception; but there is no one to
180 // catch it. The side effect is to report the error.
181 v8::TryCatch tryCatch(isolate);
182 tryCatch.SetVerbose(true);
183
184 ExecutionContext* executionContext = m_scriptState->getExecutionContext();
185 v8::Local<v8::Object> ctor = constructor(m_scriptState.get());
186 v8::Local<v8::Value> result;
187 if (!v8Call(V8ScriptRunner::callAsConstructor(
188 isolate,
189 ctor,
190 executionContext,
191 0,
192 nullptr),
193 result))
194 return false;
195
196 if (V8Element::toImplWithTypeCheck(isolate, result) != element) {
197 // TODO(dominicc): Throw InvalidStateError. Maybe Upgrade should
198 // take ExceptionState&?
199 return false;
200 }
201
202 return true;
203 }
204
166 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor( 205 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor(
167 ScriptState* scriptState) const 206 ScriptState* scriptState) const
168 { 207 {
169 DCHECK(!m_constructor.isEmpty()); 208 DCHECK(!m_constructor.isEmpty());
170 return m_constructor.newLocal(scriptState->isolate()); 209 return m_constructor.newLocal(scriptState->isolate());
171 } 210 }
172 211
173 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype( 212 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype(
174 ScriptState* scriptState) const 213 ScriptState* scriptState) const
175 { 214 {
176 DCHECK(!m_prototype.isEmpty()); 215 DCHECK(!m_prototype.isEmpty());
177 return m_prototype.newLocal(scriptState->isolate()); 216 return m_prototype.newLocal(scriptState->isolate());
178 } 217 }
179 218
180 } // namespace blink 219 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698