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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsISupportsUtils_h__ |
michael@0 | 7 | #define nsISupportsUtils_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nscore.h" |
michael@0 | 10 | #include "nsISupportsBase.h" |
michael@0 | 11 | #include "nsError.h" |
michael@0 | 12 | #include "nsDebug.h" |
michael@0 | 13 | #include "nsISupportsImpl.h" |
michael@0 | 14 | #include "mozilla/TypeTraits.h" |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Macro for adding a reference to an interface. |
michael@0 | 18 | * @param _ptr The interface pointer. |
michael@0 | 19 | */ |
michael@0 | 20 | #define NS_ADDREF(_ptr) \ |
michael@0 | 21 | (_ptr)->AddRef() |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * Macro for adding a reference to this. This macro should be used |
michael@0 | 25 | * because NS_ADDREF (when tracing) may require an ambiguous cast |
michael@0 | 26 | * from the pointers primary type to nsISupports. This macro sidesteps |
michael@0 | 27 | * that entire problem. |
michael@0 | 28 | */ |
michael@0 | 29 | #define NS_ADDREF_THIS() \ |
michael@0 | 30 | AddRef() |
michael@0 | 31 | |
michael@0 | 32 | |
michael@0 | 33 | extern "C++" { |
michael@0 | 34 | // ...because some one is accidentally including this file inside |
michael@0 | 35 | // an |extern "C"| |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | // Making this a |inline| |template| allows |expr| to be evaluated only once, |
michael@0 | 39 | // yet still denies you the ability to |AddRef()| an |nsCOMPtr|. |
michael@0 | 40 | template <class T> |
michael@0 | 41 | inline |
michael@0 | 42 | void |
michael@0 | 43 | ns_if_addref( T expr ) |
michael@0 | 44 | { |
michael@0 | 45 | if (expr) { |
michael@0 | 46 | expr->AddRef(); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | } /* extern "C++" */ |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Macro for adding a reference to an interface that checks for nullptr. |
michael@0 | 54 | * @param _expr The interface pointer. |
michael@0 | 55 | */ |
michael@0 | 56 | #define NS_IF_ADDREF(_expr) ns_if_addref(_expr) |
michael@0 | 57 | |
michael@0 | 58 | /* |
michael@0 | 59 | * Given these declarations, it explicitly OK and efficient to end a `getter' with: |
michael@0 | 60 | * |
michael@0 | 61 | * NS_IF_ADDREF(*result = mThing); |
michael@0 | 62 | * |
michael@0 | 63 | * even if |mThing| is an |nsCOMPtr|. If |mThing| is an |nsCOMPtr|, however, it is still |
michael@0 | 64 | * _illegal_ to say |NS_IF_ADDREF(mThing)|. |
michael@0 | 65 | */ |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Macro for releasing a reference to an interface. |
michael@0 | 69 | * @param _ptr The interface pointer. |
michael@0 | 70 | */ |
michael@0 | 71 | #define NS_RELEASE(_ptr) \ |
michael@0 | 72 | do { \ |
michael@0 | 73 | (_ptr)->Release(); \ |
michael@0 | 74 | (_ptr) = 0; \ |
michael@0 | 75 | } while (0) |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * Macro for releasing a reference to this interface. |
michael@0 | 79 | */ |
michael@0 | 80 | #define NS_RELEASE_THIS() \ |
michael@0 | 81 | Release() |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Macro for releasing a reference to an interface, except that this |
michael@0 | 85 | * macro preserves the return value from the underlying Release call. |
michael@0 | 86 | * The interface pointer argument will only be NULLed if the reference count |
michael@0 | 87 | * goes to zero. |
michael@0 | 88 | * |
michael@0 | 89 | * @param _ptr The interface pointer. |
michael@0 | 90 | * @param _rc The reference count. |
michael@0 | 91 | */ |
michael@0 | 92 | #define NS_RELEASE2(_ptr, _rc) \ |
michael@0 | 93 | do { \ |
michael@0 | 94 | _rc = (_ptr)->Release(); \ |
michael@0 | 95 | if (0 == (_rc)) (_ptr) = 0; \ |
michael@0 | 96 | } while (0) |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * Macro for releasing a reference to an interface that checks for nullptr; |
michael@0 | 100 | * @param _ptr The interface pointer. |
michael@0 | 101 | */ |
michael@0 | 102 | #define NS_IF_RELEASE(_ptr) \ |
michael@0 | 103 | do { \ |
michael@0 | 104 | if (_ptr) { \ |
michael@0 | 105 | (_ptr)->Release(); \ |
michael@0 | 106 | (_ptr) = 0; \ |
michael@0 | 107 | } \ |
michael@0 | 108 | } while (0) |
michael@0 | 109 | |
michael@0 | 110 | /* |
michael@0 | 111 | * Often you have to cast an implementation pointer, e.g., |this|, to an |
michael@0 | 112 | * |nsISupports*|, but because you have multiple inheritance, a simple cast |
michael@0 | 113 | * is ambiguous. One could simply say, e.g., (given a base |nsIBase|), |
michael@0 | 114 | * |static_cast<nsIBase*>(this)|; but that disguises the fact that what |
michael@0 | 115 | * you are really doing is disambiguating the |nsISupports|. You could make |
michael@0 | 116 | * that more obvious with a double cast, e.g., |static_cast<nsISupports*> |
michael@0 | 117 | (* static_cast<nsIBase*>(this))|, but that is bulky and harder to read... |
michael@0 | 118 | * |
michael@0 | 119 | * The following macro is clean, short, and obvious. In the example above, |
michael@0 | 120 | * you would use it like this: |NS_ISUPPORTS_CAST(nsIBase*, this)|. |
michael@0 | 121 | */ |
michael@0 | 122 | |
michael@0 | 123 | #define NS_ISUPPORTS_CAST(__unambiguousBase, __expr) \ |
michael@0 | 124 | static_cast<nsISupports*>(static_cast<__unambiguousBase>(__expr)) |
michael@0 | 125 | |
michael@0 | 126 | // a type-safe shortcut for calling the |QueryInterface()| member function |
michael@0 | 127 | template <class T, class DestinationType> |
michael@0 | 128 | inline |
michael@0 | 129 | nsresult |
michael@0 | 130 | CallQueryInterface( T* aSource, DestinationType** aDestination ) |
michael@0 | 131 | { |
michael@0 | 132 | // We permit nsISupports-to-nsISupports here so that one can still obtain |
michael@0 | 133 | // the canonical nsISupports pointer with CallQueryInterface. |
michael@0 | 134 | static_assert(!mozilla::IsSame<T, DestinationType>::value || |
michael@0 | 135 | mozilla::IsSame<DestinationType, nsISupports>::value, |
michael@0 | 136 | "don't use CallQueryInterface for compile-time-determinable casts"); |
michael@0 | 137 | |
michael@0 | 138 | NS_PRECONDITION(aSource, "null parameter"); |
michael@0 | 139 | NS_PRECONDITION(aDestination, "null parameter"); |
michael@0 | 140 | |
michael@0 | 141 | return aSource->QueryInterface(NS_GET_TEMPLATE_IID(DestinationType), |
michael@0 | 142 | reinterpret_cast<void**>(aDestination)); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | #endif /* __nsISupportsUtils_h */ |