#ifndef V8PerContextData_h
#define V8PerContextData_h
#include "bindings/v8/CustomElementBinding.h"
#include "bindings/v8/ScopedPersistent.h"
#include "bindings/v8/UnsafePersistent.h"
#include "bindings/v8/V8DOMActivityLogger.h"
#include "bindings/v8/WrapperTypeInfo.h"
#include "gin/public/context_holder.h"
#include "gin/public/gin_embedders.h"
#include <v8.h>
#include "wtf/HashMap.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/Vector.h"
#include "wtf/text/AtomicString.h"
#include "wtf/text/AtomicStringHash.h"
namespace WebCore {
class CustomElementDefinition;
class DOMWrapperWorld;
class V8PerContextData;
struct V8NPObject;
typedef WTF::Vector<V8NPObject*> V8NPObjectVector;
typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap;
enum V8ContextEmbedderDataField {
v8ContextDebugIdIndex = static_cast<int>(gin::kDebugIdIndex),
v8ContextPerContextDataIndex = static_cast<int>(gin::kPerContextDataStartIndex + gin::kEmbedderBlink),
};
class V8PerContextData {
public:
static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context>, PassRefPtr<DOMWrapperWorld>);
static V8PerContextData* from(v8::Handle<v8::Context>);
~V8PerContextData();
v8::Handle<v8::Context> context() { return m_context.newLocal(m_isolate); }
v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type)
{
UnsafePersistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type);
return !boilerplate.isEmpty() ? boilerplate.newLocal(m_isolate)->Clone() : createWrapperFromCacheSlowCase(type);
}
v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type)
{
UnsafePersistent<v8::Function> function = m_constructorMap.get(type);
if (!function.isEmpty())
return function.newLocal(m_isolate);
return constructorForTypeSlowCase(type);
}
v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*);
V8NPObjectMap* v8NPObjectMap() { return &m_v8NPObjectMap; }
V8DOMActivityLogger* activityLogger() { return m_activityLogger; }
void setActivityLogger(V8DOMActivityLogger* logger) { m_activityLogger = logger; }
void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>);
void clearCustomElementBinding(CustomElementDefinition*);
CustomElementBinding* customElementBinding(CustomElementDefinition*);
private:
V8PerContextData(v8::Handle<v8::Context>, PassRefPtr<DOMWrapperWorld>);
v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*);
v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*);
typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Object> > WrapperBoilerplateMap;
WrapperBoilerplateMap m_wrapperBoilerplates;
typedef WTF::HashMap<const WrapperTypeInfo*, UnsafePersistent<v8::Function> > ConstructorMap;
ConstructorMap m_constructorMap;
V8NPObjectMap m_v8NPObjectMap;
V8DOMActivityLogger* m_activityLogger;
v8::Isolate* m_isolate;
OwnPtr<gin::ContextHolder> m_contextHolder;
ScopedPersistent<v8::Context> m_context;
ScopedPersistent<v8::Value> m_errorPrototype;
typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap;
OwnPtr<CustomElementBindingMap> m_customElementBindings;
};
class V8PerContextDebugData {
public:
static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
static int contextDebugId(v8::Handle<v8::Context>);
};
}
#endif