widget/gtk/nsScreenGtk.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsScreenGtk.h"
     8 #include <gdk/gdk.h>
     9 #ifdef MOZ_X11
    10 #include <gdk/gdkx.h>
    11 #include <X11/Xatom.h>
    12 #endif
    13 #include <gtk/gtk.h>
    15 nsScreenGtk :: nsScreenGtk (  )
    16   : mScreenNum(0),
    17     mRect(0, 0, 0, 0),
    18     mAvailRect(0, 0, 0, 0)
    19 {
    20 }
    23 nsScreenGtk :: ~nsScreenGtk()
    24 {
    25 }
    28 NS_IMETHODIMP
    29 nsScreenGtk :: GetRect(int32_t *outLeft, int32_t *outTop, int32_t *outWidth, int32_t *outHeight)
    30 {
    31   *outLeft = mRect.x;
    32   *outTop = mRect.y;
    33   *outWidth = mRect.width;
    34   *outHeight = mRect.height;
    36   return NS_OK;
    38 } // GetRect
    41 NS_IMETHODIMP
    42 nsScreenGtk :: GetAvailRect(int32_t *outLeft, int32_t *outTop, int32_t *outWidth, int32_t *outHeight)
    43 {
    44   *outLeft = mAvailRect.x;
    45   *outTop = mAvailRect.y;
    46   *outWidth = mAvailRect.width;
    47   *outHeight = mAvailRect.height;
    49   return NS_OK;
    51 } // GetAvailRect
    54 NS_IMETHODIMP 
    55 nsScreenGtk :: GetPixelDepth(int32_t *aPixelDepth)
    56 {
    57   GdkVisual * visual = gdk_screen_get_system_visual(gdk_screen_get_default());
    58   *aPixelDepth = gdk_visual_get_depth(visual);
    60   return NS_OK;
    62 } // GetPixelDepth
    65 NS_IMETHODIMP 
    66 nsScreenGtk :: GetColorDepth(int32_t *aColorDepth)
    67 {
    68   return GetPixelDepth ( aColorDepth );
    70 } // GetColorDepth
    73 void
    74 nsScreenGtk :: Init (GdkWindow *aRootWindow)
    75 {
    76   // We listen for configure events on the root window to pick up
    77   // changes to this rect.  We could listen for "size_changed" signals
    78   // on the default screen to do this, except that doesn't work with
    79   // versions of GDK predating the GdkScreen object.  See bug 256646.
    80   mAvailRect = mRect = nsIntRect(0, 0, gdk_screen_width(), gdk_screen_height());
    82 #ifdef MOZ_X11
    83   // We need to account for the taskbar, etc in the available rect.
    84   // See http://freedesktop.org/Standards/wm-spec/index.html#id2767771
    86   // XXX do we care about _NET_WM_STRUT_PARTIAL?  That will
    87   // add much more complexity to the code here (our screen
    88   // could have a non-rectangular shape), but should
    89   // lead to greater accuracy.
    91   long *workareas;
    92   GdkAtom type_returned;
    93   int format_returned;
    94   int length_returned;
    96   GdkAtom cardinal_atom = gdk_x11_xatom_to_atom(XA_CARDINAL);
    98   gdk_error_trap_push();
   100   // gdk_property_get uses (length + 3) / 4, hence G_MAXLONG - 3 here.
   101   if (!gdk_property_get(aRootWindow,
   102                         gdk_atom_intern ("_NET_WORKAREA", FALSE),
   103                         cardinal_atom,
   104                         0, G_MAXLONG - 3, FALSE,
   105                         &type_returned,
   106                         &format_returned,
   107                         &length_returned,
   108                         (guchar **) &workareas)) {
   109     // This window manager doesn't support the freedesktop standard.
   110     // Nothing we can do about it, so assume full screen size.
   111     return;
   112   }
   114   // Flush the X queue to catch errors now.
   115   gdk_flush();
   117   if (!gdk_error_trap_pop() &&
   118       type_returned == cardinal_atom &&
   119       length_returned && (length_returned % 4) == 0 &&
   120       format_returned == 32) {
   121     int num_items = length_returned / sizeof(long);
   123     for (int i = 0; i < num_items; i += 4) {
   124       nsIntRect workarea(workareas[i],     workareas[i + 1],
   125                          workareas[i + 2], workareas[i + 3]);
   126       if (!mRect.Contains(workarea)) {
   127         // Note that we hit this when processing screen size changes,
   128         // since we'll get the configure event before the toolbars have
   129         // been moved.  We'll end up cleaning this up when we get the
   130         // change notification to the _NET_WORKAREA property.  However,
   131         // we still want to listen to both, so we'll handle changes
   132         // properly for desktop environments that don't set the
   133         // _NET_WORKAREA property.
   134         NS_WARNING("Invalid bounds");
   135         continue;
   136       }
   138       mAvailRect.IntersectRect(mAvailRect, workarea);
   139     }
   140   }
   141   g_free (workareas);
   142 #endif
   143 }
   145 #ifdef MOZ_X11
   146 void
   147 nsScreenGtk :: Init (XineramaScreenInfo *aScreenInfo)
   148 {
   149   nsIntRect xineRect(aScreenInfo->x_org, aScreenInfo->y_org,
   150                      aScreenInfo->width, aScreenInfo->height);
   152   mScreenNum = aScreenInfo->screen_number;
   154   mAvailRect = mRect = xineRect;
   155 }
   156 #endif

mercurial