Tue, 06 Jan 2015 21:39:09 +0100
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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
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 #include "X11Util.h"
9 #include "nsDebug.h" // for NS_ASSERTION, etc
11 namespace mozilla {
13 void
14 FindVisualAndDepth(Display* aDisplay, VisualID aVisualID,
15 Visual** aVisual, int* aDepth)
16 {
17 const Screen* screen = DefaultScreenOfDisplay(aDisplay);
19 for (int d = 0; d < screen->ndepths; d++) {
20 Depth *d_info = &screen->depths[d];
21 for (int v = 0; v < d_info->nvisuals; v++) {
22 Visual* visual = &d_info->visuals[v];
23 if (visual->visualid == aVisualID) {
24 *aVisual = visual;
25 *aDepth = d_info->depth;
26 return;
27 }
28 }
29 }
31 NS_ASSERTION(aVisualID == None, "VisualID not on Screen.");
32 *aVisual = nullptr;
33 *aDepth = 0;
34 return;
35 }
37 void
38 FinishX(Display* aDisplay)
39 {
40 unsigned long lastRequest = NextRequest(aDisplay) - 1;
41 if (lastRequest == LastKnownRequestProcessed(aDisplay))
42 return;
44 XSync(aDisplay, False);
45 }
47 ScopedXErrorHandler::ErrorEvent* ScopedXErrorHandler::sXErrorPtr;
49 int
50 ScopedXErrorHandler::ErrorHandler(Display *, XErrorEvent *ev)
51 {
52 // only record the error if no error was previously recorded.
53 // this means that in case of multiple errors, it's the first error that we report.
54 if (!sXErrorPtr->mError.error_code)
55 sXErrorPtr->mError = *ev;
56 return 0;
57 }
59 ScopedXErrorHandler::ScopedXErrorHandler()
60 {
61 // let sXErrorPtr point to this object's mXError object, but don't reset this mXError object!
62 // think of the case of nested ScopedXErrorHandler's.
63 mOldXErrorPtr = sXErrorPtr;
64 sXErrorPtr = &mXError;
65 mOldErrorHandler = XSetErrorHandler(ErrorHandler);
66 }
68 ScopedXErrorHandler::~ScopedXErrorHandler()
69 {
70 sXErrorPtr = mOldXErrorPtr;
71 XSetErrorHandler(mOldErrorHandler);
72 }
74 bool
75 ScopedXErrorHandler::SyncAndGetError(Display *dpy, XErrorEvent *ev)
76 {
77 FinishX(dpy);
79 bool retval = mXError.mError.error_code != 0;
80 if (ev)
81 *ev = mXError.mError;
82 mXError = ErrorEvent(); // reset
83 return retval;
84 }
86 } // namespace mozilla