Web Inspector: InspectorFrontendHost::inspectionLevel returns wrong value for inspector3
[WebKit-https.git] / Source / WebKit / UIProcess / WebInspectorProxy.cpp
index 694762c..12bb261 100644 (file)
 #include "config.h"
 #include "WebInspectorProxy.h"
 
-#include "APINavigationAction.h"
+#include "APINavigation.h"
 #include "APIProcessPoolConfiguration.h"
-#include "WKArray.h"
-#include "WKContextMenuItem.h"
-#include "WKMutableArray.h"
 #include "WebAutomationSession.h"
-#include "WebFramePolicyListenerProxy.h"
 #include "WebFrameProxy.h"
 #include "WebInspectorInterruptDispatcherMessages.h"
 #include "WebInspectorMessages.h"
 #include "WebInspectorProxyMessages.h"
 #include "WebInspectorUIMessages.h"
 #include "WebPageGroup.h"
+#include "WebPageInspectorController.h"
 #include "WebPageProxy.h"
 #include "WebPreferences.h"
 #include "WebProcessPool.h"
 #include "WebProcessProxy.h"
+#include <WebCore/CertificateInfo.h>
 #include <WebCore/NotImplemented.h>
+#include <WebCore/TextEncoding.h>
+#include <wtf/SetForScope.h>
 
 #if PLATFORM(GTK)
 #include "WebInspectorProxyClient.h"
 #endif
 
-using namespace WebCore;
-
 namespace WebKit {
+using namespace WebCore;
 
 const unsigned WebInspectorProxy::minimumWindowWidth = 500;
 const unsigned WebInspectorProxy::minimumWindowHeight = 400;
@@ -61,12 +60,14 @@ const unsigned WebInspectorProxy::initialWindowWidth = 1000;
 const unsigned WebInspectorProxy::initialWindowHeight = 650;
 
 WebInspectorProxy::WebInspectorProxy(WebPageProxy* inspectedPage)
-    : m_inspectedPage(inspectedPage)
-#if PLATFORM(MAC) && WK_API_ENABLED
-    , m_closeFrontendAfterInactivityTimer(RunLoop::main(), this, &WebInspectorProxy::closeFrontendAfterInactivityTimerFired)
+#if PLATFORM(MAC)
+    : m_closeFrontendAfterInactivityTimer(RunLoop::main(), this, &WebInspectorProxy::closeFrontendAfterInactivityTimerFired)
 #endif
 {
-    m_inspectedPage->process().addMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID(), *this);
+    if (inspectedPage && inspectedPage->hasRunningProcess()) {
+        m_inspectedPage = inspectedPage;
+        m_inspectedPage->process().addMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID(), *this);
+    }
 }
 
 WebInspectorProxy::~WebInspectorProxy()
@@ -86,14 +87,23 @@ WebPreferences& WebInspectorProxy::inspectorPagePreferences() const
 
 void WebInspectorProxy::invalidate()
 {
-    m_inspectedPage->process().removeMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID());
+    if (m_inspectedPage)
+        m_inspectedPage->process().removeMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID());
 
-    didClose();
+    closeFrontendPageAndWindow();
     platformInvalidate();
 
     m_inspectedPage = nullptr;
 }
 
+void WebInspectorProxy::sendMessageToFrontend(const String& message)
+{
+    if (!m_inspectorPage)
+        return;
+
+    m_inspectorPage->process().send(Messages::WebInspectorUI::SendMessageToFrontend(message), m_inspectorPage->pageID());
+}
+
 // Public APIs
 bool WebInspectorProxy::isFront()
 {
@@ -153,7 +163,7 @@ void WebInspectorProxy::close()
 
     m_inspectedPage->process().send(Messages::WebInspector::Close(), m_inspectedPage->pageID());
 
-    didClose();
+    closeFrontendPageAndWindow();
 }
 
 void WebInspectorProxy::closeForCrash()
@@ -163,6 +173,43 @@ void WebInspectorProxy::closeForCrash()
     platformDidCloseForCrash();
 }
 
+void WebInspectorProxy::reopen()
+{
+    if (!m_inspectedPage)
+        return;
+
+    close();
+    show();
+}
+
+void WebInspectorProxy::reset()
+{
+    if (m_inspectedPage) {
+        m_inspectedPage->process().removeMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID());
+        m_inspectedPage = nullptr;
+    }
+}
+
+void WebInspectorProxy::updateForNewPageProcess(WebPageProxy* inspectedPage)
+{
+    ASSERT(!m_inspectedPage);
+    ASSERT(inspectedPage);
+
+    m_inspectedPage = inspectedPage;
+    m_inspectedPage->process().addMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID(), *this);
+
+    if (m_inspectorPage)
+        m_inspectorPage->process().send(Messages::WebInspectorUI::UpdateConnection(), m_inspectorPage->pageID());
+}
+
+void WebInspectorProxy::setFrontendConnection(IPC::Attachment connectionIdentifier)
+{
+    if (!m_inspectedPage)
+        return;
+
+    m_inspectedPage->process().send(Messages::WebInspector::SetFrontendConnection(connectionIdentifier), m_inspectedPage->pageID());
+}
+
 void WebInspectorProxy::showConsole()
 {
     if (!m_inspectedPage)
@@ -278,6 +325,11 @@ void WebInspectorProxy::setAttachedWindowWidth(unsigned width)
     platformSetAttachedWindowWidth(width);
 }
 
