Web Inspector: Network: add button to show system certificate dialog
[WebKit-https.git] / Source / WebCore / inspector / InspectorFrontendHost.cpp
index 83dc66b..b1773aa 100644 (file)
@@ -30,6 +30,7 @@
 #include "config.h"
 #include "InspectorFrontendHost.h"
 
+#include "CertificateInfo.h"
 #include "ContextMenu.h"
 #include "ContextMenuController.h"
 #include "ContextMenuItem.h"
 #include "Editor.h"
 #include "Event.h"
 #include "FocusController.h"
+#include "Frame.h"
 #include "HitTestResult.h"
+#include "InspectorController.h"
 #include "InspectorFrontendClient.h"
-#include "JSMainThreadExecState.h"
-#include "MainFrame.h"
+#include "JSDOMConvertInterface.h"
+#include "JSDOMExceptionHandling.h"
+#include "JSExecState.h"
+#include "JSInspectorFrontendHost.h"
 #include "MouseEvent.h"
 #include "Node.h"
 #include "Page.h"
 #include "Pasteboard.h"
-#include "ScriptGlobalObject.h"
 #include "ScriptState.h"
 #include "UserGestureIndicator.h"
-#include <bindings/ScriptFunctionCall.h>
+#include <JavaScriptCore/ScriptFunctionCall.h>
 #include <pal/system/Sound.h>
 #include <wtf/StdLibExtras.h>
-
-using namespace Inspector;
+#include <wtf/text/Base64.h>
 
 namespace WebCore {
 
+using namespace Inspector;
+
 #if ENABLE(CONTEXT_MENUS)
 class FrontendMenuProvider : public ContextMenuProvider {
 public:
@@ -144,6 +149,19 @@ void InspectorFrontendHost::disconnectClient()
     m_frontendPage = nullptr;
 }
 
+void InspectorFrontendHost::addSelfToGlobalObjectInWorld(DOMWrapperWorld& world)
+{
+    auto& state = *execStateFromPage(world, m_frontendPage);
+    auto& vm = state.vm();
+    JSC::JSLockHolder lock(vm);
+    auto scope = DECLARE_CATCH_SCOPE(vm);
+
+    auto& globalObject = *JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject());
+    globalObject.putDirect(vm, JSC::Identifier::fromString(&vm, "InspectorFrontendHost"), toJS<IDLInterface<InspectorFrontendHost>>(state, globalObject, *this));
+    if (UNLIKELY(scope.exception()))
+        reportException(&state, scope.exception());
+}
+
 void InspectorFrontendHost::loaded()
 {
     if (m_client)
@@ -201,9 +219,9 @@ float InspectorFrontendHost::zoomFactor()
 String InspectorFrontendHost::userInterfaceLayoutDirection()
 {
     if (m_client && m_client->userInterfaceLayoutDirection() == UserInterfaceLayoutDirection::RTL)
-        return ASCIILiteral("rtl");
+        return "rtl"_s;
 
-    return ASCIILiteral("ltr");
+    return "ltr"_s;
 }
 
 void InspectorFrontendHost::setAttachedWindowHeight(unsigned height)
@@ -252,29 +270,27 @@ unsigned InspectorFrontendHost::inspectionLevel()
 
 String InspectorFrontendHost::platform()
 {
-#if PLATFORM(MAC) || PLATFORM(IOS)
-    return ASCIILiteral("mac");
+#if PLATFORM(MAC) || PLATFORM(IOS_FAMILY)
+    return "mac"_s;
 #elif OS(WINDOWS)
-    return ASCIILiteral("windows");
+    return "windows"_s;
 #elif OS(LINUX)
-    return ASCIILiteral("linux");
+    return "linux"_s;
 #elif OS(FREEBSD)
-    return ASCIILiteral("freebsd");
+    return "freebsd"_s;
 #elif OS(OPENBSD)
-    return ASCIILiteral("openbsd");
-#elif OS(SOLARIS)
-    return ASCIILiteral("solaris");
+    return "openbsd"_s;
 #else
-    return ASCIILiteral("unknown");
+    return "unknown"_s;
 #endif
 }
 
 String InspectorFrontendHost::port()
 {
 #if PLATFORM(GTK)
-    return ASCIILiteral("gtk");
+    return "gtk"_s;
 #else
-    return ASCIILiteral("unknown");
+    return "unknown"_s;
 #endif
 }
 
@@ -366,17 +382,17 @@ void InspectorFrontendHost::showContextMenu(Event& event, Vector<ContextMenuItem
 {
 #if ENABLE(CONTEXT_MENUS)
     ASSERT(m_frontendPage);
+
     auto& state = *execStateFromPage(debuggerWorld(), m_frontendPage);
-    JSC::JSObject* frontendApiObject;
-    if (!ScriptGlobalObject::get(state, "InspectorFrontendAPI", frontendApiObject)) {
-        ASSERT_NOT_REACHED();
-        return;
-    }
+    auto value = state.lexicalGlobalObject()->get(&state, JSC::Identifier::fromString(&state.vm(), "InspectorFrontendAPI"));
+    ASSERT(value);
+    ASSERT(value.isObject());
+    auto* frontendAPIObject = asObject(value);
     
     ContextMenu menu;
     populateContextMenu(WTFMove(items), menu);
 
-    auto menuProvider = FrontendMenuProvider::create(this, { &state, frontendApiObject }, menu.items());
+    auto menuProvider = FrontendMenuProvider::create(this, { &state, frontendAPIObject }, menu.items());
     m_menuProvider = menuProvider.ptr();
     m_frontendPage->contextMenuController().showContextMenu(event, menuProvider);
 #else
@@ -393,7 +409,7 @@ void InspectorFrontendHost::dispatchEventAsContextMenuEvent(Event& event)
 
     auto& mouseEvent = downcast<MouseEvent>(event);
     IntPoint mousePoint { mouseEvent.clientX(), mouseEvent.clientY() };
-    auto& frame = *mouseEvent.target()->toNode()->document().frame();
+    auto& frame = *downcast<Node>(mouseEvent.target())->document().frame();
 
     m_frontendPage->contextMenuController().showContextMenuAt(frame, mousePoint);
 #else
@@ -417,4 +433,40 @@ void InspectorFrontendHost::beep()
     PAL::systemBeep();
 }
 
+void InspectorFrontendHost::inspectInspector()
+{
+    if (m_frontendPage)
+        m_frontendPage->inspectorController().show();
+}
+
+bool InspectorFrontendHost::supportsShowCertificate() const
+{
+#if PLATFORM(COCOA)
+    return true;
+#else
+    return false;
+#endif
+}
+
+bool InspectorFrontendHost::showCertificate(const String& serializedCertificate)
+{
+    if (!m_client)
+        return false;
+
+    Vector<uint8_t> data;
+    if (!base64Decode(serializedCertificate, data))
+        return false;
+
+    CertificateInfo certificateInfo;
+    WTF::Persistence::Decoder decoder(data.data(), data.size());
+    if (!decoder.decode(certificateInfo))
+        return false;
+
+    if (certificateInfo.isEmpty())
+        return false;
+
+    m_client->showCertificate(certificateInfo);
+    return true;
+}
+
 } // namespace WebCore