Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8PerContextData.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "bindings/v8/V8PerContextData.h"
33
34 #include "bindings/v8/ScriptState.h"
35 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/V8ObjectConstructor.h"
37 #include "wtf/StringExtras.h"
38
39 #include <stdlib.h>
40
41 namespace WebCore {
42
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()))
50 {
51     m_contextHolder->SetContext(context);
52
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);
60 }
61
62 V8PerContextData::~V8PerContextData()
63 {
64 }
65
66 PassOwnPtr<V8PerContextData> V8PerContextData::create(v8::Handle<v8::Context> context)
67 {
68     return adoptPtr(new V8PerContextData(context));
69 }
70
71 V8PerContextData* V8PerContextData::from(v8::Handle<v8::Context> context)
72 {
73     return ScriptState::from(context)->perContextData();
74 }
75
76 v8::Local<v8::Object> V8PerContextData::createWrapperFromCacheSlowCase(const WrapperTypeInfo* type)
77 {
78     ASSERT(!m_errorPrototype.isEmpty());
79
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();
86     }
87     return v8::Local<v8::Object>();
88 }
89
90 v8::Local<v8::Function> V8PerContextData::constructorForTypeSlowCase(const WrapperTypeInfo* type)
91 {
92     ASSERT(!m_errorPrototype.isEmpty());
93
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>();
101
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);
107     }
108
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));
118     }
119
120     m_constructorMap.Set(type, function);
121
122     return function;
123 }
124
125 v8::Local<v8::Object> V8PerContextData::prototypeForType(const WrapperTypeInfo* type)
126 {
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>();
131 }
132
133 void V8PerContextData::addCustomElementBinding(CustomElementDefinition* definition, PassOwnPtr<CustomElementBinding> binding)
134 {
135     ASSERT(!m_customElementBindings->contains(definition));
136     m_customElementBindings->add(definition, binding);
137 }
138
139 void V8PerContextData::clearCustomElementBinding(CustomElementDefinition* definition)
140 {
141     CustomElementBindingMap::iterator it = m_customElementBindings->find(definition);
142     ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
143     m_customElementBindings->remove(it);
144 }
145
146 CustomElementBinding* V8PerContextData::customElementBinding(CustomElementDefinition* definition)
147 {
148     CustomElementBindingMap::const_iterator it = m_customElementBindings->find(definition);
149     ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
150     return it->value.get();
151 }
152
153
154 static v8::Handle<v8::Value> createDebugData(const char* worldName, int debugId, v8::Isolate* isolate)
155 {
156     char buffer[32];
157     unsigned wanted;
158     if (debugId == -1)
159         wanted = snprintf(buffer, sizeof(buffer), "%s", worldName);
160     else
161         wanted = snprintf(buffer, sizeof(buffer), "%s,%d", worldName, debugId);
162
163     if (wanted < sizeof(buffer))
164         return v8AtomicString(isolate, buffer);
165
166     return v8::Undefined(isolate);
167 }
168
169 static v8::Handle<v8::Value> debugData(v8::Handle<v8::Context> context)
170 {
171     v8::Context::Scope contextScope(context);
172     return context->GetEmbedderData(v8ContextDebugIdIndex);
173 }
174
175 static void setDebugData(v8::Handle<v8::Context> context, v8::Handle<v8::Value> value)
176 {
177     v8::Context::Scope contextScope(context);
178     context->SetEmbedderData(v8ContextDebugIdIndex, value);
179 }
180
181 bool V8PerContextDebugData::setContextDebugData(v8::Handle<v8::Context> context, const char* worldName, int debugId)
182 {
183     if (!debugData(context)->IsUndefined())
184         return false;
185     v8::HandleScope scope(context->GetIsolate());
186     v8::Handle<v8::Value> debugData = createDebugData(worldName, debugId, context->GetIsolate());
187     setDebugData(context, debugData);
188     return true;
189 }
190
191 int V8PerContextDebugData::contextDebugId(v8::Handle<v8::Context> context)
192 {
193     v8::HandleScope scope(context->GetIsolate());
194     v8::Handle<v8::Value> data = debugData(context);
195
196     if (!data->IsString())
197         return -1;
198     v8::String::Utf8Value utf8(data);
199     char* comma = strnstr(*utf8, ",", utf8.length());
200     if (!comma)
201         return -1;
202     return atoi(comma + 1);
203 }
204
205 } // namespace WebCore