2010-11-08 Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
[WebKit-https.git] / WebKit / gtk / WebCoreSupport / InspectorClientGtk.cpp
index 7039dd6..312b8c8 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 Gustavo Noronha Silva
 /*
  * Copyright (C) 2008 Gustavo Noronha Silva
+ * Copyright (C) 2010 Collabora Ltd.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -23,6 +24,7 @@
 #include "webkitwebview.h"
 #include "webkitwebinspector.h"
 #include "webkitprivate.h"
 #include "webkitwebview.h"
 #include "webkitwebinspector.h"
 #include "webkitprivate.h"
+#include "webkitversion.h"
 #include "InspectorController.h"
 #include "NotImplemented.h"
 #include "PlatformString.h"
 #include "InspectorController.h"
 #include "NotImplemented.h"
 #include "PlatformString.h"
@@ -34,7 +36,7 @@ namespace WebKit {
 
 static void notifyWebViewDestroyed(WebKitWebView* webView, InspectorFrontendClient* inspectorFrontendClient)
 {
 
 static void notifyWebViewDestroyed(WebKitWebView* webView, InspectorFrontendClient* inspectorFrontendClient)
 {
-    inspectorFrontendClient->destroyInspectorWindow();
+    inspectorFrontendClient->destroyInspectorWindow(true);
 }
 
 InspectorClient::InspectorClient(WebKitWebView* webView)
 }
 
 InspectorClient::InspectorClient(WebKitWebView* webView)
@@ -76,17 +78,9 @@ void InspectorClient::openInspectorFrontend(InspectorController* controller)
     }
 
     webkit_web_inspector_set_web_view(webInspector, inspectorWebView);
     }
 
     webkit_web_inspector_set_web_view(webInspector, inspectorWebView);
-
-    GOwnPtr<gchar> inspectorURI;
-
-    // Make the Web Inspector work when running tests
-    if (g_file_test("WebCore/inspector/front-end/inspector.html", G_FILE_TEST_EXISTS)) {
-        GOwnPtr<gchar> currentDirectory(g_get_current_dir());
-        GOwnPtr<gchar> fullPath(g_strdup_printf("%s/WebCore/inspector/front-end/inspector.html", currentDirectory.get()));
-        inspectorURI.set(g_filename_to_uri(fullPath.get(), NULL, NULL));
-    } else
-        inspectorURI.set(g_filename_to_uri(DATA_DIR"/webkit-1.0/webinspector/inspector.html", NULL, NULL));
-
+    GOwnPtr<gchar> inspectorPath(g_build_filename(inspectorFilesPath(), "inspector.html", NULL));
+    GOwnPtr<gchar> inspectorURI(g_filename_to_uri(inspectorPath.get(), 0, 0));
     webkit_web_view_load_uri(inspectorWebView, inspectorURI.get());
 
     gtk_widget_show(GTK_WIDGET(inspectorWebView));
     webkit_web_view_load_uri(inspectorWebView, inspectorURI.get());
 
     gtk_widget_show(GTK_WIDGET(inspectorWebView));
@@ -94,6 +88,9 @@ void InspectorClient::openInspectorFrontend(InspectorController* controller)
     m_frontendPage = core(inspectorWebView);
     m_frontendClient = new InspectorFrontendClient(m_inspectedWebView, inspectorWebView, webInspector, m_frontendPage, this);
     m_frontendPage->inspectorController()->setInspectorFrontendClient(m_frontendClient);
     m_frontendPage = core(inspectorWebView);
     m_frontendClient = new InspectorFrontendClient(m_inspectedWebView, inspectorWebView, webInspector, m_frontendPage, this);
     m_frontendPage->inspectorController()->setInspectorFrontendClient(m_frontendClient);
+
+    // The inspector must be in it's own PageGroup to avoid deadlock while debugging.
+    m_frontendPage->setGroupName("");
 }
 
 void InspectorClient::releaseFrontendPage()
 }
 
 void InspectorClient::releaseFrontendPage()
@@ -115,38 +112,136 @@ void InspectorClient::hideHighlight()
     gtk_widget_queue_draw(GTK_WIDGET(m_inspectedWebView));
 }
 
     gtk_widget_queue_draw(GTK_WIDGET(m_inspectedWebView));
 }
 
+#ifdef HAVE_GSETTINGS
+static String toGSettingName(String inspectorSettingName)
+{
+    if (inspectorSettingName == "resourceTrackingEnabled")
+        return String("resource-tracking-enabled");
+
+    if (inspectorSettingName == "xhrMonitor")
+        return String("xhr-monitor-enabled");
+
+    if (inspectorSettingName == "frontendSettings")
+        return String("frontend-settings");
+
+    if (inspectorSettingName == "debuggerEnabled")
+        return String("debugger-enabled");
+
+    if (inspectorSettingName == "profilerEnabled")
+        return String("profiler-enabled");
+
+    return inspectorSettingName;
+}
+
+static String truthStringFromVariant(GVariant* variant)
+{
+    if (g_variant_get_boolean(variant))
+        return String("true");
+
+    return String("false");
+}
+
+static GVariant* variantFromTruthString(const String& truth)
+{
+    if (truth == "true")
+        return g_variant_new_boolean(TRUE);
+
+    return g_variant_new_boolean(FALSE);
+}
+
+static bool shouldIgnoreSetting(const String& key)
+{
+    // Ignore this setting for now, it doesn't seem to be used for
+    // anything right now.
+    if (key == "lastActivePanel")
+        return true;
+
+    // GSettings considers trying to fetch or set a setting that is
+    // not backed by a schema as programmer error, and aborts the
+    // program's execution. We check here to avoid having an unhandled
+    // setting as a fatal error.
+    if (key == "resourceTrackingEnabled" || key == "xhrMonitor"
+        || key == "frontendSettings" || key == "debuggerEnabled"
+        || key == "profilerEnabled")
+        return false;
+
+    LOG_VERBOSE(NotYetImplemented, "Unknown key ignored: %s", key.ascii().data());
+    return true;
+}
+
 void InspectorClient::populateSetting(const String& key, String* value)
 {
 void InspectorClient::populateSetting(const String& key, String* value)
 {
-    notImplemented();
+    if (shouldIgnoreSetting(key))
+        return;
+
+    GSettings* settings = inspectorGSettings();
+    if (!settings)
+        return;
+
+    PlatformRefPtr<GVariant> variant = adoptPlatformRef(g_settings_get_value(settings, toGSettingName(key).utf8().data()));
+
+    if (key == "resourceTrackingEnabled" || key == "xhrMonitor"
+        || key == "debuggerEnabled" || key == "profilerEnabled")
+        *value = truthStringFromVariant(variant.get());
+    else if (key == "frontendSettings")
+        *value = String(g_variant_get_string(variant.get(), 0));
 }
 
 void InspectorClient::storeSetting(const String& key, const String& value)
 {
 }
 
 void InspectorClient::storeSetting(const String& key, const String& value)
 {
+    if (shouldIgnoreSetting(key))
+        return;
+
+    GSettings* settings = inspectorGSettings();
+    if (!settings)
+        return;
+
+    PlatformRefPtr<GVariant> variant(0);
+
+    // Set the key with the appropriate type, and also avoid setting
+    // unknown keys to avoid aborting the execution.
+    if (key == "resourceTrackingEnabled" || key == "xhrMonitor"
+        || key == "debuggerEnabled" || key == "profilerEnabled")
+        variant = adoptPlatformRef(variantFromTruthString(value));
+    else if (key == "frontendSettings")
+        variant = adoptPlatformRef(g_variant_new_string(value.utf8().data()));
+
+    if (!variant)
+        return;
+
+    g_settings_set_value(settings, toGSettingName(key).utf8().data(), variant.get());
+}
+#else
+void InspectorClient::populateSetting(const String&, String*)
+{
+    notImplemented();
+}
+
+void InspectorClient::storeSetting(const String&, const String&)
+{
     notImplemented();
 }
     notImplemented();
 }
+#endif // HAVE_GSETTINGS
 
 bool InspectorClient::sendMessageToFrontend(const String& message)
 {
 
 bool InspectorClient::sendMessageToFrontend(const String& message)
 {
-    if (!m_frontendPage)
-        return false;
+    return doDispatchMessageOnFrontendPage(m_frontendPage, message);
+}
 
 
-    Frame* frame = m_frontendPage->mainFrame();
-    if (!frame)
-        return false;
+const char* InspectorClient::inspectorFilesPath()
+{
+    if (m_inspectorFilesPath)
+        m_inspectorFilesPath.get();
 
 
-    ScriptController* scriptController = frame->script();
-    if (!scriptController)
-        return false;
+    const char* environmentPath = getenv("WEBKIT_INSPECTOR_PATH");
+    if (environmentPath && g_file_test(environmentPath, G_FILE_TEST_IS_DIR))
+        m_inspectorFilesPath.set(g_strdup(environmentPath));
+    else
+        m_inspectorFilesPath.set(g_build_filename(DATA_DIR, "webkitgtk-"WEBKITGTK_API_VERSION_STRING, "webinspector", NULL));
 
 
-    String dispatchToFrontend("WebInspector.dispatchMessageFromBackend(");
-    dispatchToFrontend += message;
-    dispatchToFrontend += ");";
-    scriptController->executeScript(dispatchToFrontend);
-    return true;
+    return m_inspectorFilesPath.get();
 }
 
 }
 
-bool destroyed = TRUE;
-
 InspectorFrontendClient::InspectorFrontendClient(WebKitWebView* inspectedWebView, WebKitWebView* inspectorWebView, WebKitWebInspector* webInspector, Page* inspectorPage, InspectorClient* inspectorClient)
     : InspectorFrontendClientLocal(core(inspectedWebView)->inspectorController(), inspectorPage)
     , m_inspectorWebView(inspectorWebView)
 InspectorFrontendClient::InspectorFrontendClient(WebKitWebView* inspectedWebView, WebKitWebView* inspectorWebView, WebKitWebInspector* webInspector, Page* inspectorPage, InspectorClient* inspectorClient)
     : InspectorFrontendClientLocal(core(inspectedWebView)->inspectorController(), inspectorPage)
     , m_inspectorWebView(inspectorWebView)
@@ -167,7 +262,7 @@ InspectorFrontendClient::~InspectorFrontendClient()
     ASSERT(!m_webInspector);
 }
 
     ASSERT(!m_webInspector);
 }
 
-void InspectorFrontendClient::destroyInspectorWindow()
+void InspectorFrontendClient::destroyInspectorWindow(bool notifyInspectorController)
 {
     if (!m_webInspector)
         return;
 {
     if (!m_webInspector)
         return;
@@ -177,7 +272,8 @@ void InspectorFrontendClient::destroyInspectorWindow()
     g_signal_handlers_disconnect_by_func(m_inspectorWebView, (gpointer)notifyWebViewDestroyed, (gpointer)this);
     m_inspectorWebView = 0;
 
     g_signal_handlers_disconnect_by_func(m_inspectorWebView, (gpointer)notifyWebViewDestroyed, (gpointer)this);
     m_inspectorWebView = 0;
 
-    core(m_inspectedWebView)->inspectorController()->disconnectFrontend();
+    if (notifyInspectorController)
+        core(m_inspectedWebView)->inspectorController()->disconnectFrontend();
 
     if (m_inspectorClient)
         m_inspectorClient->releaseFrontendPage();
 
     if (m_inspectorClient)
         m_inspectorClient->releaseFrontendPage();
@@ -195,18 +291,11 @@ void InspectorFrontendClient::destroyInspectorWindow()
 
 String InspectorFrontendClient::localizedStringsURL()
 {
 
 String InspectorFrontendClient::localizedStringsURL()
 {
-    GOwnPtr<gchar> URL;
-
-    // Make the Web Inspector work when running tests
-    if (g_file_test("WebCore/English.lproj/localizedStrings.js", G_FILE_TEST_EXISTS)) {
-        GOwnPtr<gchar> currentDirectory(g_get_current_dir());
-        GOwnPtr<gchar> fullPath(g_strdup_printf("%s/WebCore/English.lproj/localizedStrings.js", currentDirectory.get()));
-        URL.set(g_filename_to_uri(fullPath.get(), NULL, NULL));
-    } else
-        URL.set(g_filename_to_uri(DATA_DIR"/webkit-1.0/webinspector/localizedStrings.js", NULL, NULL));
+    GOwnPtr<gchar> stringsPath(g_build_filename(m_inspectorClient->inspectorFilesPath(), "localizedStrings.js", NULL));
+    GOwnPtr<gchar> stringsURI(g_filename_to_uri(stringsPath.get(), 0, 0));
 
     // FIXME: support l10n of localizedStrings.js
 
     // FIXME: support l10n of localizedStrings.js
-    return String::fromUTF8(URL.get());
+    return String::fromUTF8(stringsURI.get());
 }
 
 String InspectorFrontendClient::hiddenPanels()
 }
 
 String InspectorFrontendClient::hiddenPanels()
@@ -226,7 +315,12 @@ void InspectorFrontendClient::bringToFront()
 
 void InspectorFrontendClient::closeWindow()
 {
 
 void InspectorFrontendClient::closeWindow()
 {
-    destroyInspectorWindow();
+    destroyInspectorWindow(true);
+}
+
+void InspectorFrontendClient::disconnectFromBackend()
+{
+    destroyInspectorWindow(false);
 }
 
 void InspectorFrontendClient::attachWindow()
 }
 
 void InspectorFrontendClient::attachWindow()