WebKitAutomationSession.cpp [plain text]
#include "config.h"
#include "WebKitAutomationSession.h"
#include "APIAutomationSessionClient.h"
#include "WebKitAutomationSessionPrivate.h"
#include "WebKitWebViewPrivate.h"
#include <glib/gi18n-lib.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
using namespace WebKit;
enum {
PROP_0,
PROP_ID
};
enum {
CREATE_WEB_VIEW,
LAST_SIGNAL
};
struct _WebKitAutomationSessionPrivate {
RefPtr<WebAutomationSession> session;
CString id;
};
static guint signals[LAST_SIGNAL] = { 0, };
WEBKIT_DEFINE_TYPE(WebKitAutomationSession, webkit_automation_session, G_TYPE_OBJECT)
class AutomationSessionClient final : public API::AutomationSessionClient {
public:
explicit AutomationSessionClient(WebKitAutomationSession* session)
: m_session(session)
{
}
private:
String sessionIdentifier() const override
{
return String::fromUTF8(m_session->priv->id.data());
}
WebPageProxy* didRequestNewWindow(WebAutomationSession&) override
{
WebKitWebView* webView = nullptr;
g_signal_emit(m_session, signals[CREATE_WEB_VIEW], 0, &webView);
if (!webView || !webkit_web_view_is_controlled_by_automation(webView))
return nullptr;
return &webkitWebViewGetPage(webView);
}
WebKitAutomationSession* m_session;
};
static void webkitAutomationSessionGetProperty(GObject* object, guint propID, GValue* value, GParamSpec* paramSpec)
{
WebKitAutomationSession* session = WEBKIT_AUTOMATION_SESSION(object);
switch (propID) {
case PROP_ID:
g_value_set_string(value, session->priv->id.data());
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void webkitAutomationSessionSetProperty(GObject* object, guint propID, const GValue* value, GParamSpec* paramSpec)
{
WebKitAutomationSession* session = WEBKIT_AUTOMATION_SESSION(object);
switch (propID) {
case PROP_ID:
session->priv->id = g_value_get_string(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void webkitAutomationSessionConstructed(GObject* object)
{
WebKitAutomationSession* session = WEBKIT_AUTOMATION_SESSION(object);
G_OBJECT_CLASS(webkit_automation_session_parent_class)->constructed(object);
session->priv->session = adoptRef(new WebAutomationSession());
session->priv->session->setSessionIdentifier(String::fromUTF8(session->priv->id.data()));
session->priv->session->setClient(std::make_unique<AutomationSessionClient>(session));
}
static void webkit_automation_session_class_init(WebKitAutomationSessionClass* sessionClass)
{
GObjectClass* gObjectClass = G_OBJECT_CLASS(sessionClass);
gObjectClass->get_property = webkitAutomationSessionGetProperty;
gObjectClass->set_property = webkitAutomationSessionSetProperty;
gObjectClass->constructed = webkitAutomationSessionConstructed;
g_object_class_install_property(
gObjectClass,
PROP_ID,
g_param_spec_string(
"id",
_("Identifier"),
_("The automation session identifier"),
nullptr,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
signals[CREATE_WEB_VIEW] = g_signal_new(
"create-web-view",
G_TYPE_FROM_CLASS(sessionClass),
G_SIGNAL_RUN_LAST,
0,
nullptr, nullptr,
g_cclosure_marshal_generic,
WEBKIT_TYPE_WEB_VIEW, 0,
G_TYPE_NONE);
}
WebKitAutomationSession* webkitAutomationSessionCreate(const char* sessionID)
{
return WEBKIT_AUTOMATION_SESSION(g_object_new(WEBKIT_TYPE_AUTOMATION_SESSION, "id", sessionID, nullptr));
}
WebAutomationSession& webkitAutomationSessionGetSession(WebKitAutomationSession* session)
{
return *session->priv->session;
}
const char* webkit_automation_session_get_id(WebKitAutomationSession* session)
{
g_return_val_if_fail(WEBKIT_IS_AUTOMATION_SESSION(session), nullptr);
return session->priv->id.data();
}