+void WebInspectorProxy::setSheetRect(const FloatRect& rect)
+{
+    platformSetSheetRect(rect);
+}
+
 void WebInspectorProxy::startWindowDrag()
 {
     platformStartWindowDrag();
@@ -327,64 +379,6 @@ bool WebInspectorProxy::isMainOrTestInspectorPage(const URL& url)
     return url.protocol() == testPageURL.protocol() && decodeURLEscapeSequences(url.path()) == decodeURLEscapeSequences(testPageURL.path());
 }
 
-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)
-{
-    // Allow non-main frames to navigate anywhere.
-    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();
-
-    // Allow loading of the main inspector file.
-    if (WebInspectorProxy::isMainOrTestInspectorPage(request.url())) {
-        toImpl(listenerRef)->use({ });
-        return;
-    }
-
-    // Prevent everything else from loading in the inspector's page.
-    toImpl(listenerRef)->ignore();
-
-    // And instead load it in the inspected page.
-    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;
-}
-
 void WebInspectorProxy::createFrontendPage()
 {
     if (m_inspectorPage)
@@ -395,57 +389,22 @@ void WebInspectorProxy::createFrontendPage()
     if (!m_inspectorPage)
         return;
 
-    trackInspectorPage(m_inspectorPage);
-
-    WKPageNavigationClientV0 navigationClient = {
-        { 0, this },
-        decidePolicyForNavigationAction,
-        nullptr, // decidePolicyForNavigationResponse
-        nullptr, // decidePolicyForPluginLoad
-        nullptr, // didStartProvisionalNavigation
-        nullptr, // didReceiveServerRedirectForProvisionalNavigation
-        nullptr, // didFailProvisionalNavigation
-        nullptr, // didCommitNavigation
-        nullptr, // didFinishNavigation
-        nullptr, // didFailNavigation
-        nullptr, // didFailProvisionalLoadInSubframe
-        nullptr, // didFinishDocumentLoad
-        nullptr, // didSameDocumentNavigation
-        nullptr, // renderingProgressDidChange
-        nullptr, // canAuthenticateAgainstProtectionSpace
-        nullptr, // didReceiveAuthenticationChallenge
-        webProcessDidCrash,
-        nullptr, // copyWebCryptoMasterKey
-
-        nullptr, // didBeginNavigationGesture
-        nullptr, // willEndNavigationGesture
-        nullptr, // didEndNavigationGesture
-        nullptr, // didRemoveNavigationGestureSnapshot
-    };
-
-    WKPageContextMenuClientV3 contextMenuClient = {
-        { 3, this },
-        nullptr, // getContextMenuFromProposedMenu_deprecatedForUseWithV0
-        nullptr, // customContextMenuItemSelected
-        nullptr, // contextMenuDismissed
-        getContextMenuFromProposedMenu,
-        nullptr, // showContextMenu
-        nullptr, // hideContextMenu
-    };
-
-    WKPageSetPageNavigationClient(toAPI(m_inspectorPage), &navigationClient.base);
-    WKPageSetPageContextMenuClient(toAPI(m_inspectorPage), &contextMenuClient.base);
+    trackInspectorPage(m_inspectorPage, m_inspectedPage);
 
     m_inspectorPage->process().addMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID(), *this);
-    m_inspectorPage->process().assumeReadAccessToBaseURL(WebInspectorProxy::inspectorBaseURL());
+    m_inspectorPage->process().assumeReadAccessToBaseURL(*m_inspectorPage, WebInspectorProxy::inspectorBaseURL());
 }
 
