commit b7c1e2820087e60d019b57d25042a27dc83adc4a
Author: Georg Koppen <gk(a)torproject.org>
Date: Mon Mar 4 08:38:22 2019 +0000
Bug 29120: Enable media cache in memory
Back then in bug 10237 we set `media.cache_size` to `0` in the attempt
to block disk caching if we are in Private Browsing Mode. However, it
turns out that is not enough to prevent writing to disk in that case
and, moreover, is the reason for poor video performance in some cases.
The patch in this commit addresses both issues by enabling the media
cache again and making sure it stays memory-only in Private Browsing
Mode.
Big thanks to a cypherpunk for analyzing the bug and providing the
patch.
---
browser/app/profile/000-tor-browser.js | 3 ++-
dom/media/MediaCache.cpp | 45 +++++++++++++++++++++++++++-------
2 files changed, 38 insertions(+), 10 deletions(-)
diff --git a/browser/app/profile/000-tor-browser.js b/browser/app/profile/000-tor-browser.js
index 9f1e727ce724..a0b370c73279 100644
--- a/browser/app/profile/000-tor-browser.js
+++ b/browser/app/profile/000-tor-browser.js
@@ -60,7 +60,8 @@ pref("signon.rememberSignons", false);
pref("browser.formfill.enable", false);
pref("signon.autofillForms", false);
pref("browser.sessionstore.privacy_level", 2);
-pref("media.cache_size", 0);
+// Increase the maximum memory-backed cache size (#29120)
+pref("media.memory_cache_max_size", 16384);
// Misc privacy: Remote
pref("browser.send_pings", false);
diff --git a/dom/media/MediaCache.cpp b/dom/media/MediaCache.cpp
index 38197de3e4a6..056527e7926c 100644
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -154,7 +154,9 @@ class MediaCache {
// If the length is known and considered small enough, a discrete MediaCache
// with memory backing will be given. Otherwise the one MediaCache with
// file backing will be provided.
- static RefPtr<MediaCache> GetMediaCache(int64_t aContentLength);
+ // If aIsPrivateBrowsing is true, only initialization of a memory backed
+ // MediaCache will be attempted, returning nullptr if that fails.
+ static RefPtr<MediaCache> GetMediaCache(int64_t aContentLength, bool aIsPrivateBrowsing);
nsIEventTarget* OwnerThread() const { return sThread; }
@@ -706,7 +708,7 @@ void MediaCache::CloseStreamsForPrivateBrowsing() {
}
/* static */ RefPtr<MediaCache> MediaCache::GetMediaCache(
- int64_t aContentLength) {
+ int64_t aContentLength, bool aIsPrivateBrowsing) {
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
if (!sThreadInit) {
@@ -734,10 +736,30 @@ void MediaCache::CloseStreamsForPrivateBrowsing() {
return nullptr;
}
- if (aContentLength > 0 &&
- aContentLength <= int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024) {
- // Small-enough resource, use a new memory-backed MediaCache.
- RefPtr<MediaBlockCacheBase> bc = new MemoryBlockCache(aContentLength);
+ if (aIsPrivateBrowsing ||
+ (aContentLength > 0 &&
+ aContentLength <= int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024)) {
+ // We're either in private browsing mode or we have a
+ // small-enough resource, use a new memory-backed MediaCache.
+
+ int64_t cacheSize = 0;
+ if (!aIsPrivateBrowsing) {
+ cacheSize = aContentLength;
+ } else {
+ // We're in private browsing mode, we'll have to figure out a
+ // cache size since we can't fallback to a file backed MediaCache.
+ if (aContentLength < 0) {
+ // Unknown content length, we'll give the maximum allowed
+ // cache size just to be sure.
+ cacheSize = int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024;
+ } else {
+ // If the content length is less than the maximum allowed cache size,
+ // use that, otherwise we cap it.
+ cacheSize = std::min(aContentLength, int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024);
+ }
+ }
+
+ RefPtr<MediaBlockCacheBase> bc = new MemoryBlockCache(cacheSize);
nsresult rv = bc->Init();
if (NS_SUCCEEDED(rv)) {
RefPtr<MediaCache> mc = new MediaCache(bc);
@@ -745,8 +767,13 @@ void MediaCache::CloseStreamsForPrivateBrowsing() {
mc.get());
return mc;
}
- // MemoryBlockCache initialization failed, clean up and try for a
- // file-backed MediaCache below.
+
+ // MemoryBlockCache initialization failed.
+ // If we're in private browsing mode, we will bail here.
+ // Otherwise, clean up and try for a file-backed MediaCache below.
+ if (aIsPrivateBrowsing) {
+ return nullptr;
+ }
}
if (gMediaCache) {
@@ -2600,7 +2627,7 @@ nsresult MediaCacheStream::Init(int64_t aContentLength) {
mStreamLength = aContentLength;
}
- mMediaCache = MediaCache::GetMediaCache(aContentLength);
+ mMediaCache = MediaCache::GetMediaCache(aContentLength, mIsPrivateBrowsing);
if (!mMediaCache) {
return NS_ERROR_FAILURE;
}