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

mercurial