Commit 197787a5 authored by Georg Koppen's avatar Georg Koppen
Browse files

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.
parent 3f613ce3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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);
+36 −9
Original line number Diff line number Diff line
@@ -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;
  }