blob: 25d6e29580d220ac585f40d85e34bafe9696694d [file] [log] [blame]
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef DartJsInterop_h
#define DartJsInterop_h
#include "bindings/core/dart/DartDOMData.h"
#include "wtf/RefCounted.h"
#include "wtf/Vector.h"
#include <dart_api.h>
#include <dart_mirrors_api.h>
#include <v8.h>
namespace blink {
class LocalDOMWindow;
class Node;
class Element;
struct InteropPatchFile {
String libraryUri;
String patchFileUri;
String source;
};
class JsInterop {
public:
static Dart_NativeFunction resolver(Dart_Handle nameHandle, int argumentCount, bool* autoSetupScope);
static const uint8_t* symbolizer(Dart_NativeFunction);
static v8::Local<v8::Value> fromDart(DartDOMData*, Dart_Handle, Dart_Handle& exception);
static v8::Local<v8::Value> fromDartTyped(DartDOMData*, Dart_Handle, Dart_Handle& exception);
static Dart_Handle toDart(v8::Local<v8::Value>, DartDOMData*, bool legacy);
static Dart_Handle toDartTyped(v8::Local<v8::Value>, DartDOMData*);
static Dart_Handle toDartProbablyNative(v8::Local<v8::Value>);
static void buildInteropPatchFiles(DartDOMData*, Vector<InteropPatchFile>* patches, Dart_Handle& exception);
static void initializeJsInterop(DartDOMData*, const Vector<InteropPatchFile>& patches, Dart_Handle& exception);
};
class JsObject : public RefCounted<JsObject> {
WTF_MAKE_NONCOPYABLE(JsObject);
private:
JsObject(v8::Local<v8::Object> v8Handle, v8::Isolate*);
public:
static Dart_Handle toDart(v8::Local<v8::Object>, DartDOMData*, bool legacy);
static Dart_Handle toDart(v8::Local<v8::Object>, DartDOMData*, InteropDartHandles* cache, bool legacy);
static Dart_Handle toDartTyped(v8::Local<v8::Object>, DartDOMData*);
static Dart_Handle toDartTyped(v8::Local<v8::Object>, DartDOMData*, InterceptorData*);
static Dart_Handle toDartTyped(v8::Local<v8::Object>, DartDOMData*, InteropDartHandles* cache);
static Dart_Handle toDartTyped(v8::Local<v8::Object>, DartDOMData*, InterceptorData*, InteropDartHandles* cache);
static Dart_Handle toDart(PassRefPtr<JsObject>, DartDOMData*);
static InterceptorData* computeInterceptor(v8::Local<v8::Object>, DartDOMData*, InteropDartHandles* cache);
~JsObject();
static PassRefPtr<JsObject> create(v8::Local<v8::Object> v8Handle, v8::Isolate*);
static Dart_Handle createTyped(v8::Local<v8::Object>, DartDOMData*, InterceptorData*, InteropDartHandles* cache);
v8::Local<v8::Object> localV8Object(v8::Isolate*);
Node* toNode(v8::Isolate*);
Element* toElement(v8::Isolate*);
static const int dartClassId;
static const bool isNode = false;
static const bool isEventTarget = false;
static const bool isActive = false;
static const bool isGarbageCollected = false;
typedef JsObject NativeType;
friend class JsFunction;
friend class JsArray;
friend class JSFunction;
friend class JSArray;
private:
v8::Persistent<v8::Object> v8Object;
// If this JsObject happens to be a Node, m_cachedNode optionally stores
// the associated Node to improve DOM performance. We could get an
// additional 2X improvement in DOM performance if we took this a step
// further and allowed JsObject to somtimes have a valid Node handle
// but no set v8Object handle. The complexity hit for going with that
// approach would be significant as interceptors would need to be cached
// in multiple places.
Node* m_cachedNode;
};
class JsFunction : public JsObject {
WTF_MAKE_NONCOPYABLE(JsFunction);
private:
JsFunction(v8::Local<v8::Function> v8Handle, v8::Isolate*);
public:
static Dart_Handle toDart(PassRefPtr<JsFunction> jsObject);
static PassRefPtr<JsFunction> create(v8::Local<v8::Function> v8Handle, v8::Isolate*);
static const int dartClassId;
static const bool isNode = false;
static const bool isEventTarget = false;
static const bool isActive = false;
static const bool isGarbageCollected = false;
typedef JsFunction NativeType;
};
class JsArray : public JsObject {
WTF_MAKE_NONCOPYABLE(JsArray);
private:
JsArray(v8::Local<v8::Object> v8Handle, v8::Isolate*);
public:
static Dart_Handle toDart(PassRefPtr<JsArray> jsObject);
static PassRefPtr<JsArray> create(v8::Local<v8::Object> v8Handle, v8::Isolate*);
static const int dartClassId;
static const bool isNode = false;
static const bool isEventTarget = false;
static const bool isActive = false;
static const bool isGarbageCollected = false;
typedef JsArray NativeType;
};
// There isn't a JSObject class defined here as we have to determine the
// wrapper for JSObject subclasses more dynamically calling Dart to determine
// the correct subclass (all dart:html classes implement JSObject).
class JSFunction : public JsObject {
WTF_MAKE_NONCOPYABLE(JSFunction);
private:
JSFunction(v8::Local<v8::Function> v8Handle, v8::Isolate*);
public:
static Dart_Handle toDart(PassRefPtr<JSFunction> jsObject);
static PassRefPtr<JSFunction> create(v8::Local<v8::Function> v8Handle, v8::Isolate*);
static const int dartClassId;
static const bool isNode = false;
static const bool isEventTarget = false;
static const bool isActive = false;
static const bool isGarbageCollected = false;
typedef JSFunction NativeType;
};
class JSArray : public JsObject {
WTF_MAKE_NONCOPYABLE(JSArray);
private:
JSArray(v8::Local<v8::Object> v8Handle, v8::Isolate*);
public:
static Dart_Handle toDart(PassRefPtr<JSArray> jsObject);
static PassRefPtr<JSArray> create(v8::Local<v8::Object> v8Handle, v8::Isolate*);
static const int dartClassId;
static const bool isNode = false;
static const bool isEventTarget = false;
static const bool isActive = false;
static const bool isGarbageCollected = false;
typedef JSArray NativeType;
};
}
#endif // DartJsInterop_h