-// Called by WebInspectorProxy messages
-void WebInspectorProxy::createInspectorPage(IPC::Attachment connectionIdentifier, bool canAttach, bool underTest)
+void WebInspectorProxy::openLocalInspectorFrontend(bool canAttach, bool underTest)
 {
     if (!m_inspectedPage)
         return;
 
+    if (m_inspectedPage->inspectorController().hasLocalFrontend()) {
+        show();
+        return;
+    }
+
     m_underTest = underTest;
     createFrontendPage();
 
@@ -453,9 +412,11 @@ void WebInspectorProxy::createInspectorPage(IPC::Attachment connectionIdentifier
     if (!m_inspectorPage)
         return;
 
-    m_connectionIdentifier = WTFMove(connectionIdentifier);
+    m_inspectorPage->process().send(Messages::WebInspectorUI::EstablishConnection(m_inspectedPage->pageID(), m_underTest, inspectionLevel()), m_inspectorPage->pageID());
 
-    m_inspectorPage->process().send(Messages::WebInspectorUI::EstablishConnection(m_connectionIdentifier, m_inspectedPage->pageID(), m_underTest, inspectionLevel()), m_inspectorPage->pageID());
+    ASSERT(!m_isActiveFrontend);
+    m_isActiveFrontend = true;
+    m_inspectedPage->inspectorController().connectFrontend(*this);
 
     if (!m_underTest) {
         m_canAttach = platformCanAttach(canAttach);
@@ -495,6 +456,7 @@ void WebInspectorProxy::open()
     if (!m_inspectorPage)
         return;
 
+    SetForScope<bool> isOpening(m_isOpening, true);
     m_isVisible = true;
     m_inspectorPage->process().send(Messages::WebInspectorUI::SetIsVisible(m_isVisible), m_inspectorPage->pageID());
 
@@ -508,6 +470,11 @@ void WebInspectorProxy::open()
 
 void WebInspectorProxy::didClose()
 {
+    closeFrontendPageAndWindow();
+}
+
+void WebInspectorProxy::closeFrontendPageAndWindow()
+{
     if (!m_inspectorPage)
         return;
 
@@ -521,6 +488,11 @@ void WebInspectorProxy::didClose()
     m_inspectorPage->process().send(Messages::WebInspectorUI::SetIsVisible(m_isVisible), m_inspectorPage->pageID());
     m_inspectorPage->process().removeMessageReceiver(Messages::WebInspectorProxy::messageReceiverName(), m_inspectedPage->pageID());
 
+    if (m_isActiveFrontend) {
+        m_isActiveFrontend = false;
+        m_inspectedPage->inspectorController().disconnectFrontend(*this);
+    }
+
     if (m_isAttached)
         platformDetach();
 
@@ -531,13 +503,22 @@ void WebInspectorProxy::didClose()
     m_canAttach = false;
     m_underTest = false;
 
-    m_connectionIdentifier = IPC::Attachment();
-
     platformCloseFrontendPageAndWindow();
 }
 
+void WebInspectorProxy::sendMessageToBackend(const String& message)
+{
+    if (!m_inspectedPage)
+        return;
+
+    m_inspectedPage->inspectorController().dispatchMessageFromFrontend(message);
+}
+
 void WebInspectorProxy::frontendLoaded()
 {
+    if (!m_inspectedPage)
+        return;
+
     if (auto* automationSession = m_inspectedPage->process().processPool().automationSession())
         automationSession->inspectorFrontendLoaded(*m_inspectedPage);
 }
@@ -578,6 +559,11 @@ void WebInspectorProxy::inspectedURLChanged(const String& urlString)
     platformInspectedURLChanged(urlString);
 }
 
+void WebInspectorProxy::showCertificate(const CertificateInfo& certificateInfo)
+{
+    platformShowCertificate(certificateInfo);
+}
+
 void WebInspectorProxy::elementSelectionChanged(bool active)
 {
     m_elementSelectionActive = active;
@@ -612,7 +598,7 @@ bool WebInspectorProxy::shouldOpenAttached()
 
 // Unsupported configurations can use the stubs provided here.
 
-#if PLATFORM(IOS) || (PLATFORM(MAC) && !WK_API_ENABLED)
+#if PLATFORM(IOS_FAMILY)
 
 WebPageProxy* WebInspectorProxy::platformCreateFrontendPage()
 {
@@ -666,6 +652,11 @@ void WebInspectorProxy::platformInspectedURLChanged(const String&)
     notImplemented();
 }
 
+void WebInspectorProxy::platformShowCertificate(const CertificateInfo&)
+{
+    notImplemented();
+}
+
 void WebInspectorProxy::platformSave(const String& suggestedURL, const String& content, bool base64Encoded, bool forceSaveDialog)
 {
     notImplemented();
@@ -703,6 +694,11 @@ void WebInspectorProxy::platformSetAttachedWindowHeight(unsigned)
     notImplemented();
 }
 
+void WebInspectorProxy::platformSetSheetRect(const FloatRect&)
+{
+    notImplemented();
+}
+
 void WebInspectorProxy::platformStartWindowDrag()
 {
     notImplemented();
@@ -736,6 +732,6 @@ void WebInspectorProxy::platformAttachAvailabilityChanged(bool)
     notImplemented();
 }
 
-#endif // PLATFORM(IOS) || (PLATFORM(MAC) && !WK_API_ENABLED)
+#endif // PLATFORM(IOS_FAMILY)
 
 } // namespace WebKit