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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
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 | #include "MediaShutdownManager.h" |
michael@0 | 8 | #include "nsContentUtils.h" |
michael@0 | 9 | #include "mozilla/StaticPtr.h" |
michael@0 | 10 | #include "MediaDecoder.h" |
michael@0 | 11 | #include "SharedThreadPool.h" |
michael@0 | 12 | #include "prlog.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | |
michael@0 | 16 | #ifdef PR_LOGGING |
michael@0 | 17 | extern PRLogModuleInfo* gMediaDecoderLog; |
michael@0 | 18 | #define DECODER_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg) |
michael@0 | 19 | #else |
michael@0 | 20 | #define DECODER_LOG(type, msg) |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | NS_IMPL_ISUPPORTS(MediaShutdownManager, nsIObserver) |
michael@0 | 24 | |
michael@0 | 25 | MediaShutdownManager::MediaShutdownManager() |
michael@0 | 26 | : mIsObservingShutdown(false), |
michael@0 | 27 | mIsDoingXPCOMShutDown(false) |
michael@0 | 28 | { |
michael@0 | 29 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 30 | MOZ_COUNT_CTOR(MediaShutdownManager); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | MediaShutdownManager::~MediaShutdownManager() |
michael@0 | 34 | { |
michael@0 | 35 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 36 | MOZ_COUNT_DTOR(MediaShutdownManager); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | // Note that we don't use ClearOnShutdown() on this StaticRefPtr, as that |
michael@0 | 40 | // may interfere with our shutdown listener. |
michael@0 | 41 | StaticRefPtr<MediaShutdownManager> MediaShutdownManager::sInstance; |
michael@0 | 42 | |
michael@0 | 43 | MediaShutdownManager& |
michael@0 | 44 | MediaShutdownManager::Instance() |
michael@0 | 45 | { |
michael@0 | 46 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 47 | if (!sInstance) { |
michael@0 | 48 | sInstance = new MediaShutdownManager(); |
michael@0 | 49 | } |
michael@0 | 50 | return *sInstance; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void |
michael@0 | 54 | MediaShutdownManager::EnsureCorrectShutdownObserverState() |
michael@0 | 55 | { |
michael@0 | 56 | MOZ_ASSERT(!mIsDoingXPCOMShutDown); |
michael@0 | 57 | bool needShutdownObserver = mDecoders.Count() > 0; |
michael@0 | 58 | if (needShutdownObserver != mIsObservingShutdown) { |
michael@0 | 59 | mIsObservingShutdown = needShutdownObserver; |
michael@0 | 60 | if (mIsObservingShutdown) { |
michael@0 | 61 | nsContentUtils::RegisterShutdownObserver(this); |
michael@0 | 62 | } else { |
michael@0 | 63 | nsContentUtils::UnregisterShutdownObserver(this); |
michael@0 | 64 | // Clear our singleton reference. This will probably delete |
michael@0 | 65 | // this instance, so don't deref |this| clearing sInstance. |
michael@0 | 66 | sInstance = nullptr; |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | void |
michael@0 | 72 | MediaShutdownManager::Register(MediaDecoder* aDecoder) |
michael@0 | 73 | { |
michael@0 | 74 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 75 | // Don't call Register() after you've Unregistered() all the decoders, |
michael@0 | 76 | // that's not going to work. |
michael@0 | 77 | MOZ_ASSERT(!mDecoders.Contains(aDecoder)); |
michael@0 | 78 | mDecoders.PutEntry(aDecoder); |
michael@0 | 79 | MOZ_ASSERT(mDecoders.Contains(aDecoder)); |
michael@0 | 80 | MOZ_ASSERT(mDecoders.Count() > 0); |
michael@0 | 81 | EnsureCorrectShutdownObserverState(); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | void |
michael@0 | 85 | MediaShutdownManager::Unregister(MediaDecoder* aDecoder) |
michael@0 | 86 | { |
michael@0 | 87 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 88 | MOZ_ASSERT(mDecoders.Contains(aDecoder)); |
michael@0 | 89 | if (!mIsDoingXPCOMShutDown) { |
michael@0 | 90 | mDecoders.RemoveEntry(aDecoder); |
michael@0 | 91 | EnsureCorrectShutdownObserverState(); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | NS_IMETHODIMP |
michael@0 | 96 | MediaShutdownManager::Observe(nsISupports *aSubjet, |
michael@0 | 97 | const char *aTopic, |
michael@0 | 98 | const char16_t *someData) |
michael@0 | 99 | { |
michael@0 | 100 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 101 | if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) { |
michael@0 | 102 | Shutdown(); |
michael@0 | 103 | } |
michael@0 | 104 | return NS_OK; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | static PLDHashOperator |
michael@0 | 108 | ShutdownMediaDecoder(nsRefPtrHashKey<MediaDecoder>* aEntry, void*) |
michael@0 | 109 | { |
michael@0 | 110 | aEntry->GetKey()->Shutdown(); |
michael@0 | 111 | return PL_DHASH_REMOVE; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | void |
michael@0 | 115 | MediaShutdownManager::Shutdown() |
michael@0 | 116 | { |
michael@0 | 117 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 118 | MOZ_ASSERT(sInstance); |
michael@0 | 119 | |
michael@0 | 120 | DECODER_LOG(PR_LOG_DEBUG, ("MediaShutdownManager::Shutdown() start...")); |
michael@0 | 121 | |
michael@0 | 122 | // Mark that we're shutting down, so that Unregister(*) calls don't remove |
michael@0 | 123 | // hashtable entries. If Unregsiter(*) was to remove from the hash table, |
michael@0 | 124 | // the iterations over the hashtables below would be disrupted. |
michael@0 | 125 | mIsDoingXPCOMShutDown = true; |
michael@0 | 126 | |
michael@0 | 127 | // Iterate over the decoders and shut them down, and remove them from the |
michael@0 | 128 | // hashtable. |
michael@0 | 129 | mDecoders.EnumerateEntries(ShutdownMediaDecoder, nullptr); |
michael@0 | 130 | |
michael@0 | 131 | // Ensure all media shared thread pools are shutdown. This joins with all |
michael@0 | 132 | // threads in the state machine thread pool, the decoder thread pool, and |
michael@0 | 133 | // any others. |
michael@0 | 134 | SharedThreadPool::SpinUntilShutdown(); |
michael@0 | 135 | |
michael@0 | 136 | // Remove the MediaShutdownManager instance from the shutdown observer |
michael@0 | 137 | // list. |
michael@0 | 138 | nsContentUtils::UnregisterShutdownObserver(this); |
michael@0 | 139 | |
michael@0 | 140 | // Clear the singleton instance. The only remaining reference should be the |
michael@0 | 141 | // reference that the observer service used to call us with. The |
michael@0 | 142 | // MediaShutdownManager will be deleted once the observer service cleans |
michael@0 | 143 | // up after it finishes its notifications. |
michael@0 | 144 | sInstance = nullptr; |
michael@0 | 145 | |
michael@0 | 146 | DECODER_LOG(PR_LOG_DEBUG, ("MediaShutdownManager::Shutdown() end.")); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | } // namespace mozilla |