#ifndef V8Proxy_h
#define V8Proxy_h
#include "ChromiumBridge.h"
#include "Node.h"
#include "NodeFilter.h"
#include "PlatformString.h" // for WebCore::String
#include "ScriptSourceCode.h" // for WebCore::ScriptSourceCode
#include "SecurityOrigin.h" // for WebCore::SecurityOrigin
#include "V8CustomBinding.h"
#include "V8DOMMap.h"
#include "V8DOMWrapper.h"
#include "V8EventListenerList.h"
#include "V8GCController.h"
#include "V8Index.h"
#include "V8Utilities.h"
#include <v8.h>
#include <wtf/Assertions.h>
#include <wtf/PassRefPtr.h> // so generated bindings don't have to
#include <wtf/Vector.h>
#include <iterator>
#include <list>
#ifdef ENABLE_DOM_STATS_COUNTERS
#define INC_STATS(name) ChromiumBridge::incrementStatsCounter(name)
#else
#define INC_STATS(name)
#endif
namespace WebCore {
class CSSRule;
class CSSRuleList;
class CSSStyleDeclaration;
class CSSValue;
class CSSValueList;
class ClientRectList;
class DOMImplementation;
class DOMWindow;
class Document;
class Element;
class Event;
class EventListener;
class EventTarget;
class Frame;
class HTMLCollection;
class HTMLDocument;
class HTMLElement;
class HTMLOptionsCollection;
class MediaList;
class MimeType;
class MimeTypeArray;
class NamedNodeMap;
class Navigator;
class Node;
class NodeFilter;
class NodeList;
class Plugin;
class PluginArray;
class SVGElement;
#if ENABLE(SVG)
class SVGElementInstance;
#endif
class Screen;
class ScriptExecutionContext;
#if ENABLE(DOM_STORAGE)
class Storage;
class StorageEvent;
#endif
class String;
class StyleSheet;
class StyleSheetList;
class V8EventListener;
class V8ObjectEventListener;
void logInfo(Frame*, const String& message, const String& url);
struct BatchedAttribute {
const char* const name;
v8::AccessorGetter getter;
v8::AccessorSetter setter;
V8ClassIndex::V8WrapperType data;
v8::AccessControl settings;
v8::PropertyAttribute attribute;
bool onProto;
};
void batchConfigureAttributes(v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::ObjectTemplate>, const BatchedAttribute*, size_t attributeCount);
struct BatchedConstant {
const char* const name;
int value;
};
void batchConfigureConstants(v8::Handle<v8::FunctionTemplate>, v8::Handle<v8::ObjectTemplate>, const BatchedConstant*, size_t constantCount);
const int kMaxRecursionDepth = 20;
struct V8ExtensionInfo {
String scheme;
v8::Extension* extension;
};
typedef std::list<V8ExtensionInfo> V8ExtensionList;
class V8Proxy {
public:
enum ErrorType {
RangeError,
ReferenceError,
SyntaxError,
TypeError,
GeneralError
};
explicit V8Proxy(Frame* frame) : m_frame(frame), m_domNodeMap(getDOMNodeMap()), m_inlineCode(false), m_timerCallback(false), m_recursion(0) { }
~V8Proxy();
Frame* frame() { return m_frame; }
void clearForNavigation();
void clearForClose();
void updateDocument();
void updateSecurityOrigin();
void destroyGlobal();
bool inlineCode() const { return m_inlineCode; }
void setInlineCode(bool value) { m_inlineCode = value; }
bool timerCallback() const { return m_timerCallback; }
void setTimerCallback(bool value) { m_timerCallback = value; }
bool isContextInitialized();
void disconnectFrame();
bool isEnabled();
PassRefPtr<V8EventListener> findV8EventListener(v8::Local<v8::Value> listener, bool isHtml);
PassRefPtr<V8EventListener> findOrCreateV8EventListener(v8::Local<v8::Value> listener, bool isHtml);
PassRefPtr<V8EventListener> findObjectEventListener(v8::Local<v8::Value> listener, bool isHtml);
PassRefPtr<V8EventListener> findOrCreateObjectEventListener(v8::Local<v8::Value> listener, bool isHtml);
void removeV8EventListener(V8EventListener*);
void removeObjectEventListener(V8ObjectEventListener*);
#if ENABLE(SVG)
static void setSVGContext(void*, SVGElement*);
static SVGElement* svgContext(void*);
#endif
void setEventHandlerLineNumber(int lineNumber) { m_handlerLineNumber = lineNumber; }
void finishedWithEvent(Event*) { }
void evaluateInNewWorld(const Vector<ScriptSourceCode>& sources);
void evaluateInNewContext(const Vector<ScriptSourceCode>&);
v8::Local<v8::Value> evaluate(const ScriptSourceCode&, Node*);
v8::Local<v8::Value> runScript(v8::Handle<v8::Script>, bool isInlineCode);
v8::Local<v8::Value> callFunction(v8::Handle<v8::Function>, v8::Handle<v8::Object>, int argc, v8::Handle<v8::Value> argv[]);
v8::Local<v8::Value> newInstance(v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]);
v8::Local<v8::Function> getConstructor(V8ClassIndex::V8WrapperType);
v8::Local<v8::Object> createWrapperFromCache(V8ClassIndex::V8WrapperType);
static DOMWindow* retrieveWindow();
static DOMWindow* retrieveWindow(v8::Handle<v8::Context>);
static V8Proxy* retrieve();
static V8Proxy* retrieve(Frame*);
static V8Proxy* retrieve(ScriptExecutionContext*);
static Frame* retrieveFrame();
static Frame* retrieveFrame(v8::Handle<v8::Context>);
static Frame* retrieveFrameForEnteredContext();
static Frame* retrieveFrameForCurrentContext();
static Frame* retrieveFrameForCallingContext();
static v8::Local<v8::Context> context(Frame*);
static v8::Local<v8::Context> currentContext();
static bool handleOutOfMemory();
static bool canAccessFrame(Frame*, bool reportError);
static bool checkNodeSecurity(Node*);
static v8::Handle<v8::Value> checkNewLegal(const v8::Arguments&);
static v8::Handle<v8::Script> compileScript(v8::Handle<v8::String> code, const String& fileName, int baseLine);
static void setDOMException(int exceptionCode);
static v8::Handle<v8::Value> throwError(ErrorType, const char* message);
static void bindJsObjectToWindow(Frame*, const char* name, int type, v8::Handle<v8::FunctionTemplate>, void*);
template <int tag, typename T>
static v8::Handle<v8::Value> constructDOMObject(const v8::Arguments&);
static void processConsoleMessages();
static int sourceLineNumber();
static String sourceName();
v8::Local<v8::Context> context()
{
return v8::Local<v8::Context>::New(m_context);
}
bool setContextDebugId(int id);
static int contextDebugId(v8::Handle<v8::Context>);
static void registerExtension(v8::Extension*, const String& schemeRestriction);
v8::Persistent<v8::Context> createNewContext(v8::Handle<v8::Object> global);
bool installDOMWindow(v8::Handle<v8::Context> context, DOMWindow* window);
void initContextIfNeeded();
DOMWrapperMap<Node>& domNodeMap() { return m_domNodeMap; }
void updateDocumentWrapper(v8::Handle<v8::Value> wrapper);
private:
static const char* kContextDebugDataType;
static const char* kContextDebugDataValue;
void disconnectEventListeners();
void setSecurityToken();
void clearDocumentWrapper();
void updateDocumentWrapperCache();
void clearDocumentWrapperCache();
void disposeContextHandles();
static bool canAccessPrivate(DOMWindow*);
static const char* rangeExceptionName(int exceptionCode);
static const char* eventExceptionName(int exceptionCode);
static const char* xmlHttpRequestExceptionName(int exceptionCode);
static const char* domExceptionName(int exceptionCode);
#if ENABLE(XPATH)
static const char* xpathExceptionName(int exceptionCode);
#endif
#if ENABLE(SVG)
static const char* svgExceptionName(int exceptionCode);
#endif
static void createUtilityContext();
static v8::Local<v8::Context> utilityContext()
{
if (m_utilityContext.IsEmpty())
createUtilityContext();
return v8::Local<v8::Context>::New(m_utilityContext);
}
Frame* m_frame;
v8::Persistent<v8::Context> m_context;
v8::Persistent<v8::Array> m_wrapperBoilerplates;
v8::Persistent<v8::Value> m_objectPrototype;
v8::Persistent<v8::Object> m_global;
v8::Persistent<v8::Value> m_document;
DOMWrapperMap<Node>& m_domNodeMap;
static v8::Persistent<v8::Context> m_utilityContext;
int m_handlerLineNumber;
V8EventListenerList m_eventListeners;
V8EventListenerList m_xhrListeners;
bool m_inlineCode;
bool m_timerCallback;
int m_recursion;
static V8ExtensionList m_extensions;
};
template <int tag, typename T>
v8::Handle<v8::Value> V8Proxy::constructDOMObject(const v8::Arguments& args)
{
if (!args.IsConstructCall())
return throwError(V8Proxy::TypeError, "DOM object constructor cannot be called as a function.");
RefPtr<T> obj = T::create();
V8DOMWrapper::setDOMWrapper(args.Holder(), tag, obj.get());
obj->ref();
V8DOMWrapper::setJSWrapperForDOMObject(obj.get(), v8::Persistent<v8::Object>::New(args.Holder()));
return args.Holder();
}
inline static v8::Local<v8::Object> notHandledByInterceptor()
{
return v8::Local<v8::Object>();
}
inline static v8::Local<v8::Boolean> deletionNotHandledByInterceptor()
{
return v8::Local<v8::Boolean>();
}
inline v8::Handle<v8::Primitive> throwError(const char* message, V8Proxy::ErrorType type = V8Proxy::TypeError)
{
V8Proxy::throwError(type, message);
return v8::Undefined();
}
inline v8::Handle<v8::Primitive> throwError(ExceptionCode ec)
{
V8Proxy::setDOMException(ec);
return v8::Undefined();
}
inline v8::Handle<v8::Primitive> throwError(v8::Local<v8::Value> exception)
{
v8::ThrowException(exception);
return v8::Undefined();
}
template <class T> inline v8::Handle<v8::Object> toV8(PassRefPtr<T> object, v8::Local<v8::Object> holder)
{
object->ref();
V8DOMWrapper::setJSWrapperForDOMObject(object.get(), v8::Persistent<v8::Object>::New(holder));
return holder;
}
}
#endif // V8Proxy_h