2009-07-17  Holger Hans Peter Freyther  <zecke@selfish.org>

        Reviewed by NOBODY (OOPS!).

        [GTK+] Crash in screenAvailable due a null Widget*

        JSDOMWindow::open called screenAvailableRect(0). The other
        Screen methods can be called with a null widget as well, fix the
        crashing test by checking for null.

        In screenRect and screenAvailableRect it is not tried to use
        a default screen as the existing implementation didn't try either
        in case of not having a toplevel widget.

        LayoutTests/fast/frames/crash-removed-iframe.html caused a crash.

        * platform/gtk/PlatformScreenGtk.cpp:
        (WebCore::getVisual): New method to get a visual or return zero.
        (WebCore::screenDepth): Use getVisual.
        (WebCore::screenDepthPerComponent): Use getVisual.
        (WebCore::screenIsMonochrome): Use screenDepth which will do the null checking
        (WebCore::screenRect): Check for !widget.
        (WebCore::screenAvailableRect): Check for !widget.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@46035 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/platform/gtk/PlatformScreenGtk.cpp b/WebCore/platform/gtk/PlatformScreenGtk.cpp
index 27985ef..4ba6d43 100644
--- a/WebCore/platform/gtk/PlatformScreenGtk.cpp
+++ b/WebCore/platform/gtk/PlatformScreenGtk.cpp
@@ -3,6 +3,7 @@
  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
  * Copyright (C) 2008 Christian Dywan <christian@imendio.com>
  * Copyright (C) 2008 Collabora Ltd.
+ * Copyright (C) 2009 Holger Hans Peter Freyther
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -43,47 +44,55 @@
 
 namespace WebCore {
 
-int screenDepth(Widget* widget)
+static GdkVisual* getVisual(Widget* widget)
 {
+    if (!widget)
+        return 0;
+
     GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformWindow());
 
     if (!container)
-        return 24;
+        return 0;
 
     if (!GTK_WIDGET_REALIZED(container)) {
         GtkWidget* toplevel = gtk_widget_get_toplevel(container);
         if (GTK_WIDGET_TOPLEVEL(toplevel))
             container = toplevel;
         else
-            return 24;
+            return 0;
     }
 
 
-    GdkVisual* visual = gdk_drawable_get_visual(GDK_DRAWABLE(container->window));
+    return gdk_drawable_get_visual(GDK_DRAWABLE(container->window));
+}
+
+int screenDepth(Widget* widget)
+{
+    GdkVisual* visual = getVisual(widget);
+    if (!visual)
+        return 24;
     return visual->depth;
 }
 
 int screenDepthPerComponent(Widget* widget)
 {
-    GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformWindow());
-    if (!container)
+    GdkVisual* visual = getVisual(widget);
+    if (!visual)
         return 8;
 
-    GdkVisual* visual = gdk_drawable_get_visual(GDK_DRAWABLE(GTK_WIDGET(widget->root()->hostWindow()->platformWindow())->window));
     return visual->bits_per_rgb;
 }
 
 bool screenIsMonochrome(Widget* widget)
 {
-    GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformWindow());
-    if (!container)
-        return false;
-
     return screenDepth(widget) < 2;
 }
 
 FloatRect screenRect(Widget* widget)
 {
+    if (!widget)
+        return FloatRect();
+
     GtkWidget* container = gtk_widget_get_toplevel(GTK_WIDGET(widget->root()->hostWindow()->platformWindow()));
     if (!GTK_WIDGET_TOPLEVEL(container))
         return FloatRect();
@@ -101,6 +110,9 @@
 
 FloatRect screenAvailableRect(Widget* widget)
 {
+    if (!widget)
+        return FloatRect();
+
 #if PLATFORM(X11)
     GtkWidget* container = GTK_WIDGET(widget->root()->hostWindow()->platformWindow());
     if (!container)