WebInspectorProxyGtk.cpp [plain text]
#include "config.h"
#include "WebInspectorProxy.h"
#include "APINavigationAction.h"
#include "WKArray.h"
#include "WKContextMenuItem.h"
#include "WKMutableArray.h"
#include "WebFramePolicyListenerProxy.h"
#include "WebInspectorProxyClient.h"
#include "WebKitInspectorWindow.h"
#include "WebKitWebViewBasePrivate.h"
#include "WebPageGroup.h"
#include "WebProcessPool.h"
#include "WebProcessProxy.h"
#include <WebCore/FileSystem.h>
#include <WebCore/GtkUtilities.h>
#include <WebCore/NotImplemented.h>
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
namespace WebKit {
static void inspectorViewDestroyed(GtkWidget*, gpointer userData)
{
WebInspectorProxy* inspectorProxy = static_cast<WebInspectorProxy*>(userData);
inspectorProxy->close();
}
void WebInspectorProxy::setClient(std::unique_ptr<WebInspectorProxyClient>&& client)
{
m_client = WTFMove(client);
}
void WebInspectorProxy::updateInspectorWindowTitle() const
{
ASSERT(m_inspectorWindow);
webkitInspectorWindowSetSubtitle(WEBKIT_INSPECTOR_WINDOW(m_inspectorWindow), !m_inspectedURLString.isEmpty() ? m_inspectedURLString.utf8().data() : nullptr);
}
static unsigned long long exceededDatabaseQuota(WKPageRef, WKFrameRef, WKSecurityOriginRef, WKStringRef, WKStringRef, unsigned long long, unsigned long long, unsigned long long currentDatabaseUsage, unsigned long long expectedUsage, const void*)
{
return std::max<unsigned long long>(expectedUsage, currentDatabaseUsage * 1.25);
}
static void webProcessDidCrash(WKPageRef, const void* clientInfo)
{
WebInspectorProxy* webInspectorProxy = static_cast<WebInspectorProxy*>(const_cast<void*>(clientInfo));
ASSERT(webInspectorProxy);
webInspectorProxy->closeForCrash();
}
static void decidePolicyForNavigationAction(WKPageRef pageRef, WKNavigationActionRef navigationActionRef, WKFramePolicyListenerRef listenerRef, WKTypeRef, const void* clientInfo)
{
API::FrameInfo* sourceFrame = toImpl(navigationActionRef)->sourceFrame();
if (sourceFrame && !sourceFrame->isMainFrame()) {
toImpl(listenerRef)->use({ });
return;
}
const WebInspectorProxy* webInspectorProxy = static_cast<const WebInspectorProxy*>(clientInfo);
ASSERT(webInspectorProxy);
WebCore::ResourceRequest request = toImpl(navigationActionRef)->request();
if (WebInspectorProxy::isMainOrTestInspectorPage(request.url())) {
toImpl(listenerRef)->use({ });
return;
}
toImpl(listenerRef)->ignore();
webInspectorProxy->inspectedPage()->loadRequest(WTFMove(request));
}
static void getContextMenuFromProposedMenu(WKPageRef pageRef, WKArrayRef proposedMenuRef, WKArrayRef* newMenuRef, WKHitTestResultRef, WKTypeRef, const void*)
{
WKMutableArrayRef menuItems = WKMutableArrayCreate();
size_t count = WKArrayGetSize(proposedMenuRef);
for (size_t i = 0; i < count; ++i) {
WKContextMenuItemRef contextMenuItem = static_cast<WKContextMenuItemRef>(WKArrayGetItemAtIndex(proposedMenuRef, i));
switch (WKContextMenuItemGetTag(contextMenuItem)) {
case kWKContextMenuItemTagOpenLinkInNewWindow:
case kWKContextMenuItemTagOpenImageInNewWindow:
case kWKContextMenuItemTagOpenFrameInNewWindow:
case kWKContextMenuItemTagOpenMediaInNewWindow:
case kWKContextMenuItemTagDownloadLinkToDisk:
case kWKContextMenuItemTagDownloadImageToDisk:
break;
default:
WKArrayAppendItem(menuItems, contextMenuItem);
break;
}
}
*newMenuRef = menuItems;
}
WebPageProxy* WebInspectorProxy::platformCreateFrontendPage()
{
ASSERT(inspectedPage());
ASSERT(!m_inspectorView);
RefPtr<WebPreferences> preferences = WebPreferences::create(String(), "WebKit2.", "WebKit2.");
#if ENABLE(DEVELOPER_MODE)
preferences->setDeveloperExtrasEnabled(true);
preferences->setLogsPageMessagesToSystemConsoleEnabled(true);
#endif
preferences->setJavaScriptRuntimeFlags({
});
RefPtr<WebPageGroup> pageGroup = WebPageGroup::create(inspectorPageGroupIdentifierForPage(inspectedPage()), false, false);
auto pageConfiguration = API::PageConfiguration::create();
pageConfiguration->setProcessPool(&inspectorProcessPool(inspectionLevel()));
pageConfiguration->setPreferences(preferences.get());
pageConfiguration->setPageGroup(pageGroup.get());
m_inspectorView = GTK_WIDGET(webkitWebViewBaseCreate(*pageConfiguration.ptr()));
g_object_add_weak_pointer(G_OBJECT(m_inspectorView), reinterpret_cast<void**>(&m_inspectorView));
g_signal_connect(m_inspectorView, "destroy", G_CALLBACK(inspectorViewDestroyed), this);
WKPageUIClientV2 uiClient = {
{ 2, this },
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, exceededDatabaseQuota,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, };
WKPageNavigationClientV0 navigationClient = {
{ 0, this },
decidePolicyForNavigationAction,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, webProcessDidCrash,
nullptr,
nullptr, nullptr, nullptr, nullptr, };
WKPageContextMenuClientV3 contextMenuClient = {
{ 3, this },
nullptr, nullptr, nullptr, getContextMenuFromProposedMenu,
nullptr, nullptr, };
WebPageProxy* inspectorPage = webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(m_inspectorView));
ASSERT(inspectorPage);
WKPageSetPageUIClient(toAPI(inspectorPage), &uiClient.base);
WKPageSetPageNavigationClient(toAPI(inspectorPage), &navigationClient.base);
WKPageSetPageContextMenuClient(toAPI(inspectorPage), &contextMenuClient.base);
return inspectorPage;
}
void WebInspectorProxy::platformCreateFrontendWindow()
{
if (m_client && m_client->openWindow(*this))
return;
GtkWidget* inspectedViewParent = gtk_widget_get_toplevel(inspectedPage()->viewWidget());
if (!WebCore::widgetIsOnscreenToplevelWindow(inspectedViewParent))
inspectedViewParent = nullptr;
ASSERT(!m_inspectorWindow);
m_inspectorWindow = webkitInspectorWindowNew(inspectedViewParent ? GTK_WINDOW(inspectedViewParent) : nullptr);
gtk_container_add(GTK_CONTAINER(m_inspectorWindow), m_inspectorView);
gtk_widget_show(m_inspectorView);
if (!m_inspectedURLString.isEmpty())
updateInspectorWindowTitle();
g_object_add_weak_pointer(G_OBJECT(m_inspectorWindow), reinterpret_cast<void**>(&m_inspectorWindow));
gtk_window_present(GTK_WINDOW(m_inspectorWindow));
}
void WebInspectorProxy::platformCloseFrontendPageAndWindow()
{
if (m_inspectorView) {
g_signal_handlers_disconnect_by_func(m_inspectorView, reinterpret_cast<void*>(inspectorViewDestroyed), this);
m_inspectorView = nullptr;
}
if (m_client)
m_client->didClose(*this);
if (m_inspectorWindow) {
gtk_widget_destroy(m_inspectorWindow);
m_inspectorWindow = nullptr;
}
}
void WebInspectorProxy::platformDidCloseForCrash()
{
}
void WebInspectorProxy::platformInvalidate()
{
}
void WebInspectorProxy::platformHide()
{
notImplemented();
}
void WebInspectorProxy::platformBringToFront()
{
if (m_isOpening)
return;
if (m_client && m_client->bringToFront(*this))
return;
GtkWidget* parent = gtk_widget_get_toplevel(m_inspectorView);
if (WebCore::widgetIsOnscreenToplevelWindow(parent))
gtk_window_present(GTK_WINDOW(parent));
}
void WebInspectorProxy::platformBringInspectedPageToFront()
{
notImplemented();
}
bool WebInspectorProxy::platformIsFront()
{
GtkWidget* parent = gtk_widget_get_toplevel(m_inspectorView);
if (WebCore::widgetIsOnscreenToplevelWindow(parent))
return m_isVisible && gtk_window_is_active(GTK_WINDOW(parent));
return false;
}
void WebInspectorProxy::platformInspectedURLChanged(const String& url)
{
m_inspectedURLString = url;
if (m_client)
m_client->inspectedURLChanged(*this, url);
if (m_inspectorWindow)
updateInspectorWindowTitle();
}
String WebInspectorProxy::inspectorPageURL()
{
return String("resource:///org/webkit/inspector/UserInterface/Main.html");
}
String WebInspectorProxy::inspectorTestPageURL()
{
return String("resource:///org/webkit/inspector/UserInterface/Test.html");
}
String WebInspectorProxy::inspectorBaseURL()
{
return String("resource:///org/webkit/inspector/UserInterface/");
}
unsigned WebInspectorProxy::platformInspectedWindowHeight()
{
return gtk_widget_get_allocated_height(inspectedPage()->viewWidget());
}
unsigned WebInspectorProxy::platformInspectedWindowWidth()
{
return gtk_widget_get_allocated_width(inspectedPage()->viewWidget());
}
void WebInspectorProxy::platformAttach()
{
GRefPtr<GtkWidget> inspectorView = m_inspectorView;
if (m_inspectorWindow) {
gtk_container_remove(GTK_CONTAINER(m_inspectorWindow), m_inspectorView);
gtk_widget_destroy(m_inspectorWindow);
m_inspectorWindow = 0;
}
static const unsigned defaultAttachedSize = 300;
static const unsigned minimumAttachedWidth = 750;
static const unsigned minimumAttachedHeight = 250;
if (m_attachmentSide == AttachmentSide::Bottom) {
unsigned maximumAttachedHeight = platformInspectedWindowHeight() * 3 / 4;
platformSetAttachedWindowHeight(std::max(minimumAttachedHeight, std::min(defaultAttachedSize, maximumAttachedHeight)));
} else {
unsigned maximumAttachedWidth = platformInspectedWindowWidth() * 3 / 4;
platformSetAttachedWindowWidth(std::max(minimumAttachedWidth, std::min(defaultAttachedSize, maximumAttachedWidth)));
}
if (m_client && m_client->attach(*this))
return;
webkitWebViewBaseAddWebInspector(WEBKIT_WEB_VIEW_BASE(inspectedPage()->viewWidget()), m_inspectorView, m_attachmentSide);
gtk_widget_show(m_inspectorView);
}
void WebInspectorProxy::platformDetach()
{
if (!inspectedPage()->isValid())
return;
GRefPtr<GtkWidget> inspectorView = m_inspectorView;
if (!m_client || !m_client->detach(*this)) {
if (GtkWidget* parent = gtk_widget_get_parent(m_inspectorView))
gtk_container_remove(GTK_CONTAINER(parent), m_inspectorView);
}
if (!m_isVisible) {
g_signal_handlers_disconnect_by_func(m_inspectorView, reinterpret_cast<void*>(inspectorViewDestroyed), this);
m_inspectorView = nullptr;
return;
}
open();
}
void WebInspectorProxy::platformSetAttachedWindowHeight(unsigned height)
{
if (!m_isAttached)
return;
if (m_client)
m_client->didChangeAttachedHeight(*this, height);
webkitWebViewBaseSetInspectorViewSize(WEBKIT_WEB_VIEW_BASE(inspectedPage()->viewWidget()), height);
}
void WebInspectorProxy::platformSetAttachedWindowWidth(unsigned width)
{
if (!m_isAttached)
return;
if (m_client)
m_client->didChangeAttachedWidth(*this, width);
webkitWebViewBaseSetInspectorViewSize(WEBKIT_WEB_VIEW_BASE(inspectedPage()->viewWidget()), width);
}
void WebInspectorProxy::platformStartWindowDrag()
{
notImplemented();
}
void WebInspectorProxy::platformSave(const String&, const String&, bool, bool)
{
notImplemented();
}
void WebInspectorProxy::platformAppend(const String&, const String&)
{
notImplemented();
}
void WebInspectorProxy::platformAttachAvailabilityChanged(bool available)
{
if (m_client)
m_client->didChangeAttachAvailability(*this, available);
}
}