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: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* A class for non-null strong pointers to reference-counted objects. */ |
michael@0 | 8 | |
michael@0 | 9 | #ifndef mozilla_dom_OwningNonNull_h |
michael@0 | 10 | #define mozilla_dom_OwningNonNull_h |
michael@0 | 11 | |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace dom { |
michael@0 | 16 | |
michael@0 | 17 | template<class T> |
michael@0 | 18 | class OwningNonNull |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | OwningNonNull() |
michael@0 | 22 | #ifdef DEBUG |
michael@0 | 23 | : mInited(false) |
michael@0 | 24 | #endif |
michael@0 | 25 | {} |
michael@0 | 26 | |
michael@0 | 27 | operator T&() |
michael@0 | 28 | { |
michael@0 | 29 | MOZ_ASSERT(mInited); |
michael@0 | 30 | MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null"); |
michael@0 | 31 | return *mPtr; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | operator T*() |
michael@0 | 35 | { |
michael@0 | 36 | MOZ_ASSERT(mInited); |
michael@0 | 37 | MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null"); |
michael@0 | 38 | return mPtr; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | void operator=(T* aValue) |
michael@0 | 42 | { |
michael@0 | 43 | init(aValue); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | void operator=(const already_AddRefed<T>& aValue) |
michael@0 | 47 | { |
michael@0 | 48 | init(aValue); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | already_AddRefed<T> forget() |
michael@0 | 52 | { |
michael@0 | 53 | #ifdef DEBUG |
michael@0 | 54 | mInited = false; |
michael@0 | 55 | #endif |
michael@0 | 56 | return mPtr.forget(); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | // Make us work with smart pointer helpers that expect a get(). |
michael@0 | 60 | T* get() const |
michael@0 | 61 | { |
michael@0 | 62 | MOZ_ASSERT(mInited); |
michael@0 | 63 | MOZ_ASSERT(mPtr); |
michael@0 | 64 | return mPtr; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | protected: |
michael@0 | 68 | template<typename U> |
michael@0 | 69 | void init(U aValue) |
michael@0 | 70 | { |
michael@0 | 71 | mPtr = aValue; |
michael@0 | 72 | MOZ_ASSERT(mPtr); |
michael@0 | 73 | #ifdef DEBUG |
michael@0 | 74 | mInited = true; |
michael@0 | 75 | #endif |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | nsRefPtr<T> mPtr; |
michael@0 | 79 | #ifdef DEBUG |
michael@0 | 80 | bool mInited; |
michael@0 | 81 | #endif |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | } // namespace dom |
michael@0 | 85 | } // namespace mozilla |
michael@0 | 86 | |
michael@0 | 87 | #endif // mozilla_dom_OwningNonNull_h |