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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=8 et : |
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 | #ifndef mozilla_X11Util_h |
michael@0 | 9 | #define mozilla_X11Util_h |
michael@0 | 10 | |
michael@0 | 11 | // Utilities common to all X clients, regardless of UI toolkit. |
michael@0 | 12 | |
michael@0 | 13 | #if defined(MOZ_WIDGET_GTK) |
michael@0 | 14 | # include <gdk/gdk.h> |
michael@0 | 15 | # include <gdk/gdkx.h> |
michael@0 | 16 | #elif defined(MOZ_WIDGET_QT) |
michael@0 | 17 | #include "gfxQtPlatform.h" |
michael@0 | 18 | #undef CursorShape |
michael@0 | 19 | # include <X11/Xlib.h> |
michael@0 | 20 | #else |
michael@0 | 21 | # error Unknown toolkit |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | #include <string.h> // for memset |
michael@0 | 25 | #include "gfxCore.h" // for NS_GFX |
michael@0 | 26 | #include "mozilla/Scoped.h" // for SCOPED_TEMPLATE |
michael@0 | 27 | |
michael@0 | 28 | namespace mozilla { |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * Return the default X Display created and used by the UI toolkit. |
michael@0 | 32 | */ |
michael@0 | 33 | inline Display* |
michael@0 | 34 | DefaultXDisplay() |
michael@0 | 35 | { |
michael@0 | 36 | #if defined(MOZ_WIDGET_GTK) |
michael@0 | 37 | return GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); |
michael@0 | 38 | #elif defined(MOZ_WIDGET_QT) |
michael@0 | 39 | return gfxQtPlatform::GetXDisplay(); |
michael@0 | 40 | #endif |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * Sets *aVisual to point to aDisplay's Visual struct corresponding to |
michael@0 | 45 | * aVisualID, and *aDepth to its depth. When aVisualID is None, these are set |
michael@0 | 46 | * to nullptr and 0 respectively. Both out-parameter pointers are assumed |
michael@0 | 47 | * non-nullptr. |
michael@0 | 48 | */ |
michael@0 | 49 | void |
michael@0 | 50 | FindVisualAndDepth(Display* aDisplay, VisualID aVisualID, |
michael@0 | 51 | Visual** aVisual, int* aDepth); |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Ensure that all X requests have been processed. |
michael@0 | 56 | * |
michael@0 | 57 | * This is similar to XSync, but doesn't need a round trip if the previous |
michael@0 | 58 | * request was synchronous or if events have been received since the last |
michael@0 | 59 | * request. Subsequent FinishX calls will be noops if there have been no |
michael@0 | 60 | * intermediate requests. |
michael@0 | 61 | */ |
michael@0 | 62 | |
michael@0 | 63 | void |
michael@0 | 64 | FinishX(Display* aDisplay); |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Invoke XFree() on a pointer to memory allocated by Xlib (if the |
michael@0 | 68 | * pointer is nonnull) when this class goes out of scope. |
michael@0 | 69 | */ |
michael@0 | 70 | template <typename T> |
michael@0 | 71 | struct ScopedXFreePtrTraits |
michael@0 | 72 | { |
michael@0 | 73 | typedef T *type; |
michael@0 | 74 | static T *empty() { return nullptr; } |
michael@0 | 75 | static void release(T *ptr) { if (ptr != nullptr) XFree(ptr); } |
michael@0 | 76 | }; |
michael@0 | 77 | SCOPED_TEMPLATE(ScopedXFree, ScopedXFreePtrTraits) |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * On construction, set a graceful X error handler that doesn't crash the application and records X errors. |
michael@0 | 81 | * On destruction, restore the X error handler to what it was before construction. |
michael@0 | 82 | * |
michael@0 | 83 | * The SyncAndGetError() method allows to know whether a X error occurred, optionally allows to get the full XErrorEvent, |
michael@0 | 84 | * and resets the recorded X error state so that a single X error will be reported only once. |
michael@0 | 85 | * |
michael@0 | 86 | * Nesting is correctly handled: multiple nested ScopedXErrorHandler's don't interfere with each other's state. However, |
michael@0 | 87 | * if SyncAndGetError is not called on the nested ScopedXErrorHandler, then any X errors caused by X calls made while the nested |
michael@0 | 88 | * ScopedXErrorHandler was in place may then be caught by the other ScopedXErrorHandler. This is just a result of X being |
michael@0 | 89 | * asynchronous and us not doing any implicit syncing: the only method in this class what causes syncing is SyncAndGetError(). |
michael@0 | 90 | * |
michael@0 | 91 | * This class is not thread-safe at all. It is assumed that only one thread is using any ScopedXErrorHandler's. Given that it's |
michael@0 | 92 | * not used on Mac, it should be easy to make it thread-safe by using thread-local storage with __thread. |
michael@0 | 93 | */ |
michael@0 | 94 | class NS_GFX ScopedXErrorHandler |
michael@0 | 95 | { |
michael@0 | 96 | public: |
michael@0 | 97 | // trivial wrapper around XErrorEvent, just adding ctor initializing by zero. |
michael@0 | 98 | struct ErrorEvent |
michael@0 | 99 | { |
michael@0 | 100 | XErrorEvent mError; |
michael@0 | 101 | |
michael@0 | 102 | ErrorEvent() |
michael@0 | 103 | { |
michael@0 | 104 | memset(this, 0, sizeof(ErrorEvent)); |
michael@0 | 105 | } |
michael@0 | 106 | }; |
michael@0 | 107 | |
michael@0 | 108 | private: |
michael@0 | 109 | |
michael@0 | 110 | // this ScopedXErrorHandler's ErrorEvent object |
michael@0 | 111 | ErrorEvent mXError; |
michael@0 | 112 | |
michael@0 | 113 | // static pointer for use by the error handler |
michael@0 | 114 | static ErrorEvent* sXErrorPtr; |
michael@0 | 115 | |
michael@0 | 116 | // what to restore sXErrorPtr to on destruction |
michael@0 | 117 | ErrorEvent* mOldXErrorPtr; |
michael@0 | 118 | |
michael@0 | 119 | // what to restore the error handler to on destruction |
michael@0 | 120 | int (*mOldErrorHandler)(Display *, XErrorEvent *); |
michael@0 | 121 | |
michael@0 | 122 | public: |
michael@0 | 123 | |
michael@0 | 124 | static int |
michael@0 | 125 | ErrorHandler(Display *, XErrorEvent *ev); |
michael@0 | 126 | |
michael@0 | 127 | ScopedXErrorHandler(); |
michael@0 | 128 | |
michael@0 | 129 | ~ScopedXErrorHandler(); |
michael@0 | 130 | |
michael@0 | 131 | /** \returns true if a X error occurred since the last time this method was called on this ScopedXErrorHandler object, |
michael@0 | 132 | * or since the creation of this ScopedXErrorHandler object if this method was never called on it. |
michael@0 | 133 | * |
michael@0 | 134 | * \param ev this optional parameter, if set, will be filled with the XErrorEvent object. If multiple errors occurred, |
michael@0 | 135 | * the first one will be returned. |
michael@0 | 136 | */ |
michael@0 | 137 | bool SyncAndGetError(Display *dpy, XErrorEvent *ev = nullptr); |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | } // namespace mozilla |
michael@0 | 141 | |
michael@0 | 142 | #endif // mozilla_X11Util_h |