widget/qt/nsIdleServiceQt.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 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim:expandtab:shiftwidth=4:tabstop=4:
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #ifdef MOZ_X11
michael@0 9 #include "mozilla/X11Util.h"
michael@0 10 #endif
michael@0 11 #include "nsIdleServiceQt.h"
michael@0 12 #include "nsIServiceManager.h"
michael@0 13 #include "nsDebug.h"
michael@0 14 #include "prlink.h"
michael@0 15
michael@0 16 #if defined(MOZ_X11)
michael@0 17 typedef bool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base,
michael@0 18 int* error_base);
michael@0 19
michael@0 20 typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void);
michael@0 21
michael@0 22 typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw,
michael@0 23 XScreenSaverInfo *info);
michael@0 24
michael@0 25 static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nullptr;
michael@0 26 static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nullptr;
michael@0 27 static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nullptr;
michael@0 28 #endif
michael@0 29
michael@0 30 static bool sInitialized = false;
michael@0 31
michael@0 32 NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceQt, nsIdleService)
michael@0 33
michael@0 34 nsIdleServiceQt::nsIdleServiceQt()
michael@0 35 #if defined(MOZ_X11)
michael@0 36 : mXssInfo(nullptr)
michael@0 37 #endif
michael@0 38 {
michael@0 39 }
michael@0 40
michael@0 41 static void Initialize()
michael@0 42 {
michael@0 43 sInitialized = true;
michael@0 44
michael@0 45 #if defined(MOZ_X11)
michael@0 46 // This will leak - See comments in ~nsIdleServiceQt().
michael@0 47 PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1");
michael@0 48 if (!xsslib) {
michael@0 49 return;
michael@0 50 }
michael@0 51
michael@0 52 _XSSQueryExtension = (_XScreenSaverQueryExtension_fn)
michael@0 53 PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryExtension");
michael@0 54 _XSSAllocInfo = (_XScreenSaverAllocInfo_fn)
michael@0 55 PR_FindFunctionSymbol(xsslib, "XScreenSaverAllocInfo");
michael@0 56 _XSSQueryInfo = (_XScreenSaverQueryInfo_fn)
michael@0 57 PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryInfo");
michael@0 58 #endif
michael@0 59 }
michael@0 60
michael@0 61 nsIdleServiceQt::~nsIdleServiceQt()
michael@0 62 {
michael@0 63 #if defined(MOZ_X11)
michael@0 64 if (mXssInfo)
michael@0 65 XFree(mXssInfo);
michael@0 66
michael@0 67 // It is not safe to unload libXScrnSaver until each display is closed because
michael@0 68 // the library registers callbacks through XESetCloseDisplay (Bug 397607).
michael@0 69 // (Also the library and its functions are scoped for the file not the object.)
michael@0 70 #if 0
michael@0 71 if (xsslib) {
michael@0 72 PR_UnloadLibrary(xsslib);
michael@0 73 xsslib = nullptr;
michael@0 74 }
michael@0 75 #endif
michael@0 76 #endif
michael@0 77 }
michael@0 78
michael@0 79 bool
michael@0 80 nsIdleServiceQt::PollIdleTime(uint32_t *aIdleTime)
michael@0 81 {
michael@0 82 #if defined(MOZ_X11)
michael@0 83 // Ask xscreensaver about idle time:
michael@0 84 *aIdleTime = 0;
michael@0 85
michael@0 86 // We might not have a display (cf. in xpcshell)
michael@0 87 Display *dplay = mozilla::DefaultXDisplay();
michael@0 88 if (!dplay) {
michael@0 89 return false;
michael@0 90 }
michael@0 91
michael@0 92 if (!sInitialized) {
michael@0 93 Initialize();
michael@0 94 }
michael@0 95 if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) {
michael@0 96 return false;
michael@0 97 }
michael@0 98
michael@0 99 int event_base, error_base;
michael@0 100 if (_XSSQueryExtension(dplay, &event_base, &error_base)) {
michael@0 101 if (!mXssInfo)
michael@0 102 mXssInfo = _XSSAllocInfo();
michael@0 103 if (!mXssInfo)
michael@0 104 return false;
michael@0 105
michael@0 106 _XSSQueryInfo(dplay, RootWindowOfScreen(DefaultScreenOfDisplay(mozilla::DefaultXDisplay())), mXssInfo);
michael@0 107 *aIdleTime = mXssInfo->idle;
michael@0 108 return true;
michael@0 109 }
michael@0 110 #endif
michael@0 111
michael@0 112 return false;
michael@0 113 }
michael@0 114
michael@0 115 bool
michael@0 116 nsIdleServiceQt::UsePollMode()
michael@0 117 {
michael@0 118 #if defined(MOZ_X11)
michael@0 119 return false;
michael@0 120 #endif
michael@0 121 return true;
michael@0 122 }
michael@0 123

mercurial