From fd513ff83fcb88329025e82025b8dd27b0ac8c33 Mon Sep 17 00:00:00 2001 From: Georg Koppen Date: Mon, 4 Mar 2019 08:38:22 +0000 Subject: [PATCH] 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 | 52 ++++++++++++++++++++------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/browser/app/profile/000-tor-browser.js b/browser/app/profile/000-tor-browser.js index 2a5adf07d9745..f41d6d48b6f8b 100644 --- a/browser/app/profile/000-tor-browser.js +++ b/browser/app/profile/000-tor-browser.js @@ -61,7 +61,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 2429ef427b8a5..030d1cfdeb93c 100644 --- a/dom/media/MediaCache.cpp +++ b/dom/media/MediaCache.cpp @@ -159,7 +159,10 @@ 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 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 GetMediaCache(int64_t aContentLength, + bool aIsPrivateBrowsing); nsIEventTarget* OwnerThread() const { return sThread; } @@ -759,8 +762,8 @@ void MediaCache::CloseStreamsForPrivateBrowsing() { })); } -/* static */ -RefPtr MediaCache::GetMediaCache(int64_t aContentLength) { +/* static */ RefPtr MediaCache::GetMediaCache( + int64_t aContentLength, bool aIsPrivateBrowsing) { NS_ASSERTION(NS_IsMainThread(), "Only call on main thread"); if (!sThreadInit) { @@ -788,11 +791,33 @@ RefPtr MediaCache::GetMediaCache(int64_t aContentLength) { return nullptr; } - if (aContentLength > 0 && - aContentLength <= - int64_t(StaticPrefs::MediaMemoryCacheMaxSize()) * 1024) { - // Small-enough resource, use a new memory-backed MediaCache. - RefPtr bc = new MemoryBlockCache(aContentLength); + if (aIsPrivateBrowsing || + (aContentLength > 0 && + aContentLength <= + int64_t(StaticPrefs::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(StaticPrefs::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(StaticPrefs::MediaMemoryCacheMaxSize()) * 1024); + } + } + + RefPtr bc = new MemoryBlockCache(cacheSize); nsresult rv = bc->Init(); if (NS_SUCCEEDED(rv)) { RefPtr mc = new MediaCache(bc); @@ -800,8 +825,13 @@ RefPtr MediaCache::GetMediaCache(int64_t aContentLength) { 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) { @@ -2657,7 +2687,7 @@ nsresult MediaCacheStream::Init(int64_t aContentLength) { mStreamLength = aContentLength; } - mMediaCache = MediaCache::GetMediaCache(aContentLength); + mMediaCache = MediaCache::GetMediaCache(aContentLength, mIsPrivateBrowsing); if (!mMediaCache) { return NS_ERROR_FAILURE; } -- GitLab