1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/*
* Copyright (C) 2017 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitAutomationSession.h"
#include "APIAutomationSessionClient.h"
#include "WebKitApplicationInfo.h"
#include "WebKitAutomationSessionPrivate.h"
#include "WebKitNetworkProxySettingsPrivate.h"
#include "WebKitWebContextPrivate.h"
#include "WebKitWebViewPrivate.h"
#include "WebKitWebsiteDataManagerPrivate.h"
#include <glib/gi18n-lib.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
#if ENABLE(2022_GLIB_API)
#include "WebKitNetworkSession.h"
#endif
using namespace WebKit;
/**
* WebKitAutomationSession:
*
* Automation Session.
*
* WebKitAutomationSession represents an automation session of a WebKitWebContext.
* When a new session is requested, a WebKitAutomationSession is created and the signal
* WebKitWebContext::automation-started is emitted with the WebKitAutomationSession as
* argument. Then, the automation client can request the session to create a new
* #WebKitWebView to interact with it. When this happens the signal #WebKitAutomationSession::create-web-view
* is emitted.
*
* Since: 2.18
*/
enum {
PROP_0,
PROP_ID
};
enum {
CREATE_WEB_VIEW,
LAST_SIGNAL
};
struct _WebKitAutomationSessionPrivate {
RefPtr<WebAutomationSession> session;
WebKitApplicationInfo* applicationInfo;
WebKitWebContext* webContext;
CString id;
};
static guint signals[LAST_SIGNAL] = { 0, };
WEBKIT_DEFINE_FINAL_TYPE(WebKitAutomationSession, webkit_automation_session, G_TYPE_OBJECT, GObject)
class AutomationSessionClient final : public API::AutomationSessionClient {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit AutomationSessionClient(WebKitAutomationSession* session)
: m_session(session)
{
}
private:
String sessionIdentifier() const override
{
return String::fromUTF8(m_session->priv->id.data());
}
void didDisconnectFromRemote(WebAutomationSession&) override
{
#if ENABLE(REMOTE_INSPECTOR)
webkitWebContextWillCloseAutomationSession(m_session->priv->webContext);
#endif
}
void requestNewPageWithOptions(WebAutomationSession&, API::AutomationSessionBrowsingContextOptions options, CompletionHandler<void(WebPageProxy*)>&& completionHandler) override
{
WebKitWebView* webView = nullptr;
GQuark detail = options & API::AutomationSessionBrowsingContextOptionsPreferNewTab ? g_quark_from_string("tab") : g_quark_from_string("window");
g_signal_emit(m_session, signals[CREATE_WEB_VIEW], detail, &webView);
if (!webView || !webkit_web_view_is_controlled_by_automation(webView))
completionHandler(nullptr);
else
completionHandler(&webkitWebViewGetPage(webView));
}
void requestMaximizeWindowOfPage(WebAutomationSession&, WebPageProxy& page, CompletionHandler<void()>&& completionHandler) override
{
if (auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page))
webkitWebViewMaximizeWindow(webView, WTFMove(completionHandler));
else
completionHandler();
}
void requestHideWindowOfPage(WebAutomationSession&, WebPageProxy& page, CompletionHandler<void()>&& completionHandler) override
{
if (auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page))
webkitWebViewMinimizeWindow(webView, WTFMove(completionHandler));
else
completionHandler();
}
void requestRestoreWindowOfPage(WebAutomationSession&, WebPageProxy& page, CompletionHandler<void()>&& completionHandler) override
{
if (auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page))
webkitWebViewRestoreWindow(webView, WTFMove(completionHandler));
else
completionHandler();
}
bool isShowingJavaScriptDialogOnPage(WebAutomationSession&, WebPageProxy& page) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return false;
return webkitWebViewIsShowingScriptDialog(webView);
}
void dismissCurrentJavaScriptDialogOnPage(WebAutomationSession&, WebPageProxy& page) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return;
webkitWebViewDismissCurrentScriptDialog(webView);
}
void acceptCurrentJavaScriptDialogOnPage(WebAutomationSession&, WebPageProxy& page) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return;
webkitWebViewAcceptCurrentScriptDialog(webView);
}
String messageOfCurrentJavaScriptDialogOnPage(WebAutomationSession&, WebPageProxy& page) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return { };
return webkitWebViewGetCurrentScriptDialogMessage(webView);
}
void setUserInputForCurrentJavaScriptPromptOnPage(WebAutomationSession&, WebPageProxy& page, const String& userInput) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return;
webkitWebViewSetCurrentScriptDialogUserInput(webView, userInput);
}
std::optional<API::AutomationSessionClient::JavaScriptDialogType> typeOfCurrentJavaScriptDialogOnPage(WebAutomationSession&, WebPageProxy& page) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return std::nullopt;
auto dialogType = webkitWebViewGetCurrentScriptDialogType(webView);
if (!dialogType)
return std::nullopt;
switch (dialogType.value()) {
case WEBKIT_SCRIPT_DIALOG_ALERT:
return API::AutomationSessionClient::JavaScriptDialogType::Alert;
case WEBKIT_SCRIPT_DIALOG_CONFIRM:
return API::AutomationSessionClient::JavaScriptDialogType::Confirm;
case WEBKIT_SCRIPT_DIALOG_PROMPT:
return API::AutomationSessionClient::JavaScriptDialogType::Prompt;
case WEBKIT_SCRIPT_DIALOG_BEFORE_UNLOAD_CONFIRM:
return API::AutomationSessionClient::JavaScriptDialogType::BeforeUnloadConfirm;
}
ASSERT_NOT_REACHED();
return std::nullopt;
}
API::AutomationSessionClient::BrowsingContextPresentation currentPresentationOfPage(WebAutomationSession&, WebPageProxy& page) override
{
auto* webView = webkitWebContextGetWebViewForPage(m_session->priv->webContext, &page);
if (!webView)
return API::AutomationSessionClient::BrowsingContextPresentation::Window;
switch (webkit_web_view_get_automation_presentation_type(webView)) {
case WEBKIT_AUTOMATION_BROWSING_CONTEXT_PRESENTATION_WINDOW:
return API::AutomationSessionClient::BrowsingContextPresentation::Window;
case WEBKIT_AUTOMATION_BROWSING_CONTEXT_PRESENTATION_TAB:
return API::AutomationSessionClient::BrowsingContextPresentation::Tab;
}
RELEASE_ASSERT_NOT_REACHED();
}
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(makeUnique<AutomationSessionClient>(session));
}
static void webkitAutomationSessionDispose(GObject* object)
{
WebKitAutomationSession* session = WEBKIT_AUTOMATION_SESSION(object);
session->priv->session->setClient(nullptr);
if (session->priv->applicationInfo) {
webkit_application_info_unref(session->priv->applicationInfo);
session->priv->applicationInfo = nullptr;
}
G_OBJECT_CLASS(webkit_automation_session_parent_class)->dispose(object);
}
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;
gObjectClass->dispose = webkitAutomationSessionDispose;
/**
* WebKitAutomationSession:id:
*
* The session unique identifier.
*
* Since: 2.18
*/
g_object_class_install_property(
gObjectClass,
PROP_ID,
g_param_spec_string(
"id",
nullptr, nullptr,
nullptr,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
/**
* WebKitAutomationSession::create-web-view:
* @session: a #WebKitAutomationSession
*
* This signal is emitted when the automation client requests a new
* browsing context to interact with it. The callback handler should
* return a #WebKitWebView created with #WebKitWebView:is-controlled-by-automation
* construct property enabled and #WebKitWebView:automation-presentation-type construct
* property set if needed.
*
* If the signal is emitted with "tab" detail, the returned #WebKitWebView should be
* a new web view added to a new tab of the current browsing context window.
* If the signal is emitted with "window" detail, the returned #WebKitWebView should be
* a new web view added to a new window.
* When creating a new web view and there's an active browsing context, the new window
* or tab shouldn't be focused.
*
* Returns: (transfer none): a #WebKitWebView widget.
*
* Since: 2.18
*/
signals[CREATE_WEB_VIEW] = g_signal_new(
"create-web-view",
G_TYPE_FROM_CLASS(sessionClass),
static_cast<GSignalFlags>(G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
0,
nullptr, nullptr,
g_cclosure_marshal_generic,
WEBKIT_TYPE_WEB_VIEW, 0,
G_TYPE_NONE);
}
#if ENABLE(REMOTE_INSPECTOR)
static WebKitNetworkProxyMode parseProxyCapabilities(const Inspector::RemoteInspector::Client::SessionCapabilities::Proxy& proxy, WebKitNetworkProxySettings** settings)
{
if (proxy.type == "system"_s || proxy.type == "autodetect"_s)
return WEBKIT_NETWORK_PROXY_MODE_DEFAULT;
if (proxy.type == "direct"_s)
return WEBKIT_NETWORK_PROXY_MODE_NO_PROXY;
if (!proxy.ignoreAddressList.isEmpty()) {
GUniquePtr<char*> ignoreAddressList(static_cast<char**>(g_new0(char*, proxy.ignoreAddressList.size() + 1)));
unsigned i = 0;
for (const auto& ignoreAddress : proxy.ignoreAddressList)
ignoreAddressList.get()[i++] = g_strdup(ignoreAddress.utf8().data());
*settings = webkit_network_proxy_settings_new(nullptr, ignoreAddressList.get());
} else
*settings = webkit_network_proxy_settings_new(nullptr, nullptr);
if (proxy.ftpURL)
webkit_network_proxy_settings_add_proxy_for_scheme(*settings, "ftp", proxy.ftpURL->utf8().data());
if (proxy.httpURL)
webkit_network_proxy_settings_add_proxy_for_scheme(*settings, "http", proxy.httpURL->utf8().data());
if (proxy.httpsURL)
webkit_network_proxy_settings_add_proxy_for_scheme(*settings, "https", proxy.httpsURL->utf8().data());
if (proxy.socksURL)
webkit_network_proxy_settings_add_proxy_for_scheme(*settings, "socks", proxy.socksURL->utf8().data());
return WEBKIT_NETWORK_PROXY_MODE_CUSTOM;
}
WebKitAutomationSession* webkitAutomationSessionCreate(WebKitWebContext* webContext, const char* sessionID, const Inspector::RemoteInspector::Client::SessionCapabilities& capabilities)
{
auto* session = WEBKIT_AUTOMATION_SESSION(g_object_new(WEBKIT_TYPE_AUTOMATION_SESSION, "id", sessionID, nullptr));
session->priv->webContext = webContext;
#if ENABLE(2022_GLIB_API)
WebKitNetworkSession* networkSession = webkit_web_context_get_network_session_for_automation(webContext);
#endif
if (capabilities.acceptInsecureCertificates) {
#if ENABLE(2022_GLIB_API)
webkit_network_session_set_tls_errors_policy(networkSession, WEBKIT_TLS_ERRORS_POLICY_IGNORE);
#else
webkit_website_data_manager_set_tls_errors_policy(webkit_web_context_get_website_data_manager(webContext), WEBKIT_TLS_ERRORS_POLICY_IGNORE);
#endif
}
for (auto& certificate : capabilities.certificates) {
GRefPtr<GTlsCertificate> tlsCertificate = adoptGRef(g_tls_certificate_new_from_file(certificate.second.utf8().data(), nullptr));
if (tlsCertificate) {
#if ENABLE(2022_GLIB_API)
webkit_network_session_allow_tls_certificate_for_host(networkSession, tlsCertificate.get(), certificate.first.utf8().data());
#else
webkit_web_context_allow_tls_certificate_for_host(webContext, tlsCertificate.get(), certificate.first.utf8().data());
#endif
}
}
if (capabilities.proxy) {
if (capabilities.proxy->type == "pac"_s) {
// FIXME: expose pac proxy in public API.
auto settings = WebCore::SoupNetworkProxySettings(WebCore::SoupNetworkProxySettings::Mode::Auto);
if (capabilities.proxy->autoconfigURL)
settings.defaultProxyURL = capabilities.proxy->autoconfigURL->utf8();
if (!settings.isEmpty()) {
#if ENABLE(2022_GLIB_API)
auto& dataStore = webkitWebsiteDataManagerGetDataStore(webkit_network_session_get_website_data_manager(networkSession));
#else
auto& dataStore = webkitWebsiteDataManagerGetDataStore(webkit_web_context_get_website_data_manager(webContext));
#endif
dataStore.setNetworkProxySettings(WTFMove(settings));
}
} else {
WebKitNetworkProxySettings* proxySettings = nullptr;
auto proxyMode = parseProxyCapabilities(*capabilities.proxy, &proxySettings);
#if ENABLE(2022_GLIB_API)
webkit_network_session_set_proxy_settings(networkSession, proxyMode, proxySettings);
#else
webkit_website_data_manager_set_network_proxy_settings(webkit_web_context_get_website_data_manager(webContext), proxyMode, proxySettings);
#endif
if (proxySettings)
webkit_network_proxy_settings_free(proxySettings);
}
}
return session;
}
#endif
WebAutomationSession& webkitAutomationSessionGetSession(WebKitAutomationSession* session)
{
return *session->priv->session;
}
String webkitAutomationSessionGetBrowserName(WebKitAutomationSession* session)
{
if (session->priv->applicationInfo)
return String::fromUTF8(webkit_application_info_get_name(session->priv->applicationInfo));
return String::fromUTF8(g_get_prgname());
}
String webkitAutomationSessionGetBrowserVersion(WebKitAutomationSession* session)
{
if (!session->priv->applicationInfo)
return { };
guint64 major, minor, micro;
webkit_application_info_get_version(session->priv->applicationInfo, &major, &minor, µ);
if (!micro && !minor)
return String::number(major);
if (!micro)
return makeString(String::number(major), ".", String::number(minor));
return makeString(String::number(major), ".", String::number(minor), ".", String::number(micro));
}
/**
* webkit_automation_session_get_id:
* @session: a #WebKitAutomationSession
*
* Get the unique identifier of a #WebKitAutomationSession
*
* Returns: the unique identifier of @session
*
* Since: 2.18
*/
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();
}
/**
* webkit_automation_session_set_application_info:
* @session: a #WebKitAutomationSession
* @info: a #WebKitApplicationInfo
*
* Set the application information to @session.
*
* This information will be used by the driver service
* to match the requested capabilities with the actual application information. If this information
* is not provided to the session when a new automation session is requested, the creation might fail
* if the client requested a specific browser name or version. This will not have any effect when called
* after the automation session has been fully created, so this must be called in the callback of
* #WebKitWebContext::automation-started signal.
*
* Since: 2.18
*/
void webkit_automation_session_set_application_info(WebKitAutomationSession* session, WebKitApplicationInfo* info)
{
g_return_if_fail(WEBKIT_IS_AUTOMATION_SESSION(session));
g_return_if_fail(info);
if (session->priv->applicationInfo == info)
return;
if (session->priv->applicationInfo)
webkit_application_info_unref(session->priv->applicationInfo);
session->priv->applicationInfo = webkit_application_info_ref(info);
}
/**
* webkit_automation_session_get_application_info:
* @session: a #WebKitAutomationSession
*
* Get the the previously set #WebKitAutomationSession.
*
* Get the #WebKitAutomationSession previously set with webkit_automation_session_set_application_info().
*
* Returns: (transfer none): the #WebKitAutomationSession of @session, or %NULL if no one has been set.
*
* Since: 2.18
*/
WebKitApplicationInfo* webkit_automation_session_get_application_info(WebKitAutomationSession* session)
{
g_return_val_if_fail(WEBKIT_IS_AUTOMATION_SESSION(session), nullptr);
return session->priv->applicationInfo;
}
|