2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/v8/V8PerContextData.h"
34 #include "bindings/v8/ScriptState.h"
35 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/V8ObjectConstructor.h"
37 #include "wtf/StringExtras.h"
43 V8PerContextData::V8PerContextData(v8::Handle<v8::Context> context)
44 : m_wrapperBoilerplates(context->GetIsolate())
45 , m_constructorMap(context->GetIsolate())
46 , m_isolate(context->GetIsolate())
47 , m_contextHolder(adoptPtr(new gin::ContextHolder(context->GetIsolate())))
48 , m_context(m_isolate, context)
49 , m_customElementBindings(adoptPtr(new CustomElementBindingMap()))
51 m_contextHolder->SetContext(context);
53 v8::Context::Scope contextScope(context);
54 ASSERT(m_errorPrototype.isEmpty());
55 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(context->Global()->Get(v8AtomicString(m_isolate, "Error")));
56 ASSERT(!object.IsEmpty());
57 v8::Handle<v8::Value> prototypeValue = object->Get(v8AtomicString(m_isolate, "prototype"));
58 ASSERT(!prototypeValue.IsEmpty());
59 m_errorPrototype.set(m_isolate, prototypeValue);
62 V8PerContextData::~V8PerContextData()
66 PassOwnPtr<V8PerContextData> V8PerContextData::create(v8::Handle<v8::Context> context)
68 return adoptPtr(new V8PerContextData(context));
71 V8PerContextData* V8PerContextData::from(v8::Handle<v8::Context> context)
73 return ScriptState::from(context)->perContextData();
76 v8::Local<v8::Object> V8PerContextData::createWrapperFromCacheSlowCase(const WrapperTypeInfo* type)
78 ASSERT(!m_errorPrototype.isEmpty());
80 v8::Context::Scope scope(context());
81 v8::Local<v8::Function> function = constructorForType(type);
82 v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(m_isolate, function);
83 if (!instanceTemplate.IsEmpty()) {
84 m_wrapperBoilerplates.Set(type, instanceTemplate);
85 return instanceTemplate->Clone();
87 return v8::Local<v8::Object>();
90 v8::Local<v8::Function> V8PerContextData::constructorForTypeSlowCase(const WrapperTypeInfo* type)
92 ASSERT(!m_errorPrototype.isEmpty());
94 v8::Context::Scope scope(context());
95 v8::Handle<v8::FunctionTemplate> functionTemplate = type->domTemplate(m_isolate);
96 // Getting the function might fail if we're running out of stack or memory.
97 v8::TryCatch tryCatch;
98 v8::Local<v8::Function> function = functionTemplate->GetFunction();
99 if (function.IsEmpty())
100 return v8::Local<v8::Function>();
102 if (type->parentClass) {
103 v8::Local<v8::Object> prototypeTemplate = constructorForType(type->parentClass);
104 if (prototypeTemplate.IsEmpty())
105 return v8::Local<v8::Function>();
106 function->SetPrototype(prototypeTemplate);
109 v8::Local<v8::Value> prototypeValue = function->Get(v8AtomicString(m_isolate, "prototype"));
110 if (!prototypeValue.IsEmpty() && prototypeValue->IsObject()) {
111 v8::Local<v8::Object> prototypeObject = v8::Local<v8::Object>::Cast(prototypeValue);
112 if (prototypeObject->InternalFieldCount() == v8PrototypeInternalFieldcount
113 && type->wrapperTypePrototype == WrapperTypeObjectPrototype)
114 prototypeObject->SetAlignedPointerInInternalField(v8PrototypeTypeIndex, const_cast<WrapperTypeInfo*>(type));
115 type->installPerContextEnabledMethods(prototypeObject, m_isolate);
116 if (type->wrapperTypePrototype == WrapperTypeExceptionPrototype)
117 prototypeObject->SetPrototype(m_errorPrototype.newLocal(m_isolate));
120 m_constructorMap.Set(type, function);
125 v8::Local<v8::Object> V8PerContextData::prototypeForType(const WrapperTypeInfo* type)
127 v8::Local<v8::Object> constructor = constructorForType(type);
128 if (constructor.IsEmpty())
129 return v8::Local<v8::Object>();
130 return constructor->Get(v8String(m_isolate, "prototype")).As<v8::Object>();
133 void V8PerContextData::addCustomElementBinding(CustomElementDefinition* definition, PassOwnPtr<CustomElementBinding> binding)
135 ASSERT(!m_customElementBindings->contains(definition));
136 m_customElementBindings->add(definition, binding);
139 void V8PerContextData::clearCustomElementBinding(CustomElementDefinition* definition)
141 CustomElementBindingMap::iterator it = m_customElementBindings->find(definition);
142 ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
143 m_customElementBindings->remove(it);
146 CustomElementBinding* V8PerContextData::customElementBinding(CustomElementDefinition* definition)
148 CustomElementBindingMap::const_iterator it = m_customElementBindings->find(definition);
149 ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
150 return it->value.get();
154 static v8::Handle<v8::Value> createDebugData(const char* worldName, int debugId, v8::Isolate* isolate)
159 wanted = snprintf(buffer, sizeof(buffer), "%s", worldName);
161 wanted = snprintf(buffer, sizeof(buffer), "%s,%d", worldName, debugId);
163 if (wanted < sizeof(buffer))
164 return v8AtomicString(isolate, buffer);
166 return v8::Undefined(isolate);
169 static v8::Handle<v8::Value> debugData(v8::Handle<v8::Context> context)
171 v8::Context::Scope contextScope(context);
172 return context->GetEmbedderData(v8ContextDebugIdIndex);
175 static void setDebugData(v8::Handle<v8::Context> context, v8::Handle<v8::Value> value)
177 v8::Context::Scope contextScope(context);
178 context->SetEmbedderData(v8ContextDebugIdIndex, value);
181 bool V8PerContextDebugData::setContextDebugData(v8::Handle<v8::Context> context, const char* worldName, int debugId)
183 if (!debugData(context)->IsUndefined())
185 v8::HandleScope scope(context->GetIsolate());
186 v8::Handle<v8::Value> debugData = createDebugData(worldName, debugId, context->GetIsolate());
187 setDebugData(context, debugData);
191 int V8PerContextDebugData::contextDebugId(v8::Handle<v8::Context> context)
193 v8::HandleScope scope(context->GetIsolate());
194 v8::Handle<v8::Value> data = debugData(context);
196 if (!data->IsString())
198 v8::String::Utf8Value utf8(data);
199 char* comma = strnstr(*utf8, ",", utf8.length());
202 return atoi(comma + 1);
205 } // namespace WebCore