widget/qt/nsScreenManagerQt.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include <QGuiApplication>
     6 #include <QScreen>
     8 #include "nsScreenManagerQt.h"
     9 #include "nsScreenQt.h"
    11 nsScreenManagerQt::nsScreenManagerQt()
    12 {
    13     mInitialized = false;
    14     desktop = 0;
    15     screens = 0;
    16 }
    18 nsScreenManagerQt::~nsScreenManagerQt()
    19 {
    20     delete [] screens;
    21 }
    23 // addref, release, QI
    24 NS_IMPL_ISUPPORTS(nsScreenManagerQt, nsIScreenManager)
    26 void nsScreenManagerQt::init()
    27 {
    28     if (mInitialized)
    29         return;
    31     nScreens = QGuiApplication::screens().size();
    32     screens = new nsCOMPtr<nsIScreen>[nScreens];
    34     for (int i = 0; i < nScreens; ++i)
    35         screens[i] = new nsScreenQt(i);
    36     mInitialized = true;
    37 }
    39 //
    40 // ScreenForRect
    41 //
    42 // Returns the screen that contains the rectangle. If the rect overlaps
    43 // multiple screens, it picks the screen with the greatest area of intersection.
    44 //
    45 // The coordinates are in pixels (not twips) and in screen coordinates.
    46 //
    47 NS_IMETHODIMP
    48 nsScreenManagerQt::ScreenForRect(int32_t inLeft, int32_t inTop,
    49                                  int32_t inWidth, int32_t inHeight,
    50                                  nsIScreen **outScreen)
    51 {
    52     if (!mInitialized)
    53         init();
    55     QRect r(inLeft, inTop, inWidth, inHeight);
    56     int best = 0;
    57     int area = 0;
    58     for (int i = 0; i < nScreens; ++i) {
    59         const QRect& rect = QGuiApplication::screens()[i]->geometry();
    60         QRect intersection = r&rect;
    61         int a = intersection.width()*intersection.height();
    62         if (a > area) {
    63             best = i;
    64             area = a;
    65         }
    66     }
    68     NS_IF_ADDREF(*outScreen = screens[best]);
    69     return NS_OK;
    70 }
    72 //
    73 // GetPrimaryScreen
    74 //
    75 // The screen with the menubar/taskbar. This shouldn't be needed very
    76 // often.
    77 //
    78 NS_IMETHODIMP
    79 nsScreenManagerQt::GetPrimaryScreen(nsIScreen **aPrimaryScreen)
    80 {
    81     if (!desktop)
    82         init();
    84     NS_IF_ADDREF(*aPrimaryScreen = screens[0]);
    85     return NS_OK;
    86 }
    88 //
    89 // GetNumberOfScreens
    90 //
    91 // Returns how many physical screens are available.
    92 //
    93 NS_IMETHODIMP
    94 nsScreenManagerQt::GetNumberOfScreens(uint32_t *aNumberOfScreens)
    95 {
    96     if (!desktop)
    97         init();
    99     *aNumberOfScreens = nScreens;
   100     return NS_OK;
   101 }
   103 NS_IMETHODIMP
   104 nsScreenManagerQt::GetSystemDefaultScale(float *aDefaultScale)
   105 {
   106     *aDefaultScale = 1.0f;
   107     return NS_OK;
   108 }
   110 NS_IMETHODIMP
   111 nsScreenManagerQt::ScreenForNativeWidget(void *aWidget, nsIScreen **outScreen)
   112 {
   113     // I don't know how to go from GtkWindow to nsIScreen, especially
   114     // given xinerama and stuff, so let's just do this
   115     QRect rect(0, 0, 1, 1);
   116     return ScreenForRect(rect.x(), rect.y(), rect.width(), rect.height(), outScreen);
   117 }

mercurial