widget/qt/nsScreenManagerQt.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.

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

mercurial