#include "config.h"
#include "WebKitFrame.h"
#include "WebKitDOMNodePrivate.h"
#include "WebKitFramePrivate.h"
#include "WebKitScriptWorldPrivate.h"
#include <JavaScriptCore/JSGlobalObjectInlines.h>
#include <JavaScriptCore/JSLock.h>
#include <WebCore/Frame.h>
#include <WebCore/JSNode.h>
#include <WebCore/ScriptController.h>
#include <jsc/JSCContextPrivate.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
using namespace WebKit;
using namespace WebCore;
struct _WebKitFramePrivate {
RefPtr<WebFrame> webFrame;
CString uri;
};
WEBKIT_DEFINE_TYPE(WebKitFrame, webkit_frame, G_TYPE_OBJECT)
static void webkit_frame_class_init(WebKitFrameClass*)
{
}
WebKitFrame* webkitFrameCreate(WebFrame* webFrame)
{
WebKitFrame* frame = WEBKIT_FRAME(g_object_new(WEBKIT_TYPE_FRAME, NULL));
frame->priv->webFrame = webFrame;
return frame;
}
WebFrame* webkitFrameGetWebFrame(WebKitFrame* frame)
{
return frame->priv->webFrame.get();
}
guint64 webkit_frame_get_id(WebKitFrame* frame)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), 0);
return frame->priv->webFrame->frameID().toUInt64();
}
gboolean webkit_frame_is_main_frame(WebKitFrame* frame)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), FALSE);
return frame->priv->webFrame->isMainFrame();
}
const gchar* webkit_frame_get_uri(WebKitFrame* frame)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), 0);
if (frame->priv->uri.isNull())
frame->priv->uri = frame->priv->webFrame->url().string().utf8();
return frame->priv->uri.data();
}
#if PLATFORM(GTK)
JSGlobalContextRef webkit_frame_get_javascript_global_context(WebKitFrame* frame)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), 0);
return frame->priv->webFrame->jsContext();
}
JSGlobalContextRef webkit_frame_get_javascript_context_for_script_world(WebKitFrame* frame, WebKitScriptWorld* world)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), 0);
g_return_val_if_fail(WEBKIT_IS_SCRIPT_WORLD(world), 0);
return frame->priv->webFrame->jsContextForWorld(webkitScriptWorldGetInjectedBundleScriptWorld(world));
}
#endif
JSCContext* webkit_frame_get_js_context(WebKitFrame* frame)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), nullptr);
return jscContextGetOrCreate(frame->priv->webFrame->jsContext()).leakRef();
}
JSCContext* webkit_frame_get_js_context_for_script_world(WebKitFrame* frame, WebKitScriptWorld* world)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), nullptr);
g_return_val_if_fail(WEBKIT_IS_SCRIPT_WORLD(world), nullptr);
return jscContextGetOrCreate(frame->priv->webFrame->jsContextForWorld(webkitScriptWorldGetInjectedBundleScriptWorld(world))).leakRef();
}
JSCValue* webkit_frame_get_js_value_for_dom_object(WebKitFrame* frame, WebKitDOMObject* domObject)
{
return webkit_frame_get_js_value_for_dom_object_in_script_world(frame, domObject, webkit_script_world_get_default());
}
JSCValue* webkit_frame_get_js_value_for_dom_object_in_script_world(WebKitFrame* frame, WebKitDOMObject* domObject, WebKitScriptWorld* world)
{
g_return_val_if_fail(WEBKIT_IS_FRAME(frame), nullptr);
g_return_val_if_fail(WEBKIT_DOM_IS_OBJECT(domObject), nullptr);
g_return_val_if_fail(WEBKIT_IS_SCRIPT_WORLD(world), nullptr);
auto* wkWorld = webkitScriptWorldGetInjectedBundleScriptWorld(world);
auto jsContext = jscContextGetOrCreate(frame->priv->webFrame->jsContextForWorld(wkWorld));
JSDOMWindow* globalObject = frame->priv->webFrame->coreFrame()->script().globalObject(wkWorld->coreWorld());
JSValueRef jsValue = nullptr;
{
JSC::JSLockHolder lock(globalObject);
if (WEBKIT_DOM_IS_NODE(domObject))
jsValue = toRef(globalObject, toJS(globalObject, globalObject, WebKit::core(WEBKIT_DOM_NODE(domObject))));
}
return jsValue ? jscContextGetOrCreateValue(jsContext.get(), jsValue).leakRef() : nullptr;
}